1 /*
2 * Copyright 1999-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 /*
11 * Needed for EVP_PKEY_get0_asn1 and EVP_PKEY_asn1_get0_info
12 */
13 #define OPENSSL_SUPPRESS_DEPRECATED
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include "internal/cryptlib.h"
18 #include <openssl/x509.h>
19 #include <openssl/rand.h>
20 #include <openssl/encoder.h>
21 #include <openssl/decoder.h>
22 #include "internal/provider.h"
23 #include "internal/sizes.h"
24 #include "crypto/asn1.h"
25 #include "crypto/evp.h"
26 #include "crypto/x509.h"
27
28 /* Extract a private key from a PKCS8 structure */
29
evp_pkcs82pkey_legacy(const PKCS8_PRIV_KEY_INFO * p8,OSSL_LIB_CTX * libctx,const char * propq)30 EVP_PKEY *evp_pkcs82pkey_legacy(const PKCS8_PRIV_KEY_INFO *p8, OSSL_LIB_CTX *libctx,
31 const char *propq)
32 {
33 EVP_PKEY *pkey = NULL;
34 const ASN1_OBJECT *algoid;
35 char obj_tmp[80];
36
37 if (!PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8))
38 return NULL;
39
40 if ((pkey = EVP_PKEY_new()) == NULL) {
41 ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
42 return NULL;
43 }
44
45 if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(algoid))) {
46 i2t_ASN1_OBJECT(obj_tmp, 80, algoid);
47 ERR_raise_data(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM,
48 "TYPE=%s", obj_tmp);
49 goto error;
50 }
51
52 if (pkey->ameth->priv_decode_ex != NULL) {
53 if (!pkey->ameth->priv_decode_ex(pkey, p8, libctx, propq))
54 goto error;
55 } else if (pkey->ameth->priv_decode != NULL) {
56 if (!pkey->ameth->priv_decode(pkey, p8)) {
57 ERR_raise(ERR_LIB_EVP, EVP_R_PRIVATE_KEY_DECODE_ERROR);
58 goto error;
59 }
60 } else {
61 ERR_raise(ERR_LIB_EVP, EVP_R_METHOD_NOT_SUPPORTED);
62 goto error;
63 }
64
65 return pkey;
66
67 error:
68 EVP_PKEY_free(pkey);
69 return NULL;
70 }
71
EVP_PKCS82PKEY_ex(const PKCS8_PRIV_KEY_INFO * p8,OSSL_LIB_CTX * libctx,const char * propq)72 EVP_PKEY *EVP_PKCS82PKEY_ex(const PKCS8_PRIV_KEY_INFO *p8, OSSL_LIB_CTX *libctx,
73 const char *propq)
74 {
75 EVP_PKEY *pkey = NULL;
76 const unsigned char *p8_data = NULL;
77 unsigned char *encoded_data = NULL;
78 int encoded_len;
79 int selection;
80 size_t len;
81 OSSL_DECODER_CTX *dctx = NULL;
82 const ASN1_OBJECT *algoid = NULL;
83 char keytype[OSSL_MAX_NAME_SIZE];
84
85 if (p8 == NULL
86 || !PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8)
87 || !OBJ_obj2txt(keytype, sizeof(keytype), algoid, 0))
88 return NULL;
89
90 if ((encoded_len = i2d_PKCS8_PRIV_KEY_INFO(p8, &encoded_data)) <= 0
91 || encoded_data == NULL)
92 return NULL;
93
94 p8_data = encoded_data;
95 len = encoded_len;
96 selection = EVP_PKEY_KEYPAIR | EVP_PKEY_KEY_PARAMETERS;
97 dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "DER", "PrivateKeyInfo",
98 keytype, selection, libctx, propq);
99
100 if (dctx != NULL && OSSL_DECODER_CTX_get_num_decoders(dctx) == 0) {
101 OSSL_DECODER_CTX_free(dctx);
102
103 /*
104 * This could happen if OBJ_obj2txt() returned a text OID and the
105 * decoder has not got that OID as an alias. We fall back to a NULL
106 * keytype
107 */
108 dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "DER", "PrivateKeyInfo",
109 NULL, selection, libctx, propq);
110 }
111
112 if (dctx == NULL
113 || !OSSL_DECODER_from_data(dctx, &p8_data, &len))
114 /* try legacy */
115 pkey = evp_pkcs82pkey_legacy(p8, libctx, propq);
116
117 OPENSSL_clear_free(encoded_data, encoded_len);
118 OSSL_DECODER_CTX_free(dctx);
119 return pkey;
120 }
121
EVP_PKCS82PKEY(const PKCS8_PRIV_KEY_INFO * p8)122 EVP_PKEY *EVP_PKCS82PKEY(const PKCS8_PRIV_KEY_INFO *p8)
123 {
124 return EVP_PKCS82PKEY_ex(p8, NULL, NULL);
125 }
126
127 /* Turn a private key into a PKCS8 structure */
128
EVP_PKEY2PKCS8(const EVP_PKEY * pkey)129 PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(const EVP_PKEY *pkey)
130 {
131 PKCS8_PRIV_KEY_INFO *p8 = NULL;
132 OSSL_ENCODER_CTX *ctx = NULL;
133
134 /*
135 * The implementation for provider-native keys is to encode the
136 * key to a DER encoded PKCS#8 structure, then convert it to a
137 * PKCS8_PRIV_KEY_INFO with good old d2i functions.
138 */
139 if (evp_pkey_is_provided(pkey)) {
140 int selection = OSSL_KEYMGMT_SELECT_ALL;
141 unsigned char *der = NULL;
142 size_t derlen = 0;
143 const unsigned char *pp;
144
145 if ((ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection,
146 "DER", "PrivateKeyInfo",
147 NULL)) == NULL
148 || !OSSL_ENCODER_to_data(ctx, &der, &derlen))
149 goto error;
150
151 pp = der;
152 p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &pp, (long)derlen);
153 OPENSSL_free(der);
154 if (p8 == NULL)
155 goto error;
156 } else {
157 p8 = PKCS8_PRIV_KEY_INFO_new();
158 if (p8 == NULL) {
159 ERR_raise(ERR_LIB_EVP, ERR_R_ASN1_LIB);
160 return NULL;
161 }
162
163 if (pkey->ameth != NULL) {
164 if (pkey->ameth->priv_encode != NULL) {
165 if (!pkey->ameth->priv_encode(p8, pkey)) {
166 ERR_raise(ERR_LIB_EVP, EVP_R_PRIVATE_KEY_ENCODE_ERROR);
167 goto error;
168 }
169 } else {
170 ERR_raise(ERR_LIB_EVP, EVP_R_METHOD_NOT_SUPPORTED);
171 goto error;
172 }
173 } else {
174 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
175 goto error;
176 }
177 }
178 goto end;
179 error:
180 PKCS8_PRIV_KEY_INFO_free(p8);
181 p8 = NULL;
182 end:
183 OSSL_ENCODER_CTX_free(ctx);
184 return p8;
185
186 }
187
188 /* EVP_PKEY attribute functions */
189
EVP_PKEY_get_attr_count(const EVP_PKEY * key)190 int EVP_PKEY_get_attr_count(const EVP_PKEY *key)
191 {
192 return X509at_get_attr_count(key->attributes);
193 }
194
EVP_PKEY_get_attr_by_NID(const EVP_PKEY * key,int nid,int lastpos)195 int EVP_PKEY_get_attr_by_NID(const EVP_PKEY *key, int nid, int lastpos)
196 {
197 return X509at_get_attr_by_NID(key->attributes, nid, lastpos);
198 }
199
EVP_PKEY_get_attr_by_OBJ(const EVP_PKEY * key,const ASN1_OBJECT * obj,int lastpos)200 int EVP_PKEY_get_attr_by_OBJ(const EVP_PKEY *key, const ASN1_OBJECT *obj,
201 int lastpos)
202 {
203 return X509at_get_attr_by_OBJ(key->attributes, obj, lastpos);
204 }
205
EVP_PKEY_get_attr(const EVP_PKEY * key,int loc)206 X509_ATTRIBUTE *EVP_PKEY_get_attr(const EVP_PKEY *key, int loc)
207 {
208 return X509at_get_attr(key->attributes, loc);
209 }
210
EVP_PKEY_delete_attr(EVP_PKEY * key,int loc)211 X509_ATTRIBUTE *EVP_PKEY_delete_attr(EVP_PKEY *key, int loc)
212 {
213 return X509at_delete_attr(key->attributes, loc);
214 }
215
EVP_PKEY_add1_attr(EVP_PKEY * key,X509_ATTRIBUTE * attr)216 int EVP_PKEY_add1_attr(EVP_PKEY *key, X509_ATTRIBUTE *attr)
217 {
218 if (X509at_add1_attr(&key->attributes, attr))
219 return 1;
220 return 0;
221 }
222
EVP_PKEY_add1_attr_by_OBJ(EVP_PKEY * key,const ASN1_OBJECT * obj,int type,const unsigned char * bytes,int len)223 int EVP_PKEY_add1_attr_by_OBJ(EVP_PKEY *key,
224 const ASN1_OBJECT *obj, int type,
225 const unsigned char *bytes, int len)
226 {
227 if (X509at_add1_attr_by_OBJ(&key->attributes, obj, type, bytes, len))
228 return 1;
229 return 0;
230 }
231
EVP_PKEY_add1_attr_by_NID(EVP_PKEY * key,int nid,int type,const unsigned char * bytes,int len)232 int EVP_PKEY_add1_attr_by_NID(EVP_PKEY *key,
233 int nid, int type,
234 const unsigned char *bytes, int len)
235 {
236 if (X509at_add1_attr_by_NID(&key->attributes, nid, type, bytes, len))
237 return 1;
238 return 0;
239 }
240
EVP_PKEY_add1_attr_by_txt(EVP_PKEY * key,const char * attrname,int type,const unsigned char * bytes,int len)241 int EVP_PKEY_add1_attr_by_txt(EVP_PKEY *key,
242 const char *attrname, int type,
243 const unsigned char *bytes, int len)
244 {
245 if (X509at_add1_attr_by_txt(&key->attributes, attrname, type, bytes, len))
246 return 1;
247 return 0;
248 }
249
EVP_PKEY_get0_type_name(const EVP_PKEY * key)250 const char *EVP_PKEY_get0_type_name(const EVP_PKEY *key)
251 {
252 #ifndef OPENSSL_NO_DEPRECATED_3_6
253 const EVP_PKEY_ASN1_METHOD *ameth;
254 #endif
255 const char *name = NULL;
256
257 if (key->keymgmt != NULL)
258 return EVP_KEYMGMT_get0_name(key->keymgmt);
259
260 #ifndef OPENSSL_NO_DEPRECATED_3_6
261 /* Otherwise fallback to legacy */
262 ameth = EVP_PKEY_get0_asn1(key);
263 if (ameth != NULL)
264 EVP_PKEY_asn1_get0_info(NULL, NULL,
265 NULL, NULL, &name, ameth);
266 #endif
267
268 return name;
269 }
270
EVP_PKEY_get0_provider(const EVP_PKEY * key)271 const OSSL_PROVIDER *EVP_PKEY_get0_provider(const EVP_PKEY *key)
272 {
273 if (evp_pkey_is_provided(key))
274 return EVP_KEYMGMT_get0_provider(key->keymgmt);
275 return NULL;
276 }
277