xref: /openssl/crypto/asn1/d2i_pr.c (revision da1c088f)
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 *
d2i_PrivateKey_decoder(int keytype,EVP_PKEY ** a,const unsigned char ** pp,long length,OSSL_LIB_CTX * libctx,const char * propq)28 d2i_PrivateKey_decoder(int keytype, EVP_PKEY **a, const unsigned char **pp,
29                        long length, OSSL_LIB_CTX *libctx, const char *propq)
30 {
31     OSSL_DECODER_CTX *dctx = NULL;
32     size_t len = length;
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];
37     int ret;
38     const unsigned char *p = *pp;
39     const char *structure;
40     PKCS8_PRIV_KEY_INFO *p8info;
41     const ASN1_OBJECT *algoid;
42 
43     if (keytype != EVP_PKEY_NONE) {
44         key_name = evp_pkey_type2name(keytype);
45         if (key_name == NULL)
46             return NULL;
47     }
48 
49     /* This is just a probe. It might fail, so we ignore errors */
50     ERR_set_mark();
51     p8info = d2i_PKCS8_PRIV_KEY_INFO(NULL, pp, len);
52     ERR_pop_to_mark();
53     if (p8info != NULL) {
54         if (key_name == NULL
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);
60     } else {
61         structure = "type-specific";
62     }
63     *pp = p;
64 
65     if (a != NULL && (bak_a = *a) != NULL)
66         ppkey = a;
67     dctx = OSSL_DECODER_CTX_new_for_pkey(ppkey, "DER", structure, key_name,
68                                          EVP_PKEY_KEYPAIR, libctx, propq);
69     if (a != NULL)
70         *a = bak_a;
71     if (dctx == NULL)
72         goto err;
73 
74     ret = OSSL_DECODER_from_data(dctx, pp, &len);
75     OSSL_DECODER_CTX_free(dctx);
76     if (ret
77         && *ppkey != NULL
78         && evp_keymgmt_util_has(*ppkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY)) {
79         if (a != NULL)
80             *a = *ppkey;
81         return *ppkey;
82     }
83 
84  err:
85     if (ppkey != a)
86         EVP_PKEY_free(*ppkey);
87     return NULL;
88 }
89 
90 EVP_PKEY *
ossl_d2i_PrivateKey_legacy(int keytype,EVP_PKEY ** a,const unsigned char ** pp,long length,OSSL_LIB_CTX * libctx,const char * propq)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 {
94     EVP_PKEY *ret;
95     const unsigned char *p = *pp;
96 
97     if (a == NULL || *a == NULL) {
98         if ((ret = EVP_PKEY_new()) == NULL) {
99             ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
100             return NULL;
101         }
102     } else {
103         ret = *a;
104 #ifndef OPENSSL_NO_ENGINE
105         ENGINE_finish(ret->engine);
106         ret->engine = NULL;
107 #endif
108     }
109 
110     if (!EVP_PKEY_set_type(ret, keytype)) {
111         ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
112         goto err;
113     }
114 
115     ERR_set_mark();
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) {
120             EVP_PKEY *tmp;
121             PKCS8_PRIV_KEY_INFO *p8 = NULL;
122             p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);
123             if (p8 == NULL) {
124                 ERR_clear_last_mark();
125                 goto err;
126             }
127             tmp = evp_pkcs82pkey_legacy(p8, libctx, propq);
128             PKCS8_PRIV_KEY_INFO_free(p8);
129             if (tmp == NULL) {
130                 ERR_clear_last_mark();
131                 goto err;
132             }
133             EVP_PKEY_free(ret);
134             ret = tmp;
135             ERR_pop_to_mark();
136             if (EVP_PKEY_type(keytype) != EVP_PKEY_get_base_id(ret))
137                 goto err;
138         } else {
139             ERR_clear_last_mark();
140             ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
141             goto err;
142         }
143     } else {
144       ERR_clear_last_mark();
145     }
146     *pp = p;
147     if (a != NULL)
148         *a = ret;
149     return ret;
150  err:
151     if (a == NULL || *a != ret)
152         EVP_PKEY_free(ret);
153     return NULL;
154 }
155 
d2i_PrivateKey_ex(int keytype,EVP_PKEY ** a,const unsigned char ** pp,long length,OSSL_LIB_CTX * libctx,const char * propq)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 {
160     EVP_PKEY *ret;
161 
162     ret = d2i_PrivateKey_decoder(keytype, a, pp, length, libctx, propq);
163     /* try the legacy path if the decoder failed */
164     if (ret == NULL)
165         ret = ossl_d2i_PrivateKey_legacy(keytype, a, pp, length, libctx, propq);
166     return ret;
167 }
168 
d2i_PrivateKey(int type,EVP_PKEY ** a,const unsigned char ** pp,long length)169 EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp,
170                          long length)
171 {
172     return d2i_PrivateKey_ex(type, a, pp, length, NULL, NULL);
173 }
174 
d2i_AutoPrivateKey_legacy(EVP_PKEY ** a,const unsigned char ** pp,long length,OSSL_LIB_CTX * libctx,const char * propq)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 {
181     STACK_OF(ASN1_TYPE) *inkey;
182     const unsigned char *p;
183     int keytype;
184 
185     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     inkey = d2i_ASN1_SEQUENCE_ANY(NULL, &p, length);
192     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     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);
204         EVP_PKEY *ret;
205 
206         sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
207         if (p8 == NULL) {
208             ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
209             return NULL;
210         }
211         ret = evp_pkcs82pkey_legacy(p8, libctx, propq);
212         PKCS8_PRIV_KEY_INFO_free(p8);
213         if (ret == NULL)
214             return NULL;
215         *pp = p;
216         if (a != NULL) {
217             *a = ret;
218         }
219         return ret;
220     } else {
221         keytype = EVP_PKEY_RSA;
222     }
223     sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
224     return ossl_d2i_PrivateKey_legacy(keytype, a, pp, length, libctx, propq);
225 }
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  */
d2i_AutoPrivateKey_ex(EVP_PKEY ** a,const unsigned char ** pp,long length,OSSL_LIB_CTX * libctx,const char * propq)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 {
235     EVP_PKEY *ret;
236 
237     ret = d2i_PrivateKey_decoder(EVP_PKEY_NONE, a, pp, length, libctx, propq);
238     /* try the legacy path if the decoder failed */
239     if (ret == NULL)
240         ret = d2i_AutoPrivateKey_legacy(a, pp, length, libctx, propq);
241     return ret;
242 }
243 
d2i_AutoPrivateKey(EVP_PKEY ** a,const unsigned char ** pp,long length)244 EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **a, const unsigned char **pp,
245                              long length)
246 {
247     return d2i_AutoPrivateKey_ex(a, pp, length, NULL, NULL);
248 }
249