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