1 | /* $Id: key-openssl.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Crypto - Cryptographic Keys, OpenSSL glue.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 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 | #include <iprt/crypto/key.h>
|
---|
43 |
|
---|
44 | #include <iprt/err.h>
|
---|
45 | #include <iprt/string.h>
|
---|
46 | #include <iprt/crypto/digest.h>
|
---|
47 |
|
---|
48 |
|
---|
49 | #ifdef IPRT_WITH_OPENSSL
|
---|
50 | # include "internal/iprt-openssl.h"
|
---|
51 | # include "internal/magics.h"
|
---|
52 | # include "internal/openssl-pre.h"
|
---|
53 | # include <openssl/evp.h>
|
---|
54 | # include "internal/openssl-post.h"
|
---|
55 | # ifndef OPENSSL_VERSION_NUMBER
|
---|
56 | # error "Missing OPENSSL_VERSION_NUMBER!"
|
---|
57 | # endif
|
---|
58 |
|
---|
59 | # include "key-internal.h"
|
---|
60 |
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Creates an OpenSSL key for the given IPRT one, returning the message digest
|
---|
64 | * algorithm if desired.
|
---|
65 | *
|
---|
66 | * @returns IRPT status code.
|
---|
67 | * @param hKey The key to convert to an OpenSSL key.
|
---|
68 | * @param fNeedPublic Set if we need the public side of the key.
|
---|
69 | * @param pszAlgoObjId Alogrithm stuff we currently need.
|
---|
70 | * @param ppEvpKey Where to return the pointer to the key structure.
|
---|
71 | * @param ppEvpMdType Where to optionally return the message digest type.
|
---|
72 | * @param pErrInfo Where to optionally return more error details.
|
---|
73 | */
|
---|
74 | DECLHIDDEN(int) rtCrKeyToOpenSslKey(RTCRKEY hKey, bool fNeedPublic, void /*EVP_PKEY*/ **ppEvpKey, PRTERRINFO pErrInfo)
|
---|
75 | {
|
---|
76 | *ppEvpKey = NULL;
|
---|
77 | AssertReturn(hKey->u32Magic == RTCRKEYINT_MAGIC, VERR_INVALID_HANDLE);
|
---|
78 | AssertReturn(fNeedPublic == !(hKey->fFlags & RTCRKEYINT_F_PRIVATE), VERR_WRONG_TYPE);
|
---|
79 |
|
---|
80 | rtCrOpenSslInit();
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * Translate the key type from IPRT to EVP speak.
|
---|
84 | */
|
---|
85 | int idKeyType;
|
---|
86 | switch (hKey->enmType)
|
---|
87 | {
|
---|
88 | case RTCRKEYTYPE_RSA_PRIVATE:
|
---|
89 | case RTCRKEYTYPE_RSA_PUBLIC:
|
---|
90 | idKeyType = EVP_PKEY_RSA;
|
---|
91 | break;
|
---|
92 | default:
|
---|
93 | return RTErrInfoSetF(pErrInfo, VERR_NOT_SUPPORTED, "Unsupported key type: %d", hKey->enmType);
|
---|
94 | }
|
---|
95 |
|
---|
96 | /*
|
---|
97 | * Allocate a new key structure and set its type.
|
---|
98 | */
|
---|
99 | EVP_PKEY *pEvpNewKey = EVP_PKEY_new();
|
---|
100 | if (!pEvpNewKey)
|
---|
101 | return RTErrInfoSetF(pErrInfo, VERR_NO_MEMORY, "EVP_PKEY_new/%d failed", idKeyType);
|
---|
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 | *ppEvpKey = pRet = d2i_PublicKey(idKeyType, &pEvpNewKey, &puchPublicKey, hKey->cbEncoded);
|
---|
110 | else
|
---|
111 | *ppEvpKey = pRet = d2i_PrivateKey(idKeyType, &pEvpNewKey, &puchPublicKey, hKey->cbEncoded);
|
---|
112 | if (pRet)
|
---|
113 | return VINF_SUCCESS;
|
---|
114 |
|
---|
115 | /* Bail out: */
|
---|
116 | EVP_PKEY_free(pEvpNewKey);
|
---|
117 | return RTErrInfoSet(pErrInfo, VERR_CR_PKIX_OSSL_D2I_PUBLIC_KEY_FAILED,
|
---|
118 | fNeedPublic ? "d2i_PublicKey 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) rtCrKeyToOpenSslKeyEx(RTCRKEY hKey, bool fNeedPublic, const char *pszAlgoObjId,
|
---|
135 | void /*EVP_PKEY*/ **ppEvpKey, const void /*EVP_MD*/ **ppEvpMdType, PRTERRINFO pErrInfo)
|
---|
136 | {
|
---|
137 | *ppEvpKey = NULL;
|
---|
138 | if (ppEvpMdType)
|
---|
139 | *ppEvpMdType = NULL;
|
---|
140 | AssertReturn(hKey->u32Magic == RTCRKEYINT_MAGIC, VERR_INVALID_HANDLE);
|
---|
141 | AssertReturn(fNeedPublic == !(hKey->fFlags & RTCRKEYINT_F_PRIVATE), VERR_WRONG_TYPE);
|
---|
142 |
|
---|
143 | rtCrOpenSslInit();
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * Translate algorithm object ID into stuff that OpenSSL wants.
|
---|
147 | */
|
---|
148 | int iAlgoNid = OBJ_txt2nid(pszAlgoObjId);
|
---|
149 | if (iAlgoNid == NID_undef)
|
---|
150 | return RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_CIPHER_ALGO_NOT_KNOWN,
|
---|
151 | "Unknown public key algorithm [OpenSSL]: %s", pszAlgoObjId);
|
---|
152 | const char *pszAlgoSn = OBJ_nid2sn(iAlgoNid);
|
---|
153 |
|
---|
154 | # if OPENSSL_VERSION_NUMBER >= 0x10001000 && !defined(LIBRESSL_VERSION_NUMBER)
|
---|
155 | int idAlgoPkey = 0;
|
---|
156 | int idAlgoMd = 0;
|
---|
157 | if (!OBJ_find_sigid_algs(iAlgoNid, &idAlgoMd, &idAlgoPkey))
|
---|
158 | return RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_CIPHER_ALGO_NOT_KNOWN_EVP,
|
---|
159 | "OBJ_find_sigid_algs failed on %u (%s, %s)", iAlgoNid, pszAlgoSn, pszAlgoObjId);
|
---|
160 | if (ppEvpMdType)
|
---|
161 | {
|
---|
162 | const EVP_MD *pEvpMdType = EVP_get_digestbynid(idAlgoMd);
|
---|
163 | if (!pEvpMdType)
|
---|
164 | return RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_CIPHER_ALGO_NOT_KNOWN_EVP,
|
---|
165 | "EVP_get_digestbynid failed on %d (%s, %s)", idAlgoMd, pszAlgoSn, pszAlgoObjId);
|
---|
166 | *ppEvpMdType = pEvpMdType;
|
---|
167 | }
|
---|
168 | # else
|
---|
169 | const EVP_MD *pEvpMdType = EVP_get_digestbyname(pszAlgoSn);
|
---|
170 | if (!pEvpMdType)
|
---|
171 | return RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_CIPHER_ALGO_NOT_KNOWN_EVP,
|
---|
172 | "EVP_get_digestbyname failed on %s (%s)", pszAlgoSn, pszAlgoObjId);
|
---|
173 | if (ppEvpMdType)
|
---|
174 | *ppEvpMdType = pEvpMdType;
|
---|
175 | # endif
|
---|
176 |
|
---|
177 | /*
|
---|
178 | * Allocate a new key structure and set its type.
|
---|
179 | */
|
---|
180 | EVP_PKEY *pEvpNewKey = EVP_PKEY_new();
|
---|
181 | if (!pEvpNewKey)
|
---|
182 | return RTErrInfoSetF(pErrInfo, VERR_NO_MEMORY, "EVP_PKEY_new(%d) failed", iAlgoNid);
|
---|
183 |
|
---|
184 | int rc;
|
---|
185 | # if OPENSSL_VERSION_NUMBER >= 0x10001000 && !defined(LIBRESSL_VERSION_NUMBER)
|
---|
186 | if (EVP_PKEY_set_type(pEvpNewKey, idAlgoPkey))
|
---|
187 | {
|
---|
188 | int idKeyType = EVP_PKEY_base_id(pEvpNewKey);
|
---|
189 | # else
|
---|
190 | int idKeyType = pEvpNewKey->type = EVP_PKEY_type(pEvpMdType->required_pkey_type[0]);
|
---|
191 | # endif
|
---|
192 | if (idKeyType != NID_undef)
|
---|
193 |
|
---|
194 | {
|
---|
195 | /*
|
---|
196 | * Load the key into the structure.
|
---|
197 | */
|
---|
198 | const unsigned char *puchPublicKey = hKey->pbEncoded;
|
---|
199 | EVP_PKEY *pRet;
|
---|
200 | if (fNeedPublic)
|
---|
201 | *ppEvpKey = pRet = d2i_PublicKey(idKeyType, &pEvpNewKey, &puchPublicKey, hKey->cbEncoded);
|
---|
202 | else
|
---|
203 | *ppEvpKey = pRet = d2i_PrivateKey(idKeyType, &pEvpNewKey, &puchPublicKey, hKey->cbEncoded);
|
---|
204 | if (pRet)
|
---|
205 | return VINF_SUCCESS;
|
---|
206 |
|
---|
207 | /* Bail out: */
|
---|
208 | rc = RTErrInfoSet(pErrInfo, VERR_CR_PKIX_OSSL_D2I_PUBLIC_KEY_FAILED,
|
---|
209 | fNeedPublic ? "d2i_PublicKey failed" : "d2i_PrivateKey failed");
|
---|
210 | }
|
---|
211 | else
|
---|
212 | # if OPENSSL_VERSION_NUMBER < 0x10001000 || defined(LIBRESSL_VERSION_NUMBER)
|
---|
213 | rc = RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_EVP_PKEY_TYPE_ERROR, "EVP_PKEY_type() failed");
|
---|
214 | # else
|
---|
215 | rc = RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_EVP_PKEY_TYPE_ERROR, "EVP_PKEY_base_id() failed");
|
---|
216 | }
|
---|
217 | else
|
---|
218 | rc = RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_EVP_PKEY_TYPE_ERROR,
|
---|
219 | "EVP_PKEY_set_type(%u) failed (sig algo %s)", idAlgoPkey, pszAlgoSn);
|
---|
220 | # endif
|
---|
221 |
|
---|
222 | EVP_PKEY_free(pEvpNewKey);
|
---|
223 | return rc;
|
---|
224 | }
|
---|
225 |
|
---|
226 | #endif /* IPRT_WITH_OPENSSL */
|
---|
227 |
|
---|