2 * Copyright 2020-2023 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"
16 #include <openssl/err.h>
17 #include <openssl/core_names.h>
19 # include <openssl/x509.h>
21 #include "internal/param_build_set.h"
22 #include "crypto/dh.h"
26 * The intention with the "backend" source file is to offer backend functions
27 * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
28 * implementations alike.
31 static int dh_ffc_params_fromdata(DH *dh, const OSSL_PARAM params[])
34 FFC_PARAMS *ffc = ossl_dh_get0_params(dh);
36 ret = ossl_ffc_params_fromdata(ffc, params);
38 ossl_dh_cache_named_group(dh); /* This increments dh->dirty_cnt */
42 int ossl_dh_params_fromdata(DH *dh, const OSSL_PARAM params[])
44 const OSSL_PARAM *param_priv_len;
47 if (!dh_ffc_params_fromdata(dh, params))
51 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PRIV_LEN);
52 if (param_priv_len != NULL
53 && (!OSSL_PARAM_get_long(param_priv_len, &priv_len)
54 || !DH_set_length(dh, priv_len)))
60 int ossl_dh_key_fromdata(DH *dh, const OSSL_PARAM params[], int include_private)
62 const OSSL_PARAM *param_priv_key, *param_pub_key;
63 BIGNUM *priv_key = NULL, *pub_key = NULL;
68 param_priv_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
69 param_pub_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
72 && param_priv_key != NULL
73 && !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
76 if (param_pub_key != NULL
77 && !OSSL_PARAM_get_BN(param_pub_key, &pub_key))
80 if (!DH_set0_key(dh, pub_key, priv_key))
86 BN_clear_free(priv_key);
91 int ossl_dh_params_todata(DH *dh, OSSL_PARAM_BLD *bld, OSSL_PARAM params[])
93 long l = DH_get_length(dh);
95 if (!ossl_ffc_params_todata(ossl_dh_get0_params(dh), bld, params))
98 && !ossl_param_build_set_long(bld, params, OSSL_PKEY_PARAM_DH_PRIV_LEN, l))
103 int ossl_dh_key_todata(DH *dh, OSSL_PARAM_BLD *bld, OSSL_PARAM params[],
106 const BIGNUM *priv = NULL, *pub = NULL;
111 DH_get0_key(dh, &pub, &priv);
114 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PRIV_KEY, priv))
117 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PUB_KEY, pub))
123 int ossl_dh_is_foreign(const DH *dh)
126 if (dh->engine != NULL || ossl_dh_get_method(dh) != DH_OpenSSL())
132 static ossl_inline int dh_bn_dup_check(BIGNUM **out, const BIGNUM *f)
134 if (f != NULL && (*out = BN_dup(f)) == NULL)
139 DH *ossl_dh_dup(const DH *dh, int selection)
143 /* Do not try to duplicate foreign DH keys */
144 if (ossl_dh_is_foreign(dh))
147 if ((dupkey = ossl_dh_new_ex(dh->libctx)) == NULL)
150 dupkey->length = DH_get_length(dh);
151 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0
152 && !ossl_ffc_params_copy(&dupkey->params, &dh->params))
155 dupkey->flags = dh->flags;
157 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0
158 && ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0
159 || !dh_bn_dup_check(&dupkey->pub_key, dh->pub_key)))
162 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
163 && ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0
164 || !dh_bn_dup_check(&dupkey->priv_key, dh->priv_key)))
168 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_DH,
169 &dupkey->ex_data, &dh->ex_data))
181 DH *ossl_dh_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
182 OSSL_LIB_CTX *libctx, const char *propq)
184 const unsigned char *p, *pm;
188 const ASN1_STRING *pstr;
189 const X509_ALGOR *palg;
190 BIGNUM *privkey_bn = NULL;
191 ASN1_INTEGER *privkey = NULL;
194 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8inf))
197 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
199 if (ptype != V_ASN1_SEQUENCE)
201 if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
206 pmlen = pstr->length;
207 switch (OBJ_obj2nid(palg->algorithm)) {
208 case NID_dhKeyAgreement:
209 dh = d2i_DHparams(NULL, &pm, pmlen);
211 case NID_dhpublicnumber:
212 dh = d2i_DHxparams(NULL, &pm, pmlen);
220 /* We have parameters now set private key */
221 if ((privkey_bn = BN_secure_new()) == NULL
222 || !ASN1_INTEGER_to_BN(privkey, privkey_bn)) {
223 ERR_raise(ERR_LIB_DH, DH_R_BN_ERROR);
224 BN_clear_free(privkey_bn);
227 if (!DH_set0_key(dh, NULL, privkey_bn))
229 /* Calculate public key, increments dirty_cnt */
230 if (!DH_generate_key(dh))
236 ERR_raise(ERR_LIB_DH, EVP_R_DECODE_ERROR);
241 ASN1_STRING_clear_free(privkey);