1 | /* $Id: key-openssl.cpp 100442 2023-07-08 11:10:51Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Crypto - Cryptographic Keys, OpenSSL glue.
|
---|
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 | #define LOG_GROUP RTLOGGROUP_CRYPTO
|
---|
42 | #include "internal/iprt.h"
|
---|
43 | #include <iprt/crypto/key.h>
|
---|
44 |
|
---|
45 | #include <iprt/err.h>
|
---|
46 | #include <iprt/log.h>
|
---|
47 | #include <iprt/mem.h>
|
---|
48 | #include <iprt/string.h>
|
---|
49 | #include <iprt/crypto/digest.h>
|
---|
50 |
|
---|
51 |
|
---|
52 | #ifdef IPRT_WITH_OPENSSL
|
---|
53 | # include "internal/iprt-openssl.h"
|
---|
54 | # include "internal/magics.h"
|
---|
55 | # include "internal/openssl-pre.h"
|
---|
56 | # include <openssl/evp.h>
|
---|
57 | # include "internal/openssl-post.h"
|
---|
58 | # ifndef OPENSSL_VERSION_NUMBER
|
---|
59 | # error "Missing OPENSSL_VERSION_NUMBER!"
|
---|
60 | # endif
|
---|
61 |
|
---|
62 | # include "key-internal.h"
|
---|
63 |
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Helper that loads key parameters if present.
|
---|
67 | */
|
---|
68 | static int rtCrKeyToOpenSslKeyLoadParams(RTCRKEY hKey, int idKeyType, EVP_PKEY **ppEvpNewKey, PRTERRINFO pErrInfo)
|
---|
69 | {
|
---|
70 | int rc = VINF_SUCCESS;
|
---|
71 | if ( hKey->enmType == RTCRKEYTYPE_ECDSA_PUBLIC
|
---|
72 | || hKey->enmType == RTCRKEYTYPE_ECDSA_PRIVATE)
|
---|
73 | {
|
---|
74 | void *pvFree = NULL;
|
---|
75 | const uint8_t *pbRaw = NULL;
|
---|
76 | uint32_t cbRaw = 0;
|
---|
77 | if (hKey->enmType == RTCRKEYTYPE_ECDSA_PUBLIC)
|
---|
78 | rc = RTAsn1EncodeQueryRawBits(&hKey->u.EcdsaPublic.NamedCurve.Asn1Core, &pbRaw, &cbRaw, &pvFree, pErrInfo);
|
---|
79 | else
|
---|
80 | AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
|
---|
81 | if (RT_SUCCESS(rc))
|
---|
82 | {
|
---|
83 | const unsigned char *puchParams = pbRaw;
|
---|
84 | EVP_PKEY *pRet = d2i_KeyParams(idKeyType, ppEvpNewKey, &puchParams, cbRaw);
|
---|
85 | if (pRet != NULL && pRet == *ppEvpNewKey)
|
---|
86 | rc = VINF_SUCCESS;
|
---|
87 | else
|
---|
88 | rc = RTERRINFO_LOG_SET(pErrInfo, VERR_CR_PKIX_OSSL_D2I_KEY_PARAMS_FAILED, "d2i_KeyParams failed");
|
---|
89 |
|
---|
90 | RTMemTmpFree(pvFree);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | return rc;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Helper that loads key bits.
|
---|
99 | */
|
---|
100 | static int rtCrKeyToOpenSslKeyLoadKeyBits(RTCRKEY hKey, int idKeyType, EVP_PKEY **ppEvpNewKey,
|
---|
101 | bool fNeedPublic, PRTERRINFO pErrInfo)
|
---|
102 | {
|
---|
103 | /*
|
---|
104 | * Load the key into the structure.
|
---|
105 | */
|
---|
106 | const unsigned char *puchPublicKey = hKey->pbEncoded;
|
---|
107 | EVP_PKEY *pRet;
|
---|
108 | if (fNeedPublic)
|
---|
109 | pRet = d2i_PublicKey(idKeyType, ppEvpNewKey, &puchPublicKey, hKey->cbEncoded);
|
---|
110 | else
|
---|
111 | pRet = d2i_PrivateKey(idKeyType, ppEvpNewKey, &puchPublicKey, hKey->cbEncoded);
|
---|
112 | if (pRet != NULL && pRet == *ppEvpNewKey)
|
---|
113 | return VINF_SUCCESS;
|
---|
114 |
|
---|
115 | /* Bail out: */
|
---|
116 | if (fNeedPublic)
|
---|
117 | return RTERRINFO_LOG_SET(pErrInfo, VERR_CR_PKIX_OSSL_D2I_PUBLIC_KEY_FAILED, "d2i_PublicKey failed");
|
---|
118 | return RTERRINFO_LOG_SET(pErrInfo, VERR_CR_PKIX_OSSL_D2I_PRIVATE_KEY_FAILED, "d2i_PrivateKey failed");
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Creates an OpenSSL key for the given IPRT one, returning the message digest
|
---|
124 | * algorithm if desired.
|
---|
125 | *
|
---|
126 | * @returns IRPT status code.
|
---|
127 | * @param hKey The key to convert to an OpenSSL key.
|
---|
128 | * @param fNeedPublic Set if we need the public side of the key.
|
---|
129 | * @param pszAlgoObjId Alogrithm stuff we currently need.
|
---|
130 | * @param ppEvpKey Where to return the pointer to the key structure.
|
---|
131 | * @param ppEvpMdType Where to optionally return the message digest type.
|
---|
132 | * @param pErrInfo Where to optionally return more error details.
|
---|
133 | */
|
---|
134 | DECLHIDDEN(int) rtCrKeyToOpenSslKey(RTCRKEY hKey, bool fNeedPublic, void /*EVP_PKEY*/ **ppEvpKey, PRTERRINFO pErrInfo)
|
---|
135 | {
|
---|
136 | *ppEvpKey = NULL;
|
---|
137 | AssertReturn(hKey->u32Magic == RTCRKEYINT_MAGIC, VERR_INVALID_HANDLE);
|
---|
138 | AssertReturn(fNeedPublic == !(hKey->fFlags & RTCRKEYINT_F_PRIVATE), VERR_WRONG_TYPE);
|
---|
139 | AssertReturn(hKey->fFlags & RTCRKEYINT_F_INCLUDE_ENCODED, VERR_WRONG_TYPE); /* build misconfig */
|
---|
140 |
|
---|
141 | rtCrOpenSslInit();
|
---|
142 |
|
---|
143 | /*
|
---|
144 | * Translate the key type from IPRT to EVP speak.
|
---|
145 | */
|
---|
146 | int idKeyType;
|
---|
147 | switch (hKey->enmType)
|
---|
148 | {
|
---|
149 | case RTCRKEYTYPE_RSA_PRIVATE:
|
---|
150 | case RTCRKEYTYPE_RSA_PUBLIC:
|
---|
151 | idKeyType = EVP_PKEY_RSA;
|
---|
152 | break;
|
---|
153 |
|
---|
154 | case RTCRKEYTYPE_ECDSA_PUBLIC:
|
---|
155 | case RTCRKEYTYPE_ECDSA_PRIVATE:
|
---|
156 | idKeyType = EVP_PKEY_EC;
|
---|
157 | break;
|
---|
158 |
|
---|
159 | default:
|
---|
160 | return RTErrInfoSetF(pErrInfo, VERR_NOT_SUPPORTED, "Unsupported key type: %d", hKey->enmType);
|
---|
161 | }
|
---|
162 |
|
---|
163 | /*
|
---|
164 | * Allocate a new key structure and set its type.
|
---|
165 | */
|
---|
166 | EVP_PKEY *pEvpNewKey = EVP_PKEY_new();
|
---|
167 | if (!pEvpNewKey)
|
---|
168 | return RTErrInfoSetF(pErrInfo, VERR_NO_MEMORY, "EVP_PKEY_new/%d failed", idKeyType);
|
---|
169 |
|
---|
170 | /*
|
---|
171 | * Load key parameters and the key into the EVP structure.
|
---|
172 | */
|
---|
173 | int rc = rtCrKeyToOpenSslKeyLoadParams(hKey, idKeyType, &pEvpNewKey, pErrInfo);
|
---|
174 | if (RT_SUCCESS(rc))
|
---|
175 | {
|
---|
176 | rc = rtCrKeyToOpenSslKeyLoadKeyBits(hKey, idKeyType, &pEvpNewKey, fNeedPublic, pErrInfo);
|
---|
177 | if (RT_SUCCESS(rc))
|
---|
178 | {
|
---|
179 | *ppEvpKey = pEvpNewKey;
|
---|
180 | return rc;
|
---|
181 | }
|
---|
182 | }
|
---|
183 | EVP_PKEY_free(pEvpNewKey);
|
---|
184 | return rc;
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Creates an OpenSSL key for the given IPRT one, returning the message digest
|
---|
190 | * algorithm if desired.
|
---|
191 | *
|
---|
192 | * @returns IRPT status code.
|
---|
193 | * @param hKey The key to convert to an OpenSSL key.
|
---|
194 | * @param fNeedPublic Set if we need the public side of the key.
|
---|
195 | * @param pszAlgoObjId Alogrithm stuff we currently need.
|
---|
196 | * @param ppEvpKey Where to return the pointer to the key structure.
|
---|
197 | * @param ppEvpMdType Where to optionally return the message digest type.
|
---|
198 | * @param pErrInfo Where to optionally return more error details.
|
---|
199 | */
|
---|
200 | DECLHIDDEN(int) rtCrKeyToOpenSslKeyEx(RTCRKEY hKey, bool fNeedPublic, const char *pszAlgoObjId,
|
---|
201 | void /*EVP_PKEY*/ **ppEvpKey, const void /*EVP_MD*/ **ppEvpMdType, PRTERRINFO pErrInfo)
|
---|
202 | {
|
---|
203 | *ppEvpKey = NULL;
|
---|
204 | if (ppEvpMdType)
|
---|
205 | *ppEvpMdType = NULL;
|
---|
206 | AssertReturn(hKey->u32Magic == RTCRKEYINT_MAGIC, VERR_INVALID_HANDLE);
|
---|
207 | AssertReturn(fNeedPublic == !(hKey->fFlags & RTCRKEYINT_F_PRIVATE), VERR_WRONG_TYPE);
|
---|
208 | AssertReturn(hKey->fFlags & RTCRKEYINT_F_INCLUDE_ENCODED, VERR_WRONG_TYPE); /* build misconfig */
|
---|
209 |
|
---|
210 | rtCrOpenSslInit();
|
---|
211 |
|
---|
212 | /*
|
---|
213 | * Translate algorithm object ID into stuff that OpenSSL wants.
|
---|
214 | */
|
---|
215 | int iAlgoNid = OBJ_txt2nid(pszAlgoObjId);
|
---|
216 | if (iAlgoNid == NID_undef)
|
---|
217 | return RTERRINFO_LOG_SET_F(pErrInfo, VERR_CR_PKIX_OSSL_CIPHER_ALGO_NOT_KNOWN,
|
---|
218 | "Unknown public key algorithm [OpenSSL]: %s", pszAlgoObjId);
|
---|
219 | const char *pszAlgoSn = OBJ_nid2sn(iAlgoNid);
|
---|
220 |
|
---|
221 | # if OPENSSL_VERSION_NUMBER >= 0x10001000 && !defined(LIBRESSL_VERSION_NUMBER)
|
---|
222 | int idAlgoPkey = 0;
|
---|
223 | int idAlgoMd = 0;
|
---|
224 | if (!OBJ_find_sigid_algs(iAlgoNid, &idAlgoMd, &idAlgoPkey))
|
---|
225 | return RTERRINFO_LOG_SET_F(pErrInfo, VERR_CR_PKIX_OSSL_CIPHER_ALGO_NOT_KNOWN_EVP,
|
---|
226 | "OBJ_find_sigid_algs failed on %u (%s, %s)", iAlgoNid, pszAlgoSn, pszAlgoObjId);
|
---|
227 | if (ppEvpMdType)
|
---|
228 | {
|
---|
229 | const EVP_MD *pEvpMdType = EVP_get_digestbynid(idAlgoMd);
|
---|
230 | if (!pEvpMdType)
|
---|
231 | return RTERRINFO_LOG_SET_F(pErrInfo, VERR_CR_PKIX_OSSL_CIPHER_ALGO_NOT_KNOWN_EVP,
|
---|
232 | "EVP_get_digestbynid failed on %d (%s, %s)", idAlgoMd, pszAlgoSn, pszAlgoObjId);
|
---|
233 | *ppEvpMdType = pEvpMdType;
|
---|
234 | }
|
---|
235 | # else
|
---|
236 | const EVP_MD *pEvpMdType = EVP_get_digestbyname(pszAlgoSn);
|
---|
237 | if (!pEvpMdType)
|
---|
238 | return RTERRINFO_LOG_SET_F(pErrInfo, VERR_CR_PKIX_OSSL_CIPHER_ALGO_NOT_KNOWN_EVP,
|
---|
239 | "EVP_get_digestbyname failed on %s (%s)", pszAlgoSn, pszAlgoObjId);
|
---|
240 | if (ppEvpMdType)
|
---|
241 | *ppEvpMdType = pEvpMdType;
|
---|
242 | # endif
|
---|
243 |
|
---|
244 | /*
|
---|
245 | * Allocate a new key structure and set its type.
|
---|
246 | */
|
---|
247 | EVP_PKEY *pEvpNewKey = EVP_PKEY_new();
|
---|
248 | if (!pEvpNewKey)
|
---|
249 | return RTERRINFO_LOG_SET_F(pErrInfo, VERR_NO_MEMORY, "EVP_PKEY_new(%d) failed", iAlgoNid);
|
---|
250 |
|
---|
251 | int rc;
|
---|
252 | # if OPENSSL_VERSION_NUMBER >= 0x10001000 && !defined(LIBRESSL_VERSION_NUMBER)
|
---|
253 | if (EVP_PKEY_set_type(pEvpNewKey, idAlgoPkey))
|
---|
254 | {
|
---|
255 | int idKeyType = EVP_PKEY_base_id(pEvpNewKey);
|
---|
256 | # else
|
---|
257 | int idKeyType = pEvpNewKey->type = EVP_PKEY_type(pEvpMdType->required_pkey_type[0]);
|
---|
258 | # endif
|
---|
259 | if (idKeyType != NID_undef)
|
---|
260 |
|
---|
261 | {
|
---|
262 | /*
|
---|
263 | * Load key parameters and the key into the EVP structure.
|
---|
264 | */
|
---|
265 | rc = rtCrKeyToOpenSslKeyLoadParams(hKey, idKeyType, &pEvpNewKey, pErrInfo);
|
---|
266 | if (RT_SUCCESS(rc))
|
---|
267 | {
|
---|
268 | rc = rtCrKeyToOpenSslKeyLoadKeyBits(hKey, idKeyType, &pEvpNewKey, fNeedPublic, pErrInfo);
|
---|
269 | if (RT_SUCCESS(rc))
|
---|
270 | {
|
---|
271 | *ppEvpKey = pEvpNewKey;
|
---|
272 | return rc;
|
---|
273 | }
|
---|
274 | }
|
---|
275 | }
|
---|
276 | else
|
---|
277 | # if OPENSSL_VERSION_NUMBER < 0x10001000 || defined(LIBRESSL_VERSION_NUMBER)
|
---|
278 | rc = RTERRINFO_LOG_SET(pErrInfo, VERR_CR_PKIX_OSSL_EVP_PKEY_TYPE_ERROR, "EVP_PKEY_type() failed");
|
---|
279 | # else
|
---|
280 | rc = RTERRINFO_LOG_SET(pErrInfo, VERR_CR_PKIX_OSSL_EVP_PKEY_TYPE_ERROR, "EVP_PKEY_base_id() failed");
|
---|
281 | }
|
---|
282 | else
|
---|
283 | rc = RTERRINFO_LOG_SET_F(pErrInfo, VERR_CR_PKIX_OSSL_EVP_PKEY_TYPE_ERROR,
|
---|
284 | "EVP_PKEY_set_type(%u) failed (sig algo %s)", idAlgoPkey, pszAlgoSn);
|
---|
285 | # endif
|
---|
286 |
|
---|
287 | EVP_PKEY_free(pEvpNewKey);
|
---|
288 | *ppEvpKey = NULL;
|
---|
289 | return rc;
|
---|
290 | }
|
---|
291 |
|
---|
292 | #endif /* IPRT_WITH_OPENSSL */
|
---|
293 |
|
---|