/src/openssl31/crypto/asn1/d2i_pr.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
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 |
8 | | */ |
9 | | |
10 | | /* We need to use some engine deprecated APIs */ |
11 | | #define OPENSSL_SUPPRESS_DEPRECATED |
12 | | |
13 | | #include <stdio.h> |
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" |
26 | | |
27 | | static EVP_PKEY * |
28 | | d2i_PrivateKey_decoder(int keytype, EVP_PKEY **a, const unsigned char **pp, |
29 | | long length, OSSL_LIB_CTX *libctx, const char *propq) |
30 | 27.5k | { |
31 | 27.5k | OSSL_DECODER_CTX *dctx = NULL; |
32 | 27.5k | size_t len = length; |
33 | 27.5k | EVP_PKEY *pkey = NULL, *bak_a = NULL; |
34 | 27.5k | EVP_PKEY **ppkey = &pkey; |
35 | 27.5k | const char *key_name = NULL; |
36 | 27.5k | char keytypebuf[OSSL_MAX_NAME_SIZE]; |
37 | 27.5k | int ret; |
38 | 27.5k | const unsigned char *p = *pp; |
39 | 27.5k | const char *structure; |
40 | 27.5k | PKCS8_PRIV_KEY_INFO *p8info; |
41 | 27.5k | const ASN1_OBJECT *algoid; |
42 | | |
43 | 27.5k | if (keytype != EVP_PKEY_NONE) { |
44 | 0 | key_name = evp_pkey_type2name(keytype); |
45 | 0 | if (key_name == NULL) |
46 | 0 | return NULL; |
47 | 0 | } |
48 | | |
49 | | /* This is just a probe. It might fail, so we ignore errors */ |
50 | 27.5k | ERR_set_mark(); |
51 | 27.5k | p8info = d2i_PKCS8_PRIV_KEY_INFO(NULL, pp, len); |
52 | 27.5k | ERR_pop_to_mark(); |
53 | 27.5k | if (p8info != NULL) { |
54 | 1.31k | if (key_name == NULL |
55 | 1.31k | && PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8info) |
56 | 1.31k | && OBJ_obj2txt(keytypebuf, sizeof(keytypebuf), algoid, 0)) |
57 | 1.31k | key_name = keytypebuf; |
58 | 1.31k | structure = "PrivateKeyInfo"; |
59 | 1.31k | PKCS8_PRIV_KEY_INFO_free(p8info); |
60 | 26.2k | } else { |
61 | 26.2k | structure = "type-specific"; |
62 | 26.2k | } |
63 | 27.5k | *pp = p; |
64 | | |
65 | 27.5k | if (a != NULL && (bak_a = *a) != NULL) |
66 | 0 | ppkey = a; |
67 | 27.5k | dctx = OSSL_DECODER_CTX_new_for_pkey(ppkey, "DER", structure, key_name, |
68 | 27.5k | EVP_PKEY_KEYPAIR, libctx, propq); |
69 | 27.5k | if (a != NULL) |
70 | 0 | *a = bak_a; |
71 | 27.5k | if (dctx == NULL) |
72 | 0 | goto err; |
73 | | |
74 | 27.5k | ret = OSSL_DECODER_from_data(dctx, pp, &len); |
75 | 27.5k | OSSL_DECODER_CTX_free(dctx); |
76 | 27.5k | if (ret |
77 | 27.5k | && *ppkey != NULL |
78 | 27.5k | && evp_keymgmt_util_has(*ppkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY)) { |
79 | 3.04k | if (a != NULL) |
80 | 0 | *a = *ppkey; |
81 | 3.04k | return *ppkey; |
82 | 3.04k | } |
83 | | |
84 | 24.4k | err: |
85 | 24.4k | if (ppkey != a) |
86 | 24.4k | EVP_PKEY_free(*ppkey); |
87 | 24.4k | return NULL; |
88 | 27.5k | } |
89 | | |
90 | | EVP_PKEY * |
91 | | ossl_d2i_PrivateKey_legacy(int keytype, EVP_PKEY **a, const unsigned char **pp, |
92 | | long length, OSSL_LIB_CTX *libctx, const char *propq) |
93 | 41.7k | { |
94 | 41.7k | EVP_PKEY *ret; |
95 | 41.7k | const unsigned char *p = *pp; |
96 | | |
97 | 41.7k | if (a == NULL || *a == NULL) { |
98 | 41.7k | if ((ret = EVP_PKEY_new()) == NULL) { |
99 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB); |
100 | 0 | return NULL; |
101 | 0 | } |
102 | 41.7k | } else { |
103 | 0 | ret = *a; |
104 | 0 | #ifndef OPENSSL_NO_ENGINE |
105 | 0 | ENGINE_finish(ret->engine); |
106 | 0 | ret->engine = NULL; |
107 | 0 | #endif |
108 | 0 | } |
109 | | |
110 | 41.7k | if (!EVP_PKEY_set_type(ret, keytype)) { |
111 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE); |
112 | 0 | goto err; |
113 | 0 | } |
114 | | |
115 | 41.7k | ERR_set_mark(); |
116 | 41.7k | if (!ret->ameth->old_priv_decode || |
117 | 41.7k | !ret->ameth->old_priv_decode(ret, &p, length)) { |
118 | 41.2k | if (ret->ameth->priv_decode != NULL |
119 | 41.2k | || ret->ameth->priv_decode_ex != NULL) { |
120 | 41.2k | EVP_PKEY *tmp; |
121 | 41.2k | PKCS8_PRIV_KEY_INFO *p8 = NULL; |
122 | 41.2k | p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length); |
123 | 41.2k | if (p8 == NULL) { |
124 | 41.2k | ERR_clear_last_mark(); |
125 | 41.2k | goto err; |
126 | 41.2k | } |
127 | 22 | tmp = evp_pkcs82pkey_legacy(p8, libctx, propq); |
128 | 22 | PKCS8_PRIV_KEY_INFO_free(p8); |
129 | 22 | if (tmp == NULL) { |
130 | 14 | ERR_clear_last_mark(); |
131 | 14 | goto err; |
132 | 14 | } |
133 | 8 | EVP_PKEY_free(ret); |
134 | 8 | ret = tmp; |
135 | 8 | ERR_pop_to_mark(); |
136 | 8 | if (EVP_PKEY_type(keytype) != EVP_PKEY_get_base_id(ret)) |
137 | 4 | goto err; |
138 | 8 | } else { |
139 | 0 | ERR_clear_last_mark(); |
140 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB); |
141 | 0 | goto err; |
142 | 0 | } |
143 | 41.2k | } else { |
144 | 507 | ERR_clear_last_mark(); |
145 | 507 | } |
146 | 511 | *pp = p; |
147 | 511 | if (a != NULL) |
148 | 0 | *a = ret; |
149 | 511 | return ret; |
150 | 41.2k | err: |
151 | 41.2k | if (a == NULL || *a != ret) |
152 | 41.2k | EVP_PKEY_free(ret); |
153 | 41.2k | return NULL; |
154 | 41.7k | } |
155 | | |
156 | | EVP_PKEY *d2i_PrivateKey_ex(int keytype, EVP_PKEY **a, const unsigned char **pp, |
157 | | long length, OSSL_LIB_CTX *libctx, |
158 | | const char *propq) |
159 | 0 | { |
160 | 0 | EVP_PKEY *ret; |
161 | |
|
162 | 0 | ret = d2i_PrivateKey_decoder(keytype, a, pp, length, libctx, propq); |
163 | | /* try the legacy path if the decoder failed */ |
164 | 0 | if (ret == NULL) |
165 | 0 | ret = ossl_d2i_PrivateKey_legacy(keytype, a, pp, length, libctx, propq); |
166 | 0 | return ret; |
167 | 0 | } |
168 | | |
169 | | EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp, |
170 | | long length) |
171 | 0 | { |
172 | 0 | return d2i_PrivateKey_ex(type, a, pp, length, NULL, NULL); |
173 | 0 | } |
174 | | |
175 | | static EVP_PKEY *d2i_AutoPrivateKey_legacy(EVP_PKEY **a, |
176 | | const unsigned char **pp, |
177 | | long length, |
178 | | OSSL_LIB_CTX *libctx, |
179 | | const char *propq) |
180 | 47.2k | { |
181 | 47.2k | STACK_OF(ASN1_TYPE) *inkey; |
182 | 47.2k | const unsigned char *p; |
183 | 47.2k | int keytype; |
184 | | |
185 | 47.2k | p = *pp; |
186 | | /* |
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. |
190 | | */ |
191 | 47.2k | inkey = d2i_ASN1_SEQUENCE_ANY(NULL, &p, length); |
192 | 47.2k | p = *pp; |
193 | | /* |
194 | | * Since we only need to discern "traditional format" RSA and DSA keys we |
195 | | * can just count the elements. |
196 | | */ |
197 | 47.2k | if (sk_ASN1_TYPE_num(inkey) == 6) { |
198 | 1.07k | keytype = EVP_PKEY_DSA; |
199 | 46.1k | } else if (sk_ASN1_TYPE_num(inkey) == 4) { |
200 | 1.62k | keytype = EVP_PKEY_EC; |
201 | 44.5k | } else if (sk_ASN1_TYPE_num(inkey) == 3) { /* This seems to be PKCS8, not |
202 | | * traditional format */ |
203 | 5.50k | PKCS8_PRIV_KEY_INFO *p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length); |
204 | 5.50k | EVP_PKEY *ret; |
205 | | |
206 | 5.50k | sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free); |
207 | 5.50k | if (p8 == NULL) { |
208 | 4.74k | ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE); |
209 | 4.74k | return NULL; |
210 | 4.74k | } |
211 | 758 | ret = evp_pkcs82pkey_legacy(p8, libctx, propq); |
212 | 758 | PKCS8_PRIV_KEY_INFO_free(p8); |
213 | 758 | if (ret == NULL) |
214 | 395 | return NULL; |
215 | 363 | *pp = p; |
216 | 363 | if (a != NULL) { |
217 | 0 | *a = ret; |
218 | 0 | } |
219 | 363 | return ret; |
220 | 39.0k | } else { |
221 | 39.0k | keytype = EVP_PKEY_RSA; |
222 | 39.0k | } |
223 | 41.7k | sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free); |
224 | 41.7k | return ossl_d2i_PrivateKey_legacy(keytype, a, pp, length, libctx, propq); |
225 | 47.2k | } |
226 | | |
227 | | /* |
228 | | * This works like d2i_PrivateKey() except it passes the keytype as |
229 | | * EVP_PKEY_NONE, which then figures out the type during decoding. |
230 | | */ |
231 | | EVP_PKEY *d2i_AutoPrivateKey_ex(EVP_PKEY **a, const unsigned char **pp, |
232 | | long length, OSSL_LIB_CTX *libctx, |
233 | | const char *propq) |
234 | 52.9k | { |
235 | 52.9k | EVP_PKEY *ret; |
236 | | |
237 | 52.9k | ret = d2i_PrivateKey_decoder(EVP_PKEY_NONE, a, pp, length, libctx, propq); |
238 | | /* try the legacy path if the decoder failed */ |
239 | 52.9k | if (ret == NULL) |
240 | 47.2k | ret = d2i_AutoPrivateKey_legacy(a, pp, length, libctx, propq); |
241 | 52.9k | return ret; |
242 | 52.9k | } |
243 | | |
244 | | EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **a, const unsigned char **pp, |
245 | | long length) |
246 | 52.9k | { |
247 | 52.9k | return d2i_AutoPrivateKey_ex(a, pp, length, NULL, NULL); |
248 | 52.9k | } |