]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/keymgmt/dh_kmgmt.c
Providers: move all ciphers
[thirdparty/openssl.git] / providers / implementations / keymgmt / dh_kmgmt.c
CommitLineData
8b84b075
RL
1/*
2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
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"
16
17static OSSL_OP_keymgmt_importkey_fn dh_importkey;
18
19static int params_to_key(DH *dh, const OSSL_PARAM params[])
20{
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;
23
24 if (dh == NULL)
25 return 0;
26
4889dadc
MC
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);
8b84b075
RL
29 param_priv_key =
30 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PRIV_KEY);
31 param_pub_key =
32 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PUB_KEY);
33
34 /*
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.
39 */
40 if (param_pub_key == NULL)
41 return 0;
42
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))
48 goto err;
49
50 if (!DH_set0_pqg(dh, p, NULL, g))
51 goto err;
52 p = g = NULL;
53
54 if (!DH_set0_key(dh, pub_key, priv_key))
55 goto err;
56 priv_key = pub_key = NULL;
57
58 return 1;
59
60 err:
61 BN_free(p);
62 BN_free(g);
63 BN_free(priv_key);
64 BN_free(pub_key);
65 return 0;
66}
67
68static void *dh_importkey(void *provctx, const OSSL_PARAM params[])
69{
70 DH *dh;
71
72 if ((dh = DH_new()) == NULL
73 || !params_to_key(dh, params)) {
74 DH_free(dh);
75 dh = NULL;
76 }
77 return dh;
78}
79
80const OSSL_DISPATCH dh_keymgmt_functions[] = {
81 /*
82 * TODO(3.0) When implementing OSSL_FUNC_KEYMGMT_GENKEY, remember to also
83 * implement OSSL_FUNC_KEYMGMT_EXPORTKEY.
84 */
85 { OSSL_FUNC_KEYMGMT_IMPORTKEY, (void (*)(void))dh_importkey },
86 { OSSL_FUNC_KEYMGMT_FREEKEY, (void (*)(void))DH_free },
87 { 0, NULL }
88};