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