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