Implement provider-side keymgmt_dup function
[openssl.git] / crypto / ec / ecx_backend.c
1 /*
2  * Copyright 2020-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 <string.h>
11 #include <openssl/core_names.h>
12 #include <openssl/params.h>
13 #include <openssl/ec.h>
14 #include <openssl/rand.h>
15 #include <openssl/err.h>
16 #include "crypto/ecx.h"
17 #include "ecx_backend.h"
18
19 /*
20  * The intention with the "backend" source file is to offer backend support
21  * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
22  * implementations alike.
23  */
24
25 int ossl_ecx_public_from_private(ECX_KEY *key)
26 {
27     switch (key->type) {
28     case ECX_KEY_TYPE_X25519:
29         ossl_x25519_public_from_private(key->pubkey, key->privkey);
30         break;
31     case ECX_KEY_TYPE_ED25519:
32         if (!ossl_ed25519_public_from_private(key->libctx, key->pubkey,
33                                               key->privkey, key->propq)) {
34             ERR_raise(ERR_LIB_EC, EC_R_FAILED_MAKING_PUBLIC_KEY);
35             return 0;
36         }
37         break;
38     case ECX_KEY_TYPE_X448:
39         ossl_x448_public_from_private(key->pubkey, key->privkey);
40         break;
41     case ECX_KEY_TYPE_ED448:
42         if (!ossl_ed448_public_from_private(key->libctx, key->pubkey,
43                                             key->privkey, key->propq)) {
44             ERR_raise(ERR_LIB_EC, EC_R_FAILED_MAKING_PUBLIC_KEY);
45             return 0;
46         }
47         break;
48     }
49     return 1;
50 }
51
52 int ossl_ecx_key_fromdata(ECX_KEY *ecx, const OSSL_PARAM params[],
53                           int include_private)
54 {
55     size_t privkeylen = 0, pubkeylen = 0;
56     const OSSL_PARAM *param_priv_key = NULL, *param_pub_key;
57     unsigned char *pubkey;
58
59     if (ecx == NULL)
60         return 0;
61
62     param_pub_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
63     if (include_private)
64         param_priv_key =
65             OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
66
67     if (param_pub_key == NULL && param_priv_key == NULL)
68         return 0;
69
70     if (param_priv_key != NULL
71         && !OSSL_PARAM_get_octet_string(param_priv_key,
72                                         (void **)&ecx->privkey, ecx->keylen,
73                                         &privkeylen))
74         return 0;
75
76     pubkey = ecx->pubkey;
77     if (param_pub_key != NULL
78         && !OSSL_PARAM_get_octet_string(param_pub_key,
79                                         (void **)&pubkey,
80                                          sizeof(ecx->pubkey), &pubkeylen))
81         return 0;
82
83     if ((param_pub_key != NULL && pubkeylen != ecx->keylen)
84         || (param_priv_key != NULL && privkeylen != ecx->keylen))
85         return 0;
86
87     if (param_pub_key == NULL && !ossl_ecx_public_from_private(ecx))
88         return 0;
89
90     ecx->haspubkey = 1;
91
92     return 1;
93 }
94
95 ECX_KEY *ossl_ecx_key_dup(const ECX_KEY *key)
96 {
97     ECX_KEY *ret = OPENSSL_zalloc(sizeof(*ret));
98
99     if (ret == NULL) {
100         ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
101         return NULL;
102     }
103
104     ret->lock = CRYPTO_THREAD_lock_new();
105     if (ret->lock == NULL) {
106         OPENSSL_free(ret);
107         return NULL;
108     }
109
110     ret->libctx = key->libctx;
111     ret->haspubkey = key->haspubkey;
112     ret->keylen = key->keylen;
113     ret->type = key->type;
114     ret->references = 1;
115
116     if (key->propq != NULL) {
117         ret->propq = OPENSSL_strdup(key->propq);
118         if (ret->propq == NULL)
119             goto err;
120     }
121
122     memcpy(ret->pubkey, key->pubkey, sizeof(ret->pubkey));
123
124     if (key->privkey != NULL) {
125         if (ossl_ecx_key_allocate_privkey(ret) == NULL)
126             goto err;
127         memcpy(ret->privkey, key->privkey, ret->keylen);
128     }
129
130     return ret;
131
132 err:
133     ossl_ecx_key_free(ret);
134     ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
135     return NULL;
136 }
137
138 #ifndef FIPS_MODULE
139 ECX_KEY *ossl_ecx_key_op(const X509_ALGOR *palg,
140                          const unsigned char *p, int plen,
141                          int id, ecx_key_op_t op,
142                          OSSL_LIB_CTX *libctx, const char *propq)
143 {
144     ECX_KEY *key = NULL;
145     unsigned char *privkey, *pubkey;
146
147     if (op != KEY_OP_KEYGEN) {
148         if (palg != NULL) {
149             int ptype;
150
151             /* Algorithm parameters must be absent */
152             X509_ALGOR_get0(NULL, &ptype, NULL, palg);
153             if (ptype != V_ASN1_UNDEF) {
154                 ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
155                 return 0;
156             }
157             if (id == EVP_PKEY_NONE)
158                 id = OBJ_obj2nid(palg->algorithm);
159             else if (id != OBJ_obj2nid(palg->algorithm)) {
160                 ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
161                 return 0;
162             }
163         }
164
165         if (p == NULL || id == EVP_PKEY_NONE || plen != KEYLENID(id)) {
166             ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
167             return 0;
168         }
169     }
170
171     key = ossl_ecx_key_new(libctx, KEYNID2TYPE(id), 1, propq);
172     if (key == NULL) {
173         ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
174         return 0;
175     }
176     pubkey = key->pubkey;
177
178     if (op == KEY_OP_PUBLIC) {
179         memcpy(pubkey, p, plen);
180     } else {
181         privkey = ossl_ecx_key_allocate_privkey(key);
182         if (privkey == NULL) {
183             ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
184             goto err;
185         }
186         if (op == KEY_OP_KEYGEN) {
187             if (id != EVP_PKEY_NONE) {
188                 if (RAND_priv_bytes_ex(libctx, privkey, KEYLENID(id)) <= 0)
189                     goto err;
190                 if (id == EVP_PKEY_X25519) {
191                     privkey[0] &= 248;
192                     privkey[X25519_KEYLEN - 1] &= 127;
193                     privkey[X25519_KEYLEN - 1] |= 64;
194                 } else if (id == EVP_PKEY_X448) {
195                     privkey[0] &= 252;
196                     privkey[X448_KEYLEN - 1] |= 128;
197                 }
198             }
199         } else {
200             memcpy(privkey, p, KEYLENID(id));
201         }
202         if (!ossl_ecx_public_from_private(key)) {
203             ERR_raise(ERR_LIB_EC, EC_R_FAILED_MAKING_PUBLIC_KEY);
204             goto err;
205         }
206     }
207
208     return key;
209  err:
210     ossl_ecx_key_free(key);
211     return NULL;
212 }
213
214 ECX_KEY *ossl_ecx_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
215                                  OSSL_LIB_CTX *libctx, const char *propq)
216 {
217     ECX_KEY *ecx = NULL;
218     const unsigned char *p;
219     int plen;
220     ASN1_OCTET_STRING *oct = NULL;
221     const X509_ALGOR *palg;
222
223     if (!PKCS8_pkey_get0(NULL, &p, &plen, &palg, p8inf))
224         return 0;
225
226     oct = d2i_ASN1_OCTET_STRING(NULL, &p, plen);
227     if (oct == NULL) {
228         p = NULL;
229         plen = 0;
230     } else {
231         p = ASN1_STRING_get0_data(oct);
232         plen = ASN1_STRING_length(oct);
233     }
234
235     /*
236      * EVP_PKEY_NONE means that ecx_key_op() has to figure out the key type
237      * on its own.
238      */
239     ecx = ossl_ecx_key_op(palg, p, plen, EVP_PKEY_NONE, KEY_OP_PRIVATE,
240                           libctx, propq);
241     ASN1_OCTET_STRING_free(oct);
242     return ecx;
243 }
244 #endif