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