2 * Copyright 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
10 #include <openssl/core_names.h>
11 #include "crypto/dh.h"
14 * The intention with the "backend" source file is to offer backend functions
15 * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
16 * implementations alike.
19 int dh_key_fromdata(DH *dh, const OSSL_PARAM params[])
21 const OSSL_PARAM *param_priv_key, *param_pub_key;
22 BIGNUM *priv_key = NULL, *pub_key = NULL;
27 param_priv_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
28 param_pub_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
31 * DH documentation says that a public key must be present if a
32 * private key is present.
33 * We want to have at least a public key either way, so we end up
34 * requiring it unconditionally.
36 if (param_priv_key != NULL && param_pub_key == NULL)
39 if ((param_priv_key != NULL
40 && !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
41 || (param_pub_key != NULL
42 && !OSSL_PARAM_get_BN(param_pub_key, &pub_key)))
45 if (!DH_set0_key(dh, pub_key, priv_key))
51 BN_clear_free(priv_key);