2 * Copyright 2019 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_numbers.h>
11 #include <openssl/core_names.h>
12 #include <openssl/bn.h>
13 #include <openssl/dh.h>
14 #include <openssl/params.h>
15 #include "internal/provider_algs.h"
17 static OSSL_OP_keymgmt_importkey_fn dh_importkey
;
19 static int params_to_key(DH
*dh
, const OSSL_PARAM params
[])
21 const OSSL_PARAM
*param_p
, *param_g
, *param_priv_key
, *param_pub_key
;
22 BIGNUM
*p
= NULL
, *g
= NULL
, *priv_key
= NULL
, *pub_key
= NULL
;
27 param_p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_FFC_P
);
28 param_g
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_FFC_G
);
30 OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_DH_PRIV_KEY
);
32 OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_DH_PUB_KEY
);
35 * DH documentation says that a public key must be present if a
36 * private key is present.
37 * We want to have at least a public key either way, so we end up
38 * requiring it unconditionally.
40 if (param_pub_key
== NULL
)
43 if ((param_p
!= NULL
&& !OSSL_PARAM_get_BN(param_p
, &p
))
44 || (param_g
!= NULL
&& !OSSL_PARAM_get_BN(param_g
, &g
))
45 || (param_priv_key
!= NULL
46 && !OSSL_PARAM_get_BN(param_priv_key
, &priv_key
))
47 || !OSSL_PARAM_get_BN(param_pub_key
, &pub_key
))
50 if (!DH_set0_pqg(dh
, p
, NULL
, g
))
54 if (!DH_set0_key(dh
, pub_key
, priv_key
))
56 priv_key
= pub_key
= NULL
;
68 static void *dh_importkey(void *provctx
, const OSSL_PARAM params
[])
72 if ((dh
= DH_new()) == NULL
73 || !params_to_key(dh
, params
)) {
80 const OSSL_DISPATCH dh_keymgmt_functions
[] = {
82 * TODO(3.0) When implementing OSSL_FUNC_KEYMGMT_GENKEY, remember to also
83 * implement OSSL_FUNC_KEYMGMT_EXPORTKEY.
85 { OSSL_FUNC_KEYMGMT_IMPORTKEY
, (void (*)(void))dh_importkey
},
86 { OSSL_FUNC_KEYMGMT_FREEKEY
, (void (*)(void))DH_free
},