2 * Copyright 2020-2022 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[])
38 ffc = ossl_dh_get0_params(dh);
42 ret = ossl_ffc_params_fromdata(ffc, params);
44 ossl_dh_cache_named_group(dh); /* This increments dh->dirty_cnt */
48 int ossl_dh_params_fromdata(DH *dh, const OSSL_PARAM params[])
50 const OSSL_PARAM *param_priv_len;
53 if (!dh_ffc_params_fromdata(dh, params))
57 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PRIV_LEN);
58 if (param_priv_len != NULL
59 && (!OSSL_PARAM_get_long(param_priv_len, &priv_len)
60 || !DH_set_length(dh, priv_len)))
66 int ossl_dh_key_fromdata(DH *dh, const OSSL_PARAM params[], int include_private)
68 const OSSL_PARAM *param_priv_key, *param_pub_key;
69 BIGNUM *priv_key = NULL, *pub_key = NULL;
74 param_priv_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
75 param_pub_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
78 && param_priv_key != NULL
79 && !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
82 if (param_pub_key != NULL
83 && !OSSL_PARAM_get_BN(param_pub_key, &pub_key))
86 if (!DH_set0_key(dh, pub_key, priv_key))
92 BN_clear_free(priv_key);
97 int ossl_dh_params_todata(DH *dh, OSSL_PARAM_BLD *bld, OSSL_PARAM params[])
99 long l = DH_get_length(dh);
101 if (!ossl_ffc_params_todata(ossl_dh_get0_params(dh), bld, params))
104 && !ossl_param_build_set_long(bld, params, OSSL_PKEY_PARAM_DH_PRIV_LEN, l))
109 int ossl_dh_key_todata(DH *dh, OSSL_PARAM_BLD *bld, OSSL_PARAM params[],
112 const BIGNUM *priv = NULL, *pub = NULL;
117 DH_get0_key(dh, &pub, &priv);
120 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PRIV_KEY, priv))
123 && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PUB_KEY, pub))
129 int ossl_dh_is_foreign(const DH *dh)
132 if (dh->engine != NULL || ossl_dh_get_method(dh) != DH_OpenSSL())
138 static ossl_inline int dh_bn_dup_check(BIGNUM **out, const BIGNUM *f)
140 if (f != NULL && (*out = BN_dup(f)) == NULL)
145 DH *ossl_dh_dup(const DH *dh, int selection)
149 /* Do not try to duplicate foreign DH keys */
150 if (ossl_dh_is_foreign(dh))
153 if ((dupkey = ossl_dh_new_ex(dh->libctx)) == NULL)
156 dupkey->length = DH_get_length(dh);
157 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0
158 && !ossl_ffc_params_copy(&dupkey->params, &dh->params))
161 dupkey->flags = dh->flags;
163 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0
164 && ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0
165 || !dh_bn_dup_check(&dupkey->pub_key, dh->pub_key)))
168 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
169 && ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0
170 || !dh_bn_dup_check(&dupkey->priv_key, dh->priv_key)))
174 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_DH,
175 &dupkey->ex_data, &dh->ex_data))
187 DH *ossl_dh_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
188 OSSL_LIB_CTX *libctx, const char *propq)
190 const unsigned char *p, *pm;
194 const ASN1_STRING *pstr;
195 const X509_ALGOR *palg;
196 BIGNUM *privkey_bn = NULL;
197 ASN1_INTEGER *privkey = NULL;
200 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8inf))
203 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
205 if (ptype != V_ASN1_SEQUENCE)
207 if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
212 pmlen = pstr->length;
213 switch (OBJ_obj2nid(palg->algorithm)) {
214 case NID_dhKeyAgreement:
215 dh = d2i_DHparams(NULL, &pm, pmlen);
217 case NID_dhpublicnumber:
218 dh = d2i_DHxparams(NULL, &pm, pmlen);
226 /* We have parameters now set private key */
227 if ((privkey_bn = BN_secure_new()) == NULL
228 || !ASN1_INTEGER_to_BN(privkey, privkey_bn)) {
229 ERR_raise(ERR_LIB_DH, DH_R_BN_ERROR);
230 BN_clear_free(privkey_bn);
233 if (!DH_set0_key(dh, NULL, privkey_bn))
235 /* Calculate public key, increments dirty_cnt */
236 if (!DH_generate_key(dh))
242 ERR_raise(ERR_LIB_DH, EVP_R_DECODE_ERROR);
247 ASN1_STRING_clear_free(privkey);