VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/crypto/key-openssl.cpp@ 84365

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

IPRT,openssl: Adding RTCrPkcs7SimpleSignSignedData as a feeble start at PKCS#7/CMS signing. bugref:9699

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