Move CMS enveloping code out of the algorithms and into CMS
[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 <openssl/cms.h>
23 #include "internal/ffc.h"
24 #include "internal/cryptlib.h"
25 #include "crypto/asn1.h"
26 #include "crypto/dh.h"
27 #include "crypto/evp.h"
28 #include "dh_local.h"
29
30 /*
31  * i2d/d2i like DH parameter functions which use the appropriate routine for
32  * PKCS#3 DH or X9.42 DH.
33  */
34
35 static DH *d2i_dhp(const EVP_PKEY *pkey, const unsigned char **pp,
36                    long length)
37 {
38     DH *dh = NULL;
39     int is_dhx = (pkey->ameth == &dhx_asn1_meth);
40
41     if (is_dhx)
42         dh = d2i_DHxparams(NULL, pp, length);
43     else
44         dh = d2i_DHparams(NULL, pp, length);
45
46     if (dh != NULL) {
47         DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
48         DH_set_flags(dh, is_dhx ? DH_FLAG_TYPE_DHX : DH_FLAG_TYPE_DH);
49     }
50     return dh;
51 }
52
53 static int i2d_dhp(const EVP_PKEY *pkey, const DH *a, unsigned char **pp)
54 {
55     if (pkey->ameth == &dhx_asn1_meth)
56         return i2d_DHxparams(a, pp);
57     return i2d_DHparams(a, pp);
58 }
59
60 static void int_dh_free(EVP_PKEY *pkey)
61 {
62     DH_free(pkey->pkey.dh);
63 }
64
65 static int dh_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey)
66 {
67     const unsigned char *p, *pm;
68     int pklen, pmlen;
69     int ptype;
70     const void *pval;
71     const ASN1_STRING *pstr;
72     X509_ALGOR *palg;
73     ASN1_INTEGER *public_key = NULL;
74
75     DH *dh = NULL;
76
77     if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
78         return 0;
79     X509_ALGOR_get0(NULL, &ptype, &pval, palg);
80
81     if (ptype != V_ASN1_SEQUENCE) {
82         DHerr(DH_F_DH_PUB_DECODE, DH_R_PARAMETER_ENCODING_ERROR);
83         goto err;
84     }
85
86     pstr = pval;
87     pm = pstr->data;
88     pmlen = pstr->length;
89
90     if ((dh = d2i_dhp(pkey, &pm, pmlen)) == NULL) {
91         DHerr(DH_F_DH_PUB_DECODE, DH_R_DECODE_ERROR);
92         goto err;
93     }
94
95     if ((public_key = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) {
96         DHerr(DH_F_DH_PUB_DECODE, DH_R_DECODE_ERROR);
97         goto err;
98     }
99
100     /* We have parameters now set public key */
101     if ((dh->pub_key = ASN1_INTEGER_to_BN(public_key, NULL)) == NULL) {
102         DHerr(DH_F_DH_PUB_DECODE, DH_R_BN_DECODE_ERROR);
103         goto err;
104     }
105
106     ASN1_INTEGER_free(public_key);
107     EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, dh);
108     return 1;
109
110  err:
111     ASN1_INTEGER_free(public_key);
112     DH_free(dh);
113     return 0;
114 }
115
116 static int dh_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
117 {
118     DH *dh;
119     int ptype;
120     unsigned char *penc = NULL;
121     int penclen;
122     ASN1_STRING *str;
123     ASN1_INTEGER *pub_key = NULL;
124
125     dh = pkey->pkey.dh;
126
127     str = ASN1_STRING_new();
128     if (str == NULL) {
129         DHerr(DH_F_DH_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
130         goto err;
131     }
132     str->length = i2d_dhp(pkey, dh, &str->data);
133     if (str->length <= 0) {
134         DHerr(DH_F_DH_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
135         goto err;
136     }
137     ptype = V_ASN1_SEQUENCE;
138
139     pub_key = BN_to_ASN1_INTEGER(dh->pub_key, NULL);
140     if (pub_key == NULL)
141         goto err;
142
143     penclen = i2d_ASN1_INTEGER(pub_key, &penc);
144
145     ASN1_INTEGER_free(pub_key);
146
147     if (penclen <= 0) {
148         DHerr(DH_F_DH_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
149         goto err;
150     }
151
152     if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(pkey->ameth->pkey_id),
153                                ptype, str, penc, penclen))
154         return 1;
155
156  err:
157     OPENSSL_free(penc);
158     ASN1_STRING_free(str);
159
160     return 0;
161 }
162
163 /*
164  * PKCS#8 DH is defined in PKCS#11 of all places. It is similar to DH in that
165  * the AlgorithmIdentifier contains the parameters, the private key is
166  * explicitly included and the pubkey must be recalculated.
167  */
168
169 static int dh_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
170 {
171     const unsigned char *p, *pm;
172     int pklen, pmlen;
173     int ptype;
174     const void *pval;
175     const ASN1_STRING *pstr;
176     const X509_ALGOR *palg;
177     ASN1_INTEGER *privkey = NULL;
178     DH *dh = NULL;
179
180     if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
181         return 0;
182
183     X509_ALGOR_get0(NULL, &ptype, &pval, palg);
184
185     if (ptype != V_ASN1_SEQUENCE)
186         goto decerr;
187     if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
188         goto decerr;
189
190     pstr = pval;
191     pm = pstr->data;
192     pmlen = pstr->length;
193     if ((dh = d2i_dhp(pkey, &pm, pmlen)) == NULL)
194         goto decerr;
195
196     /* We have parameters now set private key */
197     if ((dh->priv_key = BN_secure_new()) == NULL
198         || !ASN1_INTEGER_to_BN(privkey, dh->priv_key)) {
199         DHerr(DH_F_DH_PRIV_DECODE, DH_R_BN_ERROR);
200         goto dherr;
201     }
202     /* Calculate public key, increments dirty_cnt */
203     if (!DH_generate_key(dh))
204         goto dherr;
205
206     EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, dh);
207
208     ASN1_STRING_clear_free(privkey);
209
210     return 1;
211
212  decerr:
213     DHerr(DH_F_DH_PRIV_DECODE, EVP_R_DECODE_ERROR);
214  dherr:
215     DH_free(dh);
216     ASN1_STRING_clear_free(privkey);
217     return 0;
218 }
219
220 static int dh_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
221 {
222     ASN1_STRING *params = NULL;
223     ASN1_INTEGER *prkey = NULL;
224     unsigned char *dp = NULL;
225     int dplen;
226
227     params = ASN1_STRING_new();
228
229     if (params == NULL) {
230         DHerr(DH_F_DH_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
231         goto err;
232     }
233
234     params->length = i2d_dhp(pkey, pkey->pkey.dh, &params->data);
235     if (params->length <= 0) {
236         DHerr(DH_F_DH_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
237         goto err;
238     }
239     params->type = V_ASN1_SEQUENCE;
240
241     /* Get private key into integer */
242     prkey = BN_to_ASN1_INTEGER(pkey->pkey.dh->priv_key, NULL);
243
244     if (prkey == NULL) {
245         DHerr(DH_F_DH_PRIV_ENCODE, DH_R_BN_ERROR);
246         goto err;
247     }
248
249     dplen = i2d_ASN1_INTEGER(prkey, &dp);
250
251     ASN1_STRING_clear_free(prkey);
252     prkey = NULL;
253
254     if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(pkey->ameth->pkey_id), 0,
255                          V_ASN1_SEQUENCE, params, dp, dplen))
256         goto err;
257
258     return 1;
259
260  err:
261     OPENSSL_free(dp);
262     ASN1_STRING_free(params);
263     ASN1_STRING_clear_free(prkey);
264     return 0;
265 }
266
267 static int dh_param_decode(EVP_PKEY *pkey,
268                            const unsigned char **pder, int derlen)
269 {
270     DH *dh;
271
272     if ((dh = d2i_dhp(pkey, pder, derlen)) == NULL)
273         return 0;
274     dh->dirty_cnt++;
275     EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, dh);
276     return 1;
277 }
278
279 static int dh_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
280 {
281     return i2d_dhp(pkey, pkey->pkey.dh, pder);
282 }
283
284 static int do_dh_print(BIO *bp, const DH *x, int indent, int ptype)
285 {
286     int reason = ERR_R_BUF_LIB;
287     const char *ktype = NULL;
288     BIGNUM *priv_key, *pub_key;
289
290     if (ptype == 2)
291         priv_key = x->priv_key;
292     else
293         priv_key = NULL;
294
295     if (ptype > 0)
296         pub_key = x->pub_key;
297     else
298         pub_key = NULL;
299
300     if (x->params.p == NULL || (ptype == 2 && priv_key == NULL)
301             || (ptype > 0 && pub_key == NULL)) {
302         reason = ERR_R_PASSED_NULL_PARAMETER;
303         goto err;
304     }
305
306     if (ptype == 2)
307         ktype = "DH Private-Key";
308     else if (ptype == 1)
309         ktype = "DH Public-Key";
310     else
311         ktype = "DH Parameters";
312
313     if (!BIO_indent(bp, indent, 128)
314             || BIO_printf(bp, "%s: (%d bit)\n", ktype, DH_bits(x)) <= 0)
315         goto err;
316     indent += 4;
317
318     if (!ASN1_bn_print(bp, "private-key:", priv_key, NULL, indent))
319         goto err;
320     if (!ASN1_bn_print(bp, "public-key:", pub_key, NULL, indent))
321         goto err;
322
323     if (!ossl_ffc_params_print(bp, &x->params, indent))
324         goto err;
325
326     if (x->length != 0) {
327         if (!BIO_indent(bp, indent, 128)
328                 || BIO_printf(bp, "recommended-private-length: %d bits\n",
329                               (int)x->length) <= 0)
330             goto err;
331     }
332
333     return 1;
334
335  err:
336     DHerr(DH_F_DO_DH_PRINT, reason);
337     return 0;
338 }
339
340 static int int_dh_size(const EVP_PKEY *pkey)
341 {
342     return DH_size(pkey->pkey.dh);
343 }
344
345 static int dh_bits(const EVP_PKEY *pkey)
346 {
347     return DH_bits(pkey->pkey.dh);
348 }
349
350 static int dh_security_bits(const EVP_PKEY *pkey)
351 {
352     return DH_security_bits(pkey->pkey.dh);
353 }
354
355 static int dh_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
356 {
357     return ossl_ffc_params_cmp(&a->pkey.dh->params, &a->pkey.dh->params,
358                                a->ameth != &dhx_asn1_meth);
359 }
360
361 static int int_dh_param_copy(DH *to, const DH *from, int is_x942)
362 {
363     if (is_x942 == -1)
364         is_x942 = (from->params.q != NULL);
365     if (!ossl_ffc_params_copy(&to->params, &from->params))
366         return 0;
367     if (!is_x942)
368         to->length = from->length;
369     to->dirty_cnt++;
370     return 1;
371 }
372
373 DH *DHparams_dup(const DH *dh)
374 {
375     DH *ret;
376     ret = DH_new();
377     if (ret == NULL)
378         return NULL;
379     if (!int_dh_param_copy(ret, dh, -1)) {
380         DH_free(ret);
381         return NULL;
382     }
383     return ret;
384 }
385
386 static int dh_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
387 {
388     if (to->pkey.dh == NULL) {
389         to->pkey.dh = DH_new();
390         if (to->pkey.dh == NULL)
391             return 0;
392     }
393     return int_dh_param_copy(to->pkey.dh, from->pkey.dh,
394                              from->ameth == &dhx_asn1_meth);
395 }
396
397 static int dh_missing_parameters(const EVP_PKEY *a)
398 {
399     return a->pkey.dh == NULL
400         || a->pkey.dh->params.p == NULL
401         || a->pkey.dh->params.g == NULL;
402 }
403
404 static int dh_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
405 {
406     if (dh_cmp_parameters(a, b) == 0)
407         return 0;
408     if (BN_cmp(b->pkey.dh->pub_key, a->pkey.dh->pub_key) != 0)
409         return 0;
410     else
411         return 1;
412 }
413
414 static int dh_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
415                           ASN1_PCTX *ctx)
416 {
417     return do_dh_print(bp, pkey->pkey.dh, indent, 0);
418 }
419
420 static int dh_public_print(BIO *bp, const EVP_PKEY *pkey, int indent,
421                            ASN1_PCTX *ctx)
422 {
423     return do_dh_print(bp, pkey->pkey.dh, indent, 1);
424 }
425
426 static int dh_private_print(BIO *bp, const EVP_PKEY *pkey, int indent,
427                             ASN1_PCTX *ctx)
428 {
429     return do_dh_print(bp, pkey->pkey.dh, indent, 2);
430 }
431
432 int DHparams_print(BIO *bp, const DH *x)
433 {
434     return do_dh_print(bp, x, 4, 0);
435 }
436
437 static int dh_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
438 {
439     switch (op) {
440     case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
441         return dh_buf2key(EVP_PKEY_get0_DH(pkey), arg2, arg1);
442     case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
443         return dh_key2buf(EVP_PKEY_get0_DH(pkey), arg2, 0, 1);
444     default:
445         return -2;
446     }
447 }
448
449 static int dhx_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
450 {
451     switch (op) {
452 #ifndef OPENSSL_NO_CMS
453     case ASN1_PKEY_CTRL_CMS_RI_TYPE:
454         *(int *)arg2 = CMS_RECIPINFO_AGREE;
455         return 1;
456 #endif
457     default:
458         return -2;
459     }
460
461 }
462
463 static int dh_pkey_public_check(const EVP_PKEY *pkey)
464 {
465     DH *dh = pkey->pkey.dh;
466
467     if (dh->pub_key == NULL) {
468         DHerr(DH_F_DH_PKEY_PUBLIC_CHECK, DH_R_MISSING_PUBKEY);
469         return 0;
470     }
471
472     return DH_check_pub_key_ex(dh, dh->pub_key);
473 }
474
475 static int dh_pkey_param_check(const EVP_PKEY *pkey)
476 {
477     DH *dh = pkey->pkey.dh;
478
479     return DH_check_ex(dh);
480 }
481
482 static size_t dh_pkey_dirty_cnt(const EVP_PKEY *pkey)
483 {
484     return pkey->pkey.dh->dirty_cnt;
485 }
486
487 static int dh_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
488                              EVP_KEYMGMT *to_keymgmt, OPENSSL_CTX *libctx,
489                              const char *propq)
490 {
491     DH *dh = from->pkey.dh;
492     OSSL_PARAM_BLD *tmpl;
493     const BIGNUM *p = DH_get0_p(dh), *g = DH_get0_g(dh), *q = DH_get0_q(dh);
494     const BIGNUM *pub_key = DH_get0_pub_key(dh);
495     const BIGNUM *priv_key = DH_get0_priv_key(dh);
496     OSSL_PARAM *params = NULL;
497     int selection = 0;
498     int rv = 0;
499
500     /*
501      * If the DH method is foreign, then we can't be sure of anything, and
502      * can therefore not export or pretend to export.
503      */
504     if (dh_get_method(dh) != DH_OpenSSL())
505         return 0;
506
507     if (p == NULL || g == NULL)
508         return 0;
509
510     tmpl = OSSL_PARAM_BLD_new();
511     if (tmpl == NULL)
512         return 0;
513     if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p)
514         || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g))
515         goto err;
516     if (q != NULL) {
517         if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, q))
518             goto err;
519     }
520     selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
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_ffc_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 };