87091cf16b33642d17a57c47ec5685c69b329e90
[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 <openssl/encoder.h>
16 #include "internal/provider.h"
17 #include "crypto/asn1.h"
18 #include "crypto/evp.h"
19 #include "crypto/x509.h"
20
21 /* Extract a private key from a PKCS8 structure */
22
23 EVP_PKEY *EVP_PKCS82PKEY_ex(const PKCS8_PRIV_KEY_INFO *p8, OSSL_LIB_CTX *libctx,
24                             const char *propq)
25 {
26     EVP_PKEY *pkey = NULL;
27     const ASN1_OBJECT *algoid;
28     char obj_tmp[80];
29
30     if (!PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8))
31         return NULL;
32
33     if ((pkey = EVP_PKEY_new()) == NULL) {
34         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
35         return NULL;
36     }
37
38     if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(algoid))) {
39         i2t_ASN1_OBJECT(obj_tmp, 80, algoid);
40         ERR_raise_data(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM,
41                        "TYPE=%s", obj_tmp);
42         goto error;
43     }
44
45     if (pkey->ameth->priv_decode_ex != NULL) {
46         if (!pkey->ameth->priv_decode_ex(pkey, p8, libctx, propq))
47             goto error;
48     } else if (pkey->ameth->priv_decode != NULL) {
49         if (!pkey->ameth->priv_decode(pkey, p8)) {
50             ERR_raise(ERR_LIB_EVP, EVP_R_PRIVATE_KEY_DECODE_ERROR);
51             goto error;
52         }
53     } else {
54         ERR_raise(ERR_LIB_EVP, 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_ex(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 = NULL;
75     OSSL_ENCODER_CTX *ctx = NULL;
76
77     /*
78      * The implementation for provider-native keys is to encode the
79      * key to a DER encoded PKCS#8 structure, then convert it to a
80      * PKCS8_PRIV_KEY_INFO with good old d2i functions.
81      */
82     if (evp_pkey_is_provided(pkey)) {
83         int selection = OSSL_KEYMGMT_SELECT_ALL;
84         unsigned char *der = NULL;
85         size_t derlen = 0;
86         const unsigned char *pp;
87
88         if ((ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection,
89                                                  "DER", "pkcs8",
90                                                  NULL)) == NULL
91             || !OSSL_ENCODER_to_data(ctx, &der, &derlen))
92             goto error;
93
94         pp = der;
95         p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &pp, (long)derlen);
96         OPENSSL_free(der);
97         if (p8 == NULL)
98             goto error;
99     } else {
100         p8 = PKCS8_PRIV_KEY_INFO_new();
101         if (p8  == NULL) {
102             ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
103             return NULL;
104         }
105
106         if (pkey->ameth != NULL) {
107             if (pkey->ameth->priv_encode != NULL) {
108                 if (!pkey->ameth->priv_encode(p8, pkey)) {
109                     ERR_raise(ERR_LIB_EVP, EVP_R_PRIVATE_KEY_ENCODE_ERROR);
110                     goto error;
111                 }
112             } else {
113                 ERR_raise(ERR_LIB_EVP, EVP_R_METHOD_NOT_SUPPORTED);
114                 goto error;
115             }
116         } else {
117             ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
118             goto error;
119         }
120     }
121     goto end;
122  error:
123     PKCS8_PRIV_KEY_INFO_free(p8);
124     p8 = NULL;
125  end:
126     OSSL_ENCODER_CTX_free(ctx);
127     return p8;
128
129 }
130
131 /* EVP_PKEY attribute functions */
132
133 int EVP_PKEY_get_attr_count(const EVP_PKEY *key)
134 {
135     return X509at_get_attr_count(key->attributes);
136 }
137
138 int EVP_PKEY_get_attr_by_NID(const EVP_PKEY *key, int nid, int lastpos)
139 {
140     return X509at_get_attr_by_NID(key->attributes, nid, lastpos);
141 }
142
143 int EVP_PKEY_get_attr_by_OBJ(const EVP_PKEY *key, const ASN1_OBJECT *obj,
144                              int lastpos)
145 {
146     return X509at_get_attr_by_OBJ(key->attributes, obj, lastpos);
147 }
148
149 X509_ATTRIBUTE *EVP_PKEY_get_attr(const EVP_PKEY *key, int loc)
150 {
151     return X509at_get_attr(key->attributes, loc);
152 }
153
154 X509_ATTRIBUTE *EVP_PKEY_delete_attr(EVP_PKEY *key, int loc)
155 {
156     return X509at_delete_attr(key->attributes, loc);
157 }
158
159 int EVP_PKEY_add1_attr(EVP_PKEY *key, X509_ATTRIBUTE *attr)
160 {
161     if (X509at_add1_attr(&key->attributes, attr))
162         return 1;
163     return 0;
164 }
165
166 int EVP_PKEY_add1_attr_by_OBJ(EVP_PKEY *key,
167                               const ASN1_OBJECT *obj, int type,
168                               const unsigned char *bytes, int len)
169 {
170     if (X509at_add1_attr_by_OBJ(&key->attributes, obj, type, bytes, len))
171         return 1;
172     return 0;
173 }
174
175 int EVP_PKEY_add1_attr_by_NID(EVP_PKEY *key,
176                               int nid, int type,
177                               const unsigned char *bytes, int len)
178 {
179     if (X509at_add1_attr_by_NID(&key->attributes, nid, type, bytes, len))
180         return 1;
181     return 0;
182 }
183
184 int EVP_PKEY_add1_attr_by_txt(EVP_PKEY *key,
185                               const char *attrname, int type,
186                               const unsigned char *bytes, int len)
187 {
188     if (X509at_add1_attr_by_txt(&key->attributes, attrname, type, bytes, len))
189         return 1;
190     return 0;
191 }
192
193 const char *EVP_PKEY_get0_first_alg_name(const EVP_PKEY *key)
194 {
195     const EVP_PKEY_ASN1_METHOD *ameth;
196     const char *name = NULL;
197
198     if (key->keymgmt != NULL)
199         return EVP_KEYMGMT_get0_first_name(key->keymgmt);
200
201     /* Otherwise fallback to legacy */
202     ameth = EVP_PKEY_get0_asn1(key);
203     if (ameth != NULL)
204         EVP_PKEY_asn1_get0_info(NULL, NULL,
205                                 NULL, NULL, &name, ameth);
206
207     return name;
208 }