EVP_PKCS82PKEY: Create provided keys if possible
[openssl.git] / crypto / asn1 / d2i_pr.c
1 /*
2  * Copyright 1995-2021 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
26 static EVP_PKEY *
27 d2i_PrivateKey_decoder(int keytype, EVP_PKEY **a, const unsigned char **pp,
28                        long length, OSSL_LIB_CTX *libctx, const char *propq)
29 {
30     OSSL_DECODER_CTX *dctx = NULL;
31     size_t len = length;
32     EVP_PKEY *pkey = NULL;
33     EVP_PKEY **ppkey = &pkey;
34     const char *key_name = NULL;
35     const char *input_structures[] = { "type-specific", "pkcs8", NULL };
36     int i, ret;
37
38     if (keytype != EVP_PKEY_NONE) {
39         key_name = evp_pkey_type2name(keytype);
40         if (key_name == NULL)
41             return NULL;
42     }
43     if (a != NULL && *a != NULL)
44         ppkey = a;
45
46     for (i = 0;  i < (int)OSSL_NELEM(input_structures); ++i) {
47         const unsigned char *p = *pp;
48
49         dctx = OSSL_DECODER_CTX_new_for_pkey(ppkey, "DER",
50                                              input_structures[i], key_name,
51                                              EVP_PKEY_KEYPAIR, libctx, propq);
52         if (dctx == NULL)
53             return NULL;
54
55         ret = OSSL_DECODER_from_data(dctx, pp, &len);
56         OSSL_DECODER_CTX_free(dctx);
57         if (ret) {
58             if (*ppkey != NULL
59                 && evp_keymgmt_util_has(*ppkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY))
60                 return *ppkey;
61             *pp = p;
62             goto err;
63         }
64     }
65     /* Fall through to error if all decodes failed */
66 err:
67     if (ppkey != a)
68         EVP_PKEY_free(*ppkey);
69     return NULL;
70 }
71
72 static EVP_PKEY *
73 d2i_PrivateKey_legacy(int keytype, EVP_PKEY **a, const unsigned char **pp,
74                       long length, OSSL_LIB_CTX *libctx, const char *propq)
75 {
76     EVP_PKEY *ret;
77     const unsigned char *p = *pp;
78
79     if ((a == NULL) || (*a == NULL)) {
80         if ((ret = EVP_PKEY_new()) == NULL) {
81             ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
82             return NULL;
83         }
84     } else {
85         ret = *a;
86 #ifndef OPENSSL_NO_ENGINE
87         ENGINE_finish(ret->engine);
88         ret->engine = NULL;
89 #endif
90     }
91
92     if (!EVP_PKEY_set_type(ret, keytype)) {
93         ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
94         goto err;
95     }
96
97     ERR_set_mark();
98     if (!ret->ameth->old_priv_decode ||
99         !ret->ameth->old_priv_decode(ret, &p, length)) {
100         if (ret->ameth->priv_decode != NULL
101                 || ret->ameth->priv_decode_ex != NULL) {
102             EVP_PKEY *tmp;
103             PKCS8_PRIV_KEY_INFO *p8 = NULL;
104             p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);
105             if (p8 == NULL) {
106                 ERR_clear_last_mark();
107                 goto err;
108             }
109             tmp = evp_pkcs82pkey_legacy(p8, libctx, propq);
110             PKCS8_PRIV_KEY_INFO_free(p8);
111             if (tmp == NULL) {
112                 ERR_clear_last_mark();
113                 goto err;
114             }
115             EVP_PKEY_free(ret);
116             ret = tmp;
117             ERR_pop_to_mark();
118             if (EVP_PKEY_type(keytype) != EVP_PKEY_base_id(ret))
119                 goto err;
120         } else {
121             ERR_clear_last_mark();
122             ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
123             goto err;
124         }
125     } else {
126       ERR_clear_last_mark();
127     }
128     *pp = p;
129     if (a != NULL)
130         (*a) = ret;
131     return ret;
132  err:
133     if (a == NULL || *a != ret)
134         EVP_PKEY_free(ret);
135     return NULL;
136 }
137
138 EVP_PKEY *d2i_PrivateKey_ex(int keytype, EVP_PKEY **a, const unsigned char **pp,
139                             long length, OSSL_LIB_CTX *libctx,
140                             const char *propq)
141 {
142     EVP_PKEY *ret;
143
144     ret = d2i_PrivateKey_decoder(keytype, a, pp, length, libctx, propq);
145     /* try the legacy path if the decoder failed */
146     if (ret == NULL)
147         ret = d2i_PrivateKey_legacy(keytype, a, pp, length, libctx, propq);
148     return ret;
149 }
150
151 EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp,
152                          long length)
153 {
154     return d2i_PrivateKey_ex(type, a, pp, length, NULL, NULL);
155 }
156
157 static EVP_PKEY *d2i_AutoPrivateKey_legacy(EVP_PKEY **a,
158                                            const unsigned char **pp,
159                                            long length,
160                                            OSSL_LIB_CTX *libctx,
161                                            const char *propq)
162 {
163     STACK_OF(ASN1_TYPE) *inkey;
164     const unsigned char *p;
165     int keytype;
166
167     p = *pp;
168     /*
169      * Dirty trick: read in the ASN1 data into a STACK_OF(ASN1_TYPE): by
170      * analyzing it we can determine the passed structure: this assumes the
171      * input is surrounded by an ASN1 SEQUENCE.
172      */
173     inkey = d2i_ASN1_SEQUENCE_ANY(NULL, &p, length);
174     p = *pp;
175     /*
176      * Since we only need to discern "traditional format" RSA and DSA keys we
177      * can just count the elements.
178      */
179     if (sk_ASN1_TYPE_num(inkey) == 6) {
180         keytype = EVP_PKEY_DSA;
181     } else if (sk_ASN1_TYPE_num(inkey) == 4) {
182         keytype = EVP_PKEY_EC;
183     } else if (sk_ASN1_TYPE_num(inkey) == 3) { /* This seems to be PKCS8, not
184                                               * traditional format */
185         PKCS8_PRIV_KEY_INFO *p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);
186         EVP_PKEY *ret;
187
188         sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
189         if (p8 == NULL) {
190             ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
191             return NULL;
192         }
193         ret = evp_pkcs82pkey_legacy(p8, libctx, propq);
194         PKCS8_PRIV_KEY_INFO_free(p8);
195         if (ret == NULL)
196             return NULL;
197         *pp = p;
198         if (a) {
199             *a = ret;
200         }
201         return ret;
202     } else {
203         keytype = EVP_PKEY_RSA;
204     }
205     sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
206     return d2i_PrivateKey_legacy(keytype, a, pp, length, libctx, propq);
207 }
208
209 /*
210  * This works like d2i_PrivateKey() except it passes the keytype as
211  * EVP_PKEY_NONE, which then figures out the type during decoding.
212  */
213 EVP_PKEY *d2i_AutoPrivateKey_ex(EVP_PKEY **a, const unsigned char **pp,
214                                 long length, OSSL_LIB_CTX *libctx,
215                                 const char *propq)
216 {
217     EVP_PKEY *ret;
218
219     ret = d2i_PrivateKey_decoder(EVP_PKEY_NONE, a, pp, length, libctx, propq);
220     /* try the legacy path if the decoder failed */
221     if (ret == NULL)
222         ret = d2i_AutoPrivateKey_legacy(a, pp, length, libctx, propq);
223     return ret;
224 }
225
226 EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **a, const unsigned char **pp,
227                              long length)
228 {
229     return d2i_AutoPrivateKey_ex(a, pp, length, NULL, NULL);
230 }