/src/openssl/crypto/dh/dh_meth.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2016-2020 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 | | /* |
11 | | * DH low level APIs are deprecated for public use, but still ok for |
12 | | * internal use. |
13 | | */ |
14 | | #include "internal/deprecated.h" |
15 | | |
16 | | #include "dh_local.h" |
17 | | #include <string.h> |
18 | | #include <openssl/err.h> |
19 | | |
20 | | DH_METHOD *DH_meth_new(const char *name, int flags) |
21 | 0 | { |
22 | 0 | DH_METHOD *dhm = OPENSSL_zalloc(sizeof(*dhm)); |
23 | |
|
24 | 0 | if (dhm != NULL) { |
25 | 0 | dhm->flags = flags; |
26 | |
|
27 | 0 | dhm->name = OPENSSL_strdup(name); |
28 | 0 | if (dhm->name != NULL) |
29 | 0 | return dhm; |
30 | | |
31 | 0 | OPENSSL_free(dhm); |
32 | 0 | } |
33 | | |
34 | 0 | ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE); |
35 | 0 | return NULL; |
36 | 0 | } |
37 | | |
38 | | void DH_meth_free(DH_METHOD *dhm) |
39 | 0 | { |
40 | 0 | if (dhm != NULL) { |
41 | 0 | OPENSSL_free(dhm->name); |
42 | 0 | OPENSSL_free(dhm); |
43 | 0 | } |
44 | 0 | } |
45 | | |
46 | | DH_METHOD *DH_meth_dup(const DH_METHOD *dhm) |
47 | 0 | { |
48 | 0 | DH_METHOD *ret = OPENSSL_malloc(sizeof(*ret)); |
49 | |
|
50 | 0 | if (ret != NULL) { |
51 | 0 | memcpy(ret, dhm, sizeof(*dhm)); |
52 | |
|
53 | 0 | ret->name = OPENSSL_strdup(dhm->name); |
54 | 0 | if (ret->name != NULL) |
55 | 0 | return ret; |
56 | | |
57 | 0 | OPENSSL_free(ret); |
58 | 0 | } |
59 | | |
60 | 0 | ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE); |
61 | 0 | return NULL; |
62 | 0 | } |
63 | | |
64 | | const char *DH_meth_get0_name(const DH_METHOD *dhm) |
65 | 0 | { |
66 | 0 | return dhm->name; |
67 | 0 | } |
68 | | |
69 | | int DH_meth_set1_name(DH_METHOD *dhm, const char *name) |
70 | 0 | { |
71 | 0 | char *tmpname = OPENSSL_strdup(name); |
72 | |
|
73 | 0 | if (tmpname == NULL) { |
74 | 0 | ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE); |
75 | 0 | return 0; |
76 | 0 | } |
77 | | |
78 | 0 | OPENSSL_free(dhm->name); |
79 | 0 | dhm->name = tmpname; |
80 | |
|
81 | 0 | return 1; |
82 | 0 | } |
83 | | |
84 | | int DH_meth_get_flags(const DH_METHOD *dhm) |
85 | 0 | { |
86 | 0 | return dhm->flags; |
87 | 0 | } |
88 | | |
89 | | int DH_meth_set_flags(DH_METHOD *dhm, int flags) |
90 | 0 | { |
91 | 0 | dhm->flags = flags; |
92 | 0 | return 1; |
93 | 0 | } |
94 | | |
95 | | void *DH_meth_get0_app_data(const DH_METHOD *dhm) |
96 | 0 | { |
97 | 0 | return dhm->app_data; |
98 | 0 | } |
99 | | |
100 | | int DH_meth_set0_app_data(DH_METHOD *dhm, void *app_data) |
101 | 0 | { |
102 | 0 | dhm->app_data = app_data; |
103 | 0 | return 1; |
104 | 0 | } |
105 | | |
106 | | int (*DH_meth_get_generate_key(const DH_METHOD *dhm)) (DH *) |
107 | 0 | { |
108 | 0 | return dhm->generate_key; |
109 | 0 | } |
110 | | |
111 | | int DH_meth_set_generate_key(DH_METHOD *dhm, int (*generate_key) (DH *)) |
112 | 0 | { |
113 | 0 | dhm->generate_key = generate_key; |
114 | 0 | return 1; |
115 | 0 | } |
116 | | |
117 | | int (*DH_meth_get_compute_key(const DH_METHOD *dhm)) |
118 | | (unsigned char *key, const BIGNUM *pub_key, DH *dh) |
119 | 0 | { |
120 | 0 | return dhm->compute_key; |
121 | 0 | } |
122 | | |
123 | | int DH_meth_set_compute_key(DH_METHOD *dhm, |
124 | | int (*compute_key) (unsigned char *key, const BIGNUM *pub_key, DH *dh)) |
125 | 0 | { |
126 | 0 | dhm->compute_key = compute_key; |
127 | 0 | return 1; |
128 | 0 | } |
129 | | |
130 | | |
131 | | int (*DH_meth_get_bn_mod_exp(const DH_METHOD *dhm)) |
132 | | (const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *, |
133 | | BN_CTX *, BN_MONT_CTX *) |
134 | 0 | { |
135 | 0 | return dhm->bn_mod_exp; |
136 | 0 | } |
137 | | |
138 | | int DH_meth_set_bn_mod_exp(DH_METHOD *dhm, |
139 | | int (*bn_mod_exp) (const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *, |
140 | | const BIGNUM *, BN_CTX *, BN_MONT_CTX *)) |
141 | 0 | { |
142 | 0 | dhm->bn_mod_exp = bn_mod_exp; |
143 | 0 | return 1; |
144 | 0 | } |
145 | | |
146 | | int (*DH_meth_get_init(const DH_METHOD *dhm))(DH *) |
147 | 0 | { |
148 | 0 | return dhm->init; |
149 | 0 | } |
150 | | |
151 | | int DH_meth_set_init(DH_METHOD *dhm, int (*init)(DH *)) |
152 | 0 | { |
153 | 0 | dhm->init = init; |
154 | 0 | return 1; |
155 | 0 | } |
156 | | |
157 | | int (*DH_meth_get_finish(const DH_METHOD *dhm)) (DH *) |
158 | 0 | { |
159 | 0 | return dhm->finish; |
160 | 0 | } |
161 | | |
162 | | int DH_meth_set_finish(DH_METHOD *dhm, int (*finish) (DH *)) |
163 | 0 | { |
164 | 0 | dhm->finish = finish; |
165 | 0 | return 1; |
166 | 0 | } |
167 | | |
168 | | int (*DH_meth_get_generate_params(const DH_METHOD *dhm)) |
169 | | (DH *, int, int, BN_GENCB *) |
170 | 0 | { |
171 | 0 | return dhm->generate_params; |
172 | 0 | } |
173 | | |
174 | | int DH_meth_set_generate_params(DH_METHOD *dhm, |
175 | | int (*generate_params) (DH *, int, int, BN_GENCB *)) |
176 | 0 | { |
177 | 0 | dhm->generate_params = generate_params; |
178 | 0 | return 1; |
179 | 0 | } |