VirtualBox

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

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

IPRT/crypto: Adding functions for checking whether a key or certificate can handle a given digest (size wise). Also, added OIDs, padding variants and stuff for sha512-224WithRSAEncryption and sha512-256WithRSAEncryption (RFC-8017). Note that OpenSSL does not implement these yet. bugref:9699

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.9 KB
Line 
1/* $Id: iprt-openssl.cpp 84248 2020-05-11 11:46:40Z 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.215389.xyz. 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
73 /*
74 * ASSUME that if the certificate has data pointers, it's been parsed out
75 * of a binary blob and we can safely access that here.
76 */
77 if (pCert->SeqCore.Asn1Core.uData.pv)
78 {
79 pabEncoded = (const unsigned char *)RTASN1CORE_GET_RAW_ASN1_PTR(&pCert->SeqCore.Asn1Core);
80 uint32_t cbEncoded = RTASN1CORE_GET_RAW_ASN1_SIZE(&pCert->SeqCore.Asn1Core);
81 X509 *pOsslCert = NULL;
82 if (d2i_X509(&pOsslCert, &pabEncoded, cbEncoded) == pOsslCert)
83 {
84 *ppvOsslCert = pOsslCert;
85 return VINF_SUCCESS;
86 }
87 }
88 /*
89 * Otherwise, we'll have to encode it into a temporary buffer that openssl
90 * can decode into its structures.
91 */
92 else
93 {
94 PRTASN1CORE pNonConstCore = (PRTASN1CORE)&pCert->SeqCore.Asn1Core;
95 uint32_t cbEncoded = 0;
96 int rc = RTAsn1EncodePrepare(pNonConstCore, RTASN1ENCODE_F_DER, &cbEncoded, pErrInfo);
97 AssertRCReturn(rc, rc);
98
99 void * const pvEncoded = RTMemTmpAllocZ(cbEncoded);
100 AssertReturn(pvEncoded, VERR_NO_TMP_MEMORY);
101
102 rc = RTAsn1EncodeToBuffer(pNonConstCore, RTASN1ENCODE_F_DER, pvEncoded, cbEncoded, pErrInfo);
103 if (RT_SUCCESS(rc))
104 {
105 pabEncoded = (const unsigned char *)pvEncoded;
106 X509 *pOsslCert = NULL;
107 if (d2i_X509(&pOsslCert, &pabEncoded, cbEncoded) == pOsslCert)
108 {
109 *ppvOsslCert = pOsslCert;
110 RTMemTmpFree(pvEncoded);
111 return VINF_SUCCESS;
112 }
113 }
114 else
115 {
116 RTMemTmpFree(pvEncoded);
117 return rc;
118 }
119 }
120
121 *ppvOsslCert = NULL;
122 return RTErrInfoSet(pErrInfo, VERR_CR_X509_OSSL_D2I_FAILED, "d2i_X509");
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
171#endif /* IPRT_WITH_OPENSSL */
172
Note: See TracBrowser for help on using the repository browser.

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