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