]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
33388b44 | 2 | * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved. |
448be743 | 3 | * |
a7f182b7 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
aa6bb135 RS |
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 | |
448be743 DSH |
8 | */ |
9 | ||
579422c8 P |
10 | /* |
11 | * ECDH and ECDSA low level APIs are deprecated for public use, but still ok | |
12 | * for internal use. | |
13 | */ | |
14 | #include "internal/deprecated.h" | |
15 | ||
448be743 | 16 | #include <stdio.h> |
b39fc560 | 17 | #include "internal/cryptlib.h" |
448be743 DSH |
18 | #include <openssl/x509.h> |
19 | #include <openssl/ec.h> | |
1e26a8ba | 20 | #include <openssl/bn.h> |
88e20b85 | 21 | #include <openssl/asn1t.h> |
25f2138b DMSP |
22 | #include "crypto/asn1.h" |
23 | #include "crypto/evp.h" | |
22b81444 | 24 | #include "crypto/x509.h" |
4fe54d67 | 25 | #include <openssl/core_names.h> |
110bff61 | 26 | #include "openssl/param_build.h" |
706457b7 | 27 | #include "ec_local.h" |
448be743 | 28 | |
cd701de9 | 29 | static int eckey_param2type(int *pptype, void **ppval, const EC_KEY *ec_key) |
0f113f3e MC |
30 | { |
31 | const EC_GROUP *group; | |
32 | int nid; | |
83156454 | 33 | |
0f113f3e | 34 | if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) { |
9311d0c4 | 35 | ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS); |
0f113f3e MC |
36 | return 0; |
37 | } | |
38 | if (EC_GROUP_get_asn1_flag(group) | |
39 | && (nid = EC_GROUP_get_curve_name(group))) | |
40 | /* we have a 'named curve' => just set the OID */ | |
41 | { | |
e0137ca9 NT |
42 | ASN1_OBJECT *asn1obj = OBJ_nid2obj(nid); |
43 | ||
44 | if (asn1obj == NULL || OBJ_length(asn1obj) == 0) { | |
45 | ASN1_OBJECT_free(asn1obj); | |
9311d0c4 | 46 | ERR_raise(ERR_LIB_EC, EC_R_MISSING_OID); |
e0137ca9 NT |
47 | return 0; |
48 | } | |
49 | *ppval = asn1obj; | |
0f113f3e MC |
50 | *pptype = V_ASN1_OBJECT; |
51 | } else { /* explicit parameters */ | |
52 | ||
53 | ASN1_STRING *pstr = NULL; | |
54 | pstr = ASN1_STRING_new(); | |
90945fa3 | 55 | if (pstr == NULL) |
0f113f3e MC |
56 | return 0; |
57 | pstr->length = i2d_ECParameters(ec_key, &pstr->data); | |
58 | if (pstr->length <= 0) { | |
59 | ASN1_STRING_free(pstr); | |
9311d0c4 | 60 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
61 | return 0; |
62 | } | |
63 | *ppval = pstr; | |
64 | *pptype = V_ASN1_SEQUENCE; | |
65 | } | |
66 | return 1; | |
67 | } | |
448be743 | 68 | |
6f81892e | 69 | static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) |
0f113f3e | 70 | { |
cd701de9 | 71 | const EC_KEY *ec_key = pkey->pkey.ec; |
0f113f3e MC |
72 | void *pval = NULL; |
73 | int ptype; | |
74 | unsigned char *penc = NULL, *p; | |
75 | int penclen; | |
76 | ||
77 | if (!eckey_param2type(&ptype, &pval, ec_key)) { | |
9311d0c4 | 78 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
79 | return 0; |
80 | } | |
81 | penclen = i2o_ECPublicKey(ec_key, NULL); | |
82 | if (penclen <= 0) | |
83 | goto err; | |
84 | penc = OPENSSL_malloc(penclen); | |
90945fa3 | 85 | if (penc == NULL) |
0f113f3e MC |
86 | goto err; |
87 | p = penc; | |
88 | penclen = i2o_ECPublicKey(ec_key, &p); | |
89 | if (penclen <= 0) | |
90 | goto err; | |
91 | if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC), | |
92 | ptype, pval, penc, penclen)) | |
93 | return 1; | |
94 | err: | |
95 | if (ptype == V_ASN1_OBJECT) | |
96 | ASN1_OBJECT_free(pval); | |
97 | else | |
98 | ASN1_STRING_free(pval); | |
b548a1f1 | 99 | OPENSSL_free(penc); |
0f113f3e MC |
100 | return 0; |
101 | } | |
448be743 | 102 | |
febe6bb7 | 103 | static EC_KEY *eckey_type2param(int ptype, const void *pval, |
b4250010 | 104 | OSSL_LIB_CTX *libctx, const char *propq) |
0f113f3e MC |
105 | { |
106 | EC_KEY *eckey = NULL; | |
037241bf RS |
107 | EC_GROUP *group = NULL; |
108 | ||
d8652be0 | 109 | if ((eckey = EC_KEY_new_ex(libctx, propq)) == NULL) { |
9311d0c4 | 110 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
febe6bb7 MC |
111 | goto ecerr; |
112 | } | |
113 | ||
0f113f3e | 114 | if (ptype == V_ASN1_SEQUENCE) { |
ac4e2577 | 115 | const ASN1_STRING *pstr = pval; |
037241bf RS |
116 | const unsigned char *pm = pstr->data; |
117 | int pmlen = pstr->length; | |
118 | ||
febe6bb7 MC |
119 | |
120 | if (d2i_ECParameters(&eckey, &pm, pmlen) == NULL) { | |
9311d0c4 | 121 | ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR); |
0f113f3e MC |
122 | goto ecerr; |
123 | } | |
124 | } else if (ptype == V_ASN1_OBJECT) { | |
ac4e2577 | 125 | const ASN1_OBJECT *poid = pval; |
0f113f3e MC |
126 | |
127 | /* | |
128 | * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID | |
129 | */ | |
febe6bb7 | 130 | |
d8652be0 | 131 | group = EC_GROUP_new_by_curve_name_ex(libctx, propq, OBJ_obj2nid(poid)); |
0f113f3e MC |
132 | if (group == NULL) |
133 | goto ecerr; | |
134 | EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE); | |
135 | if (EC_KEY_set_group(eckey, group) == 0) | |
136 | goto ecerr; | |
137 | EC_GROUP_free(group); | |
138 | } else { | |
9311d0c4 | 139 | ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR); |
0f113f3e MC |
140 | goto ecerr; |
141 | } | |
142 | ||
143 | return eckey; | |
144 | ||
145 | ecerr: | |
8fdc3734 | 146 | EC_KEY_free(eckey); |
037241bf | 147 | EC_GROUP_free(group); |
0f113f3e MC |
148 | return NULL; |
149 | } | |
448be743 | 150 | |
7674e923 | 151 | static int eckey_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey) |
0f113f3e MC |
152 | { |
153 | const unsigned char *p = NULL; | |
ac4e2577 | 154 | const void *pval; |
0f113f3e MC |
155 | int ptype, pklen; |
156 | EC_KEY *eckey = NULL; | |
157 | X509_ALGOR *palg; | |
b4250010 | 158 | OSSL_LIB_CTX *libctx = NULL; |
22b81444 | 159 | const char *propq = NULL; |
0f113f3e | 160 | |
22b81444 RL |
161 | if (!X509_PUBKEY_get0_libctx(&libctx, &propq, pubkey) |
162 | || !X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey)) | |
0f113f3e MC |
163 | return 0; |
164 | X509_ALGOR_get0(NULL, &ptype, &pval, palg); | |
165 | ||
22b81444 | 166 | eckey = eckey_type2param(ptype, pval, libctx, propq); |
0f113f3e | 167 | |
66066e1b | 168 | if (!eckey) |
0f113f3e | 169 | return 0; |
0f113f3e MC |
170 | |
171 | /* We have parameters now set public key */ | |
172 | if (!o2i_ECPublicKey(&eckey, &p, pklen)) { | |
9311d0c4 | 173 | ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR); |
0f113f3e MC |
174 | goto ecerr; |
175 | } | |
176 | ||
177 | EVP_PKEY_assign_EC_KEY(pkey, eckey); | |
178 | return 1; | |
179 | ||
180 | ecerr: | |
8fdc3734 | 181 | EC_KEY_free(eckey); |
0f113f3e MC |
182 | return 0; |
183 | } | |
448be743 | 184 | |
6f81892e | 185 | static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) |
0f113f3e MC |
186 | { |
187 | int r; | |
188 | const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec); | |
189 | const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec), | |
190 | *pb = EC_KEY_get0_public_key(b->pkey.ec); | |
83156454 | 191 | |
978ecbb0 DW |
192 | if (group == NULL || pa == NULL || pb == NULL) |
193 | return -2; | |
0f113f3e MC |
194 | r = EC_POINT_cmp(group, pa, pb, NULL); |
195 | if (r == 0) | |
196 | return 1; | |
197 | if (r == 1) | |
198 | return 0; | |
199 | return -2; | |
200 | } | |
6f81892e | 201 | |
d8652be0 | 202 | static int eckey_priv_decode_ex(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8, |
b4250010 | 203 | OSSL_LIB_CTX *libctx, const char *propq) |
0f113f3e MC |
204 | { |
205 | const unsigned char *p = NULL; | |
ac4e2577 | 206 | const void *pval; |
0f113f3e MC |
207 | int ptype, pklen; |
208 | EC_KEY *eckey = NULL; | |
245c6bc3 | 209 | const X509_ALGOR *palg; |
0f113f3e MC |
210 | |
211 | if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8)) | |
212 | return 0; | |
213 | X509_ALGOR_get0(NULL, &ptype, &pval, palg); | |
214 | ||
febe6bb7 | 215 | eckey = eckey_type2param(ptype, pval, libctx, propq); |
12a765a5 | 216 | if (eckey == NULL) |
66066e1b | 217 | goto err; |
0f113f3e MC |
218 | |
219 | /* We have parameters now set private key */ | |
220 | if (!d2i_ECPrivateKey(&eckey, &p, pklen)) { | |
9311d0c4 | 221 | ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR); |
66066e1b | 222 | goto err; |
0f113f3e MC |
223 | } |
224 | ||
0f113f3e MC |
225 | EVP_PKEY_assign_EC_KEY(pkey, eckey); |
226 | return 1; | |
227 | ||
66066e1b | 228 | err: |
8fdc3734 | 229 | EC_KEY_free(eckey); |
0f113f3e MC |
230 | return 0; |
231 | } | |
448be743 | 232 | |
6f81892e | 233 | static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) |
448be743 | 234 | { |
b8a7bd83 | 235 | EC_KEY ec_key = *(pkey->pkey.ec); |
0f113f3e MC |
236 | unsigned char *ep, *p; |
237 | int eplen, ptype; | |
238 | void *pval; | |
b8a7bd83 | 239 | unsigned int old_flags; |
0f113f3e | 240 | |
b8a7bd83 | 241 | if (!eckey_param2type(&ptype, &pval, &ec_key)) { |
9311d0c4 | 242 | ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR); |
0f113f3e MC |
243 | return 0; |
244 | } | |
245 | ||
246 | /* set the private key */ | |
247 | ||
248 | /* | |
249 | * do not include the parameters in the SEC1 private key see PKCS#11 | |
250 | * 12.11 | |
251 | */ | |
b8a7bd83 RL |
252 | old_flags = EC_KEY_get_enc_flags(&ec_key); |
253 | EC_KEY_set_enc_flags(&ec_key, old_flags | EC_PKEY_NO_PARAMETERS); | |
254 | ||
255 | eplen = i2d_ECPrivateKey(&ec_key, NULL); | |
0f113f3e | 256 | if (!eplen) { |
9311d0c4 | 257 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
258 | return 0; |
259 | } | |
b196e7d9 | 260 | ep = OPENSSL_malloc(eplen); |
90945fa3 | 261 | if (ep == NULL) { |
9311d0c4 | 262 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
263 | return 0; |
264 | } | |
265 | p = ep; | |
b8a7bd83 | 266 | if (!i2d_ECPrivateKey(&ec_key, &p)) { |
0f113f3e | 267 | OPENSSL_free(ep); |
9311d0c4 | 268 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
269 | return 0; |
270 | } | |
0f113f3e MC |
271 | |
272 | if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0, | |
e0670973 Y |
273 | ptype, pval, ep, eplen)) { |
274 | OPENSSL_free(ep); | |
0f113f3e | 275 | return 0; |
e0670973 | 276 | } |
0f113f3e MC |
277 | |
278 | return 1; | |
448be743 DSH |
279 | } |
280 | ||
6f81892e | 281 | static int int_ec_size(const EVP_PKEY *pkey) |
0f113f3e MC |
282 | { |
283 | return ECDSA_size(pkey->pkey.ec); | |
284 | } | |
6f81892e DSH |
285 | |
286 | static int ec_bits(const EVP_PKEY *pkey) | |
0f113f3e | 287 | { |
be2e334f | 288 | return EC_GROUP_order_bits(EC_KEY_get0_group(pkey->pkey.ec)); |
0f113f3e | 289 | } |
6f81892e | 290 | |
2514fa79 | 291 | static int ec_security_bits(const EVP_PKEY *pkey) |
0f113f3e MC |
292 | { |
293 | int ecbits = ec_bits(pkey); | |
83156454 | 294 | |
0f113f3e MC |
295 | if (ecbits >= 512) |
296 | return 256; | |
297 | if (ecbits >= 384) | |
298 | return 192; | |
299 | if (ecbits >= 256) | |
300 | return 128; | |
301 | if (ecbits >= 224) | |
302 | return 112; | |
303 | if (ecbits >= 160) | |
304 | return 80; | |
305 | return ecbits / 2; | |
306 | } | |
2514fa79 | 307 | |
6f81892e | 308 | static int ec_missing_parameters(const EVP_PKEY *pkey) |
0f113f3e | 309 | { |
f72f00d4 | 310 | if (pkey->pkey.ec == NULL || EC_KEY_get0_group(pkey->pkey.ec) == NULL) |
0f113f3e MC |
311 | return 1; |
312 | return 0; | |
313 | } | |
6f81892e | 314 | |
930b0c4b | 315 | static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) |
0f113f3e MC |
316 | { |
317 | EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec)); | |
188a9bd9 | 318 | |
0f113f3e MC |
319 | if (group == NULL) |
320 | return 0; | |
2986ecdc DSH |
321 | if (to->pkey.ec == NULL) { |
322 | to->pkey.ec = EC_KEY_new(); | |
323 | if (to->pkey.ec == NULL) | |
188a9bd9 | 324 | goto err; |
2986ecdc | 325 | } |
0f113f3e | 326 | if (EC_KEY_set_group(to->pkey.ec, group) == 0) |
188a9bd9 | 327 | goto err; |
0f113f3e MC |
328 | EC_GROUP_free(group); |
329 | return 1; | |
188a9bd9 BE |
330 | err: |
331 | EC_GROUP_free(group); | |
332 | return 0; | |
0f113f3e | 333 | } |
6f81892e | 334 | |
930b0c4b | 335 | static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) |
0f113f3e MC |
336 | { |
337 | const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec), | |
338 | *group_b = EC_KEY_get0_group(b->pkey.ec); | |
83156454 | 339 | |
978ecbb0 DW |
340 | if (group_a == NULL || group_b == NULL) |
341 | return -2; | |
0f113f3e MC |
342 | if (EC_GROUP_cmp(group_a, group_b, NULL)) |
343 | return 0; | |
344 | else | |
345 | return 1; | |
346 | } | |
6f81892e DSH |
347 | |
348 | static void int_ec_free(EVP_PKEY *pkey) | |
0f113f3e MC |
349 | { |
350 | EC_KEY_free(pkey->pkey.ec); | |
351 | } | |
6f81892e | 352 | |
d6755bb6 DSH |
353 | typedef enum { |
354 | EC_KEY_PRINT_PRIVATE, | |
355 | EC_KEY_PRINT_PUBLIC, | |
356 | EC_KEY_PRINT_PARAM | |
357 | } ec_print_t; | |
358 | ||
359 | static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, ec_print_t ktype) | |
0f113f3e | 360 | { |
0f113f3e | 361 | const char *ecstr; |
7fc7d1a7 DSH |
362 | unsigned char *priv = NULL, *pub = NULL; |
363 | size_t privlen = 0, publen = 0; | |
364 | int ret = 0; | |
0f113f3e | 365 | const EC_GROUP *group; |
0f113f3e MC |
366 | |
367 | if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) { | |
9311d0c4 | 368 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
7fc7d1a7 | 369 | return 0; |
0f113f3e MC |
370 | } |
371 | ||
82f52631 | 372 | if (ktype != EC_KEY_PRINT_PARAM && EC_KEY_get0_public_key(x) != NULL) { |
7fc7d1a7 DSH |
373 | publen = EC_KEY_key2buf(x, EC_KEY_get_conv_form(x), &pub, NULL); |
374 | if (publen == 0) | |
375 | goto err; | |
0f113f3e MC |
376 | } |
377 | ||
d6755bb6 | 378 | if (ktype == EC_KEY_PRINT_PRIVATE && EC_KEY_get0_private_key(x) != NULL) { |
7fc7d1a7 DSH |
379 | privlen = EC_KEY_priv2buf(x, &priv); |
380 | if (privlen == 0) | |
d810700b | 381 | goto err; |
d810700b | 382 | } |
0f113f3e | 383 | |
d6755bb6 | 384 | if (ktype == EC_KEY_PRINT_PRIVATE) |
0f113f3e | 385 | ecstr = "Private-Key"; |
d6755bb6 | 386 | else if (ktype == EC_KEY_PRINT_PUBLIC) |
0f113f3e MC |
387 | ecstr = "Public-Key"; |
388 | else | |
389 | ecstr = "ECDSA-Parameters"; | |
390 | ||
391 | if (!BIO_indent(bp, off, 128)) | |
392 | goto err; | |
be2e334f DSH |
393 | if (BIO_printf(bp, "%s: (%d bit)\n", ecstr, |
394 | EC_GROUP_order_bits(group)) <= 0) | |
0f113f3e MC |
395 | goto err; |
396 | ||
7fc7d1a7 | 397 | if (privlen != 0) { |
d810700b DSH |
398 | if (BIO_printf(bp, "%*spriv:\n", off, "") <= 0) |
399 | goto err; | |
7fc7d1a7 | 400 | if (ASN1_buf_print(bp, priv, privlen, off + 4) == 0) |
d810700b DSH |
401 | goto err; |
402 | } | |
403 | ||
7fc7d1a7 | 404 | if (publen != 0) { |
d810700b DSH |
405 | if (BIO_printf(bp, "%*spub:\n", off, "") <= 0) |
406 | goto err; | |
7fc7d1a7 | 407 | if (ASN1_buf_print(bp, pub, publen, off + 4) == 0) |
d810700b DSH |
408 | goto err; |
409 | } | |
410 | ||
0f113f3e MC |
411 | if (!ECPKParameters_print(bp, group, off)) |
412 | goto err; | |
413 | ret = 1; | |
414 | err: | |
415 | if (!ret) | |
9311d0c4 | 416 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
7fc7d1a7 DSH |
417 | OPENSSL_clear_free(priv, privlen); |
418 | OPENSSL_free(pub); | |
d810700b | 419 | return ret; |
0f113f3e | 420 | } |
35208f36 | 421 | |
3e4585c8 | 422 | static int eckey_param_decode(EVP_PKEY *pkey, |
0f113f3e MC |
423 | const unsigned char **pder, int derlen) |
424 | { | |
425 | EC_KEY *eckey; | |
75ebbd9a | 426 | |
29844ea5 | 427 | if ((eckey = d2i_ECParameters(NULL, pder, derlen)) == NULL) |
0f113f3e | 428 | return 0; |
0f113f3e MC |
429 | EVP_PKEY_assign_EC_KEY(pkey, eckey); |
430 | return 1; | |
431 | } | |
3e4585c8 DSH |
432 | |
433 | static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder) | |
0f113f3e MC |
434 | { |
435 | return i2d_ECParameters(pkey->pkey.ec, pder); | |
436 | } | |
3e4585c8 | 437 | |
35208f36 | 438 | static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent, |
0f113f3e MC |
439 | ASN1_PCTX *ctx) |
440 | { | |
d6755bb6 | 441 | return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PARAM); |
0f113f3e | 442 | } |
35208f36 DSH |
443 | |
444 | static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent, | |
0f113f3e MC |
445 | ASN1_PCTX *ctx) |
446 | { | |
d6755bb6 | 447 | return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PUBLIC); |
0f113f3e | 448 | } |
35208f36 DSH |
449 | |
450 | static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent, | |
0f113f3e MC |
451 | ASN1_PCTX *ctx) |
452 | { | |
d6755bb6 | 453 | return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PRIVATE); |
0f113f3e | 454 | } |
35208f36 | 455 | |
e4263314 | 456 | static int old_ec_priv_decode(EVP_PKEY *pkey, |
0f113f3e MC |
457 | const unsigned char **pder, int derlen) |
458 | { | |
459 | EC_KEY *ec; | |
75ebbd9a | 460 | |
66066e1b | 461 | if ((ec = d2i_ECPrivateKey(NULL, pder, derlen)) == NULL) |
0f113f3e | 462 | return 0; |
0f113f3e MC |
463 | EVP_PKEY_assign_EC_KEY(pkey, ec); |
464 | return 1; | |
465 | } | |
e4263314 DSH |
466 | |
467 | static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder) | |
0f113f3e MC |
468 | { |
469 | return i2d_ECPrivateKey(pkey->pkey.ec, pder); | |
470 | } | |
e4263314 | 471 | |
492a9e24 | 472 | static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2) |
0f113f3e MC |
473 | { |
474 | switch (op) { | |
475 | case ASN1_PKEY_CTRL_PKCS7_SIGN: | |
476 | if (arg1 == 0) { | |
477 | int snid, hnid; | |
478 | X509_ALGOR *alg1, *alg2; | |
83156454 | 479 | |
0f113f3e MC |
480 | PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2); |
481 | if (alg1 == NULL || alg1->algorithm == NULL) | |
482 | return -1; | |
483 | hnid = OBJ_obj2nid(alg1->algorithm); | |
484 | if (hnid == NID_undef) | |
485 | return -1; | |
486 | if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey))) | |
487 | return -1; | |
488 | X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0); | |
489 | } | |
490 | return 1; | |
492a9e24 | 491 | |
0f113f3e | 492 | case ASN1_PKEY_CTRL_DEFAULT_MD_NID: |
e766f4a0 PY |
493 | if (EVP_PKEY_id(pkey) == EVP_PKEY_SM2) { |
494 | /* For SM2, the only valid digest-alg is SM3 */ | |
495 | *(int *)arg2 = NID_sm3; | |
ef077ba0 | 496 | return 2; /* Make it mandatory */ |
e766f4a0 | 497 | } |
ef077ba0 | 498 | *(int *)arg2 = NID_sha256; |
eb7eb137 | 499 | return 1; |
492a9e24 | 500 | |
3bca6c27 DSH |
501 | case ASN1_PKEY_CTRL_SET1_TLS_ENCPT: |
502 | return EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(pkey), arg2, arg1, NULL); | |
503 | ||
504 | case ASN1_PKEY_CTRL_GET1_TLS_ENCPT: | |
505 | return EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(pkey), | |
506 | POINT_CONVERSION_UNCOMPRESSED, arg2, NULL); | |
507 | ||
0f113f3e MC |
508 | default: |
509 | return -2; | |
6f81892e | 510 | |
0f113f3e | 511 | } |
6f81892e | 512 | |
0f113f3e | 513 | } |
6f81892e | 514 | |
2aee35d3 PY |
515 | static int ec_pkey_check(const EVP_PKEY *pkey) |
516 | { | |
517 | EC_KEY *eckey = pkey->pkey.ec; | |
518 | ||
519 | /* stay consistent to what EVP_PKEY_check demands */ | |
520 | if (eckey->priv_key == NULL) { | |
9311d0c4 | 521 | ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY); |
2aee35d3 PY |
522 | return 0; |
523 | } | |
524 | ||
525 | return EC_KEY_check_key(eckey); | |
526 | } | |
527 | ||
b0004708 PY |
528 | static int ec_pkey_public_check(const EVP_PKEY *pkey) |
529 | { | |
530 | EC_KEY *eckey = pkey->pkey.ec; | |
531 | ||
532 | /* | |
533 | * Note: it unnecessary to check eckey->pub_key here since | |
534 | * it will be checked in EC_KEY_check_key(). In fact, the | |
535 | * EC_KEY_check_key() mainly checks the public key, and checks | |
536 | * the private key optionally (only if there is one). So if | |
537 | * someone passes a whole EC key (public + private), this | |
538 | * will also work... | |
539 | */ | |
540 | ||
541 | return EC_KEY_check_key(eckey); | |
542 | } | |
543 | ||
544 | static int ec_pkey_param_check(const EVP_PKEY *pkey) | |
545 | { | |
546 | EC_KEY *eckey = pkey->pkey.ec; | |
547 | ||
548 | /* stay consistent to what EVP_PKEY_check demands */ | |
549 | if (eckey->group == NULL) { | |
9311d0c4 | 550 | ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS); |
b0004708 PY |
551 | return 0; |
552 | } | |
553 | ||
554 | return EC_GROUP_check(eckey->group, NULL); | |
555 | } | |
556 | ||
4fe54d67 NT |
557 | static |
558 | size_t ec_pkey_dirty_cnt(const EVP_PKEY *pkey) | |
559 | { | |
560 | return pkey->pkey.ec->dirty_cnt; | |
561 | } | |
562 | ||
4fe54d67 NT |
563 | static |
564 | int ec_pkey_export_to(const EVP_PKEY *from, void *to_keydata, | |
b4250010 | 565 | EVP_KEYMGMT *to_keymgmt, OSSL_LIB_CTX *libctx, |
76e23fc5 | 566 | const char *propq) |
4fe54d67 NT |
567 | { |
568 | const EC_KEY *eckey = NULL; | |
569 | const EC_GROUP *ecg = NULL; | |
c0f39ded | 570 | unsigned char *pub_key_buf = NULL, *gen_buf = NULL; |
4fe54d67 | 571 | size_t pub_key_buflen; |
6d4e6009 | 572 | OSSL_PARAM_BLD *tmpl; |
4fe54d67 NT |
573 | OSSL_PARAM *params = NULL; |
574 | const BIGNUM *priv_key = NULL; | |
575 | const EC_POINT *pub_point = NULL; | |
0996cff9 | 576 | int selection = 0; |
4fe54d67 | 577 | int rv = 0; |
76e23fc5 | 578 | BN_CTX *bnctx = NULL; |
4fe54d67 NT |
579 | |
580 | if (from == NULL | |
581 | || (eckey = from->pkey.ec) == NULL | |
582 | || (ecg = EC_KEY_get0_group(eckey)) == NULL) | |
583 | return 0; | |
584 | ||
df13defd RL |
585 | /* |
586 | * If the EC_KEY method is foreign, then we can't be sure of anything, | |
587 | * and can therefore not export or pretend to export. | |
588 | */ | |
589 | if (EC_KEY_get_method(eckey) != EC_KEY_OpenSSL()) | |
590 | return 0; | |
591 | ||
6d4e6009 P |
592 | tmpl = OSSL_PARAM_BLD_new(); |
593 | if (tmpl == NULL) | |
594 | return 0; | |
4fe54d67 | 595 | |
c0f39ded SL |
596 | /* |
597 | * EC_POINT_point2buf() can generate random numbers in some | |
598 | * implementations so we need to ensure we use the correct libctx. | |
599 | */ | |
600 | bnctx = BN_CTX_new_ex(libctx); | |
601 | if (bnctx == NULL) | |
602 | goto err; | |
603 | BN_CTX_start(bnctx); | |
604 | ||
4fe54d67 | 605 | /* export the domain parameters */ |
c0f39ded | 606 | if (!ec_group_todata(ecg, tmpl, NULL, libctx, propq, bnctx, &gen_buf)) |
0996cff9 RL |
607 | goto err; |
608 | selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS; | |
4fe54d67 NT |
609 | |
610 | priv_key = EC_KEY_get0_private_key(eckey); | |
611 | pub_point = EC_KEY_get0_public_key(eckey); | |
612 | ||
0996cff9 RL |
613 | if (pub_point != NULL) { |
614 | /* convert pub_point to a octet string according to the SECG standard */ | |
615 | if ((pub_key_buflen = EC_POINT_point2buf(ecg, pub_point, | |
616 | POINT_CONVERSION_COMPRESSED, | |
76e23fc5 | 617 | &pub_key_buf, bnctx)) == 0 |
6d4e6009 | 618 | || !OSSL_PARAM_BLD_push_octet_string(tmpl, |
0996cff9 RL |
619 | OSSL_PKEY_PARAM_PUB_KEY, |
620 | pub_key_buf, | |
621 | pub_key_buflen)) | |
622 | goto err; | |
623 | selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY; | |
624 | } | |
4fe54d67 NT |
625 | |
626 | if (priv_key != NULL) { | |
a377871d NT |
627 | size_t sz; |
628 | int ecbits; | |
629 | int ecdh_cofactor_mode; | |
630 | ||
631 | /* | |
632 | * Key import/export should never leak the bit length of the secret | |
633 | * scalar in the key. | |
634 | * | |
635 | * For this reason, on export we use padded BIGNUMs with fixed length. | |
636 | * | |
637 | * When importing we also should make sure that, even if short lived, | |
638 | * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as | |
639 | * soon as possible, so that any processing of this BIGNUM might opt for | |
640 | * constant time implementations in the backend. | |
641 | * | |
642 | * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have | |
643 | * to preallocate the BIGNUM internal buffer to a fixed public size big | |
644 | * enough that operations performed during the processing never trigger | |
645 | * a realloc which would leak the size of the scalar through memory | |
646 | * accesses. | |
647 | * | |
648 | * Fixed Length | |
649 | * ------------ | |
650 | * | |
651 | * The order of the large prime subgroup of the curve is our choice for | |
652 | * a fixed public size, as that is generally the upper bound for | |
653 | * generating a private key in EC cryptosystems and should fit all valid | |
654 | * secret scalars. | |
655 | * | |
656 | * For padding on export we just use the bit length of the order | |
657 | * converted to bytes (rounding up). | |
658 | * | |
659 | * For preallocating the BIGNUM storage we look at the number of "words" | |
660 | * required for the internal representation of the order, and we | |
661 | * preallocate 2 extra "words" in case any of the subsequent processing | |
662 | * might temporarily overflow the order length. | |
663 | */ | |
664 | ecbits = EC_GROUP_order_bits(ecg); | |
665 | if (ecbits <= 0) | |
666 | goto err; | |
667 | ||
668 | sz = (ecbits + 7 ) / 8; | |
6d4e6009 | 669 | if (!OSSL_PARAM_BLD_push_BN_pad(tmpl, |
a377871d NT |
670 | OSSL_PKEY_PARAM_PRIV_KEY, |
671 | priv_key, sz)) | |
672 | goto err; | |
0996cff9 | 673 | selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY; |
a377871d | 674 | |
4fe54d67 NT |
675 | /* |
676 | * The ECDH Cofactor Mode is defined only if the EC_KEY actually | |
677 | * contains a private key, so we check for the flag and export it only | |
678 | * in this case. | |
679 | */ | |
a377871d | 680 | ecdh_cofactor_mode = |
4fe54d67 NT |
681 | (EC_KEY_get_flags(eckey) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0; |
682 | ||
4fe54d67 | 683 | /* Export the ECDH_COFACTOR_MODE parameter */ |
6d4e6009 | 684 | if (!OSSL_PARAM_BLD_push_int(tmpl, |
4fe54d67 NT |
685 | OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, |
686 | ecdh_cofactor_mode)) | |
687 | goto err; | |
0996cff9 | 688 | selection |= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS; |
4fe54d67 NT |
689 | } |
690 | ||
6d4e6009 | 691 | params = OSSL_PARAM_BLD_to_param(tmpl); |
4fe54d67 NT |
692 | |
693 | /* We export, the provider imports */ | |
0996cff9 | 694 | rv = evp_keymgmt_import(to_keymgmt, to_keydata, selection, params); |
4fe54d67 NT |
695 | |
696 | err: | |
6d4e6009 P |
697 | OSSL_PARAM_BLD_free(tmpl); |
698 | OSSL_PARAM_BLD_free_params(params); | |
4fe54d67 | 699 | OPENSSL_free(pub_key_buf); |
c0f39ded SL |
700 | OPENSSL_free(gen_buf); |
701 | BN_CTX_end(bnctx); | |
76e23fc5 | 702 | BN_CTX_free(bnctx); |
4fe54d67 NT |
703 | return rv; |
704 | } | |
705 | ||
629c72db | 706 | static int ec_pkey_import_from(const OSSL_PARAM params[], void *vpctx) |
0abae163 | 707 | { |
629c72db MC |
708 | EVP_PKEY_CTX *pctx = vpctx; |
709 | EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx); | |
d8652be0 | 710 | EC_KEY *ec = EC_KEY_new_ex(pctx->libctx, pctx->propquery); |
0abae163 RL |
711 | |
712 | if (ec == NULL) { | |
713 | ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE); | |
714 | return 0; | |
715 | } | |
716 | ||
c0f39ded | 717 | if (!ec_group_fromdata(ec, params) |
0abae163 RL |
718 | || !ec_key_otherparams_fromdata(ec, params) |
719 | || !ec_key_fromdata(ec, params, 1) | |
720 | || !EVP_PKEY_assign_EC_KEY(pkey, ec)) { | |
721 | EC_KEY_free(ec); | |
722 | return 0; | |
723 | } | |
724 | return 1; | |
725 | } | |
726 | ||
0f113f3e MC |
727 | const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = { |
728 | EVP_PKEY_EC, | |
729 | EVP_PKEY_EC, | |
730 | 0, | |
731 | "EC", | |
732 | "OpenSSL EC algorithm", | |
733 | ||
734 | eckey_pub_decode, | |
735 | eckey_pub_encode, | |
736 | eckey_pub_cmp, | |
737 | eckey_pub_print, | |
738 | ||
febe6bb7 | 739 | NULL, |
0f113f3e MC |
740 | eckey_priv_encode, |
741 | eckey_priv_print, | |
742 | ||
743 | int_ec_size, | |
744 | ec_bits, | |
745 | ec_security_bits, | |
746 | ||
747 | eckey_param_decode, | |
748 | eckey_param_encode, | |
749 | ec_missing_parameters, | |
750 | ec_copy_parameters, | |
751 | ec_cmp_parameters, | |
752 | eckey_param_print, | |
753 | 0, | |
754 | ||
755 | int_ec_free, | |
756 | ec_pkey_ctrl, | |
757 | old_ec_priv_decode, | |
2aee35d3 PY |
758 | old_ec_priv_encode, |
759 | ||
760 | 0, 0, 0, | |
761 | ||
b0004708 PY |
762 | ec_pkey_check, |
763 | ec_pkey_public_check, | |
4fe54d67 NT |
764 | ec_pkey_param_check, |
765 | ||
766 | 0, /* set_priv_key */ | |
767 | 0, /* set_pub_key */ | |
768 | 0, /* get_priv_key */ | |
769 | 0, /* get_pub_key */ | |
770 | ||
771 | ec_pkey_dirty_cnt, | |
0abae163 | 772 | ec_pkey_export_to, |
febe6bb7 | 773 | ec_pkey_import_from, |
d8652be0 | 774 | eckey_priv_decode_ex |
0f113f3e | 775 | }; |
88e20b85 | 776 | |
ddb634fe JL |
777 | #if !defined(OPENSSL_NO_SM2) |
778 | const EVP_PKEY_ASN1_METHOD sm2_asn1_meth = { | |
779 | EVP_PKEY_SM2, | |
780 | EVP_PKEY_EC, | |
781 | ASN1_PKEY_ALIAS | |
782 | }; | |
783 | #endif | |
784 | ||
dca5eeb4 RS |
785 | int EC_KEY_print(BIO *bp, const EC_KEY *x, int off) |
786 | { | |
787 | int private = EC_KEY_get0_private_key(x) != NULL; | |
788 | ||
789 | return do_EC_KEY_print(bp, x, off, | |
a66069db | 790 | private ? EC_KEY_PRINT_PRIVATE : EC_KEY_PRINT_PUBLIC); |
dca5eeb4 RS |
791 | } |
792 | ||
793 | int ECParameters_print(BIO *bp, const EC_KEY *x) | |
794 | { | |
795 | return do_EC_KEY_print(bp, x, 4, EC_KEY_PRINT_PARAM); | |
796 | } |