2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* We need to use some engine deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
14 #include "internal/cryptlib.h"
15 #include <openssl/bn.h>
16 #include <openssl/evp.h>
17 #include <openssl/objects.h>
18 #include <openssl/decoder.h>
19 #include <openssl/engine.h>
20 #include <openssl/x509.h>
21 #include <openssl/asn1.h>
22 #include "crypto/asn1.h"
23 #include "crypto/evp.h"
24 #include "internal/asn1.h"
25 #include "internal/sizes.h"
28 d2i_PrivateKey_decoder(int keytype, EVP_PKEY **a, const unsigned char **pp,
29 long length, OSSL_LIB_CTX *libctx, const char *propq)
31 OSSL_DECODER_CTX *dctx = NULL;
33 EVP_PKEY *pkey = NULL, *bak_a = NULL;
34 EVP_PKEY **ppkey = &pkey;
35 const char *key_name = NULL;
36 char keytypebuf[OSSL_MAX_NAME_SIZE];
38 const unsigned char *p = *pp;
39 const char *structure;
40 PKCS8_PRIV_KEY_INFO *p8info;
41 const ASN1_OBJECT *algoid;
43 if (keytype != EVP_PKEY_NONE) {
44 key_name = evp_pkey_type2name(keytype);
49 /* This is just a probe. It might fail, so we ignore errors */
51 p8info = d2i_PKCS8_PRIV_KEY_INFO(NULL, pp, len);
55 && PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8info)
56 && OBJ_obj2txt(keytypebuf, sizeof(keytypebuf), algoid, 0))
57 key_name = keytypebuf;
58 structure = "PrivateKeyInfo";
59 PKCS8_PRIV_KEY_INFO_free(p8info);
61 structure = "type-specific";
65 if (a != NULL && (bak_a = *a) != NULL)
67 dctx = OSSL_DECODER_CTX_new_for_pkey(ppkey, "DER", structure, key_name,
68 EVP_PKEY_KEYPAIR, libctx, propq);
74 ret = OSSL_DECODER_from_data(dctx, pp, &len);
75 OSSL_DECODER_CTX_free(dctx);
78 && evp_keymgmt_util_has(*ppkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY)) {
86 EVP_PKEY_free(*ppkey);
91 ossl_d2i_PrivateKey_legacy(int keytype, EVP_PKEY **a, const unsigned char **pp,
92 long length, OSSL_LIB_CTX *libctx, const char *propq)
95 const unsigned char *p = *pp;
97 if (a == NULL || *a == NULL) {
98 if ((ret = EVP_PKEY_new()) == NULL) {
99 ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
104 #ifndef OPENSSL_NO_ENGINE
105 ENGINE_finish(ret->engine);
110 if (!EVP_PKEY_set_type(ret, keytype)) {
111 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
116 if (!ret->ameth->old_priv_decode ||
117 !ret->ameth->old_priv_decode(ret, &p, length)) {
118 if (ret->ameth->priv_decode != NULL
119 || ret->ameth->priv_decode_ex != NULL) {
121 PKCS8_PRIV_KEY_INFO *p8 = NULL;
122 p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);
124 ERR_clear_last_mark();
127 tmp = evp_pkcs82pkey_legacy(p8, libctx, propq);
128 PKCS8_PRIV_KEY_INFO_free(p8);
130 ERR_clear_last_mark();
136 if (EVP_PKEY_type(keytype) != EVP_PKEY_get_base_id(ret))
139 ERR_clear_last_mark();
140 ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
144 ERR_clear_last_mark();
151 if (a == NULL || *a != ret)
156 EVP_PKEY *d2i_PrivateKey_ex(int keytype, EVP_PKEY **a, const unsigned char **pp,
157 long length, OSSL_LIB_CTX *libctx,
162 ret = d2i_PrivateKey_decoder(keytype, a, pp, length, libctx, propq);
163 /* try the legacy path if the decoder failed */
165 ret = ossl_d2i_PrivateKey_legacy(keytype, a, pp, length, libctx, propq);
169 EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp,
172 return d2i_PrivateKey_ex(type, a, pp, length, NULL, NULL);
175 static EVP_PKEY *d2i_AutoPrivateKey_legacy(EVP_PKEY **a,
176 const unsigned char **pp,
178 OSSL_LIB_CTX *libctx,
181 STACK_OF(ASN1_TYPE) *inkey;
182 const unsigned char *p;
187 * Dirty trick: read in the ASN1 data into a STACK_OF(ASN1_TYPE): by
188 * analyzing it we can determine the passed structure: this assumes the
189 * input is surrounded by an ASN1 SEQUENCE.
191 inkey = d2i_ASN1_SEQUENCE_ANY(NULL, &p, length);
194 * Since we only need to discern "traditional format" RSA and DSA keys we
195 * can just count the elements.
197 if (sk_ASN1_TYPE_num(inkey) == 6) {
198 keytype = EVP_PKEY_DSA;
199 } else if (sk_ASN1_TYPE_num(inkey) == 4) {
200 keytype = EVP_PKEY_EC;
201 } else if (sk_ASN1_TYPE_num(inkey) == 3) { /* This seems to be PKCS8, not
202 * traditional format */
203 PKCS8_PRIV_KEY_INFO *p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);
206 sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
208 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
211 ret = evp_pkcs82pkey_legacy(p8, libctx, propq);
212 PKCS8_PRIV_KEY_INFO_free(p8);
221 keytype = EVP_PKEY_RSA;
223 sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
224 return ossl_d2i_PrivateKey_legacy(keytype, a, pp, length, libctx, propq);
228 * This works like d2i_PrivateKey() except it passes the keytype as
229 * EVP_PKEY_NONE, which then figures out the type during decoding.
231 EVP_PKEY *d2i_AutoPrivateKey_ex(EVP_PKEY **a, const unsigned char **pp,
232 long length, OSSL_LIB_CTX *libctx,
237 ret = d2i_PrivateKey_decoder(EVP_PKEY_NONE, a, pp, length, libctx, propq);
238 /* try the legacy path if the decoder failed */
240 ret = d2i_AutoPrivateKey_legacy(a, pp, length, libctx, propq);
244 EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **a, const unsigned char **pp,
247 return d2i_AutoPrivateKey_ex(a, pp, length, NULL, NULL);