2 * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 * DH low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.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"
30 * i2d/d2i like DH parameter functions which use the appropriate routine for
31 * PKCS#3 DH or X9.42 DH.
34 static DH *d2i_dhp(const EVP_PKEY *pkey, const unsigned char **pp,
38 int is_dhx = (pkey->ameth == &dhx_asn1_meth);
41 dh = d2i_DHxparams(NULL, pp, length);
43 dh = d2i_DHparams(NULL, pp, length);
46 DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
47 DH_set_flags(dh, is_dhx ? DH_FLAG_TYPE_DHX : DH_FLAG_TYPE_DH);
52 static int i2d_dhp(const EVP_PKEY *pkey, const DH *a, unsigned char **pp)
54 if (pkey->ameth == &dhx_asn1_meth)
55 return i2d_DHxparams(a, pp);
56 return i2d_DHparams(a, pp);
59 static void int_dh_free(EVP_PKEY *pkey)
61 DH_free(pkey->pkey.dh);
64 static int dh_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey)
66 const unsigned char *p, *pm;
70 const ASN1_STRING *pstr;
72 ASN1_INTEGER *public_key = NULL;
76 if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
78 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
80 if (ptype != V_ASN1_SEQUENCE) {
81 DHerr(DH_F_DH_PUB_DECODE, DH_R_PARAMETER_ENCODING_ERROR);
89 if ((dh = d2i_dhp(pkey, &pm, pmlen)) == NULL) {
90 DHerr(DH_F_DH_PUB_DECODE, DH_R_DECODE_ERROR);
94 if ((public_key = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) {
95 DHerr(DH_F_DH_PUB_DECODE, DH_R_DECODE_ERROR);
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);
105 ASN1_INTEGER_free(public_key);
106 EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, dh);
110 ASN1_INTEGER_free(public_key);
115 static int dh_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
119 unsigned char *penc = NULL;
122 ASN1_INTEGER *pub_key = NULL;
126 str = ASN1_STRING_new();
128 DHerr(DH_F_DH_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
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);
136 ptype = V_ASN1_SEQUENCE;
138 pub_key = BN_to_ASN1_INTEGER(dh->pub_key, NULL);
142 penclen = i2d_ASN1_INTEGER(pub_key, &penc);
144 ASN1_INTEGER_free(pub_key);
147 DHerr(DH_F_DH_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
151 if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(pkey->ameth->pkey_id),
152 ptype, str, penc, penclen))
157 ASN1_STRING_free(str);
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.
168 static int dh_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
170 const unsigned char *p, *pm;
174 const ASN1_STRING *pstr;
175 const X509_ALGOR *palg;
176 ASN1_INTEGER *privkey = NULL;
179 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
182 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
184 if (ptype != V_ASN1_SEQUENCE)
186 if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
191 pmlen = pstr->length;
192 if ((dh = d2i_dhp(pkey, &pm, pmlen)) == NULL)
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);
201 /* Calculate public key, increments dirty_cnt */
202 if (!DH_generate_key(dh))
205 EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, dh);
207 ASN1_STRING_clear_free(privkey);
212 DHerr(DH_F_DH_PRIV_DECODE, EVP_R_DECODE_ERROR);
215 ASN1_STRING_clear_free(privkey);
219 static int dh_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
221 ASN1_STRING *params = NULL;
222 ASN1_INTEGER *prkey = NULL;
223 unsigned char *dp = NULL;
226 params = ASN1_STRING_new();
228 if (params == NULL) {
229 DHerr(DH_F_DH_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
233 params->length = i2d_dhp(pkey, pkey->pkey.dh, ¶ms->data);
234 if (params->length <= 0) {
235 DHerr(DH_F_DH_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
238 params->type = V_ASN1_SEQUENCE;
240 /* Get private key into integer */
241 prkey = BN_to_ASN1_INTEGER(pkey->pkey.dh->priv_key, NULL);
244 DHerr(DH_F_DH_PRIV_ENCODE, DH_R_BN_ERROR);
248 dplen = i2d_ASN1_INTEGER(prkey, &dp);
250 ASN1_STRING_clear_free(prkey);
253 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(pkey->ameth->pkey_id), 0,
254 V_ASN1_SEQUENCE, params, dp, dplen))
261 ASN1_STRING_free(params);
262 ASN1_STRING_clear_free(prkey);
266 static int dh_param_decode(EVP_PKEY *pkey,
267 const unsigned char **pder, int derlen)
271 if ((dh = d2i_dhp(pkey, pder, derlen)) == NULL)
274 EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, dh);
278 static int dh_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
280 return i2d_dhp(pkey, pkey->pkey.dh, pder);
283 static int do_dh_print(BIO *bp, const DH *x, int indent, int ptype)
285 int reason = ERR_R_BUF_LIB;
286 const char *ktype = NULL;
287 BIGNUM *priv_key, *pub_key;
290 priv_key = x->priv_key;
295 pub_key = x->pub_key;
299 if (x->params.p == NULL || (ptype == 2 && priv_key == NULL)
300 || (ptype > 0 && pub_key == NULL)) {
301 reason = ERR_R_PASSED_NULL_PARAMETER;
306 ktype = "DH Private-Key";
308 ktype = "DH Public-Key";
310 ktype = "DH Parameters";
312 if (!BIO_indent(bp, indent, 128)
313 || BIO_printf(bp, "%s: (%d bit)\n", ktype, DH_bits(x)) <= 0)
317 if (!ASN1_bn_print(bp, "private-key:", priv_key, NULL, indent))
319 if (!ASN1_bn_print(bp, "public-key:", pub_key, NULL, indent))
322 if (!ossl_ffc_params_print(bp, &x->params, indent))
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)
335 DHerr(DH_F_DO_DH_PRINT, reason);
339 static int int_dh_size(const EVP_PKEY *pkey)
341 return DH_size(pkey->pkey.dh);
344 static int dh_bits(const EVP_PKEY *pkey)
346 return DH_bits(pkey->pkey.dh);
349 static int dh_security_bits(const EVP_PKEY *pkey)
351 return DH_security_bits(pkey->pkey.dh);
354 static int dh_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
356 return ossl_ffc_params_cmp(&a->pkey.dh->params, &a->pkey.dh->params,
357 a->ameth != &dhx_asn1_meth);
360 static int int_dh_param_copy(DH *to, const DH *from, int is_x942)
363 is_x942 = (from->params.q != NULL);
364 if (!ossl_ffc_params_copy(&to->params, &from->params))
367 to->length = from->length;
372 DH *DHparams_dup(const DH *dh)
378 if (!int_dh_param_copy(ret, dh, -1)) {
385 static int dh_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
387 if (to->pkey.dh == NULL) {
388 to->pkey.dh = DH_new();
389 if (to->pkey.dh == NULL)
392 return int_dh_param_copy(to->pkey.dh, from->pkey.dh,
393 from->ameth == &dhx_asn1_meth);
396 static int dh_missing_parameters(const EVP_PKEY *a)
398 return a->pkey.dh == NULL
399 || a->pkey.dh->params.p == NULL
400 || a->pkey.dh->params.g == NULL;
403 static int dh_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
405 if (dh_cmp_parameters(a, b) == 0)
407 if (BN_cmp(b->pkey.dh->pub_key, a->pkey.dh->pub_key) != 0)
413 static int dh_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
416 return do_dh_print(bp, pkey->pkey.dh, indent, 0);
419 static int dh_public_print(BIO *bp, const EVP_PKEY *pkey, int indent,
422 return do_dh_print(bp, pkey->pkey.dh, indent, 1);
425 static int dh_private_print(BIO *bp, const EVP_PKEY *pkey, int indent,
428 return do_dh_print(bp, pkey->pkey.dh, indent, 2);
431 int DHparams_print(BIO *bp, const DH *x)
433 return do_dh_print(bp, x, 4, 0);
436 static int dh_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
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);
448 static int dhx_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
457 static int dh_pkey_public_check(const EVP_PKEY *pkey)
459 DH *dh = pkey->pkey.dh;
461 if (dh->pub_key == NULL) {
462 DHerr(DH_F_DH_PKEY_PUBLIC_CHECK, DH_R_MISSING_PUBKEY);
466 return DH_check_pub_key_ex(dh, dh->pub_key);
469 static int dh_pkey_param_check(const EVP_PKEY *pkey)
471 DH *dh = pkey->pkey.dh;
473 return DH_check_ex(dh);
476 static size_t dh_pkey_dirty_cnt(const EVP_PKEY *pkey)
478 return pkey->pkey.dh->dirty_cnt;
481 static int dh_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
482 EVP_KEYMGMT *to_keymgmt, OSSL_LIB_CTX *libctx,
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 const BIGNUM *pub_key = DH_get0_pub_key(dh);
489 const BIGNUM *priv_key = DH_get0_priv_key(dh);
490 OSSL_PARAM *params = NULL;
495 * If the DH method is foreign, then we can't be sure of anything, and
496 * can therefore not export or pretend to export.
498 if (dh_get_method(dh) != DH_OpenSSL())
501 if (p == NULL || g == NULL)
504 tmpl = OSSL_PARAM_BLD_new();
507 if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p)
508 || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g))
511 if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, q))
514 selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
515 if (pub_key != NULL) {
516 if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY, pub_key))
518 selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
520 if (priv_key != NULL) {
521 if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY,
524 selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
527 if ((params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
530 /* We export, the provider imports */
531 rv = evp_keymgmt_import(to_keymgmt, to_keydata, selection, params);
533 OSSL_PARAM_BLD_free_params(params);
535 OSSL_PARAM_BLD_free(tmpl);
539 static int dh_pkey_import_from_type(const OSSL_PARAM params[], void *vpctx,
542 EVP_PKEY_CTX *pctx = vpctx;
543 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
544 DH *dh = dh_new_ex(pctx->libctx);
547 ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
550 DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
551 DH_set_flags(dh, type == EVP_PKEY_DH ? DH_FLAG_TYPE_DH : DH_FLAG_TYPE_DHX);
553 if (!dh_ffc_params_fromdata(dh, params)
554 || !dh_key_fromdata(dh, params)
555 || !EVP_PKEY_assign(pkey, type, dh)) {
562 static int dh_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
564 return dh_pkey_import_from_type(params, vpctx, EVP_PKEY_DH);
567 static int dhx_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
569 return dh_pkey_import_from_type(params, vpctx, EVP_PKEY_DHX);
572 const EVP_PKEY_ASN1_METHOD dh_asn1_meth = {
578 "OpenSSL PKCS#3 DH method",
595 dh_missing_parameters,
607 dh_pkey_public_check,
617 const EVP_PKEY_ASN1_METHOD dhx_asn1_meth = {
623 "OpenSSL X9.42 DH method",
640 dh_missing_parameters,
652 dh_pkey_public_check,
657 dhx_pkey_import_from,