1 | /* $Id: x509-file.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Crypto - X.509, File related APIs.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "internal/iprt.h"
|
---|
42 | #include <iprt/crypto/x509.h>
|
---|
43 |
|
---|
44 | #include <iprt/assert.h>
|
---|
45 | #include <iprt/err.h>
|
---|
46 | #include <iprt/path.h>
|
---|
47 | #include <iprt/crypto/pem.h>
|
---|
48 |
|
---|
49 |
|
---|
50 | /*********************************************************************************************************************************
|
---|
51 | * Global Variables *
|
---|
52 | *********************************************************************************************************************************/
|
---|
53 | static RTCRPEMMARKERWORD const g_aWords_Certificate[] = { { RT_STR_TUPLE("CERTIFICATE") } };
|
---|
54 | /** X509 Certificate markers. */
|
---|
55 | RT_DECL_DATA_CONST(RTCRPEMMARKER const) g_aRTCrX509CertificateMarkers[] =
|
---|
56 | {
|
---|
57 | { g_aWords_Certificate, RT_ELEMENTS(g_aWords_Certificate) }
|
---|
58 | };
|
---|
59 | /** Number of entries in g_aRTCrX509CertificateMarkers. */
|
---|
60 | RT_DECL_DATA_CONST(uint32_t const) g_cRTCrX509CertificateMarkers = RT_ELEMENTS(g_aRTCrX509CertificateMarkers);
|
---|
61 |
|
---|
62 |
|
---|
63 | RTDECL(int) RTCrX509Certificate_ReadFromFile(PRTCRX509CERTIFICATE pCertificate, const char *pszFilename, uint32_t fFlags,
|
---|
64 | PCRTASN1ALLOCATORVTABLE pAllocator, PRTERRINFO pErrInfo)
|
---|
65 | {
|
---|
66 | AssertReturn(!(fFlags & ~RTCRX509CERT_READ_F_PEM_ONLY), VERR_INVALID_FLAGS);
|
---|
67 | PCRTCRPEMSECTION pSectionHead;
|
---|
68 | int rc = RTCrPemReadFile(pszFilename,
|
---|
69 | fFlags & RTCRX509CERT_READ_F_PEM_ONLY ? RTCRPEMREADFILE_F_ONLY_PEM : 0,
|
---|
70 | g_aRTCrX509CertificateMarkers, g_cRTCrX509CertificateMarkers,
|
---|
71 | &pSectionHead, pErrInfo);
|
---|
72 | if (RT_SUCCESS(rc))
|
---|
73 | {
|
---|
74 | if (pSectionHead)
|
---|
75 | {
|
---|
76 | RTCRX509CERTIFICATE TmpCert;
|
---|
77 | RTASN1CURSORPRIMARY PrimaryCursor;
|
---|
78 | RTAsn1CursorInitPrimary(&PrimaryCursor, pSectionHead->pbData, (uint32_t)RT_MIN(pSectionHead->cbData, UINT32_MAX),
|
---|
79 | pErrInfo, pAllocator, RTASN1CURSOR_FLAGS_DER, RTPathFilename(pszFilename));
|
---|
80 | rc = RTCrX509Certificate_DecodeAsn1(&PrimaryCursor.Cursor, 0, &TmpCert, "Cert");
|
---|
81 | if (RT_SUCCESS(rc))
|
---|
82 | {
|
---|
83 | rc = RTCrX509Certificate_CheckSanity(&TmpCert, 0, pErrInfo, "Cert");
|
---|
84 | if (RT_SUCCESS(rc))
|
---|
85 | {
|
---|
86 | rc = RTCrX509Certificate_Clone(pCertificate, &TmpCert, pAllocator);
|
---|
87 | if (RT_SUCCESS(rc))
|
---|
88 | {
|
---|
89 | if (pSectionHead->pNext || PrimaryCursor.Cursor.cbLeft)
|
---|
90 | rc = VINF_ASN1_MORE_DATA;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | RTCrX509Certificate_Delete(&TmpCert);
|
---|
94 | }
|
---|
95 | RTCrPemFreeSections(pSectionHead);
|
---|
96 | }
|
---|
97 | else
|
---|
98 | rc = rc != VINF_SUCCESS ? -rc : VERR_INTERNAL_ERROR_2;
|
---|
99 |
|
---|
100 | }
|
---|
101 | return rc;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | RTDECL(int) RTCrX509Certificate_ReadFromBuffer(PRTCRX509CERTIFICATE pCertificate, const void *pvBuf, size_t cbBuf,
|
---|
106 | uint32_t fFlags, PCRTASN1ALLOCATORVTABLE pAllocator,
|
---|
107 | PRTERRINFO pErrInfo, const char *pszErrorTag)
|
---|
108 | {
|
---|
109 | AssertReturn(!(fFlags & ~RTCRX509CERT_READ_F_PEM_ONLY), VERR_INVALID_FLAGS);
|
---|
110 | PCRTCRPEMSECTION pSectionHead;
|
---|
111 | int rc = RTCrPemParseContent(pvBuf, cbBuf,
|
---|
112 | fFlags & RTCRX509CERT_READ_F_PEM_ONLY ? RTCRPEMREADFILE_F_ONLY_PEM : 0,
|
---|
113 | g_aRTCrX509CertificateMarkers, g_cRTCrX509CertificateMarkers,
|
---|
114 | &pSectionHead, pErrInfo);
|
---|
115 | if (RT_SUCCESS(rc))
|
---|
116 | {
|
---|
117 | if (pSectionHead)
|
---|
118 | {
|
---|
119 | RTCRX509CERTIFICATE TmpCert;
|
---|
120 | RTASN1CURSORPRIMARY PrimaryCursor;
|
---|
121 | RTAsn1CursorInitPrimary(&PrimaryCursor, pSectionHead->pbData, (uint32_t)RT_MIN(pSectionHead->cbData, UINT32_MAX),
|
---|
122 | pErrInfo, pAllocator, RTASN1CURSOR_FLAGS_DER, pszErrorTag);
|
---|
123 | rc = RTCrX509Certificate_DecodeAsn1(&PrimaryCursor.Cursor, 0, &TmpCert, "Cert");
|
---|
124 | if (RT_SUCCESS(rc))
|
---|
125 | {
|
---|
126 | rc = RTCrX509Certificate_CheckSanity(&TmpCert, 0, pErrInfo, "Cert");
|
---|
127 | if (RT_SUCCESS(rc))
|
---|
128 | {
|
---|
129 | rc = RTCrX509Certificate_Clone(pCertificate, &TmpCert, pAllocator);
|
---|
130 | if (RT_SUCCESS(rc))
|
---|
131 | {
|
---|
132 | if (pSectionHead->pNext || PrimaryCursor.Cursor.cbLeft)
|
---|
133 | rc = VINF_ASN1_MORE_DATA;
|
---|
134 | }
|
---|
135 | }
|
---|
136 | RTCrX509Certificate_Delete(&TmpCert);
|
---|
137 | }
|
---|
138 | RTCrPemFreeSections(pSectionHead);
|
---|
139 | }
|
---|
140 | else
|
---|
141 | rc = rc != VINF_SUCCESS ? -rc : VERR_INTERNAL_ERROR_2;
|
---|
142 | }
|
---|
143 | return rc;
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 |
|
---|
148 | #if 0
|
---|
149 | RTDECL(int) RTCrX509Certificates_ReadFromFile(const char *pszFilename, uint32_t fFlags,
|
---|
150 | PRTCRX509CERTIFICATES pCertificates, PRTERRINFO pErrInfo)
|
---|
151 | {
|
---|
152 | AssertReturn(!(fFlags & ~RTCRX509CERT_READ_F_PEM_ONLY), VERR_INVALID_FLAGS);
|
---|
153 | PCRTCRPEMSECTION pSectionHead;
|
---|
154 | int rc = RTCrPemReadFile(pszFilename,
|
---|
155 | fFlags & RTCRX509CERT_READ_F_PEM_ONLY ? RTCRPEMREADFILE_F_ONLY_PEM : 0,
|
---|
156 | g_aRTCrX509CertificateMarkers, g_cRTCrX509CertificateMarkers,
|
---|
157 | &pSectionHead, pErrInfo);
|
---|
158 | if (RT_SUCCESS(rc))
|
---|
159 | {
|
---|
160 | pCertificates->Allocation
|
---|
161 |
|
---|
162 | PCRTCRPEMSECTION pCurSec = pSectionHead;
|
---|
163 | while (pCurSec)
|
---|
164 | {
|
---|
165 |
|
---|
166 | pCurSec = pCurSec->pNext;
|
---|
167 | }
|
---|
168 |
|
---|
169 | RTCrPemFreeSections(pSectionHead);
|
---|
170 | }
|
---|
171 | return rc;
|
---|
172 | }
|
---|
173 | #endif
|
---|
174 |
|
---|