/src/openssl35/providers/implementations/exchange/dh_exch.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2019-2026 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 <string.h> |
17 | | #include <openssl/crypto.h> |
18 | | #include <openssl/core_dispatch.h> |
19 | | #include <openssl/core_names.h> |
20 | | #include <openssl/dh.h> |
21 | | #include <openssl/err.h> |
22 | | #include <openssl/proverr.h> |
23 | | #include <openssl/params.h> |
24 | | #include "prov/providercommon.h" |
25 | | #include "prov/implementations.h" |
26 | | #include "prov/provider_ctx.h" |
27 | | #include "prov/securitycheck.h" |
28 | | #include "crypto/dh.h" |
29 | | |
30 | | static OSSL_FUNC_keyexch_newctx_fn dh_newctx; |
31 | | static OSSL_FUNC_keyexch_init_fn dh_init; |
32 | | static OSSL_FUNC_keyexch_set_peer_fn dh_set_peer; |
33 | | static OSSL_FUNC_keyexch_derive_fn dh_derive; |
34 | | static OSSL_FUNC_keyexch_freectx_fn dh_freectx; |
35 | | static OSSL_FUNC_keyexch_dupctx_fn dh_dupctx; |
36 | | static OSSL_FUNC_keyexch_set_ctx_params_fn dh_set_ctx_params; |
37 | | static OSSL_FUNC_keyexch_settable_ctx_params_fn dh_settable_ctx_params; |
38 | | static OSSL_FUNC_keyexch_get_ctx_params_fn dh_get_ctx_params; |
39 | | static OSSL_FUNC_keyexch_gettable_ctx_params_fn dh_gettable_ctx_params; |
40 | | |
41 | | /* |
42 | | * This type is only really used to handle some legacy related functionality. |
43 | | * If you need to use other KDF's (such as SSKDF) just use PROV_DH_KDF_NONE |
44 | | * here and then create and run a KDF after the key is derived. |
45 | | * Note that X942 has 2 variants of key derivation: |
46 | | * (1) DH_KDF_X9_42_ASN1 - which contains an ANS1 encoded object that has |
47 | | * the counter embedded in it. |
48 | | * (2) DH_KDF_X941_CONCAT - which is the same as ECDH_X963_KDF (which can be |
49 | | * done by creating a "X963KDF". |
50 | | */ |
51 | | enum kdf_type { |
52 | | PROV_DH_KDF_NONE = 0, |
53 | | PROV_DH_KDF_X9_42_ASN1 |
54 | | }; |
55 | | |
56 | | /* |
57 | | * What's passed as an actual key is defined by the KEYMGMT interface. |
58 | | * We happen to know that our KEYMGMT simply passes DH structures, so |
59 | | * we use that here too. |
60 | | */ |
61 | | |
62 | | typedef struct { |
63 | | OSSL_LIB_CTX *libctx; |
64 | | DH *dh; |
65 | | DH *dhpeer; |
66 | | unsigned int pad : 1; |
67 | | |
68 | | /* DH KDF */ |
69 | | /* KDF (if any) to use for DH */ |
70 | | enum kdf_type kdf_type; |
71 | | /* Message digest to use for key derivation */ |
72 | | EVP_MD *kdf_md; |
73 | | /* User key material */ |
74 | | unsigned char *kdf_ukm; |
75 | | size_t kdf_ukmlen; |
76 | | /* KDF output length */ |
77 | | size_t kdf_outlen; |
78 | | char *kdf_cekalg; |
79 | | OSSL_FIPS_IND_DECLARE |
80 | | } PROV_DH_CTX; |
81 | | |
82 | | static void *dh_newctx(void *provctx) |
83 | 4.64k | { |
84 | 4.64k | PROV_DH_CTX *pdhctx; |
85 | | |
86 | 4.64k | if (!ossl_prov_is_running()) |
87 | 0 | return NULL; |
88 | | |
89 | 4.64k | pdhctx = OPENSSL_zalloc(sizeof(PROV_DH_CTX)); |
90 | 4.64k | if (pdhctx == NULL) |
91 | 0 | return NULL; |
92 | 4.64k | OSSL_FIPS_IND_INIT(pdhctx) |
93 | 4.64k | pdhctx->libctx = PROV_LIBCTX_OF(provctx); |
94 | 4.64k | pdhctx->kdf_type = PROV_DH_KDF_NONE; |
95 | 4.64k | return pdhctx; |
96 | 4.64k | } |
97 | | |
98 | | #ifdef FIPS_MODULE |
99 | | static int dh_check_key(PROV_DH_CTX *ctx) |
100 | | { |
101 | | int key_approved = ossl_dh_check_key(ctx->dh); |
102 | | |
103 | | if (!key_approved) { |
104 | | if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0, |
105 | | ctx->libctx, "DH Init", "DH Key", |
106 | | ossl_fips_config_securitycheck_enabled)) { |
107 | | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
108 | | return 0; |
109 | | } |
110 | | } |
111 | | return 1; |
112 | | } |
113 | | |
114 | | static int digest_check(PROV_DH_CTX *ctx, const EVP_MD *md) |
115 | | { |
116 | | return ossl_fips_ind_digest_exch_check(OSSL_FIPS_IND_GET(ctx), |
117 | | OSSL_FIPS_IND_SETTABLE1, ctx->libctx, |
118 | | md, "DH Set Ctx"); |
119 | | } |
120 | | #endif |
121 | | |
122 | | static int dh_init(void *vpdhctx, void *vdh, const OSSL_PARAM params[]) |
123 | 2.79k | { |
124 | 2.79k | PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx; |
125 | | |
126 | 2.79k | if (!ossl_prov_is_running() |
127 | 2.79k | || pdhctx == NULL |
128 | 2.79k | || vdh == NULL |
129 | 2.79k | || !DH_up_ref(vdh)) |
130 | 0 | return 0; |
131 | 2.79k | DH_free(pdhctx->dh); |
132 | 2.79k | pdhctx->dh = vdh; |
133 | 2.79k | pdhctx->kdf_type = PROV_DH_KDF_NONE; |
134 | | |
135 | 2.79k | OSSL_FIPS_IND_SET_APPROVED(pdhctx) |
136 | 2.79k | if (!dh_set_ctx_params(pdhctx, params)) |
137 | 0 | return 0; |
138 | | #ifdef FIPS_MODULE |
139 | | if (!dh_check_key(pdhctx)) |
140 | | return 0; |
141 | | #endif |
142 | 2.79k | return 1; |
143 | 2.79k | } |
144 | | |
145 | | /* The 2 parties must share the same domain parameters */ |
146 | | static int dh_match_params(DH *priv, DH *peer) |
147 | 3.46k | { |
148 | 3.46k | int ret; |
149 | 3.46k | int ignore_q = 1; |
150 | 3.46k | FFC_PARAMS *dhparams_priv = ossl_dh_get0_params(priv); |
151 | 3.46k | FFC_PARAMS *dhparams_peer = ossl_dh_get0_params(peer); |
152 | | |
153 | 3.46k | if (dhparams_priv != NULL && dhparams_priv->q != NULL) |
154 | 480 | ignore_q = 0; |
155 | 3.46k | ret = dhparams_priv != NULL |
156 | 3.46k | && dhparams_peer != NULL |
157 | 3.46k | && ossl_ffc_params_cmp(dhparams_priv, dhparams_peer, ignore_q); |
158 | 3.46k | if (!ret) |
159 | 3.46k | ERR_raise(ERR_LIB_PROV, PROV_R_MISMATCHING_DOMAIN_PARAMETERS); |
160 | 3.46k | return ret; |
161 | 3.46k | } |
162 | | |
163 | | static int dh_set_peer(void *vpdhctx, void *vdh) |
164 | 4.34k | { |
165 | 4.34k | PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx; |
166 | | |
167 | 4.34k | if (!ossl_prov_is_running() |
168 | 4.34k | || pdhctx == NULL |
169 | 4.34k | || vdh == NULL |
170 | 4.34k | || !dh_match_params(vdh, pdhctx->dh) |
171 | 4.34k | || !DH_up_ref(vdh)) |
172 | 0 | return 0; |
173 | 4.34k | DH_free(pdhctx->dhpeer); |
174 | 4.34k | pdhctx->dhpeer = vdh; |
175 | 4.34k | return 1; |
176 | 4.34k | } |
177 | | |
178 | | static int dh_plain_derive(void *vpdhctx, |
179 | | unsigned char *secret, size_t *secretlen, |
180 | | size_t outlen, unsigned int pad) |
181 | 8.68k | { |
182 | 8.68k | PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx; |
183 | 8.68k | int ret; |
184 | 8.68k | size_t dhsize; |
185 | 8.68k | const BIGNUM *pub_key = NULL; |
186 | | |
187 | 8.68k | if (pdhctx->dh == NULL || pdhctx->dhpeer == NULL) { |
188 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
189 | 0 | return 0; |
190 | 0 | } |
191 | | |
192 | 8.68k | dhsize = (size_t)DH_size(pdhctx->dh); |
193 | 8.68k | if (secret == NULL) { |
194 | 4.34k | *secretlen = dhsize; |
195 | 4.34k | return 1; |
196 | 4.34k | } |
197 | 4.34k | if (outlen < dhsize) { |
198 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
199 | 0 | return 0; |
200 | 0 | } |
201 | | |
202 | 4.34k | DH_get0_key(pdhctx->dhpeer, &pub_key, NULL); |
203 | 4.34k | if (pad) |
204 | 96 | ret = DH_compute_key_padded(secret, pub_key, pdhctx->dh); |
205 | 4.24k | else |
206 | 4.24k | ret = DH_compute_key(secret, pub_key, pdhctx->dh); |
207 | 4.34k | if (ret <= 0) |
208 | 0 | return 0; |
209 | | |
210 | 4.34k | *secretlen = ret; |
211 | 4.34k | return 1; |
212 | 4.34k | } |
213 | | |
214 | | static int dh_X9_42_kdf_derive(void *vpdhctx, unsigned char *secret, |
215 | | size_t *secretlen, size_t outlen) |
216 | 0 | { |
217 | 0 | PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx; |
218 | 0 | unsigned char *stmp = NULL; |
219 | 0 | size_t stmplen; |
220 | 0 | int ret = 0; |
221 | |
|
222 | 0 | if (secret == NULL) { |
223 | 0 | *secretlen = pdhctx->kdf_outlen; |
224 | 0 | return 1; |
225 | 0 | } |
226 | | |
227 | 0 | if (pdhctx->kdf_outlen > outlen) { |
228 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
229 | 0 | return 0; |
230 | 0 | } |
231 | 0 | if (!dh_plain_derive(pdhctx, NULL, &stmplen, 0, 1)) |
232 | 0 | return 0; |
233 | 0 | if ((stmp = OPENSSL_secure_malloc(stmplen)) == NULL) |
234 | 0 | return 0; |
235 | 0 | if (!dh_plain_derive(pdhctx, stmp, &stmplen, stmplen, 1)) |
236 | 0 | goto err; |
237 | | |
238 | | /* Do KDF stuff */ |
239 | 0 | if (pdhctx->kdf_type == PROV_DH_KDF_X9_42_ASN1) { |
240 | 0 | if (!ossl_dh_kdf_X9_42_asn1(secret, pdhctx->kdf_outlen, |
241 | 0 | stmp, stmplen, |
242 | 0 | pdhctx->kdf_cekalg, |
243 | 0 | pdhctx->kdf_ukm, |
244 | 0 | pdhctx->kdf_ukmlen, |
245 | 0 | pdhctx->kdf_md, |
246 | 0 | pdhctx->libctx, NULL)) |
247 | 0 | goto err; |
248 | 0 | } |
249 | 0 | *secretlen = pdhctx->kdf_outlen; |
250 | 0 | ret = 1; |
251 | 0 | err: |
252 | 0 | OPENSSL_secure_clear_free(stmp, stmplen); |
253 | 0 | return ret; |
254 | 0 | } |
255 | | |
256 | | static int dh_derive(void *vpdhctx, unsigned char *secret, |
257 | | size_t *psecretlen, size_t outlen) |
258 | 8.68k | { |
259 | 8.68k | PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx; |
260 | | |
261 | 8.68k | if (!ossl_prov_is_running()) |
262 | 0 | return 0; |
263 | | |
264 | 8.68k | switch (pdhctx->kdf_type) { |
265 | 8.68k | case PROV_DH_KDF_NONE: |
266 | 8.68k | return dh_plain_derive(pdhctx, secret, psecretlen, outlen, |
267 | 8.68k | pdhctx->pad); |
268 | 0 | case PROV_DH_KDF_X9_42_ASN1: |
269 | 0 | return dh_X9_42_kdf_derive(pdhctx, secret, psecretlen, outlen); |
270 | 0 | default: |
271 | 0 | break; |
272 | 8.68k | } |
273 | 0 | return 0; |
274 | 8.68k | } |
275 | | |
276 | | static void dh_freectx(void *vpdhctx) |
277 | 4.64k | { |
278 | 4.64k | PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx; |
279 | | |
280 | 4.64k | OPENSSL_free(pdhctx->kdf_cekalg); |
281 | 4.64k | DH_free(pdhctx->dh); |
282 | 4.64k | DH_free(pdhctx->dhpeer); |
283 | 4.64k | EVP_MD_free(pdhctx->kdf_md); |
284 | 4.64k | OPENSSL_clear_free(pdhctx->kdf_ukm, pdhctx->kdf_ukmlen); |
285 | | |
286 | 4.64k | OPENSSL_free(pdhctx); |
287 | 4.64k | } |
288 | | |
289 | | static void *dh_dupctx(void *vpdhctx) |
290 | 0 | { |
291 | 0 | PROV_DH_CTX *srcctx = (PROV_DH_CTX *)vpdhctx; |
292 | 0 | PROV_DH_CTX *dstctx; |
293 | |
|
294 | 0 | if (!ossl_prov_is_running()) |
295 | 0 | return NULL; |
296 | | |
297 | 0 | dstctx = OPENSSL_zalloc(sizeof(*srcctx)); |
298 | 0 | if (dstctx == NULL) |
299 | 0 | return NULL; |
300 | | |
301 | 0 | *dstctx = *srcctx; |
302 | 0 | dstctx->dh = NULL; |
303 | 0 | dstctx->dhpeer = NULL; |
304 | 0 | dstctx->kdf_md = NULL; |
305 | 0 | dstctx->kdf_ukm = NULL; |
306 | 0 | dstctx->kdf_cekalg = NULL; |
307 | |
|
308 | 0 | if (srcctx->dh != NULL && !DH_up_ref(srcctx->dh)) |
309 | 0 | goto err; |
310 | 0 | else |
311 | 0 | dstctx->dh = srcctx->dh; |
312 | | |
313 | 0 | if (srcctx->dhpeer != NULL && !DH_up_ref(srcctx->dhpeer)) |
314 | 0 | goto err; |
315 | 0 | else |
316 | 0 | dstctx->dhpeer = srcctx->dhpeer; |
317 | | |
318 | 0 | if (srcctx->kdf_md != NULL && !EVP_MD_up_ref(srcctx->kdf_md)) |
319 | 0 | goto err; |
320 | 0 | else |
321 | 0 | dstctx->kdf_md = srcctx->kdf_md; |
322 | | |
323 | | /* Duplicate UKM data if present */ |
324 | 0 | if (srcctx->kdf_ukm != NULL && srcctx->kdf_ukmlen > 0) { |
325 | 0 | dstctx->kdf_ukm = OPENSSL_memdup(srcctx->kdf_ukm, |
326 | 0 | srcctx->kdf_ukmlen); |
327 | 0 | if (dstctx->kdf_ukm == NULL) |
328 | 0 | goto err; |
329 | 0 | } |
330 | | |
331 | 0 | if (srcctx->kdf_cekalg != NULL) { |
332 | 0 | dstctx->kdf_cekalg = OPENSSL_strdup(srcctx->kdf_cekalg); |
333 | 0 | if (dstctx->kdf_cekalg == NULL) |
334 | 0 | goto err; |
335 | 0 | } |
336 | | |
337 | 0 | return dstctx; |
338 | 0 | err: |
339 | 0 | dh_freectx(dstctx); |
340 | 0 | return NULL; |
341 | 0 | } |
342 | | |
343 | | static int dh_set_ctx_params(void *vpdhctx, const OSSL_PARAM params[]) |
344 | 699 | { |
345 | 699 | PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx; |
346 | 699 | const OSSL_PARAM *p; |
347 | 699 | unsigned int pad; |
348 | 699 | char name[80] = { '\0' }; /* should be big enough */ |
349 | 699 | char *str = NULL; |
350 | | |
351 | 699 | if (pdhctx == NULL) |
352 | 0 | return 0; |
353 | 699 | if (ossl_param_is_empty(params)) |
354 | 699 | return 1; |
355 | | |
356 | 0 | if (!OSSL_FIPS_IND_SET_CTX_PARAM(pdhctx, OSSL_FIPS_IND_SETTABLE0, params, |
357 | 0 | OSSL_EXCHANGE_PARAM_FIPS_KEY_CHECK)) |
358 | 0 | return 0; |
359 | 0 | if (!OSSL_FIPS_IND_SET_CTX_PARAM(pdhctx, OSSL_FIPS_IND_SETTABLE1, params, |
360 | 0 | OSSL_EXCHANGE_PARAM_FIPS_DIGEST_CHECK)) |
361 | 0 | return 0; |
362 | | |
363 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_EXCHANGE_PARAM_KDF_TYPE); |
364 | 0 | if (p != NULL) { |
365 | 0 | str = name; |
366 | 0 | if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(name))) |
367 | 0 | return 0; |
368 | | |
369 | 0 | if (name[0] == '\0') |
370 | 0 | pdhctx->kdf_type = PROV_DH_KDF_NONE; |
371 | 0 | else if (strcmp(name, OSSL_KDF_NAME_X942KDF_ASN1) == 0) |
372 | 0 | pdhctx->kdf_type = PROV_DH_KDF_X9_42_ASN1; |
373 | 0 | else |
374 | 0 | return 0; |
375 | 0 | } |
376 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_EXCHANGE_PARAM_KDF_DIGEST); |
377 | 0 | if (p != NULL) { |
378 | 0 | char mdprops[80] = { '\0' }; /* should be big enough */ |
379 | |
|
380 | 0 | str = name; |
381 | 0 | if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(name))) |
382 | 0 | return 0; |
383 | | |
384 | 0 | str = mdprops; |
385 | 0 | p = OSSL_PARAM_locate_const(params, |
386 | 0 | OSSL_EXCHANGE_PARAM_KDF_DIGEST_PROPS); |
387 | |
|
388 | 0 | if (p != NULL) { |
389 | 0 | if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops))) |
390 | 0 | return 0; |
391 | 0 | } |
392 | | |
393 | 0 | EVP_MD_free(pdhctx->kdf_md); |
394 | 0 | pdhctx->kdf_md = EVP_MD_fetch(pdhctx->libctx, name, mdprops); |
395 | 0 | if (pdhctx->kdf_md == NULL) |
396 | 0 | return 0; |
397 | | /* XOF digests are not allowed */ |
398 | 0 | if (EVP_MD_xof(pdhctx->kdf_md)) { |
399 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED); |
400 | 0 | return 0; |
401 | 0 | } |
402 | | #ifdef FIPS_MODULE |
403 | | if (!digest_check(pdhctx, pdhctx->kdf_md)) { |
404 | | EVP_MD_free(pdhctx->kdf_md); |
405 | | pdhctx->kdf_md = NULL; |
406 | | return 0; |
407 | | } |
408 | | #endif |
409 | 0 | } |
410 | | |
411 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_EXCHANGE_PARAM_KDF_OUTLEN); |
412 | 0 | if (p != NULL) { |
413 | 0 | size_t outlen; |
414 | |
|
415 | 0 | if (!OSSL_PARAM_get_size_t(p, &outlen)) |
416 | 0 | return 0; |
417 | 0 | pdhctx->kdf_outlen = outlen; |
418 | 0 | } |
419 | | |
420 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_EXCHANGE_PARAM_KDF_UKM); |
421 | 0 | if (p != NULL) { |
422 | 0 | void *tmp_ukm = NULL; |
423 | 0 | size_t tmp_ukmlen; |
424 | |
|
425 | 0 | OPENSSL_free(pdhctx->kdf_ukm); |
426 | 0 | pdhctx->kdf_ukm = NULL; |
427 | 0 | pdhctx->kdf_ukmlen = 0; |
428 | | /* ukm is an optional field so it can be NULL */ |
429 | 0 | if (p->data != NULL && p->data_size != 0) { |
430 | 0 | if (!OSSL_PARAM_get_octet_string(p, &tmp_ukm, 0, &tmp_ukmlen)) |
431 | 0 | return 0; |
432 | 0 | pdhctx->kdf_ukm = tmp_ukm; |
433 | 0 | pdhctx->kdf_ukmlen = tmp_ukmlen; |
434 | 0 | } |
435 | 0 | } |
436 | | |
437 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_EXCHANGE_PARAM_PAD); |
438 | 0 | if (p != NULL) { |
439 | 0 | if (!OSSL_PARAM_get_uint(p, &pad)) |
440 | 0 | return 0; |
441 | 0 | pdhctx->pad = pad ? 1 : 0; |
442 | 0 | } |
443 | | |
444 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_CEK_ALG); |
445 | 0 | if (p != NULL) { |
446 | 0 | str = name; |
447 | |
|
448 | 0 | OPENSSL_free(pdhctx->kdf_cekalg); |
449 | 0 | pdhctx->kdf_cekalg = NULL; |
450 | 0 | if (p->data != NULL && p->data_size != 0) { |
451 | 0 | if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(name))) |
452 | 0 | return 0; |
453 | 0 | pdhctx->kdf_cekalg = OPENSSL_strdup(name); |
454 | 0 | if (pdhctx->kdf_cekalg == NULL) |
455 | 0 | return 0; |
456 | 0 | } |
457 | 0 | } |
458 | 0 | return 1; |
459 | 0 | } |
460 | | |
461 | | static const OSSL_PARAM known_settable_ctx_params[] = { |
462 | | OSSL_PARAM_int(OSSL_EXCHANGE_PARAM_PAD, NULL), |
463 | | OSSL_PARAM_utf8_string(OSSL_EXCHANGE_PARAM_KDF_TYPE, NULL, 0), |
464 | | OSSL_PARAM_utf8_string(OSSL_EXCHANGE_PARAM_KDF_DIGEST, NULL, 0), |
465 | | OSSL_PARAM_utf8_string(OSSL_EXCHANGE_PARAM_KDF_DIGEST_PROPS, NULL, 0), |
466 | | OSSL_PARAM_size_t(OSSL_EXCHANGE_PARAM_KDF_OUTLEN, NULL), |
467 | | OSSL_PARAM_octet_string(OSSL_EXCHANGE_PARAM_KDF_UKM, NULL, 0), |
468 | | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CEK_ALG, NULL, 0), |
469 | | OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_EXCHANGE_PARAM_FIPS_KEY_CHECK) |
470 | | OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_EXCHANGE_PARAM_FIPS_DIGEST_CHECK) |
471 | | OSSL_PARAM_END |
472 | | }; |
473 | | |
474 | | static const OSSL_PARAM *dh_settable_ctx_params(ossl_unused void *vpdhctx, |
475 | | ossl_unused void *provctx) |
476 | 112 | { |
477 | 112 | return known_settable_ctx_params; |
478 | 112 | } |
479 | | |
480 | | static const OSSL_PARAM known_gettable_ctx_params[] = { |
481 | | OSSL_PARAM_utf8_string(OSSL_EXCHANGE_PARAM_KDF_TYPE, NULL, 0), |
482 | | OSSL_PARAM_utf8_string(OSSL_EXCHANGE_PARAM_KDF_DIGEST, NULL, 0), |
483 | | OSSL_PARAM_size_t(OSSL_EXCHANGE_PARAM_KDF_OUTLEN, NULL), |
484 | | OSSL_PARAM_DEFN(OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_PTR, |
485 | | NULL, 0), |
486 | | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CEK_ALG, NULL, 0), |
487 | | OSSL_FIPS_IND_GETTABLE_CTX_PARAM() |
488 | | OSSL_PARAM_END |
489 | | }; |
490 | | |
491 | | static const OSSL_PARAM *dh_gettable_ctx_params(ossl_unused void *vpdhctx, |
492 | | ossl_unused void *provctx) |
493 | 0 | { |
494 | 0 | return known_gettable_ctx_params; |
495 | 0 | } |
496 | | |
497 | | static int dh_get_ctx_params(void *vpdhctx, OSSL_PARAM params[]) |
498 | 0 | { |
499 | 0 | PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx; |
500 | 0 | OSSL_PARAM *p; |
501 | |
|
502 | 0 | if (pdhctx == NULL) |
503 | 0 | return 0; |
504 | | |
505 | 0 | p = OSSL_PARAM_locate(params, OSSL_EXCHANGE_PARAM_KDF_TYPE); |
506 | 0 | if (p != NULL) { |
507 | 0 | const char *kdf_type = NULL; |
508 | |
|
509 | 0 | switch (pdhctx->kdf_type) { |
510 | 0 | case PROV_DH_KDF_NONE: |
511 | 0 | kdf_type = ""; |
512 | 0 | break; |
513 | 0 | case PROV_DH_KDF_X9_42_ASN1: |
514 | 0 | kdf_type = OSSL_KDF_NAME_X942KDF_ASN1; |
515 | 0 | break; |
516 | 0 | default: |
517 | 0 | return 0; |
518 | 0 | } |
519 | | |
520 | 0 | if (!OSSL_PARAM_set_utf8_string(p, kdf_type)) |
521 | 0 | return 0; |
522 | 0 | } |
523 | | |
524 | 0 | p = OSSL_PARAM_locate(params, OSSL_EXCHANGE_PARAM_KDF_DIGEST); |
525 | 0 | if (p != NULL |
526 | 0 | && !OSSL_PARAM_set_utf8_string(p, pdhctx->kdf_md == NULL ? "" : EVP_MD_get0_name(pdhctx->kdf_md))) { |
527 | 0 | return 0; |
528 | 0 | } |
529 | | |
530 | 0 | p = OSSL_PARAM_locate(params, OSSL_EXCHANGE_PARAM_KDF_OUTLEN); |
531 | 0 | if (p != NULL && !OSSL_PARAM_set_size_t(p, pdhctx->kdf_outlen)) |
532 | 0 | return 0; |
533 | | |
534 | 0 | p = OSSL_PARAM_locate(params, OSSL_EXCHANGE_PARAM_KDF_UKM); |
535 | 0 | if (p != NULL |
536 | 0 | && !OSSL_PARAM_set_octet_ptr(p, pdhctx->kdf_ukm, pdhctx->kdf_ukmlen)) |
537 | 0 | return 0; |
538 | | |
539 | 0 | p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_CEK_ALG); |
540 | 0 | if (p != NULL |
541 | 0 | && !OSSL_PARAM_set_utf8_string(p, pdhctx->kdf_cekalg == NULL ? "" : pdhctx->kdf_cekalg)) |
542 | 0 | return 0; |
543 | 0 | if (!OSSL_FIPS_IND_GET_CTX_PARAM(pdhctx, params)) |
544 | 0 | return 0; |
545 | 0 | return 1; |
546 | 0 | } |
547 | | |
548 | | const OSSL_DISPATCH ossl_dh_keyexch_functions[] = { |
549 | | { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))dh_newctx }, |
550 | | { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))dh_init }, |
551 | | { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))dh_derive }, |
552 | | { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))dh_set_peer }, |
553 | | { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))dh_freectx }, |
554 | | { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))dh_dupctx }, |
555 | | { OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS, (void (*)(void))dh_set_ctx_params }, |
556 | | { OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS, |
557 | | (void (*)(void))dh_settable_ctx_params }, |
558 | | { OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS, (void (*)(void))dh_get_ctx_params }, |
559 | | { OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS, |
560 | | (void (*)(void))dh_gettable_ctx_params }, |
561 | | OSSL_DISPATCH_END |
562 | | }; |