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