/src/openssl30/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 | | } PROV_DH_CTX; |
80 | | |
81 | | static void *dh_newctx(void *provctx) |
82 | 4.64k | { |
83 | 4.64k | PROV_DH_CTX *pdhctx; |
84 | | |
85 | 4.64k | if (!ossl_prov_is_running()) |
86 | 0 | return NULL; |
87 | | |
88 | 4.64k | pdhctx = OPENSSL_zalloc(sizeof(PROV_DH_CTX)); |
89 | 4.64k | if (pdhctx == NULL) |
90 | 0 | return NULL; |
91 | 4.64k | pdhctx->libctx = PROV_LIBCTX_OF(provctx); |
92 | 4.64k | pdhctx->kdf_type = PROV_DH_KDF_NONE; |
93 | 4.64k | return pdhctx; |
94 | 4.64k | } |
95 | | |
96 | | static int dh_init(void *vpdhctx, void *vdh, const OSSL_PARAM params[]) |
97 | 1.85k | { |
98 | 1.85k | PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx; |
99 | | |
100 | 1.85k | if (!ossl_prov_is_running() |
101 | 1.85k | || pdhctx == NULL |
102 | 1.85k | || vdh == NULL |
103 | 1.85k | || !DH_up_ref(vdh)) |
104 | 0 | return 0; |
105 | 1.85k | DH_free(pdhctx->dh); |
106 | 1.85k | pdhctx->dh = vdh; |
107 | 1.85k | pdhctx->kdf_type = PROV_DH_KDF_NONE; |
108 | 1.85k | return dh_set_ctx_params(pdhctx, params) |
109 | 1.85k | && ossl_dh_check_key(pdhctx->libctx, vdh); |
110 | 1.85k | } |
111 | | |
112 | | /* The 2 parties must share the same domain parameters */ |
113 | | static int dh_match_params(DH *priv, DH *peer) |
114 | 3.46k | { |
115 | 3.46k | int ret; |
116 | 3.46k | int ignore_q = 1; |
117 | 3.46k | FFC_PARAMS *dhparams_priv = ossl_dh_get0_params(priv); |
118 | 3.46k | FFC_PARAMS *dhparams_peer = ossl_dh_get0_params(peer); |
119 | | |
120 | 3.46k | if (dhparams_priv != NULL && dhparams_priv->q != NULL) |
121 | 480 | ignore_q = 0; |
122 | 3.46k | ret = dhparams_priv != NULL |
123 | 3.46k | && dhparams_peer != NULL |
124 | 3.46k | && ossl_ffc_params_cmp(dhparams_priv, dhparams_peer, ignore_q); |
125 | 3.46k | if (!ret) |
126 | 3.46k | ERR_raise(ERR_LIB_PROV, PROV_R_MISMATCHING_DOMAIN_PARAMETERS); |
127 | 3.46k | return ret; |
128 | 3.46k | } |
129 | | |
130 | | static int dh_set_peer(void *vpdhctx, void *vdh) |
131 | 4.34k | { |
132 | 4.34k | PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx; |
133 | | |
134 | 4.34k | if (!ossl_prov_is_running() |
135 | 4.34k | || pdhctx == NULL |
136 | 4.34k | || vdh == NULL |
137 | 4.34k | || !dh_match_params(vdh, pdhctx->dh) |
138 | 4.34k | || !DH_up_ref(vdh)) |
139 | 0 | return 0; |
140 | 4.34k | DH_free(pdhctx->dhpeer); |
141 | 4.34k | pdhctx->dhpeer = vdh; |
142 | 4.34k | return 1; |
143 | 4.34k | } |
144 | | |
145 | | static int dh_plain_derive(void *vpdhctx, |
146 | | unsigned char *secret, size_t *secretlen, |
147 | | size_t outlen, unsigned int pad) |
148 | 8.68k | { |
149 | 8.68k | PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx; |
150 | 8.68k | int ret; |
151 | 8.68k | size_t dhsize; |
152 | 8.68k | const BIGNUM *pub_key = NULL; |
153 | | |
154 | 8.68k | if (pdhctx->dh == NULL || pdhctx->dhpeer == NULL) { |
155 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
156 | 0 | return 0; |
157 | 0 | } |
158 | | |
159 | 8.68k | dhsize = (size_t)DH_size(pdhctx->dh); |
160 | 8.68k | if (secret == NULL) { |
161 | 4.34k | *secretlen = dhsize; |
162 | 4.34k | return 1; |
163 | 4.34k | } |
164 | 4.34k | if (outlen < dhsize) { |
165 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
166 | 0 | return 0; |
167 | 0 | } |
168 | | |
169 | 4.34k | DH_get0_key(pdhctx->dhpeer, &pub_key, NULL); |
170 | 4.34k | if (pad) |
171 | 96 | ret = DH_compute_key_padded(secret, pub_key, pdhctx->dh); |
172 | 4.24k | else |
173 | 4.24k | ret = DH_compute_key(secret, pub_key, pdhctx->dh); |
174 | 4.34k | if (ret <= 0) |
175 | 0 | return 0; |
176 | | |
177 | 4.34k | *secretlen = ret; |
178 | 4.34k | return 1; |
179 | 4.34k | } |
180 | | |
181 | | static int dh_X9_42_kdf_derive(void *vpdhctx, unsigned char *secret, |
182 | | size_t *secretlen, size_t outlen) |
183 | 0 | { |
184 | 0 | PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx; |
185 | 0 | unsigned char *stmp = NULL; |
186 | 0 | size_t stmplen; |
187 | 0 | int ret = 0; |
188 | |
|
189 | 0 | if (secret == NULL) { |
190 | 0 | *secretlen = pdhctx->kdf_outlen; |
191 | 0 | return 1; |
192 | 0 | } |
193 | | |
194 | 0 | if (pdhctx->kdf_outlen > outlen) { |
195 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
196 | 0 | return 0; |
197 | 0 | } |
198 | 0 | if (!dh_plain_derive(pdhctx, NULL, &stmplen, 0, 1)) |
199 | 0 | return 0; |
200 | 0 | if ((stmp = OPENSSL_secure_malloc(stmplen)) == NULL) { |
201 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); |
202 | 0 | return 0; |
203 | 0 | } |
204 | 0 | if (!dh_plain_derive(pdhctx, stmp, &stmplen, stmplen, 1)) |
205 | 0 | goto err; |
206 | | |
207 | | /* Do KDF stuff */ |
208 | 0 | if (pdhctx->kdf_type == PROV_DH_KDF_X9_42_ASN1) { |
209 | 0 | if (!ossl_dh_kdf_X9_42_asn1(secret, pdhctx->kdf_outlen, |
210 | 0 | stmp, stmplen, |
211 | 0 | pdhctx->kdf_cekalg, |
212 | 0 | pdhctx->kdf_ukm, |
213 | 0 | pdhctx->kdf_ukmlen, |
214 | 0 | pdhctx->kdf_md, |
215 | 0 | pdhctx->libctx, NULL)) |
216 | 0 | goto err; |
217 | 0 | } |
218 | 0 | *secretlen = pdhctx->kdf_outlen; |
219 | 0 | ret = 1; |
220 | 0 | err: |
221 | 0 | OPENSSL_secure_clear_free(stmp, stmplen); |
222 | 0 | return ret; |
223 | 0 | } |
224 | | |
225 | | static int dh_derive(void *vpdhctx, unsigned char *secret, |
226 | | size_t *psecretlen, size_t outlen) |
227 | 8.68k | { |
228 | 8.68k | PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx; |
229 | | |
230 | 8.68k | if (!ossl_prov_is_running()) |
231 | 0 | return 0; |
232 | | |
233 | 8.68k | switch (pdhctx->kdf_type) { |
234 | 8.68k | case PROV_DH_KDF_NONE: |
235 | 8.68k | return dh_plain_derive(pdhctx, secret, psecretlen, outlen, |
236 | 8.68k | pdhctx->pad); |
237 | 0 | case PROV_DH_KDF_X9_42_ASN1: |
238 | 0 | return dh_X9_42_kdf_derive(pdhctx, secret, psecretlen, outlen); |
239 | 0 | default: |
240 | 0 | break; |
241 | 8.68k | } |
242 | 0 | return 0; |
243 | 8.68k | } |
244 | | |
245 | | static void dh_freectx(void *vpdhctx) |
246 | 4.64k | { |
247 | 4.64k | PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx; |
248 | | |
249 | 4.64k | OPENSSL_free(pdhctx->kdf_cekalg); |
250 | 4.64k | DH_free(pdhctx->dh); |
251 | 4.64k | DH_free(pdhctx->dhpeer); |
252 | 4.64k | EVP_MD_free(pdhctx->kdf_md); |
253 | 4.64k | OPENSSL_clear_free(pdhctx->kdf_ukm, pdhctx->kdf_ukmlen); |
254 | | |
255 | 4.64k | OPENSSL_free(pdhctx); |
256 | 4.64k | } |
257 | | |
258 | | static void *dh_dupctx(void *vpdhctx) |
259 | 0 | { |
260 | 0 | PROV_DH_CTX *srcctx = (PROV_DH_CTX *)vpdhctx; |
261 | 0 | PROV_DH_CTX *dstctx; |
262 | |
|
263 | 0 | if (!ossl_prov_is_running()) |
264 | 0 | return NULL; |
265 | | |
266 | 0 | dstctx = OPENSSL_zalloc(sizeof(*srcctx)); |
267 | 0 | if (dstctx == NULL) |
268 | 0 | return NULL; |
269 | | |
270 | 0 | *dstctx = *srcctx; |
271 | 0 | dstctx->dh = NULL; |
272 | 0 | dstctx->dhpeer = NULL; |
273 | 0 | dstctx->kdf_md = NULL; |
274 | 0 | dstctx->kdf_ukm = NULL; |
275 | 0 | dstctx->kdf_cekalg = NULL; |
276 | |
|
277 | 0 | if (srcctx->dh != NULL && !DH_up_ref(srcctx->dh)) |
278 | 0 | goto err; |
279 | 0 | else |
280 | 0 | dstctx->dh = srcctx->dh; |
281 | | |
282 | 0 | if (srcctx->dhpeer != NULL && !DH_up_ref(srcctx->dhpeer)) |
283 | 0 | goto err; |
284 | 0 | else |
285 | 0 | dstctx->dhpeer = srcctx->dhpeer; |
286 | | |
287 | 0 | if (srcctx->kdf_md != NULL && !EVP_MD_up_ref(srcctx->kdf_md)) |
288 | 0 | goto err; |
289 | 0 | else |
290 | 0 | dstctx->kdf_md = srcctx->kdf_md; |
291 | | |
292 | | /* Duplicate UKM data if present */ |
293 | 0 | if (srcctx->kdf_ukm != NULL && srcctx->kdf_ukmlen > 0) { |
294 | 0 | dstctx->kdf_ukm = OPENSSL_memdup(srcctx->kdf_ukm, |
295 | 0 | srcctx->kdf_ukmlen); |
296 | 0 | if (dstctx->kdf_ukm == NULL) |
297 | 0 | goto err; |
298 | 0 | } |
299 | | |
300 | 0 | if (srcctx->kdf_cekalg != NULL) { |
301 | 0 | dstctx->kdf_cekalg = OPENSSL_strdup(srcctx->kdf_cekalg); |
302 | 0 | if (dstctx->kdf_cekalg == NULL) |
303 | 0 | goto err; |
304 | 0 | } |
305 | | |
306 | 0 | return dstctx; |
307 | 0 | err: |
308 | 0 | dh_freectx(dstctx); |
309 | 0 | return NULL; |
310 | 0 | } |
311 | | |
312 | | static int dh_set_ctx_params(void *vpdhctx, const OSSL_PARAM params[]) |
313 | 757 | { |
314 | 757 | PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx; |
315 | 757 | const OSSL_PARAM *p; |
316 | 757 | unsigned int pad; |
317 | 757 | char name[80] = { '\0' }; /* should be big enough */ |
318 | 757 | char *str = NULL; |
319 | | |
320 | 757 | if (pdhctx == NULL) |
321 | 0 | return 0; |
322 | 757 | if (params == NULL) |
323 | 757 | return 1; |
324 | | |
325 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_EXCHANGE_PARAM_KDF_TYPE); |
326 | 0 | if (p != NULL) { |
327 | 0 | str = name; |
328 | 0 | if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(name))) |
329 | 0 | return 0; |
330 | | |
331 | 0 | if (name[0] == '\0') |
332 | 0 | pdhctx->kdf_type = PROV_DH_KDF_NONE; |
333 | 0 | else if (strcmp(name, OSSL_KDF_NAME_X942KDF_ASN1) == 0) |
334 | 0 | pdhctx->kdf_type = PROV_DH_KDF_X9_42_ASN1; |
335 | 0 | else |
336 | 0 | return 0; |
337 | 0 | } |
338 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_EXCHANGE_PARAM_KDF_DIGEST); |
339 | 0 | if (p != NULL) { |
340 | 0 | char mdprops[80] = { '\0' }; /* should be big enough */ |
341 | |
|
342 | 0 | str = name; |
343 | 0 | if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(name))) |
344 | 0 | return 0; |
345 | | |
346 | 0 | str = mdprops; |
347 | 0 | p = OSSL_PARAM_locate_const(params, |
348 | 0 | OSSL_EXCHANGE_PARAM_KDF_DIGEST_PROPS); |
349 | |
|
350 | 0 | if (p != NULL) { |
351 | 0 | if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops))) |
352 | 0 | return 0; |
353 | 0 | } |
354 | | |
355 | 0 | EVP_MD_free(pdhctx->kdf_md); |
356 | 0 | pdhctx->kdf_md = EVP_MD_fetch(pdhctx->libctx, name, mdprops); |
357 | 0 | if (!ossl_digest_is_allowed(pdhctx->libctx, pdhctx->kdf_md)) { |
358 | 0 | EVP_MD_free(pdhctx->kdf_md); |
359 | 0 | pdhctx->kdf_md = NULL; |
360 | 0 | } |
361 | 0 | if (pdhctx->kdf_md == NULL) |
362 | 0 | return 0; |
363 | 0 | } |
364 | | |
365 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_EXCHANGE_PARAM_KDF_OUTLEN); |
366 | 0 | if (p != NULL) { |
367 | 0 | size_t outlen; |
368 | |
|
369 | 0 | if (!OSSL_PARAM_get_size_t(p, &outlen)) |
370 | 0 | return 0; |
371 | 0 | pdhctx->kdf_outlen = outlen; |
372 | 0 | } |
373 | | |
374 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_EXCHANGE_PARAM_KDF_UKM); |
375 | 0 | if (p != NULL) { |
376 | 0 | void *tmp_ukm = NULL; |
377 | 0 | size_t tmp_ukmlen; |
378 | |
|
379 | 0 | OPENSSL_free(pdhctx->kdf_ukm); |
380 | 0 | pdhctx->kdf_ukm = NULL; |
381 | 0 | pdhctx->kdf_ukmlen = 0; |
382 | | /* ukm is an optional field so it can be NULL */ |
383 | 0 | if (p->data != NULL && p->data_size != 0) { |
384 | 0 | if (!OSSL_PARAM_get_octet_string(p, &tmp_ukm, 0, &tmp_ukmlen)) |
385 | 0 | return 0; |
386 | 0 | pdhctx->kdf_ukm = tmp_ukm; |
387 | 0 | pdhctx->kdf_ukmlen = tmp_ukmlen; |
388 | 0 | } |
389 | 0 | } |
390 | | |
391 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_EXCHANGE_PARAM_PAD); |
392 | 0 | if (p != NULL) { |
393 | 0 | if (!OSSL_PARAM_get_uint(p, &pad)) |
394 | 0 | return 0; |
395 | 0 | pdhctx->pad = pad ? 1 : 0; |
396 | 0 | } |
397 | | |
398 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_CEK_ALG); |
399 | 0 | if (p != NULL) { |
400 | 0 | str = name; |
401 | |
|
402 | 0 | OPENSSL_free(pdhctx->kdf_cekalg); |
403 | 0 | pdhctx->kdf_cekalg = NULL; |
404 | 0 | if (p->data != NULL && p->data_size != 0) { |
405 | 0 | if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(name))) |
406 | 0 | return 0; |
407 | 0 | pdhctx->kdf_cekalg = OPENSSL_strdup(name); |
408 | 0 | if (pdhctx->kdf_cekalg == NULL) |
409 | 0 | return 0; |
410 | 0 | } |
411 | 0 | } |
412 | 0 | return 1; |
413 | 0 | } |
414 | | |
415 | | static const OSSL_PARAM known_settable_ctx_params[] = { |
416 | | OSSL_PARAM_int(OSSL_EXCHANGE_PARAM_PAD, NULL), |
417 | | OSSL_PARAM_utf8_string(OSSL_EXCHANGE_PARAM_KDF_TYPE, NULL, 0), |
418 | | OSSL_PARAM_utf8_string(OSSL_EXCHANGE_PARAM_KDF_DIGEST, NULL, 0), |
419 | | OSSL_PARAM_utf8_string(OSSL_EXCHANGE_PARAM_KDF_DIGEST_PROPS, NULL, 0), |
420 | | OSSL_PARAM_size_t(OSSL_EXCHANGE_PARAM_KDF_OUTLEN, NULL), |
421 | | OSSL_PARAM_octet_string(OSSL_EXCHANGE_PARAM_KDF_UKM, NULL, 0), |
422 | | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CEK_ALG, NULL, 0), |
423 | | OSSL_PARAM_END |
424 | | }; |
425 | | |
426 | | static const OSSL_PARAM *dh_settable_ctx_params(ossl_unused void *vpdhctx, |
427 | | ossl_unused void *provctx) |
428 | 112 | { |
429 | 112 | return known_settable_ctx_params; |
430 | 112 | } |
431 | | |
432 | | static const OSSL_PARAM known_gettable_ctx_params[] = { |
433 | | OSSL_PARAM_utf8_string(OSSL_EXCHANGE_PARAM_KDF_TYPE, NULL, 0), |
434 | | OSSL_PARAM_utf8_string(OSSL_EXCHANGE_PARAM_KDF_DIGEST, NULL, 0), |
435 | | OSSL_PARAM_size_t(OSSL_EXCHANGE_PARAM_KDF_OUTLEN, NULL), |
436 | | OSSL_PARAM_DEFN(OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_PTR, |
437 | | NULL, 0), |
438 | | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CEK_ALG, NULL, 0), |
439 | | OSSL_PARAM_END |
440 | | }; |
441 | | |
442 | | static const OSSL_PARAM *dh_gettable_ctx_params(ossl_unused void *vpdhctx, |
443 | | ossl_unused void *provctx) |
444 | 0 | { |
445 | 0 | return known_gettable_ctx_params; |
446 | 0 | } |
447 | | |
448 | | static int dh_get_ctx_params(void *vpdhctx, OSSL_PARAM params[]) |
449 | 0 | { |
450 | 0 | PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx; |
451 | 0 | OSSL_PARAM *p; |
452 | |
|
453 | 0 | if (pdhctx == NULL) |
454 | 0 | return 0; |
455 | | |
456 | 0 | p = OSSL_PARAM_locate(params, OSSL_EXCHANGE_PARAM_KDF_TYPE); |
457 | 0 | if (p != NULL) { |
458 | 0 | const char *kdf_type = NULL; |
459 | |
|
460 | 0 | switch (pdhctx->kdf_type) { |
461 | 0 | case PROV_DH_KDF_NONE: |
462 | 0 | kdf_type = ""; |
463 | 0 | break; |
464 | 0 | case PROV_DH_KDF_X9_42_ASN1: |
465 | 0 | kdf_type = OSSL_KDF_NAME_X942KDF_ASN1; |
466 | 0 | break; |
467 | 0 | default: |
468 | 0 | return 0; |
469 | 0 | } |
470 | | |
471 | 0 | if (!OSSL_PARAM_set_utf8_string(p, kdf_type)) |
472 | 0 | return 0; |
473 | 0 | } |
474 | | |
475 | 0 | p = OSSL_PARAM_locate(params, OSSL_EXCHANGE_PARAM_KDF_DIGEST); |
476 | 0 | if (p != NULL |
477 | 0 | && !OSSL_PARAM_set_utf8_string(p, pdhctx->kdf_md == NULL ? "" : EVP_MD_get0_name(pdhctx->kdf_md))) { |
478 | 0 | return 0; |
479 | 0 | } |
480 | | |
481 | 0 | p = OSSL_PARAM_locate(params, OSSL_EXCHANGE_PARAM_KDF_OUTLEN); |
482 | 0 | if (p != NULL && !OSSL_PARAM_set_size_t(p, pdhctx->kdf_outlen)) |
483 | 0 | return 0; |
484 | | |
485 | 0 | p = OSSL_PARAM_locate(params, OSSL_EXCHANGE_PARAM_KDF_UKM); |
486 | 0 | if (p != NULL |
487 | 0 | && !OSSL_PARAM_set_octet_ptr(p, pdhctx->kdf_ukm, pdhctx->kdf_ukmlen)) |
488 | 0 | return 0; |
489 | | |
490 | 0 | p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_CEK_ALG); |
491 | 0 | if (p != NULL |
492 | 0 | && !OSSL_PARAM_set_utf8_string(p, pdhctx->kdf_cekalg == NULL ? "" : pdhctx->kdf_cekalg)) |
493 | 0 | return 0; |
494 | | |
495 | 0 | return 1; |
496 | 0 | } |
497 | | |
498 | | const OSSL_DISPATCH ossl_dh_keyexch_functions[] = { |
499 | | { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))dh_newctx }, |
500 | | { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))dh_init }, |
501 | | { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))dh_derive }, |
502 | | { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))dh_set_peer }, |
503 | | { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))dh_freectx }, |
504 | | { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))dh_dupctx }, |
505 | | { OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS, (void (*)(void))dh_set_ctx_params }, |
506 | | { OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS, |
507 | | (void (*)(void))dh_settable_ctx_params }, |
508 | | { OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS, (void (*)(void))dh_get_ctx_params }, |
509 | | { OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS, |
510 | | (void (*)(void))dh_gettable_ctx_params }, |
511 | | { 0, NULL } |
512 | | }; |