1 | /* $Id: RTCrStoreCertAddFromFile.cpp 57548 2015-08-26 11:36:25Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Cryptographic (Certificate) Store, RTCrStoreCertAddFromFile.
|
---|
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/store.h>
|
---|
33 |
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/err.h>
|
---|
36 | #include <iprt/crypto/pem.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | /*********************************************************************************************************************************
|
---|
40 | * Global Variables *
|
---|
41 | *********************************************************************************************************************************/
|
---|
42 | /** BEGIN CERTIFICATE / END CERTIFICATE. */
|
---|
43 | static RTCRPEMMARKERWORD const g_aWords_Certificate[] =
|
---|
44 | {
|
---|
45 | { RT_STR_TUPLE("CERTIFICATE") }
|
---|
46 | };
|
---|
47 |
|
---|
48 | /** BEGIN TRUSTED CERTIFICATE / END TRUSTED CERTIFICATE. */
|
---|
49 | static RTCRPEMMARKERWORD const g_aWords_TrustedCertificate[] =
|
---|
50 | {
|
---|
51 | { RT_STR_TUPLE("TRUSTED") },
|
---|
52 | { RT_STR_TUPLE("CERTIFICATE") }
|
---|
53 | };
|
---|
54 |
|
---|
55 | /** BEGIN X509 CERTIFICATE / END X509 CERTIFICATE. (old) */
|
---|
56 | static RTCRPEMMARKERWORD const g_aWords_X509Certificate[] =
|
---|
57 | {
|
---|
58 | { RT_STR_TUPLE("X509") },
|
---|
59 | { RT_STR_TUPLE("CERTIFICATE") }
|
---|
60 | };
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * X509 Certificate markers.
|
---|
64 | *
|
---|
65 | * @remark See crypto/pem/pem.h in OpenSSL for a matching list.
|
---|
66 | */
|
---|
67 | static RTCRPEMMARKER const g_aCertificateMarkers[] =
|
---|
68 | {
|
---|
69 | { g_aWords_Certificate, RT_ELEMENTS(g_aWords_Certificate) },
|
---|
70 | { g_aWords_TrustedCertificate, RT_ELEMENTS(g_aWords_TrustedCertificate) },
|
---|
71 | { g_aWords_X509Certificate, RT_ELEMENTS(g_aWords_X509Certificate) }
|
---|
72 | };
|
---|
73 |
|
---|
74 |
|
---|
75 | #if 0
|
---|
76 | RTDECL(int) RTCrX509Certificates_ReadFromFile(const char *pszFilename, uint32_t fFlags,
|
---|
77 | PRTCRX509CERTIFICATES pCertificates, PRTERRINFO pErrInfo)
|
---|
78 | {
|
---|
79 | AssertReturn(!fFlags, VERR_INVALID_FLAGS);
|
---|
80 | PCRTCRPEMSECTION pSectionHead;
|
---|
81 | int rc = RTCrPemReadFile(pszFilename, 0, g_aCertificateMarkers, RT_ELEMENTS(g_aCertificateMarkers), &pSectionHead, pErrInfo);
|
---|
82 | if (RT_SUCCESS(rc))
|
---|
83 | {
|
---|
84 | pCertificates->Allocation
|
---|
85 |
|
---|
86 | PCRTCRPEMSECTION pCurSec = pSectionHead;
|
---|
87 | while (pCurSec)
|
---|
88 | {
|
---|
89 |
|
---|
90 | pCurSec = pCurSec->pNext;
|
---|
91 | }
|
---|
92 |
|
---|
93 | RTCrPemFreeSections(pSectionHead);
|
---|
94 | }
|
---|
95 | return rc;
|
---|
96 | }
|
---|
97 | #endif
|
---|
98 |
|
---|
99 |
|
---|
100 | RTDECL(int) RTCrStoreCertAddFromFile(RTCRSTORE hStore, uint32_t fFlags, const char *pszFilename, PRTERRINFO pErrInfo)
|
---|
101 | {
|
---|
102 | AssertReturn(!fFlags, VERR_INVALID_FLAGS);
|
---|
103 | #if 0
|
---|
104 | RTCRX509CERTIFICATES Certs;
|
---|
105 | int rc = RTCrX509Certificates_ReadFromFile(pszFilename, 0, &Certs, pErrInfo);
|
---|
106 | if (RT_SUCCESS(rc))
|
---|
107 | {
|
---|
108 | for (uint32_t i = 0; i < Certs.cCerts; i++)
|
---|
109 | {
|
---|
110 | int rc2 = RTCrStoreCertAddEncoded(hStore, RTCRCERTCTX_F_ENC_X509_DER,
|
---|
111 | RTASN1CORE_GET_RAW_ASN1_PTR(&Certs.paCerts[i].SeqCore.Asn1Core),
|
---|
112 | RTASN1CORE_GET_RAW_ASN1_SIZE(&Certs.paCerts[i].SeqCore.Asn1Core),
|
---|
113 | RT_SUCCESS(rc) ? pErrInfo : NULL);
|
---|
114 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
115 | rc = rc2;
|
---|
116 | }
|
---|
117 |
|
---|
118 | RTAsn1Destroy(&Certs.SetCore.Asn1Core);
|
---|
119 | }
|
---|
120 | return rc;
|
---|
121 | #else
|
---|
122 |
|
---|
123 | PCRTCRPEMSECTION pSectionHead;
|
---|
124 | int rc = RTCrPemReadFile(pszFilename, 0, g_aCertificateMarkers, RT_ELEMENTS(g_aCertificateMarkers), &pSectionHead, pErrInfo);
|
---|
125 | if (RT_SUCCESS(rc))
|
---|
126 | {
|
---|
127 | PCRTCRPEMSECTION pCurSec = pSectionHead;
|
---|
128 | while (pCurSec)
|
---|
129 | {
|
---|
130 | int rc2 = RTCrStoreCertAddEncoded(hStore, RTCRCERTCTX_F_ENC_X509_DER, pCurSec->pbData, pCurSec->cbData,
|
---|
131 | RT_SUCCESS(rc) ? pErrInfo : NULL);
|
---|
132 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
133 | rc = rc2;
|
---|
134 | pCurSec = pCurSec->pNext;
|
---|
135 | }
|
---|
136 |
|
---|
137 | RTCrPemFreeSections(pSectionHead);
|
---|
138 | }
|
---|
139 | return rc;
|
---|
140 | #endif
|
---|
141 | }
|
---|
142 |
|
---|