fa8b5b846be050247da3f99606e90b3df5a577fb
[openssl.git] / crypto / engine / tb_dh.c
1 /*
2  * Copyright 2001-2018 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 /* We need to use some engine deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
12
13 #include "eng_local.h"
14
15 static ENGINE_TABLE *dh_table = NULL;
16 static const int dummy_nid = 1;
17
18 void ENGINE_unregister_DH(ENGINE *e)
19 {
20     engine_table_unregister(&dh_table, e);
21 }
22
23 static void engine_unregister_all_DH(void)
24 {
25     engine_table_cleanup(&dh_table);
26 }
27
28 int ENGINE_register_DH(ENGINE *e)
29 {
30     if (e->dh_meth)
31         return engine_table_register(&dh_table,
32                                      engine_unregister_all_DH, e, &dummy_nid,
33                                      1, 0);
34     return 1;
35 }
36
37 void ENGINE_register_all_DH(void)
38 {
39     ENGINE *e;
40
41     for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e))
42         ENGINE_register_DH(e);
43 }
44
45 int ENGINE_set_default_DH(ENGINE *e)
46 {
47     if (e->dh_meth)
48         return engine_table_register(&dh_table,
49                                      engine_unregister_all_DH, e, &dummy_nid,
50                                      1, 1);
51     return 1;
52 }
53
54 /*
55  * Exposed API function to get a functional reference from the implementation
56  * table (ie. try to get a functional reference from the tabled structural
57  * references).
58  */
59 ENGINE *ENGINE_get_default_DH(void)
60 {
61     return engine_table_select(&dh_table, dummy_nid);
62 }
63
64 /* Obtains an DH implementation from an ENGINE functional reference */
65 const DH_METHOD *ENGINE_get_DH(const ENGINE *e)
66 {
67     return e->dh_meth;
68 }
69
70 /* Sets an DH implementation in an ENGINE structure */
71 int ENGINE_set_DH(ENGINE *e, const DH_METHOD *dh_meth)
72 {
73     e->dh_meth = dh_meth;
74     return 1;
75 }