DH: make the private key length importable / exportable
[openssl.git] / crypto / dh / dh_ameth.c
1 /*
2  * Copyright 2006-2020 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  * DH low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <stdio.h>
17 #include <openssl/x509.h>
18 #include <openssl/asn1.h>
19 #include <openssl/bn.h>
20 #include <openssl/core_names.h>
21 #include <openssl/param_build.h>
22 #include "internal/ffc.h"
23 #include "internal/cryptlib.h"
24 #include "crypto/asn1.h"
25 #include "crypto/dh.h"
26 #include "crypto/evp.h"
27 #include "dh_local.h"
28
29 /*
30  * i2d/d2i like DH parameter functions which use the appropriate routine for
31  * PKCS#3 DH or X9.42 DH.
32  */
33
34 static DH *d2i_dhp(const EVP_PKEY *pkey, const unsigned char **pp,
35                    long length)
36 {
37     DH *dh = NULL;
38     int is_dhx = (pkey->ameth == &dhx_asn1_meth);
39
40     if (is_dhx)
41         dh = d2i_DHxparams(NULL, pp, length);
42     else
43         dh = d2i_DHparams(NULL, pp, length);
44
45     if (dh != NULL) {
46         DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
47         DH_set_flags(dh, is_dhx ? DH_FLAG_TYPE_DHX : DH_FLAG_TYPE_DH);
48     }
49     return dh;
50 }
51
52 static int i2d_dhp(const EVP_PKEY *pkey, const DH *a, unsigned char **pp)
53 {
54     if (pkey->ameth == &dhx_asn1_meth)
55         return i2d_DHxparams(a, pp);
56     return i2d_DHparams(a, pp);
57 }
58
59 static void int_dh_free(EVP_PKEY *pkey)
60 {
61     DH_free(pkey->pkey.dh);
62 }
63
64 static int dh_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey)
65 {
66     const unsigned char *p, *pm;
67     int pklen, pmlen;
68     int ptype;
69     const void *pval;
70     const ASN1_STRING *pstr;
71     X509_ALGOR *palg;
72     ASN1_INTEGER *public_key = NULL;
73
74     DH *dh = NULL;
75
76     if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
77         return 0;
78     X509_ALGOR_get0(NULL, &ptype, &pval, palg);
79
80     if (ptype != V_ASN1_SEQUENCE) {
81         DHerr(DH_F_DH_PUB_DECODE, DH_R_PARAMETER_ENCODING_ERROR);
82         goto err;
83     }
84
85     pstr = pval;
86     pm = pstr->data;
87     pmlen = pstr->length;
88
89     if ((dh = d2i_dhp(pkey, &pm, pmlen)) == NULL) {
90         DHerr(DH_F_DH_PUB_DECODE, DH_R_DECODE_ERROR);
91         goto err;
92     }
93
94     if ((public_key = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) {
95         DHerr(DH_F_DH_PUB_DECODE, DH_R_DECODE_ERROR);
96         goto err;
97     }
98
99     /* We have parameters now set public key */
100     if ((dh->pub_key = ASN1_INTEGER_to_BN(public_key, NULL)) == NULL) {
101         DHerr(DH_F_DH_PUB_DECODE, DH_R_BN_DECODE_ERROR);
102         goto err;
103     }
104
105     ASN1_INTEGER_free(public_key);
106     EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, dh);
107     return 1;
108
109  err:
110     ASN1_INTEGER_free(public_key);
111     DH_free(dh);
112     return 0;
113 }
114
115 static int dh_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
116 {
117     DH *dh;
118     int ptype;
119     unsigned char *penc = NULL;
120     int penclen;
121     ASN1_STRING *str;
122     ASN1_INTEGER *pub_key = NULL;
123
124     dh = pkey->pkey.dh;
125
126     str = ASN1_STRING_new();
127     if (str == NULL) {
128         DHerr(DH_F_DH_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
129         goto err;
130     }
131     str->length = i2d_dhp(pkey, dh, &str->data);
132     if (str->length <= 0) {
133         DHerr(DH_F_DH_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
134         goto err;
135     }
136     ptype = V_ASN1_SEQUENCE;
137
138     pub_key = BN_to_ASN1_INTEGER(dh->pub_key, NULL);
139     if (pub_key == NULL)
140         goto err;
141
142     penclen = i2d_ASN1_INTEGER(pub_key, &penc);
143
144     ASN1_INTEGER_free(pub_key);
145
146     if (penclen <= 0) {
147         DHerr(DH_F_DH_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
148         goto err;
149     }
150
151     if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(pkey->ameth->pkey_id),
152                                ptype, str, penc, penclen))
153         return 1;
154
155  err:
156     OPENSSL_free(penc);
157     ASN1_STRING_free(str);
158
159     return 0;
160 }
161
162 /*
163  * PKCS#8 DH is defined in PKCS#11 of all places. It is similar to DH in that
164  * the AlgorithmIdentifier contains the parameters, the private key is
165  * explicitly included and the pubkey must be recalculated.
166  */
167
168 static int dh_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
169 {
170     const unsigned char *p, *pm;
171     int pklen, pmlen;
172     int ptype;
173     const void *pval;
174     const ASN1_STRING *pstr;
175     const X509_ALGOR *palg;
176     ASN1_INTEGER *privkey = NULL;
177     DH *dh = NULL;
178
179     if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
180         return 0;
181
182     X509_ALGOR_get0(NULL, &ptype, &pval, palg);
183
184     if (ptype != V_ASN1_SEQUENCE)
185         goto decerr;
186     if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
187         goto decerr;
188
189     pstr = pval;
190     pm = pstr->data;
191     pmlen = pstr->length;
192     if ((dh = d2i_dhp(pkey, &pm, pmlen)) == NULL)
193         goto decerr;
194
195     /* We have parameters now set private key */
196     if ((dh->priv_key = BN_secure_new()) == NULL
197         || !ASN1_INTEGER_to_BN(privkey, dh->priv_key)) {
198         DHerr(DH_F_DH_PRIV_DECODE, DH_R_BN_ERROR);
199         goto dherr;
200     }
201     /* Calculate public key, increments dirty_cnt */
202     if (!DH_generate_key(dh))
203         goto dherr;
204
205     EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, dh);
206
207     ASN1_STRING_clear_free(privkey);
208
209     return 1;
210
211  decerr:
212     DHerr(DH_F_DH_PRIV_DECODE, EVP_R_DECODE_ERROR);
213  dherr:
214     DH_free(dh);
215     ASN1_STRING_clear_free(privkey);
216     return 0;
217 }
218
219 static int dh_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
220 {
221     ASN1_STRING *params = NULL;
222     ASN1_INTEGER *prkey = NULL;
223     unsigned char *dp = NULL;
224     int dplen;
225
226     params = ASN1_STRING_new();
227
228     if (params == NULL) {
229         DHerr(DH_F_DH_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
230         goto err;
231     }
232
233     params->length = i2d_dhp(pkey, pkey->pkey.dh, &params->data);
234     if (params->length <= 0) {
235         DHerr(DH_F_DH_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
236         goto err;
237     }
238     params->type = V_ASN1_SEQUENCE;
239
240     /* Get private key into integer */
241     prkey = BN_to_ASN1_INTEGER(pkey->pkey.dh->priv_key, NULL);
242
243     if (prkey == NULL) {
244         DHerr(DH_F_DH_PRIV_ENCODE, DH_R_BN_ERROR);
245         goto err;
246     }
247
248     dplen = i2d_ASN1_INTEGER(prkey, &dp);
249
250     ASN1_STRING_clear_free(prkey);
251     prkey = NULL;
252
253     if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(pkey->ameth->pkey_id), 0,
254                          V_ASN1_SEQUENCE, params, dp, dplen))
255         goto err;
256
257     return 1;
258
259  err:
260     OPENSSL_free(dp);
261     ASN1_STRING_free(params);
262     ASN1_STRING_clear_free(prkey);
263     return 0;
264 }
265
266 static int dh_param_decode(EVP_PKEY *pkey,
267                            const unsigned char **pder, int derlen)
268 {
269     DH *dh;
270
271     if ((dh = d2i_dhp(pkey, pder, derlen)) == NULL)
272         return 0;
273     dh->dirty_cnt++;
274     EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, dh);
275     return 1;
276 }
277
278 static int dh_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
279 {
280     return i2d_dhp(pkey, pkey->pkey.dh, pder);
281 }
282
283 static int do_dh_print(BIO *bp, const DH *x, int indent, int ptype)
284 {
285     int reason = ERR_R_BUF_LIB;
286     const char *ktype = NULL;
287     BIGNUM *priv_key, *pub_key;
288
289     if (ptype == 2)
290         priv_key = x->priv_key;
291     else
292         priv_key = NULL;
293
294     if (ptype > 0)
295         pub_key = x->pub_key;
296     else
297         pub_key = NULL;
298
299     if (x->params.p == NULL || (ptype == 2 && priv_key == NULL)
300             || (ptype > 0 && pub_key == NULL)) {
301         reason = ERR_R_PASSED_NULL_PARAMETER;
302         goto err;
303     }
304
305     if (ptype == 2)
306         ktype = "DH Private-Key";
307     else if (ptype == 1)
308         ktype = "DH Public-Key";
309     else
310         ktype = "DH Parameters";
311
312     if (!BIO_indent(bp, indent, 128)
313             || BIO_printf(bp, "%s: (%d bit)\n", ktype, DH_bits(x)) <= 0)
314         goto err;
315     indent += 4;
316
317     if (!ASN1_bn_print(bp, "private-key:", priv_key, NULL, indent))
318         goto err;
319     if (!ASN1_bn_print(bp, "public-key:", pub_key, NULL, indent))
320         goto err;
321
322     if (!ossl_ffc_params_print(bp, &x->params, indent))
323         goto err;
324
325     if (x->length != 0) {
326         if (!BIO_indent(bp, indent, 128)
327                 || BIO_printf(bp, "recommended-private-length: %d bits\n",
328                               (int)x->length) <= 0)
329             goto err;
330     }
331
332     return 1;
333
334  err:
335     DHerr(DH_F_DO_DH_PRINT, reason);
336     return 0;
337 }
338
339 static int int_dh_size(const EVP_PKEY *pkey)
340 {
341     return DH_size(pkey->pkey.dh);
342 }
343
344 static int dh_bits(const EVP_PKEY *pkey)
345 {
346     return DH_bits(pkey->pkey.dh);
347 }
348
349 static int dh_security_bits(const EVP_PKEY *pkey)
350 {
351     return DH_security_bits(pkey->pkey.dh);
352 }
353
354 static int dh_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
355 {
356     return ossl_ffc_params_cmp(&a->pkey.dh->params, &a->pkey.dh->params,
357                                a->ameth != &dhx_asn1_meth);
358 }
359
360 static int int_dh_param_copy(DH *to, const DH *from, int is_x942)
361 {
362     if (is_x942 == -1)
363         is_x942 = (from->params.q != NULL);
364     if (!ossl_ffc_params_copy(&to->params, &from->params))
365         return 0;
366     if (!is_x942)
367         to->length = from->length;
368     to->dirty_cnt++;
369     return 1;
370 }
371
372 DH *DHparams_dup(const DH *dh)
373 {
374     DH *ret;
375     ret = DH_new();
376     if (ret == NULL)
377         return NULL;
378     if (!int_dh_param_copy(ret, dh, -1)) {
379         DH_free(ret);
380         return NULL;
381     }
382     return ret;
383 }
384
385 static int dh_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
386 {
387     if (to->pkey.dh == NULL) {
388         to->pkey.dh = DH_new();
389         if (to->pkey.dh == NULL)
390             return 0;
391     }
392     return int_dh_param_copy(to->pkey.dh, from->pkey.dh,
393                              from->ameth == &dhx_asn1_meth);
394 }
395
396 static int dh_missing_parameters(const EVP_PKEY *a)
397 {
398     return a->pkey.dh == NULL
399         || a->pkey.dh->params.p == NULL
400         || a->pkey.dh->params.g == NULL;
401 }
402
403 static int dh_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
404 {
405     if (dh_cmp_parameters(a, b) == 0)
406         return 0;
407     if (BN_cmp(b->pkey.dh->pub_key, a->pkey.dh->pub_key) != 0)
408         return 0;
409     else
410         return 1;
411 }
412
413 static int dh_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
414                           ASN1_PCTX *ctx)
415 {
416     return do_dh_print(bp, pkey->pkey.dh, indent, 0);
417 }
418
419 static int dh_public_print(BIO *bp, const EVP_PKEY *pkey, int indent,
420                            ASN1_PCTX *ctx)
421 {
422     return do_dh_print(bp, pkey->pkey.dh, indent, 1);
423 }
424
425 static int dh_private_print(BIO *bp, const EVP_PKEY *pkey, int indent,
426                             ASN1_PCTX *ctx)
427 {
428     return do_dh_print(bp, pkey->pkey.dh, indent, 2);
429 }
430
431 int DHparams_print(BIO *bp, const DH *x)
432 {
433     return do_dh_print(bp, x, 4, 0);
434 }
435
436 static int dh_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
437 {
438     switch (op) {
439     case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
440         return dh_buf2key(EVP_PKEY_get0_DH(pkey), arg2, arg1);
441     case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
442         return dh_key2buf(EVP_PKEY_get0_DH(pkey), arg2, 0, 1);
443     default:
444         return -2;
445     }
446 }
447
448 static int dhx_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
449 {
450     switch (op) {
451     default:
452         return -2;
453     }
454
455 }
456
457 static int dh_pkey_public_check(const EVP_PKEY *pkey)
458 {
459     DH *dh = pkey->pkey.dh;
460
461     if (dh->pub_key == NULL) {
462         DHerr(DH_F_DH_PKEY_PUBLIC_CHECK, DH_R_MISSING_PUBKEY);
463         return 0;
464     }
465
466     return DH_check_pub_key_ex(dh, dh->pub_key);
467 }
468
469 static int dh_pkey_param_check(const EVP_PKEY *pkey)
470 {
471     DH *dh = pkey->pkey.dh;
472
473     return DH_check_ex(dh);
474 }
475
476 static size_t dh_pkey_dirty_cnt(const EVP_PKEY *pkey)
477 {
478     return pkey->pkey.dh->dirty_cnt;
479 }
480
481 static int dh_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
482                              EVP_KEYMGMT *to_keymgmt, OSSL_LIB_CTX *libctx,
483                              const char *propq)
484 {
485     DH *dh = from->pkey.dh;
486     OSSL_PARAM_BLD *tmpl;
487     const BIGNUM *p = DH_get0_p(dh), *g = DH_get0_g(dh), *q = DH_get0_q(dh);
488     long l = DH_get_length(dh);
489     const BIGNUM *pub_key = DH_get0_pub_key(dh);
490     const BIGNUM *priv_key = DH_get0_priv_key(dh);
491     OSSL_PARAM *params = NULL;
492     int selection = 0;
493     int rv = 0;
494
495     /*
496      * If the DH method is foreign, then we can't be sure of anything, and
497      * can therefore not export or pretend to export.
498      */
499     if (dh_get_method(dh) != DH_OpenSSL())
500         return 0;
501
502     if (p == NULL || g == NULL)
503         return 0;
504
505     tmpl = OSSL_PARAM_BLD_new();
506     if (tmpl == NULL)
507         return 0;
508     if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p)
509         || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g))
510         goto err;
511     if (q != NULL) {
512         if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, q))
513             goto err;
514     }
515     selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
516     if (l > 0) {
517         if (!OSSL_PARAM_BLD_push_long(tmpl, OSSL_PKEY_PARAM_DH_PRIV_LEN, l))
518             goto err;
519         selection |= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS;
520     }
521     if (pub_key != NULL) {
522         if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY, pub_key))
523             goto err;
524         selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
525     }
526     if (priv_key != NULL) {
527         if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY,
528                                     priv_key))
529             goto err;
530         selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
531     }
532
533     if ((params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
534         goto err;
535
536     /* We export, the provider imports */
537     rv = evp_keymgmt_import(to_keymgmt, to_keydata, selection, params);
538
539     OSSL_PARAM_BLD_free_params(params);
540 err:
541     OSSL_PARAM_BLD_free(tmpl);
542     return rv;
543 }
544
545 static int dh_pkey_import_from_type(const OSSL_PARAM params[], void *vpctx,
546                                     int type)
547 {
548     EVP_PKEY_CTX *pctx = vpctx;
549     EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
550     DH *dh = dh_new_ex(pctx->libctx);
551
552     if (dh == NULL) {
553         ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
554         return 0;
555     }
556     DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
557     DH_set_flags(dh, type == EVP_PKEY_DH ? DH_FLAG_TYPE_DH : DH_FLAG_TYPE_DHX);
558
559     if (!dh_params_fromdata(dh, params)
560         || !dh_key_fromdata(dh, params)
561         || !EVP_PKEY_assign(pkey, type, dh)) {
562         DH_free(dh);
563         return 0;
564     }
565     return 1;
566 }
567
568 static int dh_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
569 {
570     return dh_pkey_import_from_type(params, vpctx, EVP_PKEY_DH);
571 }
572
573 static int dhx_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
574 {
575     return dh_pkey_import_from_type(params, vpctx, EVP_PKEY_DHX);
576 }
577
578 const EVP_PKEY_ASN1_METHOD dh_asn1_meth = {
579     EVP_PKEY_DH,
580     EVP_PKEY_DH,
581     0,
582
583     "DH",
584     "OpenSSL PKCS#3 DH method",
585
586     dh_pub_decode,
587     dh_pub_encode,
588     dh_pub_cmp,
589     dh_public_print,
590
591     dh_priv_decode,
592     dh_priv_encode,
593     dh_private_print,
594
595     int_dh_size,
596     dh_bits,
597     dh_security_bits,
598
599     dh_param_decode,
600     dh_param_encode,
601     dh_missing_parameters,
602     dh_copy_parameters,
603     dh_cmp_parameters,
604     dh_param_print,
605     0,
606
607     int_dh_free,
608     dh_pkey_ctrl,
609
610     0, 0, 0, 0, 0,
611
612     0,
613     dh_pkey_public_check,
614     dh_pkey_param_check,
615
616     0, 0, 0, 0,
617
618     dh_pkey_dirty_cnt,
619     dh_pkey_export_to,
620     dh_pkey_import_from,
621 };
622
623 const EVP_PKEY_ASN1_METHOD dhx_asn1_meth = {
624     EVP_PKEY_DHX,
625     EVP_PKEY_DHX,
626     0,
627
628     "X9.42 DH",
629     "OpenSSL X9.42 DH method",
630
631     dh_pub_decode,
632     dh_pub_encode,
633     dh_pub_cmp,
634     dh_public_print,
635
636     dh_priv_decode,
637     dh_priv_encode,
638     dh_private_print,
639
640     int_dh_size,
641     dh_bits,
642     dh_security_bits,
643
644     dh_param_decode,
645     dh_param_encode,
646     dh_missing_parameters,
647     dh_copy_parameters,
648     dh_cmp_parameters,
649     dh_param_print,
650     0,
651
652     int_dh_free,
653     dhx_pkey_ctrl,
654
655     0, 0, 0, 0, 0,
656
657     0,
658     dh_pkey_public_check,
659     dh_pkey_param_check,
660     0, 0, 0, 0,
661     dh_pkey_dirty_cnt,
662     dh_pkey_export_to,
663     dhx_pkey_import_from,
664 };