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