VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/crypto/iprt-openssl.cpp@ 102294

Last change on this file since 102294 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.6 KB
Line 
1/* $Id: iprt-openssl.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Crypto - OpenSSL Helpers.
4 */
5
6/*
7 * Copyright (C) 2006-2023 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
43#ifdef IPRT_WITH_OPENSSL /* Whole file. */
44# include <iprt/err.h>
45# include <iprt/string.h>
46# include <iprt/mem.h>
47# include <iprt/asn1.h>
48# include <iprt/crypto/digest.h>
49# include <iprt/crypto/pkcs7.h>
50# include <iprt/crypto/spc.h>
51
52# include "internal/iprt-openssl.h"
53# include "internal/openssl-pre.h"
54# include <openssl/x509.h>
55# include <openssl/err.h>
56# include "internal/openssl-post.h"
57
58
59DECLHIDDEN(void) rtCrOpenSslInit(void)
60{
61 static bool s_fOssInitalized;
62 if (!s_fOssInitalized)
63 {
64 OpenSSL_add_all_algorithms();
65 ERR_load_ERR_strings();
66 ERR_load_crypto_strings();
67
68 /* Add some OIDs we might possibly want to use. */
69 static struct { const char *pszOid, *pszDesc; } const s_aOids[] =
70 {
71 { RTCRSPC_PE_IMAGE_HASHES_V1_OID, "Ms-SpcPeImagePageHashesV1" },
72 { RTCRSPC_PE_IMAGE_HASHES_V2_OID, "Ms-SpcPeImagePageHashesV2" },
73 { RTCRSPC_STMT_TYPE_INDIVIDUAL_CODE_SIGNING, "Ms-SpcIndividualCodeSigning" },
74 { RTCRSPCPEIMAGEDATA_OID, "Ms-SpcPeImageData" },
75 { RTCRSPCINDIRECTDATACONTENT_OID, "Ms-SpcIndirectDataContext" },
76 { RTCR_PKCS9_ID_MS_TIMESTAMP, "Ms-CounterSign" },
77 { RTCR_PKCS9_ID_MS_NESTED_SIGNATURE, "Ms-SpcNestedSignature" },
78 { RTCR_PKCS9_ID_MS_STATEMENT_TYPE, "Ms-SpcStatementType" },
79 { RTCR_PKCS9_ID_MS_SP_OPUS_INFO, "Ms-SpcOpusInfo" },
80 { "1.3.6.1.4.1.311.3.2.1", "Ms-SpcTimeStampRequest" }, /** @todo define */
81 { "1.3.6.1.4.1.311.10.1", "Ms-CertTrustList" }, /** @todo define */
82 };
83 for (unsigned i = 0; i < RT_ELEMENTS(s_aOids); i++)
84 OBJ_create(s_aOids[i].pszOid, s_aOids[i].pszDesc, s_aOids[i].pszDesc);
85
86 s_fOssInitalized = true;
87 }
88}
89
90
91DECLHIDDEN(int) rtCrOpenSslErrInfoCallback(const char *pach, size_t cch, void *pvUser)
92{
93 PRTERRINFO pErrInfo = (PRTERRINFO)pvUser;
94 size_t cchAlready = pErrInfo->fFlags & RTERRINFO_FLAGS_SET ? strlen(pErrInfo->pszMsg) : 0;
95 if (cchAlready + 1 < pErrInfo->cbMsg)
96 RTStrCopyEx(pErrInfo->pszMsg + cchAlready, pErrInfo->cbMsg - cchAlready, pach, cch);
97 return -1;
98}
99
100
101DECLHIDDEN(int) rtCrOpenSslConvertX509Cert(void **ppvOsslCert, PCRTCRX509CERTIFICATE pCert, PRTERRINFO pErrInfo)
102{
103 const unsigned char *pabEncoded;
104 uint32_t cbEncoded;
105 void *pvFree;
106 int rc = RTAsn1EncodeQueryRawBits(RTCrX509Certificate_GetAsn1Core(pCert),
107 (const uint8_t **)&pabEncoded, &cbEncoded, &pvFree, pErrInfo);
108 if (RT_SUCCESS(rc))
109 {
110 X509 *pOsslCert = NULL;
111 X509 *pOsslCertRet = d2i_X509(&pOsslCert, &pabEncoded, cbEncoded);
112 RTMemTmpFree(pvFree);
113 if (pOsslCert != NULL && pOsslCertRet == pOsslCert)
114 {
115 *ppvOsslCert = pOsslCert;
116 return VINF_SUCCESS;
117 }
118 rc = RTErrInfoSet(pErrInfo, VERR_CR_X509_OSSL_D2I_FAILED, "d2i_X509");
119
120 }
121 *ppvOsslCert = NULL;
122 return rc;
123}
124
125
126DECLHIDDEN(void) rtCrOpenSslFreeConvertedX509Cert(void *pvOsslCert)
127{
128 X509_free((X509 *)pvOsslCert);
129}
130
131
132DECLHIDDEN(int) rtCrOpenSslAddX509CertToStack(void *pvOsslStack, PCRTCRX509CERTIFICATE pCert, PRTERRINFO pErrInfo)
133{
134 X509 *pOsslCert = NULL;
135 int rc = rtCrOpenSslConvertX509Cert((void **)&pOsslCert, pCert, pErrInfo);
136 if (RT_SUCCESS(rc))
137 {
138 if (sk_X509_push((STACK_OF(X509) *)pvOsslStack, pOsslCert))
139 rc = VINF_SUCCESS;
140 else
141 {
142 rtCrOpenSslFreeConvertedX509Cert(pOsslCert);
143 rc = RTErrInfoSet(pErrInfo, VERR_NO_MEMORY, "sk_X509_push");
144 }
145 }
146 return rc;
147}
148
149
150DECLHIDDEN(const void /*EVP_MD*/ *) rtCrOpenSslConvertDigestType(RTDIGESTTYPE enmDigestType, PRTERRINFO pErrInfo)
151{
152 const char *pszAlgoObjId = RTCrDigestTypeToAlgorithmOid(enmDigestType);
153 AssertReturnStmt(pszAlgoObjId, RTErrInfoSetF(pErrInfo, VERR_INVALID_PARAMETER, "Invalid type: %d", enmDigestType), NULL);
154
155 int iAlgoNid = OBJ_txt2nid(pszAlgoObjId);
156 AssertReturnStmt(iAlgoNid != NID_undef,
157 RTErrInfoSetF(pErrInfo, VERR_CR_DIGEST_OSSL_DIGEST_INIT_ERROR,
158 "OpenSSL does not know: %s (%s)", pszAlgoObjId, RTCrDigestTypeToName(enmDigestType)),
159 NULL);
160
161 const char *pszAlgoSn = OBJ_nid2sn(iAlgoNid);
162 const EVP_MD *pEvpMdType = EVP_get_digestbyname(pszAlgoSn);
163 AssertReturnStmt(pEvpMdType,
164 RTErrInfoSetF(pErrInfo, VERR_CR_DIGEST_OSSL_DIGEST_INIT_ERROR, "OpenSSL/EVP does not know: %d (%s; %s; %s)",
165 iAlgoNid, pszAlgoSn, pszAlgoSn, RTCrDigestTypeToName(enmDigestType)),
166 NULL);
167
168 return pEvpMdType;
169}
170
171DECLHIDDEN(int) rtCrOpenSslConvertPkcs7Attribute(void **ppvOsslAttrib, PCRTCRPKCS7ATTRIBUTE pAttrib, PRTERRINFO pErrInfo)
172{
173 const unsigned char *pabEncoded;
174 uint32_t cbEncoded;
175 void *pvFree;
176 int rc = RTAsn1EncodeQueryRawBits(RTCrPkcs7Attribute_GetAsn1Core(pAttrib),
177 (const uint8_t **)&pabEncoded, &cbEncoded, &pvFree, pErrInfo);
178 if (RT_SUCCESS(rc))
179 {
180 X509_ATTRIBUTE *pOsslAttrib = NULL;
181 X509_ATTRIBUTE *pOsslAttribRet = d2i_X509_ATTRIBUTE(&pOsslAttrib, &pabEncoded, cbEncoded);
182 RTMemTmpFree(pvFree);
183 if (pOsslAttrib != NULL && pOsslAttribRet == pOsslAttrib)
184 {
185 *ppvOsslAttrib = pOsslAttrib;
186 return VINF_SUCCESS;
187 }
188 rc = RTErrInfoSet(pErrInfo, VERR_CR_X509_OSSL_D2I_FAILED, "d2i_X509_ATTRIBUTE");
189 }
190 *ppvOsslAttrib = NULL;
191 return rc;
192}
193
194
195DECLHIDDEN(void) rtCrOpenSslFreeConvertedPkcs7Attribute(void *pvOsslAttrib)
196{
197 X509_ATTRIBUTE_free((X509_ATTRIBUTE *)pvOsslAttrib);
198}
199
200
201#endif /* IPRT_WITH_OPENSSL */
202
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette