/src/openssl/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  | 0  | { | 
31  | 0  |     OSSL_DECODER_CTX *dctx = NULL;  | 
32  | 0  |     size_t len = length;  | 
33  | 0  |     EVP_PKEY *pkey = NULL, *bak_a = NULL;  | 
34  | 0  |     EVP_PKEY **ppkey = &pkey;  | 
35  | 0  |     const char *key_name = NULL;  | 
36  | 0  |     char keytypebuf[OSSL_MAX_NAME_SIZE];  | 
37  | 0  |     int ret;  | 
38  | 0  |     const unsigned char *p = *pp;  | 
39  | 0  |     const char *structure;  | 
40  | 0  |     PKCS8_PRIV_KEY_INFO *p8info;  | 
41  | 0  |     const ASN1_OBJECT *algoid;  | 
42  |  | 
  | 
43  | 0  |     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  | 0  |     ERR_set_mark();  | 
51  | 0  |     p8info = d2i_PKCS8_PRIV_KEY_INFO(NULL, pp, len);  | 
52  | 0  |     ERR_pop_to_mark();  | 
53  | 0  |     if (p8info != NULL) { | 
54  | 0  |         if (key_name == NULL  | 
55  | 0  |                 && PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8info)  | 
56  | 0  |                 && OBJ_obj2txt(keytypebuf, sizeof(keytypebuf), algoid, 0))  | 
57  | 0  |             key_name = keytypebuf;  | 
58  | 0  |         structure = "PrivateKeyInfo";  | 
59  | 0  |         PKCS8_PRIV_KEY_INFO_free(p8info);  | 
60  | 0  |     } else { | 
61  | 0  |         structure = "type-specific";  | 
62  | 0  |     }  | 
63  | 0  |     *pp = p;  | 
64  |  | 
  | 
