]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/evp_pkey.c
Prune low-level ASN.1 parse errors from error queue in decoder_process()
[thirdparty/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 EVPerr(0, EVP_R_PRIVATE_KEY_DECODE_ERROR);
46 goto error;
47 }
48 } else if (pkey->ameth->priv_decode != NULL) {
49 if (!pkey->ameth->priv_decode(pkey, p8)) {
50 EVPerr(0, EVP_R_PRIVATE_KEY_DECODE_ERROR);
51 goto error;
52 }
53 } else {
54 EVPerr(0, EVP_R_METHOD_NOT_SUPPORTED);
55 goto error;
56 }
57
58 return pkey;
59
60 error:
61 EVP_PKEY_free(pkey);
62 return NULL;
63 }
64
65 EVP_PKEY *EVP_PKCS82PKEY(const PKCS8_PRIV_KEY_INFO *p8)
66 {
67 return EVP_PKCS82PKEY_with_libctx(p8, NULL, NULL);
68 }
69
70 /* Turn a private key into a PKCS8 structure */
71
72 PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(const EVP_PKEY *pkey)
73 {
74 PKCS8_PRIV_KEY_INFO *p8 = PKCS8_PRIV_KEY_INFO_new();
75 if (p8 == NULL) {
76 EVPerr(EVP_F_EVP_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
77 return NULL;
78 }
79
80 /* Force a key downgrade if that's possible */
81 /* TODO(3.0) Is there a better way for provider-native keys? */
82 if (EVP_PKEY_get0(pkey) == NULL)
83 return NULL;
84
85 if (pkey->ameth) {
86 if (pkey->ameth->priv_encode) {
87 if (!pkey->ameth->priv_encode(p8, pkey)) {
88 EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_PRIVATE_KEY_ENCODE_ERROR);
89 goto error;
90 }
91 } else {
92 EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_METHOD_NOT_SUPPORTED);
93 goto error;
94 }
95 } else {
96 EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
97 goto error;
98 }
99 return p8;
100 error:
101 PKCS8_PRIV_KEY_INFO_free(p8);
102 return NULL;
103 }
104
105 /* EVP_PKEY attribute functions */
106
107 int EVP_PKEY_get_attr_count(const EVP_PKEY *key)
108 {
109 return X509at_get_attr_count(key->attributes);
110 }
111
112 int EVP_PKEY_get_attr_by_NID(const EVP_PKEY *key, int nid, int lastpos)
113 {
114 return X509at_get_attr_by_NID(key->attributes, nid, lastpos);
115 }
116
117 int EVP_PKEY_get_attr_by_OBJ(const EVP_PKEY *key, const ASN1_OBJECT *obj,
118 int lastpos)
119 {
120 return X509at_get_attr_by_OBJ(key->attributes, obj, lastpos);
121 }
122
123 X509_ATTRIBUTE *EVP_PKEY_get_attr(const EVP_PKEY *key, int loc)
124 {
125 return X509at_get_attr(key->attributes, loc);
126 }
127
128 X509_ATTRIBUTE *EVP_PKEY_delete_attr(EVP_PKEY *key, int loc)
129 {
130 return X509at_delete_attr(key->attributes, loc);
131 }
132
133 int EVP_PKEY_add1_attr(EVP_PKEY *key, X509_ATTRIBUTE *attr)
134 {
135 if (X509at_add1_attr(&key->attributes, attr))
136 return 1;
137 return 0;
138 }
139
140 int EVP_PKEY_add1_attr_by_OBJ(EVP_PKEY *key,
141 const ASN1_OBJECT *obj, int type,
142 const unsigned char *bytes, int len)
143 {
144 if (X509at_add1_attr_by_OBJ(&key->attributes, obj, type, bytes, len))
145 return 1;
146 return 0;
147 }
148
149 int EVP_PKEY_add1_attr_by_NID(EVP_PKEY *key,
150 int nid, int type,
151 const unsigned char *bytes, int len)
152 {
153 if (X509at_add1_attr_by_NID(&key->attributes, nid, type, bytes, len))
154 return 1;
155 return 0;
156 }
157
158 int EVP_PKEY_add1_attr_by_txt(EVP_PKEY *key,
159 const char *attrname, int type,
160 const unsigned char *bytes, int len)
161 {
162 if (X509at_add1_attr_by_txt(&key->attributes, attrname, type, bytes, len))
163 return 1;
164 return 0;
165 }
166
167 const char *EVP_PKEY_get0_first_alg_name(const EVP_PKEY *key)
168 {
169 const EVP_PKEY_ASN1_METHOD *ameth;
170 const char *name = NULL;
171
172 if (key->keymgmt != NULL)
173 return EVP_KEYMGMT_get0_first_name(key->keymgmt);
174
175 /* Otherwise fallback to legacy */
176 ameth = EVP_PKEY_get0_asn1(key);
177 if (ameth != NULL)
178 EVP_PKEY_asn1_get0_info(NULL, NULL,
179 NULL, NULL, &name, ameth);
180
181 return name;
182 }