1 | /* $Id: x509-file.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Crypto - X.509, File related APIs.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include "internal/iprt.h"
|
---|
32 | #include <iprt/crypto/x509.h>
|
---|
33 |
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/err.h>
|
---|
36 | #include <iprt/path.h>
|
---|
37 | #include <iprt/crypto/pem.h>
|
---|
38 |
|
---|
39 |
|
---|
40 | /*********************************************************************************************************************************
|
---|
41 | * Global Variables *
|
---|
42 | *********************************************************************************************************************************/
|
---|
43 | static RTCRPEMMARKERWORD const g_aWords_Certificate[] = { { RT_STR_TUPLE("CERTIFICATE") } };
|
---|
44 | /** X509 Certificate markers. */
|
---|
45 | static RTCRPEMMARKER const g_aCertificateMarkers[] = { { g_aWords_Certificate, RT_ELEMENTS(g_aWords_Certificate) } };
|
---|
46 |
|
---|
47 |
|
---|
48 | RTDECL(int) RTCrX509Certificate_ReadFromFile(PRTCRX509CERTIFICATE pCertificate, const char *pszFilename, uint32_t fFlags,
|
---|
49 | PCRTASN1ALLOCATORVTABLE pAllocator, PRTERRINFO pErrInfo)
|
---|
50 | {
|
---|
51 | AssertReturn(!fFlags, VERR_INVALID_FLAGS);
|
---|
52 | PCRTCRPEMSECTION pSectionHead;
|
---|
53 | int rc = RTCrPemReadFile(pszFilename, 0, g_aCertificateMarkers, RT_ELEMENTS(g_aCertificateMarkers), &pSectionHead, pErrInfo);
|
---|
54 | if (RT_SUCCESS(rc))
|
---|
55 | {
|
---|
56 | RTCRX509CERTIFICATE TmpCert;
|
---|
57 | RTASN1CURSORPRIMARY PrimaryCursor;
|
---|
58 | RTAsn1CursorInitPrimary(&PrimaryCursor, pSectionHead->pbData, (uint32_t)RT_MIN(pSectionHead->cbData, UINT32_MAX),
|
---|
59 | pErrInfo, pAllocator, RTASN1CURSOR_FLAGS_DER, RTPathFilename(pszFilename));
|
---|
60 | rc = RTCrX509Certificate_DecodeAsn1(&PrimaryCursor.Cursor, 0, &TmpCert, "Cert");
|
---|
61 | if (RT_SUCCESS(rc))
|
---|
62 | {
|
---|
63 | rc = RTCrX509Certificate_CheckSanity(&TmpCert, 0, pErrInfo, "Cert");
|
---|
64 | if (RT_SUCCESS(rc))
|
---|
65 | {
|
---|
66 | rc = RTCrX509Certificate_Clone(pCertificate, &TmpCert, &g_RTAsn1DefaultAllocator);
|
---|
67 | if (RT_SUCCESS(rc))
|
---|
68 | {
|
---|
69 | if (pSectionHead->pNext || PrimaryCursor.Cursor.cbLeft)
|
---|
70 | rc = VINF_ASN1_MORE_DATA;
|
---|
71 | }
|
---|
72 | }
|
---|
73 | RTCrX509Certificate_Delete(&TmpCert);
|
---|
74 | }
|
---|
75 | RTCrPemFreeSections(pSectionHead);
|
---|
76 | }
|
---|
77 | return rc;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 |
|
---|
82 | #if 0
|
---|
83 | RTDECL(int) RTCrX509Certificates_ReadFromFile(const char *pszFilename, uint32_t fFlags,
|
---|
84 | PRTCRX509CERTIFICATES pCertificates, PRTERRINFO pErrInfo)
|
---|
85 | {
|
---|
86 | AssertReturn(!fFlags, VERR_INVALID_FLAGS);
|
---|
87 | PCRTCRPEMSECTION pSectionHead;
|
---|
88 | int rc = RTCrPemReadFile(pszFilename, 0, g_aCertificateMarkers, RT_ELEMENTS(g_aCertificateMarkers), &pSectionHead, pErrInfo);
|
---|
89 | if (RT_SUCCESS(rc))
|
---|
90 | {
|
---|
91 | pCertificates->Allocation
|
---|
92 |
|
---|
93 | PCRTCRPEMSECTION pCurSec = pSectionHead;
|
---|
94 | while (pCurSec)
|
---|
95 | {
|
---|
96 |
|
---|
97 | pCurSec = pCurSec->pNext;
|
---|
98 | }
|
---|
99 |
|
---|
100 | RTCrPemFreeSections(pSectionHead);
|
---|
101 | }
|
---|
102 | return rc;
|
---|
103 | }
|
---|
104 | #endif
|
---|
105 |
|
---|