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