1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/* We need to use some engine deprecated APIs */
11e1051a39Sopenharmony_ci#define OPENSSL_SUPPRESS_DEPRECATED
12e1051a39Sopenharmony_ci
13e1051a39Sopenharmony_ci#include "eng_local.h"
14e1051a39Sopenharmony_ci
15e1051a39Sopenharmony_cistatic ENGINE_TABLE *dh_table = NULL;
16e1051a39Sopenharmony_cistatic const int dummy_nid = 1;
17e1051a39Sopenharmony_ci
18e1051a39Sopenharmony_civoid ENGINE_unregister_DH(ENGINE *e)
19e1051a39Sopenharmony_ci{
20e1051a39Sopenharmony_ci    engine_table_unregister(&dh_table, e);
21e1051a39Sopenharmony_ci}
22e1051a39Sopenharmony_ci
23e1051a39Sopenharmony_cistatic void engine_unregister_all_DH(void)
24e1051a39Sopenharmony_ci{
25e1051a39Sopenharmony_ci    engine_table_cleanup(&dh_table);
26e1051a39Sopenharmony_ci}
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_ciint ENGINE_register_DH(ENGINE *e)
29e1051a39Sopenharmony_ci{
30e1051a39Sopenharmony_ci    if (e->dh_meth)
31e1051a39Sopenharmony_ci        return engine_table_register(&dh_table,
32e1051a39Sopenharmony_ci                                     engine_unregister_all_DH, e, &dummy_nid,
33e1051a39Sopenharmony_ci                                     1, 0);
34e1051a39Sopenharmony_ci    return 1;
35e1051a39Sopenharmony_ci}
36e1051a39Sopenharmony_ci
37e1051a39Sopenharmony_civoid ENGINE_register_all_DH(void)
38e1051a39Sopenharmony_ci{
39e1051a39Sopenharmony_ci    ENGINE *e;
40e1051a39Sopenharmony_ci
41e1051a39Sopenharmony_ci    for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e))
42e1051a39Sopenharmony_ci        ENGINE_register_DH(e);
43e1051a39Sopenharmony_ci}
44e1051a39Sopenharmony_ci
45e1051a39Sopenharmony_ciint ENGINE_set_default_DH(ENGINE *e)
46e1051a39Sopenharmony_ci{
47e1051a39Sopenharmony_ci    if (e->dh_meth)
48e1051a39Sopenharmony_ci        return engine_table_register(&dh_table,
49e1051a39Sopenharmony_ci                                     engine_unregister_all_DH, e, &dummy_nid,
50e1051a39Sopenharmony_ci                                     1, 1);
51e1051a39Sopenharmony_ci    return 1;
52e1051a39Sopenharmony_ci}
53e1051a39Sopenharmony_ci
54e1051a39Sopenharmony_ci/*
55e1051a39Sopenharmony_ci * Exposed API function to get a functional reference from the implementation
56e1051a39Sopenharmony_ci * table (ie. try to get a functional reference from the tabled structural
57e1051a39Sopenharmony_ci * references).
58e1051a39Sopenharmony_ci */
59e1051a39Sopenharmony_ciENGINE *ENGINE_get_default_DH(void)
60e1051a39Sopenharmony_ci{
61e1051a39Sopenharmony_ci    return ossl_engine_table_select(&dh_table, dummy_nid,
62e1051a39Sopenharmony_ci                                    OPENSSL_FILE, OPENSSL_LINE);
63e1051a39Sopenharmony_ci}
64e1051a39Sopenharmony_ci
65e1051a39Sopenharmony_ci/* Obtains an DH implementation from an ENGINE functional reference */
66e1051a39Sopenharmony_ciconst DH_METHOD *ENGINE_get_DH(const ENGINE *e)
67e1051a39Sopenharmony_ci{
68e1051a39Sopenharmony_ci    return e->dh_meth;
69e1051a39Sopenharmony_ci}
70e1051a39Sopenharmony_ci
71e1051a39Sopenharmony_ci/* Sets an DH implementation in an ENGINE structure */
72e1051a39Sopenharmony_ciint ENGINE_set_DH(ENGINE *e, const DH_METHOD *dh_meth)
73e1051a39Sopenharmony_ci{
74e1051a39Sopenharmony_ci    e->dh_meth = dh_meth;
75e1051a39Sopenharmony_ci    return 1;
76e1051a39Sopenharmony_ci}
77