65  | 0  |     if (a != NULL && (bak_a = *a) != NULL)  | 
66  | 0  |         ppkey = a;  | 
67  | 0  |     dctx = OSSL_DECODER_CTX_new_for_pkey(ppkey, "DER", structure, key_name,  | 
68  | 0  |                                          EVP_PKEY_KEYPAIR, libctx, propq);  | 
69  | 0  |     if (a != NULL)  | 
70  | 0  |         *a = bak_a;  | 
71  | 0  |     if (dctx == NULL)  | 
72  | 0  |         goto err;  | 
73  |  |  | 
74  | 0  |     ret = OSSL_DECODER_from_data(dctx, pp, &len);  | 
75  | 0  |     OSSL_DECODER_CTX_free(dctx);  | 
76  | 0  |     if (ret  | 
77  | 0  |         && *ppkey != NULL  | 
78  | 0  |         && evp_keymgmt_util_has(*ppkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY)) { | 
79  | 0  |         if (a != NULL)  | 
80  | 0  |             *a = *ppkey;  | 
81  | 0  |         return *ppkey;  | 
82  | 0  |     }  | 
83  |  |  | 
84  | 0  |  err:  | 
85  | 0  |     if (ppkey != a)  | 
86  | 0  |         EVP_PKEY_free(*ppkey);  | 
87  | 0  |     return NULL;  | 
88  | 0  | }  | 
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  | 0  | { | 
94  | 0  |     EVP_PKEY *ret;  | 
95  | 0  |     const unsigned char *p = *pp;  | 
96  |  | 
  | 
97  | 0  |     if (a == NULL || *a == NULL) { | 
98  | 0  |         if ((ret = EVP_PKEY_new()) == NULL) { | 
99  | 0  |             ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);  | 
100  | 0  |             return NULL;  | 
101  | 0  |         }  | 
102  | 0  |     } 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  | 0  |     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  | 0  |     ERR_set_mark();  | 
116  | 0  |     if (!ret->ameth->old_priv_decode ||  | 
117  | 0  |         !ret->ameth->old_priv_decode(ret, &p, length)) { | 
118  | 0  |         if (ret->ameth->priv_decode != NULL  | 
119  | 0  |                 || ret->ameth->priv_decode_ex != NULL) { | 
120  | 0  |             EVP_PKEY *tmp;  | 
121  | 0  |             PKCS8_PRIV_KEY_INFO *p8 = NULL;  | 
122  | 0  |             p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);  | 
123  | 0  |             if (p8 == NULL) { | 
124  | 0  |                 ERR_clear_last_mark();  | 
125  | 0  |                 goto err;  | 
126  | 0  |             }  | 
127  | 0  |             tmp = evp_pkcs82pkey_legacy(p8, libctx, propq);  | 
128  | 0  |             PKCS8_PRIV_KEY_INFO_free(p8);  | 
129  | 0  |             if (tmp == NULL) { | 
130  | 0  |                 ERR_clear_last_mark();  | 
131  | 0  |                 goto err;  | 
132  | 0  |             }  | 
133  | 0  |             EVP_PKEY_free(ret);  | 
134  | 0  |             ret = tmp;  | 
135  | 0  |             ERR_pop_to_mark();  | 
136  | 0  |             if (EVP_PKEY_type(keytype) != EVP_PKEY_get_base_id(ret))  | 
137  | 0  |                 goto err;  | 
138  | 0  |         } 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  | 0  |     } else { | 
144  | 0  |       ERR_clear_last_mark();  | 
145  | 0  |     }  | 
146  | 0  |     *pp = p;  | 
147  | 0  |     if (a != NULL)  | 
148  | 0  |         *a = ret;  | 
149  | 0  |     return ret;  | 
150  | 0  |  err:  | 
151  | 0  |     if (a == NULL || *a != ret)  | 
152  | 0  |         EVP_PKEY_free(ret);  | 
153  | 0  |     return NULL;  | 
154  | 0  | }  | 
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  | 0  | { | 
181  | 0  |     STACK_OF(ASN1_TYPE) *inkey;  | 
182  | 0  |     const unsigned char *p;  | 
183  | 0  |     int keytype;  | 
184  |  | 
  | 
185  | 0  |     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  | 0  |     inkey = d2i_ASN1_SEQUENCE_ANY(NULL, &p, length);  | 
192  | 0  |     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  | 0  |     if (sk_ASN1_TYPE_num(inkey) == 6) { | 
198  | 0  |         keytype = EVP_PKEY_DSA;  | 
199  | 0  |     } else if (sk_ASN1_TYPE_num(inkey) == 4) { | 
200  | 0  |         keytype = EVP_PKEY_EC;  | 
201  | 0  |     } else if (sk_ASN1_TYPE_num(inkey) == 3) { /* This seems to be PKCS8, not | 
202  |  |                                               * traditional format */  | 
203  | 0  |         PKCS8_PRIV_KEY_INFO *p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);  | 
204  | 0  |         EVP_PKEY *ret;  | 
205  |  | 
  | 
206  | 0  |         sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);  | 
207  | 0  |         if (p8 == NULL) { | 
208  | 0  |             ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);  | 
209  | 0  |             return NULL;  | 
210  | 0  |         }  | 
211  | 0  |         ret = evp_pkcs82pkey_legacy(p8, libctx, propq);  | 
212  | 0  |         PKCS8_PRIV_KEY_INFO_free(p8);  | 
213  | 0  |         if (ret == NULL)  | 
214  | 0  |             return NULL;  | 
215  | 0  |         *pp = p;  | 
216  | 0  |         if (a != NULL) { | 
217  | 0  |             *a = ret;  | 
218  | 0  |         }  | 
219  | 0  |         return ret;  | 
220  | 0  |     } else { | 
221  | 0  |         keytype = EVP_PKEY_RSA;  | 
222  | 0  |     }  | 
223  | 0  |     sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);  | 
224  | 0  |     return ossl_d2i_PrivateKey_legacy(keytype, a, pp, length, libctx, propq);  | 
225  | 0  | }  | 
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  | 0  | { | 
235  | 0  |     EVP_PKEY *ret;  | 
236  |  | 
  | 
237  | 0  |     ret = d2i_PrivateKey_decoder(EVP_PKEY_NONE, a, pp, length, libctx, propq);  | 
238  |  |     /* try the legacy path if the decoder failed */  | 
239  | 0  |     if (ret == NULL)  | 
240  | 0  |         ret = d2i_AutoPrivateKey_legacy(a, pp, length, libctx, propq);  | 
241  | 0  |     return ret;  | 
242  | 0  | }  | 
243  |  |  | 
244  |  | EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **a, const unsigned char **pp,  | 
245  |  |                              long length)  | 
246  | 0  | { | 
247  | 0  |     return d2i_AutoPrivateKey_ex(a, pp, length, NULL, NULL);  | 
248  | 0  | }  |