/src/openssl35/providers/implementations/keymgmt/dh_kmgmt.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2019-2025 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 | | #include "internal/common.h" |
16 | | |
17 | | #include <string.h> /* strcmp */ |
18 | | #include <openssl/core_dispatch.h> |
19 | | #include <openssl/core_names.h> |
20 | | #include <openssl/bn.h> |
21 | | #include <openssl/err.h> |
22 | | #include <openssl/self_test.h> |
23 | | #include "prov/implementations.h" |
24 | | #include "prov/providercommon.h" |
25 | | #include "prov/provider_ctx.h" |
26 | | #include "crypto/dh.h" |
27 | | #include "internal/fips.h" |
28 | | #include "internal/sizes.h" |
29 | | |
30 | | static OSSL_FUNC_keymgmt_new_fn dh_newdata; |
31 | | static OSSL_FUNC_keymgmt_free_fn dh_freedata; |
32 | | static OSSL_FUNC_keymgmt_gen_init_fn dh_gen_init; |
33 | | static OSSL_FUNC_keymgmt_gen_init_fn dhx_gen_init; |
34 | | static OSSL_FUNC_keymgmt_gen_set_template_fn dh_gen_set_template; |
35 | | static OSSL_FUNC_keymgmt_gen_set_params_fn dh_gen_set_params; |
36 | | static OSSL_FUNC_keymgmt_gen_settable_params_fn dh_gen_settable_params; |
37 | | static OSSL_FUNC_keymgmt_gen_fn dh_gen; |
38 | | static OSSL_FUNC_keymgmt_gen_cleanup_fn dh_gen_cleanup; |
39 | | static OSSL_FUNC_keymgmt_load_fn dh_load; |
40 | | static OSSL_FUNC_keymgmt_get_params_fn dh_get_params; |
41 | | static OSSL_FUNC_keymgmt_gettable_params_fn dh_gettable_params; |
42 | | static OSSL_FUNC_keymgmt_set_params_fn dh_set_params; |
43 | | static OSSL_FUNC_keymgmt_settable_params_fn dh_settable_params; |
44 | | static OSSL_FUNC_keymgmt_has_fn dh_has; |
45 | | static OSSL_FUNC_keymgmt_match_fn dh_match; |
46 | | static OSSL_FUNC_keymgmt_validate_fn dh_validate; |
47 | | static OSSL_FUNC_keymgmt_import_fn dh_import; |
48 | | static OSSL_FUNC_keymgmt_import_types_fn dh_import_types; |
49 | | static OSSL_FUNC_keymgmt_export_fn dh_export; |
50 | | static OSSL_FUNC_keymgmt_export_types_fn dh_export_types; |
51 | | static OSSL_FUNC_keymgmt_dup_fn dh_dup; |
52 | | |
53 | | #define DH_POSSIBLE_SELECTIONS \ |
54 | 75.2k | (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) |
55 | | |
56 | | struct dh_gen_ctx { |
57 | | OSSL_LIB_CTX *libctx; |
58 | | |
59 | | FFC_PARAMS *ffc_params; |
60 | | int selection; |
61 | | /* All these parameters are used for parameter generation only */ |
62 | | /* If there is a group name then the remaining parameters are not needed */ |
63 | | int group_nid; |
64 | | size_t pbits; |
65 | | size_t qbits; |
66 | | unsigned char *seed; /* optional FIPS186-4 param for testing */ |
67 | | size_t seedlen; |
68 | | int gindex; /* optional FIPS186-4 generator index (ignored if -1) */ |
69 | | int gen_type; /* see dhtype2id */ |
70 | | int generator; /* Used by DH_PARAMGEN_TYPE_GENERATOR in non fips mode only */ |
71 | | int pcounter; |
72 | | int hindex; |
73 | | int priv_len; |
74 | | |
75 | | char *mdname; |
76 | | char *mdprops; |
77 | | OSSL_CALLBACK *cb; |
78 | | void *cbarg; |
79 | | int dh_type; |
80 | | }; |
81 | | |
82 | | static int dh_gen_type_name2id_w_default(const char *name, int type) |
83 | 0 | { |
84 | 0 | if (strcmp(name, "default") == 0) { |
85 | | #ifdef FIPS_MODULE |
86 | | if (type == DH_FLAG_TYPE_DHX) |
87 | | return DH_PARAMGEN_TYPE_FIPS_186_4; |
88 | | |
89 | | return DH_PARAMGEN_TYPE_GROUP; |
90 | | #else |
91 | 0 | if (type == DH_FLAG_TYPE_DHX) |
92 | 0 | return DH_PARAMGEN_TYPE_FIPS_186_2; |
93 | | |
94 | 0 | return DH_PARAMGEN_TYPE_GENERATOR; |
95 | 0 | #endif |
96 | 0 | } |
97 | | |
98 | 0 | return ossl_dh_gen_type_name2id(name, type); |
99 | 0 | } |
100 | | |
101 | | static void *dh_newdata(void *provctx) |
102 | 9.06k | { |
103 | 9.06k | DH *dh = NULL; |
104 | | |
105 | 9.06k | if (ossl_prov_is_running()) { |
106 | 9.06k | dh = ossl_dh_new_ex(PROV_LIBCTX_OF(provctx)); |
107 | 9.06k | if (dh != NULL) { |
108 | 9.06k | DH_clear_flags(dh, DH_FLAG_TYPE_MASK); |
109 | 9.06k | DH_set_flags(dh, DH_FLAG_TYPE_DH); |
110 | 9.06k | } |
111 | 9.06k | } |
112 | 9.06k | return dh; |
113 | 9.06k | } |
114 | | |
115 | | static void *dhx_newdata(void *provctx) |
116 | 0 | { |
117 | 0 | DH *dh = NULL; |
118 | |
|
119 | 0 | dh = ossl_dh_new_ex(PROV_LIBCTX_OF(provctx)); |
120 | 0 | if (dh != NULL) { |
121 | 0 | DH_clear_flags(dh, DH_FLAG_TYPE_MASK); |
122 | 0 | DH_set_flags(dh, DH_FLAG_TYPE_DHX); |
123 | 0 | } |
124 | 0 | return dh; |
125 | 0 | } |
126 | | |
127 | | static void dh_freedata(void *keydata) |
128 | 60.3k | { |
129 | 60.3k | DH_free(keydata); |
130 | 60.3k | } |
131 | | |
132 | | static int dh_has(const void *keydata, int selection) |
133 | 9.49k | { |
134 | 9.49k | const DH *dh = keydata; |
135 | 9.49k | int ok = 1; |
136 | | |
137 | 9.49k | if (!ossl_prov_is_running() || dh == NULL) |
138 | 0 | return 0; |
139 | 9.49k | if ((selection & DH_POSSIBLE_SELECTIONS) == 0) |
140 | 0 | return 1; /* the selection is not missing */ |
141 | | |
142 | 9.49k | if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) |
143 | 9.27k | ok = ok && (DH_get0_pub_key(dh) != NULL); |
144 | 9.49k | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) |
145 | 91 | ok = ok && (DH_get0_priv_key(dh) != NULL); |
146 | 9.49k | if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) |
147 | 128 | ok = ok && (DH_get0_p(dh) != NULL && DH_get0_g(dh) != NULL); |
148 | 9.49k | return ok; |
149 | 9.49k | } |
150 | | |
151 | | static int dh_match(const void *keydata1, const void *keydata2, int selection) |
152 | 8.98k | { |
153 | 8.98k | const DH *dh1 = keydata1; |
154 | 8.98k | const DH *dh2 = keydata2; |
155 | 8.98k | int ok = 1; |
156 | | |
157 | 8.98k | if (!ossl_prov_is_running()) |
158 | 0 | return 0; |
159 | | |
160 | 8.98k | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { |
161 | 8.98k | int key_checked = 0; |
162 | | |
163 | 8.98k | if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { |
164 | 8.98k | const BIGNUM *pa = DH_get0_pub_key(dh1); |
165 | 8.98k | const BIGNUM *pb = DH_get0_pub_key(dh2); |
166 | | |
167 | 8.98k | if (pa != NULL && pb != NULL) { |
168 | 286 | ok = ok && BN_cmp(pa, pb) == 0; |
169 | 286 | key_checked = 1; |
170 | 286 | } |
171 | 8.98k | } |
172 | 8.98k | if (!key_checked |
173 | 8.98k | && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { |
174 | 8.69k | const BIGNUM *pa = DH_get0_priv_key(dh1); |
175 | 8.69k | const BIGNUM *pb = DH_get0_priv_key(dh2); |
176 | | |
177 | 8.69k | if (pa != NULL && pb != NULL) { |
178 | 0 | ok = ok && BN_cmp(pa, pb) == 0; |
179 | 0 | key_checked = 1; |
180 | 0 | } |
181 | 8.69k | } |
182 | 8.98k | ok = ok && key_checked; |
183 | 8.98k | } |
184 | 8.98k | if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) { |
185 | 8.98k | FFC_PARAMS *dhparams1 = ossl_dh_get0_params((DH *)dh1); |
186 | 8.98k | FFC_PARAMS *dhparams2 = ossl_dh_get0_params((DH *)dh2); |
187 | | |
188 | 8.98k | ok = ok && ossl_ffc_params_cmp(dhparams1, dhparams2, 1); |
189 | 8.98k | } |
190 | 8.98k | return ok; |
191 | 8.98k | } |
192 | | |
193 | | static int dh_import(void *keydata, int selection, const OSSL_PARAM params[]) |
194 | 9.06k | { |
195 | 9.06k | DH *dh = keydata; |
196 | 9.06k | int ok = 1; |
197 | | |
198 | 9.06k | if (!ossl_prov_is_running() || dh == NULL) |
199 | 0 | return 0; |
200 | | |
201 | 9.06k | if ((selection & DH_POSSIBLE_SELECTIONS) == 0) |
202 | 0 | return 0; |
203 | | |
204 | | /* a key without parameters is meaningless */ |
205 | 9.06k | ok = ok && ossl_dh_params_fromdata(dh, params); |
206 | | |
207 | 9.06k | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { |
208 | 9.06k | int include_private = |
209 | 9.06k | selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0; |
210 | | |
211 | 9.06k | ok = ok && ossl_dh_key_fromdata(dh, params, include_private); |
212 | | #ifdef FIPS_MODULE |
213 | | /* |
214 | | * FIPS 140-3 IG 10.3.A additional comment 1 mandates that a pairwise |
215 | | * consistency check be undertaken on key import. The required test |
216 | | * is described in SP 800-56Ar3 5.6.2.1.4. |
217 | | */ |
218 | | if (ok > 0 && !ossl_fips_self_testing()) { |
219 | | ok = ossl_dh_check_pairwise(dh, 1); |
220 | | if (ok <= 0) |
221 | | ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT); |
222 | | } |
223 | | #endif /* FIPS_MODULE */ |
224 | 9.06k | } |
225 | | |
226 | 9.06k | return ok; |
227 | 9.06k | } |
228 | | |
229 | | static int dh_export(void *keydata, int selection, OSSL_CALLBACK *param_cb, |
230 | | void *cbarg) |
231 | 0 | { |
232 | 0 | DH *dh = keydata; |
233 | 0 | OSSL_PARAM_BLD *tmpl = NULL; |
234 | 0 | OSSL_PARAM *params = NULL; |
235 | 0 | int ok = 1; |
236 | |
|
237 | 0 | if (!ossl_prov_is_running() || dh == NULL) |
238 | 0 | return 0; |
239 | | |
240 | 0 | if ((selection & DH_POSSIBLE_SELECTIONS) == 0) |
241 | 0 | return 0; |
242 | | |
243 | 0 | tmpl = OSSL_PARAM_BLD_new(); |
244 | 0 | if (tmpl == NULL) |
245 | 0 | return 0; |
246 | | |
247 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0) |
248 | 0 | ok = ok && ossl_dh_params_todata(dh, tmpl, NULL); |
249 | |
|
250 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { |
251 | 0 | int include_private = |
252 | 0 | selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0; |
253 | |
|
254 | 0 | ok = ok && ossl_dh_key_todata(dh, tmpl, NULL, include_private); |
255 | 0 | } |
256 | |
|
257 | 0 | if (!ok || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) { |
258 | 0 | ok = 0; |
259 | 0 | goto err; |
260 | 0 | } |
261 | | |
262 | 0 | ok = param_cb(params, cbarg); |
263 | 0 | OSSL_PARAM_free(params); |
264 | 0 | err: |
265 | 0 | OSSL_PARAM_BLD_free(tmpl); |
266 | 0 | return ok; |
267 | 0 | } |
268 | | |
269 | | /* IMEXPORT = IMPORT + EXPORT */ |
270 | | |
271 | | # define DH_IMEXPORTABLE_PARAMETERS \ |
272 | | OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_P, NULL, 0), \ |
273 | | OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_Q, NULL, 0), \ |
274 | | OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_G, NULL, 0), \ |
275 | | OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_COFACTOR, NULL, 0), \ |
276 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL), \ |
277 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL), \ |
278 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL), \ |
279 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_PRIV_LEN, NULL), \ |
280 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0), \ |
281 | | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0) |
282 | | # define DH_IMEXPORTABLE_PUBLIC_KEY \ |
283 | | OSSL_PARAM_BN(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0) |
284 | | # define DH_IMEXPORTABLE_PRIVATE_KEY \ |
285 | | OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0) |
286 | | static const OSSL_PARAM dh_all_types[] = { |
287 | | DH_IMEXPORTABLE_PARAMETERS, |
288 | | DH_IMEXPORTABLE_PUBLIC_KEY, |
289 | | DH_IMEXPORTABLE_PRIVATE_KEY, |
290 | | OSSL_PARAM_END |
291 | | }; |
292 | | static const OSSL_PARAM dh_parameter_types[] = { |
293 | | DH_IMEXPORTABLE_PARAMETERS, |
294 | | OSSL_PARAM_END |
295 | | }; |
296 | | static const OSSL_PARAM dh_key_types[] = { |
297 | | DH_IMEXPORTABLE_PUBLIC_KEY, |
298 | | DH_IMEXPORTABLE_PRIVATE_KEY, |
299 | | OSSL_PARAM_END |
300 | | }; |
301 | | static const OSSL_PARAM *dh_types[] = { |
302 | | NULL, /* Index 0 = none of them */ |
303 | | dh_parameter_types, /* Index 1 = parameter types */ |
304 | | dh_key_types, /* Index 2 = key types */ |
305 | | dh_all_types /* Index 3 = 1 + 2 */ |
306 | | }; |
307 | | |
308 | | static const OSSL_PARAM *dh_imexport_types(int selection) |
309 | 0 | { |
310 | 0 | int type_select = 0; |
311 | |
|
312 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0) |
313 | 0 | type_select += 1; |
314 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) |
315 | 0 | type_select += 2; |
316 | 0 | return dh_types[type_select]; |
317 | 0 | } |
318 | | |
319 | | static const OSSL_PARAM *dh_import_types(int selection) |
320 | 0 | { |
321 | 0 | return dh_imexport_types(selection); |
322 | 0 | } |
323 | | |
324 | | static const OSSL_PARAM *dh_export_types(int selection) |
325 | 0 | { |
326 | 0 | return dh_imexport_types(selection); |
327 | 0 | } |
328 | | |
329 | | static ossl_inline int dh_get_params(void *key, OSSL_PARAM params[]) |
330 | 57.7k | { |
331 | 57.7k | DH *dh = key; |
332 | 57.7k | OSSL_PARAM *p; |
333 | | |
334 | 57.7k | if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL |
335 | 57.7k | && !OSSL_PARAM_set_int(p, DH_bits(dh))) |
336 | 0 | return 0; |
337 | 57.7k | if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL |
338 | 57.7k | && !OSSL_PARAM_set_int(p, DH_security_bits(dh))) |
339 | 0 | return 0; |
340 | 57.7k | if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL |
341 | 57.7k | && !OSSL_PARAM_set_int(p, DH_size(dh))) |
342 | 0 | return 0; |
343 | 57.7k | if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY)) != NULL) { |
344 | 7.72k | if (p->data_type != OSSL_PARAM_OCTET_STRING) |
345 | 0 | return 0; |
346 | 7.72k | p->return_size = ossl_dh_key2buf(dh, (unsigned char **)&p->data, |
347 | 7.72k | p->data_size, 0); |
348 | 7.72k | if (p->return_size == 0) |
349 | 0 | return 0; |
350 | 7.72k | } |
351 | | |
352 | 57.7k | return ossl_dh_params_todata(dh, NULL, params) |
353 | 57.7k | && ossl_dh_key_todata(dh, NULL, params, 1); |
354 | 57.7k | } |
355 | | |
356 | | static const OSSL_PARAM dh_params[] = { |
357 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL), |
358 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL), |
359 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL), |
360 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0), |
361 | | DH_IMEXPORTABLE_PARAMETERS, |
362 | | DH_IMEXPORTABLE_PUBLIC_KEY, |
363 | | DH_IMEXPORTABLE_PRIVATE_KEY, |
364 | | OSSL_PARAM_END |
365 | | }; |
366 | | |
367 | | static const OSSL_PARAM *dh_gettable_params(void *provctx) |
368 | 0 | { |
369 | 0 | return dh_params; |
370 | 0 | } |
371 | | |
372 | | static const OSSL_PARAM dh_known_settable_params[] = { |
373 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0), |
374 | | OSSL_PARAM_END |
375 | | }; |
376 | | |
377 | | static const OSSL_PARAM *dh_settable_params(void *provctx) |
378 | 0 | { |
379 | 0 | return dh_known_settable_params; |
380 | 0 | } |
381 | | |
382 | | static int dh_set_params(void *key, const OSSL_PARAM params[]) |
383 | 1.72k | { |
384 | 1.72k | DH *dh = key; |
385 | 1.72k | const OSSL_PARAM *p; |
386 | | |
387 | 1.72k | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY); |
388 | 1.72k | if (p != NULL |
389 | 1.72k | && (p->data_type != OSSL_PARAM_OCTET_STRING |
390 | 1.72k | || !ossl_dh_buf2key(dh, p->data, p->data_size))) |
391 | 39 | return 0; |
392 | | |
393 | 1.68k | return 1; |
394 | 1.72k | } |
395 | | |
396 | | static int dh_validate_public(const DH *dh, int checktype) |
397 | 16.6k | { |
398 | 16.6k | const BIGNUM *pub_key = NULL; |
399 | 16.6k | int res = 0; |
400 | | |
401 | 16.6k | DH_get0_key(dh, &pub_key, NULL); |
402 | 16.6k | if (pub_key == NULL) |
403 | 10.3k | return 0; |
404 | | |
405 | | /* |
406 | | * The partial test is only valid for named group's with q = (p - 1) / 2 |
407 | | * but for that case it is also fully sufficient to check the key validity. |
408 | | */ |
409 | 6.23k | if (ossl_dh_is_named_safe_prime_group(dh)) |
410 | 320 | return ossl_dh_check_pub_key_partial(dh, pub_key, &res); |
411 | | |
412 | 5.91k | return DH_check_pub_key_ex(dh, pub_key); |
413 | 6.23k | } |
414 | | |
415 | | static int dh_validate_private(const DH *dh) |
416 | 8.98k | { |
417 | 8.98k | int status = 0; |
418 | 8.98k | const BIGNUM *priv_key = NULL; |
419 | | |
420 | 8.98k | DH_get0_key(dh, NULL, &priv_key); |
421 | 8.98k | if (priv_key == NULL) |
422 | 8.93k | return 0; |
423 | 42 | return ossl_dh_check_priv_key(dh, priv_key, &status); |
424 | 8.98k | } |
425 | | |
426 | | static int dh_validate(const void *keydata, int selection, int checktype) |
427 | 56.6k | { |
428 | 56.6k | const DH *dh = keydata; |
429 | 56.6k | int ok = 1; |
430 | | |
431 | 56.6k | if (!ossl_prov_is_running()) |
432 | 0 | return 0; |
433 | | |
434 | 56.6k | if ((selection & DH_POSSIBLE_SELECTIONS) == 0) |
435 | 0 | return 1; /* nothing to validate */ |
436 | | |
437 | 56.6k | if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) { |
438 | | /* |
439 | | * Both of these functions check parameters. DH_check_params_ex() |
440 | | * performs a lightweight check (e.g. it does not check that p is a |
441 | | * safe prime) |
442 | | */ |
443 | 17.9k | if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK) |
444 | 9.06k | ok = ok && DH_check_params_ex(dh); |
445 | 8.89k | else |
446 | 8.89k | ok = ok && DH_check_ex(dh); |
447 | 17.9k | } |
448 | | |
449 | 56.6k | if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) |
450 | 29.7k | ok = ok && dh_validate_public(dh, checktype); |
451 | | |
452 | 56.6k | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) |
453 | 17.9k | ok = ok && dh_validate_private(dh); |
454 | | |
455 | 56.6k | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) |
456 | 56.6k | == OSSL_KEYMGMT_SELECT_KEYPAIR) |
457 | 8.95k | ok = ok && ossl_dh_check_pairwise(dh, 0); |
458 | 56.6k | return ok; |
459 | 56.6k | } |
460 | | |
461 | | static void *dh_gen_init_base(void *provctx, int selection, |
462 | | const OSSL_PARAM params[], int type) |
463 | 6.89k | { |
464 | 6.89k | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx); |
465 | 6.89k | struct dh_gen_ctx *gctx = NULL; |
466 | | |
467 | 6.89k | if (!ossl_prov_is_running()) |
468 | 0 | return NULL; |
469 | | |
470 | 6.89k | if ((selection & (OSSL_KEYMGMT_SELECT_KEYPAIR |
471 | 6.89k | | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)) == 0) |
472 | 0 | return NULL; |
473 | | |
474 | 6.89k | if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) { |
475 | 6.89k | gctx->selection = selection; |
476 | 6.89k | gctx->libctx = libctx; |
477 | 6.89k | gctx->pbits = 2048; |
478 | 6.89k | gctx->qbits = 224; |
479 | 6.89k | gctx->mdname = NULL; |
480 | | #ifdef FIPS_MODULE |
481 | | gctx->gen_type = (type == DH_FLAG_TYPE_DHX) |
482 | | ? DH_PARAMGEN_TYPE_FIPS_186_4 |
483 | | : DH_PARAMGEN_TYPE_GROUP; |
484 | | #else |
485 | 6.89k | gctx->gen_type = (type == DH_FLAG_TYPE_DHX) |
486 | 6.89k | ? DH_PARAMGEN_TYPE_FIPS_186_2 |
487 | 6.89k | : DH_PARAMGEN_TYPE_GENERATOR; |
488 | 6.89k | #endif |
489 | 6.89k | gctx->gindex = -1; |
490 | 6.89k | gctx->hindex = 0; |
491 | 6.89k | gctx->pcounter = -1; |
492 | 6.89k | gctx->generator = DH_GENERATOR_2; |
493 | 6.89k | gctx->dh_type = type; |
494 | 6.89k | } |
495 | 6.89k | if (!dh_gen_set_params(gctx, params)) { |
496 | 0 | OPENSSL_free(gctx); |
497 | 0 | gctx = NULL; |
498 | 0 | } |
499 | 6.89k | return gctx; |
500 | 6.89k | } |
501 | | |
502 | | static void *dh_gen_init(void *provctx, int selection, |
503 | | const OSSL_PARAM params[]) |
504 | 6.89k | { |
505 | 6.89k | return dh_gen_init_base(provctx, selection, params, DH_FLAG_TYPE_DH); |
506 | 6.89k | } |
507 | | |
508 | | static void *dhx_gen_init(void *provctx, int selection, |
509 | | const OSSL_PARAM params[]) |
510 | 0 | { |
511 | 0 | return dh_gen_init_base(provctx, selection, params, DH_FLAG_TYPE_DHX); |
512 | 0 | } |
513 | | |
514 | | static int dh_gen_set_template(void *genctx, void *templ) |
515 | 5.02k | { |
516 | 5.02k | struct dh_gen_ctx *gctx = genctx; |
517 | 5.02k | DH *dh = templ; |
518 | | |
519 | 5.02k | if (!ossl_prov_is_running() || gctx == NULL || dh == NULL) |
520 | 0 | return 0; |
521 | 5.02k | gctx->ffc_params = ossl_dh_get0_params(dh); |
522 | 5.02k | return 1; |
523 | 5.02k | } |
524 | | |
525 | | static int dh_set_gen_seed(struct dh_gen_ctx *gctx, unsigned char *seed, |
526 | | size_t seedlen) |
527 | 0 | { |
528 | 0 | OPENSSL_clear_free(gctx->seed, gctx->seedlen); |
529 | 0 | gctx->seed = NULL; |
530 | 0 | gctx->seedlen = 0; |
531 | 0 | if (seed != NULL && seedlen > 0) { |
532 | 0 | gctx->seed = OPENSSL_memdup(seed, seedlen); |
533 | 0 | if (gctx->seed == NULL) |
534 | 0 | return 0; |
535 | 0 | gctx->seedlen = seedlen; |
536 | 0 | } |
537 | 0 | return 1; |
538 | 0 | } |
539 | | |
540 | | static int dh_gen_common_set_params(void *genctx, const OSSL_PARAM params[]) |
541 | 2.49k | { |
542 | 2.49k | struct dh_gen_ctx *gctx = genctx; |
543 | 2.49k | const OSSL_PARAM *p; |
544 | 2.49k | int gen_type = -1; |
545 | | |
546 | 2.49k | if (gctx == NULL) |
547 | 0 | return 0; |
548 | 2.49k | if (ossl_param_is_empty(params)) |
549 | 1.97k | return 1; |
550 | | |
551 | 513 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_TYPE); |
552 | 513 | if (p != NULL) { |
553 | 0 | if (p->data_type != OSSL_PARAM_UTF8_STRING |
554 | 0 | || ((gen_type = |
555 | 0 | dh_gen_type_name2id_w_default(p->data, gctx->dh_type)) == -1)) { |
556 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); |
557 | 0 | return 0; |
558 | 0 | } |
559 | 0 | if (gen_type != -1) |
560 | 0 | gctx->gen_type = gen_type; |
561 | 0 | } |
562 | 513 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME); |
563 | 513 | if (p != NULL) { |
564 | 513 | const DH_NAMED_GROUP *group = NULL; |
565 | | |
566 | 513 | if (p->data_type != OSSL_PARAM_UTF8_STRING |
567 | 513 | || p->data == NULL |
568 | 513 | || (group = ossl_ffc_name_to_dh_named_group(p->data)) == NULL |
569 | 513 | || ((gctx->group_nid = |
570 | 513 | ossl_ffc_named_group_get_uid(group)) == NID_undef)) { |
571 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); |
572 | 0 | return 0; |
573 | 0 | } |
574 | 513 | } |
575 | 513 | if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PBITS)) != NULL |
576 | 513 | && !OSSL_PARAM_get_size_t(p, &gctx->pbits)) |
577 | 0 | return 0; |
578 | 513 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PRIV_LEN); |
579 | 513 | if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->priv_len)) |
580 | 0 | return 0; |
581 | 513 | return 1; |
582 | 513 | } |
583 | | |
584 | | static const OSSL_PARAM *dh_gen_settable_params(ossl_unused void *genctx, |
585 | | ossl_unused void *provctx) |
586 | 0 | { |
587 | 0 | static const OSSL_PARAM dh_gen_settable[] = { |
588 | 0 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE, NULL, 0), |
589 | 0 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0), |
590 | 0 | OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_PRIV_LEN, NULL), |
591 | 0 | OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_PBITS, NULL), |
592 | 0 | OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_GENERATOR, NULL), |
593 | 0 | OSSL_PARAM_END |
594 | 0 | }; |
595 | 0 | return dh_gen_settable; |
596 | 0 | } |
597 | | |
598 | | static const OSSL_PARAM *dhx_gen_settable_params(ossl_unused void *genctx, |
599 | | ossl_unused void *provctx) |
600 | 0 | { |
601 | 0 | static const OSSL_PARAM dhx_gen_settable[] = { |
602 | 0 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE, NULL, 0), |
603 | 0 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0), |
604 | 0 | OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_PRIV_LEN, NULL), |
605 | 0 | OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_PBITS, NULL), |
606 | 0 | OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_QBITS, NULL), |
607 | 0 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST, NULL, 0), |
608 | 0 | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS, NULL, 0), |
609 | 0 | OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL), |
610 | 0 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0), |
611 | 0 | OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL), |
612 | 0 | OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL), |
613 | 0 | OSSL_PARAM_END |
614 | 0 | }; |
615 | 0 | return dhx_gen_settable; |
616 | 0 | } |
617 | | |
618 | | static int dhx_gen_set_params(void *genctx, const OSSL_PARAM params[]) |
619 | 0 | { |
620 | 0 | struct dh_gen_ctx *gctx = genctx; |
621 | 0 | const OSSL_PARAM *p; |
622 | |
|
623 | 0 | if (!dh_gen_common_set_params(genctx, params)) |
624 | 0 | return 0; |
625 | | |
626 | | /* Parameters related to fips186-4 and fips186-2 */ |
627 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX); |
628 | 0 | if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->gindex)) |
629 | 0 | return 0; |
630 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER); |
631 | 0 | if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->pcounter)) |
632 | 0 | return 0; |
633 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H); |
634 | 0 | if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->hindex)) |
635 | 0 | return 0; |
636 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED); |
637 | 0 | if (p != NULL |
638 | 0 | && (p->data_type != OSSL_PARAM_OCTET_STRING |
639 | 0 | || !dh_set_gen_seed(gctx, p->data, p->data_size))) |
640 | 0 | return 0; |
641 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_QBITS)) != NULL |
642 | 0 | && !OSSL_PARAM_get_size_t(p, &gctx->qbits)) |
643 | 0 | return 0; |
644 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST); |
645 | 0 | if (p != NULL) { |
646 | 0 | if (p->data_type != OSSL_PARAM_UTF8_STRING) |
647 | 0 | return 0; |
648 | 0 | OPENSSL_free(gctx->mdname); |
649 | 0 | gctx->mdname = OPENSSL_strdup(p->data); |
650 | 0 | if (gctx->mdname == NULL) |
651 | 0 | return 0; |
652 | 0 | } |
653 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST_PROPS); |
654 | 0 | if (p != NULL) { |
655 | 0 | if (p->data_type != OSSL_PARAM_UTF8_STRING) |
656 | 0 | return 0; |
657 | 0 | OPENSSL_free(gctx->mdprops); |
658 | 0 | gctx->mdprops = OPENSSL_strdup(p->data); |
659 | 0 | if (gctx->mdprops == NULL) |
660 | 0 | return 0; |
661 | 0 | } |
662 | | |
663 | | /* Parameters that are not allowed for DHX */ |
664 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_GENERATOR); |
665 | 0 | if (p != NULL) { |
666 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_UNSUPPORTED); |
667 | 0 | return 0; |
668 | 0 | } |
669 | 0 | return 1; |
670 | 0 | } |
671 | | |
672 | | static int dh_gen_set_params(void *genctx, const OSSL_PARAM params[]) |
673 | 8.76k | { |
674 | 8.76k | struct dh_gen_ctx *gctx = genctx; |
675 | 8.76k | const OSSL_PARAM *p; |
676 | | |
677 | 8.76k | if (!dh_gen_common_set_params(genctx, params)) |
678 | 0 | return 0; |
679 | | |
680 | 8.76k | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_GENERATOR); |
681 | 8.76k | if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->generator)) |
682 | 0 | return 0; |
683 | | |
684 | | /* Parameters that are not allowed for DH */ |
685 | 8.76k | if (OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX) != NULL |
686 | 8.76k | || OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER) != NULL |
687 | 8.76k | || OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H) != NULL |
688 | 8.76k | || OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED) != NULL |
689 | 8.76k | || OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_QBITS) != NULL |
690 | 8.76k | || OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST) != NULL |
691 | 8.76k | || OSSL_PARAM_locate_const(params, |
692 | 8.76k | OSSL_PKEY_PARAM_FFC_DIGEST_PROPS) != NULL) { |
693 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); |
694 | 0 | return 0; |
695 | 0 | } |
696 | 8.76k | return 1; |
697 | 8.76k | } |
698 | | |
699 | | static int dh_gencb(int p, int n, BN_GENCB *cb) |
700 | 0 | { |
701 | 0 | struct dh_gen_ctx *gctx = BN_GENCB_get_arg(cb); |
702 | 0 | OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END }; |
703 | |
|
704 | 0 | params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p); |
705 | 0 | params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n); |
706 | |
|
707 | 0 | return gctx->cb(params, gctx->cbarg); |
708 | 0 | } |
709 | | |
710 | | static void *dh_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) |
711 | 4.54k | { |
712 | 4.54k | int ret = 0; |
713 | 4.54k | struct dh_gen_ctx *gctx = genctx; |
714 | 4.54k | DH *dh = NULL; |
715 | 4.54k | BN_GENCB *gencb = NULL; |
716 | 4.54k | FFC_PARAMS *ffc; |
717 | | |
718 | 4.54k | if (!ossl_prov_is_running() || gctx == NULL) |
719 | 0 | return NULL; |
720 | | |
721 | | /* |
722 | | * If a group name is selected then the type is group regardless of what |
723 | | * the user selected. This overrides rather than errors for backwards |
724 | | * compatibility. |
725 | | */ |
726 | 4.54k | if (gctx->group_nid != NID_undef) |
727 | 1.28k | gctx->gen_type = DH_PARAMGEN_TYPE_GROUP; |
728 | | |
729 | | /* |
730 | | * Do a bounds check on context gen_type. Must be in range: |
731 | | * DH_PARAMGEN_TYPE_GENERATOR <= gen_type <= DH_PARAMGEN_TYPE_GROUP |
732 | | * Noted here as this needs to be adjusted if a new group type is |
733 | | * added. |
734 | | */ |
735 | 4.54k | if (!ossl_assert((gctx->gen_type >= DH_PARAMGEN_TYPE_GENERATOR) |
736 | 4.54k | && (gctx->gen_type <= DH_PARAMGEN_TYPE_GROUP))) { |
737 | 0 | ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR, |
738 | 0 | "gen_type set to unsupported value %d", gctx->gen_type); |
739 | 0 | return NULL; |
740 | 0 | } |
741 | | |
742 | | /* For parameter generation - If there is a group name just create it */ |
743 | 4.54k | if (gctx->gen_type == DH_PARAMGEN_TYPE_GROUP |
744 | 4.54k | && gctx->ffc_params == NULL) { |
745 | | /* Select a named group if there is not one already */ |
746 | 1.28k | if (gctx->group_nid == NID_undef) |
747 | 0 | gctx->group_nid = ossl_dh_get_named_group_uid_from_size(gctx->pbits); |
748 | 1.28k | if (gctx->group_nid == NID_undef) |
749 | 0 | return NULL; |
750 | 1.28k | dh = ossl_dh_new_by_nid_ex(gctx->libctx, gctx->group_nid); |
751 | 1.28k | if (dh == NULL) |
752 | 0 | return NULL; |
753 | 1.28k | ffc = ossl_dh_get0_params(dh); |
754 | 3.26k | } else { |
755 | 3.26k | dh = ossl_dh_new_ex(gctx->libctx); |
756 | 3.26k | if (dh == NULL) |
757 | 0 | return NULL; |
758 | 3.26k | ffc = ossl_dh_get0_params(dh); |
759 | | |
760 | | /* Copy the template value if one was passed */ |
761 | 3.26k | if (gctx->ffc_params != NULL |
762 | 3.26k | && !ossl_ffc_params_copy(ffc, gctx->ffc_params)) |
763 | 0 | goto end; |
764 | | |
765 | 3.26k | if (!ossl_ffc_params_set_seed(ffc, gctx->seed, gctx->seedlen)) |
766 | 0 | goto end; |
767 | 3.26k | if (gctx->gindex != -1) { |
768 | 0 | ossl_ffc_params_set_gindex(ffc, gctx->gindex); |
769 | 0 | if (gctx->pcounter != -1) |
770 | 0 | ossl_ffc_params_set_pcounter(ffc, gctx->pcounter); |
771 | 3.26k | } else if (gctx->hindex != 0) { |
772 | 0 | ossl_ffc_params_set_h(ffc, gctx->hindex); |
773 | 0 | } |
774 | 3.26k | if (gctx->mdname != NULL) |
775 | 0 | ossl_ffc_set_digest(ffc, gctx->mdname, gctx->mdprops); |
776 | 3.26k | gctx->cb = osslcb; |
777 | 3.26k | gctx->cbarg = cbarg; |
778 | 3.26k | gencb = BN_GENCB_new(); |
779 | 3.26k | if (gencb != NULL) |
780 | 3.26k | BN_GENCB_set(gencb, dh_gencb, genctx); |
781 | | |
782 | 3.26k | if ((gctx->selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) { |
783 | | /* |
784 | | * NOTE: The old safe prime generator code is not used in fips mode, |
785 | | * (i.e internally it ignores the generator and chooses a named |
786 | | * group based on pbits. |
787 | | */ |
788 | 0 | if (gctx->gen_type == DH_PARAMGEN_TYPE_GENERATOR) |
789 | 0 | ret = DH_generate_parameters_ex(dh, gctx->pbits, |
790 | 0 | gctx->generator, gencb); |
791 | 0 | else |
792 | 0 | ret = ossl_dh_generate_ffc_parameters(dh, gctx->gen_type, |
793 | 0 | gctx->pbits, gctx->qbits, |
794 | 0 | gencb); |
795 | 0 | if (ret <= 0) |
796 | 0 | goto end; |
797 | 0 | } |
798 | 3.26k | } |
799 | | |
800 | 4.54k | if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { |
801 | 3.30k | if (ffc->p == NULL || ffc->g == NULL) |
802 | 0 | goto end; |
803 | 3.30k | if (gctx->priv_len > 0) |
804 | 0 | DH_set_length(dh, (long)gctx->priv_len); |
805 | 3.30k | ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_LEGACY, |
806 | 3.30k | gctx->gen_type == DH_PARAMGEN_TYPE_FIPS_186_2); |
807 | 3.30k | if (DH_generate_key(dh) <= 0) |
808 | 0 | goto end; |
809 | | #ifdef FIPS_MODULE |
810 | | if (!ossl_fips_self_testing()) { |
811 | | ret = ossl_dh_check_pairwise(dh, 0); |
812 | | if (ret <= 0) { |
813 | | ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT); |
814 | | goto end; |
815 | | } |
816 | | } |
817 | | #endif /* FIPS_MODULE */ |
818 | 3.30k | } |
819 | 4.54k | DH_clear_flags(dh, DH_FLAG_TYPE_MASK); |
820 | 4.54k | DH_set_flags(dh, gctx->dh_type); |
821 | | |
822 | 4.54k | ret = 1; |
823 | 4.54k | end: |
824 | 4.54k | if (ret <= 0) { |
825 | 0 | DH_free(dh); |
826 | 0 | dh = NULL; |
827 | 0 | } |
828 | 4.54k | BN_GENCB_free(gencb); |
829 | 4.54k | return dh; |
830 | 4.54k | } |
831 | | |
832 | | static void dh_gen_cleanup(void *genctx) |
833 | 6.89k | { |
834 | 6.89k | struct dh_gen_ctx *gctx = genctx; |
835 | | |
836 | 6.89k | if (gctx == NULL) |
837 | 0 | return; |
838 | | |
839 | 6.89k | OPENSSL_free(gctx->mdname); |
840 | 6.89k | OPENSSL_free(gctx->mdprops); |
841 | 6.89k | OPENSSL_clear_free(gctx->seed, gctx->seedlen); |
842 | 6.89k | OPENSSL_free(gctx); |
843 | 6.89k | } |
844 | | |
845 | | static void *dh_load(const void *reference, size_t reference_sz) |
846 | 35.4k | { |
847 | 35.4k | DH *dh = NULL; |
848 | | |
849 | 35.4k | if (ossl_prov_is_running() && reference_sz == sizeof(dh)) { |
850 | | /* The contents of the reference is the address to our object */ |
851 | 35.4k | dh = *(DH **)reference; |
852 | | /* We grabbed, so we detach it */ |
853 | 35.4k | *(DH **)reference = NULL; |
854 | 35.4k | return dh; |
855 | 35.4k | } |
856 | 0 | return NULL; |
857 | 35.4k | } |
858 | | |
859 | | static void *dh_dup(const void *keydata_from, int selection) |
860 | 8.98k | { |
861 | 8.98k | if (ossl_prov_is_running()) |
862 | 8.98k | return ossl_dh_dup(keydata_from, selection); |
863 | 0 | return NULL; |
864 | 8.98k | } |
865 | | |
866 | | const OSSL_DISPATCH ossl_dh_keymgmt_functions[] = { |
867 | | { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dh_newdata }, |
868 | | { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))dh_gen_init }, |
869 | | { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, (void (*)(void))dh_gen_set_template }, |
870 | | { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))dh_gen_set_params }, |
871 | | { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, |
872 | | (void (*)(void))dh_gen_settable_params }, |
873 | | { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))dh_gen }, |
874 | | { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))dh_gen_cleanup }, |
875 | | { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))dh_load }, |
876 | | { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dh_freedata }, |
877 | | { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dh_get_params }, |
878 | | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))dh_gettable_params }, |
879 | | { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))dh_set_params }, |
880 | | { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))dh_settable_params }, |
881 | | { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dh_has }, |
882 | | { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))dh_match }, |
883 | | { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))dh_validate }, |
884 | | { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dh_import }, |
885 | | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dh_import_types }, |
886 | | { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dh_export }, |
887 | | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dh_export_types }, |
888 | | { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))dh_dup }, |
889 | | OSSL_DISPATCH_END |
890 | | }; |
891 | | |
892 | | /* For any DH key, we use the "DH" algorithms regardless of sub-type. */ |
893 | | static const char *dhx_query_operation_name(int operation_id) |
894 | 0 | { |
895 | 0 | return "DH"; |
896 | 0 | } |
897 | | |
898 | | const OSSL_DISPATCH ossl_dhx_keymgmt_functions[] = { |
899 | | { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dhx_newdata }, |
900 | | { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))dhx_gen_init }, |
901 | | { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, (void (*)(void))dh_gen_set_template }, |
902 | | { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))dhx_gen_set_params }, |
903 | | { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, |
904 | | (void (*)(void))dhx_gen_settable_params }, |
905 | | { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))dh_gen }, |
906 | | { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))dh_gen_cleanup }, |
907 | | { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))dh_load }, |
908 | | { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dh_freedata }, |
909 | | { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dh_get_params }, |
910 | | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))dh_gettable_params }, |
911 | | { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))dh_set_params }, |
912 | | { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))dh_settable_params }, |
913 | | { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dh_has }, |
914 | | { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))dh_match }, |
915 | | { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))dh_validate }, |
916 | | { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dh_import }, |
917 | | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dh_import_types }, |
918 | | { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dh_export }, |
919 | | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dh_export_types }, |
920 | | { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME, |
921 | | (void (*)(void))dhx_query_operation_name }, |
922 | | { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))dh_dup }, |
923 | | OSSL_DISPATCH_END |
924 | | }; |