/src/openssl/crypto/evp/pmeth_lib.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2006-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 | | /* |
11 | | * Low level key APIs (DH etc) are deprecated for public use, but still ok for |
12 | | * internal use. |
13 | | */ |
14 | | #include "internal/deprecated.h" |
15 | | |
16 | | #include <stdio.h> |
17 | | #include <stdlib.h> |
18 | | #include <openssl/evp.h> |
19 | | #include <openssl/core_names.h> |
20 | | #include <openssl/dh.h> |
21 | | #include <openssl/rsa.h> |
22 | | #include <openssl/kdf.h> |
23 | | #include "internal/cryptlib.h" |
24 | | #ifndef FIPS_MODULE |
25 | | #include "crypto/asn1.h" |
26 | | #endif |
27 | | #include "crypto/evp.h" |
28 | | #include "crypto/dh.h" |
29 | | #include "crypto/ec.h" |
30 | | #include "internal/ffc.h" |
31 | | #include "internal/numbers.h" |
32 | | #include "internal/provider.h" |
33 | | #include "internal/common.h" |
34 | | #include "evp_local.h" |
35 | | |
36 | | #ifndef FIPS_MODULE |
37 | | |
38 | | static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx, |
39 | | int keytype, int optype, |
40 | | int cmd, const char *name, |
41 | | const void *data, size_t data_len); |
42 | | static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx, |
43 | | int cmd, const char *name); |
44 | | static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx); |
45 | | |
46 | | #endif /* FIPS_MODULE */ |
47 | | |
48 | | int evp_pkey_ctx_state(const EVP_PKEY_CTX *ctx) |
49 | 0 | { |
50 | 0 | if (ctx->operation == EVP_PKEY_OP_UNDEFINED) |
51 | 0 | return EVP_PKEY_STATE_UNKNOWN; |
52 | | |
53 | 0 | if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
54 | 0 | && ctx->op.kex.algctx != NULL) |
55 | 0 | || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
56 | 0 | && ctx->op.sig.algctx != NULL) |
57 | 0 | || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
58 | 0 | && ctx->op.ciph.algctx != NULL) |
59 | 0 | || (EVP_PKEY_CTX_IS_GEN_OP(ctx) |
60 | 0 | && ctx->op.keymgmt.genctx != NULL) |
61 | 0 | || (EVP_PKEY_CTX_IS_KEM_OP(ctx) |
62 | 0 | && ctx->op.encap.algctx != NULL)) |
63 | 0 | return EVP_PKEY_STATE_PROVIDER; |
64 | | |
65 | 0 | return EVP_PKEY_STATE_LEGACY; |
66 | 0 | } |
67 | | |
68 | | static EVP_PKEY_CTX *int_ctx_new(OSSL_LIB_CTX *libctx, EVP_PKEY *pkey, |
69 | | const char *keytype, const char *propquery, |
70 | | int id) |
71 | 0 | { |
72 | 0 | EVP_PKEY_CTX *ret = NULL; |
73 | 0 | EVP_KEYMGMT *keymgmt = NULL; |
74 | | |
75 | | /* Code below to be removed when legacy support is dropped. */ |
76 | | /* BEGIN legacy */ |
77 | 0 | if (id == -1) { |
78 | 0 | if (pkey != NULL && !evp_pkey_is_provided(pkey)) { |
79 | 0 | id = pkey->type; |
80 | 0 | } else { |
81 | 0 | if (pkey != NULL) { |
82 | | /* Must be provided if we get here */ |
83 | 0 | keytype = EVP_KEYMGMT_get0_name(pkey->keymgmt); |
84 | 0 | } |
85 | 0 | #ifndef FIPS_MODULE |
86 | 0 | if (keytype != NULL) { |
87 | 0 | id = evp_pkey_name2type(keytype); |
88 | 0 | if (id == NID_undef) |
89 | 0 | id = -1; |
90 | 0 | } |
91 | 0 | #endif |
92 | 0 | } |
93 | 0 | } |
94 | |
|
95 | 0 | #ifndef FIPS_MODULE |
96 | | /* |
97 | | * Here, we extract what information we can for the purpose of |
98 | | * supporting usage with implementations from providers, to make |
99 | | * for a smooth transition from legacy stuff to provider based stuff. |
100 | | */ |
101 | 0 | if (id != -1) |
102 | 0 | keytype = OBJ_nid2sn(id); |
103 | | |
104 | | /* END legacy */ |
105 | 0 | #endif /* FIPS_MODULE */ |
106 | | /* We try fetching a provider implementation. */ |
107 | 0 | if (keytype != NULL) { |
108 | | /* |
109 | | * If |pkey| is given and is provided, we take a reference to its |
110 | | * keymgmt. Otherwise, we fetch one for the keytype we got. This |
111 | | * is to ensure that operation init functions can access what they |
112 | | * need through this single pointer. |
113 | | */ |
114 | 0 | if (pkey != NULL && pkey->keymgmt != NULL) { |
115 | 0 | if (!EVP_KEYMGMT_up_ref(pkey->keymgmt)) |
116 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
117 | 0 | else |
118 | 0 | keymgmt = pkey->keymgmt; |
119 | 0 | } else { |
120 | 0 | keymgmt = EVP_KEYMGMT_fetch(libctx, keytype, propquery); |
121 | 0 | } |
122 | 0 | if (keymgmt == NULL) |
123 | 0 | return NULL; /* EVP_KEYMGMT_fetch() recorded an error */ |
124 | | |
125 | 0 | #ifndef FIPS_MODULE |
126 | | /* |
127 | | * Chase down the legacy NID, as that might be needed for diverse |
128 | | * purposes, such as ensure that EVP_PKEY_type() can return sensible |
129 | | * values. We go through all keymgmt names, because the keytype |
130 | | * that's passed to this function doesn't necessarily translate |
131 | | * directly. |
132 | | */ |
133 | 0 | if (keymgmt != NULL) { |
134 | 0 | int tmp_id = evp_keymgmt_get_legacy_alg(keymgmt); |
135 | |
|
136 | 0 | if (tmp_id != NID_undef) { |
137 | 0 | if (id == -1) { |
138 | 0 | id = tmp_id; |
139 | 0 | } else { |
140 | | /* |
141 | | * It really really shouldn't differ. If it still does, |
142 | | * something is very wrong. |
143 | | */ |
144 | 0 | if (!ossl_assert(id == tmp_id)) { |
145 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
146 | 0 | EVP_KEYMGMT_free(keymgmt); |
147 | 0 | return NULL; |
148 | 0 | } |
149 | 0 | } |
150 | 0 | } |
151 | 0 | } |
152 | 0 | #endif |
153 | 0 | } |
154 | | |
155 | 0 | if (keymgmt == NULL) { |
156 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
157 | 0 | } else { |
158 | 0 | ret = OPENSSL_zalloc(sizeof(*ret)); |
159 | 0 | } |
160 | |
|
161 | 0 | if (ret == NULL) { |
162 | 0 | EVP_KEYMGMT_free(keymgmt); |
163 | 0 | return NULL; |
164 | 0 | } |
165 | 0 | if (propquery != NULL) { |
166 | 0 | ret->propquery = OPENSSL_strdup(propquery); |
167 | 0 | if (ret->propquery == NULL) { |
168 | 0 | OPENSSL_free(ret); |
169 | 0 | EVP_KEYMGMT_free(keymgmt); |
170 | 0 | return NULL; |
171 | 0 | } |
172 | 0 | } |
173 | 0 | ret->libctx = libctx; |
174 | 0 | ret->keytype = keytype; |
175 | 0 | ret->keymgmt = keymgmt; |
176 | 0 | ret->legacy_keytype = id; |
177 | 0 | ret->operation = EVP_PKEY_OP_UNDEFINED; |
178 | |
|
179 | 0 | if (pkey != NULL && !EVP_PKEY_up_ref(pkey)) { |
180 | 0 | EVP_PKEY_CTX_free(ret); |
181 | 0 | return NULL; |
182 | 0 | } |
183 | | |
184 | 0 | ret->pkey = pkey; |
185 | |
|
186 | 0 | return ret; |
187 | 0 | } |
188 | | |
189 | | /*- All methods below can also be used in FIPS_MODULE */ |
190 | | |
191 | | EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_name(OSSL_LIB_CTX *libctx, |
192 | | const char *name, |
193 | | const char *propquery) |
194 | 0 | { |
195 | 0 | return int_ctx_new(libctx, NULL, name, propquery, -1); |
196 | 0 | } |
197 | | |
198 | | EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_pkey(OSSL_LIB_CTX *libctx, EVP_PKEY *pkey, |
199 | | const char *propquery) |
200 | 0 | { |
201 | 0 | return int_ctx_new(libctx, pkey, NULL, propquery, -1); |
202 | 0 | } |
203 | | |
204 | | void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx) |
205 | 0 | { |
206 | 0 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) { |
207 | 0 | if (ctx->op.sig.algctx != NULL && ctx->op.sig.signature != NULL) |
208 | 0 | ctx->op.sig.signature->freectx(ctx->op.sig.algctx); |
209 | 0 | EVP_SIGNATURE_free(ctx->op.sig.signature); |
210 | 0 | ctx->op.sig.algctx = NULL; |
211 | 0 | ctx->op.sig.signature = NULL; |
212 | 0 | } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { |
213 | 0 | if (ctx->op.kex.algctx != NULL && ctx->op.kex.exchange != NULL) |
214 | 0 | ctx->op.kex.exchange->freectx(ctx->op.kex.algctx); |
215 | 0 | EVP_KEYEXCH_free(ctx->op.kex.exchange); |
216 | 0 | ctx->op.kex.algctx = NULL; |
217 | 0 | ctx->op.kex.exchange = NULL; |
218 | 0 | } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) { |
219 | 0 | if (ctx->op.encap.algctx != NULL && ctx->op.encap.kem != NULL) |
220 | 0 | ctx->op.encap.kem->freectx(ctx->op.encap.algctx); |
221 | 0 | EVP_KEM_free(ctx->op.encap.kem); |
222 | 0 | ctx->op.encap.algctx = NULL; |
223 | 0 | ctx->op.encap.kem = NULL; |
224 | 0 | } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) { |
225 | 0 | if (ctx->op.ciph.algctx != NULL && ctx->op.ciph.cipher != NULL) |
226 | 0 | ctx->op.ciph.cipher->freectx(ctx->op.ciph.algctx); |
227 | 0 | EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher); |
228 | 0 | ctx->op.ciph.algctx = NULL; |
229 | 0 | ctx->op.ciph.cipher = NULL; |
230 | 0 | } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) { |
231 | 0 | if (ctx->op.keymgmt.genctx != NULL && ctx->keymgmt != NULL) |
232 | 0 | evp_keymgmt_gen_cleanup(ctx->keymgmt, ctx->op.keymgmt.genctx); |
233 | 0 | } |
234 | 0 | } |
235 | | |
236 | | void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx) |
237 | 18.1k | { |
238 | 18.1k | if (ctx == NULL) |
239 | 18.1k | return; |
240 | | |
241 | 0 | evp_pkey_ctx_free_old_ops(ctx); |
242 | 0 | #ifndef FIPS_MODULE |
243 | 0 | evp_pkey_ctx_free_all_cached_data(ctx); |
244 | 0 | #endif |
245 | 0 | EVP_KEYMGMT_free(ctx->keymgmt); |
246 | |
|
247 | 0 | OPENSSL_free(ctx->propquery); |
248 | 0 | EVP_PKEY_free(ctx->pkey); |
249 | 0 | EVP_PKEY_free(ctx->peerkey); |
250 | 0 | BN_free(ctx->rsa_pubexp); |
251 | 0 | OPENSSL_free(ctx); |
252 | 0 | } |
253 | | |
254 | | #ifndef FIPS_MODULE |
255 | | EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e) |
256 | 0 | { |
257 | 0 | if (!ossl_assert(e == NULL)) |
258 | 0 | return NULL; |
259 | 0 | return int_ctx_new(NULL, pkey, NULL, NULL, -1); |
260 | 0 | } |
261 | | |
262 | | EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e) |
263 | 0 | { |
264 | 0 | if (!ossl_assert(e == NULL)) |
265 | 0 | return NULL; |
266 | 0 | return int_ctx_new(NULL, NULL, NULL, NULL, id); |
267 | 0 | } |
268 | | |
269 | | EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx) |
270 | 0 | { |
271 | 0 | EVP_PKEY_CTX *rctx; |
272 | |
|
273 | 0 | rctx = OPENSSL_zalloc(sizeof(*rctx)); |
274 | 0 | if (rctx == NULL) |
275 | 0 | return NULL; |
276 | | |
277 | 0 | if (pctx->pkey != NULL && !EVP_PKEY_up_ref(pctx->pkey)) |
278 | 0 | goto err; |
279 | | |
280 | 0 | rctx->pkey = pctx->pkey; |
281 | 0 | rctx->operation = pctx->operation; |
282 | 0 | rctx->libctx = pctx->libctx; |
283 | 0 | rctx->keytype = pctx->keytype; |
284 | 0 | rctx->propquery = NULL; |
285 | 0 | if (pctx->propquery != NULL) { |
286 | 0 | rctx->propquery = OPENSSL_strdup(pctx->propquery); |
287 | 0 | if (rctx->propquery == NULL) |
288 | 0 | goto err; |
289 | 0 | } |
290 | 0 | rctx->legacy_keytype = pctx->legacy_keytype; |
291 | |
|
292 | 0 | if (pctx->keymgmt != NULL) { |
293 | 0 | if (!EVP_KEYMGMT_up_ref(pctx->keymgmt)) |
294 | 0 | goto err; |
295 | 0 | rctx->keymgmt = pctx->keymgmt; |
296 | 0 | } |
297 | | |
298 | 0 | if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) { |
299 | 0 | if (pctx->op.kex.exchange != NULL) { |
300 | 0 | rctx->op.kex.exchange = pctx->op.kex.exchange; |
301 | 0 | if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) |
302 | 0 | goto err; |
303 | 0 | } |
304 | 0 | if (pctx->op.kex.algctx != NULL) { |
305 | 0 | if (!ossl_assert(pctx->op.kex.exchange != NULL)) |
306 | 0 | goto err; |
307 | | |
308 | 0 | if (pctx->op.kex.exchange->dupctx != NULL) |
309 | 0 | rctx->op.kex.algctx |
310 | 0 | = pctx->op.kex.exchange->dupctx(pctx->op.kex.algctx); |
311 | |
|
312 | 0 | if (rctx->op.kex.algctx == NULL) { |
313 | 0 | EVP_KEYEXCH_free(rctx->op.kex.exchange); |
314 | 0 | rctx->op.kex.exchange = NULL; |
315 | 0 | goto err; |
316 | 0 | } |
317 | 0 | return rctx; |
318 | 0 | } |
319 | 0 | } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) { |
320 | 0 | if (pctx->op.sig.signature != NULL) { |
321 | 0 | rctx->op.sig.signature = pctx->op.sig.signature; |
322 | 0 | if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) |
323 | 0 | goto err; |
324 | 0 | } |
325 | 0 | if (pctx->op.sig.algctx != NULL) { |
326 | 0 | if (!ossl_assert(pctx->op.sig.signature != NULL)) |
327 | 0 | goto err; |
328 | | |
329 | 0 | if (pctx->op.sig.signature->dupctx != NULL) |
330 | 0 | rctx->op.sig.algctx |
331 | 0 | = pctx->op.sig.signature->dupctx(pctx->op.sig.algctx); |
332 | |
|
333 | 0 | if (rctx->op.sig.algctx == NULL) { |
334 | 0 | EVP_SIGNATURE_free(rctx->op.sig.signature); |
335 | 0 | rctx->op.sig.signature = NULL; |
336 | 0 | goto err; |
337 | 0 | } |
338 | 0 | return rctx; |
339 | 0 | } |
340 | 0 | } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) { |
341 | 0 | if (pctx->op.ciph.cipher != NULL) { |
342 | 0 | rctx->op.ciph.cipher = pctx->op.ciph.cipher; |
343 | 0 | if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher)) |
344 | 0 | goto err; |
345 | 0 | } |
346 | 0 | if (pctx->op.ciph.algctx != NULL) { |
347 | 0 | if (!ossl_assert(pctx->op.ciph.cipher != NULL)) |
348 | 0 | goto err; |
349 | | |
350 | 0 | if (pctx->op.ciph.cipher->dupctx != NULL) |
351 | 0 | rctx->op.ciph.algctx |
352 | 0 | = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.algctx); |
353 | |
|
354 | 0 | if (rctx->op.ciph.algctx == NULL) { |
355 | 0 | EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher); |
356 | 0 | rctx->op.ciph.cipher = NULL; |
357 | 0 | goto err; |
358 | 0 | } |
359 | 0 | return rctx; |
360 | 0 | } |
361 | 0 | } else if (EVP_PKEY_CTX_IS_KEM_OP(pctx)) { |
362 | 0 | if (pctx->op.encap.kem != NULL) { |
363 | 0 | rctx->op.encap.kem = pctx->op.encap.kem; |
364 | 0 | if (!EVP_KEM_up_ref(rctx->op.encap.kem)) |
365 | 0 | goto err; |
366 | 0 | } |
367 | 0 | if (pctx->op.encap.algctx != NULL) { |
368 | 0 | if (!ossl_assert(pctx->op.encap.kem != NULL)) |
369 | 0 | goto err; |
370 | | |
371 | 0 | if (pctx->op.encap.kem->dupctx != NULL) |
372 | 0 | rctx->op.encap.algctx |
373 | 0 | = pctx->op.encap.kem->dupctx(pctx->op.encap.algctx); |
374 | |
|
375 | 0 | if (rctx->op.encap.algctx == NULL) { |
376 | 0 | EVP_KEM_free(rctx->op.encap.kem); |
377 | 0 | rctx->op.encap.kem = NULL; |
378 | 0 | goto err; |
379 | 0 | } |
380 | 0 | return rctx; |
381 | 0 | } |
382 | 0 | } else if (EVP_PKEY_CTX_IS_GEN_OP(pctx)) { |
383 | | /* Not supported - This would need a gen_dupctx() to work */ |
384 | 0 | goto err; |
385 | 0 | } |
386 | | |
387 | 0 | if (pctx->peerkey != NULL && !EVP_PKEY_up_ref(pctx->peerkey)) |
388 | 0 | goto err; |
389 | | |
390 | 0 | rctx->peerkey = pctx->peerkey; |
391 | |
|
392 | 0 | if (rctx->operation == EVP_PKEY_OP_UNDEFINED) { |
393 | 0 | EVP_KEYMGMT *tmp_keymgmt = pctx->keymgmt; |
394 | 0 | void *provkey; |
395 | |
|
396 | 0 | if (pctx->pkey == NULL) |
397 | 0 | return rctx; |
398 | | |
399 | 0 | provkey = evp_pkey_export_to_provider(pctx->pkey, pctx->libctx, |
400 | 0 | &tmp_keymgmt, pctx->propquery); |
401 | 0 | if (provkey == NULL) |
402 | 0 | goto err; |
403 | 0 | if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) |
404 | 0 | goto err; |
405 | 0 | EVP_KEYMGMT_free(rctx->keymgmt); |
406 | 0 | rctx->keymgmt = tmp_keymgmt; |
407 | 0 | return rctx; |
408 | 0 | } |
409 | 0 | err: |
410 | 0 | EVP_PKEY_CTX_free(rctx); |
411 | 0 | return NULL; |
412 | 0 | } |
413 | | #endif |
414 | | |
415 | | int EVP_PKEY_CTX_is_a(EVP_PKEY_CTX *ctx, const char *keytype) |
416 | 0 | { |
417 | 0 | #ifndef FIPS_MODULE |
418 | 0 | if (evp_pkey_ctx_is_legacy(ctx)) |
419 | 0 | return (ctx->legacy_keytype == evp_pkey_name2type(keytype)); |
420 | 0 | #endif |
421 | 0 | return EVP_KEYMGMT_is_a(ctx->keymgmt, keytype); |
422 | 0 | } |
423 | | |
424 | | int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params) |
425 | 0 | { |
426 | 0 | switch (evp_pkey_ctx_state(ctx)) { |
427 | 0 | case EVP_PKEY_STATE_PROVIDER: |
428 | 0 | if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
429 | 0 | && ctx->op.kex.exchange != NULL |
430 | 0 | && ctx->op.kex.exchange->set_ctx_params != NULL) |
431 | 0 | return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.algctx, |
432 | 0 | params); |
433 | 0 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
434 | 0 | && ctx->op.sig.signature != NULL |
435 | 0 | && ctx->op.sig.signature->set_ctx_params != NULL) |
436 | 0 | return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.algctx, |
437 | 0 | params); |
438 | 0 | if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
439 | 0 | && ctx->op.ciph.cipher != NULL |
440 | 0 | && ctx->op.ciph.cipher->set_ctx_params != NULL) |
441 | 0 | return ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.algctx, |
442 | 0 | params); |
443 | 0 | if (EVP_PKEY_CTX_IS_GEN_OP(ctx) |
444 | 0 | && ctx->keymgmt != NULL |
445 | 0 | && ctx->keymgmt->gen_set_params != NULL) |
446 | 0 | return evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx, |
447 | 0 | params); |
448 | 0 | if (EVP_PKEY_CTX_IS_KEM_OP(ctx) |
449 | 0 | && ctx->op.encap.kem != NULL |
450 | 0 | && ctx->op.encap.kem->set_ctx_params != NULL) |
451 | 0 | return ctx->op.encap.kem->set_ctx_params(ctx->op.encap.algctx, |
452 | 0 | params); |
453 | 0 | break; |
454 | 0 | case EVP_PKEY_STATE_UNKNOWN: |
455 | 0 | break; |
456 | 0 | #ifndef FIPS_MODULE |
457 | 0 | case EVP_PKEY_STATE_LEGACY: |
458 | 0 | return evp_pkey_ctx_set_params_to_ctrl(ctx, params); |
459 | 0 | #endif |
460 | 0 | } |
461 | 0 | return 0; |
462 | 0 | } |
463 | | |
464 | | int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params) |
465 | 0 | { |
466 | 0 | switch (evp_pkey_ctx_state(ctx)) { |
467 | 0 | case EVP_PKEY_STATE_PROVIDER: |
468 | 0 | if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
469 | 0 | && ctx->op.kex.exchange != NULL |
470 | 0 | && ctx->op.kex.exchange->get_ctx_params != NULL) |
471 | 0 | return ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.algctx, |
472 | 0 | params); |
473 | 0 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
474 | 0 | && ctx->op.sig.signature != NULL |
475 | 0 | && ctx->op.sig.signature->get_ctx_params != NULL) |
476 | 0 | return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.algctx, |
477 | 0 | params); |
478 | 0 | if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
479 | 0 | && ctx->op.ciph.cipher != NULL |
480 | 0 | && ctx->op.ciph.cipher->get_ctx_params != NULL) |
481 | 0 | return ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.algctx, |
482 | 0 | params); |
483 | 0 | if (EVP_PKEY_CTX_IS_KEM_OP(ctx) |
484 | 0 | && ctx->op.encap.kem != NULL |
485 | 0 | && ctx->op.encap.kem->get_ctx_params != NULL) |
486 | 0 | return ctx->op.encap.kem->get_ctx_params(ctx->op.encap.algctx, |
487 | 0 | params); |
488 | 0 | if (EVP_PKEY_CTX_IS_GEN_OP(ctx) |
489 | 0 | && ctx->keymgmt != NULL |
490 | 0 | && ctx->keymgmt->gen_get_params != NULL) |
491 | 0 | return evp_keymgmt_gen_get_params(ctx->keymgmt, ctx->op.keymgmt.genctx, |
492 | 0 | params); |
493 | 0 | break; |
494 | 0 | case EVP_PKEY_STATE_UNKNOWN: |
495 | 0 | break; |
496 | 0 | #ifndef FIPS_MODULE |
497 | 0 | case EVP_PKEY_STATE_LEGACY: |
498 | 0 | return evp_pkey_ctx_get_params_to_ctrl(ctx, params); |
499 | 0 | #endif |
500 | 0 | } |
501 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_GET_CTX_PARAMS_NOT_SUPPORTED, |
502 | 0 | "EVP_PKEY_OP=0x%x", ctx->operation); |
503 | 0 | return 0; |
504 | 0 | } |
505 | | |
506 | | #ifndef FIPS_MODULE |
507 | | const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(const EVP_PKEY_CTX *ctx) |
508 | 0 | { |
509 | 0 | void *provctx; |
510 | |
|
511 | 0 | if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
512 | 0 | && ctx->op.kex.exchange != NULL |
513 | 0 | && ctx->op.kex.exchange->gettable_ctx_params != NULL) { |
514 | 0 | provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange)); |
515 | 0 | return ctx->op.kex.exchange->gettable_ctx_params(ctx->op.kex.algctx, |
516 | 0 | provctx); |
517 | 0 | } |
518 | 0 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
519 | 0 | && ctx->op.sig.signature != NULL |
520 | 0 | && ctx->op.sig.signature->gettable_ctx_params != NULL) { |
521 | 0 | provctx = ossl_provider_ctx( |
522 | 0 | EVP_SIGNATURE_get0_provider(ctx->op.sig.signature)); |
523 | 0 | return ctx->op.sig.signature->gettable_ctx_params(ctx->op.sig.algctx, |
524 | 0 | provctx); |
525 | 0 | } |
526 | 0 | if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
527 | 0 | && ctx->op.ciph.cipher != NULL |
528 | 0 | && ctx->op.ciph.cipher->gettable_ctx_params != NULL) { |
529 | 0 | provctx = ossl_provider_ctx( |
530 | 0 | EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher)); |
531 | 0 | return ctx->op.ciph.cipher->gettable_ctx_params(ctx->op.ciph.algctx, |
532 | 0 | provctx); |
533 | 0 | } |
534 | 0 | if (EVP_PKEY_CTX_IS_KEM_OP(ctx) |
535 | 0 | && ctx->op.encap.kem != NULL |
536 | 0 | && ctx->op.encap.kem->gettable_ctx_params != NULL) { |
537 | 0 | provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem)); |
538 | 0 | return ctx->op.encap.kem->gettable_ctx_params(ctx->op.encap.algctx, |
539 | 0 | provctx); |
540 | 0 | } |
541 | 0 | if (EVP_PKEY_CTX_IS_GEN_OP(ctx) |
542 | 0 | && ctx->keymgmt != NULL |
543 | 0 | && ctx->keymgmt->gen_gettable_params != NULL) { |
544 | 0 | provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(ctx->keymgmt)); |
545 | 0 | return ctx->keymgmt->gen_gettable_params(ctx->op.keymgmt.genctx, |
546 | 0 | provctx); |
547 | 0 | } |
548 | 0 | return NULL; |
549 | 0 | } |
550 | | |
551 | | const OSSL_PARAM *EVP_PKEY_CTX_settable_params(const EVP_PKEY_CTX *ctx) |
552 | 0 | { |
553 | 0 | void *provctx; |
554 | |
|
555 | 0 | if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
556 | 0 | && ctx->op.kex.exchange != NULL |
557 | 0 | && ctx->op.kex.exchange->settable_ctx_params != NULL) { |
558 | 0 | provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange)); |
559 | 0 | return ctx->op.kex.exchange->settable_ctx_params(ctx->op.kex.algctx, |
560 | 0 | provctx); |
561 | 0 | } |
562 | 0 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
563 | 0 | && ctx->op.sig.signature != NULL |
564 | 0 | && ctx->op.sig.signature->settable_ctx_params != NULL) { |
565 | 0 | provctx = ossl_provider_ctx( |
566 | 0 | EVP_SIGNATURE_get0_provider(ctx->op.sig.signature)); |
567 | 0 | return ctx->op.sig.signature->settable_ctx_params(ctx->op.sig.algctx, |
568 | 0 | provctx); |
569 | 0 | } |
570 | 0 | if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
571 | 0 | && ctx->op.ciph.cipher != NULL |
572 | 0 | && ctx->op.ciph.cipher->settable_ctx_params != NULL) { |
573 | 0 | provctx = ossl_provider_ctx( |
574 | 0 | EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher)); |
575 | 0 | return ctx->op.ciph.cipher->settable_ctx_params(ctx->op.ciph.algctx, |
576 | 0 | provctx); |
577 | 0 | } |
578 | 0 | if (EVP_PKEY_CTX_IS_GEN_OP(ctx) |
579 | 0 | && ctx->keymgmt != NULL |
580 | 0 | && ctx->keymgmt->gen_settable_params != NULL) { |
581 | 0 | provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(ctx->keymgmt)); |
582 | 0 | return ctx->keymgmt->gen_settable_params(ctx->op.keymgmt.genctx, |
583 | 0 | provctx); |
584 | 0 | } |
585 | 0 | if (EVP_PKEY_CTX_IS_KEM_OP(ctx) |
586 | 0 | && ctx->op.encap.kem != NULL |
587 | 0 | && ctx->op.encap.kem->settable_ctx_params != NULL) { |
588 | 0 | provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem)); |
589 | 0 | return ctx->op.encap.kem->settable_ctx_params(ctx->op.encap.algctx, |
590 | 0 | provctx); |
591 | 0 | } |
592 | 0 | return NULL; |
593 | 0 | } |
594 | | |
595 | | /* |
596 | | * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params(). |
597 | | * |
598 | | * Return 1 on success, 0 or negative for errors. |
599 | | * |
600 | | * In particular they return -2 if any of the params is not supported. |
601 | | * |
602 | | * They are not available in FIPS_MODULE as they depend on |
603 | | * - EVP_PKEY_CTX_{get,set}_params() |
604 | | * - EVP_PKEY_CTX_{gettable,settable}_params() |
605 | | * |
606 | | */ |
607 | | int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params) |
608 | 0 | { |
609 | 0 | if (ctx == NULL || params == NULL) |
610 | 0 | return 0; |
611 | | |
612 | | /* |
613 | | * We only check for provider side EVP_PKEY_CTX. For #legacy, we |
614 | | * depend on the translation that happens in EVP_PKEY_CTX_set_params() |
615 | | * call, and that the resulting ctrl call will return -2 if it doesn't |
616 | | * known the ctrl command number. |
617 | | */ |
618 | 0 | if (evp_pkey_ctx_is_provided(ctx)) { |
619 | 0 | const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx); |
620 | 0 | const OSSL_PARAM *p; |
621 | |
|
622 | 0 | for (p = params; p->key != NULL; p++) { |
623 | | /* Check the ctx actually understands this parameter */ |
624 | 0 | if (OSSL_PARAM_locate_const(settable, p->key) == NULL) |
625 | 0 | return -2; |
626 | 0 | } |
627 | 0 | } |
628 | | |
629 | 0 | return EVP_PKEY_CTX_set_params(ctx, params); |
630 | 0 | } |
631 | | |
632 | | int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params) |
633 | 0 | { |
634 | 0 | if (ctx == NULL || params == NULL) |
635 | 0 | return 0; |
636 | | |
637 | | /* |
638 | | * We only check for provider side EVP_PKEY_CTX. For #legacy, we |
639 | | * depend on the translation that happens in EVP_PKEY_CTX_get_params() |
640 | | * call, and that the resulting ctrl call will return -2 if it doesn't |
641 | | * known the ctrl command number. |
642 | | */ |
643 | 0 | if (evp_pkey_ctx_is_provided(ctx)) { |
644 | 0 | const OSSL_PARAM *gettable = EVP_PKEY_CTX_gettable_params(ctx); |
645 | 0 | const OSSL_PARAM *p; |
646 | |
|
647 | 0 | for (p = params; p->key != NULL; p++) { |
648 | | /* Check the ctx actually understands this parameter */ |
649 | 0 | if (OSSL_PARAM_locate_const(gettable, p->key) == NULL) |
650 | 0 | return -2; |
651 | 0 | } |
652 | 0 | } |
653 | | |
654 | 0 | return EVP_PKEY_CTX_get_params(ctx, params); |
655 | 0 | } |
656 | | |
657 | | int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md) |
658 | 0 | { |
659 | 0 | OSSL_PARAM sig_md_params[2], *p = sig_md_params; |
660 | | /* 80 should be big enough */ |
661 | 0 | char name[80] = ""; |
662 | 0 | const EVP_MD *tmp; |
663 | |
|
664 | 0 | if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) { |
665 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
666 | | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ |
667 | 0 | return -2; |
668 | 0 | } |
669 | | |
670 | 0 | if (ctx->op.sig.algctx == NULL) |
671 | 0 | return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, |
672 | 0 | EVP_PKEY_CTRL_GET_MD, 0, (void *)(md)); |
673 | | |
674 | 0 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, |
675 | 0 | name, |
676 | 0 | sizeof(name)); |
677 | 0 | *p = OSSL_PARAM_construct_end(); |
678 | |
|
679 | 0 | if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params)) |
680 | 0 | return 0; |
681 | | |
682 | 0 | tmp = evp_get_digestbyname_ex(ctx->libctx, name); |
683 | 0 | if (tmp == NULL) |
684 | 0 | return 0; |
685 | | |
686 | 0 | *md = tmp; |
687 | |
|
688 | 0 | return 1; |
689 | 0 | } |
690 | | |
691 | | static int evp_pkey_ctx_set_md(EVP_PKEY_CTX *ctx, const EVP_MD *md, |
692 | | int fallback, const char *param, int op, |
693 | | int ctrl) |
694 | 0 | { |
695 | 0 | OSSL_PARAM md_params[2], *p = md_params; |
696 | 0 | const char *name; |
697 | |
|
698 | 0 | if (ctx == NULL || (ctx->operation & op) == 0) { |
699 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
700 | | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ |
701 | 0 | return -2; |
702 | 0 | } |
703 | | |
704 | 0 | if (fallback) |
705 | 0 | return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, 0, (void *)(md)); |
706 | | |
707 | 0 | if (md == NULL) { |
708 | 0 | name = ""; |
709 | 0 | } else { |
710 | 0 | name = EVP_MD_get0_name(md); |
711 | 0 | } |
712 | |
|
713 | 0 | *p++ = OSSL_PARAM_construct_utf8_string(param, |
714 | | /* |
715 | | * Cast away the const. This is read |
716 | | * only so should be safe |
717 | | */ |
718 | 0 | (char *)name, 0); |
719 | 0 | *p = OSSL_PARAM_construct_end(); |
720 | |
|
721 | 0 | return EVP_PKEY_CTX_set_params(ctx, md_params); |
722 | 0 | } |
723 | | |
724 | | int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) |
725 | 0 | { |
726 | 0 | return evp_pkey_ctx_set_md(ctx, md, ctx->op.sig.algctx == NULL, |
727 | 0 | OSSL_SIGNATURE_PARAM_DIGEST, |
728 | 0 | EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD); |
729 | 0 | } |
730 | | |
731 | | int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) |
732 | 0 | { |
733 | 0 | return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL, |
734 | 0 | OSSL_KDF_PARAM_DIGEST, |
735 | 0 | EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_TLS_MD); |
736 | 0 | } |
737 | | |
738 | | static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX *ctx, int fallback, |
739 | | const char *param, int op, int ctrl, |
740 | | const unsigned char *data, |
741 | | int datalen) |
742 | 0 | { |
743 | 0 | OSSL_PARAM octet_string_params[2], *p = octet_string_params; |
744 | |
|
745 | 0 | if (ctx == NULL || (ctx->operation & op) == 0) { |
746 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
747 | | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ |
748 | 0 | return -2; |
749 | 0 | } |
750 | | |
751 | | /* Code below to be removed when legacy support is dropped. */ |
752 | 0 | if (fallback) |
753 | 0 | return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data)); |
754 | | /* end of legacy support */ |
755 | | |
756 | 0 | if (datalen < 0) { |
757 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH); |
758 | 0 | return 0; |
759 | 0 | } |
760 | | |
761 | 0 | *p++ = OSSL_PARAM_construct_octet_string(param, |
762 | | /* |
763 | | * Cast away the const. This is read |
764 | | * only so should be safe |
765 | | */ |
766 | 0 | (unsigned char *)data, |
767 | 0 | (size_t)datalen); |
768 | 0 | *p = OSSL_PARAM_construct_end(); |
769 | |
|
770 | 0 | return EVP_PKEY_CTX_set_params(ctx, octet_string_params); |
771 | 0 | } |
772 | | |
773 | | static int evp_pkey_ctx_add1_octet_string(EVP_PKEY_CTX *ctx, int fallback, |
774 | | const char *param, int op, int ctrl, |
775 | | const unsigned char *data, |
776 | | int datalen) |
777 | 0 | { |
778 | 0 | OSSL_PARAM os_params[2]; |
779 | 0 | const OSSL_PARAM *gettables; |
780 | 0 | unsigned char *info = NULL; |
781 | 0 | size_t info_len = 0; |
782 | 0 | size_t info_alloc = 0; |
783 | 0 | int ret = 0; |
784 | |
|
785 | 0 | if (ctx == NULL || (ctx->operation & op) == 0) { |
786 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
787 | | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ |
788 | 0 | return -2; |
789 | 0 | } |
790 | | |
791 | | /* Code below to be removed when legacy support is dropped. */ |
792 | 0 | if (fallback) |
793 | 0 | return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data)); |
794 | | /* end of legacy support */ |
795 | | |
796 | 0 | if (datalen < 0) { |
797 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH); |
798 | 0 | return 0; |
799 | 0 | } else if (datalen == 0) { |
800 | 0 | return 1; |
801 | 0 | } |
802 | | |
803 | | /* Check for older provider that doesn't support getting this parameter */ |
804 | 0 | gettables = EVP_PKEY_CTX_gettable_params(ctx); |
805 | 0 | if (gettables == NULL || OSSL_PARAM_locate_const(gettables, param) == NULL) |
806 | 0 | return evp_pkey_ctx_set1_octet_string(ctx, fallback, param, op, ctrl, |
807 | 0 | data, datalen); |
808 | | |
809 | | /* Get the original value length */ |
810 | 0 | os_params[0] = OSSL_PARAM_construct_octet_string(param, NULL, 0); |
811 | 0 | os_params[1] = OSSL_PARAM_construct_end(); |
812 | |
|
813 | 0 | if (!EVP_PKEY_CTX_get_params(ctx, os_params)) |
814 | 0 | return 0; |
815 | | |
816 | | /* This should not happen but check to be sure. */ |
817 | 0 | if (os_params[0].return_size == OSSL_PARAM_UNMODIFIED) |
818 | 0 | return 0; |
819 | | |
820 | 0 | info_alloc = os_params[0].return_size + datalen; |
821 | 0 | if (info_alloc == 0) |
822 | 0 | return 0; |
823 | 0 | info = OPENSSL_zalloc(info_alloc); |
824 | 0 | if (info == NULL) |
825 | 0 | return 0; |
826 | 0 | info_len = os_params[0].return_size; |
827 | |
|
828 | 0 | os_params[0] = OSSL_PARAM_construct_octet_string(param, info, info_alloc); |
829 | | |
830 | | /* if we have data, then go get it */ |
831 | 0 | if (info_len > 0) { |
832 | 0 | if (!EVP_PKEY_CTX_get_params(ctx, os_params)) |
833 | 0 | goto error; |
834 | 0 | } |
835 | | |
836 | | /* Copy the input data */ |
837 | 0 | memcpy(&info[info_len], data, datalen); |
838 | 0 | ret = EVP_PKEY_CTX_set_params(ctx, os_params); |
839 | |
|
840 | 0 | error: |
841 | 0 | OPENSSL_clear_free(info, info_alloc); |
842 | 0 | return ret; |
843 | 0 | } |
844 | | |
845 | | int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX *ctx, |
846 | | const unsigned char *sec, int seclen) |
847 | 0 | { |
848 | 0 | return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL, |
849 | 0 | OSSL_KDF_PARAM_SECRET, |
850 | 0 | EVP_PKEY_OP_DERIVE, |
851 | 0 | EVP_PKEY_CTRL_TLS_SECRET, |
852 | 0 | sec, seclen); |
853 | 0 | } |
854 | | |
855 | | int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX *ctx, |
856 | | const unsigned char *seed, int seedlen) |
857 | 0 | { |
858 | 0 | return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL, |
859 | 0 | OSSL_KDF_PARAM_SEED, |
860 | 0 | EVP_PKEY_OP_DERIVE, |
861 | 0 | EVP_PKEY_CTRL_TLS_SEED, |
862 | 0 | seed, seedlen); |
863 | 0 | } |
864 | | |
865 | | int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) |
866 | 0 | { |
867 | 0 | return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL, |
868 | 0 | OSSL_KDF_PARAM_DIGEST, |
869 | 0 | EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_HKDF_MD); |
870 | 0 | } |
871 | | |
872 | | int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx, |
873 | | const unsigned char *salt, int saltlen) |
874 | 0 | { |
875 | 0 | return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL, |
876 | 0 | OSSL_KDF_PARAM_SALT, |
877 | 0 | EVP_PKEY_OP_DERIVE, |
878 | 0 | EVP_PKEY_CTRL_HKDF_SALT, |
879 | 0 | salt, saltlen); |
880 | 0 | } |
881 | | |
882 | | int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx, |
883 | | const unsigned char *key, int keylen) |
884 | 0 | { |
885 | 0 | return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL, |
886 | 0 | OSSL_KDF_PARAM_KEY, |
887 | 0 | EVP_PKEY_OP_DERIVE, |
888 | 0 | EVP_PKEY_CTRL_HKDF_KEY, |
889 | 0 | key, keylen); |
890 | 0 | } |
891 | | |
892 | | int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx, |
893 | | const unsigned char *info, int infolen) |
894 | 0 | { |
895 | 0 | return evp_pkey_ctx_add1_octet_string(ctx, ctx->op.kex.algctx == NULL, |
896 | 0 | OSSL_KDF_PARAM_INFO, |
897 | 0 | EVP_PKEY_OP_DERIVE, |
898 | 0 | EVP_PKEY_CTRL_HKDF_INFO, |
899 | 0 | info, infolen); |
900 | 0 | } |
901 | | |
902 | | int EVP_PKEY_CTX_set_hkdf_mode(EVP_PKEY_CTX *ctx, int mode) |
903 | 0 | { |
904 | 0 | OSSL_PARAM int_params[2], *p = int_params; |
905 | |
|
906 | 0 | if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { |
907 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
908 | | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ |
909 | 0 | return -2; |
910 | 0 | } |
911 | | |
912 | | /* Code below to be removed when legacy support is dropped. */ |
913 | 0 | if (ctx->op.kex.algctx == NULL) |
914 | 0 | return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE, |
915 | 0 | EVP_PKEY_CTRL_HKDF_MODE, mode, NULL); |
916 | | /* end of legacy support */ |
917 | | |
918 | 0 | if (mode < 0) { |
919 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE); |
920 | 0 | return 0; |
921 | 0 | } |
922 | | |
923 | 0 | *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode); |
924 | 0 | *p = OSSL_PARAM_construct_end(); |
925 | |
|
926 | 0 | return EVP_PKEY_CTX_set_params(ctx, int_params); |
927 | 0 | } |
928 | | |
929 | | int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX *ctx, const char *pass, |
930 | | int passlen) |
931 | 0 | { |
932 | 0 | return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL, |
933 | 0 | OSSL_KDF_PARAM_PASSWORD, |
934 | 0 | EVP_PKEY_OP_DERIVE, |
935 | 0 | EVP_PKEY_CTRL_PASS, |
936 | 0 | (const unsigned char *)pass, passlen); |
937 | 0 | } |
938 | | |
939 | | int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX *ctx, |
940 | | const unsigned char *salt, int saltlen) |
941 | 0 | { |
942 | 0 | return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL, |
943 | 0 | OSSL_KDF_PARAM_SALT, |
944 | 0 | EVP_PKEY_OP_DERIVE, |
945 | 0 | EVP_PKEY_CTRL_SCRYPT_SALT, |
946 | 0 | salt, saltlen); |
947 | 0 | } |
948 | | |
949 | | static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX *ctx, const char *param, |
950 | | int op, int ctrl, uint64_t val) |
951 | 0 | { |
952 | 0 | OSSL_PARAM uint64_params[2], *p = uint64_params; |
953 | |
|
954 | 0 | if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { |
955 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
956 | | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ |
957 | 0 | return -2; |
958 | 0 | } |
959 | | |
960 | | /* Code below to be removed when legacy support is dropped. */ |
961 | 0 | if (ctx->op.kex.algctx == NULL) |
962 | 0 | return EVP_PKEY_CTX_ctrl_uint64(ctx, -1, op, ctrl, val); |
963 | | /* end of legacy support */ |
964 | | |
965 | 0 | *p++ = OSSL_PARAM_construct_uint64(param, &val); |
966 | 0 | *p = OSSL_PARAM_construct_end(); |
967 | |
|
968 | 0 | return EVP_PKEY_CTX_set_params(ctx, uint64_params); |
969 | 0 | } |
970 | | |
971 | | int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX *ctx, uint64_t n) |
972 | 0 | { |
973 | 0 | return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_N, |
974 | 0 | EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_N, |
975 | 0 | n); |
976 | 0 | } |
977 | | |
978 | | int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX *ctx, uint64_t r) |
979 | 0 | { |
980 | 0 | return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_R, |
981 | 0 | EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_R, |
982 | 0 | r); |
983 | 0 | } |
984 | | |
985 | | int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX *ctx, uint64_t p) |
986 | 0 | { |
987 | 0 | return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_P, |
988 | 0 | EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_P, |
989 | 0 | p); |
990 | 0 | } |
991 | | |
992 | | int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX *ctx, |
993 | | uint64_t maxmem_bytes) |
994 | 0 | { |
995 | 0 | return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_MAXMEM, |
996 | 0 | EVP_PKEY_OP_DERIVE, |
997 | 0 | EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES, |
998 | 0 | maxmem_bytes); |
999 | 0 | } |
1000 | | |
1001 | | int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX *ctx, const unsigned char *key, |
1002 | | int keylen) |
1003 | 0 | { |
1004 | 0 | return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.keymgmt.genctx == NULL, |
1005 | 0 | OSSL_PKEY_PARAM_PRIV_KEY, |
1006 | 0 | EVP_PKEY_OP_KEYGEN, |
1007 | 0 | EVP_PKEY_CTRL_SET_MAC_KEY, |
1008 | 0 | key, keylen); |
1009 | 0 | } |
1010 | | |
1011 | | int EVP_PKEY_CTX_set_kem_op(EVP_PKEY_CTX *ctx, const char *op) |
1012 | 0 | { |
1013 | 0 | OSSL_PARAM params[2], *p = params; |
1014 | |
|
1015 | 0 | if (ctx == NULL || op == NULL) { |
1016 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE); |
1017 | 0 | return 0; |
1018 | 0 | } |
1019 | 0 | if (!EVP_PKEY_CTX_IS_KEM_OP(ctx)) { |
1020 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
1021 | 0 | return -2; |
1022 | 0 | } |
1023 | 0 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION, |
1024 | 0 | (char *)op, 0); |
1025 | 0 | *p = OSSL_PARAM_construct_end(); |
1026 | 0 | return EVP_PKEY_CTX_set_params(ctx, params); |
1027 | 0 | } |
1028 | | |
1029 | | int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int len) |
1030 | 0 | { |
1031 | 0 | return EVP_PKEY_CTX_ctrl(ctx, -1, -1, |
1032 | 0 | EVP_PKEY_CTRL_SET1_ID, (int)len, (void *)(id)); |
1033 | 0 | } |
1034 | | |
1035 | | int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX *ctx, void *id) |
1036 | 0 | { |
1037 | 0 | return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET1_ID, 0, (void *)id); |
1038 | 0 | } |
1039 | | |
1040 | | int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX *ctx, size_t *id_len) |
1041 | 0 | { |
1042 | 0 | return EVP_PKEY_CTX_ctrl(ctx, -1, -1, |
1043 | 0 | EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void *)id_len); |
1044 | 0 | } |
1045 | | |
1046 | | static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX *ctx, int keytype, int optype, |
1047 | | int cmd, int p1, void *p2) |
1048 | 0 | { |
1049 | 0 | int ret = 0; |
1050 | |
|
1051 | 0 | if (ctx->operation == EVP_PKEY_OP_UNDEFINED) { |
1052 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_OPERATION_SET); |
1053 | 0 | return -1; |
1054 | 0 | } |
1055 | | |
1056 | 0 | if ((optype != -1) && !(ctx->operation & optype)) { |
1057 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
1058 | 0 | return -1; |
1059 | 0 | } |
1060 | | |
1061 | 0 | switch (evp_pkey_ctx_state(ctx)) { |
1062 | 0 | case EVP_PKEY_STATE_PROVIDER: |
1063 | 0 | return evp_pkey_ctx_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2); |
1064 | 0 | case EVP_PKEY_STATE_UNKNOWN: |
1065 | 0 | case EVP_PKEY_STATE_LEGACY: |
1066 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
1067 | 0 | return -2; |
1068 | 0 | } |
1069 | 0 | return ret; |
1070 | 0 | } |
1071 | | |
1072 | | int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, |
1073 | | int cmd, int p1, void *p2) |
1074 | 0 | { |
1075 | 0 | int ret = 0; |
1076 | |
|
1077 | 0 | if (ctx == NULL) { |
1078 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
1079 | 0 | return -2; |
1080 | 0 | } |
1081 | | /* If unsupported, we don't want that reported here */ |
1082 | 0 | ERR_set_mark(); |
1083 | 0 | ret = evp_pkey_ctx_store_cached_data(ctx, keytype, optype, |
1084 | 0 | cmd, NULL, p2, p1); |
1085 | 0 | if (ret == -2) { |
1086 | 0 | ERR_pop_to_mark(); |
1087 | 0 | } else { |
1088 | 0 | ERR_clear_last_mark(); |
1089 | | /* |
1090 | | * If there was an error, there was an error. |
1091 | | * If the operation isn't initialized yet, we also return, as |
1092 | | * the saved values will be used then anyway. |
1093 | | */ |
1094 | 0 | if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED) |
1095 | 0 | return ret; |
1096 | 0 | } |
1097 | 0 | return evp_pkey_ctx_ctrl_int(ctx, keytype, optype, cmd, p1, p2); |
1098 | 0 | } |
1099 | | |
1100 | | int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype, |
1101 | | int cmd, uint64_t value) |
1102 | 0 | { |
1103 | 0 | return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value); |
1104 | 0 | } |
1105 | | |
1106 | | static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX *ctx, |
1107 | | const char *name, const char *value) |
1108 | 0 | { |
1109 | 0 | int ret = 0; |
1110 | |
|
1111 | 0 | if (ctx == NULL) { |
1112 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
1113 | 0 | return -2; |
1114 | 0 | } |
1115 | | |
1116 | 0 | switch (evp_pkey_ctx_state(ctx)) { |
1117 | 0 | case EVP_PKEY_STATE_PROVIDER: |
1118 | 0 | return evp_pkey_ctx_ctrl_str_to_param(ctx, name, value); |
1119 | 0 | case EVP_PKEY_STATE_UNKNOWN: |
1120 | 0 | case EVP_PKEY_STATE_LEGACY: |
1121 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
1122 | 0 | return -2; |
1123 | 0 | } |
1124 | | |
1125 | 0 | return ret; |
1126 | 0 | } |
1127 | | |
1128 | | int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx, |
1129 | | const char *name, const char *value) |
1130 | 0 | { |
1131 | 0 | int ret = 0; |
1132 | | |
1133 | | /* If unsupported, we don't want that reported here */ |
1134 | 0 | ERR_set_mark(); |
1135 | 0 | ret = evp_pkey_ctx_store_cached_data(ctx, -1, -1, -1, |
1136 | 0 | name, value, strlen(value) + 1); |
1137 | 0 | if (ret == -2) { |
1138 | 0 | ERR_pop_to_mark(); |
1139 | 0 | } else { |
1140 | 0 | ERR_clear_last_mark(); |
1141 | | /* |
1142 | | * If there was an error, there was an error. |
1143 | | * If the operation isn't initialized yet, we also return, as |
1144 | | * the saved values will be used then anyway. |
1145 | | */ |
1146 | 0 | if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED) |
1147 | 0 | return ret; |
1148 | 0 | } |
1149 | | |
1150 | 0 | return evp_pkey_ctx_ctrl_str_int(ctx, name, value); |
1151 | 0 | } |
1152 | | |
1153 | | static int decode_cmd(int cmd, const char *name) |
1154 | 0 | { |
1155 | 0 | if (cmd == -1) { |
1156 | | /* |
1157 | | * The consequence of the assertion not being true is that this |
1158 | | * function will return -1, which will cause the calling functions |
1159 | | * to signal that the command is unsupported... in non-debug mode. |
1160 | | */ |
1161 | 0 | if (ossl_assert(name != NULL)) |
1162 | 0 | if (strcmp(name, "distid") == 0 || strcmp(name, "hexdistid") == 0) |
1163 | 0 | cmd = EVP_PKEY_CTRL_SET1_ID; |
1164 | 0 | } |
1165 | |
|
1166 | 0 | return cmd; |
1167 | 0 | } |
1168 | | |
1169 | | static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx, |
1170 | | int keytype, int optype, |
1171 | | int cmd, const char *name, |
1172 | | const void *data, size_t data_len) |
1173 | 0 | { |
1174 | | /* |
1175 | | * Check that it's one of the supported commands. The ctrl commands |
1176 | | * number cases here must correspond to the cases in the bottom switch |
1177 | | * in this function. |
1178 | | */ |
1179 | 0 | switch (cmd = decode_cmd(cmd, name)) { |
1180 | 0 | case EVP_PKEY_CTRL_SET1_ID: |
1181 | 0 | break; |
1182 | 0 | default: |
1183 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
1184 | 0 | return -2; |
1185 | 0 | } |
1186 | | |
1187 | 0 | if (keytype != -1) { |
1188 | 0 | switch (evp_pkey_ctx_state(ctx)) { |
1189 | 0 | case EVP_PKEY_STATE_PROVIDER: |
1190 | 0 | if (ctx->keymgmt == NULL) { |
1191 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
1192 | 0 | return -2; |
1193 | 0 | } |
1194 | 0 | if (!EVP_KEYMGMT_is_a(ctx->keymgmt, |
1195 | 0 | evp_pkey_type2name(keytype))) { |
1196 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
1197 | 0 | return -1; |
1198 | 0 | } |
1199 | 0 | break; |
1200 | 0 | case EVP_PKEY_STATE_UNKNOWN: |
1201 | 0 | case EVP_PKEY_STATE_LEGACY: |
1202 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
1203 | 0 | return -2; |
1204 | 0 | } |
1205 | 0 | } |
1206 | 0 | if (optype != -1 && (ctx->operation & optype) == 0) { |
1207 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
1208 | 0 | return -1; |
1209 | 0 | } |
1210 | | |
1211 | 0 | switch (cmd) { |
1212 | 0 | case EVP_PKEY_CTRL_SET1_ID: |
1213 | 0 | evp_pkey_ctx_free_cached_data(ctx, cmd, name); |
1214 | 0 | if (name != NULL) { |
1215 | 0 | ctx->cached_parameters.dist_id_name = OPENSSL_strdup(name); |
1216 | 0 | if (ctx->cached_parameters.dist_id_name == NULL) |
1217 | 0 | return 0; |
1218 | 0 | } |
1219 | 0 | if (data_len > 0) { |
1220 | 0 | ctx->cached_parameters.dist_id = OPENSSL_memdup(data, data_len); |
1221 | 0 | if (ctx->cached_parameters.dist_id == NULL) |
1222 | 0 | return 0; |
1223 | 0 | } |
1224 | 0 | ctx->cached_parameters.dist_id_set = 1; |
1225 | 0 | ctx->cached_parameters.dist_id_len = data_len; |
1226 | 0 | break; |
1227 | 0 | } |
1228 | 0 | return 1; |
1229 | 0 | } |
1230 | | |
1231 | | static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx, |
1232 | | int cmd, const char *name) |
1233 | 0 | { |
1234 | 0 | cmd = decode_cmd(cmd, name); |
1235 | 0 | switch (cmd) { |
1236 | 0 | case EVP_PKEY_CTRL_SET1_ID: |
1237 | 0 | OPENSSL_free(ctx->cached_parameters.dist_id); |
1238 | 0 | OPENSSL_free(ctx->cached_parameters.dist_id_name); |
1239 | 0 | ctx->cached_parameters.dist_id = NULL; |
1240 | 0 | ctx->cached_parameters.dist_id_name = NULL; |
1241 | 0 | break; |
1242 | 0 | } |
1243 | 0 | } |
1244 | | |
1245 | | static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx) |
1246 | 0 | { |
1247 | 0 | evp_pkey_ctx_free_cached_data(ctx, EVP_PKEY_CTRL_SET1_ID, NULL); |
1248 | 0 | } |
1249 | | |
1250 | | int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx) |
1251 | 0 | { |
1252 | 0 | int ret = 1; |
1253 | |
|
1254 | 0 | if (ret && ctx->cached_parameters.dist_id_set) { |
1255 | 0 | const char *name = ctx->cached_parameters.dist_id_name; |
1256 | 0 | const void *val = ctx->cached_parameters.dist_id; |
1257 | 0 | size_t len = ctx->cached_parameters.dist_id_len; |
1258 | |
|
1259 | 0 | if (name != NULL) |
1260 | 0 | ret = evp_pkey_ctx_ctrl_str_int(ctx, name, val); |
1261 | 0 | else |
1262 | 0 | ret = evp_pkey_ctx_ctrl_int(ctx, -1, ctx->operation, |
1263 | 0 | EVP_PKEY_CTRL_SET1_ID, |
1264 | 0 | (int)len, (void *)val); |
1265 | 0 | } |
1266 | |
|
1267 | 0 | return ret; |
1268 | 0 | } |
1269 | | |
1270 | | OSSL_LIB_CTX *EVP_PKEY_CTX_get0_libctx(EVP_PKEY_CTX *ctx) |
1271 | 0 | { |
1272 | 0 | return ctx->libctx; |
1273 | 0 | } |
1274 | | |
1275 | | const char *EVP_PKEY_CTX_get0_propq(const EVP_PKEY_CTX *ctx) |
1276 | 0 | { |
1277 | 0 | return ctx->propquery; |
1278 | 0 | } |
1279 | | |
1280 | | const OSSL_PROVIDER *EVP_PKEY_CTX_get0_provider(const EVP_PKEY_CTX *ctx) |
1281 | 0 | { |
1282 | 0 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) { |
1283 | 0 | if (ctx->op.sig.signature != NULL) |
1284 | 0 | return EVP_SIGNATURE_get0_provider(ctx->op.sig.signature); |
1285 | 0 | } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { |
1286 | 0 | if (ctx->op.kex.exchange != NULL) |
1287 | 0 | return EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange); |
1288 | 0 | } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) { |
1289 | 0 | if (ctx->op.encap.kem != NULL) |
1290 | 0 | return EVP_KEM_get0_provider(ctx->op.encap.kem); |
1291 | 0 | } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) { |
1292 | 0 | if (ctx->op.ciph.cipher != NULL) |
1293 | 0 | return EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher); |
1294 | 0 | } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) { |
1295 | 0 | if (ctx->keymgmt != NULL) |
1296 | 0 | return EVP_KEYMGMT_get0_provider(ctx->keymgmt); |
1297 | 0 | } |
1298 | | |
1299 | 0 | return NULL; |
1300 | 0 | } |
1301 | | |
1302 | | /* Utility functions to send a string of hex string to a ctrl */ |
1303 | | |
1304 | | int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str) |
1305 | 0 | { |
1306 | 0 | size_t len; |
1307 | |
|
1308 | 0 | len = strlen(str); |
1309 | 0 | if (len > INT_MAX) |
1310 | 0 | return -1; |
1311 | 0 | return EVP_PKEY_CTX_ctrl(ctx, -1, -1, cmd, (int)len, (void *)str); |
1312 | 0 | } |
1313 | | |
1314 | | int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex) |
1315 | 0 | { |
1316 | 0 | unsigned char *bin; |
1317 | 0 | long binlen; |
1318 | 0 | int rv = -1; |
1319 | |
|
1320 | 0 | bin = OPENSSL_hexstr2buf(hex, &binlen); |
1321 | 0 | if (bin == NULL) |
1322 | 0 | return 0; |
1323 | 0 | if (binlen <= INT_MAX) |
1324 | 0 | rv = EVP_PKEY_CTX_ctrl(ctx, -1, -1, cmd, binlen, bin); |
1325 | 0 | OPENSSL_free(bin); |
1326 | 0 | return rv; |
1327 | 0 | } |
1328 | | |
1329 | | /* Pass a message digest to a ctrl */ |
1330 | | int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md) |
1331 | 0 | { |
1332 | 0 | const EVP_MD *m; |
1333 | |
|
1334 | 0 | if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) { |
1335 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_DIGEST); |
1336 | 0 | return 0; |
1337 | 0 | } |
1338 | 0 | return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m); |
1339 | 0 | } |
1340 | | |
1341 | | int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx) |
1342 | 0 | { |
1343 | 0 | return ctx->operation; |
1344 | 0 | } |
1345 | | |
1346 | | void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen) |
1347 | 0 | { |
1348 | 0 | ctx->keygen_info = dat; |
1349 | 0 | ctx->keygen_info_count = datlen; |
1350 | 0 | } |
1351 | | |
1352 | | void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data) |
1353 | 0 | { |
1354 | 0 | ctx->data = data; |
1355 | 0 | } |
1356 | | |
1357 | | void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx) |
1358 | 0 | { |
1359 | 0 | return ctx->data; |
1360 | 0 | } |
1361 | | |
1362 | | EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx) |
1363 | 0 | { |
1364 | 0 | return ctx->pkey; |
1365 | 0 | } |
1366 | | |
1367 | | EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx) |
1368 | 0 | { |
1369 | 0 | return ctx->peerkey; |
1370 | 0 | } |
1371 | | |
1372 | | void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data) |
1373 | 0 | { |
1374 | 0 | ctx->app_data = data; |
1375 | 0 | } |
1376 | | |
1377 | | void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx) |
1378 | 0 | { |
1379 | 0 | return ctx->app_data; |
1380 | 0 | } |
1381 | | #endif /* FIPS_MODULE */ |