]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
33388b44 | 2 | * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved. |
0b6f3c66 | 3 | * |
4a8b0c55 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
62867571 RS |
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 | |
0b6f3c66 DSH |
8 | */ |
9 | ||
ada66e78 | 10 | /* |
b03ec3b5 | 11 | * Low level key APIs (DH etc) are deprecated for public use, but still ok for |
ada66e78 P |
12 | * internal use. |
13 | */ | |
14 | #include "internal/deprecated.h" | |
15 | ||
0b6f3c66 DSH |
16 | #include <stdio.h> |
17 | #include <stdlib.h> | |
3c27208f | 18 | #include <openssl/engine.h> |
33bed28b | 19 | #include <openssl/evp.h> |
99119000 | 20 | #include <openssl/x509v3.h> |
35aca9ec MC |
21 | #include <openssl/core_names.h> |
22 | #include <openssl/dh.h> | |
89abd1b6 | 23 | #include <openssl/rsa.h> |
ac2d58c7 | 24 | #include <openssl/kdf.h> |
35aca9ec | 25 | #include "internal/cryptlib.h" |
25f2138b DMSP |
26 | #include "crypto/asn1.h" |
27 | #include "crypto/evp.h" | |
7165593c SL |
28 | #include "crypto/dh.h" |
29 | #include "internal/ffc.h" | |
99119000 | 30 | #include "internal/numbers.h" |
390acbeb | 31 | #include "internal/provider.h" |
706457b7 | 32 | #include "evp_local.h" |
0b6f3c66 | 33 | |
f844f9eb | 34 | #ifndef FIPS_MODULE |
e683582b | 35 | |
86df26b3 RL |
36 | static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx, |
37 | int keytype, int optype, | |
38 | int cmd, const char *name, | |
39 | const void *data, size_t data_len); | |
40 | static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx, | |
41 | int cmd, const char *name); | |
42 | static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx); | |
43 | ||
19bd1fa1 | 44 | typedef const EVP_PKEY_METHOD *(*pmeth_fn)(void); |
0f113f3e | 45 | typedef int sk_cmp_fn_type(const char *const *a, const char *const *b); |
5ce278a7 | 46 | |
df2ee0e2 | 47 | static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL; |
0b6f3c66 | 48 | |
cefa762e | 49 | /* This array needs to be in order of NIDs */ |
19bd1fa1 | 50 | static pmeth_fn standard_methods[] = { |
e683582b | 51 | # ifndef OPENSSL_NO_RSA |
19bd1fa1 | 52 | rsa_pkey_method, |
e683582b SL |
53 | # endif |
54 | # ifndef OPENSSL_NO_DH | |
19bd1fa1 | 55 | dh_pkey_method, |
e683582b SL |
56 | # endif |
57 | # ifndef OPENSSL_NO_DSA | |
19bd1fa1 | 58 | dsa_pkey_method, |
e683582b SL |
59 | # endif |
60 | # ifndef OPENSSL_NO_EC | |
19bd1fa1 | 61 | ec_pkey_method, |
e683582b SL |
62 | # endif |
63 | # ifndef OPENSSL_NO_RSA | |
19bd1fa1 | 64 | rsa_pss_pkey_method, |
e683582b SL |
65 | # endif |
66 | # ifndef OPENSSL_NO_DH | |
19bd1fa1 | 67 | dhx_pkey_method, |
e683582b | 68 | # endif |
e683582b | 69 | # ifndef OPENSSL_NO_EC |
19bd1fa1 PS |
70 | ecx25519_pkey_method, |
71 | ecx448_pkey_method, | |
e683582b | 72 | # endif |
e683582b | 73 | # ifndef OPENSSL_NO_EC |
19bd1fa1 PS |
74 | ed25519_pkey_method, |
75 | ed448_pkey_method, | |
e683582b SL |
76 | # endif |
77 | # ifndef OPENSSL_NO_SM2 | |
19bd1fa1 | 78 | sm2_pkey_method, |
e683582b | 79 | # endif |
0f113f3e | 80 | }; |
0b6f3c66 | 81 | |
19bd1fa1 PS |
82 | DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func); |
83 | ||
84 | static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b) | |
85 | { | |
86 | return ((*a)->pkey_id - ((**b)())->pkey_id); | |
87 | } | |
88 | ||
89 | IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func); | |
babb3798 | 90 | |
0f113f3e MC |
91 | static int pmeth_cmp(const EVP_PKEY_METHOD *const *a, |
92 | const EVP_PKEY_METHOD *const *b) | |
93 | { | |
94 | return ((*a)->pkey_id - (*b)->pkey_id); | |
95 | } | |
0b6f3c66 | 96 | |
c9777d26 | 97 | const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type) |
0f113f3e | 98 | { |
19bd1fa1 | 99 | pmeth_fn *ret; |
0f113f3e | 100 | EVP_PKEY_METHOD tmp; |
19bd1fa1 | 101 | const EVP_PKEY_METHOD *t = &tmp; |
12a765a5 | 102 | |
0f113f3e MC |
103 | tmp.pkey_id = type; |
104 | if (app_pkey_methods) { | |
105 | int idx; | |
106 | idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp); | |
107 | if (idx >= 0) | |
108 | return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx); | |
109 | } | |
19bd1fa1 PS |
110 | ret = OBJ_bsearch_pmeth_func(&t, standard_methods, |
111 | sizeof(standard_methods) / | |
112 | sizeof(pmeth_fn)); | |
12a765a5 | 113 | if (ret == NULL || *ret == NULL) |
0f113f3e | 114 | return NULL; |
19bd1fa1 | 115 | return (**ret)(); |
0f113f3e | 116 | } |
0b6f3c66 | 117 | |
e683582b SL |
118 | EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags) |
119 | { | |
120 | EVP_PKEY_METHOD *pmeth; | |
121 | ||
122 | pmeth = OPENSSL_zalloc(sizeof(*pmeth)); | |
123 | if (pmeth == NULL) { | |
124 | EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE); | |
125 | return NULL; | |
126 | } | |
127 | ||
128 | pmeth->pkey_id = id; | |
129 | pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC; | |
130 | return pmeth; | |
131 | } | |
50914496 | 132 | |
86df26b3 RL |
133 | /* Three possible states: */ |
134 | # define EVP_PKEY_STATE_UNKNOWN 0 | |
135 | # define EVP_PKEY_STATE_LEGACY 1 | |
136 | # define EVP_PKEY_STATE_PROVIDER 2 | |
137 | ||
138 | static int evp_pkey_ctx_state(EVP_PKEY_CTX *ctx) | |
139 | { | |
140 | if (ctx->operation == EVP_PKEY_OP_UNDEFINED) | |
141 | return EVP_PKEY_STATE_UNKNOWN; | |
142 | ||
143 | if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) | |
144 | && ctx->op.kex.exchprovctx != NULL) | |
145 | || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) | |
146 | && ctx->op.sig.sigprovctx != NULL) | |
147 | || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) | |
148 | && ctx->op.ciph.ciphprovctx != NULL) | |
149 | || (EVP_PKEY_CTX_IS_GEN_OP(ctx) | |
150 | && ctx->op.keymgmt.genctx != NULL)) | |
151 | return EVP_PKEY_STATE_PROVIDER; | |
152 | ||
153 | return EVP_PKEY_STATE_LEGACY; | |
154 | } | |
155 | ||
50914496 RL |
156 | static void help_get_legacy_alg_type_from_keymgmt(const char *keytype, |
157 | void *arg) | |
158 | { | |
159 | int *type = arg; | |
160 | ||
161 | if (*type == NID_undef) | |
162 | *type = evp_pkey_name2type(keytype); | |
163 | } | |
164 | ||
165 | static int get_legacy_alg_type_from_keymgmt(const EVP_KEYMGMT *keymgmt) | |
166 | { | |
167 | int type = NID_undef; | |
168 | ||
169 | EVP_KEYMGMT_names_do_all(keymgmt, help_get_legacy_alg_type_from_keymgmt, | |
170 | &type); | |
171 | return type; | |
172 | } | |
f844f9eb | 173 | #endif /* FIPS_MODULE */ |
e683582b | 174 | |
b533510f MC |
175 | static int is_legacy_alg(int id, const char *keytype) |
176 | { | |
177 | #ifndef FIPS_MODULE | |
178 | /* Certain EVP_PKEY keytypes are only available in legacy form */ | |
50914496 RL |
179 | if (id == -1) |
180 | id = evp_pkey_name2type(keytype); | |
181 | ||
b533510f MC |
182 | switch (id) { |
183 | /* | |
50914496 | 184 | * TODO(3.0): Remove SM2 when they are converted to have provider |
b533510f MC |
185 | * support |
186 | */ | |
187 | case EVP_PKEY_SM2: | |
b533510f MC |
188 | return 1; |
189 | default: | |
190 | return 0; | |
191 | } | |
192 | #else | |
193 | return 0; | |
194 | #endif | |
195 | } | |
196 | ||
3ee348b0 RL |
197 | static EVP_PKEY_CTX *int_ctx_new(OPENSSL_CTX *libctx, |
198 | EVP_PKEY *pkey, ENGINE *e, | |
4b9e90f4 | 199 | const char *keytype, const char *propquery, |
a07c17ef | 200 | int id) |
e683582b | 201 | |
0f113f3e | 202 | { |
50914496 | 203 | EVP_PKEY_CTX *ret = NULL; |
d0ea49a8 | 204 | const EVP_PKEY_METHOD *pmeth = NULL; |
4b9e90f4 | 205 | EVP_KEYMGMT *keymgmt = NULL; |
d0ea49a8 RL |
206 | |
207 | /* | |
50914496 | 208 | * If the given |pkey| is provided, we extract the keytype from its |
5e5bc836 | 209 | * keymgmt and skip over the legacy code. |
982efd77 | 210 | */ |
5e5bc836 | 211 | if (pkey != NULL && evp_pkey_is_provided(pkey)) { |
982efd77 RL |
212 | /* If we have an engine, something went wrong somewhere... */ |
213 | if (!ossl_assert(e == NULL)) | |
214 | return NULL; | |
4b9e90f4 | 215 | keytype = evp_first_name(pkey->keymgmt->prov, pkey->keymgmt->name_id); |
982efd77 RL |
216 | goto common; |
217 | } | |
50914496 | 218 | |
f844f9eb | 219 | #ifndef FIPS_MODULE |
50914496 RL |
220 | /* |
221 | * TODO(3.0) This legacy code section should be removed when we stop | |
222 | * supporting engines | |
223 | */ | |
d0ea49a8 | 224 | /* BEGIN legacy */ |
0f113f3e | 225 | if (id == -1) { |
50914496 RL |
226 | if (pkey != NULL) |
227 | id = pkey->type; | |
228 | else if (keytype != NULL) | |
229 | id = evp_pkey_name2type(keytype); | |
230 | if (id == NID_undef) | |
231 | id = -1; | |
0f113f3e | 232 | } |
50914496 RL |
233 | /* If no ID was found here, we can only resort to find a keymgmt */ |
234 | if (id == -1) | |
235 | goto common; | |
60653e5b RL |
236 | |
237 | /* | |
238 | * Here, we extract what information we can for the purpose of | |
239 | * supporting usage with implementations from providers, to make | |
240 | * for a smooth transition from legacy stuff to provider based stuff. | |
241 | * | |
242 | * If an engine is given, this is entirely legacy, and we should not | |
243 | * pretend anything else, so we only set the name when no engine is | |
244 | * given. If both are already given, someone made a mistake, and | |
245 | * since that can only happen internally, it's safe to make an | |
246 | * assertion. | |
247 | */ | |
4b9e90f4 | 248 | if (!ossl_assert(e == NULL || keytype == NULL)) |
60653e5b RL |
249 | return NULL; |
250 | if (e == NULL) | |
4b9e90f4 | 251 | keytype = OBJ_nid2sn(id); |
60653e5b | 252 | |
e683582b | 253 | # ifndef OPENSSL_NO_ENGINE |
c2976edf | 254 | if (e == NULL && pkey != NULL) |
d19b01ad | 255 | e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine; |
0f113f3e MC |
256 | /* Try to find an ENGINE which implements this method */ |
257 | if (e) { | |
258 | if (!ENGINE_init(e)) { | |
259 | EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB); | |
260 | return NULL; | |
261 | } | |
c2976edf | 262 | } else { |
0f113f3e | 263 | e = ENGINE_get_pkey_meth_engine(id); |
c2976edf | 264 | } |
0f113f3e MC |
265 | |
266 | /* | |
0d4fb843 | 267 | * If an ENGINE handled this method look it up. Otherwise use internal |
0f113f3e MC |
268 | * tables. |
269 | */ | |
50914496 | 270 | if (e != NULL) |
0f113f3e | 271 | pmeth = ENGINE_get_pkey_meth(e, id); |
50914496 | 272 | else |
e683582b | 273 | # endif |
0f113f3e | 274 | pmeth = EVP_PKEY_meth_find(id); |
c9777d26 | 275 | |
d0ea49a8 | 276 | /* END legacy */ |
f844f9eb | 277 | #endif /* FIPS_MODULE */ |
d0ea49a8 | 278 | common: |
4b9e90f4 RL |
279 | /* |
280 | * If there's no engine and there's a name, we try fetching a provider | |
281 | * implementation. | |
282 | */ | |
5fcb97c6 | 283 | if (e == NULL && keytype != NULL) { |
b533510f MC |
284 | int legacy = is_legacy_alg(id, keytype); |
285 | ||
50914496 RL |
286 | /* This could fail so ignore errors */ |
287 | if (legacy) | |
b533510f | 288 | ERR_set_mark(); |
b533510f | 289 | |
4b9e90f4 | 290 | keymgmt = EVP_KEYMGMT_fetch(libctx, keytype, propquery); |
50914496 | 291 | if (legacy) |
b533510f | 292 | ERR_pop_to_mark(); |
50914496 RL |
293 | else if (keymgmt == NULL) |
294 | return NULL; /* EVP_KEYMGMT_fetch() recorded an error */ | |
295 | ||
296 | #ifndef FIPS_MODULE | |
297 | /* | |
298 | * Chase down the legacy NID, as that might be needed for diverse | |
299 | * purposes, such as ensure that EVP_PKEY_type() can return sensible | |
300 | * values, or that there's a better chance to "downgrade" a key when | |
301 | * needed. We go through all keymgmt names, because the keytype | |
302 | * that's passed to this function doesn't necessarily translate | |
303 | * directly. | |
304 | * TODO: Remove this when #legacy keys are gone. | |
305 | */ | |
306 | if (keymgmt != NULL) { | |
307 | int tmp_id = get_legacy_alg_type_from_keymgmt(keymgmt); | |
308 | ||
309 | if (tmp_id != NID_undef) { | |
310 | if (id == -1) { | |
311 | id = tmp_id; | |
312 | } else { | |
313 | /* | |
314 | * It really really shouldn't differ. If it still does, | |
315 | * something is very wrong. | |
316 | */ | |
317 | if (!ossl_assert(id == tmp_id)) { | |
318 | EVPerr(EVP_F_INT_CTX_NEW, ERR_R_INTERNAL_ERROR); | |
319 | EVP_KEYMGMT_free(keymgmt); | |
320 | return NULL; | |
321 | } | |
322 | } | |
323 | } | |
b533510f | 324 | } |
50914496 RL |
325 | #endif |
326 | } | |
327 | ||
328 | if (pmeth == NULL && keymgmt == NULL) { | |
329 | EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM); | |
330 | } else { | |
331 | ret = OPENSSL_zalloc(sizeof(*ret)); | |
332 | if (ret == NULL) | |
333 | EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE); | |
5fcb97c6 | 334 | } |
4b9e90f4 | 335 | |
f844f9eb | 336 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
50914496 | 337 | if ((ret == NULL || pmeth == NULL) && e != NULL) |
7c96dbcd | 338 | ENGINE_finish(e); |
a63bf2c5 | 339 | #endif |
50914496 RL |
340 | |
341 | if (ret == NULL) { | |
342 | EVP_KEYMGMT_free(keymgmt); | |
0f113f3e MC |
343 | return NULL; |
344 | } | |
50914496 | 345 | |
3ee348b0 | 346 | ret->libctx = libctx; |
a07c17ef | 347 | ret->propquery = propquery; |
4b9e90f4 RL |
348 | ret->keytype = keytype; |
349 | ret->keymgmt = keymgmt; | |
50914496 | 350 | ret->legacy_keytype = id; /* TODO: Remove when #legacy key are gone */ |
0f113f3e MC |
351 | ret->engine = e; |
352 | ret->pmeth = pmeth; | |
353 | ret->operation = EVP_PKEY_OP_UNDEFINED; | |
354 | ret->pkey = pkey; | |
a6465b3f | 355 | if (pkey != NULL) |
03273d61 | 356 | EVP_PKEY_up_ref(pkey); |
0f113f3e | 357 | |
8b84b075 | 358 | if (pmeth != NULL && pmeth->init != NULL) { |
0f113f3e | 359 | if (pmeth->init(ret) <= 0) { |
83b4049a | 360 | ret->pmeth = NULL; |
0f113f3e MC |
361 | EVP_PKEY_CTX_free(ret); |
362 | return NULL; | |
363 | } | |
364 | } | |
365 | ||
366 | return ret; | |
367 | } | |
368 | ||
f844f9eb | 369 | /*- All methods below can also be used in FIPS_MODULE */ |
e683582b SL |
370 | |
371 | EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_name(OPENSSL_CTX *libctx, | |
372 | const char *name, | |
373 | const char *propquery) | |
374 | { | |
375 | return int_ctx_new(libctx, NULL, NULL, name, propquery, -1); | |
376 | } | |
377 | ||
2ee4a50a MC |
378 | EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_pkey(OPENSSL_CTX *libctx, EVP_PKEY *pkey, |
379 | const char *propquery) | |
e683582b | 380 | { |
2ee4a50a | 381 | return int_ctx_new(libctx, pkey, NULL, NULL, propquery, -1); |
e683582b SL |
382 | } |
383 | ||
864b89ce MC |
384 | void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx) |
385 | { | |
e683582b | 386 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) { |
864b89ce MC |
387 | if (ctx->op.sig.sigprovctx != NULL && ctx->op.sig.signature != NULL) |
388 | ctx->op.sig.signature->freectx(ctx->op.sig.sigprovctx); | |
389 | EVP_SIGNATURE_free(ctx->op.sig.signature); | |
fb1ecf85 RL |
390 | ctx->op.sig.sigprovctx = NULL; |
391 | ctx->op.sig.signature = NULL; | |
62f49b90 | 392 | } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { |
e683582b SL |
393 | if (ctx->op.kex.exchprovctx != NULL && ctx->op.kex.exchange != NULL) |
394 | ctx->op.kex.exchange->freectx(ctx->op.kex.exchprovctx); | |
395 | EVP_KEYEXCH_free(ctx->op.kex.exchange); | |
396 | ctx->op.kex.exchprovctx = NULL; | |
397 | ctx->op.kex.exchange = NULL; | |
62f49b90 SL |
398 | } |
399 | /* TODO(3.0): add dependancies and uncomment this when available for fips mode */ | |
f844f9eb | 400 | #ifndef FIPS_MODULE |
62f49b90 | 401 | else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) { |
2c938e2e MC |
402 | if (ctx->op.ciph.ciphprovctx != NULL && ctx->op.ciph.cipher != NULL) |
403 | ctx->op.ciph.cipher->freectx(ctx->op.ciph.ciphprovctx); | |
404 | EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher); | |
405 | ctx->op.ciph.ciphprovctx = NULL; | |
406 | ctx->op.ciph.cipher = NULL; | |
62924755 RL |
407 | } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) { |
408 | if (ctx->op.keymgmt.genctx != NULL && ctx->keymgmt != NULL) | |
409 | evp_keymgmt_gen_cleanup(ctx->keymgmt, ctx->op.keymgmt.genctx); | |
864b89ce | 410 | } |
e683582b | 411 | #endif |
864b89ce MC |
412 | } |
413 | ||
e683582b | 414 | void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx) |
0f113f3e | 415 | { |
e683582b SL |
416 | if (ctx == NULL) |
417 | return; | |
418 | if (ctx->pmeth && ctx->pmeth->cleanup) | |
419 | ctx->pmeth->cleanup(ctx); | |
b4faea50 | 420 | |
e683582b | 421 | evp_pkey_ctx_free_old_ops(ctx); |
86df26b3 RL |
422 | #ifndef FIPS_MODULE |
423 | evp_pkey_ctx_free_all_cached_data(ctx); | |
424 | #endif | |
e683582b | 425 | EVP_KEYMGMT_free(ctx->keymgmt); |
0f113f3e | 426 | |
e683582b SL |
427 | EVP_PKEY_free(ctx->pkey); |
428 | EVP_PKEY_free(ctx->peerkey); | |
f844f9eb | 429 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
e683582b SL |
430 | ENGINE_finish(ctx->engine); |
431 | #endif | |
432 | OPENSSL_free(ctx); | |
0f113f3e | 433 | } |
ba30bad5 | 434 | |
f844f9eb | 435 | #ifndef FIPS_MODULE |
e683582b | 436 | |
f830c68f | 437 | void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags, |
0f113f3e MC |
438 | const EVP_PKEY_METHOD *meth) |
439 | { | |
440 | if (ppkey_id) | |
441 | *ppkey_id = meth->pkey_id; | |
442 | if (pflags) | |
443 | *pflags = meth->flags; | |
444 | } | |
f830c68f DSH |
445 | |
446 | void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src) | |
0f113f3e | 447 | { |
4cbb196b AT |
448 | int pkey_id = dst->pkey_id; |
449 | int flags = dst->flags; | |
f830c68f | 450 | |
4cbb196b | 451 | *dst = *src; |
f830c68f | 452 | |
4cbb196b AT |
453 | /* We only copy the function pointers so restore the other values */ |
454 | dst->pkey_id = pkey_id; | |
455 | dst->flags = flags; | |
0f113f3e | 456 | } |
f830c68f | 457 | |
ba30bad5 | 458 | void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth) |
0f113f3e MC |
459 | { |
460 | if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC)) | |
461 | OPENSSL_free(pmeth); | |
462 | } | |
ba30bad5 | 463 | |
f5cda4cb | 464 | EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e) |
0f113f3e | 465 | { |
3ee348b0 | 466 | return int_ctx_new(NULL, pkey, e, NULL, NULL, -1); |
0f113f3e | 467 | } |
f5cda4cb DSH |
468 | |
469 | EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e) | |
0f113f3e | 470 | { |
3ee348b0 | 471 | return int_ctx_new(NULL, NULL, e, NULL, NULL, id); |
a07c17ef RL |
472 | } |
473 | ||
9fdcc21f | 474 | EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx) |
0f113f3e MC |
475 | { |
476 | EVP_PKEY_CTX *rctx; | |
ff64702b MC |
477 | |
478 | if (((pctx->pmeth == NULL) || (pctx->pmeth->copy == NULL)) | |
864b89ce MC |
479 | && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx) |
480 | && pctx->op.kex.exchprovctx == NULL) | |
481 | || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx) | |
482 | && pctx->op.sig.sigprovctx == NULL))) | |
0f113f3e | 483 | return NULL; |
e683582b | 484 | # ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
485 | /* Make sure it's safe to copy a pkey context using an ENGINE */ |
486 | if (pctx->engine && !ENGINE_init(pctx->engine)) { | |
487 | EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB); | |
488 | return 0; | |
489 | } | |
e683582b | 490 | # endif |
ff64702b | 491 | rctx = OPENSSL_zalloc(sizeof(*rctx)); |
3484236d F |
492 | if (rctx == NULL) { |
493 | EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 494 | return NULL; |
3484236d | 495 | } |
8bdcef40 | 496 | |
ff64702b MC |
497 | if (pctx->pkey != NULL) |
498 | EVP_PKEY_up_ref(pctx->pkey); | |
499 | rctx->pkey = pctx->pkey; | |
500 | rctx->operation = pctx->operation; | |
3ee348b0 | 501 | rctx->libctx = pctx->libctx; |
f23bc0b7 | 502 | rctx->keytype = pctx->keytype; |
a07c17ef | 503 | rctx->propquery = pctx->propquery; |
ff64702b | 504 | |
864b89ce MC |
505 | if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) { |
506 | if (pctx->op.kex.exchange != NULL) { | |
507 | rctx->op.kex.exchange = pctx->op.kex.exchange; | |
508 | if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) { | |
509 | OPENSSL_free(rctx); | |
510 | return NULL; | |
511 | } | |
ff64702b | 512 | } |
864b89ce MC |
513 | if (pctx->op.kex.exchprovctx != NULL) { |
514 | if (!ossl_assert(pctx->op.kex.exchange != NULL)) | |
515 | return NULL; | |
516 | rctx->op.kex.exchprovctx | |
517 | = pctx->op.kex.exchange->dupctx(pctx->op.kex.exchprovctx); | |
518 | if (rctx->op.kex.exchprovctx == NULL) { | |
519 | EVP_KEYEXCH_free(rctx->op.kex.exchange); | |
520 | OPENSSL_free(rctx); | |
521 | return NULL; | |
522 | } | |
523 | return rctx; | |
524 | } | |
525 | } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) { | |
526 | if (pctx->op.sig.signature != NULL) { | |
527 | rctx->op.sig.signature = pctx->op.sig.signature; | |
528 | if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) { | |
529 | OPENSSL_free(rctx); | |
530 | return NULL; | |
531 | } | |
532 | } | |
533 | if (pctx->op.sig.sigprovctx != NULL) { | |
534 | if (!ossl_assert(pctx->op.sig.signature != NULL)) | |
535 | return NULL; | |
536 | rctx->op.sig.sigprovctx | |
537 | = pctx->op.sig.signature->dupctx(pctx->op.sig.sigprovctx); | |
538 | if (rctx->op.sig.sigprovctx == NULL) { | |
539 | EVP_SIGNATURE_free(rctx->op.sig.signature); | |
540 | OPENSSL_free(rctx); | |
541 | return NULL; | |
542 | } | |
543 | return rctx; | |
ff64702b | 544 | } |
2c938e2e MC |
545 | } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) { |
546 | if (pctx->op.ciph.cipher != NULL) { | |
547 | rctx->op.ciph.cipher = pctx->op.ciph.cipher; | |
548 | if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher)) { | |
549 | OPENSSL_free(rctx); | |
550 | return NULL; | |
551 | } | |
552 | } | |
553 | if (pctx->op.ciph.ciphprovctx != NULL) { | |
554 | if (!ossl_assert(pctx->op.ciph.cipher != NULL)) | |
555 | return NULL; | |
556 | rctx->op.ciph.ciphprovctx | |
557 | = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.ciphprovctx); | |
558 | if (rctx->op.ciph.ciphprovctx == NULL) { | |
559 | EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher); | |
560 | OPENSSL_free(rctx); | |
561 | return NULL; | |
562 | } | |
563 | return rctx; | |
564 | } | |
ff64702b MC |
565 | } |
566 | ||
0f113f3e | 567 | rctx->pmeth = pctx->pmeth; |
e683582b | 568 | # ifndef OPENSSL_NO_ENGINE |
0f113f3e | 569 | rctx->engine = pctx->engine; |
e683582b | 570 | # endif |
8bdcef40 | 571 | |
0f113f3e | 572 | if (pctx->peerkey) |
03273d61 | 573 | EVP_PKEY_up_ref(pctx->peerkey); |
0f113f3e | 574 | rctx->peerkey = pctx->peerkey; |
8bdcef40 | 575 | |
0f113f3e MC |
576 | if (pctx->pmeth->copy(rctx, pctx) > 0) |
577 | return rctx; | |
8bdcef40 | 578 | |
83b4049a | 579 | rctx->pmeth = NULL; |
0f113f3e MC |
580 | EVP_PKEY_CTX_free(rctx); |
581 | return NULL; | |
8bdcef40 | 582 | |
0f113f3e | 583 | } |
8bdcef40 | 584 | |
ba30bad5 | 585 | int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth) |
0f113f3e MC |
586 | { |
587 | if (app_pkey_methods == NULL) { | |
588 | app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp); | |
3484236d F |
589 | if (app_pkey_methods == NULL){ |
590 | EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 591 | return 0; |
3484236d | 592 | } |
0f113f3e | 593 | } |
3484236d F |
594 | if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) { |
595 | EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 596 | return 0; |
3484236d | 597 | } |
0f113f3e MC |
598 | sk_EVP_PKEY_METHOD_sort(app_pkey_methods); |
599 | return 1; | |
600 | } | |
ba30bad5 | 601 | |
0822e89a PY |
602 | void evp_app_cleanup_int(void) |
603 | { | |
604 | if (app_pkey_methods != NULL) | |
605 | sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free); | |
606 | } | |
607 | ||
608 | int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth) | |
609 | { | |
610 | const EVP_PKEY_METHOD *ret; | |
611 | ||
612 | ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth); | |
613 | ||
614 | return ret == NULL ? 0 : 1; | |
615 | } | |
616 | ||
48ed9c23 DSH |
617 | size_t EVP_PKEY_meth_get_count(void) |
618 | { | |
619 | size_t rv = OSSL_NELEM(standard_methods); | |
620 | ||
621 | if (app_pkey_methods) | |
622 | rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods); | |
623 | return rv; | |
624 | } | |
625 | ||
626 | const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx) | |
627 | { | |
628 | if (idx < OSSL_NELEM(standard_methods)) | |
19bd1fa1 | 629 | return (standard_methods[idx])(); |
48ed9c23 DSH |
630 | if (app_pkey_methods == NULL) |
631 | return NULL; | |
632 | idx -= OSSL_NELEM(standard_methods); | |
633 | if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods)) | |
634 | return NULL; | |
635 | return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx); | |
636 | } | |
e683582b | 637 | #endif |
48ed9c23 | 638 | |
e683582b | 639 | int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params) |
0f113f3e | 640 | { |
e683582b SL |
641 | if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
642 | && ctx->op.kex.exchprovctx != NULL | |
643 | && ctx->op.kex.exchange != NULL | |
644 | && ctx->op.kex.exchange->set_ctx_params != NULL) | |
645 | return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx, | |
646 | params); | |
647 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) | |
648 | && ctx->op.sig.sigprovctx != NULL | |
649 | && ctx->op.sig.signature != NULL | |
650 | && ctx->op.sig.signature->set_ctx_params != NULL) | |
651 | return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx, | |
652 | params); | |
653 | if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) | |
654 | && ctx->op.ciph.ciphprovctx != NULL | |
655 | && ctx->op.ciph.cipher != NULL | |
656 | && ctx->op.ciph.cipher->set_ctx_params != NULL) | |
657 | return ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.ciphprovctx, | |
658 | params); | |
62924755 RL |
659 | if (EVP_PKEY_CTX_IS_GEN_OP(ctx) |
660 | && ctx->op.keymgmt.genctx != NULL | |
661 | && ctx->keymgmt != NULL | |
662 | && ctx->keymgmt->gen_set_params != NULL) | |
663 | return evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx, | |
664 | params); | |
e683582b | 665 | return 0; |
0f113f3e | 666 | } |
5da98aa6 | 667 | |
9c45222d MC |
668 | int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params) |
669 | { | |
4fe54d67 NT |
670 | if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
671 | && ctx->op.kex.exchprovctx != NULL | |
672 | && ctx->op.kex.exchange != NULL | |
673 | && ctx->op.kex.exchange->get_ctx_params != NULL) | |
674 | return ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.exchprovctx, | |
675 | params); | |
864b89ce MC |
676 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
677 | && ctx->op.sig.sigprovctx != NULL | |
678 | && ctx->op.sig.signature != NULL | |
679 | && ctx->op.sig.signature->get_ctx_params != NULL) | |
680 | return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.sigprovctx, | |
681 | params); | |
2c938e2e MC |
682 | if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
683 | && ctx->op.ciph.ciphprovctx != NULL | |
684 | && ctx->op.ciph.cipher != NULL | |
685 | && ctx->op.ciph.cipher->get_ctx_params != NULL) | |
686 | return ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.ciphprovctx, | |
687 | params); | |
9c45222d MC |
688 | return 0; |
689 | } | |
690 | ||
11a1b341 | 691 | #ifndef FIPS_MODULE |
9c45222d MC |
692 | const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx) |
693 | { | |
18ec26ba P |
694 | void *provctx; |
695 | ||
4fe54d67 NT |
696 | if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
697 | && ctx->op.kex.exchange != NULL | |
18ec26ba P |
698 | && ctx->op.kex.exchange->gettable_ctx_params != NULL) { |
699 | provctx = ossl_provider_ctx(EVP_KEYEXCH_provider(ctx->op.kex.exchange)); | |
700 | return ctx->op.kex.exchange->gettable_ctx_params(provctx); | |
701 | } | |
864b89ce MC |
702 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
703 | && ctx->op.sig.signature != NULL | |
18ec26ba P |
704 | && ctx->op.sig.signature->gettable_ctx_params != NULL) { |
705 | provctx = ossl_provider_ctx( | |
706 | EVP_SIGNATURE_provider(ctx->op.sig.signature)); | |
707 | return ctx->op.sig.signature->gettable_ctx_params(provctx); | |
708 | } | |
2c938e2e MC |
709 | if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
710 | && ctx->op.ciph.cipher != NULL | |
18ec26ba P |
711 | && ctx->op.ciph.cipher->gettable_ctx_params != NULL) { |
712 | provctx = ossl_provider_ctx( | |
713 | EVP_ASYM_CIPHER_provider(ctx->op.ciph.cipher)); | |
714 | return ctx->op.ciph.cipher->gettable_ctx_params(provctx); | |
715 | } | |
9c45222d MC |
716 | return NULL; |
717 | } | |
718 | ||
9c45222d MC |
719 | const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx) |
720 | { | |
18ec26ba P |
721 | void *provctx; |
722 | ||
864b89ce MC |
723 | if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
724 | && ctx->op.kex.exchange != NULL | |
18ec26ba P |
725 | && ctx->op.kex.exchange->settable_ctx_params != NULL) { |
726 | provctx = ossl_provider_ctx(EVP_KEYEXCH_provider(ctx->op.kex.exchange)); | |
727 | return ctx->op.kex.exchange->settable_ctx_params(provctx); | |
728 | } | |
864b89ce MC |
729 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
730 | && ctx->op.sig.signature != NULL | |
18ec26ba P |
731 | && ctx->op.sig.signature->settable_ctx_params != NULL) { |
732 | provctx = ossl_provider_ctx( | |
733 | EVP_SIGNATURE_provider(ctx->op.sig.signature)); | |
734 | return ctx->op.sig.signature->settable_ctx_params(provctx); | |
735 | } | |
2c938e2e MC |
736 | if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
737 | && ctx->op.ciph.cipher != NULL | |
18ec26ba P |
738 | && ctx->op.ciph.cipher->settable_ctx_params != NULL) { |
739 | provctx = ossl_provider_ctx( | |
740 | EVP_ASYM_CIPHER_provider(ctx->op.ciph.cipher)); | |
741 | return ctx->op.ciph.cipher->settable_ctx_params(provctx); | |
742 | } | |
62924755 | 743 | if (EVP_PKEY_CTX_IS_GEN_OP(ctx) |
5e77b79a | 744 | && ctx->keymgmt != NULL) |
e3efe7a5 | 745 | return EVP_KEYMGMT_gen_settable_params(ctx->keymgmt); |
9c45222d MC |
746 | |
747 | return NULL; | |
748 | } | |
749 | ||
4fe54d67 NT |
750 | /* |
751 | * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params(). | |
752 | * | |
753 | * Return 1 on success, 0 or negative for errors. | |
754 | * | |
755 | * In particular they return -2 if any of the params is not supported. | |
756 | * | |
f844f9eb | 757 | * They are not available in FIPS_MODULE as they depend on |
4fe54d67 NT |
758 | * - EVP_PKEY_CTX_{get,set}_params() |
759 | * - EVP_PKEY_CTX_{gettable,settable}_params() | |
760 | * | |
761 | */ | |
762 | int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params) | |
763 | { | |
764 | const OSSL_PARAM *p; | |
765 | ||
766 | if (ctx == NULL || params == NULL) | |
767 | return 0; | |
768 | ||
769 | for (p = params; p->key != NULL; p++) { | |
770 | /* Check the ctx actually understands this parameter */ | |
771 | if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_settable_params(ctx), | |
772 | p->key) == NULL ) | |
773 | return -2; | |
774 | } | |
775 | ||
776 | return EVP_PKEY_CTX_set_params(ctx, params); | |
777 | } | |
778 | ||
779 | int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params) | |
780 | { | |
781 | const OSSL_PARAM *p; | |
782 | ||
783 | if (ctx == NULL || params == NULL) | |
784 | return 0; | |
785 | ||
786 | for (p = params; p->key != NULL; p++ ) { | |
787 | /* Check the ctx actually understands this parameter */ | |
788 | if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_gettable_params(ctx), | |
789 | p->key) == NULL ) | |
790 | return -2; | |
791 | } | |
792 | ||
793 | return EVP_PKEY_CTX_get_params(ctx, params); | |
794 | } | |
795 | ||
e683582b | 796 | # ifndef OPENSSL_NO_DH |
35aca9ec MC |
797 | int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad) |
798 | { | |
799 | OSSL_PARAM dh_pad_params[2]; | |
1c3ace68 | 800 | unsigned int upad = pad; |
35aca9ec | 801 | |
864b89ce MC |
802 | /* We use EVP_PKEY_CTX_ctrl return values */ |
803 | if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { | |
804 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
805 | return -2; | |
806 | } | |
807 | ||
35aca9ec | 808 | /* TODO(3.0): Remove this eventually when no more legacy */ |
864b89ce | 809 | if (ctx->op.kex.exchprovctx == NULL) |
35aca9ec MC |
810 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE, |
811 | EVP_PKEY_CTRL_DH_PAD, pad, NULL); | |
812 | ||
1c3ace68 | 813 | dh_pad_params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &upad); |
35aca9ec MC |
814 | dh_pad_params[1] = OSSL_PARAM_construct_end(); |
815 | ||
816 | return EVP_PKEY_CTX_set_params(ctx, dh_pad_params); | |
817 | } | |
e683582b | 818 | # endif |
35aca9ec | 819 | |
9c45222d MC |
820 | int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md) |
821 | { | |
ac2d58c7 | 822 | OSSL_PARAM sig_md_params[2], *p = sig_md_params; |
9c45222d MC |
823 | /* 80 should be big enough */ |
824 | char name[80] = ""; | |
825 | const EVP_MD *tmp; | |
826 | ||
864b89ce | 827 | if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) { |
9c45222d MC |
828 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
829 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
830 | return -2; | |
831 | } | |
832 | ||
833 | /* TODO(3.0): Remove this eventually when no more legacy */ | |
864b89ce | 834 | if (ctx->op.sig.sigprovctx == NULL) |
9c45222d MC |
835 | return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, |
836 | EVP_PKEY_CTRL_GET_MD, 0, (void *)(md)); | |
837 | ||
838 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, | |
839 | name, | |
840 | sizeof(name)); | |
ac2d58c7 | 841 | *p = OSSL_PARAM_construct_end(); |
9c45222d MC |
842 | |
843 | if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params)) | |
844 | return 0; | |
845 | ||
7606bed9 | 846 | tmp = evp_get_digestbyname_ex(ctx->libctx, name); |
9c45222d MC |
847 | if (tmp == NULL) |
848 | return 0; | |
849 | ||
850 | *md = tmp; | |
851 | ||
852 | return 1; | |
853 | } | |
854 | ||
05d2f72e MC |
855 | static int evp_pkey_ctx_set_md(EVP_PKEY_CTX *ctx, const EVP_MD *md, |
856 | int fallback, const char *param, int op, | |
857 | int ctrl) | |
4889dadc | 858 | { |
05d2f72e | 859 | OSSL_PARAM md_params[2], *p = md_params; |
4889dadc MC |
860 | const char *name; |
861 | ||
05d2f72e | 862 | if (ctx == NULL || (ctx->operation & op) == 0) { |
9c45222d MC |
863 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
864 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
865 | return -2; | |
866 | } | |
867 | ||
4889dadc | 868 | /* TODO(3.0): Remove this eventually when no more legacy */ |
05d2f72e MC |
869 | if (fallback) |
870 | return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, 0, (void *)(md)); | |
4889dadc | 871 | |
9c45222d MC |
872 | if (md == NULL) { |
873 | name = ""; | |
9c45222d | 874 | } else { |
9c45222d MC |
875 | name = EVP_MD_name(md); |
876 | } | |
4889dadc | 877 | |
05d2f72e | 878 | *p++ = OSSL_PARAM_construct_utf8_string(param, |
9c45222d MC |
879 | /* |
880 | * Cast away the const. This is read | |
881 | * only so should be safe | |
882 | */ | |
8b6ffd40 | 883 | (char *)name, 0); |
ac2d58c7 | 884 | *p = OSSL_PARAM_construct_end(); |
4889dadc | 885 | |
05d2f72e MC |
886 | return EVP_PKEY_CTX_set_params(ctx, md_params); |
887 | } | |
888 | ||
889 | int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) | |
890 | { | |
891 | return evp_pkey_ctx_set_md(ctx, md, ctx->op.sig.sigprovctx == NULL, | |
892 | OSSL_SIGNATURE_PARAM_DIGEST, | |
893 | EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD); | |
4889dadc MC |
894 | } |
895 | ||
ac2d58c7 MC |
896 | int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) |
897 | { | |
05d2f72e MC |
898 | return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.exchprovctx == NULL, |
899 | OSSL_KDF_PARAM_DIGEST, | |
900 | EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_TLS_MD); | |
901 | } | |
902 | ||
903 | static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX *ctx, int fallback, | |
904 | const char *param, int op, int ctrl, | |
905 | const unsigned char *data, | |
906 | int datalen) | |
907 | { | |
908 | OSSL_PARAM octet_string_params[2], *p = octet_string_params; | |
ac2d58c7 | 909 | |
5d51925a | 910 | if (ctx == NULL || (ctx->operation & op) == 0) { |
ac2d58c7 MC |
911 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
912 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
913 | return -2; | |
914 | } | |
915 | ||
916 | /* TODO(3.0): Remove this eventually when no more legacy */ | |
05d2f72e MC |
917 | if (fallback) |
918 | return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data)); | |
ac2d58c7 | 919 | |
05d2f72e MC |
920 | if (datalen < 0) { |
921 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH); | |
922 | return 0; | |
ac2d58c7 MC |
923 | } |
924 | ||
05d2f72e | 925 | *p++ = OSSL_PARAM_construct_octet_string(param, |
ac2d58c7 MC |
926 | /* |
927 | * Cast away the const. This is read | |
928 | * only so should be safe | |
929 | */ | |
05d2f72e MC |
930 | (unsigned char *)data, |
931 | (size_t)datalen); | |
194de849 | 932 | *p = OSSL_PARAM_construct_end(); |
ac2d58c7 | 933 | |
05d2f72e | 934 | return EVP_PKEY_CTX_set_params(ctx, octet_string_params); |
ac2d58c7 MC |
935 | } |
936 | ||
937 | int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX *ctx, | |
938 | const unsigned char *sec, int seclen) | |
939 | { | |
05d2f72e MC |
940 | return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL, |
941 | OSSL_KDF_PARAM_SECRET, | |
942 | EVP_PKEY_OP_DERIVE, | |
943 | EVP_PKEY_CTRL_TLS_SECRET, | |
944 | sec, seclen); | |
945 | } | |
ac2d58c7 | 946 | |
05d2f72e MC |
947 | int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX *ctx, |
948 | const unsigned char *seed, int seedlen) | |
949 | { | |
950 | return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL, | |
951 | OSSL_KDF_PARAM_SEED, | |
952 | EVP_PKEY_OP_DERIVE, | |
953 | EVP_PKEY_CTRL_TLS_SEED, | |
954 | seed, seedlen); | |
955 | } | |
ac2d58c7 | 956 | |
05d2f72e MC |
957 | int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) |
958 | { | |
959 | return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.exchprovctx == NULL, | |
960 | OSSL_KDF_PARAM_DIGEST, | |
961 | EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_HKDF_MD); | |
962 | } | |
ac2d58c7 | 963 | |
05d2f72e MC |
964 | int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx, |
965 | const unsigned char *salt, int saltlen) | |
966 | { | |
967 | return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL, | |
968 | OSSL_KDF_PARAM_SALT, | |
969 | EVP_PKEY_OP_DERIVE, | |
970 | EVP_PKEY_CTRL_HKDF_SALT, | |
971 | salt, saltlen); | |
972 | } | |
ac2d58c7 | 973 | |
05d2f72e MC |
974 | int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx, |
975 | const unsigned char *key, int keylen) | |
976 | { | |
977 | return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL, | |
978 | OSSL_KDF_PARAM_KEY, | |
979 | EVP_PKEY_OP_DERIVE, | |
980 | EVP_PKEY_CTRL_HKDF_KEY, | |
981 | key, keylen); | |
982 | } | |
ac2d58c7 | 983 | |
05d2f72e MC |
984 | int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx, |
985 | const unsigned char *info, int infolen) | |
986 | { | |
987 | return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL, | |
988 | OSSL_KDF_PARAM_INFO, | |
989 | EVP_PKEY_OP_DERIVE, | |
990 | EVP_PKEY_CTRL_HKDF_INFO, | |
991 | info, infolen); | |
ac2d58c7 MC |
992 | } |
993 | ||
05d2f72e | 994 | int EVP_PKEY_CTX_hkdf_mode(EVP_PKEY_CTX *ctx, int mode) |
ac2d58c7 | 995 | { |
05d2f72e | 996 | OSSL_PARAM int_params[2], *p = int_params; |
ac2d58c7 MC |
997 | |
998 | if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { | |
999 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
1000 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
1001 | return -2; | |
1002 | } | |
1003 | ||
1004 | /* TODO(3.0): Remove this eventually when no more legacy */ | |
1005 | if (ctx->op.kex.exchprovctx == NULL) | |
1006 | return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE, | |
05d2f72e MC |
1007 | EVP_PKEY_CTRL_HKDF_MODE, mode, NULL); |
1008 | ||
ac2d58c7 | 1009 | |
05d2f72e MC |
1010 | if (mode < 0) { |
1011 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE); | |
ac2d58c7 MC |
1012 | return 0; |
1013 | } | |
1014 | ||
05d2f72e | 1015 | *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode); |
194de849 | 1016 | *p = OSSL_PARAM_construct_end(); |
ac2d58c7 | 1017 | |
05d2f72e | 1018 | return EVP_PKEY_CTX_set_params(ctx, int_params); |
ac2d58c7 MC |
1019 | } |
1020 | ||
194de849 MC |
1021 | int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX *ctx, const char *pass, |
1022 | int passlen) | |
1023 | { | |
1024 | return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL, | |
1025 | OSSL_KDF_PARAM_PASSWORD, | |
1026 | EVP_PKEY_OP_DERIVE, | |
1027 | EVP_PKEY_CTRL_PASS, | |
1028 | (const unsigned char *)pass, passlen); | |
1029 | } | |
1030 | ||
1031 | int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX *ctx, | |
1032 | const unsigned char *salt, int saltlen) | |
1033 | { | |
1034 | return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL, | |
1035 | OSSL_KDF_PARAM_SALT, | |
1036 | EVP_PKEY_OP_DERIVE, | |
1037 | EVP_PKEY_CTRL_SCRYPT_SALT, | |
1038 | salt, saltlen); | |
1039 | } | |
1040 | ||
1041 | static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX *ctx, const char *param, | |
1042 | int op, int ctrl, uint64_t val) | |
1043 | { | |
1044 | OSSL_PARAM uint64_params[2], *p = uint64_params; | |
1045 | ||
1046 | if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { | |
1047 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
1048 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
1049 | return -2; | |
1050 | } | |
1051 | ||
1052 | /* TODO(3.0): Remove this eventually when no more legacy */ | |
1053 | if (ctx->op.kex.exchprovctx == NULL) | |
1054 | return EVP_PKEY_CTX_ctrl_uint64(ctx, -1, op, ctrl, val); | |
1055 | ||
1056 | *p++ = OSSL_PARAM_construct_uint64(param, &val); | |
1057 | *p = OSSL_PARAM_construct_end(); | |
1058 | ||
1059 | return EVP_PKEY_CTX_set_params(ctx, uint64_params); | |
1060 | } | |
1061 | ||
1062 | int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX *ctx, uint64_t n) | |
1063 | { | |
1064 | return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_N, | |
1065 | EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_N, | |
1066 | n); | |
1067 | } | |
1068 | ||
1069 | int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX *ctx, uint64_t r) | |
1070 | { | |
1071 | return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_R, | |
1072 | EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_R, | |
1073 | r); | |
1074 | } | |
1075 | ||
1076 | int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX *ctx, uint64_t p) | |
1077 | { | |
1078 | return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_P, | |
1079 | EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_P, | |
1080 | p); | |
1081 | } | |
1082 | ||
1083 | int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX *ctx, | |
1084 | uint64_t maxmem_bytes) | |
1085 | { | |
1086 | return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_MAXMEM, | |
1087 | EVP_PKEY_OP_DERIVE, | |
1088 | EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES, | |
1089 | maxmem_bytes); | |
1090 | } | |
1091 | ||
5d51925a MC |
1092 | int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX *ctx, const unsigned char *key, |
1093 | int keylen) | |
1094 | { | |
1095 | return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.keymgmt.genctx == NULL, | |
1096 | OSSL_PKEY_PARAM_PRIV_KEY, | |
1097 | EVP_PKEY_OP_KEYGEN, | |
1098 | EVP_PKEY_CTRL_SET_MAC_KEY, | |
1099 | key, keylen); | |
1100 | } | |
1101 | ||
8d6481f5 RL |
1102 | int evp_pkey_ctx_set1_id_prov(EVP_PKEY_CTX *ctx, const void *id, int len) |
1103 | { | |
1104 | OSSL_PARAM params[2], *p = params; | |
1105 | int ret; | |
1106 | ||
1107 | if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) { | |
1108 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
1109 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
1110 | return -2; | |
1111 | } | |
1112 | ||
1113 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_DIST_ID, | |
1114 | /* | |
1115 | * Cast away the const. This is | |
1116 | * read only so should be safe | |
1117 | */ | |
1118 | (void *)id, (size_t)len); | |
1119 | *p++ = OSSL_PARAM_construct_end(); | |
1120 | ||
1121 | ret = evp_pkey_ctx_set_params_strict(ctx, params); | |
1122 | if (ret == -2) | |
1123 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
1124 | return ret; | |
1125 | } | |
1126 | ||
1127 | int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int len) | |
1128 | { | |
1129 | return EVP_PKEY_CTX_ctrl(ctx, -1, -1, | |
1130 | EVP_PKEY_CTRL_SET1_ID, (int)len, (void*)(id)); | |
1131 | } | |
1132 | ||
1133 | static int get1_id_data(EVP_PKEY_CTX *ctx, void *id, size_t *id_len) | |
1134 | { | |
1135 | int ret; | |
1136 | void *tmp_id = NULL; | |
1137 | OSSL_PARAM params[2], *p = params; | |
1138 | ||
1139 | if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) { | |
1140 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
1141 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
1142 | return -2; | |
1143 | } | |
1144 | ||
1145 | *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_PKEY_PARAM_DIST_ID, | |
1146 | &tmp_id, 0); | |
1147 | *p++ = OSSL_PARAM_construct_end(); | |
1148 | ||
1149 | ret = evp_pkey_ctx_get_params_strict(ctx, params); | |
1150 | if (ret == -2) { | |
1151 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
1152 | } else if (ret > 0) { | |
1153 | size_t tmp_id_len = params[0].return_size; | |
1154 | ||
1155 | if (id != NULL) | |
1156 | memcpy(id, tmp_id, tmp_id_len); | |
1157 | if (id_len != NULL) | |
1158 | *id_len = tmp_id_len; | |
1159 | } | |
1160 | return ret; | |
1161 | } | |
1162 | ||
1163 | int evp_pkey_ctx_get1_id_prov(EVP_PKEY_CTX *ctx, void *id) | |
1164 | { | |
1165 | return get1_id_data(ctx, id, NULL); | |
1166 | } | |
1167 | ||
1168 | int evp_pkey_ctx_get1_id_len_prov(EVP_PKEY_CTX *ctx, size_t *id_len) | |
1169 | { | |
1170 | return get1_id_data(ctx, NULL, id_len); | |
1171 | } | |
1172 | ||
1173 | int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX *ctx, void *id) | |
1174 | { | |
1175 | return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET1_ID, 0, (void*)id); | |
1176 | } | |
1177 | ||
1178 | int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX *ctx, size_t *id_len) | |
1179 | { | |
1180 | return EVP_PKEY_CTX_ctrl(ctx, -1, -1, | |
1181 | EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void*)id_len); | |
1182 | } | |
1183 | ||
35aca9ec MC |
1184 | static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype, |
1185 | int cmd, int p1, void *p2) | |
1186 | { | |
86df26b3 RL |
1187 | switch (cmd) { |
1188 | case EVP_PKEY_CTRL_SET1_ID: | |
1189 | return evp_pkey_ctx_set1_id_prov(ctx, p2, p1); | |
1190 | case EVP_PKEY_CTRL_GET1_ID: | |
1191 | return evp_pkey_ctx_get1_id_prov(ctx, p2); | |
1192 | case EVP_PKEY_CTRL_GET1_ID_LEN: | |
1193 | return evp_pkey_ctx_get1_id_len_prov(ctx, p2); | |
1194 | } | |
1195 | ||
e683582b | 1196 | # ifndef OPENSSL_NO_DH |
116d2510 SL |
1197 | if (keytype == EVP_PKEY_DHX) { |
1198 | switch (cmd) { | |
1199 | case EVP_PKEY_CTRL_DH_KDF_TYPE: | |
1200 | return EVP_PKEY_CTX_set_dh_kdf_type(ctx, p1); | |
1201 | case EVP_PKEY_CTRL_DH_KDF_MD: | |
1202 | return EVP_PKEY_CTX_set_dh_kdf_md(ctx, p2); | |
1203 | case EVP_PKEY_CTRL_DH_KDF_OUTLEN: | |
1204 | return EVP_PKEY_CTX_set_dh_kdf_outlen(ctx, p1); | |
1205 | case EVP_PKEY_CTRL_DH_KDF_UKM: | |
1206 | return EVP_PKEY_CTX_set0_dh_kdf_ukm(ctx, p2, p1); | |
1207 | case EVP_PKEY_CTRL_DH_KDF_OID: | |
1208 | return EVP_PKEY_CTX_set0_dh_kdf_oid(ctx, p2); | |
1209 | case EVP_PKEY_CTRL_GET_DH_KDF_MD: | |
1210 | return EVP_PKEY_CTX_get_dh_kdf_md(ctx, p2); | |
1211 | case EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN: | |
1212 | return EVP_PKEY_CTX_get_dh_kdf_outlen(ctx, p2); | |
1213 | case EVP_PKEY_CTRL_GET_DH_KDF_UKM: | |
1214 | return EVP_PKEY_CTX_get0_dh_kdf_ukm(ctx, p2); | |
1215 | case EVP_PKEY_CTRL_GET_DH_KDF_OID: | |
1216 | return EVP_PKEY_CTX_get0_dh_kdf_oid(ctx, p2); | |
1217 | } | |
1218 | } | |
4fe54d67 NT |
1219 | if (keytype == EVP_PKEY_DH) { |
1220 | switch (cmd) { | |
1221 | case EVP_PKEY_CTRL_DH_PAD: | |
1222 | return EVP_PKEY_CTX_set_dh_pad(ctx, p1); | |
7165593c SL |
1223 | case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN: |
1224 | return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, p1); | |
1225 | case EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN: | |
1226 | return EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx, p1); | |
1227 | case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR: | |
1228 | return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, p1); | |
1229 | case EVP_PKEY_CTRL_DH_PARAMGEN_TYPE: | |
1230 | return EVP_PKEY_CTX_set_dh_paramgen_type(ctx, p1); | |
1231 | case EVP_PKEY_CTRL_DH_RFC5114: | |
1232 | return EVP_PKEY_CTX_set_dh_rfc5114(ctx, p1); | |
4fe54d67 NT |
1233 | } |
1234 | } | |
1235 | # endif | |
b03ec3b5 SL |
1236 | # ifndef OPENSSL_NO_DSA |
1237 | if (keytype == EVP_PKEY_DSA) { | |
1238 | switch (cmd) { | |
1239 | case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS: | |
1240 | return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, p1); | |
1241 | case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS: | |
1242 | return EVP_PKEY_CTX_set_dsa_paramgen_q_bits(ctx, p1); | |
1243 | case EVP_PKEY_CTRL_DSA_PARAMGEN_MD: | |
1244 | return EVP_PKEY_CTX_set_dsa_paramgen_md(ctx, p2); | |
1245 | } | |
1246 | } | |
1247 | # endif | |
4fe54d67 NT |
1248 | # ifndef OPENSSL_NO_EC |
1249 | if (keytype == EVP_PKEY_EC) { | |
1250 | switch (cmd) { | |
10d756a7 RL |
1251 | case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID: |
1252 | return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, p1); | |
6f4b7663 RL |
1253 | case EVP_PKEY_CTRL_EC_ECDH_COFACTOR: |
1254 | if (p1 == -2) { | |
1255 | return EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx); | |
1256 | } else if (p1 < -1 || p1 > 1) { | |
1257 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
1258 | return -2; | |
1259 | } else { | |
1260 | return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, p1); | |
1261 | } | |
1262 | case EVP_PKEY_CTRL_EC_KDF_TYPE: | |
1263 | if (p1 == -2) { | |
1264 | return EVP_PKEY_CTX_get_ecdh_kdf_type(ctx); | |
1265 | } else { | |
1266 | return EVP_PKEY_CTX_set_ecdh_kdf_type(ctx, p1); | |
1267 | } | |
1268 | case EVP_PKEY_CTRL_GET_EC_KDF_MD: | |
1269 | return EVP_PKEY_CTX_get_ecdh_kdf_md(ctx, p2); | |
1270 | case EVP_PKEY_CTRL_EC_KDF_MD: | |
1271 | return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, p2); | |
1272 | case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN: | |
1273 | return EVP_PKEY_CTX_get_ecdh_kdf_outlen(ctx, p2); | |
1274 | case EVP_PKEY_CTRL_EC_KDF_OUTLEN: | |
1275 | return EVP_PKEY_CTX_set_ecdh_kdf_outlen(ctx, p1); | |
1276 | case EVP_PKEY_CTRL_GET_EC_KDF_UKM: | |
1277 | return EVP_PKEY_CTX_get0_ecdh_kdf_ukm(ctx, p2); | |
1278 | case EVP_PKEY_CTRL_EC_KDF_UKM: | |
1279 | return EVP_PKEY_CTX_set0_ecdh_kdf_ukm(ctx, p2, p1); | |
4fe54d67 NT |
1280 | } |
1281 | } | |
e683582b | 1282 | # endif |
2decdad3 RL |
1283 | if (keytype == EVP_PKEY_RSA) { |
1284 | switch (cmd) { | |
1285 | case EVP_PKEY_CTRL_RSA_OAEP_MD: | |
1286 | return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2); | |
1287 | case EVP_PKEY_CTRL_GET_RSA_OAEP_MD: | |
1288 | return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2); | |
1289 | case EVP_PKEY_CTRL_RSA_MGF1_MD: | |
1290 | return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2); | |
1291 | case EVP_PKEY_CTRL_RSA_OAEP_LABEL: | |
1292 | return EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, p2, p1); | |
1293 | case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL: | |
1294 | return EVP_PKEY_CTX_get0_rsa_oaep_label(ctx, (unsigned char **)p2); | |
1295 | case EVP_PKEY_CTRL_RSA_KEYGEN_BITS: | |
1296 | return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, p1); | |
1297 | case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP: | |
1298 | return EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, p2); | |
1299 | case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES: | |
1300 | return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, p1); | |
1301 | } | |
1302 | } | |
1303 | /* | |
1304 | * keytype == -1 is used when several key types share the same structure, | |
1305 | * or for generic controls that are the same across multiple key types. | |
1306 | */ | |
4fe54d67 | 1307 | if (keytype == -1) { |
ac2d58c7 MC |
1308 | if (optype == EVP_PKEY_OP_DERIVE) { |
1309 | switch (cmd) { | |
05d2f72e | 1310 | /* TLS1-PRF */ |
ac2d58c7 MC |
1311 | case EVP_PKEY_CTRL_TLS_MD: |
1312 | return EVP_PKEY_CTX_set_tls1_prf_md(ctx, p2); | |
1313 | case EVP_PKEY_CTRL_TLS_SECRET: | |
1314 | return EVP_PKEY_CTX_set1_tls1_prf_secret(ctx, p2, p1); | |
1315 | case EVP_PKEY_CTRL_TLS_SEED: | |
1316 | return EVP_PKEY_CTX_add1_tls1_prf_seed(ctx, p2, p1); | |
05d2f72e MC |
1317 | |
1318 | /* HKDF */ | |
1319 | case EVP_PKEY_CTRL_HKDF_MD: | |
1320 | return EVP_PKEY_CTX_set_hkdf_md(ctx, p2); | |
1321 | case EVP_PKEY_CTRL_HKDF_SALT : | |
1322 | return EVP_PKEY_CTX_set1_hkdf_salt(ctx, p2, p1); | |
1323 | case EVP_PKEY_CTRL_HKDF_KEY: | |
1324 | return EVP_PKEY_CTX_set1_hkdf_key(ctx, p2, p1); | |
1325 | case EVP_PKEY_CTRL_HKDF_INFO: | |
1326 | return EVP_PKEY_CTX_add1_hkdf_info(ctx, p2, p1); | |
1327 | case EVP_PKEY_CTRL_HKDF_MODE: | |
1328 | return EVP_PKEY_CTX_hkdf_mode(ctx, p1); | |
194de849 MC |
1329 | |
1330 | /* Scrypt */ | |
1331 | case EVP_PKEY_CTRL_PASS: | |
1332 | return EVP_PKEY_CTX_set1_pbe_pass(ctx, p2, p1); | |
1333 | case EVP_PKEY_CTRL_SCRYPT_SALT: | |
1334 | return EVP_PKEY_CTX_set1_scrypt_salt(ctx, p2, p1); | |
1335 | case EVP_PKEY_CTRL_SCRYPT_N: | |
1336 | return EVP_PKEY_CTX_set_scrypt_N(ctx, p1); | |
1337 | case EVP_PKEY_CTRL_SCRYPT_R: | |
1338 | return EVP_PKEY_CTX_set_scrypt_r(ctx, p1); | |
1339 | case EVP_PKEY_CTRL_SCRYPT_P: | |
1340 | return EVP_PKEY_CTX_set_scrypt_p(ctx, p1); | |
1341 | case EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES: | |
1342 | return EVP_PKEY_CTX_set_scrypt_maxmem_bytes(ctx, p1); | |
ac2d58c7 | 1343 | } |
a540ef90 MC |
1344 | } else if (optype == EVP_PKEY_OP_KEYGEN) { |
1345 | OSSL_PARAM params[2], *p = params; | |
1346 | ||
1347 | switch (cmd) { | |
1348 | case EVP_PKEY_CTRL_CIPHER: | |
1349 | { | |
1350 | char *ciphname = (char *)EVP_CIPHER_name(p2); | |
1351 | ||
1352 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_CIPHER, | |
1353 | ciphname, 0); | |
1354 | *p = OSSL_PARAM_construct_end(); | |
1355 | ||
1356 | return EVP_PKEY_CTX_set_params(ctx, params); | |
1357 | } | |
1358 | case EVP_PKEY_CTRL_SET_MAC_KEY: | |
1359 | { | |
1360 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, | |
1361 | p2, p1); | |
1362 | *p = OSSL_PARAM_construct_end(); | |
1363 | ||
1364 | return EVP_PKEY_CTX_set_params(ctx, params); | |
1365 | } | |
1366 | } | |
ac2d58c7 | 1367 | } |
4fe54d67 | 1368 | switch (cmd) { |
6f4b7663 RL |
1369 | case EVP_PKEY_CTRL_MD: |
1370 | return EVP_PKEY_CTX_set_signature_md(ctx, p2); | |
1371 | case EVP_PKEY_CTRL_GET_MD: | |
1372 | return EVP_PKEY_CTX_get_signature_md(ctx, p2); | |
1373 | case EVP_PKEY_CTRL_RSA_PADDING: | |
1374 | return EVP_PKEY_CTX_set_rsa_padding(ctx, p1); | |
1375 | case EVP_PKEY_CTRL_GET_RSA_PADDING: | |
1376 | return EVP_PKEY_CTX_get_rsa_padding(ctx, p2); | |
6f4b7663 RL |
1377 | case EVP_PKEY_CTRL_GET_RSA_MGF1_MD: |
1378 | return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2); | |
6f4b7663 RL |
1379 | case EVP_PKEY_CTRL_RSA_PSS_SALTLEN: |
1380 | return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, p1); | |
1381 | case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN: | |
1382 | return EVP_PKEY_CTX_get_rsa_pss_saltlen(ctx, p2); | |
1383 | case EVP_PKEY_CTRL_PKCS7_ENCRYPT: | |
1384 | case EVP_PKEY_CTRL_PKCS7_DECRYPT: | |
e683582b | 1385 | # ifndef OPENSSL_NO_CMS |
6f4b7663 RL |
1386 | case EVP_PKEY_CTRL_CMS_DECRYPT: |
1387 | case EVP_PKEY_CTRL_CMS_ENCRYPT: | |
e683582b | 1388 | # endif |
cc572c25 RL |
1389 | /* TODO (3.0) Temporary hack, this should probe */ |
1390 | if (!EVP_PKEY_is_a(EVP_PKEY_CTX_get0_pkey(ctx), "RSASSA-PSS")) | |
6f4b7663 RL |
1391 | return 1; |
1392 | ERR_raise(ERR_LIB_EVP, | |
1393 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
1394 | return -2; | |
4fe54d67 | 1395 | } |
35aca9ec | 1396 | } |
a540ef90 MC |
1397 | |
1398 | /* | |
1399 | * GOST CMS format is different for different cipher algorithms. | |
1400 | * Most of other algorithms don't have such a difference | |
1401 | * so this ctrl is just ignored. | |
1402 | */ | |
1403 | if (cmd == EVP_PKEY_CTRL_CIPHER) | |
1404 | return -2; | |
1405 | ||
35aca9ec MC |
1406 | return 0; |
1407 | } | |
1408 | ||
86df26b3 RL |
1409 | static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX *ctx, int keytype, int optype, |
1410 | int cmd, int p1, void *p2) | |
0f113f3e | 1411 | { |
86df26b3 | 1412 | int ret = 0; |
4803717f | 1413 | |
35aca9ec | 1414 | if (ctx == NULL) { |
86df26b3 | 1415 | EVPerr(0, EVP_R_COMMAND_NOT_SUPPORTED); |
35aca9ec MC |
1416 | return -2; |
1417 | } | |
1418 | ||
86df26b3 RL |
1419 | /* |
1420 | * If the method has a |digest_custom| function, we can relax the | |
1421 | * operation type check, since this can be called before the operation | |
1422 | * is initialized. | |
1423 | */ | |
1424 | if (ctx->pmeth == NULL || ctx->pmeth->digest_custom == NULL) { | |
1425 | if (ctx->operation == EVP_PKEY_OP_UNDEFINED) { | |
1426 | EVPerr(0, EVP_R_NO_OPERATION_SET); | |
1427 | return -1; | |
1428 | } | |
35aca9ec | 1429 | |
86df26b3 RL |
1430 | if ((optype != -1) && !(ctx->operation & optype)) { |
1431 | EVPerr(0, EVP_R_INVALID_OPERATION); | |
1432 | return -1; | |
1433 | } | |
0f113f3e | 1434 | } |
0f113f3e | 1435 | |
86df26b3 RL |
1436 | switch (evp_pkey_ctx_state(ctx)) { |
1437 | case EVP_PKEY_STATE_PROVIDER: | |
1438 | return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2); | |
1439 | case EVP_PKEY_STATE_UNKNOWN: | |
1440 | case EVP_PKEY_STATE_LEGACY: | |
1441 | if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) { | |
1442 | EVPerr(0, EVP_R_COMMAND_NOT_SUPPORTED); | |
1443 | return -2; | |
1444 | } | |
1445 | if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype)) | |
1446 | return -1; | |
4803717f | 1447 | |
86df26b3 | 1448 | ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2); |
0f113f3e | 1449 | |
86df26b3 RL |
1450 | if (ret == -2) |
1451 | EVPerr(0, EVP_R_COMMAND_NOT_SUPPORTED); | |
1452 | break; | |
0f113f3e | 1453 | } |
86df26b3 RL |
1454 | return ret; |
1455 | } | |
0f113f3e | 1456 | |
86df26b3 RL |
1457 | int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, |
1458 | int cmd, int p1, void *p2) | |
1459 | { | |
1460 | int ret = 0; | |
1461 | ||
1462 | /* If unsupported, we don't want that reported here */ | |
1463 | ERR_set_mark(); | |
1464 | ret = evp_pkey_ctx_store_cached_data(ctx, keytype, optype, | |
1465 | cmd, NULL, p2, p1); | |
1466 | if (ret == -2) { | |
1467 | ERR_pop_to_mark(); | |
1468 | } else { | |
1469 | ERR_clear_last_mark(); | |
1470 | /* | |
1471 | * If there was an error, there was an error. | |
1472 | * If the operation isn't initialized yet, we also return, as | |
1473 | * the saved values will be used then anyway. | |
1474 | */ | |
1475 | if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED) | |
1476 | return ret; | |
1477 | } | |
0f113f3e | 1478 | |
86df26b3 | 1479 | return evp_pkey_ctx_ctrl_int(ctx, keytype, optype, cmd, p1, p2); |
0f113f3e | 1480 | } |
0b6f3c66 | 1481 | |
cefa762e | 1482 | int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype, |
64bf1016 | 1483 | int cmd, uint64_t value) |
cefa762e JB |
1484 | { |
1485 | return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value); | |
1486 | } | |
1487 | ||
35aca9ec MC |
1488 | static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name, |
1489 | const char *value) | |
1490 | { | |
ac2d58c7 MC |
1491 | if (strcmp(name, "md") == 0) |
1492 | name = OSSL_ALG_PARAM_DIGEST; | |
1493 | else if (strcmp(name, "rsa_padding_mode") == 0) | |
972fa318 RL |
1494 | name = OSSL_ASYM_CIPHER_PARAM_PAD_MODE; |
1495 | else if (strcmp(name, "rsa_mgf1_md") == 0) | |
1496 | name = OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST; | |
1497 | else if (strcmp(name, "rsa_oaep_md") == 0) | |
1498 | name = OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST; | |
1499 | else if (strcmp(name, "rsa_oaep_label") == 0) | |
1500 | name = OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL; | |
6f4b7663 RL |
1501 | else if (strcmp(name, "rsa_pss_saltlen") == 0) |
1502 | name = OSSL_SIGNATURE_PARAM_PSS_SALTLEN; | |
2972af10 RL |
1503 | else if (strcmp(name, "rsa_keygen_bits") == 0) |
1504 | name = OSSL_PKEY_PARAM_RSA_BITS; | |
1505 | else if (strcmp(name, "rsa_keygen_pubexp") == 0) | |
1506 | name = OSSL_PKEY_PARAM_RSA_E; | |
1507 | else if (strcmp(name, "rsa_keygen_primes") == 0) | |
1508 | name = OSSL_PKEY_PARAM_RSA_PRIMES; | |
e25761b1 RL |
1509 | else if (strcmp(name, "rsa_pss_keygen_md") == 0) |
1510 | name = OSSL_PKEY_PARAM_RSA_DIGEST; | |
1511 | else if (strcmp(name, "rsa_pss_keygen_mgf1_md") == 0) | |
1512 | name = OSSL_PKEY_PARAM_RSA_MGF1_DIGEST; | |
1513 | else if (strcmp(name, "rsa_pss_keygen_saltlen") == 0) | |
1514 | name = OSSL_PKEY_PARAM_RSA_PSS_SALTLEN; | |
b03ec3b5 SL |
1515 | # ifndef OPENSSL_NO_DSA |
1516 | else if (strcmp(name, "dsa_paramgen_bits") == 0) | |
1517 | name = OSSL_PKEY_PARAM_FFC_PBITS; | |
1518 | else if (strcmp(name, "dsa_paramgen_q_bits") == 0) | |
1519 | name = OSSL_PKEY_PARAM_FFC_QBITS; | |
1520 | else if (strcmp(name, "dsa_paramgen_md") == 0) | |
1521 | name = OSSL_PKEY_PARAM_FFC_DIGEST; | |
1522 | # endif | |
e683582b | 1523 | # ifndef OPENSSL_NO_DH |
7165593c | 1524 | else if (strcmp(name, "dh_paramgen_generator") == 0) |
b8086652 | 1525 | name = OSSL_PKEY_PARAM_DH_GENERATOR; |
7165593c SL |
1526 | else if (strcmp(name, "dh_paramgen_prime_len") == 0) |
1527 | name = OSSL_PKEY_PARAM_FFC_PBITS; | |
1528 | else if (strcmp(name, "dh_paramgen_subprime_len") == 0) | |
1529 | name = OSSL_PKEY_PARAM_FFC_QBITS; | |
1530 | else if (strcmp(name, "dh_paramgen_type") == 0) { | |
1531 | name = OSSL_PKEY_PARAM_FFC_TYPE; | |
1532 | value = dh_gen_type_id2name(atoi(value)); | |
1533 | } else if (strcmp(name, "dh_param") == 0) | |
023b188c | 1534 | name = OSSL_PKEY_PARAM_GROUP_NAME; |
7165593c | 1535 | else if (strcmp(name, "dh_rfc5114") == 0) { |
023b188c | 1536 | name = OSSL_PKEY_PARAM_GROUP_NAME; |
7165593c SL |
1537 | value = ffc_named_group_from_uid(atoi(value)); |
1538 | } else if (strcmp(name, "dh_pad") == 0) | |
972fa318 | 1539 | name = OSSL_EXCHANGE_PARAM_PAD; |
e683582b | 1540 | # endif |
4fe54d67 | 1541 | # ifndef OPENSSL_NO_EC |
10d756a7 | 1542 | else if (strcmp(name, "ec_paramgen_curve") == 0) |
11a1b341 | 1543 | name = OSSL_PKEY_PARAM_GROUP_NAME; |
4fe54d67 NT |
1544 | else if (strcmp(name, "ecdh_cofactor_mode") == 0) |
1545 | name = OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE; | |
1546 | else if (strcmp(name, "ecdh_kdf_md") == 0) | |
f552d900 | 1547 | name = OSSL_EXCHANGE_PARAM_KDF_DIGEST; |
c0f39ded SL |
1548 | else if (strcmp(name, "ec_param_enc") == 0) |
1549 | name = OSSL_PKEY_PARAM_EC_ENCODING; | |
4fe54d67 | 1550 | # endif |
194de849 MC |
1551 | else if (strcmp(name, "N") == 0) |
1552 | name = OSSL_KDF_PARAM_SCRYPT_N; | |
89abd1b6 | 1553 | |
972fa318 RL |
1554 | { |
1555 | /* | |
1556 | * TODO(3.0) reduce the code above to only translate known legacy | |
1557 | * string to the corresponding core name (see core_names.h), but | |
1558 | * otherwise leave it to this code block to do the actual work. | |
1559 | */ | |
1560 | const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx); | |
1561 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
1562 | int rv = 0; | |
292c8bdc | 1563 | int exists = 0; |
89abd1b6 | 1564 | |
972fa318 | 1565 | if (!OSSL_PARAM_allocate_from_text(¶ms[0], settable, name, value, |
292c8bdc P |
1566 | strlen(value), &exists)) { |
1567 | if (!exists) { | |
e25761b1 RL |
1568 | ERR_raise_data(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED, |
1569 | "name=%s,value=%s", name, value); | |
292c8bdc P |
1570 | return -2; |
1571 | } | |
89abd1b6 | 1572 | return 0; |
292c8bdc | 1573 | } |
972fa318 RL |
1574 | if (EVP_PKEY_CTX_set_params(ctx, params)) |
1575 | rv = 1; | |
1576 | OPENSSL_free(params[0].data); | |
1577 | return rv; | |
89abd1b6 | 1578 | } |
35aca9ec MC |
1579 | } |
1580 | ||
86df26b3 RL |
1581 | static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX *ctx, |
1582 | const char *name, const char *value) | |
0f113f3e | 1583 | { |
86df26b3 RL |
1584 | int ret = 0; |
1585 | ||
35aca9ec | 1586 | if (ctx == NULL) { |
86df26b3 | 1587 | EVPerr(0, EVP_R_COMMAND_NOT_SUPPORTED); |
35aca9ec MC |
1588 | return -2; |
1589 | } | |
1590 | ||
86df26b3 RL |
1591 | switch (evp_pkey_ctx_state(ctx)) { |
1592 | case EVP_PKEY_STATE_PROVIDER: | |
35aca9ec | 1593 | return legacy_ctrl_str_to_param(ctx, name, value); |
86df26b3 RL |
1594 | case EVP_PKEY_STATE_UNKNOWN: |
1595 | case EVP_PKEY_STATE_LEGACY: | |
1596 | if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->ctrl_str == NULL) { | |
1597 | EVPerr(0, EVP_R_COMMAND_NOT_SUPPORTED); | |
1598 | return -2; | |
1599 | } | |
1600 | if (strcmp(name, "digest") == 0) | |
b9689452 RL |
1601 | ret = EVP_PKEY_CTX_md(ctx, |
1602 | EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT, | |
86df26b3 RL |
1603 | EVP_PKEY_CTRL_MD, value); |
1604 | else | |
1605 | ret = ctx->pmeth->ctrl_str(ctx, name, value); | |
1606 | break; | |
1607 | } | |
35aca9ec | 1608 | |
86df26b3 RL |
1609 | return ret; |
1610 | } | |
1611 | ||
1612 | int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx, | |
1613 | const char *name, const char *value) | |
1614 | { | |
1615 | int ret = 0; | |
1616 | ||
1617 | /* If unsupported, we don't want that reported here */ | |
1618 | ERR_set_mark(); | |
1619 | ret = evp_pkey_ctx_store_cached_data(ctx, -1, -1, -1, | |
1620 | name, value, strlen(value) + 1); | |
1621 | if (ret == -2) { | |
1622 | ERR_pop_to_mark(); | |
1623 | } else { | |
1624 | ERR_clear_last_mark(); | |
1625 | /* | |
1626 | * If there was an error, there was an error. | |
1627 | * If the operation isn't initialized yet, we also return, as | |
1628 | * the saved values will be used then anyway. | |
1629 | */ | |
1630 | if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED) | |
1631 | return ret; | |
1632 | } | |
1633 | ||
1634 | return evp_pkey_ctx_ctrl_str_int(ctx, name, value); | |
1635 | } | |
1636 | ||
1637 | static int decode_cmd(int cmd, const char *name) | |
1638 | { | |
1639 | if (cmd == -1) { | |
1640 | /* | |
1641 | * The consequence of the assertion not being true is that this | |
1642 | * function will return -1, which will cause the calling functions | |
1643 | * to signal that the command is unsupported... in non-debug mode. | |
1644 | */ | |
1645 | if (ossl_assert(name != NULL)) | |
1646 | if (strcmp(name, "distid") == 0 || strcmp(name, "hexdistid") == 0) | |
1647 | cmd = EVP_PKEY_CTRL_SET1_ID; | |
1648 | } | |
1649 | ||
1650 | return cmd; | |
1651 | } | |
1652 | ||
1653 | static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx, | |
1654 | int keytype, int optype, | |
1655 | int cmd, const char *name, | |
1656 | const void *data, size_t data_len) | |
1657 | { | |
1658 | if ((keytype != -1 && ctx->pmeth->pkey_id != keytype) | |
1659 | || ((optype != -1) && !(ctx->operation & optype))) { | |
1660 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); | |
1661 | return -1; | |
0f113f3e | 1662 | } |
86df26b3 RL |
1663 | |
1664 | cmd = decode_cmd(cmd, name); | |
1665 | switch (cmd) { | |
1666 | case EVP_PKEY_CTRL_SET1_ID: | |
1667 | evp_pkey_ctx_free_cached_data(ctx, cmd, name); | |
1668 | if (name != NULL) { | |
1669 | ctx->cached_parameters.dist_id_name = OPENSSL_strdup(name); | |
1670 | if (ctx->cached_parameters.dist_id_name == NULL) { | |
1671 | ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); | |
1672 | return 0; | |
1673 | } | |
1674 | } | |
1675 | if (data_len > 0) { | |
1676 | ctx->cached_parameters.dist_id = OPENSSL_memdup(data, data_len); | |
1677 | if (ctx->cached_parameters.dist_id == NULL) { | |
1678 | ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); | |
1679 | return 0; | |
1680 | } | |
1681 | } | |
1682 | ctx->cached_parameters.dist_id_set = 1; | |
1683 | ctx->cached_parameters.dist_id_len = data_len; | |
1684 | return 1; | |
1685 | } | |
1686 | ||
1687 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
1688 | return -2; | |
1689 | } | |
1690 | ||
1691 | static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx, | |
1692 | int cmd, const char *name) | |
1693 | { | |
1694 | cmd = decode_cmd(cmd, name); | |
1695 | switch (cmd) { | |
1696 | case EVP_PKEY_CTRL_SET1_ID: | |
1697 | OPENSSL_free(ctx->cached_parameters.dist_id); | |
1698 | OPENSSL_free(ctx->cached_parameters.dist_id_name); | |
1699 | ctx->cached_parameters.dist_id = NULL; | |
1700 | ctx->cached_parameters.dist_id_name = NULL; | |
1701 | break; | |
1702 | } | |
1703 | } | |
1704 | ||
1705 | static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx) | |
1706 | { | |
1707 | evp_pkey_ctx_free_cached_data(ctx, EVP_PKEY_CTRL_SET1_ID, NULL); | |
1708 | } | |
1709 | ||
1710 | int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx) | |
1711 | { | |
1712 | int ret = 1; | |
1713 | ||
1714 | if (ret && ctx->cached_parameters.dist_id_set) { | |
1715 | const char *name = ctx->cached_parameters.dist_id_name; | |
1716 | const void *val = ctx->cached_parameters.dist_id; | |
1717 | size_t len = ctx->cached_parameters.dist_id_len; | |
1718 | ||
1719 | if (name != NULL) | |
1720 | ret = evp_pkey_ctx_ctrl_str_int(ctx, name, val); | |
1721 | else | |
1722 | ret = evp_pkey_ctx_ctrl_int(ctx, -1, ctx->operation, | |
1723 | EVP_PKEY_CTRL_SET1_ID, | |
1724 | (int)len, (void *)val); | |
1725 | } | |
1726 | ||
1727 | return ret; | |
0f113f3e | 1728 | } |
f5cda4cb | 1729 | |
99119000 DSH |
1730 | /* Utility functions to send a string of hex string to a ctrl */ |
1731 | ||
1732 | int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str) | |
1733 | { | |
1734 | size_t len; | |
1735 | ||
1736 | len = strlen(str); | |
1737 | if (len > INT_MAX) | |
1738 | return -1; | |
1739 | return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str); | |
1740 | } | |
1741 | ||
1742 | int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex) | |
1743 | { | |
1744 | unsigned char *bin; | |
1745 | long binlen; | |
1746 | int rv = -1; | |
1747 | ||
14f051a0 | 1748 | bin = OPENSSL_hexstr2buf(hex, &binlen); |
99119000 DSH |
1749 | if (bin == NULL) |
1750 | return 0; | |
1751 | if (binlen <= INT_MAX) | |
1752 | rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin); | |
1753 | OPENSSL_free(bin); | |
1754 | return rv; | |
1755 | } | |
52ad523c | 1756 | |
410877ba DSH |
1757 | /* Pass a message digest to a ctrl */ |
1758 | int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md) | |
1759 | { | |
1760 | const EVP_MD *m; | |
c82bafc5 | 1761 | |
410877ba DSH |
1762 | if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) { |
1763 | EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST); | |
1764 | return 0; | |
1765 | } | |
1766 | return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m); | |
1767 | } | |
99119000 | 1768 | |
b28dea4e | 1769 | int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
1770 | { |
1771 | return ctx->operation; | |
1772 | } | |
b28dea4e DSH |
1773 | |
1774 | void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen) | |
0f113f3e MC |
1775 | { |
1776 | ctx->keygen_info = dat; | |
1777 | ctx->keygen_info_count = datlen; | |
1778 | } | |
b28dea4e | 1779 | |
f5cda4cb | 1780 | void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data) |
0f113f3e MC |
1781 | { |
1782 | ctx->data = data; | |
1783 | } | |
f5cda4cb | 1784 | |
9fdcc21f | 1785 | void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx) |
0f113f3e MC |
1786 | { |
1787 | return ctx->data; | |
1788 | } | |
f5cda4cb | 1789 | |
81cebb8b | 1790 | EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
1791 | { |
1792 | return ctx->pkey; | |
1793 | } | |
81cebb8b | 1794 | |
0e1dba93 | 1795 | EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
1796 | { |
1797 | return ctx->peerkey; | |
1798 | } | |
1799 | ||
f5cda4cb | 1800 | void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data) |
0f113f3e MC |
1801 | { |
1802 | ctx->app_data = data; | |
1803 | } | |
f5cda4cb DSH |
1804 | |
1805 | void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx) | |
0f113f3e MC |
1806 | { |
1807 | return ctx->app_data; | |
1808 | } | |
ba30bad5 DSH |
1809 | |
1810 | void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1811 | int (*init) (EVP_PKEY_CTX *ctx)) |
1812 | { | |
1813 | pmeth->init = init; | |
1814 | } | |
8bdcef40 DSH |
1815 | |
1816 | void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth, | |
0f113f3e | 1817 | int (*copy) (EVP_PKEY_CTX *dst, |
9fdcc21f | 1818 | const EVP_PKEY_CTX *src)) |
0f113f3e MC |
1819 | { |
1820 | pmeth->copy = copy; | |
1821 | } | |
ba30bad5 DSH |
1822 | |
1823 | void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1824 | void (*cleanup) (EVP_PKEY_CTX *ctx)) |
1825 | { | |
1826 | pmeth->cleanup = cleanup; | |
1827 | } | |
ba30bad5 DSH |
1828 | |
1829 | void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1830 | int (*paramgen_init) (EVP_PKEY_CTX *ctx), |
1831 | int (*paramgen) (EVP_PKEY_CTX *ctx, | |
1832 | EVP_PKEY *pkey)) | |
1833 | { | |
1834 | pmeth->paramgen_init = paramgen_init; | |
1835 | pmeth->paramgen = paramgen; | |
1836 | } | |
ba30bad5 DSH |
1837 | |
1838 | void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1839 | int (*keygen_init) (EVP_PKEY_CTX *ctx), |
1840 | int (*keygen) (EVP_PKEY_CTX *ctx, | |
1841 | EVP_PKEY *pkey)) | |
1842 | { | |
1843 | pmeth->keygen_init = keygen_init; | |
1844 | pmeth->keygen = keygen; | |
1845 | } | |
ba30bad5 DSH |
1846 | |
1847 | void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1848 | int (*sign_init) (EVP_PKEY_CTX *ctx), |
1849 | int (*sign) (EVP_PKEY_CTX *ctx, | |
1850 | unsigned char *sig, size_t *siglen, | |
1851 | const unsigned char *tbs, | |
1852 | size_t tbslen)) | |
1853 | { | |
1854 | pmeth->sign_init = sign_init; | |
1855 | pmeth->sign = sign; | |
1856 | } | |
ba30bad5 DSH |
1857 | |
1858 | void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1859 | int (*verify_init) (EVP_PKEY_CTX *ctx), |
1860 | int (*verify) (EVP_PKEY_CTX *ctx, | |
1861 | const unsigned char *sig, | |
1862 | size_t siglen, | |
1863 | const unsigned char *tbs, | |
1864 | size_t tbslen)) | |
1865 | { | |
1866 | pmeth->verify_init = verify_init; | |
1867 | pmeth->verify = verify; | |
1868 | } | |
ba30bad5 DSH |
1869 | |
1870 | void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1871 | int (*verify_recover_init) (EVP_PKEY_CTX |
1872 | *ctx), | |
1873 | int (*verify_recover) (EVP_PKEY_CTX | |
1874 | *ctx, | |
1875 | unsigned char | |
1876 | *sig, | |
1877 | size_t *siglen, | |
1878 | const unsigned | |
1879 | char *tbs, | |
1880 | size_t tbslen)) | |
1881 | { | |
1882 | pmeth->verify_recover_init = verify_recover_init; | |
1883 | pmeth->verify_recover = verify_recover; | |
1884 | } | |
ba30bad5 DSH |
1885 | |
1886 | void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1887 | int (*signctx_init) (EVP_PKEY_CTX *ctx, |
1888 | EVP_MD_CTX *mctx), | |
1889 | int (*signctx) (EVP_PKEY_CTX *ctx, | |
1890 | unsigned char *sig, | |
1891 | size_t *siglen, | |
1892 | EVP_MD_CTX *mctx)) | |
1893 | { | |
1894 | pmeth->signctx_init = signctx_init; | |
1895 | pmeth->signctx = signctx; | |
1896 | } | |
ba30bad5 DSH |
1897 | |
1898 | void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1899 | int (*verifyctx_init) (EVP_PKEY_CTX *ctx, |
1900 | EVP_MD_CTX *mctx), | |
1901 | int (*verifyctx) (EVP_PKEY_CTX *ctx, | |
1902 | const unsigned char *sig, | |
1903 | int siglen, | |
1904 | EVP_MD_CTX *mctx)) | |
1905 | { | |
1906 | pmeth->verifyctx_init = verifyctx_init; | |
1907 | pmeth->verifyctx = verifyctx; | |
1908 | } | |
ba30bad5 DSH |
1909 | |
1910 | void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1911 | int (*encrypt_init) (EVP_PKEY_CTX *ctx), |
1912 | int (*encryptfn) (EVP_PKEY_CTX *ctx, | |
1913 | unsigned char *out, | |
1914 | size_t *outlen, | |
1915 | const unsigned char *in, | |
1916 | size_t inlen)) | |
1917 | { | |
1918 | pmeth->encrypt_init = encrypt_init; | |
1919 | pmeth->encrypt = encryptfn; | |
1920 | } | |
ba30bad5 DSH |
1921 | |
1922 | void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1923 | int (*decrypt_init) (EVP_PKEY_CTX *ctx), |
1924 | int (*decrypt) (EVP_PKEY_CTX *ctx, | |
1925 | unsigned char *out, | |
1926 | size_t *outlen, | |
1927 | const unsigned char *in, | |
1928 | size_t inlen)) | |
1929 | { | |
1930 | pmeth->decrypt_init = decrypt_init; | |
1931 | pmeth->decrypt = decrypt; | |
1932 | } | |
ba30bad5 DSH |
1933 | |
1934 | void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1935 | int (*derive_init) (EVP_PKEY_CTX *ctx), |
1936 | int (*derive) (EVP_PKEY_CTX *ctx, | |
1937 | unsigned char *key, | |
1938 | size_t *keylen)) | |
1939 | { | |
1940 | pmeth->derive_init = derive_init; | |
1941 | pmeth->derive = derive; | |
1942 | } | |
ba30bad5 DSH |
1943 | |
1944 | void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1945 | int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1, |
1946 | void *p2), | |
1947 | int (*ctrl_str) (EVP_PKEY_CTX *ctx, | |
1948 | const char *type, | |
1949 | const char *value)) | |
1950 | { | |
1951 | pmeth->ctrl = ctrl; | |
1952 | pmeth->ctrl_str = ctrl_str; | |
1953 | } | |
e7451ed1 | 1954 | |
2555285f AH |
1955 | void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth, |
1956 | int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen, | |
1957 | const unsigned char *tbs, size_t tbslen)) | |
1958 | { | |
1959 | pmeth->digestsign = digestsign; | |
1960 | } | |
1961 | ||
1962 | void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth, | |
1963 | int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig, | |
1964 | size_t siglen, const unsigned char *tbs, | |
1965 | size_t tbslen)) | |
1966 | { | |
1967 | pmeth->digestverify = digestverify; | |
1968 | } | |
1969 | ||
2aee35d3 PY |
1970 | void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth, |
1971 | int (*check) (EVP_PKEY *pkey)) | |
1972 | { | |
1973 | pmeth->check = check; | |
1974 | } | |
1975 | ||
b0004708 PY |
1976 | void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth, |
1977 | int (*check) (EVP_PKEY *pkey)) | |
1978 | { | |
1979 | pmeth->public_check = check; | |
1980 | } | |
1981 | ||
1982 | void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth, | |
1983 | int (*check) (EVP_PKEY *pkey)) | |
1984 | { | |
1985 | pmeth->param_check = check; | |
1986 | } | |
1987 | ||
0a8fdef7 PY |
1988 | void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth, |
1989 | int (*digest_custom) (EVP_PKEY_CTX *ctx, | |
1990 | EVP_MD_CTX *mctx)) | |
1991 | { | |
1992 | pmeth->digest_custom = digest_custom; | |
1993 | } | |
1994 | ||
693be9a2 | 1995 | void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1996 | int (**pinit) (EVP_PKEY_CTX *ctx)) |
1997 | { | |
1998 | *pinit = pmeth->init; | |
1999 | } | |
2000 | ||
693be9a2 | 2001 | void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 | 2002 | int (**pcopy) (EVP_PKEY_CTX *dst, |
9fdcc21f | 2003 | const EVP_PKEY_CTX *src)) |
e7451ed1 DSH |
2004 | { |
2005 | *pcopy = pmeth->copy; | |
2006 | } | |
2007 | ||
693be9a2 | 2008 | void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
2009 | void (**pcleanup) (EVP_PKEY_CTX *ctx)) |
2010 | { | |
2011 | *pcleanup = pmeth->cleanup; | |
2012 | } | |
2013 | ||
693be9a2 | 2014 | void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
2015 | int (**pparamgen_init) (EVP_PKEY_CTX *ctx), |
2016 | int (**pparamgen) (EVP_PKEY_CTX *ctx, | |
2017 | EVP_PKEY *pkey)) | |
2018 | { | |
2019 | if (pparamgen_init) | |
2020 | *pparamgen_init = pmeth->paramgen_init; | |
2021 | if (pparamgen) | |
2022 | *pparamgen = pmeth->paramgen; | |
2023 | } | |
2024 | ||
693be9a2 | 2025 | void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
2026 | int (**pkeygen_init) (EVP_PKEY_CTX *ctx), |
2027 | int (**pkeygen) (EVP_PKEY_CTX *ctx, | |
2028 | EVP_PKEY *pkey)) | |
2029 | { | |
2030 | if (pkeygen_init) | |
2031 | *pkeygen_init = pmeth->keygen_init; | |
2032 | if (pkeygen) | |
2033 | *pkeygen = pmeth->keygen; | |
2034 | } | |
2035 | ||
693be9a2 | 2036 | void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
2037 | int (**psign_init) (EVP_PKEY_CTX *ctx), |
2038 | int (**psign) (EVP_PKEY_CTX *ctx, | |
2039 | unsigned char *sig, size_t *siglen, | |
2040 | const unsigned char *tbs, | |
2041 | size_t tbslen)) | |
2042 | { | |
2043 | if (psign_init) | |
2044 | *psign_init = pmeth->sign_init; | |
2045 | if (psign) | |
2046 | *psign = pmeth->sign; | |
2047 | } | |
2048 | ||
693be9a2 | 2049 | void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
2050 | int (**pverify_init) (EVP_PKEY_CTX *ctx), |
2051 | int (**pverify) (EVP_PKEY_CTX *ctx, | |
2052 | const unsigned char *sig, | |
2053 | size_t siglen, | |
2054 | const unsigned char *tbs, | |
2055 | size_t tbslen)) | |
2056 | { | |
2057 | if (pverify_init) | |
2058 | *pverify_init = pmeth->verify_init; | |
2059 | if (pverify) | |
2060 | *pverify = pmeth->verify; | |
2061 | } | |
2062 | ||
693be9a2 | 2063 | void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
2064 | int (**pverify_recover_init) (EVP_PKEY_CTX |
2065 | *ctx), | |
2066 | int (**pverify_recover) (EVP_PKEY_CTX | |
2067 | *ctx, | |
2068 | unsigned char | |
2069 | *sig, | |
2070 | size_t *siglen, | |
2071 | const unsigned | |
2072 | char *tbs, | |
2073 | size_t tbslen)) | |
2074 | { | |
2075 | if (pverify_recover_init) | |
2076 | *pverify_recover_init = pmeth->verify_recover_init; | |
2077 | if (pverify_recover) | |
2078 | *pverify_recover = pmeth->verify_recover; | |
2079 | } | |
2080 | ||
693be9a2 | 2081 | void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
2082 | int (**psignctx_init) (EVP_PKEY_CTX *ctx, |
2083 | EVP_MD_CTX *mctx), | |
2084 | int (**psignctx) (EVP_PKEY_CTX *ctx, | |
2085 | unsigned char *sig, | |
2086 | size_t *siglen, | |
2087 | EVP_MD_CTX *mctx)) | |
2088 | { | |
2089 | if (psignctx_init) | |
2090 | *psignctx_init = pmeth->signctx_init; | |
2091 | if (psignctx) | |
2092 | *psignctx = pmeth->signctx; | |
2093 | } | |
2094 | ||
693be9a2 | 2095 | void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
2096 | int (**pverifyctx_init) (EVP_PKEY_CTX *ctx, |
2097 | EVP_MD_CTX *mctx), | |
2098 | int (**pverifyctx) (EVP_PKEY_CTX *ctx, | |
2099 | const unsigned char *sig, | |
2100 | int siglen, | |
2101 | EVP_MD_CTX *mctx)) | |
2102 | { | |
2103 | if (pverifyctx_init) | |
2104 | *pverifyctx_init = pmeth->verifyctx_init; | |
2105 | if (pverifyctx) | |
2106 | *pverifyctx = pmeth->verifyctx; | |
2107 | } | |
2108 | ||
693be9a2 | 2109 | void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
2110 | int (**pencrypt_init) (EVP_PKEY_CTX *ctx), |
2111 | int (**pencryptfn) (EVP_PKEY_CTX *ctx, | |
2112 | unsigned char *out, | |
2113 | size_t *outlen, | |
2114 | const unsigned char *in, | |
2115 | size_t inlen)) | |
2116 | { | |
2117 | if (pencrypt_init) | |
2118 | *pencrypt_init = pmeth->encrypt_init; | |
2119 | if (pencryptfn) | |
2120 | *pencryptfn = pmeth->encrypt; | |
2121 | } | |
2122 | ||
693be9a2 | 2123 | void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
2124 | int (**pdecrypt_init) (EVP_PKEY_CTX *ctx), |
2125 | int (**pdecrypt) (EVP_PKEY_CTX *ctx, | |
2126 | unsigned char *out, | |
2127 | size_t *outlen, | |
2128 | const unsigned char *in, | |
2129 | size_t inlen)) | |
2130 | { | |
2131 | if (pdecrypt_init) | |
2132 | *pdecrypt_init = pmeth->decrypt_init; | |
2133 | if (pdecrypt) | |
2134 | *pdecrypt = pmeth->decrypt; | |
2135 | } | |
2136 | ||
693be9a2 | 2137 | void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
2138 | int (**pderive_init) (EVP_PKEY_CTX *ctx), |
2139 | int (**pderive) (EVP_PKEY_CTX *ctx, | |
2140 | unsigned char *key, | |
2141 | size_t *keylen)) | |
2142 | { | |
2143 | if (pderive_init) | |
2144 | *pderive_init = pmeth->derive_init; | |
2145 | if (pderive) | |
2146 | *pderive = pmeth->derive; | |
2147 | } | |
2148 | ||
693be9a2 | 2149 | void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
2150 | int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1, |
2151 | void *p2), | |
2152 | int (**pctrl_str) (EVP_PKEY_CTX *ctx, | |
2153 | const char *type, | |
2154 | const char *value)) | |
2155 | { | |
2156 | if (pctrl) | |
2157 | *pctrl = pmeth->ctrl; | |
2158 | if (pctrl_str) | |
2159 | *pctrl_str = pmeth->ctrl_str; | |
2160 | } | |
2aee35d3 | 2161 | |
2555285f AH |
2162 | void EVP_PKEY_meth_get_digestsign(EVP_PKEY_METHOD *pmeth, |
2163 | int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen, | |
2164 | const unsigned char *tbs, size_t tbslen)) | |
2165 | { | |
2166 | if (digestsign) | |
2167 | *digestsign = pmeth->digestsign; | |
2168 | } | |
2169 | ||
2170 | void EVP_PKEY_meth_get_digestverify(EVP_PKEY_METHOD *pmeth, | |
2171 | int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig, | |
2172 | size_t siglen, const unsigned char *tbs, | |
2173 | size_t tbslen)) | |
2174 | { | |
2175 | if (digestverify) | |
2176 | *digestverify = pmeth->digestverify; | |
2177 | } | |
2178 | ||
693be9a2 | 2179 | void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth, |
2aee35d3 PY |
2180 | int (**pcheck) (EVP_PKEY *pkey)) |
2181 | { | |
34f5c8b1 | 2182 | if (pcheck != NULL) |
2aee35d3 PY |
2183 | *pcheck = pmeth->check; |
2184 | } | |
b0004708 | 2185 | |
693be9a2 | 2186 | void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth, |
b0004708 PY |
2187 | int (**pcheck) (EVP_PKEY *pkey)) |
2188 | { | |
34f5c8b1 | 2189 | if (pcheck != NULL) |
b0004708 PY |
2190 | *pcheck = pmeth->public_check; |
2191 | } | |
2192 | ||
693be9a2 | 2193 | void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth, |
b0004708 PY |
2194 | int (**pcheck) (EVP_PKEY *pkey)) |
2195 | { | |
34f5c8b1 | 2196 | if (pcheck != NULL) |
b0004708 PY |
2197 | *pcheck = pmeth->param_check; |
2198 | } | |
0a8fdef7 PY |
2199 | |
2200 | void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth, | |
2201 | int (**pdigest_custom) (EVP_PKEY_CTX *ctx, | |
2202 | EVP_MD_CTX *mctx)) | |
2203 | { | |
675f4cee | 2204 | if (pdigest_custom != NULL) |
0a8fdef7 PY |
2205 | *pdigest_custom = pmeth->digest_custom; |
2206 | } | |
e683582b | 2207 | |
f844f9eb | 2208 | #endif /* FIPS_MODULE */ |