EVP: Implement support for key downgrading in backends
[openssl.git] / crypto / ec / ec_pmeth.c
1 /*
2  * Copyright 2006-2018 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
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         ECerr(EC_F_PKEY_EC_INIT, ERR_R_MALLOC_FAILURE);
53         return 0;
54     }
55
56     dctx->cofactor_mode = -1;
57     dctx->kdf_type = EVP_PKEY_ECDH_KDF_NONE;
58     ctx->data = dctx;
59     return 1;
60 }
61
62 static int pkey_ec_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
63 {
64     EC_PKEY_CTX *dctx, *sctx;
65     if (!pkey_ec_init(dst))
66         return 0;
67     sctx = src->data;
68     dctx = dst->data;
69     if (sctx->gen_group) {
70         dctx->gen_group = EC_GROUP_dup(sctx->gen_group);
71         if (!dctx->gen_group)
72             return 0;
73     }
74     dctx->md = sctx->md;
75
76     if (sctx->co_key) {
77         dctx->co_key = EC_KEY_dup(sctx->co_key);
78         if (!dctx->co_key)
79             return 0;
80     }
81     dctx->kdf_type = sctx->kdf_type;
82     dctx->kdf_md = sctx->kdf_md;
83     dctx->kdf_outlen = sctx->kdf_outlen;
84     if (sctx->kdf_ukm) {
85         dctx->kdf_ukm = OPENSSL_memdup(sctx->kdf_ukm, sctx->kdf_ukmlen);
86         if (!dctx->kdf_ukm)
87             return 0;
88     } else
89         dctx->kdf_ukm = NULL;
90     dctx->kdf_ukmlen = sctx->kdf_ukmlen;
91     return 1;
92 }
93
94 static void pkey_ec_cleanup(EVP_PKEY_CTX *ctx)
95 {
96     EC_PKEY_CTX *dctx = ctx->data;
97     if (dctx != NULL) {
98         EC_GROUP_free(dctx->gen_group);
99         EC_KEY_free(dctx->co_key);
100         OPENSSL_free(dctx->kdf_ukm);
101         OPENSSL_free(dctx);
102         ctx->data = NULL;
103     }
104 }
105
106 static int pkey_ec_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
107                         const unsigned char *tbs, size_t tbslen)
108 {
109     int ret, type;
110     unsigned int sltmp;
111     EC_PKEY_CTX *dctx = ctx->data;
112     EC_KEY *ec = ctx->pkey->pkey.ec;
113     const int sig_sz = ECDSA_size(ec);
114
115     /* ensure cast to size_t is safe */
116     if (!ossl_assert(sig_sz > 0))
117         return 0;
118
119     if (sig == NULL) {
120         *siglen = (size_t)sig_sz;
121         return 1;
122     }
123
124     if (*siglen < (size_t)sig_sz) {
125         ECerr(EC_F_PKEY_EC_SIGN, EC_R_BUFFER_TOO_SMALL);
126         return 0;
127     }
128
129     type = (dctx->md != NULL) ? EVP_MD_type(dctx->md) : NID_sha1;
130
131     ret = ECDSA_sign(type, tbs, tbslen, sig, &sltmp, ec);
132
133     if (ret <= 0)
134         return ret;
135     *siglen = (size_t)sltmp;
136     return 1;
137 }
138
139 static int pkey_ec_verify(EVP_PKEY_CTX *ctx,
140                           const unsigned char *sig, size_t siglen,
141                           const unsigned char *tbs, size_t tbslen)
142 {
143     int ret, type;
144     EC_PKEY_CTX *dctx = ctx->data;
145     EC_KEY *ec = ctx->pkey->pkey.ec;
146
147     if (dctx->md)
148         type = EVP_MD_type(dctx->md);
149     else
150         type = NID_sha1;
151
152     ret = ECDSA_verify(type, tbs, tbslen, sig, siglen, ec);
153
154     return ret;
155 }
156
157 #ifndef OPENSSL_NO_EC
158 static int pkey_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
159 {
160     int ret;
161     size_t outlen;
162     const EC_POINT *pubkey = NULL;
163     EC_KEY *eckey;
164     EC_PKEY_CTX *dctx = ctx->data;
165     if (!ctx->pkey || !ctx->peerkey) {
166         ECerr(EC_F_PKEY_EC_DERIVE, EC_R_KEYS_NOT_SET);
167         return 0;
168     }
169
170     eckey = dctx->co_key ? dctx->co_key : ctx->pkey->pkey.ec;
171
172     if (!key) {
173         const EC_GROUP *group;
174         group = EC_KEY_get0_group(eckey);
175         *keylen = (EC_GROUP_get_degree(group) + 7) / 8;
176         return 1;
177     }
178     pubkey = EC_KEY_get0_public_key(ctx->peerkey->pkey.ec);
179
180     /*
181      * NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is not
182      * an error, the result is truncated.
183      */
184
185     outlen = *keylen;
186
187     ret = ECDH_compute_key(key, outlen, pubkey, eckey, 0);
188     if (ret <= 0)
189         return 0;
190     *keylen = ret;
191     return 1;
192 }
193
194 static int pkey_ec_kdf_derive(EVP_PKEY_CTX *ctx,
195                               unsigned char *key, size_t *keylen)
196 {
197     EC_PKEY_CTX *dctx = ctx->data;
198     unsigned char *ktmp = NULL;
199     size_t ktmplen;
200     int rv = 0;
201     if (dctx->kdf_type == EVP_PKEY_ECDH_KDF_NONE)
202         return pkey_ec_derive(ctx, key, keylen);
203     if (!key) {
204         *keylen = dctx->kdf_outlen;
205         return 1;
206     }
207     if (*keylen != dctx->kdf_outlen)
208         return 0;
209     if (!pkey_ec_derive(ctx, NULL, &ktmplen))
210         return 0;
211     if ((ktmp = OPENSSL_malloc(ktmplen)) == NULL) {
212         ECerr(EC_F_PKEY_EC_KDF_DERIVE, ERR_R_MALLOC_FAILURE);
213         return 0;
214     }
215     if (!pkey_ec_derive(ctx, ktmp, &ktmplen))
216         goto err;
217     /* Do KDF stuff */
218     if (!ecdh_KDF_X9_63(key, *keylen, ktmp, ktmplen,
219                         dctx->kdf_ukm, dctx->kdf_ukmlen, dctx->kdf_md))
220         goto err;
221     rv = 1;
222
223  err:
224     OPENSSL_clear_free(ktmp, ktmplen);
225     return rv;
226 }
227 #endif
228
229 static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
230 {
231     EC_PKEY_CTX *dctx = ctx->data;
232     EC_GROUP *group;
233     switch (type) {
234     case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
235         group = EC_GROUP_new_by_curve_name(p1);
236         if (group == NULL) {
237             ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_CURVE);
238             return 0;
239         }
240         EC_GROUP_free(dctx->gen_group);
241         dctx->gen_group = group;
242         return 1;
243
244     case EVP_PKEY_CTRL_EC_PARAM_ENC:
245         if (!dctx->gen_group) {
246             ECerr(EC_F_PKEY_EC_CTRL, EC_R_NO_PARAMETERS_SET);
247             return 0;
248         }
249         EC_GROUP_set_asn1_flag(dctx->gen_group, p1);
250         return 1;
251
252 #ifndef OPENSSL_NO_EC
253     case EVP_PKEY_CTRL_EC_ECDH_COFACTOR:
254         if (p1 == -2) {
255             if (dctx->cofactor_mode != -1)
256                 return dctx->cofactor_mode;
257             else {
258                 EC_KEY *ec_key = ctx->pkey->pkey.ec;
259                 return EC_KEY_get_flags(ec_key) & EC_FLAG_COFACTOR_ECDH ? 1 : 0;
260             }
261         } else if (p1 < -1 || p1 > 1)
262             return -2;
263         dctx->cofactor_mode = p1;
264         if (p1 != -1) {
265             EC_KEY *ec_key = ctx->pkey->pkey.ec;
266             if (!ec_key->group)
267                 return -2;
268             /* If cofactor is 1 cofactor mode does nothing */
269             if (BN_is_one(ec_key->group->cofactor))
270                 return 1;
271             if (!dctx->co_key) {
272                 dctx->co_key = EC_KEY_dup(ec_key);
273                 if (!dctx->co_key)
274                     return 0;
275             }
276             if (p1)
277                 EC_KEY_set_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
278             else
279                 EC_KEY_clear_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
280         } else {
281             EC_KEY_free(dctx->co_key);
282             dctx->co_key = NULL;
283         }
284         return 1;
285 #endif
286
287     case EVP_PKEY_CTRL_EC_KDF_TYPE:
288         if (p1 == -2)
289             return dctx->kdf_type;
290         if (p1 != EVP_PKEY_ECDH_KDF_NONE && p1 != EVP_PKEY_ECDH_KDF_X9_63)
291             return -2;
292         dctx->kdf_type = p1;
293         return 1;
294
295     case EVP_PKEY_CTRL_EC_KDF_MD:
296         dctx->kdf_md = p2;
297         return 1;
298
299     case EVP_PKEY_CTRL_GET_EC_KDF_MD:
300         *(const EVP_MD **)p2 = dctx->kdf_md;
301         return 1;
302
303     case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
304         if (p1 <= 0)
305             return -2;
306         dctx->kdf_outlen = (size_t)p1;
307         return 1;
308
309     case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
310         *(int *)p2 = dctx->kdf_outlen;
311         return 1;
312
313     case EVP_PKEY_CTRL_EC_KDF_UKM:
314         OPENSSL_free(dctx->kdf_ukm);
315         dctx->kdf_ukm = p2;
316         if (p2)
317             dctx->kdf_ukmlen = p1;
318         else
319             dctx->kdf_ukmlen = 0;
320         return 1;
321
322     case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
323         *(unsigned char **)p2 = dctx->kdf_ukm;
324         return dctx->kdf_ukmlen;
325
326     case EVP_PKEY_CTRL_MD:
327         if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
328             EVP_MD_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 &&
329             EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
330             EVP_MD_type((const EVP_MD *)p2) != NID_sha256 &&
331             EVP_MD_type((const EVP_MD *)p2) != NID_sha384 &&
332             EVP_MD_type((const EVP_MD *)p2) != NID_sha512 &&
333             EVP_MD_type((const EVP_MD *)p2) != NID_sha3_224 &&
334             EVP_MD_type((const EVP_MD *)p2) != NID_sha3_256 &&
335             EVP_MD_type((const EVP_MD *)p2) != NID_sha3_384 &&
336             EVP_MD_type((const EVP_MD *)p2) != NID_sha3_512 &&
337             EVP_MD_type((const EVP_MD *)p2) != NID_sm3) {
338             ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_DIGEST_TYPE);
339             return 0;
340         }
341         dctx->md = p2;
342         return 1;
343
344     case EVP_PKEY_CTRL_GET_MD:
345         *(const EVP_MD **)p2 = dctx->md;
346         return 1;
347
348     case EVP_PKEY_CTRL_PEER_KEY:
349         /* Default behaviour is OK */
350     case EVP_PKEY_CTRL_DIGESTINIT:
351     case EVP_PKEY_CTRL_PKCS7_SIGN:
352     case EVP_PKEY_CTRL_CMS_SIGN:
353         return 1;
354
355     default:
356         return -2;
357
358     }
359 }
360
361 static int pkey_ec_ctrl_str(EVP_PKEY_CTX *ctx,
362                             const char *type, const char *value)
363 {
364     if (strcmp(type, "ec_paramgen_curve") == 0) {
365         int nid;
366         nid = EC_curve_nist2nid(value);
367         if (nid == NID_undef)
368             nid = OBJ_sn2nid(value);
369         if (nid == NID_undef)
370             nid = OBJ_ln2nid(value);
371         if (nid == NID_undef) {
372             ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_CURVE);
373             return 0;
374         }
375         return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
376     } else if (strcmp(type, "ec_param_enc") == 0) {
377         int param_enc;
378         if (strcmp(value, "explicit") == 0)
379             param_enc = 0;
380         else if (strcmp(value, "named_curve") == 0)
381             param_enc = OPENSSL_EC_NAMED_CURVE;
382         else
383             return -2;
384         return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
385     } else if (strcmp(type, "ecdh_kdf_md") == 0) {
386         const EVP_MD *md;
387         if ((md = EVP_get_digestbyname(value)) == NULL) {
388             ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_DIGEST);
389             return 0;
390         }
391         return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md);
392     } else if (strcmp(type, "ecdh_cofactor_mode") == 0) {
393         int co_mode;
394         co_mode = atoi(value);
395         return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, co_mode);
396     }
397
398     return -2;
399 }
400
401 static int pkey_ec_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
402 {
403     EC_KEY *ec = NULL;
404     EC_PKEY_CTX *dctx = ctx->data;
405     int ret;
406
407     if (dctx->gen_group == NULL) {
408         ECerr(EC_F_PKEY_EC_PARAMGEN, EC_R_NO_PARAMETERS_SET);
409         return 0;
410     }
411     ec = EC_KEY_new();
412     if (ec == NULL)
413         return 0;
414     if (!(ret = EC_KEY_set_group(ec, dctx->gen_group))
415         || !ossl_assert(ret = EVP_PKEY_assign_EC_KEY(pkey, ec)))
416         EC_KEY_free(ec);
417     return ret;
418 }
419
420 static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
421 {
422     EC_KEY *ec = NULL;
423     EC_PKEY_CTX *dctx = ctx->data;
424     int ret;
425
426     if (ctx->pkey == NULL && dctx->gen_group == NULL) {
427         ECerr(EC_F_PKEY_EC_KEYGEN, EC_R_NO_PARAMETERS_SET);
428         return 0;
429     }
430     ec = EC_KEY_new();
431     if (ec == NULL)
432         return 0;
433     if (!ossl_assert(EVP_PKEY_assign_EC_KEY(pkey, ec))) {
434         EC_KEY_free(ec);
435         return 0;
436     }
437     /* Note: if error is returned, we count on caller to free pkey->pkey.ec */
438     if (ctx->pkey != NULL)
439         ret = EVP_PKEY_copy_parameters(pkey, ctx->pkey);
440     else
441         ret = EC_KEY_set_group(ec, dctx->gen_group);
442
443     return ret ? EC_KEY_generate_key(ec) : 0;
444 }
445
446 static const EVP_PKEY_METHOD ec_pkey_meth = {
447     EVP_PKEY_EC,
448     0,
449     pkey_ec_init,
450     pkey_ec_copy,
451     pkey_ec_cleanup,
452
453     0,
454     pkey_ec_paramgen,
455
456     0,
457     pkey_ec_keygen,
458
459     0,
460     pkey_ec_sign,
461
462     0,
463     pkey_ec_verify,
464
465     0, 0,
466
467     0, 0, 0, 0,
468
469     0,
470     0,
471
472     0,
473     0,
474
475     0,
476 #ifndef OPENSSL_NO_EC
477     pkey_ec_kdf_derive,
478 #else
479     0,
480 #endif
481     pkey_ec_ctrl,
482     pkey_ec_ctrl_str
483 };
484
485 const EVP_PKEY_METHOD *ec_pkey_method(void)
486 {
487     return &ec_pkey_meth;
488 }