Prune low-level ASN.1 parse errors from error queue in der2key_decode() etc.
[openssl.git] / crypto / evp / evp_pkey.c
1 /*
2  * Copyright 1999-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 #include <stdio.h>
11 #include <stdlib.h>
12 #include "internal/cryptlib.h"
13 #include <openssl/x509.h>
14 #include <openssl/rand.h>
15 #include "crypto/asn1.h"
16 #include "crypto/evp.h"
17 #include "crypto/x509.h"
18
19 /* Extract a private key from a PKCS8 structure */
20
21 EVP_PKEY *EVP_PKCS82PKEY_with_libctx(const PKCS8_PRIV_KEY_INFO *p8,
22                                      OPENSSL_CTX *libctx, const char *propq)
23 {
24     EVP_PKEY *pkey = NULL;
25     const ASN1_OBJECT *algoid;
26     char obj_tmp[80];
27
28     if (!PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8))
29         return NULL;
30
31     if ((pkey = EVP_PKEY_new()) == NULL) {
32         EVPerr(0, ERR_R_MALLOC_FAILURE);
33         return NULL;
34     }
35
36     if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(algoid))) {
37         EVPerr(0, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
38         i2t_ASN1_OBJECT(obj_tmp, 80, algoid);
39         ERR_add_error_data(2, "TYPE=", obj_tmp);
40         goto error;
41     }
42
43     if (pkey->ameth->priv_decode_with_libctx != NULL) {
44         if (!pkey->ameth->priv_decode_with_libctx(pkey, p8, libctx, propq))
45             goto error;
46     } else if (pkey->ameth->priv_decode != NULL) {
47         if (!pkey->ameth->priv_decode(pkey, p8)) {
48             EVPerr(0, EVP_R_PRIVATE_KEY_DECODE_ERROR);
49             goto error;
50         }
51     } else {
52         EVPerr(0, EVP_R_METHOD_NOT_SUPPORTED);
53         goto error;
54     }
55
56     return pkey;
57
58  error:
59     EVP_PKEY_free(pkey);
60     return NULL;
61 }
62
63 EVP_PKEY *EVP_PKCS82PKEY(const PKCS8_PRIV_KEY_INFO *p8)
64 {
65     return EVP_PKCS82PKEY_with_libctx(p8, NULL, NULL);
66 }
67
68 /* Turn a private key into a PKCS8 structure */
69
70 PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(const EVP_PKEY *pkey)
71 {
72     PKCS8_PRIV_KEY_INFO *p8 = PKCS8_PRIV_KEY_INFO_new();
73     if (p8  == NULL) {
74         EVPerr(EVP_F_EVP_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
75         return NULL;
76     }
77
78     /* Force a key downgrade if that's possible */
79     /* TODO(3.0) Is there a better way for provider-native keys? */
80     if (EVP_PKEY_get0(pkey) == NULL)
81         return NULL;
82
83     if (pkey->ameth) {
84         if (pkey->ameth->priv_encode) {
85             if (!pkey->ameth->priv_encode(p8, pkey)) {
86                 EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_PRIVATE_KEY_ENCODE_ERROR);
87                 goto error;
88             }
89         } else {
90             EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_METHOD_NOT_SUPPORTED);
91             goto error;
92         }
93     } else {
94         EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
95         goto error;
96     }
97     return p8;
98  error:
99     PKCS8_PRIV_KEY_INFO_free(p8);
100     return NULL;
101 }
102
103 /* EVP_PKEY attribute functions */
104
105 int EVP_PKEY_get_attr_count(const EVP_PKEY *key)
106 {
107     return X509at_get_attr_count(key->attributes);
108 }
109
110 int EVP_PKEY_get_attr_by_NID(const EVP_PKEY *key, int nid, int lastpos)
111 {
112     return X509at_get_attr_by_NID(key->attributes, nid, lastpos);
113 }
114
115 int EVP_PKEY_get_attr_by_OBJ(const EVP_PKEY *key, const ASN1_OBJECT *obj,
116                              int lastpos)
117 {
118     return X509at_get_attr_by_OBJ(key->attributes, obj, lastpos);
119 }
120
121 X509_ATTRIBUTE *EVP_PKEY_get_attr(const EVP_PKEY *key, int loc)
122 {
123     return X509at_get_attr(key->attributes, loc);
124 }
125
126 X509_ATTRIBUTE *EVP_PKEY_delete_attr(EVP_PKEY *key, int loc)
127 {
128     return X509at_delete_attr(key->attributes, loc);
129 }
130
131 int EVP_PKEY_add1_attr(EVP_PKEY *key, X509_ATTRIBUTE *attr)
132 {
133     if (X509at_add1_attr(&key->attributes, attr))
134         return 1;
135     return 0;
136 }
137
138 int EVP_PKEY_add1_attr_by_OBJ(EVP_PKEY *key,
139                               const ASN1_OBJECT *obj, int type,
140                               const unsigned char *bytes, int len)
141 {
142     if (X509at_add1_attr_by_OBJ(&key->attributes, obj, type, bytes, len))
143         return 1;
144     return 0;
145 }
146
147 int EVP_PKEY_add1_attr_by_NID(EVP_PKEY *key,
148                               int nid, int type,
149                               const unsigned char *bytes, int len)
150 {
151     if (X509at_add1_attr_by_NID(&key->attributes, nid, type, bytes, len))
152         return 1;
153     return 0;
154 }
155
156 int EVP_PKEY_add1_attr_by_txt(EVP_PKEY *key,
157                               const char *attrname, int type,
158                               const unsigned char *bytes, int len)
159 {
160     if (X509at_add1_attr_by_txt(&key->attributes, attrname, type, bytes, len))
161         return 1;
162     return 0;
163 }
164
165 const char *EVP_PKEY_get0_first_alg_name(const EVP_PKEY *key)
166 {
167     const EVP_PKEY_ASN1_METHOD *ameth;
168     const char *name = NULL;
169
170     if (key->keymgmt != NULL)
171         return EVP_KEYMGMT_get0_first_name(key->keymgmt);
172
173     /* Otherwise fallback to legacy */
174     ameth = EVP_PKEY_get0_asn1(key);
175     if (ameth != NULL)
176         EVP_PKEY_asn1_get0_info(NULL, NULL,
177                                 NULL, NULL, &name, ameth);
178
179     return name;
180 }