1 | /* $Id: iprt-openssl.cpp 84230 2020-05-10 00:52:05Z 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.alldomusa.eu.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 |
|
---|
39 | # include "internal/iprt-openssl.h"
|
---|
40 | # include <openssl/x509.h>
|
---|
41 | # include <openssl/err.h>
|
---|
42 |
|
---|
43 |
|
---|
44 | DECLHIDDEN(void) rtCrOpenSslInit(void)
|
---|
45 | {
|
---|
46 | static bool s_fOssInitalized;
|
---|
47 | if (!s_fOssInitalized)
|
---|
48 | {
|
---|
49 | OpenSSL_add_all_algorithms();
|
---|
50 | ERR_load_ERR_strings();
|
---|
51 | ERR_load_crypto_strings();
|
---|
52 |
|
---|
53 | s_fOssInitalized = true;
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | DECLHIDDEN(int) rtCrOpenSslErrInfoCallback(const char *pach, size_t cch, void *pvUser)
|
---|
59 | {
|
---|
60 | PRTERRINFO pErrInfo = (PRTERRINFO)pvUser;
|
---|
61 | size_t cchAlready = pErrInfo->fFlags & RTERRINFO_FLAGS_SET ? strlen(pErrInfo->pszMsg) : 0;
|
---|
62 | if (cchAlready + 1 < pErrInfo->cbMsg)
|
---|
63 | RTStrCopyEx(pErrInfo->pszMsg + cchAlready, pErrInfo->cbMsg - cchAlready, pach, cch);
|
---|
64 | return -1;
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | DECLHIDDEN(int) rtCrOpenSslConvertX509Cert(void **ppvOsslCert, PCRTCRX509CERTIFICATE pCert, PRTERRINFO pErrInfo)
|
---|
69 | {
|
---|
70 | const unsigned char *pabEncoded;
|
---|
71 |
|
---|
72 | /*
|
---|
73 | * ASSUME that if the certificate has data pointers, it's been parsed out
|
---|
74 | * of a binary blob and we can safely access that here.
|
---|
75 | */
|
---|
76 | if (pCert->SeqCore.Asn1Core.uData.pv)
|
---|
77 | {
|
---|
78 | pabEncoded = (const unsigned char *)RTASN1CORE_GET_RAW_ASN1_PTR(&pCert->SeqCore.Asn1Core);
|
---|
79 | uint32_t cbEncoded = RTASN1CORE_GET_RAW_ASN1_SIZE(&pCert->SeqCore.Asn1Core);
|
---|
80 | X509 *pOsslCert = NULL;
|
---|
81 | if (d2i_X509(&pOsslCert, &pabEncoded, cbEncoded) == pOsslCert)
|
---|
82 | {
|
---|
83 | *ppvOsslCert = pOsslCert;
|
---|
84 | return VINF_SUCCESS;
|
---|
85 | }
|
---|
86 | }
|
---|
87 | /*
|
---|
88 | * Otherwise, we'll have to encode it into a temporary buffer that openssl
|
---|
89 | * can decode into its structures.
|
---|
90 | */
|
---|
91 | else
|
---|
92 | {
|
---|
93 | PRTASN1CORE pNonConstCore = (PRTASN1CORE)&pCert->SeqCore.Asn1Core;
|
---|
94 | uint32_t cbEncoded = 0;
|
---|
95 | int rc = RTAsn1EncodePrepare(pNonConstCore, RTASN1ENCODE_F_DER, &cbEncoded, pErrInfo);
|
---|
96 | AssertRCReturn(rc, rc);
|
---|
97 |
|
---|
98 | void * const pvEncoded = RTMemTmpAllocZ(cbEncoded);
|
---|
99 | AssertReturn(pvEncoded, VERR_NO_TMP_MEMORY);
|
---|
100 |
|
---|
101 | rc = RTAsn1EncodeToBuffer(pNonConstCore, RTASN1ENCODE_F_DER, pvEncoded, cbEncoded, pErrInfo);
|
---|
102 | if (RT_SUCCESS(rc))
|
---|
103 | {
|
---|
104 | pabEncoded = (const unsigned char *)pvEncoded;
|
---|
105 | X509 *pOsslCert = NULL;
|
---|
106 | if (d2i_X509(&pOsslCert, &pabEncoded, cbEncoded) == pOsslCert)
|
---|
107 | {
|
---|
108 | *ppvOsslCert = pOsslCert;
|
---|
109 | RTMemTmpFree(pvEncoded);
|
---|
110 | return VINF_SUCCESS;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | else
|
---|
114 | {
|
---|
115 | RTMemTmpFree(pvEncoded);
|
---|
116 | return rc;
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | *ppvOsslCert = NULL;
|
---|
121 | return RTErrInfoSet(pErrInfo, VERR_CR_X509_OSSL_D2I_FAILED, "d2i_X509");
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | DECLHIDDEN(void) rtCrOpenSslFreeConvertedX509Cert(void *pvOsslCert)
|
---|
126 | {
|
---|
127 | X509_free((X509 *)pvOsslCert);
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | DECLHIDDEN(int) rtCrOpenSslAddX509CertToStack(void *pvOsslStack, PCRTCRX509CERTIFICATE pCert, PRTERRINFO pErrInfo)
|
---|
132 | {
|
---|
133 | X509 *pOsslCert = NULL;
|
---|
134 | int rc = rtCrOpenSslConvertX509Cert((void **)&pOsslCert, pCert, pErrInfo);
|
---|
135 | if (RT_SUCCESS(rc))
|
---|
136 | {
|
---|
137 | if (sk_X509_push((STACK_OF(X509) *)pvOsslStack, pOsslCert))
|
---|
138 | rc = VINF_SUCCESS;
|
---|
139 | else
|
---|
140 | {
|
---|
141 | rtCrOpenSslFreeConvertedX509Cert(pOsslCert);
|
---|
142 | rc = RTErrInfoSet(pErrInfo, VERR_NO_MEMORY, "sk_X509_push");
|
---|
143 | }
|
---|
144 | }
|
---|
145 | return rc;
|
---|
146 | }
|
---|
147 |
|
---|
148 | #endif /* IPRT_WITH_OPENSSL */
|
---|
149 |
|
---|