xref: /freebsd-13-stable/crypto/openssl/crypto/ec/ec_ameth.c (revision cf3d446e18bf2ddb6739fa4495e9f5ad6538aa54)
1 /*
2  * Copyright 2006-2023 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/x509.h>
13 #include <openssl/ec.h>
14 #include <openssl/bn.h>
15 #include <openssl/cms.h>
16 #include <openssl/asn1t.h>
17 #include "crypto/asn1.h"
18 #include "crypto/evp.h"
19 #include "ec_local.h"
20 
21 #ifndef OPENSSL_NO_CMS
22 static int ecdh_cms_decrypt(CMS_RecipientInfo *ri);
23 static int ecdh_cms_encrypt(CMS_RecipientInfo *ri);
24 #endif
25 
eckey_param2type(int * pptype,void ** ppval,const EC_KEY * ec_key)26 static int eckey_param2type(int *pptype, void **ppval, const EC_KEY *ec_key)
27 {
28     const EC_GROUP *group;
29     int nid;
30     if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) {
31         ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_PARAMETERS);
32         return 0;
33     }
34     if (EC_GROUP_get_asn1_flag(group)
35         && (nid = EC_GROUP_get_curve_name(group)))
36         /* we have a 'named curve' => just set the OID */
37     {
38         ASN1_OBJECT *asn1obj = OBJ_nid2obj(nid);
39 
40         if (asn1obj == NULL || OBJ_length(asn1obj) == 0) {
41             ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_OID);
42             return 0;
43         }
44         *ppval = asn1obj;
45         *pptype = V_ASN1_OBJECT;
46     } else {                    /* explicit parameters */
47 
48         ASN1_STRING *pstr = NULL;
49         pstr = ASN1_STRING_new();
50         if (pstr == NULL)
51             return 0;
52 
53         /*
54          * The cast in the following line is intentional as the
55          * `i2d_ECParameters` signature can't be constified (see discussion at
56          * https://github.com/openssl/openssl/pull/9347 where related and
57          * required constification backports were rejected).
58          *
59          * This cast should be safe anyway, because we can expect
60          * `i2d_ECParameters()` to treat the first argument as if it was const.
61          */
62         pstr->length = i2d_ECParameters((EC_KEY *)ec_key, &pstr->data);
63         if (pstr->length <= 0) {
64             ASN1_STRING_free(pstr);
65             ECerr(EC_F_ECKEY_PARAM2TYPE, ERR_R_EC_LIB);
66             return 0;
67         }
68         *ppval = pstr;
69         *pptype = V_ASN1_SEQUENCE;
70     }
71     return 1;
72 }
73 
eckey_pub_encode(X509_PUBKEY * pk,const EVP_PKEY * pkey)74 static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
75 {
76     const EC_KEY *ec_key = pkey->pkey.ec;
77     void *pval = NULL;
78     int ptype;
79     unsigned char *penc = NULL, *p;
80     int penclen;
81 
82     if (!eckey_param2type(&ptype, &pval, ec_key)) {
83         ECerr(EC_F_ECKEY_PUB_ENCODE, ERR_R_EC_LIB);
84         return 0;
85     }
86     penclen = i2o_ECPublicKey(ec_key, NULL);
87     if (penclen <= 0)
88         goto err;
89     penc = OPENSSL_malloc(penclen);
90     if (penc == NULL)
91         goto err;
92     p = penc;
93     penclen = i2o_ECPublicKey(ec_key, &p);
94     if (penclen <= 0)
95         goto err;
96     if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC),
97                                ptype, pval, penc, penclen))
98         return 1;
99  err:
100     if (ptype == V_ASN1_SEQUENCE)
101         ASN1_STRING_free(pval);
102     OPENSSL_free(penc);
103     return 0;
104 }
105 
eckey_type2param(int ptype,const void * pval)106 static EC_KEY *eckey_type2param(int ptype, const void *pval)
107 {
108     EC_KEY *eckey = NULL;
109     EC_GROUP *group = NULL;
110 
111     if (ptype == V_ASN1_SEQUENCE) {
112         const ASN1_STRING *pstr = pval;
113         const unsigned char *pm = pstr->data;
114         int pmlen = pstr->length;
115 
116         if ((eckey = d2i_ECParameters(NULL, &pm, pmlen)) == NULL) {
117             ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
118             goto ecerr;
119         }
120     } else if (ptype == V_ASN1_OBJECT) {
121         const ASN1_OBJECT *poid = pval;
122 
123         /*
124          * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
125          */
126         if ((eckey = EC_KEY_new()) == NULL) {
127             ECerr(EC_F_ECKEY_TYPE2PARAM, ERR_R_MALLOC_FAILURE);
128             goto ecerr;
129         }
130         group = EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid));
131         if (group == NULL)
132             goto ecerr;
133         EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
134         if (EC_KEY_set_group(eckey, group) == 0)
135             goto ecerr;
136         EC_GROUP_free(group);
137     } else {
138         ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
139         goto ecerr;
140     }
141 
142     return eckey;
143 
144  ecerr:
145     EC_KEY_free(eckey);
146     EC_GROUP_free(group);
147     return NULL;
148 }
149 
eckey_pub_decode(EVP_PKEY * pkey,X509_PUBKEY * pubkey)150 static int eckey_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
151 {
152     const unsigned char *p = NULL;
153     const void *pval;
154     int ptype, pklen;
155     EC_KEY *eckey = NULL;
156     X509_ALGOR *palg;
157 
158     if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
159         return 0;
160     X509_ALGOR_get0(NULL, &ptype, &pval, palg);
161 
162     eckey = eckey_type2param(ptype, pval);
163 
164     if (!eckey) {
165         ECerr(EC_F_ECKEY_PUB_DECODE, ERR_R_EC_LIB);
166         return 0;
167     }
168 
169     /* We have parameters now set public key */
170     if (!o2i_ECPublicKey(&eckey, &p, pklen)) {
171         ECerr(EC_F_ECKEY_PUB_DECODE, EC_R_DECODE_ERROR);
172         goto ecerr;
173     }
174 
175     EVP_PKEY_assign_EC_KEY(pkey, eckey);
176     return 1;
177 
178  ecerr:
179     EC_KEY_free(eckey);
180     return 0;
181 }
182 
eckey_pub_cmp(const EVP_PKEY * a,const EVP_PKEY * b)183 static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
184 {
185     int r;
186     const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
187     const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
188         *pb = EC_KEY_get0_public_key(b->pkey.ec);
189     if (group == NULL || pa == NULL || pb == NULL)
190         return -2;
191     r = EC_POINT_cmp(group, pa, pb, NULL);
192     if (r == 0)
193         return 1;
194     if (r == 1)
195         return 0;
196     return -2;
197 }
198 
eckey_priv_decode(EVP_PKEY * pkey,const PKCS8_PRIV_KEY_INFO * p8)199 static int eckey_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
200 {
201     const unsigned char *p = NULL;
202     const void *pval;
203     int ptype, pklen;
204     EC_KEY *eckey = NULL;
205     const X509_ALGOR *palg;
206 
207     if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
208         return 0;
209     X509_ALGOR_get0(NULL, &ptype, &pval, palg);
210 
211     eckey = eckey_type2param(ptype, pval);
212 
213     if (!eckey)
214         goto ecliberr;
215 
216     /* We have parameters now set private key */
217     if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
218         ECerr(EC_F_ECKEY_PRIV_DECODE, EC_R_DECODE_ERROR);
219         goto ecerr;
220     }
221 
222     EVP_PKEY_assign_EC_KEY(pkey, eckey);
223     return 1;
224 
225  ecliberr:
226     ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
227  ecerr:
228     EC_KEY_free(eckey);
229     return 0;
230 }
231 
eckey_priv_encode(PKCS8_PRIV_KEY_INFO * p8,const EVP_PKEY * pkey)232 static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
233 {
234     EC_KEY ec_key = *(pkey->pkey.ec);
235     unsigned char *ep, *p;
236     int eplen, ptype;
237     void *pval;
238     unsigned int old_flags;
239 
240     if (!eckey_param2type(&ptype, &pval, &ec_key)) {
241         ECerr(EC_F_ECKEY_PRIV_ENCODE, EC_R_DECODE_ERROR);
242         return 0;
243     }
244 
245     /* set the private key */
246 
247     /*
248      * do not include the parameters in the SEC1 private key see PKCS#11
249      * 12.11
250      */
251     old_flags = EC_KEY_get_enc_flags(&ec_key);
252     EC_KEY_set_enc_flags(&ec_key, old_flags | EC_PKEY_NO_PARAMETERS);
253 
254     eplen = i2d_ECPrivateKey(&ec_key, NULL);
255     if (!eplen) {
256         if (ptype == V_ASN1_SEQUENCE)
257             ASN1_STRING_free(pval);
258         ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
259         return 0;
260     }
261     ep = OPENSSL_malloc(eplen);
262     if (ep == NULL) {
263         if (ptype == V_ASN1_SEQUENCE)
264             ASN1_STRING_free(pval);
265         ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
266         return 0;
267     }
268     p = ep;
269     if (!i2d_ECPrivateKey(&ec_key, &p)) {
270         OPENSSL_clear_free(ep, eplen);
271         if (ptype == V_ASN1_SEQUENCE)
272             ASN1_STRING_free(pval);
273         ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
274         return 0;
275     }
276 
277     if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0,
278                          ptype, pval, ep, eplen)) {
279         OPENSSL_clear_free(ep, eplen);
280         if (ptype == V_ASN1_SEQUENCE)
281             ASN1_STRING_free(pval);
282         return 0;
283     }
284 
285     return 1;
286 }
287 
int_ec_size(const EVP_PKEY * pkey)288 static int int_ec_size(const EVP_PKEY *pkey)
289 {
290     return ECDSA_size(pkey->pkey.ec);
291 }
292 
ec_bits(const EVP_PKEY * pkey)293 static int ec_bits(const EVP_PKEY *pkey)
294 {
295     return EC_GROUP_order_bits(EC_KEY_get0_group(pkey->pkey.ec));
296 }
297 
ec_security_bits(const EVP_PKEY * pkey)298 static int ec_security_bits(const EVP_PKEY *pkey)
299 {
300     int ecbits = ec_bits(pkey);
301     if (ecbits >= 512)
302         return 256;
303     if (ecbits >= 384)
304         return 192;
305     if (ecbits >= 256)
306         return 128;
307     if (ecbits >= 224)
308         return 112;
309     if (ecbits >= 160)
310         return 80;
311     return ecbits / 2;
312 }
313 
ec_missing_parameters(const EVP_PKEY * pkey)314 static int ec_missing_parameters(const EVP_PKEY *pkey)
315 {
316     if (pkey->pkey.ec == NULL || EC_KEY_get0_group(pkey->pkey.ec) == NULL)
317         return 1;
318     return 0;
319 }
320 
ec_copy_parameters(EVP_PKEY * to,const EVP_PKEY * from)321 static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
322 {
323     EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
324 
325     if (group == NULL)
326         return 0;
327     if (to->pkey.ec == NULL) {
328         to->pkey.ec = EC_KEY_new();
329         if (to->pkey.ec == NULL)
330             goto err;
331     }
332     if (EC_KEY_set_group(to->pkey.ec, group) == 0)
333         goto err;
334     EC_GROUP_free(group);
335     return 1;
336  err:
337     EC_GROUP_free(group);
338     return 0;
339 }
340 
ec_cmp_parameters(const EVP_PKEY * a,const EVP_PKEY * b)341 static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
342 {
343     const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
344         *group_b = EC_KEY_get0_group(b->pkey.ec);
345     if (group_a == NULL || group_b == NULL)
346         return -2;
347     if (EC_GROUP_cmp(group_a, group_b, NULL))
348         return 0;
349     else
350         return 1;
351 }
352 
int_ec_free(EVP_PKEY * pkey)353 static void int_ec_free(EVP_PKEY *pkey)
354 {
355     EC_KEY_free(pkey->pkey.ec);
356 }
357 
358 typedef enum {
359     EC_KEY_PRINT_PRIVATE,
360     EC_KEY_PRINT_PUBLIC,
361     EC_KEY_PRINT_PARAM
362 } ec_print_t;
363 
do_EC_KEY_print(BIO * bp,const EC_KEY * x,int off,ec_print_t ktype)364 static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, ec_print_t ktype)
365 {
366     const char *ecstr;
367     unsigned char *priv = NULL, *pub = NULL;
368     size_t privlen = 0, publen = 0;
369     int ret = 0;
370     const EC_GROUP *group;
371 
372     if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
373         ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_PASSED_NULL_PARAMETER);
374         return 0;
375     }
376 
377     if (ktype != EC_KEY_PRINT_PARAM && EC_KEY_get0_public_key(x) != NULL) {
378         publen = EC_KEY_key2buf(x, EC_KEY_get_conv_form(x), &pub, NULL);
379         if (publen == 0)
380             goto err;
381     }
382 
383     if (ktype == EC_KEY_PRINT_PRIVATE && EC_KEY_get0_private_key(x) != NULL) {
384         privlen = EC_KEY_priv2buf(x, &priv);
385         if (privlen == 0)
386             goto err;
387     }
388 
389     if (ktype == EC_KEY_PRINT_PRIVATE)
390         ecstr = "Private-Key";
391     else if (ktype == EC_KEY_PRINT_PUBLIC)
392         ecstr = "Public-Key";
393     else
394         ecstr = "ECDSA-Parameters";
395 
396     if (!BIO_indent(bp, off, 128))
397         goto err;
398     if (BIO_printf(bp, "%s: (%d bit)\n", ecstr,
399                    EC_GROUP_order_bits(group)) <= 0)
400         goto err;
401 
402     if (privlen != 0) {
403         if (BIO_printf(bp, "%*spriv:\n", off, "") <= 0)
404             goto err;
405         if (ASN1_buf_print(bp, priv, privlen, off + 4) == 0)
406             goto err;
407     }
408 
409     if (publen != 0) {
410         if (BIO_printf(bp, "%*spub:\n", off, "") <= 0)
411             goto err;
412         if (ASN1_buf_print(bp, pub, publen, off + 4) == 0)
413             goto err;
414     }
415 
416     if (!ECPKParameters_print(bp, group, off))
417         goto err;
418     ret = 1;
419  err:
420     if (!ret)
421         ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_EC_LIB);
422     OPENSSL_clear_free(priv, privlen);
423     OPENSSL_free(pub);
424     return ret;
425 }
426 
eckey_param_decode(EVP_PKEY * pkey,const unsigned char ** pder,int derlen)427 static int eckey_param_decode(EVP_PKEY *pkey,
428                               const unsigned char **pder, int derlen)
429 {
430     EC_KEY *eckey;
431 
432     if ((eckey = d2i_ECParameters(NULL, pder, derlen)) == NULL) {
433         ECerr(EC_F_ECKEY_PARAM_DECODE, ERR_R_EC_LIB);
434         return 0;
435     }
436     EVP_PKEY_assign_EC_KEY(pkey, eckey);
437     return 1;
438 }
439 
eckey_param_encode(const EVP_PKEY * pkey,unsigned char ** pder)440 static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
441 {
442     return i2d_ECParameters(pkey->pkey.ec, pder);
443 }
444 
eckey_param_print(BIO * bp,const EVP_PKEY * pkey,int indent,ASN1_PCTX * ctx)445 static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
446                              ASN1_PCTX *ctx)
447 {
448     return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PARAM);
449 }
450 
eckey_pub_print(BIO * bp,const EVP_PKEY * pkey,int indent,ASN1_PCTX * ctx)451 static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
452                            ASN1_PCTX *ctx)
453 {
454     return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PUBLIC);
455 }
456 
eckey_priv_print(BIO * bp,const EVP_PKEY * pkey,int indent,ASN1_PCTX * ctx)457 static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
458                             ASN1_PCTX *ctx)
459 {
460     return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PRIVATE);
461 }
462 
old_ec_priv_decode(EVP_PKEY * pkey,const unsigned char ** pder,int derlen)463 static int old_ec_priv_decode(EVP_PKEY *pkey,
464                               const unsigned char **pder, int derlen)
465 {
466     EC_KEY *ec;
467 
468     if ((ec = d2i_ECPrivateKey(NULL, pder, derlen)) == NULL) {
469         ECerr(EC_F_OLD_EC_PRIV_DECODE, EC_R_DECODE_ERROR);
470         return 0;
471     }
472     EVP_PKEY_assign_EC_KEY(pkey, ec);
473     return 1;
474 }
475 
old_ec_priv_encode(const EVP_PKEY * pkey,unsigned char ** pder)476 static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
477 {
478     return i2d_ECPrivateKey(pkey->pkey.ec, pder);
479 }
480 
ec_pkey_ctrl(EVP_PKEY * pkey,int op,long arg1,void * arg2)481 static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
482 {
483     switch (op) {
484     case ASN1_PKEY_CTRL_PKCS7_SIGN:
485         if (arg1 == 0) {
486             int snid, hnid;
487             X509_ALGOR *alg1, *alg2;
488             PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
489             if (alg1 == NULL || alg1->algorithm == NULL)
490                 return -1;
491             hnid = OBJ_obj2nid(alg1->algorithm);
492             if (hnid == NID_undef)
493                 return -1;
494             if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
495                 return -1;
496             X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
497         }
498         return 1;
499 #ifndef OPENSSL_NO_CMS
500     case ASN1_PKEY_CTRL_CMS_SIGN:
501         if (arg1 == 0) {
502             int snid, hnid;
503             X509_ALGOR *alg1, *alg2;
504             CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
505             if (alg1 == NULL || alg1->algorithm == NULL)
506                 return -1;
507             hnid = OBJ_obj2nid(alg1->algorithm);
508             if (hnid == NID_undef)
509                 return -1;
510             if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
511                 return -1;
512             X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
513         }
514         return 1;
515 
516     case ASN1_PKEY_CTRL_CMS_ENVELOPE:
517         if (arg1 == 1)
518             return ecdh_cms_decrypt(arg2);
519         else if (arg1 == 0)
520             return ecdh_cms_encrypt(arg2);
521         return -2;
522 
523     case ASN1_PKEY_CTRL_CMS_RI_TYPE:
524         *(int *)arg2 = CMS_RECIPINFO_AGREE;
525         return 1;
526 #endif
527 
528     case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
529         if (EVP_PKEY_id(pkey) == EVP_PKEY_SM2) {
530             /* For SM2, the only valid digest-alg is SM3 */
531             *(int *)arg2 = NID_sm3;
532         } else {
533             *(int *)arg2 = NID_sha256;
534         }
535         return 1;
536 
537     case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
538         return EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(pkey), arg2, arg1, NULL);
539 
540     case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
541         return EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(pkey),
542                               POINT_CONVERSION_UNCOMPRESSED, arg2, NULL);
543 
544     default:
545         return -2;
546 
547     }
548 
549 }
550 
ec_pkey_check(const EVP_PKEY * pkey)551 static int ec_pkey_check(const EVP_PKEY *pkey)
552 {
553     EC_KEY *eckey = pkey->pkey.ec;
554 
555     /* stay consistent to what EVP_PKEY_check demands */
556     if (eckey->priv_key == NULL) {
557         ECerr(EC_F_EC_PKEY_CHECK, EC_R_MISSING_PRIVATE_KEY);
558         return 0;
559     }
560 
561     return EC_KEY_check_key(eckey);
562 }
563 
ec_pkey_public_check(const EVP_PKEY * pkey)564 static int ec_pkey_public_check(const EVP_PKEY *pkey)
565 {
566     EC_KEY *eckey = pkey->pkey.ec;
567 
568     /*
569      * Note: it unnecessary to check eckey->pub_key here since
570      * it will be checked in EC_KEY_check_key(). In fact, the
571      * EC_KEY_check_key() mainly checks the public key, and checks
572      * the private key optionally (only if there is one). So if
573      * someone passes a whole EC key (public + private), this
574      * will also work...
575      */
576 
577     return EC_KEY_check_key(eckey);
578 }
579 
ec_pkey_param_check(const EVP_PKEY * pkey)580 static int ec_pkey_param_check(const EVP_PKEY *pkey)
581 {
582     EC_KEY *eckey = pkey->pkey.ec;
583 
584     /* stay consistent to what EVP_PKEY_check demands */
585     if (eckey->group == NULL) {
586         ECerr(EC_F_EC_PKEY_PARAM_CHECK, EC_R_MISSING_PARAMETERS);
587         return 0;
588     }
589 
590     return EC_GROUP_check(eckey->group, NULL);
591 }
592 
593 const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = {
594     EVP_PKEY_EC,
595     EVP_PKEY_EC,
596     0,
597     "EC",
598     "OpenSSL EC algorithm",
599 
600     eckey_pub_decode,
601     eckey_pub_encode,
602     eckey_pub_cmp,
603     eckey_pub_print,
604 
605     eckey_priv_decode,
606     eckey_priv_encode,
607     eckey_priv_print,
608 
609     int_ec_size,
610     ec_bits,
611     ec_security_bits,
612 
613     eckey_param_decode,
614     eckey_param_encode,
615     ec_missing_parameters,
616     ec_copy_parameters,
617     ec_cmp_parameters,
618     eckey_param_print,
619     0,
620 
621     int_ec_free,
622     ec_pkey_ctrl,
623     old_ec_priv_decode,
624     old_ec_priv_encode,
625 
626     0, 0, 0,
627 
628     ec_pkey_check,
629     ec_pkey_public_check,
630     ec_pkey_param_check
631 };
632 
633 #if !defined(OPENSSL_NO_SM2)
634 const EVP_PKEY_ASN1_METHOD sm2_asn1_meth = {
635    EVP_PKEY_SM2,
636    EVP_PKEY_EC,
637    ASN1_PKEY_ALIAS
638 };
639 #endif
640 
EC_KEY_print(BIO * bp,const EC_KEY * x,int off)641 int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
642 {
643     int private = EC_KEY_get0_private_key(x) != NULL;
644 
645     return do_EC_KEY_print(bp, x, off,
646                 private ? EC_KEY_PRINT_PRIVATE : EC_KEY_PRINT_PUBLIC);
647 }
648 
ECParameters_print(BIO * bp,const EC_KEY * x)649 int ECParameters_print(BIO *bp, const EC_KEY *x)
650 {
651     return do_EC_KEY_print(bp, x, 4, EC_KEY_PRINT_PARAM);
652 }
653 
654 #ifndef OPENSSL_NO_CMS
655 
ecdh_cms_set_peerkey(EVP_PKEY_CTX * pctx,X509_ALGOR * alg,ASN1_BIT_STRING * pubkey)656 static int ecdh_cms_set_peerkey(EVP_PKEY_CTX *pctx,
657                                 X509_ALGOR *alg, ASN1_BIT_STRING *pubkey)
658 {
659     const ASN1_OBJECT *aoid;
660     int atype;
661     const void *aval;
662     int rv = 0;
663     EVP_PKEY *pkpeer = NULL;
664     EC_KEY *ecpeer = NULL;
665     const unsigned char *p;
666     int plen;
667     X509_ALGOR_get0(&aoid, &atype, &aval, alg);
668     if (OBJ_obj2nid(aoid) != NID_X9_62_id_ecPublicKey)
669         goto err;
670     /* If absent parameters get group from main key */
671     if (atype == V_ASN1_UNDEF || atype == V_ASN1_NULL) {
672         const EC_GROUP *grp;
673         EVP_PKEY *pk;
674         pk = EVP_PKEY_CTX_get0_pkey(pctx);
675         if (!pk)
676             goto err;
677         grp = EC_KEY_get0_group(pk->pkey.ec);
678         ecpeer = EC_KEY_new();
679         if (ecpeer == NULL)
680             goto err;
681         if (!EC_KEY_set_group(ecpeer, grp))
682             goto err;
683     } else {
684         ecpeer = eckey_type2param(atype, aval);
685         if (!ecpeer)
686             goto err;
687     }
688     /* We have parameters now set public key */
689     plen = ASN1_STRING_length(pubkey);
690     p = ASN1_STRING_get0_data(pubkey);
691     if (!p || !plen)
692         goto err;
693     if (!o2i_ECPublicKey(&ecpeer, &p, plen))
694         goto err;
695     pkpeer = EVP_PKEY_new();
696     if (pkpeer == NULL)
697         goto err;
698     EVP_PKEY_set1_EC_KEY(pkpeer, ecpeer);
699     if (EVP_PKEY_derive_set_peer(pctx, pkpeer) > 0)
700         rv = 1;
701  err:
702     EC_KEY_free(ecpeer);
703     EVP_PKEY_free(pkpeer);
704     return rv;
705 }
706 
707 /* Set KDF parameters based on KDF NID */
ecdh_cms_set_kdf_param(EVP_PKEY_CTX * pctx,int eckdf_nid)708 static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX *pctx, int eckdf_nid)
709 {
710     int kdf_nid, kdfmd_nid, cofactor;
711     const EVP_MD *kdf_md;
712     if (eckdf_nid == NID_undef)
713         return 0;
714 
715     /* Lookup KDF type, cofactor mode and digest */
716     if (!OBJ_find_sigid_algs(eckdf_nid, &kdfmd_nid, &kdf_nid))
717         return 0;
718 
719     if (kdf_nid == NID_dh_std_kdf)
720         cofactor = 0;
721     else if (kdf_nid == NID_dh_cofactor_kdf)
722         cofactor = 1;
723     else
724         return 0;
725 
726     if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx, cofactor) <= 0)
727         return 0;
728 
729     if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, EVP_PKEY_ECDH_KDF_X9_63) <= 0)
730         return 0;
731 
732     kdf_md = EVP_get_digestbynid(kdfmd_nid);
733     if (!kdf_md)
734         return 0;
735 
736     if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
737         return 0;
738     return 1;
739 }
740 
ecdh_cms_set_shared_info(EVP_PKEY_CTX * pctx,CMS_RecipientInfo * ri)741 static int ecdh_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
742 {
743     int rv = 0;
744 
745     X509_ALGOR *alg, *kekalg = NULL;
746     ASN1_OCTET_STRING *ukm;
747     const unsigned char *p;
748     unsigned char *der = NULL;
749     int plen, keylen;
750     const EVP_CIPHER *kekcipher;
751     EVP_CIPHER_CTX *kekctx;
752 
753     if (!CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm))
754         return 0;
755 
756     if (!ecdh_cms_set_kdf_param(pctx, OBJ_obj2nid(alg->algorithm))) {
757         ECerr(EC_F_ECDH_CMS_SET_SHARED_INFO, EC_R_KDF_PARAMETER_ERROR);
758         return 0;
759     }
760 
761     if (alg->parameter->type != V_ASN1_SEQUENCE)
762         return 0;
763 
764     p = alg->parameter->value.sequence->data;
765     plen = alg->parameter->value.sequence->length;
766     kekalg = d2i_X509_ALGOR(NULL, &p, plen);
767     if (!kekalg)
768         goto err;
769     kekctx = CMS_RecipientInfo_kari_get0_ctx(ri);
770     if (!kekctx)
771         goto err;
772     kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);
773     if (!kekcipher || EVP_CIPHER_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
774         goto err;
775     if (!EVP_EncryptInit_ex(kekctx, kekcipher, NULL, NULL, NULL))
776         goto err;
777     if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0)
778         goto err;
779 
780     keylen = EVP_CIPHER_CTX_key_length(kekctx);
781     if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
782         goto err;
783 
784     plen = CMS_SharedInfo_encode(&der, kekalg, ukm, keylen);
785 
786     if (!plen)
787         goto err;
788 
789     if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, der, plen) <= 0)
790         goto err;
791     der = NULL;
792 
793     rv = 1;
794  err:
795     X509_ALGOR_free(kekalg);
796     OPENSSL_free(der);
797     return rv;
798 }
799 
ecdh_cms_decrypt(CMS_RecipientInfo * ri)800 static int ecdh_cms_decrypt(CMS_RecipientInfo *ri)
801 {
802     EVP_PKEY_CTX *pctx;
803     pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
804     if (!pctx)
805         return 0;
806     /* See if we need to set peer key */
807     if (!EVP_PKEY_CTX_get0_peerkey(pctx)) {
808         X509_ALGOR *alg;
809         ASN1_BIT_STRING *pubkey;
810         if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &alg, &pubkey,
811                                                  NULL, NULL, NULL))
812             return 0;
813         if (!alg || !pubkey)
814             return 0;
815         if (!ecdh_cms_set_peerkey(pctx, alg, pubkey)) {
816             ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_PEER_KEY_ERROR);
817             return 0;
818         }
819     }
820     /* Set ECDH derivation parameters and initialise unwrap context */
821     if (!ecdh_cms_set_shared_info(pctx, ri)) {
822         ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_SHARED_INFO_ERROR);
823         return 0;
824     }
825     return 1;
826 }
827 
ecdh_cms_encrypt(CMS_RecipientInfo * ri)828 static int ecdh_cms_encrypt(CMS_RecipientInfo *ri)
829 {
830     EVP_PKEY_CTX *pctx;
831     EVP_PKEY *pkey;
832     EVP_CIPHER_CTX *ctx;
833     int keylen;
834     X509_ALGOR *talg, *wrap_alg = NULL;
835     const ASN1_OBJECT *aoid;
836     ASN1_BIT_STRING *pubkey;
837     ASN1_STRING *wrap_str;
838     ASN1_OCTET_STRING *ukm;
839     unsigned char *penc = NULL;
840     int penclen;
841     int rv = 0;
842     int ecdh_nid, kdf_type, kdf_nid, wrap_nid;
843     const EVP_MD *kdf_md;
844     pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
845     if (!pctx)
846         return 0;
847     /* Get ephemeral key */
848     pkey = EVP_PKEY_CTX_get0_pkey(pctx);
849     if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &talg, &pubkey,
850                                              NULL, NULL, NULL))
851         goto err;
852     X509_ALGOR_get0(&aoid, NULL, NULL, talg);
853     /* Is everything uninitialised? */
854     if (aoid == OBJ_nid2obj(NID_undef)) {
855 
856         EC_KEY *eckey = pkey->pkey.ec;
857         /* Set the key */
858         unsigned char *p;
859 
860         penclen = i2o_ECPublicKey(eckey, NULL);
861         if (penclen <= 0)
862             goto err;
863         penc = OPENSSL_malloc(penclen);
864         if (penc == NULL)
865             goto err;
866         p = penc;
867         penclen = i2o_ECPublicKey(eckey, &p);
868         if (penclen <= 0)
869             goto err;
870         ASN1_STRING_set0(pubkey, penc, penclen);
871         pubkey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
872         pubkey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
873 
874         penc = NULL;
875         X509_ALGOR_set0(talg, OBJ_nid2obj(NID_X9_62_id_ecPublicKey),
876                         V_ASN1_UNDEF, NULL);
877     }
878 
879     /* See if custom parameters set */
880     kdf_type = EVP_PKEY_CTX_get_ecdh_kdf_type(pctx);
881     if (kdf_type <= 0)
882         goto err;
883     if (!EVP_PKEY_CTX_get_ecdh_kdf_md(pctx, &kdf_md))
884         goto err;
885     ecdh_nid = EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx);
886     if (ecdh_nid < 0)
887         goto err;
888     else if (ecdh_nid == 0)
889         ecdh_nid = NID_dh_std_kdf;
890     else if (ecdh_nid == 1)
891         ecdh_nid = NID_dh_cofactor_kdf;
892 
893     if (kdf_type == EVP_PKEY_ECDH_KDF_NONE) {
894         kdf_type = EVP_PKEY_ECDH_KDF_X9_63;
895         if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, kdf_type) <= 0)
896             goto err;
897     } else
898         /* Unknown KDF */
899         goto err;
900     if (kdf_md == NULL) {
901         /* Fixme later for better MD */
902         kdf_md = EVP_sha1();
903         if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
904             goto err;
905     }
906 
907     if (!CMS_RecipientInfo_kari_get0_alg(ri, &talg, &ukm))
908         goto err;
909 
910     /* Lookup NID for KDF+cofactor+digest */
911 
912     if (!OBJ_find_sigid_by_algs(&kdf_nid, EVP_MD_type(kdf_md), ecdh_nid))
913         goto err;
914     /* Get wrap NID */
915     ctx = CMS_RecipientInfo_kari_get0_ctx(ri);
916     wrap_nid = EVP_CIPHER_CTX_type(ctx);
917     keylen = EVP_CIPHER_CTX_key_length(ctx);
918 
919     /* Package wrap algorithm in an AlgorithmIdentifier */
920 
921     wrap_alg = X509_ALGOR_new();
922     if (wrap_alg == NULL)
923         goto err;
924     wrap_alg->algorithm = OBJ_nid2obj(wrap_nid);
925     wrap_alg->parameter = ASN1_TYPE_new();
926     if (wrap_alg->parameter == NULL)
927         goto err;
928     if (EVP_CIPHER_param_to_asn1(ctx, wrap_alg->parameter) <= 0)
929         goto err;
930     if (ASN1_TYPE_get(wrap_alg->parameter) == NID_undef) {
931         ASN1_TYPE_free(wrap_alg->parameter);
932         wrap_alg->parameter = NULL;
933     }
934 
935     if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
936         goto err;
937 
938     penclen = CMS_SharedInfo_encode(&penc, wrap_alg, ukm, keylen);
939 
940     if (!penclen)
941         goto err;
942 
943     if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, penc, penclen) <= 0)
944         goto err;
945     penc = NULL;
946 
947     /*
948      * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
949      * of another AlgorithmIdentifier.
950      */
951     penclen = i2d_X509_ALGOR(wrap_alg, &penc);
952     if (!penc || !penclen)
953         goto err;
954     wrap_str = ASN1_STRING_new();
955     if (wrap_str == NULL)
956         goto err;
957     ASN1_STRING_set0(wrap_str, penc, penclen);
958     penc = NULL;
959     X509_ALGOR_set0(talg, OBJ_nid2obj(kdf_nid), V_ASN1_SEQUENCE, wrap_str);
960 
961     rv = 1;
962 
963  err:
964     OPENSSL_free(penc);
965     X509_ALGOR_free(wrap_alg);
966     return rv;
967 }
968 
969 #endif
970