Add RFC7919 support to EVP
[openssl.git] / crypto / dh / dh_pmeth.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/asn1t.h>
13 #include <openssl/x509.h>
14 #include <openssl/evp.h>
15 #include "dh_locl.h"
16 #include <openssl/bn.h>
17 #include <openssl/dsa.h>
18 #include <openssl/objects.h>
19 #include "internal/evp_int.h"
20
21 /* DH pkey context structure */
22
23 typedef struct {
24     /* Parameter gen parameters */
25     int prime_len;
26     int generator;
27     int use_dsa;
28     int subprime_len;
29     /* message digest used for parameter generation */
30     const EVP_MD *md;
31     int rfc5114_param;
32     int param_nid;
33     /* Keygen callback info */
34     int gentmp[2];
35     /* KDF (if any) to use for DH */
36     char kdf_type;
37     /* OID to use for KDF */
38     ASN1_OBJECT *kdf_oid;
39     /* Message digest to use for key derivation */
40     const EVP_MD *kdf_md;
41     /* User key material */
42     unsigned char *kdf_ukm;
43     size_t kdf_ukmlen;
44     /* KDF output length */
45     size_t kdf_outlen;
46 } DH_PKEY_CTX;
47
48 static int pkey_dh_init(EVP_PKEY_CTX *ctx)
49 {
50     DH_PKEY_CTX *dctx;
51
52     dctx = OPENSSL_zalloc(sizeof(*dctx));
53     if (dctx == NULL)
54         return 0;
55     dctx->prime_len = 1024;
56     dctx->subprime_len = -1;
57     dctx->generator = 2;
58     dctx->kdf_type = EVP_PKEY_DH_KDF_NONE;
59
60     ctx->data = dctx;
61     ctx->keygen_info = dctx->gentmp;
62     ctx->keygen_info_count = 2;
63
64     return 1;
65 }
66
67 static void pkey_dh_cleanup(EVP_PKEY_CTX *ctx)
68 {
69     DH_PKEY_CTX *dctx = ctx->data;
70     if (dctx != NULL) {
71         OPENSSL_free(dctx->kdf_ukm);
72         ASN1_OBJECT_free(dctx->kdf_oid);
73         OPENSSL_free(dctx);
74     }
75 }
76
77
78 static int pkey_dh_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
79 {
80     DH_PKEY_CTX *dctx, *sctx;
81     if (!pkey_dh_init(dst))
82         return 0;
83     sctx = src->data;
84     dctx = dst->data;
85     dctx->prime_len = sctx->prime_len;
86     dctx->subprime_len = sctx->subprime_len;
87     dctx->generator = sctx->generator;
88     dctx->use_dsa = sctx->use_dsa;
89     dctx->md = sctx->md;
90     dctx->rfc5114_param = sctx->rfc5114_param;
91     dctx->param_nid = sctx->param_nid;
92
93     dctx->kdf_type = sctx->kdf_type;
94     dctx->kdf_oid = OBJ_dup(sctx->kdf_oid);
95     if (dctx->kdf_oid == NULL)
96         return 0;
97     dctx->kdf_md = sctx->kdf_md;
98     if (sctx->kdf_ukm != NULL) {
99         dctx->kdf_ukm = OPENSSL_memdup(sctx->kdf_ukm, sctx->kdf_ukmlen);
100         if (dctx->kdf_ukm == NULL)
101           return 0;
102         dctx->kdf_ukmlen = sctx->kdf_ukmlen;
103     }
104     dctx->kdf_outlen = sctx->kdf_outlen;
105     return 1;
106 }
107
108 static int pkey_dh_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
109 {
110     DH_PKEY_CTX *dctx = ctx->data;
111     switch (type) {
112     case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN:
113         if (p1 < 256)
114             return -2;
115         dctx->prime_len = p1;
116         return 1;
117
118     case EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN:
119         if (dctx->use_dsa == 0)
120             return -2;
121         dctx->subprime_len = p1;
122         return 1;
123
124     case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR:
125         if (dctx->use_dsa)
126             return -2;
127         dctx->generator = p1;
128         return 1;
129
130     case EVP_PKEY_CTRL_DH_PARAMGEN_TYPE:
131 #ifdef OPENSSL_NO_DSA
132         if (p1 != 0)
133             return -2;
134 #else
135         if (p1 < 0 || p1 > 2)
136             return -2;
137 #endif
138         dctx->use_dsa = p1;
139         return 1;
140
141     case EVP_PKEY_CTRL_DH_RFC5114:
142         if (p1 < 1 || p1 > 3 || dctx->param_nid != NID_undef)
143             return -2;
144         dctx->rfc5114_param = p1;
145         return 1;
146
147     case EVP_PKEY_CTRL_DH_NID:
148         if (p1 <= 0 || dctx->rfc5114_param != 0)
149             return -2;
150         dctx->param_nid = p1;
151         return 1;
152
153     case EVP_PKEY_CTRL_PEER_KEY:
154         /* Default behaviour is OK */
155         return 1;
156
157     case EVP_PKEY_CTRL_DH_KDF_TYPE:
158         if (p1 == -2)
159             return dctx->kdf_type;
160 #ifdef OPENSSL_NO_CMS
161         if (p1 != EVP_PKEY_DH_KDF_NONE)
162 #else
163         if (p1 != EVP_PKEY_DH_KDF_NONE && p1 != EVP_PKEY_DH_KDF_X9_42)
164 #endif
165             return -2;
166         dctx->kdf_type = p1;
167         return 1;
168
169     case EVP_PKEY_CTRL_DH_KDF_MD:
170         dctx->kdf_md = p2;
171         return 1;
172
173     case EVP_PKEY_CTRL_GET_DH_KDF_MD:
174         *(const EVP_MD **)p2 = dctx->kdf_md;
175         return 1;
176
177     case EVP_PKEY_CTRL_DH_KDF_OUTLEN:
178         if (p1 <= 0)
179             return -2;
180         dctx->kdf_outlen = (size_t)p1;
181         return 1;
182
183     case EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN:
184         *(int *)p2 = dctx->kdf_outlen;
185         return 1;
186
187     case EVP_PKEY_CTRL_DH_KDF_UKM:
188         OPENSSL_free(dctx->kdf_ukm);
189         dctx->kdf_ukm = p2;
190         if (p2)
191             dctx->kdf_ukmlen = p1;
192         else
193             dctx->kdf_ukmlen = 0;
194         return 1;
195
196     case EVP_PKEY_CTRL_GET_DH_KDF_UKM:
197         *(unsigned char **)p2 = dctx->kdf_ukm;
198         return dctx->kdf_ukmlen;
199
200     case EVP_PKEY_CTRL_DH_KDF_OID:
201         ASN1_OBJECT_free(dctx->kdf_oid);
202         dctx->kdf_oid = p2;
203         return 1;
204
205     case EVP_PKEY_CTRL_GET_DH_KDF_OID:
206         *(ASN1_OBJECT **)p2 = dctx->kdf_oid;
207         return 1;
208
209     default:
210         return -2;
211
212     }
213 }
214
215 static int pkey_dh_ctrl_str(EVP_PKEY_CTX *ctx,
216                             const char *type, const char *value)
217 {
218     if (strcmp(type, "dh_paramgen_prime_len") == 0) {
219         int len;
220         len = atoi(value);
221         return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, len);
222     }
223     if (strcmp(type, "dh_rfc5114") == 0) {
224         DH_PKEY_CTX *dctx = ctx->data;
225         int len;
226         len = atoi(value);
227         if (len < 0 || len > 3)
228             return -2;
229         dctx->rfc5114_param = len;
230         return 1;
231     }
232     if (strcmp(type, "dh_param") == 0) {
233         DH_PKEY_CTX *dctx = ctx->data;
234         int nid = OBJ_sn2nid(value);
235
236         if (nid == NID_undef) {
237             DHerr(DH_F_PKEY_DH_CTRL_STR, DH_R_INVALID_PARAMETER_NAME);
238             return -2;
239         }
240         dctx->param_nid = nid;
241         return 1;
242     }
243     if (strcmp(type, "dh_paramgen_generator") == 0) {
244         int len;
245         len = atoi(value);
246         return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, len);
247     }
248     if (strcmp(type, "dh_paramgen_subprime_len") == 0) {
249         int len;
250         len = atoi(value);
251         return EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx, len);
252     }
253     if (strcmp(type, "dh_paramgen_type") == 0) {
254         int typ;
255         typ = atoi(value);
256         return EVP_PKEY_CTX_set_dh_paramgen_type(ctx, typ);
257     }
258     return -2;
259 }
260
261 #ifndef OPENSSL_NO_DSA
262
263 extern int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits,
264                                 const EVP_MD *evpmd,
265                                 const unsigned char *seed_in, size_t seed_len,
266                                 unsigned char *seed_out, int *counter_ret,
267                                 unsigned long *h_ret, BN_GENCB *cb);
268
269 extern int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,
270                                  const EVP_MD *evpmd,
271                                  const unsigned char *seed_in,
272                                  size_t seed_len, int idx,
273                                  unsigned char *seed_out, int *counter_ret,
274                                  unsigned long *h_ret, BN_GENCB *cb);
275
276 static DSA *dsa_dh_generate(DH_PKEY_CTX *dctx, BN_GENCB *pcb)
277 {
278     DSA *ret;
279     int rv = 0;
280     int prime_len = dctx->prime_len;
281     int subprime_len = dctx->subprime_len;
282     const EVP_MD *md = dctx->md;
283     if (dctx->use_dsa > 2)
284         return NULL;
285     ret = DSA_new();
286     if (ret == NULL)
287         return NULL;
288     if (subprime_len == -1) {
289         if (prime_len >= 2048)
290             subprime_len = 256;
291         else
292             subprime_len = 160;
293     }
294     if (md == NULL) {
295         if (prime_len >= 2048)
296             md = EVP_sha256();
297         else
298             md = EVP_sha1();
299     }
300     if (dctx->use_dsa == 1)
301         rv = dsa_builtin_paramgen(ret, prime_len, subprime_len, md,
302                                   NULL, 0, NULL, NULL, NULL, pcb);
303     else if (dctx->use_dsa == 2)
304         rv = dsa_builtin_paramgen2(ret, prime_len, subprime_len, md,
305                                    NULL, 0, -1, NULL, NULL, NULL, pcb);
306     if (rv <= 0) {
307         DSA_free(ret);
308         return NULL;
309     }
310     return ret;
311 }
312
313 #endif
314
315 static int pkey_dh_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
316 {
317     DH *dh = NULL;
318     DH_PKEY_CTX *dctx = ctx->data;
319     BN_GENCB *pcb;
320     int ret;
321     if (dctx->rfc5114_param) {
322         switch (dctx->rfc5114_param) {
323         case 1:
324             dh = DH_get_1024_160();
325             break;
326
327         case 2:
328             dh = DH_get_2048_224();
329             break;
330
331         case 3:
332             dh = DH_get_2048_256();
333             break;
334
335         default:
336             return -2;
337         }
338         EVP_PKEY_assign(pkey, EVP_PKEY_DHX, dh);
339         return 1;
340     }
341
342     if (dctx->param_nid != 0) {
343         if ((dh = DH_new_by_nid(dctx->param_nid)) == NULL)
344             return 0;
345         EVP_PKEY_assign(pkey, EVP_PKEY_DH, dh);
346         return 1;
347     }
348
349     if (ctx->pkey_gencb) {
350         pcb = BN_GENCB_new();
351         if (pcb == NULL)
352             return 0;
353         evp_pkey_set_cb_translate(pcb, ctx);
354     } else
355         pcb = NULL;
356 #ifndef OPENSSL_NO_DSA
357     if (dctx->use_dsa) {
358         DSA *dsa_dh;
359         dsa_dh = dsa_dh_generate(dctx, pcb);
360         BN_GENCB_free(pcb);
361         if (dsa_dh == NULL)
362             return 0;
363         dh = DSA_dup_DH(dsa_dh);
364         DSA_free(dsa_dh);
365         if (!dh)
366             return 0;
367         EVP_PKEY_assign(pkey, EVP_PKEY_DHX, dh);
368         return 1;
369     }
370 #endif
371     dh = DH_new();
372     if (dh == NULL) {
373         BN_GENCB_free(pcb);
374         return 0;
375     }
376     ret = DH_generate_parameters_ex(dh,
377                                     dctx->prime_len, dctx->generator, pcb);
378     BN_GENCB_free(pcb);
379     if (ret)
380         EVP_PKEY_assign_DH(pkey, dh);
381     else
382         DH_free(dh);
383     return ret;
384 }
385
386 static int pkey_dh_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
387 {
388     DH_PKEY_CTX *dctx = ctx->data;
389     DH *dh = NULL;
390
391     if (ctx->pkey == NULL && dctx->param_nid == 0) {
392         DHerr(DH_F_PKEY_DH_KEYGEN, DH_R_NO_PARAMETERS_SET);
393         return 0;
394     }
395     if (dctx->param_nid != 0)
396         dh = DH_new_by_nid(dctx->param_nid);
397     else
398         dh = DH_new();
399     if (dh == NULL)
400         return 0;
401     EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, dh);
402     /* Note: if error return, pkey is freed by parent routine */
403     if (ctx->pkey != NULL && !EVP_PKEY_copy_parameters(pkey, ctx->pkey))
404         return 0;
405     return DH_generate_key(pkey->pkey.dh);
406 }
407
408 static int pkey_dh_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
409                           size_t *keylen)
410 {
411     int ret;
412     DH *dh;
413     DH_PKEY_CTX *dctx = ctx->data;
414     BIGNUM *dhpub;
415     if (!ctx->pkey || !ctx->peerkey) {
416         DHerr(DH_F_PKEY_DH_DERIVE, DH_R_KEYS_NOT_SET);
417         return 0;
418     }
419     dh = ctx->pkey->pkey.dh;
420     dhpub = ctx->peerkey->pkey.dh->pub_key;
421     if (dctx->kdf_type == EVP_PKEY_DH_KDF_NONE) {
422         if (key == NULL) {
423             *keylen = DH_size(dh);
424             return 1;
425         }
426         ret = DH_compute_key(key, dhpub, dh);
427         if (ret < 0)
428             return ret;
429         *keylen = ret;
430         return 1;
431     }
432 #ifndef OPENSSL_NO_CMS
433     else if (dctx->kdf_type == EVP_PKEY_DH_KDF_X9_42) {
434
435         unsigned char *Z = NULL;
436         size_t Zlen = 0;
437         if (!dctx->kdf_outlen || !dctx->kdf_oid)
438             return 0;
439         if (key == NULL) {
440             *keylen = dctx->kdf_outlen;
441             return 1;
442         }
443         if (*keylen != dctx->kdf_outlen)
444             return 0;
445         ret = 0;
446         Zlen = DH_size(dh);
447         Z = OPENSSL_malloc(Zlen);
448         if (Z == NULL) {
449             goto err;
450         }
451         if (DH_compute_key_padded(Z, dhpub, dh) <= 0)
452             goto err;
453         if (!DH_KDF_X9_42(key, *keylen, Z, Zlen, dctx->kdf_oid,
454                           dctx->kdf_ukm, dctx->kdf_ukmlen, dctx->kdf_md))
455             goto err;
456         *keylen = dctx->kdf_outlen;
457         ret = 1;
458  err:
459         OPENSSL_clear_free(Z, Zlen);
460         return ret;
461     }
462 #endif
463     return 0;
464 }
465
466 const EVP_PKEY_METHOD dh_pkey_meth = {
467     EVP_PKEY_DH,
468     0,
469     pkey_dh_init,
470     pkey_dh_copy,
471     pkey_dh_cleanup,
472
473     0,
474     pkey_dh_paramgen,
475
476     0,
477     pkey_dh_keygen,
478
479     0,
480     0,
481
482     0,
483     0,
484
485     0, 0,
486
487     0, 0, 0, 0,
488
489     0, 0,
490
491     0, 0,
492
493     0,
494     pkey_dh_derive,
495
496     pkey_dh_ctrl,
497     pkey_dh_ctrl_str
498 };
499
500 const EVP_PKEY_METHOD dhx_pkey_meth = {
501     EVP_PKEY_DHX,
502     0,
503     pkey_dh_init,
504     pkey_dh_copy,
505     pkey_dh_cleanup,
506
507     0,
508     pkey_dh_paramgen,
509
510     0,
511     pkey_dh_keygen,
512
513     0,
514     0,
515
516     0,
517     0,
518
519     0, 0,
520
521     0, 0, 0, 0,
522
523     0, 0,
524
525     0, 0,
526
527     0,
528     pkey_dh_derive,
529
530     pkey_dh_ctrl,
531     pkey_dh_ctrl_str
532 };