]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ec_ameth.c
perlasm/ppc-xlate.pl: add PowerISA 3.0B instructions.
[thirdparty/openssl.git] / crypto / ec / ec_ameth.c
CommitLineData
0f113f3e 1/*
aa6bb135 2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
448be743 3 *
aa6bb135
RS
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
448be743
DSH
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
448be743
DSH
12#include <openssl/x509.h>
13#include <openssl/ec.h>
1e26a8ba 14#include <openssl/bn.h>
3c27208f 15#include <openssl/cms.h>
88e20b85 16#include <openssl/asn1t.h>
5fe736e5 17#include "internal/asn1_int.h"
3aeb9348 18#include "internal/evp_int.h"
b8a7bd83 19#include "ec_lcl.h"
448be743 20
e968561d 21#ifndef OPENSSL_NO_CMS
88e20b85
DSH
22static int ecdh_cms_decrypt(CMS_RecipientInfo *ri);
23static int ecdh_cms_encrypt(CMS_RecipientInfo *ri);
e968561d 24#endif
88e20b85 25
448be743 26static int eckey_param2type(int *pptype, void **ppval, EC_KEY *ec_key)
0f113f3e
MC
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();
90945fa3 44 if (pstr == NULL)
0f113f3e
MC
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}
448be743 57
6f81892e 58static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
0f113f3e
MC
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);
90945fa3 74 if (penc == NULL)
0f113f3e
MC
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);
b548a1f1 88 OPENSSL_free(penc);
0f113f3e
MC
89 return 0;
90}
448be743 91
ac4e2577 92static EC_KEY *eckey_type2param(int ptype, const void *pval)
0f113f3e
MC
93{
94 EC_KEY *eckey = NULL;
95 if (ptype == V_ASN1_SEQUENCE) {
ac4e2577 96 const ASN1_STRING *pstr = pval;
0f113f3e
MC
97 const unsigned char *pm = NULL;
98 int pmlen;
99 pm = pstr->data;
100 pmlen = pstr->length;
75ebbd9a 101 if ((eckey = d2i_ECParameters(NULL, &pm, pmlen)) == NULL) {
0f113f3e
MC
102 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
103 goto ecerr;
104 }
105 } else if (ptype == V_ASN1_OBJECT) {
ac4e2577 106 const ASN1_OBJECT *poid = pval;
0f113f3e
MC
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:
8fdc3734 131 EC_KEY_free(eckey);
0f113f3e
MC
132 return NULL;
133}
448be743
DSH
134
135static int eckey_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
0f113f3e
MC
136{
137 const unsigned char *p = NULL;
ac4e2577 138 const void *pval;
0f113f3e
MC
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:
8fdc3734 164 EC_KEY_free(eckey);
0f113f3e
MC
165 return 0;
166}
448be743 167
6f81892e 168static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
0f113f3e
MC
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);
978ecbb0
DW
174 if (group == NULL || pa == NULL || pb == NULL)
175 return -2;
0f113f3e
MC
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}
6f81892e 183
245c6bc3 184static int eckey_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
0f113f3e
MC
185{
186 const unsigned char *p = NULL;
ac4e2577 187 const void *pval;
0f113f3e
MC
188 int ptype, pklen;
189 EC_KEY *eckey = NULL;
245c6bc3 190 const X509_ALGOR *palg;
0f113f3e
MC
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
0f113f3e
MC
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:
8fdc3734 213 EC_KEY_free(eckey);
0f113f3e
MC
214 return 0;
215}
448be743 216
6f81892e 217static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
448be743 218{
b8a7bd83 219 EC_KEY ec_key = *(pkey->pkey.ec);
0f113f3e
MC
220 unsigned char *ep, *p;
221 int eplen, ptype;
222 void *pval;
b8a7bd83 223 unsigned int old_flags;
0f113f3e 224
b8a7bd83 225 if (!eckey_param2type(&ptype, &pval, &ec_key)) {
0f113f3e
MC
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 */
b8a7bd83
RL
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);
0f113f3e 240 if (!eplen) {
0f113f3e
MC
241 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
242 return 0;
243 }
b196e7d9 244 ep = OPENSSL_malloc(eplen);
90945fa3 245 if (ep == NULL) {
0f113f3e
MC
246 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
247 return 0;
248 }
249 p = ep;
b8a7bd83 250 if (!i2d_ECPrivateKey(&ec_key, &p)) {
0f113f3e
MC
251 OPENSSL_free(ep);
252 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
253 return 0;
254 }
0f113f3e
MC
255
256 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0,
e0670973
Y
257 ptype, pval, ep, eplen)) {
258 OPENSSL_free(ep);
0f113f3e 259 return 0;
e0670973 260 }
0f113f3e
MC
261
262 return 1;
448be743
DSH
263}
264
6f81892e 265static int int_ec_size(const EVP_PKEY *pkey)
0f113f3e
MC
266{
267 return ECDSA_size(pkey->pkey.ec);
268}
6f81892e
DSH
269
270static int ec_bits(const EVP_PKEY *pkey)
0f113f3e 271{
be2e334f 272 return EC_GROUP_order_bits(EC_KEY_get0_group(pkey->pkey.ec));
0f113f3e 273}
6f81892e 274
2514fa79 275static int ec_security_bits(const EVP_PKEY *pkey)
0f113f3e
MC
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}
2514fa79 290
6f81892e 291static int ec_missing_parameters(const EVP_PKEY *pkey)
0f113f3e 292{
f72f00d4 293 if (pkey->pkey.ec == NULL || EC_KEY_get0_group(pkey->pkey.ec) == NULL)
0f113f3e
MC
294 return 1;
295 return 0;
296}
6f81892e 297
930b0c4b 298static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
0f113f3e
MC
299{
300 EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
301 if (group == NULL)
302 return 0;
2986ecdc
DSH
303 if (to->pkey.ec == NULL) {
304 to->pkey.ec = EC_KEY_new();
305 if (to->pkey.ec == NULL)
306 return 0;
307 }
0f113f3e
MC
308 if (EC_KEY_set_group(to->pkey.ec, group) == 0)
309 return 0;
310 EC_GROUP_free(group);
311 return 1;
312}
6f81892e 313
930b0c4b 314static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
0f113f3e
MC
315{
316 const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
317 *group_b = EC_KEY_get0_group(b->pkey.ec);
978ecbb0
DW
318 if (group_a == NULL || group_b == NULL)
319 return -2;
0f113f3e
MC
320 if (EC_GROUP_cmp(group_a, group_b, NULL))
321 return 0;
322 else
323 return 1;
324}
6f81892e
DSH
325
326static void int_ec_free(EVP_PKEY *pkey)
0f113f3e
MC
327{
328 EC_KEY_free(pkey->pkey.ec);
329}
6f81892e 330
d6755bb6
DSH
331typedef enum {
332 EC_KEY_PRINT_PRIVATE,
333 EC_KEY_PRINT_PUBLIC,
334 EC_KEY_PRINT_PARAM
335} ec_print_t;
336
337static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, ec_print_t ktype)
0f113f3e 338{
0f113f3e 339 const char *ecstr;
7fc7d1a7
DSH
340 unsigned char *priv = NULL, *pub = NULL;
341 size_t privlen = 0, publen = 0;
342 int ret = 0;
0f113f3e 343 const EC_GROUP *group;
0f113f3e
MC
344
345 if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
7fc7d1a7
DSH
346 ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_PASSED_NULL_PARAMETER);
347 return 0;
0f113f3e
MC
348 }
349
82f52631 350 if (ktype != EC_KEY_PRINT_PARAM && EC_KEY_get0_public_key(x) != NULL) {
7fc7d1a7
DSH
351 publen = EC_KEY_key2buf(x, EC_KEY_get_conv_form(x), &pub, NULL);
352 if (publen == 0)
353 goto err;
0f113f3e
MC
354 }
355
d6755bb6 356 if (ktype == EC_KEY_PRINT_PRIVATE && EC_KEY_get0_private_key(x) != NULL) {
7fc7d1a7
DSH
357 privlen = EC_KEY_priv2buf(x, &priv);
358 if (privlen == 0)
d810700b 359 goto err;
d810700b 360 }
0f113f3e 361
d6755bb6 362 if (ktype == EC_KEY_PRINT_PRIVATE)
0f113f3e 363 ecstr = "Private-Key";
d6755bb6 364 else if (ktype == EC_KEY_PRINT_PUBLIC)
0f113f3e
MC
365 ecstr = "Public-Key";
366 else
367 ecstr = "ECDSA-Parameters";
368
369 if (!BIO_indent(bp, off, 128))
370 goto err;
be2e334f
DSH
371 if (BIO_printf(bp, "%s: (%d bit)\n", ecstr,
372 EC_GROUP_order_bits(group)) <= 0)
0f113f3e
MC
373 goto err;
374
7fc7d1a7 375 if (privlen != 0) {
d810700b
DSH
376 if (BIO_printf(bp, "%*spriv:\n", off, "") <= 0)
377 goto err;
7fc7d1a7 378 if (ASN1_buf_print(bp, priv, privlen, off + 4) == 0)
d810700b
DSH
379 goto err;
380 }
381
7fc7d1a7 382 if (publen != 0) {
d810700b
DSH
383 if (BIO_printf(bp, "%*spub:\n", off, "") <= 0)
384 goto err;
7fc7d1a7 385 if (ASN1_buf_print(bp, pub, publen, off + 4) == 0)
d810700b
DSH
386 goto err;
387 }
388
0f113f3e
MC
389 if (!ECPKParameters_print(bp, group, off))
390 goto err;
391 ret = 1;
392 err:
393 if (!ret)
7fc7d1a7
DSH
394 ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_EC_LIB);
395 OPENSSL_clear_free(priv, privlen);
396 OPENSSL_free(pub);
d810700b 397 return ret;
0f113f3e 398}
35208f36 399
3e4585c8 400static int eckey_param_decode(EVP_PKEY *pkey,
0f113f3e
MC
401 const unsigned char **pder, int derlen)
402{
403 EC_KEY *eckey;
75ebbd9a
RS
404
405 if ((eckey = d2i_ECParameters(NULL, pder, derlen)) == NULL) {
0f113f3e
MC
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}
3e4585c8
DSH
412
413static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
0f113f3e
MC
414{
415 return i2d_ECParameters(pkey->pkey.ec, pder);
416}
3e4585c8 417
35208f36 418static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
0f113f3e
MC
419 ASN1_PCTX *ctx)
420{
d6755bb6 421 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PARAM);
0f113f3e 422}
35208f36
DSH
423
424static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
0f113f3e
MC
425 ASN1_PCTX *ctx)
426{
d6755bb6 427 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PUBLIC);
0f113f3e 428}
35208f36
DSH
429
430static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
0f113f3e
MC
431 ASN1_PCTX *ctx)
432{
d6755bb6 433 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PRIVATE);
0f113f3e 434}
35208f36 435
e4263314 436static int old_ec_priv_decode(EVP_PKEY *pkey,
0f113f3e
MC
437 const unsigned char **pder, int derlen)
438{
439 EC_KEY *ec;
75ebbd9a
RS
440
441 if ((ec = d2i_ECPrivateKey(NULL, pder, derlen)) == NULL) {
0f113f3e
MC
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}
e4263314
DSH
448
449static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
0f113f3e
MC
450{
451 return i2d_ECPrivateKey(pkey->pkey.ec, pder);
452}
e4263314 453
492a9e24 454static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
0f113f3e
MC
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;
8931b30d 472#ifndef OPENSSL_NO_CMS
0f113f3e
MC
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;
8931b30d 499#endif
492a9e24 500
0f113f3e
MC
501 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
502 *(int *)arg2 = NID_sha256;
503 return 2;
492a9e24 504
3bca6c27
DSH
505 case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
506 return EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(pkey), arg2, arg1, NULL);
507
508 case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
509 return EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(pkey),
510 POINT_CONVERSION_UNCOMPRESSED, arg2, NULL);
511
0f113f3e
MC
512 default:
513 return -2;
6f81892e 514
0f113f3e 515 }
6f81892e 516
0f113f3e 517}
6f81892e 518
0f113f3e
MC
519const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = {
520 EVP_PKEY_EC,
521 EVP_PKEY_EC,
522 0,
523 "EC",
524 "OpenSSL EC algorithm",
525
526 eckey_pub_decode,
527 eckey_pub_encode,
528 eckey_pub_cmp,
529 eckey_pub_print,
530
531 eckey_priv_decode,
532 eckey_priv_encode,
533 eckey_priv_print,
534
535 int_ec_size,
536 ec_bits,
537 ec_security_bits,
538
539 eckey_param_decode,
540 eckey_param_encode,
541 ec_missing_parameters,
542 ec_copy_parameters,
543 ec_cmp_parameters,
544 eckey_param_print,
545 0,
546
547 int_ec_free,
548 ec_pkey_ctrl,
549 old_ec_priv_decode,
550 old_ec_priv_encode
551};
88e20b85 552
dca5eeb4
RS
553int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
554{
555 int private = EC_KEY_get0_private_key(x) != NULL;
556
557 return do_EC_KEY_print(bp, x, off,
a66069db 558 private ? EC_KEY_PRINT_PRIVATE : EC_KEY_PRINT_PUBLIC);
dca5eeb4
RS
559}
560
561int ECParameters_print(BIO *bp, const EC_KEY *x)
562{
563 return do_EC_KEY_print(bp, x, 4, EC_KEY_PRINT_PARAM);
564}
565
88e20b85
DSH
566#ifndef OPENSSL_NO_CMS
567
568static int ecdh_cms_set_peerkey(EVP_PKEY_CTX *pctx,
0f113f3e
MC
569 X509_ALGOR *alg, ASN1_BIT_STRING *pubkey)
570{
ac4e2577 571 const ASN1_OBJECT *aoid;
0f113f3e 572 int atype;
ac4e2577 573 const void *aval;
0f113f3e
MC
574 int rv = 0;
575 EVP_PKEY *pkpeer = NULL;
576 EC_KEY *ecpeer = NULL;
577 const unsigned char *p;
578 int plen;
579 X509_ALGOR_get0(&aoid, &atype, &aval, alg);
580 if (OBJ_obj2nid(aoid) != NID_X9_62_id_ecPublicKey)
581 goto err;
582 /* If absent parameters get group from main key */
583 if (atype == V_ASN1_UNDEF || atype == V_ASN1_NULL) {
584 const EC_GROUP *grp;
585 EVP_PKEY *pk;
586 pk = EVP_PKEY_CTX_get0_pkey(pctx);
587 if (!pk)
588 goto err;
589 grp = EC_KEY_get0_group(pk->pkey.ec);
590 ecpeer = EC_KEY_new();
90945fa3 591 if (ecpeer == NULL)
0f113f3e
MC
592 goto err;
593 if (!EC_KEY_set_group(ecpeer, grp))
594 goto err;
595 } else {
596 ecpeer = eckey_type2param(atype, aval);
597 if (!ecpeer)
598 goto err;
599 }
600 /* We have parameters now set public key */
601 plen = ASN1_STRING_length(pubkey);
17ebf85a 602 p = ASN1_STRING_get0_data(pubkey);
0f113f3e
MC
603 if (!p || !plen)
604 goto err;
605 if (!o2i_ECPublicKey(&ecpeer, &p, plen))
606 goto err;
607 pkpeer = EVP_PKEY_new();
90945fa3 608 if (pkpeer == NULL)
0f113f3e
MC
609 goto err;
610 EVP_PKEY_set1_EC_KEY(pkpeer, ecpeer);
611 if (EVP_PKEY_derive_set_peer(pctx, pkpeer) > 0)
612 rv = 1;
613 err:
8fdc3734 614 EC_KEY_free(ecpeer);
c5ba2d99 615 EVP_PKEY_free(pkpeer);
0f113f3e
MC
616 return rv;
617}
618
88e20b85
DSH
619/* Set KDF parameters based on KDF NID */
620static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX *pctx, int eckdf_nid)
0f113f3e
MC
621{
622 int kdf_nid, kdfmd_nid, cofactor;
623 const EVP_MD *kdf_md;
624 if (eckdf_nid == NID_undef)
625 return 0;
626
627 /* Lookup KDF type, cofactor mode and digest */
628 if (!OBJ_find_sigid_algs(eckdf_nid, &kdfmd_nid, &kdf_nid))
629 return 0;
630
631 if (kdf_nid == NID_dh_std_kdf)
632 cofactor = 0;
633 else if (kdf_nid == NID_dh_cofactor_kdf)
634 cofactor = 1;
635 else
636 return 0;
637
638 if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx, cofactor) <= 0)
639 return 0;
640
641 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, EVP_PKEY_ECDH_KDF_X9_62) <= 0)
642 return 0;
643
644 kdf_md = EVP_get_digestbynid(kdfmd_nid);
645 if (!kdf_md)
646 return 0;
647
648 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
649 return 0;
650 return 1;
651}
88e20b85 652
88e20b85 653static int ecdh_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
0f113f3e
MC
654{
655 int rv = 0;
656
657 X509_ALGOR *alg, *kekalg = NULL;
658 ASN1_OCTET_STRING *ukm;
659 const unsigned char *p;
660 unsigned char *der = NULL;
661 int plen, keylen;
662 const EVP_CIPHER *kekcipher;
663 EVP_CIPHER_CTX *kekctx;
664
665 if (!CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm))
666 return 0;
667
668 if (!ecdh_cms_set_kdf_param(pctx, OBJ_obj2nid(alg->algorithm))) {
669 ECerr(EC_F_ECDH_CMS_SET_SHARED_INFO, EC_R_KDF_PARAMETER_ERROR);
670 return 0;
671 }
672
673 if (alg->parameter->type != V_ASN1_SEQUENCE)
674 return 0;
675
676 p = alg->parameter->value.sequence->data;
677 plen = alg->parameter->value.sequence->length;
678 kekalg = d2i_X509_ALGOR(NULL, &p, plen);
679 if (!kekalg)
680 goto err;
681 kekctx = CMS_RecipientInfo_kari_get0_ctx(ri);
682 if (!kekctx)
683 goto err;
684 kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);
685 if (!kekcipher || EVP_CIPHER_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
686 goto err;
687 if (!EVP_EncryptInit_ex(kekctx, kekcipher, NULL, NULL, NULL))
688 goto err;
689 if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0)
690 goto err;
691
692 keylen = EVP_CIPHER_CTX_key_length(kekctx);
693 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
694 goto err;
695
696 plen = CMS_SharedInfo_encode(&der, kekalg, ukm, keylen);
697
698 if (!plen)
699 goto err;
700
701 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, der, plen) <= 0)
702 goto err;
703 der = NULL;
704
705 rv = 1;
706 err:
222561fe
RS
707 X509_ALGOR_free(kekalg);
708 OPENSSL_free(der);
0f113f3e
MC
709 return rv;
710}
88e20b85
DSH
711
712static int ecdh_cms_decrypt(CMS_RecipientInfo *ri)
0f113f3e
MC
713{
714 EVP_PKEY_CTX *pctx;
715 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
716 if (!pctx)
717 return 0;
718 /* See if we need to set peer key */
719 if (!EVP_PKEY_CTX_get0_peerkey(pctx)) {
720 X509_ALGOR *alg;
721 ASN1_BIT_STRING *pubkey;
722 if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &alg, &pubkey,
723 NULL, NULL, NULL))
724 return 0;
725 if (!alg || !pubkey)
726 return 0;
727 if (!ecdh_cms_set_peerkey(pctx, alg, pubkey)) {
728 ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_PEER_KEY_ERROR);
729 return 0;
730 }
731 }
732 /* Set ECDH derivation parameters and initialise unwrap context */
733 if (!ecdh_cms_set_shared_info(pctx, ri)) {
734 ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_SHARED_INFO_ERROR);
735 return 0;
736 }
737 return 1;
738}
88e20b85
DSH
739
740static int ecdh_cms_encrypt(CMS_RecipientInfo *ri)
0f113f3e
MC
741{
742 EVP_PKEY_CTX *pctx;
743 EVP_PKEY *pkey;
744 EVP_CIPHER_CTX *ctx;
745 int keylen;
746 X509_ALGOR *talg, *wrap_alg = NULL;
ac4e2577 747 const ASN1_OBJECT *aoid;
0f113f3e
MC
748 ASN1_BIT_STRING *pubkey;
749 ASN1_STRING *wrap_str;
750 ASN1_OCTET_STRING *ukm;
751 unsigned char *penc = NULL;
752 int penclen;
753 int rv = 0;
754 int ecdh_nid, kdf_type, kdf_nid, wrap_nid;
755 const EVP_MD *kdf_md;
756 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
757 if (!pctx)
758 return 0;
759 /* Get ephemeral key */
760 pkey = EVP_PKEY_CTX_get0_pkey(pctx);
761 if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &talg, &pubkey,
762 NULL, NULL, NULL))
763 goto err;
764 X509_ALGOR_get0(&aoid, NULL, NULL, talg);
765 /* Is everything uninitialised? */
766 if (aoid == OBJ_nid2obj(NID_undef)) {
767
768 EC_KEY *eckey = pkey->pkey.ec;
769 /* Set the key */
770 unsigned char *p;
771
772 penclen = i2o_ECPublicKey(eckey, NULL);
773 if (penclen <= 0)
774 goto err;
775 penc = OPENSSL_malloc(penclen);
90945fa3 776 if (penc == NULL)
0f113f3e
MC
777 goto err;
778 p = penc;
779 penclen = i2o_ECPublicKey(eckey, &p);
780 if (penclen <= 0)
781 goto err;
782 ASN1_STRING_set0(pubkey, penc, penclen);
783 pubkey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
784 pubkey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
785
786 penc = NULL;
787 X509_ALGOR_set0(talg, OBJ_nid2obj(NID_X9_62_id_ecPublicKey),
788 V_ASN1_UNDEF, NULL);
789 }
790
0d4fb843 791 /* See if custom parameters set */
0f113f3e
MC
792 kdf_type = EVP_PKEY_CTX_get_ecdh_kdf_type(pctx);
793 if (kdf_type <= 0)
794 goto err;
795 if (!EVP_PKEY_CTX_get_ecdh_kdf_md(pctx, &kdf_md))
796 goto err;
797 ecdh_nid = EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx);
798 if (ecdh_nid < 0)
799 goto err;
800 else if (ecdh_nid == 0)
801 ecdh_nid = NID_dh_std_kdf;
802 else if (ecdh_nid == 1)
803 ecdh_nid = NID_dh_cofactor_kdf;
804
805 if (kdf_type == EVP_PKEY_ECDH_KDF_NONE) {
806 kdf_type = EVP_PKEY_ECDH_KDF_X9_62;
807 if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, kdf_type) <= 0)
808 goto err;
809 } else
0d4fb843 810 /* Unknown KDF */
0f113f3e
MC
811 goto err;
812 if (kdf_md == NULL) {
813 /* Fixme later for better MD */
814 kdf_md = EVP_sha1();
815 if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
816 goto err;
817 }
818
819 if (!CMS_RecipientInfo_kari_get0_alg(ri, &talg, &ukm))
820 goto err;
821
822 /* Lookup NID for KDF+cofactor+digest */
823
824 if (!OBJ_find_sigid_by_algs(&kdf_nid, EVP_MD_type(kdf_md), ecdh_nid))
825 goto err;
826 /* Get wrap NID */
827 ctx = CMS_RecipientInfo_kari_get0_ctx(ri);
828 wrap_nid = EVP_CIPHER_CTX_type(ctx);
829 keylen = EVP_CIPHER_CTX_key_length(ctx);
830
831 /* Package wrap algorithm in an AlgorithmIdentifier */
832
833 wrap_alg = X509_ALGOR_new();
90945fa3 834 if (wrap_alg == NULL)
0f113f3e
MC
835 goto err;
836 wrap_alg->algorithm = OBJ_nid2obj(wrap_nid);
837 wrap_alg->parameter = ASN1_TYPE_new();
90945fa3 838 if (wrap_alg->parameter == NULL)
0f113f3e
MC
839 goto err;
840 if (EVP_CIPHER_param_to_asn1(ctx, wrap_alg->parameter) <= 0)
841 goto err;
842 if (ASN1_TYPE_get(wrap_alg->parameter) == NID_undef) {
843 ASN1_TYPE_free(wrap_alg->parameter);
844 wrap_alg->parameter = NULL;
845 }
846
847 if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
848 goto err;
849
850 penclen = CMS_SharedInfo_encode(&penc, wrap_alg, ukm, keylen);
851
852 if (!penclen)
853 goto err;
854
855 if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, penc, penclen) <= 0)
856 goto err;
857 penc = NULL;
858
859 /*
860 * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
861 * of another AlgorithmIdentifier.
862 */
863 penclen = i2d_X509_ALGOR(wrap_alg, &penc);
864 if (!penc || !penclen)
865 goto err;
866 wrap_str = ASN1_STRING_new();
90945fa3 867 if (wrap_str == NULL)
0f113f3e
MC
868 goto err;
869 ASN1_STRING_set0(wrap_str, penc, penclen);
870 penc = NULL;
871 X509_ALGOR_set0(talg, OBJ_nid2obj(kdf_nid), V_ASN1_SEQUENCE, wrap_str);
872
873 rv = 1;
874
875 err:
222561fe
RS
876 OPENSSL_free(penc);
877 X509_ALGOR_free(wrap_alg);
0f113f3e
MC
878 return rv;
879}
88e20b85
DSH
880
881#endif