Fix safestack issues in asn1.h
[openssl.git] / crypto / asn1 / d2i_pr.c
1 /*
2  * Copyright 1995-2020 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/engine.h>
19 #include <openssl/x509.h>
20 #include <openssl/asn1.h>
21 #include "crypto/asn1.h"
22 #include "crypto/evp.h"
23
24 EVP_PKEY *d2i_PrivateKey_ex(int type, EVP_PKEY **a, const unsigned char **pp,
25                             long length, OPENSSL_CTX *libctx, const char *propq)
26 {
27     EVP_PKEY *ret;
28     const unsigned char *p = *pp;
29
30     if ((a == NULL) || (*a == NULL)) {
31         if ((ret = EVP_PKEY_new()) == NULL) {
32             ASN1err(0, ERR_R_EVP_LIB);
33             return NULL;
34         }
35     } else {
36         ret = *a;
37 #ifndef OPENSSL_NO_ENGINE
38         ENGINE_finish(ret->engine);
39         ret->engine = NULL;
40 #endif
41     }
42
43     if (!EVP_PKEY_set_type(ret, type)) {
44         ASN1err(0, ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
45         goto err;
46     }
47
48     if (!ret->ameth->old_priv_decode ||
49         !ret->ameth->old_priv_decode(ret, &p, length)) {
50         if (ret->ameth->priv_decode != NULL
51                 || ret->ameth->priv_decode_with_libctx != NULL) {
52             EVP_PKEY *tmp;
53             PKCS8_PRIV_KEY_INFO *p8 = NULL;
54             p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);
55             if (p8 == NULL)
56                 goto err;
57             tmp = EVP_PKCS82PKEY_with_libctx(p8, libctx, propq);
58             PKCS8_PRIV_KEY_INFO_free(p8);
59             if (tmp == NULL)
60                 goto err;
61             EVP_PKEY_free(ret);
62             ret = tmp;
63             if (EVP_PKEY_type(type) != EVP_PKEY_base_id(ret))
64                 goto err;
65         } else {
66             ASN1err(0, ERR_R_ASN1_LIB);
67             goto err;
68         }
69     }
70     *pp = p;
71     if (a != NULL)
72         (*a) = ret;
73     return ret;
74  err:
75     if (a == NULL || *a != ret)
76         EVP_PKEY_free(ret);
77     return NULL;
78 }
79
80 EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp,
81                          long length)
82 {
83     return d2i_PrivateKey_ex(type, a, pp, length, NULL, NULL);
84 }
85
86 /*
87  * This works like d2i_PrivateKey() except it automatically works out the
88  * type
89  */
90
91 EVP_PKEY *d2i_AutoPrivateKey_ex(EVP_PKEY **a, const unsigned char **pp,
92                                 long length, OPENSSL_CTX *libctx,
93                                 const char *propq)
94 {
95     STACK_OF(ASN1_TYPE) *inkey;
96     const unsigned char *p;
97     int keytype;
98     p = *pp;
99     /*
100      * Dirty trick: read in the ASN1 data into a STACK_OF(ASN1_TYPE): by
101      * analyzing it we can determine the passed structure: this assumes the
102      * input is surrounded by an ASN1 SEQUENCE.
103      */
104     inkey = d2i_ASN1_SEQUENCE_ANY(NULL, &p, length);
105     p = *pp;
106     /*
107      * Since we only need to discern "traditional format" RSA and DSA keys we
108      * can just count the elements.
109      */
110     if (sk_ASN1_TYPE_num(inkey) == 6) {
111         keytype = EVP_PKEY_DSA;
112     } else if (sk_ASN1_TYPE_num(inkey) == 4) {
113         keytype = EVP_PKEY_EC;
114     } else if (sk_ASN1_TYPE_num(inkey) == 3) { /* This seems to be PKCS8, not
115                                               * traditional format */
116         PKCS8_PRIV_KEY_INFO *p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);
117         EVP_PKEY *ret;
118
119         sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
120         if (p8 == NULL) {
121             ASN1err(0, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
122             return NULL;
123         }
124         ret = EVP_PKCS82PKEY_with_libctx(p8, libctx, propq);
125         PKCS8_PRIV_KEY_INFO_free(p8);
126         if (ret == NULL)
127             return NULL;
128         *pp = p;
129         if (a) {
130             *a = ret;
131         }
132         return ret;
133     } else {
134         keytype = EVP_PKEY_RSA;
135     }
136     sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
137     return d2i_PrivateKey_ex(keytype, a, pp, length, libctx, propq);
138 }
139
140 EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **a, const unsigned char **pp,
141                              long length)
142 {
143     return d2i_AutoPrivateKey_ex(a, pp, length, NULL, NULL);
144 }