VirtualBox

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

Last change on this file since 78365 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.1 KB
Line 
1/* $Id: key-openssl.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * IPRT - Crypto - Cryptographic Keys, OpenSSL glue.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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, const char *pszAlgoObjId,
63 void /*EVP_PKEY*/ **ppEvpKey, const void /*EVP_MD*/ **ppEvpMdType, PRTERRINFO pErrInfo)
64{
65 *ppEvpKey = NULL;
66 if (ppEvpMdType)
67 *ppEvpMdType = NULL;
68 AssertReturn(hKey->u32Magic == RTCRKEYINT_MAGIC, VERR_INVALID_HANDLE);
69 AssertReturn(fNeedPublic == !(hKey->fFlags & RTCRKEYINT_F_PRIVATE), VERR_WRONG_TYPE);
70
71 rtCrOpenSslInit();
72
73 /*
74 * Translate algorithm object ID into stuff that OpenSSL wants.
75 */
76 int iAlgoNid = OBJ_txt2nid(pszAlgoObjId);
77 if (iAlgoNid == NID_undef)
78 return RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_CIPHER_ALGO_NOT_KNOWN,
79 "Unknown public key algorithm [OpenSSL]: %s", pszAlgoObjId);
80 const char *pszAlgoSn = OBJ_nid2sn(iAlgoNid);
81
82# if OPENSSL_VERSION_NUMBER >= 0x10001000 && !defined(LIBRESSL_VERSION_NUMBER)
83 int idAlgoPkey = 0;
84 int idAlgoMd = 0;
85 if (!OBJ_find_sigid_algs(iAlgoNid, &idAlgoMd, &idAlgoPkey))
86 return RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_CIPHER_ALGO_NOT_KNOWN_EVP,
87 "OBJ_find_sigid_algs failed on %u (%s, %s)", iAlgoNid, pszAlgoSn, pszAlgoObjId);
88 if (ppEvpMdType)
89 {
90 const EVP_MD *pEvpMdType = EVP_get_digestbynid(idAlgoMd);
91 if (!pEvpMdType)
92 return RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_CIPHER_ALGO_NOT_KNOWN_EVP,
93 "EVP_get_digestbynid failed on %d (%s, %s)", idAlgoMd, pszAlgoSn, pszAlgoObjId);
94 *ppEvpMdType = pEvpMdType;
95 }
96# else
97 const EVP_MD *pEvpMdType = EVP_get_digestbyname(pszAlgoSn);
98 if (!pEvpMdType)
99 return RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_CIPHER_ALGO_NOT_KNOWN_EVP,
100 "EVP_get_digestbyname failed on %s (%s)", pszAlgoSn, pszAlgoObjId);
101 if (ppEvpMdType)
102 *ppEvpMdType = pEvpMdType;
103# endif
104
105 /*
106 * Allocate a new key structure and set its type.
107 */
108 EVP_PKEY *pEvpNewKey = EVP_PKEY_new();
109 if (!pEvpNewKey)
110 return RTErrInfoSetF(pErrInfo, VERR_NO_MEMORY, "EVP_PKEY_new(%d) failed", iAlgoNid);
111
112 int rc;
113# if OPENSSL_VERSION_NUMBER >= 0x10001000 && !defined(LIBRESSL_VERSION_NUMBER)
114 if (EVP_PKEY_set_type(pEvpNewKey, idAlgoPkey))
115 {
116 int idKeyType = EVP_PKEY_base_id(pEvpNewKey);
117# else
118 int idKeyType = pEvpNewKey->type = EVP_PKEY_type(pEvpMdType->required_pkey_type[0]);
119# endif
120 if (idKeyType != NID_undef)
121
122 {
123 /*
124 * Load the key into the structure.
125 */
126 const unsigned char *puchPublicKey = hKey->pbEncoded;
127 EVP_PKEY *pRet;
128 if (fNeedPublic)
129 *ppEvpKey = pRet = d2i_PublicKey(idKeyType, &pEvpNewKey, &puchPublicKey, hKey->cbEncoded);
130 else
131 *ppEvpKey = pRet = d2i_PrivateKey(idKeyType, &pEvpNewKey, &puchPublicKey, hKey->cbEncoded);
132 if (pRet)
133 return VINF_SUCCESS;
134
135 /* Bail out: */
136 rc = RTErrInfoSet(pErrInfo, VERR_CR_PKIX_OSSL_D2I_PUBLIC_KEY_FAILED,
137 fNeedPublic ? "d2i_PublicKey failed" : "d2i_PrivateKey failed");
138 }
139 else
140# if OPENSSL_VERSION_NUMBER < 0x10001000 || defined(LIBRESSL_VERSION_NUMBER)
141 rc = RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_EVP_PKEY_TYPE_ERROR, "EVP_PKEY_type() failed");
142# else
143 rc = RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_EVP_PKEY_TYPE_ERROR, "EVP_PKEY_base_id() failed");
144 }
145 else
146 rc = RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_OSSL_EVP_PKEY_TYPE_ERROR,
147 "EVP_PKEY_set_type(%u) failed (sig algo %s)", idAlgoPkey, pszAlgoSn);
148# endif
149
150 EVP_PKEY_free(pEvpNewKey);
151 return rc;
152}
153
154#endif /* IPRT_WITH_OPENSSL */
155
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