1 /*
2 * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 /*
11 * ECDH and ECDSA low level APIs are deprecated for public use, but still ok
12 * for internal use.
13 */
14 #include "internal/deprecated.h"
15
16 #include <stdio.h>
17 #include "internal/cryptlib.h"
18 #include <openssl/asn1t.h>
19 #include <openssl/x509.h>
20 #include <openssl/ec.h>
21 #include "ec_local.h"
22 #include <openssl/evp.h>
23 #include "crypto/evp.h"
24
25 /* EC pkey context structure */
26
27 typedef struct {
28 /* Key and paramgen group */
29 EC_GROUP *gen_group;
30 /* message digest */
31 const EVP_MD *md;
32 /* Duplicate key if custom cofactor needed */
33 EC_KEY *co_key;
34 /* Cofactor mode */
35 signed char cofactor_mode;
36 /* KDF (if any) to use for ECDH */
37 char kdf_type;
38 /* Message digest to use for key derivation */
39 const EVP_MD *kdf_md;
40 /* User key material */
41 unsigned char *kdf_ukm;
42 size_t kdf_ukmlen;
43 /* KDF output length */
44 size_t kdf_outlen;
45 } EC_PKEY_CTX;
46
pkey_ec_init(EVP_PKEY_CTX * ctx)47 static int pkey_ec_init(EVP_PKEY_CTX *ctx)
48 {
49 EC_PKEY_CTX *dctx;
50
51 if ((dctx = OPENSSL_zalloc(sizeof(*dctx))) == NULL)
52 return 0;
53
54 dctx->cofactor_mode = -1;
55 dctx->kdf_type = EVP_PKEY_ECDH_KDF_NONE;
56 ctx->data = dctx;
57 return 1;
58 }
59
pkey_ec_copy(EVP_PKEY_CTX * dst,const EVP_PKEY_CTX * src)60 static int pkey_ec_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
61 {
62 EC_PKEY_CTX *dctx, *sctx;
63 if (!pkey_ec_init(dst))
64 return 0;
65 sctx = src->data;
66 dctx = dst->data;
67 if (sctx->gen_group) {
68 dctx->gen_group = EC_GROUP_dup(sctx->gen_group);
69 if (!dctx->gen_group)
70 return 0;
71 }
72 dctx->md = sctx->md;
73
74 if (sctx->co_key) {
75 dctx->co_key = EC_KEY_dup(sctx->co_key);
76 if (!dctx->co_key)
77 return 0;
78 }
79 dctx->kdf_type = sctx->kdf_type;
80 dctx->kdf_md = sctx->kdf_md;
81 dctx->kdf_outlen = sctx->kdf_outlen;
82 if (sctx->kdf_ukm) {
83 dctx->kdf_ukm = OPENSSL_memdup(sctx->kdf_ukm, sctx->kdf_ukmlen);
84 if (!dctx->kdf_ukm)
85 return 0;
86 } else
87 dctx->kdf_ukm = NULL;
88 dctx->kdf_ukmlen = sctx->kdf_ukmlen;
89 return 1;
90 }
91
pkey_ec_cleanup(EVP_PKEY_CTX * ctx)92 static void pkey_ec_cleanup(EVP_PKEY_CTX *ctx)
93 {
94 EC_PKEY_CTX *dctx = ctx->data;
95 if (dctx != NULL) {
96 EC_GROUP_free(dctx->gen_group);
97 EC_KEY_free(dctx->co_key);
98 OPENSSL_free(dctx->kdf_ukm);
99 OPENSSL_free(dctx);
100 ctx->data = NULL;
101 }
102 }
103
pkey_ec_sign(EVP_PKEY_CTX * ctx,unsigned char * sig,size_t * siglen,const unsigned char * tbs,size_t tbslen)104 static int pkey_ec_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
105 const unsigned char *tbs, size_t tbslen)
106 {
107 int ret, type;
108 unsigned int sltmp;
109 EC_PKEY_CTX *dctx = ctx->data;
110 /*
111 * Discard const. Its marked as const because this may be a cached copy of
112 * the "real" key. These calls don't make any modifications that need to
113 * be reflected back in the "original" key.
114 */
115 EC_KEY *ec = (EC_KEY *)EVP_PKEY_get0_EC_KEY(ctx->pkey);
116 const int sig_sz = ECDSA_size(ec);
117
118 /* ensure cast to size_t is safe */
119 if (!ossl_assert(sig_sz > 0))
120 return 0;
121
122 if (sig == NULL) {
123 *siglen = (size_t)sig_sz;
124 return 1;
125 }
126
127 if (*siglen < (size_t)sig_sz) {
128 ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
129 return 0;
130 }
131
132 type = (dctx->md != NULL) ? EVP_MD_get_type(dctx->md) : NID_sha1;
133
134 ret = ECDSA_sign(type, tbs, tbslen, sig, &sltmp, ec);
135
136 if (ret <= 0)
137 return ret;
138 *siglen = (size_t)sltmp;
139 return 1;
140 }
141
pkey_ec_verify(EVP_PKEY_CTX * ctx,const unsigned char * sig,size_t siglen,const unsigned char * tbs,size_t tbslen)142 static int pkey_ec_verify(EVP_PKEY_CTX *ctx,
143 const unsigned char *sig, size_t siglen,
144 const unsigned char *tbs, size_t tbslen)
145 {
146 int ret, type;
147 EC_PKEY_CTX *dctx = ctx->data;
148 /*
149 * Discard const. Its marked as const because this may be a cached copy of
150 * the "real" key. These calls don't make any modifications that need to
151 * be reflected back in the "original" key.
152 */
153 EC_KEY *ec = (EC_KEY *)EVP_PKEY_get0_EC_KEY(ctx->pkey);
154
155 if (dctx->md)
156 type = EVP_MD_get_type(dctx->md);
157 else
158 type = NID_sha1;
159
160 ret = ECDSA_verify(type, tbs, tbslen, sig, siglen, ec);
161
162 return ret;
163 }
164
165 #ifndef OPENSSL_NO_EC
pkey_ec_derive(EVP_PKEY_CTX * ctx,unsigned char * key,size_t * keylen)166 static int pkey_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
167 {
168 int ret;
169 size_t outlen;
170 const EC_POINT *pubkey = NULL;
171 EC_KEY *eckey;
172 const EC_KEY *eckeypub;
173 EC_PKEY_CTX *dctx = ctx->data;
174
175 if (ctx->pkey == NULL || ctx->peerkey == NULL) {
176 ERR_raise(ERR_LIB_EC, EC_R_KEYS_NOT_SET);
177 return 0;
178 }
179 eckeypub = EVP_PKEY_get0_EC_KEY(ctx->peerkey);
180 if (eckeypub == NULL) {
181 ERR_raise(ERR_LIB_EC, EC_R_KEYS_NOT_SET);
182 return 0;
183 }
184
185 eckey = dctx->co_key ? dctx->co_key
186 : (EC_KEY *)EVP_PKEY_get0_EC_KEY(ctx->pkey);
187
188 if (!key) {
189 const EC_GROUP *group;
190 group = EC_KEY_get0_group(eckey);
191
192 if (group == NULL)
193 return 0;
194 *keylen = (EC_GROUP_get_degree(group) + 7) / 8;
195 return 1;
196 }
197 pubkey = EC_KEY_get0_public_key(eckeypub);
198
199 /*
200 * NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is not
201 * an error, the result is truncated.
202 */
203
204 outlen = *keylen;
205
206 ret = ECDH_compute_key(key, outlen, pubkey, eckey, 0);
207 if (ret <= 0)
208 return 0;
209 *keylen = ret;
210 return 1;
211 }
212
pkey_ec_kdf_derive(EVP_PKEY_CTX * ctx,unsigned char * key,size_t * keylen)213 static int pkey_ec_kdf_derive(EVP_PKEY_CTX *ctx,
214 unsigned char *key, size_t *keylen)
215 {
216 EC_PKEY_CTX *dctx = ctx->data;
217 unsigned char *ktmp = NULL;
218 size_t ktmplen;
219 int rv = 0;
220 if (dctx->kdf_type == EVP_PKEY_ECDH_KDF_NONE)
221 return pkey_ec_derive(ctx, key, keylen);
222 if (!key) {
223 *keylen = dctx->kdf_outlen;
224 return 1;
225 }
226 if (*keylen != dctx->kdf_outlen)
227 return 0;
228 if (!pkey_ec_derive(ctx, NULL, &ktmplen))
229 return 0;
230 if ((ktmp = OPENSSL_malloc(ktmplen)) == NULL)
231 return 0;
232 if (!pkey_ec_derive(ctx, ktmp, &ktmplen))
233 goto err;
234 /* Do KDF stuff */
235 if (!ossl_ecdh_kdf_X9_63(key, *keylen, ktmp, ktmplen,
236 dctx->kdf_ukm, dctx->kdf_ukmlen, dctx->kdf_md,
237 ctx->libctx, ctx->propquery))
238 goto err;
239 rv = 1;
240
241 err:
242 OPENSSL_clear_free(ktmp, ktmplen);
243 return rv;
244 }
245 #endif
246
pkey_ec_ctrl(EVP_PKEY_CTX * ctx,int type,int p1,void * p2)247 static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
248 {
249 EC_PKEY_CTX *dctx = ctx->data;
250 EC_GROUP *group;
251 switch (type) {
252 case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
253 group = EC_GROUP_new_by_curve_name(p1);
254 if (group == NULL) {
255 ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
256 return 0;
257 }
258 EC_GROUP_free(dctx->gen_group);
259 dctx->gen_group = group;
260 return 1;
261
262 case EVP_PKEY_CTRL_EC_PARAM_ENC:
263 if (!dctx->gen_group) {
264 ERR_raise(ERR_LIB_EC, EC_R_NO_PARAMETERS_SET);
265 return 0;
266 }
267 EC_GROUP_set_asn1_flag(dctx->gen_group, p1);
268 return 1;
269
270 #ifndef OPENSSL_NO_EC
271 case EVP_PKEY_CTRL_EC_ECDH_COFACTOR:
272 if (p1 == -2) {
273 if (dctx->cofactor_mode != -1)
274 return dctx->cofactor_mode;
275 else {
276 const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ctx->pkey);
277 return EC_KEY_get_flags(ec_key) & EC_FLAG_COFACTOR_ECDH ? 1 : 0;
278 }
279 } else if (p1 < -1 || p1 > 1)
280 return -2;
281 dctx->cofactor_mode = p1;
282 if (p1 != -1) {
283 EC_KEY *ec_key = (EC_KEY *)EVP_PKEY_get0_EC_KEY(ctx->pkey);
284
285 /*
286 * We discarded the "const" above. This will only work if the key is
287 * a "real" legacy key, and not a cached copy of a provided key
288 */
289 if (evp_pkey_is_provided(ctx->pkey)) {
290 ERR_raise(ERR_LIB_EC, ERR_R_UNSUPPORTED);
291 return 0;
292 }
293 if (!ec_key->group)
294 return -2;
295 /* If cofactor is 1 cofactor mode does nothing */
296 if (BN_is_one(ec_key->group->cofactor))
297 return 1;
298 if (!dctx->co_key) {
299 dctx->co_key = EC_KEY_dup(ec_key);
300 if (!dctx->co_key)
301 return 0;
302 }
303 if (p1)
304 EC_KEY_set_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
305 else
306 EC_KEY_clear_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
307 } else {
308 EC_KEY_free(dctx->co_key);
309 dctx->co_key = NULL;
310 }
311 return 1;
312 #endif
313
314 case EVP_PKEY_CTRL_EC_KDF_TYPE:
315 if (p1 == -2)
316 return dctx->kdf_type;
317 if (p1 != EVP_PKEY_ECDH_KDF_NONE && p1 != EVP_PKEY_ECDH_KDF_X9_63)
318 return -2;
319 dctx->kdf_type = p1;
320 return 1;
321
322 case EVP_PKEY_CTRL_EC_KDF_MD:
323 dctx->kdf_md = p2;
324 return 1;
325
326 case EVP_PKEY_CTRL_GET_EC_KDF_MD:
327 *(const EVP_MD **)p2 = dctx->kdf_md;
328 return 1;
329
330 case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
331 if (p1 <= 0)
332 return -2;
333 dctx->kdf_outlen = (size_t)p1;
334 return 1;
335
336 case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
337 *(int *)p2 = dctx->kdf_outlen;
338 return 1;
339
340 case EVP_PKEY_CTRL_EC_KDF_UKM:
341 OPENSSL_free(dctx->kdf_ukm);
342 dctx->kdf_ukm = p2;
343 if (p2)
344 dctx->kdf_ukmlen = p1;
345 else
346 dctx->kdf_ukmlen = 0;
347 return 1;
348
349 case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
350 *(unsigned char **)p2 = dctx->kdf_ukm;
351 return dctx->kdf_ukmlen;
352
353 case EVP_PKEY_CTRL_MD:
354 if (EVP_MD_get_type((const EVP_MD *)p2) != NID_sha1 &&
355 EVP_MD_get_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 &&
356 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha224 &&
357 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha256 &&
358 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha384 &&
359 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha512 &&
360 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_224 &&
361 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_256 &&
362 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_384 &&
363 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_512 &&
364 EVP_MD_get_type((const EVP_MD *)p2) != NID_sm3) {
365 ERR_raise(ERR_LIB_EC, EC_R_INVALID_DIGEST_TYPE);
366 return 0;
367 }
368 dctx->md = p2;
369 return 1;
370
371 case EVP_PKEY_CTRL_GET_MD:
372 *(const EVP_MD **)p2 = dctx->md;
373 return 1;
374
375 case EVP_PKEY_CTRL_PEER_KEY:
376 /* Default behaviour is OK */
377 case EVP_PKEY_CTRL_DIGESTINIT:
378 case EVP_PKEY_CTRL_PKCS7_SIGN:
379 case EVP_PKEY_CTRL_CMS_SIGN:
380 return 1;
381
382 default:
383 return -2;
384
385 }
386 }
387
pkey_ec_ctrl_str(EVP_PKEY_CTX * ctx,const char * type,const char * value)388 static int pkey_ec_ctrl_str(EVP_PKEY_CTX *ctx,
389 const char *type, const char *value)
390 {
391 if (strcmp(type, "ec_paramgen_curve") == 0) {
392 int nid;
393 nid = EC_curve_nist2nid(value);
394 if (nid == NID_undef)
395 nid = OBJ_sn2nid(value);
396 if (nid == NID_undef)
397 nid = OBJ_ln2nid(value);
398 if (nid == NID_undef) {
399 ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
400 return 0;
401 }
402 return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
403 } else if (strcmp(type, "ec_param_enc") == 0) {
404 int param_enc;
405 if (strcmp(value, "explicit") == 0)
406 param_enc = 0;
407 else if (strcmp(value, "named_curve") == 0)
408 param_enc = OPENSSL_EC_NAMED_CURVE;
409 else
410 return -2;
411 return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
412 } else if (strcmp(type, "ecdh_kdf_md") == 0) {
413 const EVP_MD *md;
414 if ((md = EVP_get_digestbyname(value)) == NULL) {
415 ERR_raise(ERR_LIB_EC, EC_R_INVALID_DIGEST);
416 return 0;
417 }
418 return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md);
419 } else if (strcmp(type, "ecdh_cofactor_mode") == 0) {
420 int co_mode;
421 co_mode = atoi(value);
422 return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, co_mode);
423 }
424
425 return -2;
426 }
427
pkey_ec_paramgen(EVP_PKEY_CTX * ctx,EVP_PKEY * pkey)428 static int pkey_ec_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
429 {
430 EC_KEY *ec = NULL;
431 EC_PKEY_CTX *dctx = ctx->data;
432 int ret;
433
434 if (dctx->gen_group == NULL) {
435 ERR_raise(ERR_LIB_EC, EC_R_NO_PARAMETERS_SET);
436 return 0;
437 }
438 ec = EC_KEY_new();
439 if (ec == NULL)
440 return 0;
441 if (!(ret = EC_KEY_set_group(ec, dctx->gen_group))
442 || !ossl_assert(ret = EVP_PKEY_assign_EC_KEY(pkey, ec)))
443 EC_KEY_free(ec);
444 return ret;
445 }
446
pkey_ec_keygen(EVP_PKEY_CTX * ctx,EVP_PKEY * pkey)447 static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
448 {
449 EC_KEY *ec = NULL;
450 EC_PKEY_CTX *dctx = ctx->data;
451 int ret;
452
453 if (ctx->pkey == NULL && dctx->gen_group == NULL) {
454 ERR_raise(ERR_LIB_EC, EC_R_NO_PARAMETERS_SET);
455 return 0;
456 }
457 ec = EC_KEY_new();
458 if (ec == NULL)
459 return 0;
460 if (!ossl_assert(EVP_PKEY_assign_EC_KEY(pkey, ec))) {
461 EC_KEY_free(ec);
462 return 0;
463 }
464 /* Note: if error is returned, we count on caller to free pkey->pkey.ec */
465 if (ctx->pkey != NULL)
466 ret = EVP_PKEY_copy_parameters(pkey, ctx->pkey);
467 else
468 ret = EC_KEY_set_group(ec, dctx->gen_group);
469
470 return ret ? EC_KEY_generate_key(ec) : 0;
471 }
472
473 static const EVP_PKEY_METHOD ec_pkey_meth = {
474 EVP_PKEY_EC,
475 0,
476 pkey_ec_init,
477 pkey_ec_copy,
478 pkey_ec_cleanup,
479
480 0,
481 pkey_ec_paramgen,
482
483 0,
484 pkey_ec_keygen,
485
486 0,
487 pkey_ec_sign,
488
489 0,
490 pkey_ec_verify,
491
492 0, 0,
493
494 0, 0, 0, 0,
495
496 0,
497 0,
498
499 0,
500 0,
501
502 0,
503 #ifndef OPENSSL_NO_EC
504 pkey_ec_kdf_derive,
505 #else
506 0,
507 #endif
508 pkey_ec_ctrl,
509 pkey_ec_ctrl_str
510 };
511
ossl_ec_pkey_method(void)512 const EVP_PKEY_METHOD *ossl_ec_pkey_method(void)
513 {
514 return &ec_pkey_meth;
515 }
516