[51770] | 1 | /* $Id: x509-file.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * IPRT - Crypto - X.509, File related APIs.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[51770] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[51770] | 11 | *
|
---|
[96407] | 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 | *
|
---|
[51770] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 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
|
---|
[51770] | 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.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[51770] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[51770] | 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 |
|
---|
[57358] | 50 | /*********************************************************************************************************************************
|
---|
| 51 | * Global Variables *
|
---|
| 52 | *********************************************************************************************************************************/
|
---|
[51770] | 53 | static RTCRPEMMARKERWORD const g_aWords_Certificate[] = { { RT_STR_TUPLE("CERTIFICATE") } };
|
---|
| 54 | /** X509 Certificate markers. */
|
---|
[59625] | 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);
|
---|
[51770] | 61 |
|
---|
| 62 |
|
---|
| 63 | RTDECL(int) RTCrX509Certificate_ReadFromFile(PRTCRX509CERTIFICATE pCertificate, const char *pszFilename, uint32_t fFlags,
|
---|
| 64 | PCRTASN1ALLOCATORVTABLE pAllocator, PRTERRINFO pErrInfo)
|
---|
| 65 | {
|
---|
[62220] | 66 | AssertReturn(!(fFlags & ~RTCRX509CERT_READ_F_PEM_ONLY), VERR_INVALID_FLAGS);
|
---|
[51770] | 67 | PCRTCRPEMSECTION pSectionHead;
|
---|
[62220] | 68 | int rc = RTCrPemReadFile(pszFilename,
|
---|
| 69 | fFlags & RTCRX509CERT_READ_F_PEM_ONLY ? RTCRPEMREADFILE_F_ONLY_PEM : 0,
|
---|
| 70 | g_aRTCrX509CertificateMarkers, g_cRTCrX509CertificateMarkers,
|
---|
[59625] | 71 | &pSectionHead, pErrInfo);
|
---|
[51770] | 72 | if (RT_SUCCESS(rc))
|
---|
| 73 | {
|
---|
[62220] | 74 | if (pSectionHead)
|
---|
[51770] | 75 | {
|
---|
[62220] | 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");
|
---|
[51770] | 81 | if (RT_SUCCESS(rc))
|
---|
| 82 | {
|
---|
[62220] | 83 | rc = RTCrX509Certificate_CheckSanity(&TmpCert, 0, pErrInfo, "Cert");
|
---|
[51770] | 84 | if (RT_SUCCESS(rc))
|
---|
| 85 | {
|
---|
[62220] | 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 | }
|
---|
[51770] | 92 | }
|
---|
[62220] | 93 | RTCrX509Certificate_Delete(&TmpCert);
|
---|
[51770] | 94 | }
|
---|
[62220] | 95 | RTCrPemFreeSections(pSectionHead);
|
---|
[51770] | 96 | }
|
---|
[62220] | 97 | else
|
---|
| 98 | rc = rc != VINF_SUCCESS ? -rc : VERR_INTERNAL_ERROR_2;
|
---|
| 99 |
|
---|
[51770] | 100 | }
|
---|
| 101 | return rc;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 |
|
---|
[59620] | 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 | {
|
---|
[62220] | 109 | AssertReturn(!(fFlags & ~RTCRX509CERT_READ_F_PEM_ONLY), VERR_INVALID_FLAGS);
|
---|
[59620] | 110 | PCRTCRPEMSECTION pSectionHead;
|
---|
[62220] | 111 | int rc = RTCrPemParseContent(pvBuf, cbBuf,
|
---|
| 112 | fFlags & RTCRX509CERT_READ_F_PEM_ONLY ? RTCRPEMREADFILE_F_ONLY_PEM : 0,
|
---|
| 113 | g_aRTCrX509CertificateMarkers, g_cRTCrX509CertificateMarkers,
|
---|
[59620] | 114 | &pSectionHead, pErrInfo);
|
---|
| 115 | if (RT_SUCCESS(rc))
|
---|
| 116 | {
|
---|
[62220] | 117 | if (pSectionHead)
|
---|
[59620] | 118 | {
|
---|
[62220] | 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");
|
---|
[59620] | 124 | if (RT_SUCCESS(rc))
|
---|
| 125 | {
|
---|
[62220] | 126 | rc = RTCrX509Certificate_CheckSanity(&TmpCert, 0, pErrInfo, "Cert");
|
---|
[59620] | 127 | if (RT_SUCCESS(rc))
|
---|
| 128 | {
|
---|
[62220] | 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 | }
|
---|
[59620] | 135 | }
|
---|
[62220] | 136 | RTCrX509Certificate_Delete(&TmpCert);
|
---|
[59620] | 137 | }
|
---|
[62220] | 138 | RTCrPemFreeSections(pSectionHead);
|
---|
[59620] | 139 | }
|
---|
[62220] | 140 | else
|
---|
| 141 | rc = rc != VINF_SUCCESS ? -rc : VERR_INTERNAL_ERROR_2;
|
---|
[59620] | 142 | }
|
---|
| 143 | return rc;
|
---|
| 144 | }
|
---|
[51770] | 145 |
|
---|
[59620] | 146 |
|
---|
| 147 |
|
---|
[51770] | 148 | #if 0
|
---|
| 149 | RTDECL(int) RTCrX509Certificates_ReadFromFile(const char *pszFilename, uint32_t fFlags,
|
---|
| 150 | PRTCRX509CERTIFICATES pCertificates, PRTERRINFO pErrInfo)
|
---|
| 151 | {
|
---|
[62220] | 152 | AssertReturn(!(fFlags & ~RTCRX509CERT_READ_F_PEM_ONLY), VERR_INVALID_FLAGS);
|
---|
[51770] | 153 | PCRTCRPEMSECTION pSectionHead;
|
---|
[62220] | 154 | int rc = RTCrPemReadFile(pszFilename,
|
---|
| 155 | fFlags & RTCRX509CERT_READ_F_PEM_ONLY ? RTCRPEMREADFILE_F_ONLY_PEM : 0,
|
---|
| 156 | g_aRTCrX509CertificateMarkers, g_cRTCrX509CertificateMarkers,
|
---|
[59625] | 157 | &pSectionHead, pErrInfo);
|
---|
[51770] | 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 |
|
---|