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