]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ec/ec_ameth.c
Fix missing propq in ecdh_cms_set_shared_info()
[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
610 int ec_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
611 EVP_KEYMGMT *to_keymgmt, OPENSSL_CTX *libctx,
612 const char *propq)
613 {
614 const EC_KEY *eckey = NULL;
615 const EC_GROUP *ecg = NULL;
616 unsigned char *pub_key_buf = NULL, *gen_buf = NULL;
617 size_t pub_key_buflen;
618 OSSL_PARAM_BLD *tmpl;
619 OSSL_PARAM *params = NULL;
620 const BIGNUM *priv_key = NULL;
621 const EC_POINT *pub_point = NULL;
622 int selection = 0;
623 int rv = 0;
624 BN_CTX *bnctx = NULL;
625
626 if (from == NULL
627 || (eckey = from->pkey.ec) == NULL
628 || (ecg = EC_KEY_get0_group(eckey)) == NULL)
629 return 0;
630
631 /*
632 * If the EC_KEY method is foreign, then we can't be sure of anything,
633 * and can therefore not export or pretend to export.
634 */
635 if (EC_KEY_get_method(eckey) != EC_KEY_OpenSSL())
636 return 0;
637
638 tmpl = OSSL_PARAM_BLD_new();
639 if (tmpl == NULL)
640 return 0;
641
642 /*
643 * EC_POINT_point2buf() can generate random numbers in some
644 * implementations so we need to ensure we use the correct libctx.
645 */
646 bnctx = BN_CTX_new_ex(libctx);
647 if (bnctx == NULL)
648 goto err;
649 BN_CTX_start(bnctx);
650
651 /* export the domain parameters */
652 if (!ec_group_todata(ecg, tmpl, NULL, libctx, propq, bnctx, &gen_buf))
653 goto err;
654 selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
655
656 priv_key = EC_KEY_get0_private_key(eckey);
657 pub_point = EC_KEY_get0_public_key(eckey);
658
659 if (pub_point != NULL) {
660 /* convert pub_point to a octet string according to the SECG standard */
661 if ((pub_key_buflen = EC_POINT_point2buf(ecg, pub_point,
662 POINT_CONVERSION_COMPRESSED,
663 &pub_key_buf, bnctx)) == 0
664 || !OSSL_PARAM_BLD_push_octet_string(tmpl,
665 OSSL_PKEY_PARAM_PUB_KEY,
666 pub_key_buf,
667 pub_key_buflen))
668 goto err;
669 selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
670 }
671
672 if (priv_key != NULL) {
673 size_t sz;
674 int ecbits;
675 int ecdh_cofactor_mode;
676
677 /*
678 * Key import/export should never leak the bit length of the secret
679 * scalar in the key.
680 *
681 * For this reason, on export we use padded BIGNUMs with fixed length.
682 *
683 * When importing we also should make sure that, even if short lived,
684 * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
685 * soon as possible, so that any processing of this BIGNUM might opt for
686 * constant time implementations in the backend.
687 *
688 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
689 * to preallocate the BIGNUM internal buffer to a fixed public size big
690 * enough that operations performed during the processing never trigger
691 * a realloc which would leak the size of the scalar through memory
692 * accesses.
693 *
694 * Fixed Length
695 * ------------
696 *
697 * The order of the large prime subgroup of the curve is our choice for
698 * a fixed public size, as that is generally the upper bound for
699 * generating a private key in EC cryptosystems and should fit all valid
700 * secret scalars.
701 *
702 * For padding on export we just use the bit length of the order
703 * converted to bytes (rounding up).
704 *
705 * For preallocating the BIGNUM storage we look at the number of "words"
706 * required for the internal representation of the order, and we
707 * preallocate 2 extra "words" in case any of the subsequent processing
708 * might temporarily overflow the order length.
709 */
710 ecbits = EC_GROUP_order_bits(ecg);
711 if (ecbits <= 0)
712 goto err;
713
714 sz = (ecbits + 7 ) / 8;
715 if (!OSSL_PARAM_BLD_push_BN_pad(tmpl,
716 OSSL_PKEY_PARAM_PRIV_KEY,
717 priv_key, sz))
718 goto err;
719 selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
720
721 /*
722 * The ECDH Cofactor Mode is defined only if the EC_KEY actually
723 * contains a private key, so we check for the flag and export it only
724 * in this case.
725 */
726 ecdh_cofactor_mode =
727 (EC_KEY_get_flags(eckey) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
728
729 /* Export the ECDH_COFACTOR_MODE parameter */
730 if (!OSSL_PARAM_BLD_push_int(tmpl,
731 OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
732 ecdh_cofactor_mode))
733 goto err;
734 selection |= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS;
735 }
736
737 params = OSSL_PARAM_BLD_to_param(tmpl);
738
739 /* We export, the provider imports */
740 rv = evp_keymgmt_import(to_keymgmt, to_keydata, selection, params);
741
742 err:
743 OSSL_PARAM_BLD_free(tmpl);
744 OSSL_PARAM_BLD_free_params(params);
745 OPENSSL_free(pub_key_buf);
746 OPENSSL_free(gen_buf);
747 BN_CTX_end(bnctx);
748 BN_CTX_free(bnctx);
749 return rv;
750 }
751
752 static int ec_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
753 {
754 EVP_PKEY_CTX *pctx = vpctx;
755 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
756 EC_KEY *ec = EC_KEY_new_with_libctx(pctx->libctx, pctx->propquery);
757
758 if (ec == NULL) {
759 ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
760 return 0;
761 }
762
763 if (!ec_group_fromdata(ec, params)
764 || !ec_key_otherparams_fromdata(ec, params)
765 || !ec_key_fromdata(ec, params, 1)
766 || !EVP_PKEY_assign_EC_KEY(pkey, ec)) {
767 EC_KEY_free(ec);
768 return 0;
769 }
770 return 1;
771 }
772
773 const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = {
774 EVP_PKEY_EC,
775 EVP_PKEY_EC,
776 0,
777 "EC",
778 "OpenSSL EC algorithm",
779
780 eckey_pub_decode,
781 eckey_pub_encode,
782 eckey_pub_cmp,
783 eckey_pub_print,
784
785 NULL,
786 eckey_priv_encode,
787 eckey_priv_print,
788
789 int_ec_size,
790 ec_bits,
791 ec_security_bits,
792
793 eckey_param_decode,
794 eckey_param_encode,
795 ec_missing_parameters,
796 ec_copy_parameters,
797 ec_cmp_parameters,
798 eckey_param_print,
799 0,
800
801 int_ec_free,
802 ec_pkey_ctrl,
803 old_ec_priv_decode,
804 old_ec_priv_encode,
805
806 0, 0, 0,
807
808 ec_pkey_check,
809 ec_pkey_public_check,
810 ec_pkey_param_check,
811
812 0, /* set_priv_key */
813 0, /* set_pub_key */
814 0, /* get_priv_key */
815 0, /* get_pub_key */
816
817 ec_pkey_dirty_cnt,
818 ec_pkey_export_to,
819 ec_pkey_import_from,
820 eckey_priv_decode_with_libctx
821 };
822
823 #if !defined(OPENSSL_NO_SM2)
824 const EVP_PKEY_ASN1_METHOD sm2_asn1_meth = {
825 EVP_PKEY_SM2,
826 EVP_PKEY_EC,
827 ASN1_PKEY_ALIAS
828 };
829 #endif
830
831 int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
832 {
833 int private = EC_KEY_get0_private_key(x) != NULL;
834
835 return do_EC_KEY_print(bp, x, off,
836 private ? EC_KEY_PRINT_PRIVATE : EC_KEY_PRINT_PUBLIC);
837 }
838
839 int ECParameters_print(BIO *bp, const EC_KEY *x)
840 {
841 return do_EC_KEY_print(bp, x, 4, EC_KEY_PRINT_PARAM);
842 }
843
844 #ifndef OPENSSL_NO_CMS
845
846 static int ecdh_cms_set_peerkey(EVP_PKEY_CTX *pctx,
847 X509_ALGOR *alg, ASN1_BIT_STRING *pubkey)
848 {
849 const ASN1_OBJECT *aoid;
850 int atype;
851 const void *aval;
852 int rv = 0;
853 EVP_PKEY *pkpeer = NULL;
854 EC_KEY *ecpeer = NULL;
855 const unsigned char *p;
856 int plen;
857
858 X509_ALGOR_get0(&aoid, &atype, &aval, alg);
859 if (OBJ_obj2nid(aoid) != NID_X9_62_id_ecPublicKey)
860 goto err;
861 /* If absent parameters get group from main key */
862 if (atype == V_ASN1_UNDEF || atype == V_ASN1_NULL) {
863 const EC_GROUP *grp;
864 EVP_PKEY *pk;
865 pk = EVP_PKEY_CTX_get0_pkey(pctx);
866 if (pk == NULL)
867 goto err;
868 grp = EC_KEY_get0_group(pk->pkey.ec);
869 ecpeer = EC_KEY_new();
870 if (ecpeer == NULL)
871 goto err;
872 if (!EC_KEY_set_group(ecpeer, grp))
873 goto err;
874 } else {
875 ecpeer = eckey_type2param(atype, aval, pctx->libctx, pctx->propquery);
876 if (!ecpeer)
877 goto err;
878 }
879 /* We have parameters now set public key */
880 plen = ASN1_STRING_length(pubkey);
881 p = ASN1_STRING_get0_data(pubkey);
882 if (p == NULL || plen == 0)
883 goto err;
884 if (!o2i_ECPublicKey(&ecpeer, &p, plen))
885 goto err;
886 pkpeer = EVP_PKEY_new();
887 if (pkpeer == NULL)
888 goto err;
889 EVP_PKEY_set1_EC_KEY(pkpeer, ecpeer);
890 if (EVP_PKEY_derive_set_peer(pctx, pkpeer) > 0)
891 rv = 1;
892 err:
893 EC_KEY_free(ecpeer);
894 EVP_PKEY_free(pkpeer);
895 return rv;
896 }
897
898 /* Set KDF parameters based on KDF NID */
899 static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX *pctx, int eckdf_nid)
900 {
901 int kdf_nid, kdfmd_nid, cofactor;
902 const EVP_MD *kdf_md;
903 if (eckdf_nid == NID_undef)
904 return 0;
905
906 /* Lookup KDF type, cofactor mode and digest */
907 if (!OBJ_find_sigid_algs(eckdf_nid, &kdfmd_nid, &kdf_nid))
908 return 0;
909
910 if (kdf_nid == NID_dh_std_kdf)
911 cofactor = 0;
912 else if (kdf_nid == NID_dh_cofactor_kdf)
913 cofactor = 1;
914 else
915 return 0;
916
917 if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx, cofactor) <= 0)
918 return 0;
919
920 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, EVP_PKEY_ECDH_KDF_X9_63) <= 0)
921 return 0;
922
923 kdf_md = EVP_get_digestbynid(kdfmd_nid);
924 if (!kdf_md)
925 return 0;
926
927 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
928 return 0;
929 return 1;
930 }
931
932 static int ecdh_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
933 {
934 int rv = 0;
935
936 X509_ALGOR *alg, *kekalg = NULL;
937 ASN1_OCTET_STRING *ukm;
938 const unsigned char *p;
939 unsigned char *der = NULL;
940 int plen, keylen;
941 EVP_CIPHER *kekcipher = NULL;
942 EVP_CIPHER_CTX *kekctx;
943 const char *name;
944
945 if (!CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm))
946 return 0;
947
948 if (!ecdh_cms_set_kdf_param(pctx, OBJ_obj2nid(alg->algorithm))) {
949 ECerr(EC_F_ECDH_CMS_SET_SHARED_INFO, EC_R_KDF_PARAMETER_ERROR);
950 return 0;
951 }
952
953 if (alg->parameter->type != V_ASN1_SEQUENCE)
954 return 0;
955
956 p = alg->parameter->value.sequence->data;
957 plen = alg->parameter->value.sequence->length;
958 kekalg = d2i_X509_ALGOR(NULL, &p, plen);
959 if (kekalg == NULL)
960 goto err;
961 kekctx = CMS_RecipientInfo_kari_get0_ctx(ri);
962 if (kekctx == NULL)
963 goto err;
964 name = OBJ_nid2sn(OBJ_obj2nid(kekalg->algorithm));
965 kekcipher = EVP_CIPHER_fetch(pctx->libctx, name, pctx->propquery);
966 if (kekcipher == NULL || EVP_CIPHER_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
967 goto err;
968 if (!EVP_EncryptInit_ex(kekctx, kekcipher, NULL, NULL, NULL))
969 goto err;
970 if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0)
971 goto err;
972
973 keylen = EVP_CIPHER_CTX_key_length(kekctx);
974 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
975 goto err;
976
977 plen = CMS_SharedInfo_encode(&der, kekalg, ukm, keylen);
978
979 if (plen <= 0)
980 goto err;
981
982 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, der, plen) <= 0)
983 goto err;
984 der = NULL;
985
986 rv = 1;
987 err:
988 EVP_CIPHER_free(kekcipher);
989 X509_ALGOR_free(kekalg);
990 OPENSSL_free(der);
991 return rv;
992 }
993
994 static int ecdh_cms_decrypt(CMS_RecipientInfo *ri)
995 {
996 EVP_PKEY_CTX *pctx;
997
998 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
999 if (pctx == NULL)
1000 return 0;
1001 /* See if we need to set peer key */
1002 if (!EVP_PKEY_CTX_get0_peerkey(pctx)) {
1003 X509_ALGOR *alg;
1004 ASN1_BIT_STRING *pubkey;
1005
1006 if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &alg, &pubkey,
1007 NULL, NULL, NULL))
1008 return 0;
1009 if (!alg || !pubkey)
1010 return 0;
1011 if (!ecdh_cms_set_peerkey(pctx, alg, pubkey)) {
1012 ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_PEER_KEY_ERROR);
1013 return 0;
1014 }
1015 }
1016 /* Set ECDH derivation parameters and initialise unwrap context */
1017 if (!ecdh_cms_set_shared_info(pctx, ri)) {
1018 ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_SHARED_INFO_ERROR);
1019 return 0;
1020 }
1021 return 1;
1022 }
1023
1024 static int ecdh_cms_encrypt(CMS_RecipientInfo *ri)
1025 {
1026 EVP_PKEY_CTX *pctx;
1027 EVP_PKEY *pkey;
1028 EVP_CIPHER_CTX *ctx;
1029 int keylen;
1030 X509_ALGOR *talg, *wrap_alg = NULL;
1031 const ASN1_OBJECT *aoid;
1032 ASN1_BIT_STRING *pubkey;
1033 ASN1_STRING *wrap_str;
1034 ASN1_OCTET_STRING *ukm;
1035 unsigned char *penc = NULL;
1036 int penclen;
1037 int rv = 0;
1038 int ecdh_nid, kdf_type, kdf_nid, wrap_nid;
1039 const EVP_MD *kdf_md;
1040
1041 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
1042 if (pctx == NULL)
1043 return 0;
1044 /* Get ephemeral key */
1045 pkey = EVP_PKEY_CTX_get0_pkey(pctx);
1046 if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &talg, &pubkey,
1047 NULL, NULL, NULL))
1048 goto err;
1049 X509_ALGOR_get0(&aoid, NULL, NULL, talg);
1050 /* Is everything uninitialised? */
1051 if (aoid == OBJ_nid2obj(NID_undef)) {
1052
1053 EC_KEY *eckey = pkey->pkey.ec;
1054 /* Set the key */
1055 unsigned char *p;
1056
1057 penclen = i2o_ECPublicKey(eckey, NULL);
1058 if (penclen <= 0)
1059 goto err;
1060 penc = OPENSSL_malloc(penclen);
1061 if (penc == NULL)
1062 goto err;
1063 p = penc;
1064 penclen = i2o_ECPublicKey(eckey, &p);
1065 if (penclen <= 0)
1066 goto err;
1067 ASN1_STRING_set0(pubkey, penc, penclen);
1068 pubkey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
1069 pubkey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
1070
1071 penc = NULL;
1072 X509_ALGOR_set0(talg, OBJ_nid2obj(NID_X9_62_id_ecPublicKey),
1073 V_ASN1_UNDEF, NULL);
1074 }
1075
1076 /* See if custom parameters set */
1077 kdf_type = EVP_PKEY_CTX_get_ecdh_kdf_type(pctx);
1078 if (kdf_type <= 0)
1079 goto err;
1080 if (!EVP_PKEY_CTX_get_ecdh_kdf_md(pctx, &kdf_md))
1081 goto err;
1082 ecdh_nid = EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx);
1083 if (ecdh_nid < 0)
1084 goto err;
1085 else if (ecdh_nid == 0)
1086 ecdh_nid = NID_dh_std_kdf;
1087 else if (ecdh_nid == 1)
1088 ecdh_nid = NID_dh_cofactor_kdf;
1089
1090 if (kdf_type == EVP_PKEY_ECDH_KDF_NONE) {
1091 kdf_type = EVP_PKEY_ECDH_KDF_X9_63;
1092 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, kdf_type) <= 0)
1093 goto err;
1094 } else
1095 /* Unknown KDF */
1096 goto err;
1097 if (kdf_md == NULL) {
1098 /* Fixme later for better MD */
1099 kdf_md = EVP_sha1();
1100 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
1101 goto err;
1102 }
1103
1104 if (!CMS_RecipientInfo_kari_get0_alg(ri, &talg, &ukm))
1105 goto err;
1106
1107 /* Lookup NID for KDF+cofactor+digest */
1108
1109 if (!OBJ_find_sigid_by_algs(&kdf_nid, EVP_MD_type(kdf_md), ecdh_nid))
1110 goto err;
1111 /* Get wrap NID */
1112 ctx = CMS_RecipientInfo_kari_get0_ctx(ri);
1113 wrap_nid = EVP_CIPHER_CTX_type(ctx);
1114 keylen = EVP_CIPHER_CTX_key_length(ctx);
1115
1116 /* Package wrap algorithm in an AlgorithmIdentifier */
1117
1118 wrap_alg = X509_ALGOR_new();
1119 if (wrap_alg == NULL)
1120 goto err;
1121 wrap_alg->algorithm = OBJ_nid2obj(wrap_nid);
1122 wrap_alg->parameter = ASN1_TYPE_new();
1123 if (wrap_alg->parameter == NULL)
1124 goto err;
1125 if (EVP_CIPHER_param_to_asn1(ctx, wrap_alg->parameter) <= 0)
1126 goto err;
1127 if (ASN1_TYPE_get(wrap_alg->parameter) == NID_undef) {
1128 ASN1_TYPE_free(wrap_alg->parameter);
1129 wrap_alg->parameter = NULL;
1130 }
1131
1132 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
1133 goto err;
1134
1135 penclen = CMS_SharedInfo_encode(&penc, wrap_alg, ukm, keylen);
1136
1137 if (!penclen)
1138 goto err;
1139
1140 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, penc, penclen) <= 0)
1141 goto err;
1142 penc = NULL;
1143
1144 /*
1145 * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
1146 * of another AlgorithmIdentifier.
1147 */
1148 penclen = i2d_X509_ALGOR(wrap_alg, &penc);
1149 if (!penc || !penclen)
1150 goto err;
1151 wrap_str = ASN1_STRING_new();
1152 if (wrap_str == NULL)
1153 goto err;
1154 ASN1_STRING_set0(wrap_str, penc, penclen);
1155 penc = NULL;
1156 X509_ALGOR_set0(talg, OBJ_nid2obj(kdf_nid), V_ASN1_SEQUENCE, wrap_str);
1157
1158 rv = 1;
1159
1160 err:
1161 OPENSSL_free(penc);
1162 X509_ALGOR_free(wrap_alg);
1163 return rv;
1164 }
1165
1166 #endif