VirtualBox

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

Last change on this file since 84379 was 84310, checked in by vboxsync, 5 years ago

IPRT/crypto: Adding RTAsn1EncodeQueryRawBits to deal with getting encoded bytes cheaply if possible and always safely. Fixed another place using RTASN1CORE_GET_RAW_ASN1_PTR and assuming input was decoded and had valid data pointers. Added RTCrStoreCertAddPkcs7 and RTCrStoreCertAddX509 for more conveniently adding decoded certs to stores. Added RTCRPKCS7VERIFY_SD_F_TRUST_ALL_CERTS to the PKCS7 verification code. Added RTCrPkcs7_ReadFromBuffer. bugref:9699

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 KB
Line 
1/* $Id: iprt-openssl.cpp 84310 2020-05-14 17:40:35Z vboxsync $ */
2/** @file
3 * IPRT - Crypto - OpenSSL Helpers.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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
33#ifdef IPRT_WITH_OPENSSL /* Whole file. */
34# include <iprt/err.h>
35# include <iprt/string.h>
36# include <iprt/mem.h>
37# include <iprt/asn1.h>
38# include <iprt/crypto/digest.h>
39
40# include "internal/iprt-openssl.h"
41# include <openssl/x509.h>
42# include <openssl/err.h>
43
44
45DECLHIDDEN(void) rtCrOpenSslInit(void)
46{
47 static bool s_fOssInitalized;
48 if (!s_fOssInitalized)
49 {
50 OpenSSL_add_all_algorithms();
51 ERR_load_ERR_strings();
52 ERR_load_crypto_strings();
53
54 s_fOssInitalized = true;
55 }
56}
57
58
59DECLHIDDEN(int) rtCrOpenSslErrInfoCallback(const char *pach, size_t cch, void *pvUser)
60{
61 PRTERRINFO pErrInfo = (PRTERRINFO)pvUser;
62 size_t cchAlready = pErrInfo->fFlags & RTERRINFO_FLAGS_SET ? strlen(pErrInfo->pszMsg) : 0;
63 if (cchAlready + 1 < pErrInfo->cbMsg)
64 RTStrCopyEx(pErrInfo->pszMsg + cchAlready, pErrInfo->cbMsg - cchAlready, pach, cch);
65 return -1;
66}
67
68
69DECLHIDDEN(int) rtCrOpenSslConvertX509Cert(void **ppvOsslCert, PCRTCRX509CERTIFICATE pCert, PRTERRINFO pErrInfo)
70{
71 const unsigned char *pabEncoded;
72 uint32_t cbEncoded;
73 void *pvFree;
74 int rc = RTAsn1EncodeQueryRawBits(RTCrX509Certificate_GetAsn1Core(pCert),
75 (const uint8_t **)&pabEncoded, &cbEncoded, &pvFree, pErrInfo);
76 if (RT_SUCCESS(rc))
77 {
78 X509 *pOsslCert = NULL;
79 X509 *pOsslCertRet = d2i_X509(&pOsslCert, &pabEncoded, cbEncoded);
80 RTMemTmpFree(pvFree);
81 if (pOsslCertRet == pOsslCert)
82 {
83 *ppvOsslCert = pOsslCert;
84 return VINF_SUCCESS;
85 }
86 rc = RTErrInfoSet(pErrInfo, VERR_CR_X509_OSSL_D2I_FAILED, "d2i_X509");
87
88 }
89 *ppvOsslCert = NULL;
90 return rc;
91}
92
93
94DECLHIDDEN(void) rtCrOpenSslFreeConvertedX509Cert(void *pvOsslCert)
95{
96 X509_free((X509 *)pvOsslCert);
97}
98
99
100DECLHIDDEN(int) rtCrOpenSslAddX509CertToStack(void *pvOsslStack, PCRTCRX509CERTIFICATE pCert, PRTERRINFO pErrInfo)
101{
102 X509 *pOsslCert = NULL;
103 int rc = rtCrOpenSslConvertX509Cert((void **)&pOsslCert, pCert, pErrInfo);
104 if (RT_SUCCESS(rc))
105 {
106 if (sk_X509_push((STACK_OF(X509) *)pvOsslStack, pOsslCert))
107 rc = VINF_SUCCESS;
108 else
109 {
110 rtCrOpenSslFreeConvertedX509Cert(pOsslCert);
111 rc = RTErrInfoSet(pErrInfo, VERR_NO_MEMORY, "sk_X509_push");
112 }
113 }
114 return rc;
115}
116
117
118DECLHIDDEN(const void /*EVP_MD*/ *) rtCrOpenSslConvertDigestType(RTDIGESTTYPE enmDigestType, PRTERRINFO pErrInfo)
119{
120 const char *pszAlgoObjId = RTCrDigestTypeToAlgorithmOid(enmDigestType);
121 AssertReturnStmt(pszAlgoObjId, RTErrInfoSetF(pErrInfo, VERR_INVALID_PARAMETER, "Invalid type: %d", enmDigestType), NULL);
122
123 int iAlgoNid = OBJ_txt2nid(pszAlgoObjId);
124 AssertReturnStmt(iAlgoNid != NID_undef,
125 RTErrInfoSetF(pErrInfo, VERR_CR_DIGEST_OSSL_DIGEST_INIT_ERROR,
126 "OpenSSL does not know: %s (%s)", pszAlgoObjId, RTCrDigestTypeToName(enmDigestType)),
127 NULL);
128
129 const char *pszAlgoSn = OBJ_nid2sn(iAlgoNid);
130 const EVP_MD *pEvpMdType = EVP_get_digestbyname(pszAlgoSn);
131 AssertReturnStmt(pEvpMdType,
132 RTErrInfoSetF(pErrInfo, VERR_CR_DIGEST_OSSL_DIGEST_INIT_ERROR, "OpenSSL/EVP does not know: %d (%s; %s; %s)",
133 iAlgoNid, pszAlgoSn, pszAlgoSn, RTCrDigestTypeToName(enmDigestType)),
134 NULL);
135
136 return pEvpMdType;
137}
138
139#endif /* IPRT_WITH_OPENSSL */
140
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