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