]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ec/ec_ameth.c
X509: Add d2i_PUBKEY_ex(), which take a libctx and propq
[thirdparty/openssl.git] / crypto / ec / ec_ameth.c
1 /*
2 * Copyright 2006-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 /*
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
16 #include <stdio.h>
17 #include "internal/cryptlib.h"
18 #include <openssl/x509.h>
19 #include <openssl/ec.h>
20 #include <openssl/bn.h>
21 #include <openssl/cms.h>
22 #include <openssl/asn1t.h>
23 #include "crypto/asn1.h"
24 #include "crypto/evp.h"
25 #include "crypto/x509.h"
26 #include <openssl/core_names.h>
27 #include "openssl/param_build.h"
28 #include "ec_local.h"
29
30 #ifndef OPENSSL_NO_CMS
31 static int ecdh_cms_decrypt(CMS_RecipientInfo *ri);
32 static int ecdh_cms_encrypt(CMS_RecipientInfo *ri);
33 #endif
34
35 static int eckey_param2type(int *pptype, void **ppval, const EC_KEY *ec_key)
36 {
37 const EC_GROUP *group;
38 int nid;
39
40 if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) {
41 ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_PARAMETERS);
42 return 0;
43 }
44 if (EC_GROUP_get_asn1_flag(group)
45 && (nid = EC_GROUP_get_curve_name(group)))
46 /* we have a 'named curve' => just set the OID */
47 {
48 ASN1_OBJECT *asn1obj = OBJ_nid2obj(nid);
49
50 if (asn1obj == NULL || OBJ_length(asn1obj) == 0) {
51 ASN1_OBJECT_free(asn1obj);
52 ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_OID);
53 return 0;
54 }
55 *ppval = asn1obj;
56 *pptype = V_ASN1_OBJECT;
57 } else { /* explicit parameters */
58
59 ASN1_STRING *pstr = NULL;
60 pstr = ASN1_STRING_new();
61 if (pstr == NULL)
62 return 0;
63 pstr->length = i2d_ECParameters(ec_key, &pstr->data);
64 if (pstr->length <= 0) {
65 ASN1_STRING_free(pstr);
66 ECerr(EC_F_ECKEY_PARAM2TYPE, ERR_R_EC_LIB);
67 return 0;
68 }
69 *ppval = pstr;
70 *pptype = V_ASN1_SEQUENCE;
71 }
72 return 1;
73 }
74
75 static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
76 {
77 const EC_KEY *ec_key = pkey->pkey.ec;
78 void *pval = NULL;
79 int ptype;
80 unsigned char *penc = NULL, *p;
81 int penclen;
82
83 if (!eckey_param2type(&ptype, &pval, ec_key)) {
84 ECerr(EC_F_ECKEY_PUB_ENCODE, ERR_R_EC_LIB);
85 return 0;
86 }
87 penclen = i2o_ECPublicKey(ec_key, NULL);
88 if (penclen <= 0)
89 goto err;
90 penc = OPENSSL_malloc(penclen);
91 if (penc == NULL)
92 goto err;
93 p = penc;
94 penclen = i2o_ECPublicKey(ec_key, &p);
95 if (penclen <= 0)
96 goto err;
97 if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC),
98 ptype, pval, penc, penclen))
99 return 1;
100 err:
101 if (ptype == V_ASN1_OBJECT)
102 ASN1_OBJECT_free(pval);
103 else
104 ASN1_STRING_free(pval);
105 OPENSSL_free(penc);
106 return 0;
107 }
108
109 static EC_KEY *eckey_type2param(int ptype, const void *pval,
110 OPENSSL_CTX *libctx, const char *propq)
111 {
112 EC_KEY *eckey = NULL;
113 EC_GROUP *group = NULL;
114
115 if ((eckey = EC_KEY_new_with_libctx(libctx, propq)) == NULL) {
116 ECerr(EC_F_ECKEY_TYPE2PARAM, ERR_R_MALLOC_FAILURE);
117 goto ecerr;
118 }
119
120 if (ptype == V_ASN1_SEQUENCE) {
121 const ASN1_STRING *pstr = pval;
122 const unsigned char *pm = pstr->data;
123 int pmlen = pstr->length;
124
125
126 if (d2i_ECParameters(&eckey, &pm, pmlen) == NULL) {
127 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
128 goto ecerr;
129 }
130 } else if (ptype == V_ASN1_OBJECT) {
131 const ASN1_OBJECT *poid = pval;
132
133 /*
134 * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
135 */
136
137 group = EC_GROUP_new_by_curve_name_with_libctx(libctx, propq,
138 OBJ_obj2nid(poid));
139 if (group == NULL)
140 goto ecerr;
141 EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
142 if (EC_KEY_set_group(eckey, group) == 0)
143 goto ecerr;
144 EC_GROUP_free(group);
145 } else {
146 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
147 goto ecerr;
148 }
149
150 return eckey;
151
152 ecerr:
153 EC_KEY_free(eckey);
154 EC_GROUP_free(group);
155 return NULL;
156 }
157
158 static int eckey_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey)
159 {
160 const unsigned char *p = NULL;
161 const void *pval;
162 int ptype, pklen;
163 EC_KEY *eckey = NULL;
164 X509_ALGOR *palg;
165 OPENSSL_CTX *libctx = NULL;
166 const char *propq = NULL;
167
168 if (!X509_PUBKEY_get0_libctx(&libctx, &propq, pubkey)
169 || !X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
170 return 0;
171 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
172
173 eckey = eckey_type2param(ptype, pval, libctx, propq);
174
175 if (!eckey) {
176 ECerr(EC_F_ECKEY_PUB_DECODE, ERR_R_EC_LIB);
177 return 0;
178 }
179
180 /* We have parameters now set public key */
181 if (!o2i_ECPublicKey(&eckey, &p, pklen)) {
182 ECerr(EC_F_ECKEY_PUB_DECODE, EC_R_DECODE_ERROR);
183 goto ecerr;
184 }
185
186 EVP_PKEY_assign_EC_KEY(pkey, eckey);
187 return 1;
188
189 ecerr:
190 EC_KEY_free(eckey);
191 return 0;
192 }
193
194 static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
195 {
196 int r;
197 const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
198 const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
199 *pb = EC_KEY_get0_public_key(b->pkey.ec);
200
201 if (group == NULL || pa == NULL || pb == NULL)
202 return -2;
203 r = EC_POINT_cmp(group, pa, pb, NULL);
204 if (r == 0)
205 return 1;
206 if (r == 1)
207 return 0;
208 return -2;
209 }
210
211 static int eckey_priv_decode_with_libctx(EVP_PKEY *pkey,
212 const PKCS8_PRIV_KEY_INFO *p8,
213 OPENSSL_CTX *libctx,
214 const char *propq)
215 {
216 const unsigned char *p = NULL;
217 const void *pval;
218 int ptype, pklen;
219 EC_KEY *eckey = NULL;
220 const X509_ALGOR *palg;
221
222 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
223 return 0;
224 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
225
226 eckey = eckey_type2param(ptype, pval, libctx, propq);
227
228 if (eckey == NULL)
229 goto ecliberr;
230
231 /* We have parameters now set private key */
232 if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
233 ECerr(0, EC_R_DECODE_ERROR);
234 goto ecerr;
235 }
236
237 EVP_PKEY_assign_EC_KEY(pkey, eckey);
238 return 1;
239
240 ecliberr:
241 ECerr(0, ERR_R_EC_LIB);
242 ecerr:
243 EC_KEY_free(eckey);
244 return 0;
245 }
246
247 static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
248 {
249 EC_KEY ec_key = *(pkey->pkey.ec);
250 unsigned char *ep, *p;
251 int eplen, ptype;
252 void *pval;
253 unsigned int old_flags;
254
255 if (!eckey_param2type(&ptype, &pval, &ec_key)) {
256 ECerr(EC_F_ECKEY_PRIV_ENCODE, EC_R_DECODE_ERROR);
257 return 0;
258 }
259
260 /* set the private key */
261
262 /*
263 * do not include the parameters in the SEC1 private key see PKCS#11
264 * 12.11
265 */
266 old_flags = EC_KEY_get_enc_flags(&ec_key);
267 EC_KEY_set_enc_flags(&ec_key, old_flags | EC_PKEY_NO_PARAMETERS);
268
269 eplen = i2d_ECPrivateKey(&ec_key, NULL);
270 if (!eplen) {
271 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
272 return 0;
273 }
274 ep = OPENSSL_malloc(eplen);
275 if (ep == NULL) {
276 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
277 return 0;
278 }
279 p = ep;
280 if (!i2d_ECPrivateKey(&ec_key, &p)) {
281 OPENSSL_free(ep);
282 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
283 return 0;
284 }
285
286 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0,
287 ptype, pval, ep, eplen)) {
288 OPENSSL_free(ep);
289 return 0;
290 }
291
292 return 1;
293 }
294
295 static int int_ec_size(const EVP_PKEY *pkey)
296 {
297 return ECDSA_size(pkey->pkey.ec);
298 }
299
300 static int ec_bits(const EVP_PKEY *pkey)
301 {
302 return EC_GROUP_order_bits(EC_KEY_get0_group(pkey->pkey.ec));
303 }
304
305 static int ec_security_bits(const EVP_PKEY *pkey)
306 {
307 int ecbits = ec_bits(pkey);
308
309 if (ecbits >= 512)
310 return 256;
311 if (ecbits >= 384)
312 return 192;
313 if (ecbits >= 256)
314 return 128;
315 if (ecbits >= 224)
316 return 112;
317 if (ecbits >= 160)
318 return 80;
319 return ecbits / 2;
320 }
321
322 static int ec_missing_parameters(const EVP_PKEY *pkey)
323 {
324 if (pkey->pkey.ec == NULL || EC_KEY_get0_group(pkey->pkey.ec) == NULL)
325 return 1;
326 return 0;
327 }
328
329 static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
330 {
331 EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
332
333 if (group == NULL)
334 return 0;
335 if (to->pkey.ec == NULL) {
336 to->pkey.ec = EC_KEY_new();
337 if (to->pkey.ec == NULL)
338 goto err;
339 }
340 if (EC_KEY_set_group(to->pkey.ec, group) == 0)
341 goto err;
342 EC_GROUP_free(group);
343 return 1;
344 err:
345 EC_GROUP_free(group);
346 return 0;
347 }
348
349 static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
350 {
351 const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
352 *group_b = EC_KEY_get0_group(b->pkey.ec);
353
354 if (group_a == NULL || group_b == NULL)
355 return -2;
356 if (EC_GROUP_cmp(group_a, group_b, NULL))
357 return 0;
358 else
359 return 1;
360 }
361
362 static void int_ec_free(EVP_PKEY *pkey)
363 {
364 EC_KEY_free(pkey->pkey.ec);
365 }
366
367 typedef enum {
368 EC_KEY_PRINT_PRIVATE,
369 EC_KEY_PRINT_PUBLIC,
370 EC_KEY_PRINT_PARAM
371 } ec_print_t;
372
373 static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, ec_print_t ktype)
374 {
375 const char *ecstr;
376 unsigned char *priv = NULL, *pub = NULL;
377 size_t privlen = 0, publen = 0;
378 int ret = 0;
379 const EC_GROUP *group;
380
381 if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
382 ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_PASSED_NULL_PARAMETER);
383 return 0;
384 }
385
386 if (ktype != EC_KEY_PRINT_PARAM && EC_KEY_get0_public_key(x) != NULL) {
387 publen = EC_KEY_key2buf(x, EC_KEY_get_conv_form(x), &pub, NULL);
388 if (publen == 0)
389 goto err;
390 }
391
392 if (ktype == EC_KEY_PRINT_PRIVATE && EC_KEY_get0_private_key(x) != NULL) {
393 privlen = EC_KEY_priv2buf(x, &priv);
394 if (privlen == 0)
395 goto err;
396 }
397
398 if (ktype == EC_KEY_PRINT_PRIVATE)
399 ecstr = "Private-Key";
400 else if (ktype == EC_KEY_PRINT_PUBLIC)
401 ecstr = "Public-Key";
402 else
403 ecstr = "ECDSA-Parameters";
404
405 if (!BIO_indent(bp, off, 128))
406 goto err;
407 if (BIO_printf(bp, "%s: (%d bit)\n", ecstr,
408 EC_GROUP_order_bits(group)) <= 0)
409 goto err;
410
411 if (privlen != 0) {
412 if (BIO_printf(bp, "%*spriv:\n", off, "") <= 0)
413 goto err;
414 if (ASN1_buf_print(bp, priv, privlen, off + 4) == 0)
415 goto err;
416 }
417
418 if (publen != 0) {
419 if (BIO_printf(bp, "%*spub:\n", off, "") <= 0)
420 goto err;
421 if (ASN1_buf_print(bp, pub, publen, off + 4) == 0)
422 goto err;
423 }
424
425 if (!ECPKParameters_print(bp, group, off))
426 goto err;
427 ret = 1;
428 err:
429 if (!ret)
430 ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_EC_LIB);
431 OPENSSL_clear_free(priv, privlen);
432 OPENSSL_free(pub);
433 return ret;
434 }
435
436 static int eckey_param_decode(EVP_PKEY *pkey,
437 const unsigned char **pder, int derlen)
438 {
439 EC_KEY *eckey;
440
441 if ((eckey = d2i_ECParameters(NULL, pder, derlen)) == NULL) {
442 ECerr(EC_F_ECKEY_PARAM_DECODE, ERR_R_EC_LIB);
443 return 0;
444 }
445 EVP_PKEY_assign_EC_KEY(pkey, eckey);
446 return 1;
447 }
448
449 static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
450 {
451 return i2d_ECParameters(pkey->pkey.ec, pder);
452 }
453
454 static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
455 ASN1_PCTX *ctx)
456 {
457 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PARAM);
458 }
459
460 static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
461 ASN1_PCTX *ctx)
462 {
463 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PUBLIC);
464 }
465
466 static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
467 ASN1_PCTX *ctx)
468 {
469 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PRIVATE);
470 }
471
472 static int old_ec_priv_decode(EVP_PKEY *pkey,
473 const unsigned char **pder, int derlen)
474 {
475 EC_KEY *ec;
476
477 if ((ec = d2i_ECPrivateKey(NULL, pder, derlen)) == NULL) {
478 ECerr(EC_F_OLD_EC_PRIV_DECODE, EC_R_DECODE_ERROR);
479 return 0;
480 }
481 EVP_PKEY_assign_EC_KEY(pkey, ec);
482 return 1;
483 }
484
485 static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
486 {
487 return i2d_ECPrivateKey(pkey->pkey.ec, pder);
488 }
489
490 static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
491 {
492 switch (op) {
493 case ASN1_PKEY_CTRL_PKCS7_SIGN:
494 if (arg1 == 0) {
495 int snid, hnid;
496 X509_ALGOR *alg1, *alg2;
497
498 PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
499 if (alg1 == NULL || alg1->algorithm == NULL)
500 return -1;
501 hnid = OBJ_obj2nid(alg1->algorithm);
502 if (hnid == NID_undef)
503 return -1;
504 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
505 return -1;
506 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
507 }
508 return 1;
509 #ifndef OPENSSL_NO_CMS
510 case ASN1_PKEY_CTRL_CMS_SIGN:
511 if (arg1 == 0) {
512 int snid, hnid;
513 X509_ALGOR *alg1, *alg2;
514 CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
515 if (alg1 == NULL || alg1->algorithm == NULL)
516 return -1;
517 hnid = OBJ_obj2nid(alg1->algorithm);
518 if (hnid == NID_undef)
519 return -1;
520 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
521 return -1;
522 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
523 }
524 return 1;
525
526 case ASN1_PKEY_CTRL_CMS_ENVELOPE:
527 if (arg1 == 1)
528 return ecdh_cms_decrypt(arg2);
529 else if (arg1 == 0)
530 return ecdh_cms_encrypt(arg2);
531 return -2;
532
533 case ASN1_PKEY_CTRL_CMS_RI_TYPE:
534 *(int *)arg2 = CMS_RECIPINFO_AGREE;
535 return 1;
536 #endif
537
538 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
539 if (EVP_PKEY_id(pkey) == EVP_PKEY_SM2) {
540 /* For SM2, the only valid digest-alg is SM3 */
541 *(int *)arg2 = NID_sm3;
542 return 2; /* Make it mandatory */
543 }
544 *(int *)arg2 = NID_sha256;
545 return 1;
546
547 case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
548 return EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(pkey), arg2, arg1, NULL);
549
550 case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
551 return EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(pkey),
552 POINT_CONVERSION_UNCOMPRESSED, arg2, NULL);
553
554 default:
555 return -2;
556
557 }
558
559 }
560
561 static int ec_pkey_check(const EVP_PKEY *pkey)
562 {
563 EC_KEY *eckey = pkey->pkey.ec;
564
565 /* stay consistent to what EVP_PKEY_check demands */
566 if (eckey->priv_key == NULL) {
567 ECerr(EC_F_EC_PKEY_CHECK, EC_R_MISSING_PRIVATE_KEY);
568 return 0;
569 }
570
571 return EC_KEY_check_key(eckey);
572 }
573
574 static int ec_pkey_public_check(const EVP_PKEY *pkey)
575 {
576 EC_KEY *eckey = pkey->pkey.ec;
577
578 /*
579 * Note: it unnecessary to check eckey->pub_key here since
580 * it will be checked in EC_KEY_check_key(). In fact, the
581 * EC_KEY_check_key() mainly checks the public key, and checks
582 * the private key optionally (only if there is one). So if
583 * someone passes a whole EC key (public + private), this
584 * will also work...
585 */
586
587 return EC_KEY_check_key(eckey);
588 }
589
590 static int ec_pkey_param_check(const EVP_PKEY *pkey)
591 {
592 EC_KEY *eckey = pkey->pkey.ec;
593
594 /* stay consistent to what EVP_PKEY_check demands */
595 if (eckey->group == NULL) {
596 ECerr(EC_F_EC_PKEY_PARAM_CHECK, EC_R_MISSING_PARAMETERS);
597 return 0;
598 }
599
600 return EC_GROUP_check(eckey->group, NULL);
601 }
602
603 static
604 size_t ec_pkey_dirty_cnt(const EVP_PKEY *pkey)
605 {
606 return pkey->pkey.ec->dirty_cnt;
607 }
608
609 static ossl_inline
610 int ecparams_to_params(const EC_KEY *eckey, OSSL_PARAM_BLD *tmpl)
611 {
612 const EC_GROUP *ecg;
613 int curve_nid;
614
615 if (eckey == NULL)
616 return 0;
617
618 ecg = EC_KEY_get0_group(eckey);
619 if (ecg == NULL)
620 return 0;
621
622 curve_nid = EC_GROUP_get_curve_name(ecg);
623
624 if (curve_nid == NID_undef) {
625 /* explicit parameters */
626
627 /*
628 * TODO(3.0): should we support explicit parameters curves?
629 */
630 return 0;
631 } else {
632 /* named curve */
633 const char *curve_name = NULL;
634
635 if ((curve_name = OBJ_nid2sn(curve_nid)) == NULL)
636 return 0;
637
638 if (!OSSL_PARAM_BLD_push_utf8_string(tmpl, OSSL_PKEY_PARAM_GROUP_NAME,
639 curve_name, 0))
640 return 0;
641 }
642
643 return 1;
644 }
645
646 static
647 int ec_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
648 EVP_KEYMGMT *to_keymgmt, OPENSSL_CTX *libctx,
649 const char *propq)
650 {
651 const EC_KEY *eckey = NULL;
652 const EC_GROUP *ecg = NULL;
653 unsigned char *pub_key_buf = NULL;
654 size_t pub_key_buflen;
655 OSSL_PARAM_BLD *tmpl;
656 OSSL_PARAM *params = NULL;
657 const BIGNUM *priv_key = NULL;
658 const EC_POINT *pub_point = NULL;
659 int selection = 0;
660 int rv = 0;
661 BN_CTX *bnctx = NULL;
662
663 if (from == NULL
664 || (eckey = from->pkey.ec) == NULL
665 || (ecg = EC_KEY_get0_group(eckey)) == NULL)
666 return 0;
667
668 /*
669 * If the EC_KEY method is foreign, then we can't be sure of anything,
670 * and can therefore not export or pretend to export.
671 */
672 if (EC_KEY_get_method(eckey) != EC_KEY_OpenSSL())
673 return 0;
674
675 tmpl = OSSL_PARAM_BLD_new();
676 if (tmpl == NULL)
677 return 0;
678
679 /* export the domain parameters */
680 if (!ecparams_to_params(eckey, tmpl))
681 goto err;
682 selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
683
684 priv_key = EC_KEY_get0_private_key(eckey);
685 pub_point = EC_KEY_get0_public_key(eckey);
686
687 if (pub_point != NULL) {
688 /*
689 * EC_POINT_point2buf() can generate random numbers in some
690 * implementations so we need to ensure we use the correct libctx.
691 */
692 bnctx = BN_CTX_new_ex(libctx);
693 if (bnctx == NULL)
694 goto err;
695
696 /* convert pub_point to a octet string according to the SECG standard */
697 if ((pub_key_buflen = EC_POINT_point2buf(ecg, pub_point,
698 POINT_CONVERSION_COMPRESSED,
699 &pub_key_buf, bnctx)) == 0
700 || !OSSL_PARAM_BLD_push_octet_string(tmpl,
701 OSSL_PKEY_PARAM_PUB_KEY,
702 pub_key_buf,
703 pub_key_buflen))
704 goto err;
705 selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
706 }
707
708 if (priv_key != NULL) {
709 size_t sz;
710 int ecbits;
711 int ecdh_cofactor_mode;
712
713 /*
714 * Key import/export should never leak the bit length of the secret
715 * scalar in the key.
716 *
717 * For this reason, on export we use padded BIGNUMs with fixed length.
718 *
719 * When importing we also should make sure that, even if short lived,
720 * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
721 * soon as possible, so that any processing of this BIGNUM might opt for
722 * constant time implementations in the backend.
723 *
724 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
725 * to preallocate the BIGNUM internal buffer to a fixed public size big
726 * enough that operations performed during the processing never trigger
727 * a realloc which would leak the size of the scalar through memory
728 * accesses.
729 *
730 * Fixed Length
731 * ------------
732 *
733 * The order of the large prime subgroup of the curve is our choice for
734 * a fixed public size, as that is generally the upper bound for
735 * generating a private key in EC cryptosystems and should fit all valid
736 * secret scalars.
737 *
738 * For padding on export we just use the bit length of the order
739 * converted to bytes (rounding up).
740 *
741 * For preallocating the BIGNUM storage we look at the number of "words"
742 * required for the internal representation of the order, and we
743 * preallocate 2 extra "words" in case any of the subsequent processing
744 * might temporarily overflow the order length.
745 */
746 ecbits = EC_GROUP_order_bits(ecg);
747 if (ecbits <= 0)
748 goto err;
749
750 sz = (ecbits + 7 ) / 8;
751 if (!OSSL_PARAM_BLD_push_BN_pad(tmpl,
752 OSSL_PKEY_PARAM_PRIV_KEY,
753 priv_key, sz))
754 goto err;
755 selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
756
757 /*
758 * The ECDH Cofactor Mode is defined only if the EC_KEY actually
759 * contains a private key, so we check for the flag and export it only
760 * in this case.
761 */
762 ecdh_cofactor_mode =
763 (EC_KEY_get_flags(eckey) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
764
765 /* Export the ECDH_COFACTOR_MODE parameter */
766 if (!OSSL_PARAM_BLD_push_int(tmpl,
767 OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
768 ecdh_cofactor_mode))
769 goto err;
770 selection |= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS;
771 }
772
773 params = OSSL_PARAM_BLD_to_param(tmpl);
774
775 /* We export, the provider imports */
776 rv = evp_keymgmt_import(to_keymgmt, to_keydata, selection, params);
777
778 err:
779 OSSL_PARAM_BLD_free(tmpl);
780 OSSL_PARAM_BLD_free_params(params);
781 OPENSSL_free(pub_key_buf);
782 BN_CTX_free(bnctx);
783 return rv;
784 }
785
786 static int ec_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
787 {
788 EVP_PKEY_CTX *pctx = vpctx;
789 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
790 EC_KEY *ec = EC_KEY_new_with_libctx(pctx->libctx, pctx->propquery);
791
792 if (ec == NULL) {
793 ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
794 return 0;
795 }
796
797 if (!ec_key_domparams_fromdata(ec, params)
798 || !ec_key_otherparams_fromdata(ec, params)
799 || !ec_key_fromdata(ec, params, 1)
800 || !EVP_PKEY_assign_EC_KEY(pkey, ec)) {
801 EC_KEY_free(ec);
802 return 0;
803 }
804 return 1;
805 }
806
807 const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = {
808 EVP_PKEY_EC,
809 EVP_PKEY_EC,
810 0,
811 "EC",
812 "OpenSSL EC algorithm",
813
814 eckey_pub_decode,
815 eckey_pub_encode,
816 eckey_pub_cmp,
817 eckey_pub_print,
818
819 NULL,
820 eckey_priv_encode,
821 eckey_priv_print,
822
823 int_ec_size,
824 ec_bits,
825 ec_security_bits,
826
827 eckey_param_decode,
828 eckey_param_encode,
829 ec_missing_parameters,
830 ec_copy_parameters,
831 ec_cmp_parameters,
832 eckey_param_print,
833 0,
834
835 int_ec_free,
836 ec_pkey_ctrl,
837 old_ec_priv_decode,
838 old_ec_priv_encode,
839
840 0, 0, 0,
841
842 ec_pkey_check,
843 ec_pkey_public_check,
844 ec_pkey_param_check,
845
846 0, /* set_priv_key */
847 0, /* set_pub_key */
848 0, /* get_priv_key */
849 0, /* get_pub_key */
850
851 ec_pkey_dirty_cnt,
852 ec_pkey_export_to,
853 ec_pkey_import_from,
854 eckey_priv_decode_with_libctx
855 };
856
857 #if !defined(OPENSSL_NO_SM2)
858 const EVP_PKEY_ASN1_METHOD sm2_asn1_meth = {
859 EVP_PKEY_SM2,
860 EVP_PKEY_EC,
861 ASN1_PKEY_ALIAS
862 };
863 #endif
864
865 int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
866 {
867 int private = EC_KEY_get0_private_key(x) != NULL;
868
869 return do_EC_KEY_print(bp, x, off,
870 private ? EC_KEY_PRINT_PRIVATE : EC_KEY_PRINT_PUBLIC);
871 }
872
873 int ECParameters_print(BIO *bp, const EC_KEY *x)
874 {
875 return do_EC_KEY_print(bp, x, 4, EC_KEY_PRINT_PARAM);
876 }
877
878 #ifndef OPENSSL_NO_CMS
879
880 static int ecdh_cms_set_peerkey(EVP_PKEY_CTX *pctx,
881 X509_ALGOR *alg, ASN1_BIT_STRING *pubkey)
882 {
883 const ASN1_OBJECT *aoid;
884 int atype;
885 const void *aval;
886 int rv = 0;
887 EVP_PKEY *pkpeer = NULL;
888 EC_KEY *ecpeer = NULL;
889 const unsigned char *p;
890 int plen;
891
892 X509_ALGOR_get0(&aoid, &atype, &aval, alg);
893 if (OBJ_obj2nid(aoid) != NID_X9_62_id_ecPublicKey)
894 goto err;
895 /* If absent parameters get group from main key */
896 if (atype == V_ASN1_UNDEF || atype == V_ASN1_NULL) {
897 const EC_GROUP *grp;
898 EVP_PKEY *pk;
899 pk = EVP_PKEY_CTX_get0_pkey(pctx);
900 if (pk == NULL)
901 goto err;
902 grp = EC_KEY_get0_group(pk->pkey.ec);
903 ecpeer = EC_KEY_new();
904 if (ecpeer == NULL)
905 goto err;
906 if (!EC_KEY_set_group(ecpeer, grp))
907 goto err;
908 } else {
909 ecpeer = eckey_type2param(atype, aval, pctx->libctx, pctx->propquery);
910 if (!ecpeer)
911 goto err;
912 }
913 /* We have parameters now set public key */
914 plen = ASN1_STRING_length(pubkey);
915 p = ASN1_STRING_get0_data(pubkey);
916 if (p == NULL || plen == 0)
917 goto err;
918 if (!o2i_ECPublicKey(&ecpeer, &p, plen))
919 goto err;
920 pkpeer = EVP_PKEY_new();
921 if (pkpeer == NULL)
922 goto err;
923 EVP_PKEY_set1_EC_KEY(pkpeer, ecpeer);
924 if (EVP_PKEY_derive_set_peer(pctx, pkpeer) > 0)
925 rv = 1;
926 err:
927 EC_KEY_free(ecpeer);
928 EVP_PKEY_free(pkpeer);
929 return rv;
930 }
931
932 /* Set KDF parameters based on KDF NID */
933 static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX *pctx, int eckdf_nid)
934 {
935 int kdf_nid, kdfmd_nid, cofactor;
936 const EVP_MD *kdf_md;
937 if (eckdf_nid == NID_undef)
938 return 0;
939
940 /* Lookup KDF type, cofactor mode and digest */
941 if (!OBJ_find_sigid_algs(eckdf_nid, &kdfmd_nid, &kdf_nid))
942 return 0;
943
944 if (kdf_nid == NID_dh_std_kdf)
945 cofactor = 0;
946 else if (kdf_nid == NID_dh_cofactor_kdf)
947 cofactor = 1;
948 else
949 return 0;
950
951 if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx, cofactor) <= 0)
952 return 0;
953
954 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, EVP_PKEY_ECDH_KDF_X9_63) <= 0)
955 return 0;
956
957 kdf_md = EVP_get_digestbynid(kdfmd_nid);
958 if (!kdf_md)
959 return 0;
960
961 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
962 return 0;
963 return 1;
964 }
965
966 static int ecdh_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
967 {
968 int rv = 0;
969
970 X509_ALGOR *alg, *kekalg = NULL;
971 ASN1_OCTET_STRING *ukm;
972 const unsigned char *p;
973 unsigned char *der = NULL;
974 int plen, keylen;
975 EVP_CIPHER *kekcipher = NULL;
976 EVP_CIPHER_CTX *kekctx;
977 const char *name;
978
979 if (!CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm))
980 return 0;
981
982 if (!ecdh_cms_set_kdf_param(pctx, OBJ_obj2nid(alg->algorithm))) {
983 ECerr(EC_F_ECDH_CMS_SET_SHARED_INFO, EC_R_KDF_PARAMETER_ERROR);
984 return 0;
985 }
986
987 if (alg->parameter->type != V_ASN1_SEQUENCE)
988 return 0;
989
990 p = alg->parameter->value.sequence->data;
991 plen = alg->parameter->value.sequence->length;
992 kekalg = d2i_X509_ALGOR(NULL, &p, plen);
993 if (kekalg == NULL)
994 goto err;
995 kekctx = CMS_RecipientInfo_kari_get0_ctx(ri);
996 if (kekctx == NULL)
997 goto err;
998 name = OBJ_nid2sn(OBJ_obj2nid(kekalg->algorithm));
999 kekcipher = EVP_CIPHER_fetch(pctx->libctx, name, NULL);
1000 if (kekcipher == NULL || EVP_CIPHER_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
1001 goto err;
1002 if (!EVP_EncryptInit_ex(kekctx, kekcipher, NULL, NULL, NULL))
1003 goto err;
1004 if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0)
1005 goto err;
1006
1007 keylen = EVP_CIPHER_CTX_key_length(kekctx);
1008 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
1009 goto err;
1010
1011 plen = CMS_SharedInfo_encode(&der, kekalg, ukm, keylen);
1012
1013 if (!plen)
1014 goto err;
1015
1016 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, der, plen) <= 0)
1017 goto err;
1018 der = NULL;
1019
1020 rv = 1;
1021 err:
1022 EVP_CIPHER_free(kekcipher);
1023 X509_ALGOR_free(kekalg);
1024 OPENSSL_free(der);
1025 return rv;
1026 }
1027
1028 static int ecdh_cms_decrypt(CMS_RecipientInfo *ri)
1029 {
1030 EVP_PKEY_CTX *pctx;
1031
1032 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
1033 if (pctx == NULL)
1034 return 0;
1035 /* See if we need to set peer key */
1036 if (!EVP_PKEY_CTX_get0_peerkey(pctx)) {
1037 X509_ALGOR *alg;
1038 ASN1_BIT_STRING *pubkey;
1039
1040 if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &alg, &pubkey,
1041 NULL, NULL, NULL))
1042 return 0;
1043 if (!alg || !pubkey)
1044 return 0;
1045 if (!ecdh_cms_set_peerkey(pctx, alg, pubkey)) {
1046 ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_PEER_KEY_ERROR);
1047 return 0;
1048 }
1049 }
1050 /* Set ECDH derivation parameters and initialise unwrap context */
1051 if (!ecdh_cms_set_shared_info(pctx, ri)) {
1052 ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_SHARED_INFO_ERROR);
1053 return 0;
1054 }
1055 return 1;
1056 }
1057
1058 static int ecdh_cms_encrypt(CMS_RecipientInfo *ri)
1059 {
1060 EVP_PKEY_CTX *pctx;
1061 EVP_PKEY *pkey;
1062 EVP_CIPHER_CTX *ctx;
1063 int keylen;
1064 X509_ALGOR *talg, *wrap_alg = NULL;
1065 const ASN1_OBJECT *aoid;
1066 ASN1_BIT_STRING *pubkey;
1067 ASN1_STRING *wrap_str;
1068 ASN1_OCTET_STRING *ukm;
1069 unsigned char *penc = NULL;
1070 int penclen;
1071 int rv = 0;
1072 int ecdh_nid, kdf_type, kdf_nid, wrap_nid;
1073 const EVP_MD *kdf_md;
1074
1075 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
1076 if (pctx == NULL)
1077 return 0;
1078 /* Get ephemeral key */
1079 pkey = EVP_PKEY_CTX_get0_pkey(pctx);
1080 if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &talg, &pubkey,
1081 NULL, NULL, NULL))
1082 goto err;
1083 X509_ALGOR_get0(&aoid, NULL, NULL, talg);
1084 /* Is everything uninitialised? */
1085 if (aoid == OBJ_nid2obj(NID_undef)) {
1086
1087 EC_KEY *eckey = pkey->pkey.ec;
1088 /* Set the key */
1089 unsigned char *p;
1090
1091 penclen = i2o_ECPublicKey(eckey, NULL);
1092 if (penclen <= 0)
1093 goto err;
1094 penc = OPENSSL_malloc(penclen);
1095 if (penc == NULL)
1096 goto err;
1097 p = penc;
1098 penclen = i2o_ECPublicKey(eckey, &p);
1099 if (penclen <= 0)
1100 goto err;
1101 ASN1_STRING_set0(pubkey, penc, penclen);
1102 pubkey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
1103 pubkey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
1104
1105 penc = NULL;
1106 X509_ALGOR_set0(talg, OBJ_nid2obj(NID_X9_62_id_ecPublicKey),
1107 V_ASN1_UNDEF, NULL);
1108 }
1109
1110 /* See if custom parameters set */
1111 kdf_type = EVP_PKEY_CTX_get_ecdh_kdf_type(pctx);
1112 if (kdf_type <= 0)
1113 goto err;
1114 if (!EVP_PKEY_CTX_get_ecdh_kdf_md(pctx, &kdf_md))
1115 goto err;
1116 ecdh_nid = EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx);
1117 if (ecdh_nid < 0)
1118 goto err;
1119 else if (ecdh_nid == 0)
1120 ecdh_nid = NID_dh_std_kdf;
1121 else if (ecdh_nid == 1)
1122 ecdh_nid = NID_dh_cofactor_kdf;
1123
1124 if (kdf_type == EVP_PKEY_ECDH_KDF_NONE) {
1125 kdf_type = EVP_PKEY_ECDH_KDF_X9_63;
1126 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, kdf_type) <= 0)
1127 goto err;
1128 } else
1129 /* Unknown KDF */
1130 goto err;
1131 if (kdf_md == NULL) {
1132 /* Fixme later for better MD */
1133 kdf_md = EVP_sha1();
1134 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
1135 goto err;
1136 }
1137
1138 if (!CMS_RecipientInfo_kari_get0_alg(ri, &talg, &ukm))
1139 goto err;
1140
1141 /* Lookup NID for KDF+cofactor+digest */
1142
1143 if (!OBJ_find_sigid_by_algs(&kdf_nid, EVP_MD_type(kdf_md), ecdh_nid))
1144 goto err;
1145 /* Get wrap NID */
1146 ctx = CMS_RecipientInfo_kari_get0_ctx(ri);
1147 wrap_nid = EVP_CIPHER_CTX_type(ctx);
1148 keylen = EVP_CIPHER_CTX_key_length(ctx);
1149
1150 /* Package wrap algorithm in an AlgorithmIdentifier */
1151
1152 wrap_alg = X509_ALGOR_new();
1153 if (wrap_alg == NULL)
1154 goto err;
1155 wrap_alg->algorithm = OBJ_nid2obj(wrap_nid);
1156 wrap_alg->parameter = ASN1_TYPE_new();
1157 if (wrap_alg->parameter == NULL)
1158 goto err;
1159 if (EVP_CIPHER_param_to_asn1(ctx, wrap_alg->parameter) <= 0)
1160 goto err;
1161 if (ASN1_TYPE_get(wrap_alg->parameter) == NID_undef) {
1162 ASN1_TYPE_free(wrap_alg->parameter);
1163 wrap_alg->parameter = NULL;
1164 }
1165
1166 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
1167 goto err;
1168
1169 penclen = CMS_SharedInfo_encode(&penc, wrap_alg, ukm, keylen);
1170
1171 if (!penclen)
1172 goto err;
1173
1174 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, penc, penclen) <= 0)
1175 goto err;
1176 penc = NULL;
1177
1178 /*
1179 * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
1180 * of another AlgorithmIdentifier.
1181 */
1182 penclen = i2d_X509_ALGOR(wrap_alg, &penc);
1183 if (!penc || !penclen)
1184 goto err;
1185 wrap_str = ASN1_STRING_new();
1186 if (wrap_str == NULL)
1187 goto err;
1188 ASN1_STRING_set0(wrap_str, penc, penclen);
1189 penc = NULL;
1190 X509_ALGOR_set0(talg, OBJ_nid2obj(kdf_nid), V_ASN1_SEQUENCE, wrap_str);
1191
1192 rv = 1;
1193
1194 err:
1195 OPENSSL_free(penc);
1196 X509_ALGOR_free(wrap_alg);
1197 return rv;
1198 }
1199
1200 #endif