/src/openssl34/crypto/evp/exchange.c
Line | Count | Source |
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 | | #include <openssl/crypto.h> |
11 | | #include <openssl/evp.h> |
12 | | #include <openssl/err.h> |
13 | | #include "internal/cryptlib.h" |
14 | | #include "internal/refcount.h" |
15 | | #include "internal/provider.h" |
16 | | #include "internal/core.h" |
17 | | #include "internal/numbers.h" /* includes SIZE_MAX */ |
18 | | #include "crypto/evp.h" |
19 | | #include "evp_local.h" |
20 | | |
21 | | static EVP_KEYEXCH *evp_keyexch_new(OSSL_PROVIDER *prov) |
22 | 84 | { |
23 | 84 | EVP_KEYEXCH *exchange = OPENSSL_zalloc(sizeof(EVP_KEYEXCH)); |
24 | | |
25 | 84 | if (exchange == NULL) |
26 | 0 | return NULL; |
27 | | |
28 | 84 | if (!CRYPTO_NEW_REF(&exchange->refcnt, 1)) { |
29 | 0 | OPENSSL_free(exchange); |
30 | 0 | return NULL; |
31 | 0 | } |
32 | 84 | exchange->prov = prov; |
33 | 84 | ossl_provider_up_ref(prov); |
34 | | |
35 | 84 | return exchange; |
36 | 84 | } |
37 | | |
38 | | static void *evp_keyexch_from_algorithm(int name_id, |
39 | | const OSSL_ALGORITHM *algodef, |
40 | | OSSL_PROVIDER *prov) |
41 | 161 | { |
42 | 161 | const OSSL_DISPATCH *fns = algodef->implementation; |
43 | 161 | EVP_KEYEXCH *exchange = NULL; |
44 | 161 | int fncnt = 0, sparamfncnt = 0, gparamfncnt = 0; |
45 | | |
46 | 161 | if ((exchange = evp_keyexch_new(prov)) == NULL) { |
47 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
48 | 0 | goto err; |
49 | 0 | } |
50 | | |
51 | 161 | exchange->name_id = name_id; |
52 | 161 | if ((exchange->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) |
53 | 0 | goto err; |
54 | 161 | exchange->description = algodef->algorithm_description; |
55 | | |
56 | 1.58k | for (; fns->function_id != 0; fns++) { |
57 | 1.42k | switch (fns->function_id) { |
58 | 161 | case OSSL_FUNC_KEYEXCH_NEWCTX: |
59 | 161 | if (exchange->newctx != NULL) |
60 | 0 | break; |
61 | 161 | exchange->newctx = OSSL_FUNC_keyexch_newctx(fns); |
62 | 161 | fncnt++; |
63 | 161 | break; |
64 | 161 | case OSSL_FUNC_KEYEXCH_INIT: |
65 | 161 | if (exchange->init != NULL) |
66 | 0 | break; |
67 | 161 | exchange->init = OSSL_FUNC_keyexch_init(fns); |
68 | 161 | fncnt++; |
69 | 161 | break; |
70 | 92 | case OSSL_FUNC_KEYEXCH_SET_PEER: |
71 | 92 | if (exchange->set_peer != NULL) |
72 | 0 | break; |
73 | 92 | exchange->set_peer = OSSL_FUNC_keyexch_set_peer(fns); |
74 | 92 | break; |
75 | 161 | case OSSL_FUNC_KEYEXCH_DERIVE: |
76 | 161 | if (exchange->derive != NULL) |
77 | 0 | break; |
78 | 161 | exchange->derive = OSSL_FUNC_keyexch_derive(fns); |
79 | 161 | fncnt++; |
80 | 161 | break; |
81 | 161 | case OSSL_FUNC_KEYEXCH_FREECTX: |
82 | 161 | if (exchange->freectx != NULL) |
83 | 0 | break; |
84 | 161 | exchange->freectx = OSSL_FUNC_keyexch_freectx(fns); |
85 | 161 | fncnt++; |
86 | 161 | break; |
87 | 161 | case OSSL_FUNC_KEYEXCH_DUPCTX: |
88 | 161 | if (exchange->dupctx != NULL) |
89 | 0 | break; |
90 | 161 | exchange->dupctx = OSSL_FUNC_keyexch_dupctx(fns); |
91 | 161 | break; |
92 | 147 | case OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS: |
93 | 147 | if (exchange->get_ctx_params != NULL) |
94 | 0 | break; |
95 | 147 | exchange->get_ctx_params = OSSL_FUNC_keyexch_get_ctx_params(fns); |
96 | 147 | gparamfncnt++; |
97 | 147 | break; |
98 | 147 | case OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS: |
99 | 147 | if (exchange->gettable_ctx_params != NULL) |
100 | 0 | break; |
101 | 147 | exchange->gettable_ctx_params |
102 | 147 | = OSSL_FUNC_keyexch_gettable_ctx_params(fns); |
103 | 147 | gparamfncnt++; |
104 | 147 | break; |
105 | 115 | case OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS: |
106 | 115 | if (exchange->set_ctx_params != NULL) |
107 | 0 | break; |
108 | 115 | exchange->set_ctx_params = OSSL_FUNC_keyexch_set_ctx_params(fns); |
109 | 115 | sparamfncnt++; |
110 | 115 | break; |
111 | 115 | case OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS: |
112 | 115 | if (exchange->settable_ctx_params != NULL) |
113 | 0 | break; |
114 | 115 | exchange->settable_ctx_params |
115 | 115 | = OSSL_FUNC_keyexch_settable_ctx_params(fns); |
116 | 115 | sparamfncnt++; |
117 | 115 | break; |
118 | 1.42k | } |
119 | 1.42k | } |
120 | 161 | if (fncnt != 4 |
121 | 161 | || (gparamfncnt != 0 && gparamfncnt != 2) |
122 | 161 | || (sparamfncnt != 0 && sparamfncnt != 2)) { |
123 | | /* |
124 | | * In order to be a consistent set of functions we must have at least |
125 | | * a complete set of "exchange" functions: init, derive, newctx, |
126 | | * and freectx. The set_ctx_params and settable_ctx_params functions are |
127 | | * optional, but if one of them is present then the other one must also |
128 | | * be present. Same goes for get_ctx_params and gettable_ctx_params. |
129 | | * The dupctx and set_peer functions are optional. |
130 | | */ |
131 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS); |
132 | 0 | goto err; |
133 | 0 | } |
134 | | |
135 | 161 | return exchange; |
136 | | |
137 | 0 | err: |
138 | 0 | EVP_KEYEXCH_free(exchange); |
139 | 0 | return NULL; |
140 | 161 | } |
141 | | |
142 | | void EVP_KEYEXCH_free(EVP_KEYEXCH *exchange) |
143 | 345k | { |
144 | 345k | int i; |
145 | | |
146 | 345k | if (exchange == NULL) |
147 | 34.5k | return; |
148 | 310k | CRYPTO_DOWN_REF(&exchange->refcnt, &i); |
149 | 310k | if (i > 0) |
150 | 310k | return; |
151 | 182 | OPENSSL_free(exchange->type_name); |
152 | 182 | ossl_provider_free(exchange->prov); |
153 | 182 | CRYPTO_FREE_REF(&exchange->refcnt); |
154 | 182 | OPENSSL_free(exchange); |
155 | 182 | } |
156 | | |
157 | | int EVP_KEYEXCH_up_ref(EVP_KEYEXCH *exchange) |
158 | 310k | { |
159 | 310k | int ref = 0; |
160 | | |
161 | 310k | CRYPTO_UP_REF(&exchange->refcnt, &ref); |
162 | 310k | return 1; |
163 | 310k | } |
164 | | |
165 | | OSSL_PROVIDER *EVP_KEYEXCH_get0_provider(const EVP_KEYEXCH *exchange) |
166 | 83.9k | { |
167 | 83.9k | return exchange->prov; |
168 | 83.9k | } |
169 | | |
170 | | EVP_KEYEXCH *EVP_KEYEXCH_fetch(OSSL_LIB_CTX *ctx, const char *algorithm, |
171 | | const char *properties) |
172 | 375k | { |
173 | 375k | return evp_generic_fetch(ctx, OSSL_OP_KEYEXCH, algorithm, properties, |
174 | 375k | evp_keyexch_from_algorithm, |
175 | 375k | (int (*)(void *))EVP_KEYEXCH_up_ref, |
176 | 375k | (void (*)(void *))EVP_KEYEXCH_free); |
177 | 375k | } |
178 | | |
179 | | EVP_KEYEXCH *evp_keyexch_fetch_from_prov(OSSL_PROVIDER *prov, |
180 | | const char *algorithm, |
181 | | const char *properties) |
182 | 102 | { |
183 | 102 | return evp_generic_fetch_from_prov(prov, OSSL_OP_KEYEXCH, |
184 | 102 | algorithm, properties, |
185 | 102 | evp_keyexch_from_algorithm, |
186 | 102 | (int (*)(void *))EVP_KEYEXCH_up_ref, |
187 | 102 | (void (*)(void *))EVP_KEYEXCH_free); |
188 | 102 | } |
189 | | |
190 | | int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx) |
191 | 42.1k | { |
192 | 42.1k | return EVP_PKEY_derive_init_ex(ctx, NULL); |
193 | 42.1k | } |
194 | | |
195 | | int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]) |
196 | 34.4k | { |
197 | 34.4k | int ret; |
198 | 34.4k | void *provkey = NULL; |
199 | 34.4k | EVP_KEYEXCH *exchange = NULL; |
200 | 34.4k | EVP_KEYMGMT *tmp_keymgmt = NULL; |
201 | 34.4k | const OSSL_PROVIDER *tmp_prov = NULL; |
202 | 34.4k | const char *supported_exch = NULL; |
203 | 34.4k | int iter; |
204 | | |
205 | 34.4k | if (ctx == NULL) { |
206 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
207 | 0 | return -2; |
208 | 0 | } |
209 | | |
210 | 34.4k | evp_pkey_ctx_free_old_ops(ctx); |
211 | 34.4k | ctx->operation = EVP_PKEY_OP_DERIVE; |
212 | | |
213 | 34.4k | ERR_set_mark(); |
214 | | |
215 | 34.4k | if (evp_pkey_ctx_is_legacy(ctx)) |
216 | 0 | goto legacy; |
217 | | |
218 | | /* |
219 | | * Some algorithms (e.g. legacy KDFs) don't have a pkey - so we create |
220 | | * a blank one. |
221 | | */ |
222 | 34.4k | if (ctx->pkey == NULL) { |
223 | 0 | EVP_PKEY *pkey = EVP_PKEY_new(); |
224 | |
|
225 | 0 | if (pkey == NULL |
226 | 0 | || !EVP_PKEY_set_type_by_keymgmt(pkey, ctx->keymgmt) |
227 | 0 | || (pkey->keydata = evp_keymgmt_newdata(ctx->keymgmt)) == NULL) { |
228 | 0 | ERR_clear_last_mark(); |
229 | 0 | EVP_PKEY_free(pkey); |
230 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
231 | 0 | goto err; |
232 | 0 | } |
233 | 0 | ctx->pkey = pkey; |
234 | 0 | } |
235 | | |
236 | | /* |
237 | | * Try to derive the supported exch from |ctx->keymgmt|. |
238 | | */ |
239 | 34.4k | if (!ossl_assert(ctx->pkey->keymgmt == NULL |
240 | 34.4k | || ctx->pkey->keymgmt == ctx->keymgmt)) { |
241 | 0 | ERR_clear_last_mark(); |
242 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
243 | 0 | goto err; |
244 | 0 | } |
245 | 34.4k | supported_exch = evp_keymgmt_util_query_operation_name(ctx->keymgmt, |
246 | 34.4k | OSSL_OP_KEYEXCH); |
247 | 34.4k | if (supported_exch == NULL) { |
248 | 0 | ERR_clear_last_mark(); |
249 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
250 | 0 | goto err; |
251 | 0 | } |
252 | | |
253 | | /* |
254 | | * We perform two iterations: |
255 | | * |
256 | | * 1. Do the normal exchange fetch, using the fetching data given by |
257 | | * the EVP_PKEY_CTX. |
258 | | * 2. Do the provider specific exchange fetch, from the same provider |
259 | | * as |ctx->keymgmt| |
260 | | * |
261 | | * We then try to fetch the keymgmt from the same provider as the |
262 | | * exchange, and try to export |ctx->pkey| to that keymgmt (when |
263 | | * this keymgmt happens to be the same as |ctx->keymgmt|, the export |
264 | | * is a no-op, but we call it anyway to not complicate the code even |
265 | | * more). |
266 | | * If the export call succeeds (returns a non-NULL provider key pointer), |
267 | | * we're done and can perform the operation itself. If not, we perform |
268 | | * the second iteration, or jump to legacy. |
269 | | */ |
270 | 68.8k | for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) { |
271 | 34.4k | EVP_KEYMGMT *tmp_keymgmt_tofree = NULL; |
272 | | |
273 | | /* |
274 | | * If we're on the second iteration, free the results from the first. |
275 | | * They are NULL on the first iteration, so no need to check what |
276 | | * iteration we're on. |
277 | | */ |
278 | 34.4k | EVP_KEYEXCH_free(exchange); |
279 | 34.4k | exchange = NULL; |
280 | 34.4k | EVP_KEYMGMT_free(tmp_keymgmt); |
281 | 34.4k | tmp_keymgmt = NULL; |
282 | | |
283 | 34.4k | switch (iter) { |
284 | 34.4k | case 1: |
285 | 34.4k | exchange = EVP_KEYEXCH_fetch(ctx->libctx, supported_exch, ctx->propquery); |
286 | 34.4k | if (exchange != NULL) |
287 | 34.3k | tmp_prov = EVP_KEYEXCH_get0_provider(exchange); |
288 | 34.4k | break; |
289 | 70 | case 2: |
290 | 70 | tmp_prov = EVP_KEYMGMT_get0_provider(ctx->keymgmt); |
291 | 70 | exchange = evp_keyexch_fetch_from_prov((OSSL_PROVIDER *)tmp_prov, |
292 | 70 | supported_exch, ctx->propquery); |
293 | 70 | if (exchange == NULL) |
294 | 70 | goto legacy; |
295 | 0 | break; |
296 | 34.4k | } |
297 | 34.4k | if (exchange == NULL) |
298 | 70 | continue; |
299 | | |
300 | | /* |
301 | | * Ensure that the key is provided, either natively, or as a cached |
302 | | * export. We start by fetching the keymgmt with the same name as |
303 | | * |ctx->keymgmt|, but from the provider of the exchange method, using |
304 | | * the same property query as when fetching the exchange method. |
305 | | * With the keymgmt we found (if we did), we try to export |ctx->pkey| |
306 | | * to it (evp_pkey_export_to_provider() is smart enough to only actually |
307 | | * export it if |tmp_keymgmt| is different from |ctx->pkey|'s keymgmt) |
308 | | */ |
309 | 34.3k | tmp_keymgmt_tofree = tmp_keymgmt = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov, |
310 | 34.3k | EVP_KEYMGMT_get0_name(ctx->keymgmt), |
311 | 34.3k | ctx->propquery); |
312 | 34.3k | if (tmp_keymgmt != NULL) |
313 | 34.3k | provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx, |
314 | 34.3k | &tmp_keymgmt, ctx->propquery); |
315 | 34.3k | if (tmp_keymgmt == NULL) |
316 | 0 | EVP_KEYMGMT_free(tmp_keymgmt_tofree); |
317 | 34.3k | } |
318 | | |
319 | 34.3k | if (provkey == NULL) { |
320 | 0 | EVP_KEYEXCH_free(exchange); |
321 | 0 | goto legacy; |
322 | 0 | } |
323 | | |
324 | 34.3k | ERR_pop_to_mark(); |
325 | | |
326 | | /* No more legacy from here down to legacy: */ |
327 | | |
328 | | /* A Coverity false positive with up_ref/down_ref and free */ |
329 | | /* coverity[use_after_free] */ |
330 | 34.3k | ctx->op.kex.exchange = exchange; |
331 | | /* A Coverity false positive with up_ref/down_ref and free */ |
332 | | /* coverity[deref_arg] */ |
333 | 34.3k | ctx->op.kex.algctx = exchange->newctx(ossl_provider_ctx(exchange->prov)); |
334 | 34.3k | if (ctx->op.kex.algctx == NULL) { |
335 | | /* The provider key can stay in the cache */ |
336 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
337 | 0 | goto err; |
338 | 0 | } |
339 | 34.3k | ret = exchange->init(ctx->op.kex.algctx, provkey, params); |
340 | | |
341 | 34.3k | EVP_KEYMGMT_free(tmp_keymgmt); |
342 | 34.3k | return ret ? 1 : 0; |
343 | 0 | err: |
344 | 0 | evp_pkey_ctx_free_old_ops(ctx); |
345 | 0 | ctx->operation = EVP_PKEY_OP_UNDEFINED; |
346 | 0 | EVP_KEYMGMT_free(tmp_keymgmt); |
347 | 0 | return 0; |
348 | | |
349 | 70 | legacy: |
350 | | /* |
351 | | * If we don't have the full support we need with provided methods, |
352 | | * let's go see if legacy does. |
353 | | */ |
354 | 70 | ERR_pop_to_mark(); |
355 | | |
356 | | #ifdef FIPS_MODULE |
357 | | return 0; |
358 | | #else |
359 | 70 | if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) { |
360 | 70 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
361 | 70 | return -2; |
362 | 70 | } |
363 | | |
364 | 0 | if (ctx->pmeth->derive_init == NULL) |
365 | 0 | return 1; |
366 | 0 | ret = ctx->pmeth->derive_init(ctx); |
367 | 0 | if (ret <= 0) |
368 | 0 | ctx->operation = EVP_PKEY_OP_UNDEFINED; |
369 | 0 | EVP_KEYMGMT_free(tmp_keymgmt); |
370 | 0 | return ret; |
371 | 0 | #endif |
372 | 0 | } |
373 | | |
374 | | int EVP_PKEY_derive_set_peer_ex(EVP_PKEY_CTX *ctx, EVP_PKEY *peer, |
375 | | int validate_peer) |
376 | 8.08k | { |
377 | 8.08k | int ret = 0, check; |
378 | 8.08k | void *provkey = NULL; |
379 | 8.08k | EVP_PKEY_CTX *check_ctx = NULL; |
380 | 8.08k | EVP_KEYMGMT *tmp_keymgmt = NULL, *tmp_keymgmt_tofree = NULL; |
381 | | |
382 | 8.08k | if (ctx == NULL) { |
383 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
384 | 0 | return -1; |
385 | 0 | } |
386 | | |
387 | 8.08k | if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx) || ctx->op.kex.algctx == NULL) |
388 | 0 | goto legacy; |
389 | | |
390 | 8.08k | if (ctx->op.kex.exchange->set_peer == NULL) { |
391 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
392 | 0 | return -2; |
393 | 0 | } |
394 | | |
395 | 8.08k | if (validate_peer) { |
396 | 8.08k | check_ctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, peer, ctx->propquery); |
397 | 8.08k | if (check_ctx == NULL) |
398 | 0 | return -1; |
399 | 8.08k | check = EVP_PKEY_public_check(check_ctx); |
400 | 8.08k | EVP_PKEY_CTX_free(check_ctx); |
401 | 8.08k | if (check <= 0) |
402 | 4 | return -1; |
403 | 8.08k | } |
404 | | |
405 | | /* |
406 | | * Ensure that the |peer| is provided, either natively, or as a cached |
407 | | * export. We start by fetching the keymgmt with the same name as |
408 | | * |ctx->keymgmt|, but from the provider of the exchange method, using |
409 | | * the same property query as when fetching the exchange method. |
410 | | * With the keymgmt we found (if we did), we try to export |peer| |
411 | | * to it (evp_pkey_export_to_provider() is smart enough to only actually |
412 | | * export it if |tmp_keymgmt| is different from |peer|'s keymgmt) |
413 | | */ |
414 | 8.08k | tmp_keymgmt_tofree = tmp_keymgmt = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *) |
415 | 8.08k | EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange), |
416 | 8.08k | EVP_KEYMGMT_get0_name(ctx->keymgmt), |
417 | 8.08k | ctx->propquery); |
418 | 8.08k | if (tmp_keymgmt != NULL) |
419 | | /* A Coverity issue with up_ref/down_ref and free */ |
420 | | /* coverity[pass_freed_arg] */ |
421 | 8.08k | provkey = evp_pkey_export_to_provider(peer, ctx->libctx, |
422 | 8.08k | &tmp_keymgmt, ctx->propquery); |
423 | 8.08k | EVP_KEYMGMT_free(tmp_keymgmt_tofree); |
424 | | |
425 | | /* |
426 | | * If making the key provided wasn't possible, legacy may be able to pick |
427 | | * it up |
428 | | */ |
429 | 8.08k | if (provkey == NULL) |
430 | 0 | goto legacy; |
431 | 8.08k | ret = ctx->op.kex.exchange->set_peer(ctx->op.kex.algctx, provkey); |
432 | 8.08k | if (ret <= 0) |
433 | 0 | return ret; |
434 | 8.08k | EVP_PKEY_free(ctx->peerkey); |
435 | 8.08k | ctx->peerkey = peer; |
436 | 8.08k | EVP_PKEY_up_ref(peer); |
437 | 8.08k | return 1; |
438 | | |
439 | 0 | legacy: |
440 | | #ifdef FIPS_MODULE |
441 | | return ret; |
442 | | #else |
443 | 0 | if (ctx->pmeth == NULL |
444 | 0 | || !(ctx->pmeth->derive != NULL |
445 | 0 | || ctx->pmeth->encrypt != NULL |
446 | 0 | || ctx->pmeth->decrypt != NULL) |
447 | 0 | || ctx->pmeth->ctrl == NULL) { |
448 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
449 | 0 | return -2; |
450 | 0 | } |
451 | 0 | if (ctx->operation != EVP_PKEY_OP_DERIVE |
452 | 0 | && ctx->operation != EVP_PKEY_OP_ENCRYPT |
453 | 0 | && ctx->operation != EVP_PKEY_OP_DECRYPT) { |
454 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED); |
455 | 0 | return -1; |
456 | 0 | } |
457 | | |
458 | 0 | ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer); |
459 | |
|
460 | 0 | if (ret <= 0) |
461 | 0 | return ret; |
462 | | |
463 | 0 | if (ret == 2) |
464 | 0 | return 1; |
465 | | |
466 | 0 | if (ctx->pkey == NULL) { |
467 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET); |
468 | 0 | return -1; |
469 | 0 | } |
470 | | |
471 | 0 | if (ctx->pkey->type != peer->type) { |
472 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES); |
473 | 0 | return -1; |
474 | 0 | } |
475 | | |
476 | | /* |
477 | | * For clarity. The error is if parameters in peer are |
478 | | * present (!missing) but don't match. EVP_PKEY_parameters_eq may return |
479 | | * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1 |
480 | | * (different key types) is impossible here because it is checked earlier. |
481 | | * -2 is OK for us here, as well as 1, so we can check for 0 only. |
482 | | */ |
483 | 0 | if (!EVP_PKEY_missing_parameters(peer) && !EVP_PKEY_parameters_eq(ctx->pkey, peer)) { |
484 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_PARAMETERS); |
485 | 0 | return -1; |
486 | 0 | } |
487 | | |
488 | 0 | EVP_PKEY_free(ctx->peerkey); |
489 | 0 | ctx->peerkey = peer; |
490 | |
|
491 | 0 | ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer); |
492 | |
|
493 | 0 | if (ret <= 0) { |
494 | 0 | ctx->peerkey = NULL; |
495 | 0 | return ret; |
496 | 0 | } |
497 | | |
498 | 0 | EVP_PKEY_up_ref(peer); |
499 | 0 | return 1; |
500 | 0 | #endif |
501 | 0 | } |
502 | | |
503 | | int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer) |
504 | 42.1k | { |
505 | 42.1k | return EVP_PKEY_derive_set_peer_ex(ctx, peer, 1); |
506 | 42.1k | } |
507 | | |
508 | | int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen) |
509 | 45.4k | { |
510 | 45.4k | int ret; |
511 | | |
512 | 45.4k | if (ctx == NULL || pkeylen == NULL) { |
513 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
514 | 0 | return -1; |
515 | 0 | } |
516 | | |
517 | 45.4k | if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { |
518 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED); |
519 | 0 | return -1; |
520 | 0 | } |
521 | | |
522 | 45.4k | if (ctx->op.kex.algctx == NULL) |
523 | 70 | goto legacy; |
524 | | |
525 | 45.4k | ret = ctx->op.kex.exchange->derive(ctx->op.kex.algctx, key, pkeylen, |
526 | 45.4k | key != NULL ? *pkeylen : 0); |
527 | | |
528 | 45.4k | return ret; |
529 | 70 | legacy: |
530 | 70 | if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) { |
531 | 70 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
532 | 70 | return -2; |
533 | 70 | } |
534 | | |
535 | 0 | M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE) return ctx->pmeth->derive(ctx, key, pkeylen); |
536 | 0 | } |
537 | | |
538 | | int evp_keyexch_get_number(const EVP_KEYEXCH *keyexch) |
539 | 0 | { |
540 | 0 | return keyexch->name_id; |
541 | 0 | } |
542 | | |
543 | | const char *EVP_KEYEXCH_get0_name(const EVP_KEYEXCH *keyexch) |
544 | 0 | { |
545 | 0 | return keyexch->type_name; |
546 | 0 | } |
547 | | |
548 | | const char *EVP_KEYEXCH_get0_description(const EVP_KEYEXCH *keyexch) |
549 | 0 | { |
550 | 0 | return keyexch->description; |
551 | 0 | } |
552 | | |
553 | | int EVP_KEYEXCH_is_a(const EVP_KEYEXCH *keyexch, const char *name) |
554 | 0 | { |
555 | 0 | return keyexch != NULL |
556 | 0 | && evp_is_a(keyexch->prov, keyexch->name_id, NULL, name); |
557 | 0 | } |
558 | | |
559 | | void EVP_KEYEXCH_do_all_provided(OSSL_LIB_CTX *libctx, |
560 | | void (*fn)(EVP_KEYEXCH *keyexch, void *arg), |
561 | | void *arg) |
562 | 8 | { |
563 | 8 | evp_generic_do_all(libctx, OSSL_OP_KEYEXCH, |
564 | 8 | (void (*)(void *, void *))fn, arg, |
565 | 8 | evp_keyexch_from_algorithm, |
566 | 8 | (int (*)(void *))EVP_KEYEXCH_up_ref, |
567 | 8 | (void (*)(void *))EVP_KEYEXCH_free); |
568 | 8 | } |
569 | | |
570 | | int EVP_KEYEXCH_names_do_all(const EVP_KEYEXCH *keyexch, |
571 | | void (*fn)(const char *name, void *data), |
572 | | void *data) |
573 | 0 | { |
574 | 0 | if (keyexch->prov != NULL) |
575 | 0 | return evp_names_do_all(keyexch->prov, keyexch->name_id, fn, data); |
576 | | |
577 | 0 | return 1; |
578 | 0 | } |
579 | | |
580 | | const OSSL_PARAM *EVP_KEYEXCH_gettable_ctx_params(const EVP_KEYEXCH *keyexch) |
581 | 0 | { |
582 | 0 | void *provctx; |
583 | |
|
584 | 0 | if (keyexch == NULL || keyexch->gettable_ctx_params == NULL) |
585 | 0 | return NULL; |
586 | | |
587 | 0 | provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(keyexch)); |
588 | 0 | return keyexch->gettable_ctx_params(NULL, provctx); |
589 | 0 | } |
590 | | |
591 | | const OSSL_PARAM *EVP_KEYEXCH_settable_ctx_params(const EVP_KEYEXCH *keyexch) |
592 | 83 | { |
593 | 83 | void *provctx; |
594 | | |
595 | 83 | if (keyexch == NULL || keyexch->settable_ctx_params == NULL) |
596 | 4 | return NULL; |
597 | 79 | provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(keyexch)); |
598 | | return keyexch->settable_ctx_params(NULL, provctx); |
599 | 83 | } |