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