/src/openssl33/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 | 411k | { |
144 | 411k | int i; |
145 | | |
146 | 411k | if (exchange == NULL) |
147 | 41.9k | return; |
148 | 369k | CRYPTO_DOWN_REF(&exchange->refcnt, &i); |
149 | 369k | if (i > 0) |
150 | 369k | 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 | 369k | { |
159 | 369k | int ref = 0; |
160 | | |
161 | 369k | CRYPTO_UP_REF(&exchange->refcnt, &ref); |
162 | 369k | return 1; |
163 | 369k | } |
164 | | |
165 | | OSSL_PROVIDER *EVP_KEYEXCH_get0_provider(const EVP_KEYEXCH *exchange) |
166 | 83.3k | { |
167 | 83.3k | return exchange->prov; |
168 | 83.3k | } |
169 | | |
170 | | EVP_KEYEXCH *EVP_KEYEXCH_fetch(OSSL_LIB_CTX *ctx, const char *algorithm, |
171 | | const char *properties) |
172 | 368k | { |
173 | 368k | return evp_generic_fetch(ctx, OSSL_OP_KEYEXCH, algorithm, properties, |
174 | 368k | evp_keyexch_from_algorithm, |
175 | 368k | (int (*)(void *))EVP_KEYEXCH_up_ref, |
176 | 368k | (void (*)(void *))EVP_KEYEXCH_free); |
177 | 368k | } |
178 | | |
179 | | EVP_KEYEXCH *evp_keyexch_fetch_from_prov(OSSL_PROVIDER *prov, |
180 | | const char *algorithm, |
181 | | const char *properties) |
182 | 72 | { |
183 | 72 | return evp_generic_fetch_from_prov(prov, OSSL_OP_KEYEXCH, |
184 | 72 | algorithm, properties, |
185 | 72 | evp_keyexch_from_algorithm, |
186 | 72 | (int (*)(void *))EVP_KEYEXCH_up_ref, |
187 | 72 | (void (*)(void *))EVP_KEYEXCH_free); |
188 | 72 | } |
189 | | |
190 | | int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx) |
191 | 41.7k | { |
192 | 41.7k | return EVP_PKEY_derive_init_ex(ctx, NULL); |
193 | 41.7k | } |
194 | | |
195 | | int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]) |
196 | 33.6k | { |
197 | 33.6k | int ret; |
198 | 33.6k | void *provkey = NULL; |
199 | 33.6k | EVP_KEYEXCH *exchange = NULL; |
200 | 33.6k | EVP_KEYMGMT *tmp_keymgmt = NULL; |
201 | 33.6k | const OSSL_PROVIDER *tmp_prov = NULL; |
202 | 33.6k | const char *supported_exch = NULL; |
203 | 33.6k | int iter; |
204 | | |
205 | 33.6k | if (ctx == NULL) { |
206 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
207 | 0 | return -2; |
208 | 0 | } |
209 | | |
210 | 33.6k | evp_pkey_ctx_free_old_ops(ctx); |
211 | 33.6k | ctx->operation = EVP_PKEY_OP_DERIVE; |
212 | | |
213 | 33.6k | ERR_set_mark(); |
214 | | |
215 | 33.6k | 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 | 33.6k | 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 | 33.6k | if (!ossl_assert(ctx->pkey->keymgmt == NULL |
240 | 33.6k | || 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 | 33.6k | supported_exch = evp_keymgmt_util_query_operation_name(ctx->keymgmt, |
246 | 33.6k | OSSL_OP_KEYEXCH); |
247 | 33.6k | 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 | 67.3k | for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) { |
271 | 33.7k | 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 | 33.7k | EVP_KEYEXCH_free(exchange); |
279 | 33.7k | EVP_KEYMGMT_free(tmp_keymgmt); |
280 | | |
281 | 33.7k | switch (iter) { |
282 | 33.6k | case 1: |
283 | 33.6k | exchange = EVP_KEYEXCH_fetch(ctx->libctx, supported_exch, ctx->propquery); |
284 | 33.6k | if (exchange != NULL) |
285 | 33.6k | tmp_prov = EVP_KEYEXCH_get0_provider(exchange); |
286 | 33.6k | break; |
287 | 42 | case 2: |
288 | 42 | tmp_prov = EVP_KEYMGMT_get0_provider(ctx->keymgmt); |
289 | 42 | exchange = evp_keyexch_fetch_from_prov((OSSL_PROVIDER *)tmp_prov, |
290 | 42 | supported_exch, ctx->propquery); |
291 | 42 | if (exchange == NULL) |
292 | 42 | goto legacy; |
293 | 0 | break; |
294 | 33.7k | } |
295 | 33.6k | if (exchange == NULL) |
296 | 42 | continue; |
297 | | |
298 | | /* |
299 | | * Ensure that the key is provided, either natively, or as a cached |
300 | | * export. We start by fetching the keymgmt with the same name as |
301 | | * |ctx->keymgmt|, but from the provider of the exchange method, using |
302 | | * the same property query as when fetching the exchange method. |
303 | | * With the keymgmt we found (if we did), we try to export |ctx->pkey| |
304 | | * to it (evp_pkey_export_to_provider() is smart enough to only actually |
305 | | * export it if |tmp_keymgmt| is different from |ctx->pkey|'s keymgmt) |
306 | | */ |
307 | 33.6k | tmp_keymgmt_tofree = tmp_keymgmt = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov, |
308 | 33.6k | EVP_KEYMGMT_get0_name(ctx->keymgmt), |
309 | 33.6k | ctx->propquery); |
310 | 33.6k | if (tmp_keymgmt != NULL) |
311 | 33.6k | provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx, |
312 | 33.6k | &tmp_keymgmt, ctx->propquery); |
313 | 33.6k | if (tmp_keymgmt == NULL) |
314 | 0 | EVP_KEYMGMT_free(tmp_keymgmt_tofree); |
315 | 33.6k | } |
316 | | |
317 | 33.6k | if (provkey == NULL) { |
318 | 0 | EVP_KEYEXCH_free(exchange); |
319 | 0 | goto legacy; |
320 | 0 | } |
321 | | |
322 | 33.6k | ERR_pop_to_mark(); |
323 | | |
324 | | /* No more legacy from here down to legacy: */ |
325 | | |
326 | | /* A Coverity false positive with up_ref/down_ref and free */ |
327 | | /* coverity[use_after_free] */ |
328 | 33.6k | ctx->op.kex.exchange = exchange; |
329 | | /* A Coverity false positive with up_ref/down_ref and free */ |
330 | | /* coverity[deref_arg] */ |
331 | 33.6k | ctx->op.kex.algctx = exchange->newctx(ossl_provider_ctx(exchange->prov)); |
332 | 33.6k | if (ctx->op.kex.algctx == NULL) { |
333 | | /* The provider key can stay in the cache */ |
334 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
335 | 0 | goto err; |
336 | 0 | } |
337 | 33.6k | ret = exchange->init(ctx->op.kex.algctx, provkey, params); |
338 | | |
339 | 33.6k | EVP_KEYMGMT_free(tmp_keymgmt); |
340 | 33.6k | return ret ? 1 : 0; |
341 | 0 | err: |
342 | 0 | evp_pkey_ctx_free_old_ops(ctx); |
343 | 0 | ctx->operation = EVP_PKEY_OP_UNDEFINED; |
344 | 0 | EVP_KEYMGMT_free(tmp_keymgmt); |
345 | 0 | return 0; |
346 | | |
347 | 42 | legacy: |
348 | | /* |
349 | | * If we don't have the full support we need with provided methods, |
350 | | * let's go see if legacy does. |
351 | | */ |
352 | 42 | ERR_pop_to_mark(); |
353 | | |
354 | | #ifdef FIPS_MODULE |
355 | | return 0; |
356 | | #else |
357 | 42 | if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) { |
358 | 42 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
359 | 42 | return -2; |
360 | 42 | } |
361 | | |
362 | 0 | if (ctx->pmeth->derive_init == NULL) |
363 | 0 | return 1; |
364 | 0 | ret = ctx->pmeth->derive_init(ctx); |
365 | 0 | if (ret <= 0) |
366 | 0 | ctx->operation = EVP_PKEY_OP_UNDEFINED; |
367 | 0 | EVP_KEYMGMT_free(tmp_keymgmt); |
368 | 0 | return ret; |
369 | 0 | #endif |
370 | 0 | } |
371 | | |
372 | | int EVP_PKEY_derive_set_peer_ex(EVP_PKEY_CTX *ctx, EVP_PKEY *peer, |
373 | | int validate_peer) |
374 | 10.8k | { |
375 | 10.8k | int ret = 0, check; |
376 | 10.8k | void *provkey = NULL; |
377 | 10.8k | EVP_PKEY_CTX *check_ctx = NULL; |
378 | 10.8k | EVP_KEYMGMT *tmp_keymgmt = NULL, *tmp_keymgmt_tofree = NULL; |
379 | | |
380 | 10.8k | if (ctx == NULL) { |
381 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
382 | 0 | return -1; |
383 | 0 | } |
384 | | |
385 | 10.8k | if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx) || ctx->op.kex.algctx == NULL) |
386 | 0 | goto legacy; |
387 | | |
388 | 10.8k | if (ctx->op.kex.exchange->set_peer == NULL) { |
389 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
390 | 0 | return -2; |
391 | 0 | } |
392 | | |
393 | 10.8k | if (validate_peer) { |
394 | 10.8k | check_ctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, peer, ctx->propquery); |
395 | 10.8k | if (check_ctx == NULL) |
396 | 0 | return -1; |
397 | 10.8k | check = EVP_PKEY_public_check(check_ctx); |
398 | 10.8k | EVP_PKEY_CTX_free(check_ctx); |
399 | 10.8k | if (check <= 0) |
400 | 332 | return -1; |
401 | 10.8k | } |
402 | | |
403 | | /* |
404 | | * Ensure that the |peer| is provided, either natively, or as a cached |
405 | | * export. We start by fetching the keymgmt with the same name as |
406 | | * |ctx->keymgmt|, but from the provider of the exchange method, using |
407 | | * the same property query as when fetching the exchange method. |
408 | | * With the keymgmt we found (if we did), we try to export |peer| |
409 | | * to it (evp_pkey_export_to_provider() is smart enough to only actually |
410 | | * export it if |tmp_keymgmt| is different from |peer|'s keymgmt) |
411 | | */ |
412 | 10.5k | tmp_keymgmt_tofree = tmp_keymgmt = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *) |
413 | 10.5k | EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange), |
414 | 10.5k | EVP_KEYMGMT_get0_name(ctx->keymgmt), |
415 | 10.5k | ctx->propquery); |
416 | 10.5k | if (tmp_keymgmt != NULL) |
417 | | /* A Coverity issue with up_ref/down_ref and free */ |
418 | | /* coverity[pass_freed_arg] */ |
419 | 10.5k | provkey = evp_pkey_export_to_provider(peer, ctx->libctx, |
420 | 10.5k | &tmp_keymgmt, ctx->propquery); |
421 | 10.5k | EVP_KEYMGMT_free(tmp_keymgmt_tofree); |
422 | | |
423 | | /* |
424 | | * If making the key provided wasn't possible, legacy may be able to pick |
425 | | * it up |
426 | | */ |
427 | 10.5k | if (provkey == NULL) |
428 | 0 | goto legacy; |
429 | 10.5k | ret = ctx->op.kex.exchange->set_peer(ctx->op.kex.algctx, provkey); |
430 | 10.5k | if (ret <= 0) |
431 | 0 | return ret; |
432 | 10.5k | EVP_PKEY_free(ctx->peerkey); |
433 | 10.5k | ctx->peerkey = peer; |
434 | 10.5k | EVP_PKEY_up_ref(peer); |
435 | 10.5k | return 1; |
436 | | |
437 | 0 | legacy: |
438 | | #ifdef FIPS_MODULE |
439 | | return ret; |
440 | | #else |
441 | 0 | if (ctx->pmeth == NULL |
442 | 0 | || !(ctx->pmeth->derive != NULL |
443 | 0 | || ctx->pmeth->encrypt != NULL |
444 | 0 | || ctx->pmeth->decrypt != NULL) |
445 | 0 | || ctx->pmeth->ctrl == NULL) { |
446 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
447 | 0 | return -2; |
448 | 0 | } |
449 | 0 | if (ctx->operation != EVP_PKEY_OP_DERIVE |
450 | 0 | && ctx->operation != EVP_PKEY_OP_ENCRYPT |
451 | 0 | && ctx->operation != EVP_PKEY_OP_DECRYPT) { |
452 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED); |
453 | 0 | return -1; |
454 | 0 | } |
455 | | |
456 | 0 | ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer); |
457 | |
|
458 | 0 | if (ret <= 0) |
459 | 0 | return ret; |
460 | | |
461 | 0 | if (ret == 2) |
462 | 0 | return 1; |
463 | | |
464 | 0 | if (ctx->pkey == NULL) { |
465 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET); |
466 | 0 | return -1; |
467 | 0 | } |
468 | | |
469 | 0 | if (ctx->pkey->type != peer->type) { |
470 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES); |
471 | 0 | return -1; |
472 | 0 | } |
473 | | |
474 | | /* |
475 | | * For clarity. The error is if parameters in peer are |
476 | | * present (!missing) but don't match. EVP_PKEY_parameters_eq may return |
477 | | * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1 |
478 | | * (different key types) is impossible here because it is checked earlier. |
479 | | * -2 is OK for us here, as well as 1, so we can check for 0 only. |
480 | | */ |
481 | 0 | if (!EVP_PKEY_missing_parameters(peer) && !EVP_PKEY_parameters_eq(ctx->pkey, peer)) { |
482 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_PARAMETERS); |
483 | 0 | return -1; |
484 | 0 | } |
485 | | |
486 | 0 | EVP_PKEY_free(ctx->peerkey); |
487 | 0 | ctx->peerkey = peer; |
488 | |
|
489 | 0 | ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer); |
490 | |
|
491 | 0 | if (ret <= 0) { |
492 | 0 | ctx->peerkey = NULL; |
493 | 0 | return ret; |
494 | 0 | } |
495 | | |
496 | 0 | EVP_PKEY_up_ref(peer); |
497 | 0 | return 1; |
498 | 0 | #endif |
499 | 0 | } |
500 | | |
501 | | int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer) |
502 | 41.7k | { |
503 | 41.7k | return EVP_PKEY_derive_set_peer_ex(ctx, peer, 1); |
504 | 41.7k | } |
505 | | |
506 | | int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen) |
507 | 21.0k | { |
508 | 21.0k | int ret; |
509 | | |
510 | 21.0k | if (ctx == NULL || pkeylen == NULL) { |
511 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
512 | 0 | return -1; |
513 | 0 | } |
514 | | |
515 | 21.0k | if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { |
516 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED); |
517 | 0 | return -1; |
518 | 0 | } |
519 | | |
520 | 21.0k | if (ctx->op.kex.algctx == NULL) |
521 | 0 | goto legacy; |
522 | | |
523 | 21.0k | ret = ctx->op.kex.exchange->derive(ctx->op.kex.algctx, key, pkeylen, |
524 | 21.0k | key != NULL ? *pkeylen : 0); |
525 | | |
526 | 21.0k | return ret; |
527 | 0 | legacy: |
528 | 0 | if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) { |
529 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
530 | 0 | return -2; |
531 | 0 | } |
532 | | |
533 | 0 | M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE) return ctx->pmeth->derive(ctx, key, pkeylen); |
534 | 0 | } |
535 | | |
536 | | int evp_keyexch_get_number(const EVP_KEYEXCH *keyexch) |
537 | 0 | { |
538 | 0 | return keyexch->name_id; |
539 | 0 | } |
540 | | |
541 | | const char *EVP_KEYEXCH_get0_name(const EVP_KEYEXCH *keyexch) |
542 | 0 | { |
543 | 0 | return keyexch->type_name; |
544 | 0 | } |
545 | | |
546 | | const char *EVP_KEYEXCH_get0_description(const EVP_KEYEXCH *keyexch) |
547 | 0 | { |
548 | 0 | return keyexch->description; |
549 | 0 | } |
550 | | |
551 | | int EVP_KEYEXCH_is_a(const EVP_KEYEXCH *keyexch, const char *name) |
552 | 0 | { |
553 | 0 | return keyexch != NULL |
554 | 0 | && evp_is_a(keyexch->prov, keyexch->name_id, NULL, name); |
555 | 0 | } |
556 | | |
557 | | void EVP_KEYEXCH_do_all_provided(OSSL_LIB_CTX *libctx, |
558 | | void (*fn)(EVP_KEYEXCH *keyexch, void *arg), |
559 | | void *arg) |
560 | 8 | { |
561 | 8 | evp_generic_do_all(libctx, OSSL_OP_KEYEXCH, |
562 | 8 | (void (*)(void *, void *))fn, arg, |
563 | 8 | evp_keyexch_from_algorithm, |
564 | 8 | (int (*)(void *))EVP_KEYEXCH_up_ref, |
565 | 8 | (void (*)(void *))EVP_KEYEXCH_free); |
566 | 8 | } |
567 | | |
568 | | int EVP_KEYEXCH_names_do_all(const EVP_KEYEXCH *keyexch, |
569 | | void (*fn)(const char *name, void *data), |
570 | | void *data) |
571 | 0 | { |
572 | 0 | if (keyexch->prov != NULL) |
573 | 0 | return evp_names_do_all(keyexch->prov, keyexch->name_id, fn, data); |
574 | | |
575 | 0 | return 1; |
576 | 0 | } |
577 | | |
578 | | const OSSL_PARAM *EVP_KEYEXCH_gettable_ctx_params(const EVP_KEYEXCH *keyexch) |
579 | 0 | { |
580 | 0 | void *provctx; |
581 | |
|
582 | 0 | if (keyexch == NULL || keyexch->gettable_ctx_params == NULL) |
583 | 0 | return NULL; |
584 | | |
585 | 0 | provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(keyexch)); |
586 | 0 | return keyexch->gettable_ctx_params(NULL, provctx); |
587 | 0 | } |
588 | | |
589 | | const OSSL_PARAM *EVP_KEYEXCH_settable_ctx_params(const EVP_KEYEXCH *keyexch) |
590 | 94 | { |
591 | 94 | void *provctx; |
592 | | |
593 | 94 | if (keyexch == NULL || keyexch->settable_ctx_params == NULL) |
594 | 4 | return NULL; |
595 | 90 | provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(keyexch)); |
596 | | return keyexch->settable_ctx_params(NULL, provctx); |
597 | 94 | } |