]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
b0edda11 | 2 | * Copyright 2006-2018 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 | ||
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
b39fc560 | 12 | #include "internal/cryptlib.h" |
3c27208f | 13 | #include <openssl/engine.h> |
33bed28b | 14 | #include <openssl/evp.h> |
99119000 | 15 | #include <openssl/x509v3.h> |
5fe736e5 | 16 | #include "internal/asn1_int.h" |
27af42f9 | 17 | #include "internal/evp_int.h" |
99119000 | 18 | #include "internal/numbers.h" |
ff64702b | 19 | #include "evp_locl.h" |
0b6f3c66 | 20 | |
0f113f3e | 21 | typedef int sk_cmp_fn_type(const char *const *a, const char *const *b); |
5ce278a7 | 22 | |
df2ee0e2 | 23 | static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL; |
0b6f3c66 | 24 | |
cefa762e | 25 | /* This array needs to be in order of NIDs */ |
0f113f3e | 26 | static const EVP_PKEY_METHOD *standard_methods[] = { |
d4f0339c | 27 | #ifndef OPENSSL_NO_RSA |
0f113f3e | 28 | &rsa_pkey_meth, |
d4f0339c DSH |
29 | #endif |
30 | #ifndef OPENSSL_NO_DH | |
0f113f3e | 31 | &dh_pkey_meth, |
d4f0339c DSH |
32 | #endif |
33 | #ifndef OPENSSL_NO_DSA | |
0f113f3e | 34 | &dsa_pkey_meth, |
d4f0339c | 35 | #endif |
ef236ec3 | 36 | #ifndef OPENSSL_NO_EC |
0f113f3e | 37 | &ec_pkey_meth, |
ef236ec3 | 38 | #endif |
0f113f3e | 39 | &hmac_pkey_meth, |
b4a3aeeb | 40 | #ifndef OPENSSL_NO_CMAC |
0f113f3e | 41 | &cmac_pkey_meth, |
b4a3aeeb | 42 | #endif |
6577e008 DSH |
43 | #ifndef OPENSSL_NO_RSA |
44 | &rsa_pss_pkey_meth, | |
45 | #endif | |
afb14cda | 46 | #ifndef OPENSSL_NO_DH |
1eff3485 | 47 | &dhx_pkey_meth, |
cefa762e JB |
48 | #endif |
49 | #ifndef OPENSSL_NO_SCRYPT | |
50 | &scrypt_pkey_meth, | |
afb14cda | 51 | #endif |
aacfb134 | 52 | &tls1_prf_pkey_meth, |
262bd85f DSH |
53 | #ifndef OPENSSL_NO_EC |
54 | &ecx25519_pkey_meth, | |
13735cfe | 55 | &ecx448_pkey_meth, |
262bd85f | 56 | #endif |
52ad5b60 TS |
57 | &hkdf_pkey_meth, |
58 | #ifndef OPENSSL_NO_POLY1305 | |
59 | &poly1305_pkey_meth, | |
60 | #endif | |
3f5616d7 TS |
61 | #ifndef OPENSSL_NO_SIPHASH |
62 | &siphash_pkey_meth, | |
63 | #endif | |
42a3008a DSH |
64 | #ifndef OPENSSL_NO_EC |
65 | &ed25519_pkey_meth, | |
13735cfe | 66 | &ed448_pkey_meth, |
42a3008a | 67 | #endif |
ddb634fe JL |
68 | #ifndef OPENSSL_NO_SM2 |
69 | &sm2_pkey_meth, | |
70 | #endif | |
0f113f3e | 71 | }; |
0b6f3c66 | 72 | |
606f6c47 | 73 | DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *, |
0f113f3e | 74 | pmeth); |
babb3798 | 75 | |
0f113f3e MC |
76 | static int pmeth_cmp(const EVP_PKEY_METHOD *const *a, |
77 | const EVP_PKEY_METHOD *const *b) | |
78 | { | |
79 | return ((*a)->pkey_id - (*b)->pkey_id); | |
80 | } | |
0b6f3c66 | 81 | |
606f6c47 | 82 | IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *, |
0f113f3e | 83 | pmeth); |
babb3798 | 84 | |
c9777d26 | 85 | const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type) |
0f113f3e MC |
86 | { |
87 | EVP_PKEY_METHOD tmp; | |
88 | const EVP_PKEY_METHOD *t = &tmp, **ret; | |
89 | tmp.pkey_id = type; | |
90 | if (app_pkey_methods) { | |
91 | int idx; | |
92 | idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp); | |
93 | if (idx >= 0) | |
94 | return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx); | |
95 | } | |
96 | ret = OBJ_bsearch_pmeth(&t, standard_methods, | |
97 | sizeof(standard_methods) / | |
98 | sizeof(EVP_PKEY_METHOD *)); | |
99 | if (!ret || !*ret) | |
100 | return NULL; | |
101 | return *ret; | |
102 | } | |
0b6f3c66 | 103 | |
f5cda4cb | 104 | static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id) |
0f113f3e MC |
105 | { |
106 | EVP_PKEY_CTX *ret; | |
107 | const EVP_PKEY_METHOD *pmeth; | |
2f2e6b62 | 108 | |
0f113f3e | 109 | if (id == -1) { |
a6465b3f P |
110 | if (pkey == NULL) |
111 | return 0; | |
2f2e6b62 | 112 | id = pkey->type; |
0f113f3e | 113 | } |
a63bf2c5 | 114 | #ifndef OPENSSL_NO_ENGINE |
c2976edf | 115 | if (e == NULL && pkey != NULL) |
d19b01ad | 116 | e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine; |
0f113f3e MC |
117 | /* Try to find an ENGINE which implements this method */ |
118 | if (e) { | |
119 | if (!ENGINE_init(e)) { | |
120 | EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB); | |
121 | return NULL; | |
122 | } | |
c2976edf | 123 | } else { |
0f113f3e | 124 | e = ENGINE_get_pkey_meth_engine(id); |
c2976edf | 125 | } |
0f113f3e MC |
126 | |
127 | /* | |
0d4fb843 | 128 | * If an ENGINE handled this method look it up. Otherwise use internal |
0f113f3e MC |
129 | * tables. |
130 | */ | |
0f113f3e MC |
131 | if (e) |
132 | pmeth = ENGINE_get_pkey_meth(e, id); | |
133 | else | |
a63bf2c5 | 134 | #endif |
0f113f3e | 135 | pmeth = EVP_PKEY_meth_find(id); |
c9777d26 | 136 | |
0f113f3e | 137 | if (pmeth == NULL) { |
918a27fa DSH |
138 | #ifndef OPENSSL_NO_ENGINE |
139 | ENGINE_finish(e); | |
140 | #endif | |
0f113f3e MC |
141 | EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM); |
142 | return NULL; | |
143 | } | |
c9777d26 | 144 | |
64b25758 | 145 | ret = OPENSSL_zalloc(sizeof(*ret)); |
90945fa3 | 146 | if (ret == NULL) { |
a63bf2c5 | 147 | #ifndef OPENSSL_NO_ENGINE |
7c96dbcd | 148 | ENGINE_finish(e); |
a63bf2c5 | 149 | #endif |
0f113f3e MC |
150 | EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE); |
151 | return NULL; | |
152 | } | |
153 | ret->engine = e; | |
154 | ret->pmeth = pmeth; | |
155 | ret->operation = EVP_PKEY_OP_UNDEFINED; | |
156 | ret->pkey = pkey; | |
a6465b3f | 157 | if (pkey != NULL) |
03273d61 | 158 | EVP_PKEY_up_ref(pkey); |
0f113f3e MC |
159 | |
160 | if (pmeth->init) { | |
161 | if (pmeth->init(ret) <= 0) { | |
83b4049a | 162 | ret->pmeth = NULL; |
0f113f3e MC |
163 | EVP_PKEY_CTX_free(ret); |
164 | return NULL; | |
165 | } | |
166 | } | |
167 | ||
168 | return ret; | |
169 | } | |
170 | ||
171 | EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags) | |
172 | { | |
173 | EVP_PKEY_METHOD *pmeth; | |
b4faea50 | 174 | |
b51bce94 | 175 | pmeth = OPENSSL_zalloc(sizeof(*pmeth)); |
3484236d F |
176 | if (pmeth == NULL) { |
177 | EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 178 | return NULL; |
3484236d | 179 | } |
0f113f3e | 180 | |
0f113f3e MC |
181 | pmeth->pkey_id = id; |
182 | pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC; | |
0f113f3e MC |
183 | return pmeth; |
184 | } | |
ba30bad5 | 185 | |
f830c68f | 186 | void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags, |
0f113f3e MC |
187 | const EVP_PKEY_METHOD *meth) |
188 | { | |
189 | if (ppkey_id) | |
190 | *ppkey_id = meth->pkey_id; | |
191 | if (pflags) | |
192 | *pflags = meth->flags; | |
193 | } | |
f830c68f DSH |
194 | |
195 | void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src) | |
0f113f3e | 196 | { |
f830c68f | 197 | |
0f113f3e MC |
198 | dst->init = src->init; |
199 | dst->copy = src->copy; | |
200 | dst->cleanup = src->cleanup; | |
f830c68f | 201 | |
0f113f3e MC |
202 | dst->paramgen_init = src->paramgen_init; |
203 | dst->paramgen = src->paramgen; | |
f830c68f | 204 | |
0f113f3e MC |
205 | dst->keygen_init = src->keygen_init; |
206 | dst->keygen = src->keygen; | |
f830c68f | 207 | |
0f113f3e MC |
208 | dst->sign_init = src->sign_init; |
209 | dst->sign = src->sign; | |
f830c68f | 210 | |
0f113f3e MC |
211 | dst->verify_init = src->verify_init; |
212 | dst->verify = src->verify; | |
f830c68f | 213 | |
0f113f3e MC |
214 | dst->verify_recover_init = src->verify_recover_init; |
215 | dst->verify_recover = src->verify_recover; | |
f830c68f | 216 | |
0f113f3e MC |
217 | dst->signctx_init = src->signctx_init; |
218 | dst->signctx = src->signctx; | |
f830c68f | 219 | |
0f113f3e MC |
220 | dst->verifyctx_init = src->verifyctx_init; |
221 | dst->verifyctx = src->verifyctx; | |
f830c68f | 222 | |
0f113f3e MC |
223 | dst->encrypt_init = src->encrypt_init; |
224 | dst->encrypt = src->encrypt; | |
f830c68f | 225 | |
0f113f3e MC |
226 | dst->decrypt_init = src->decrypt_init; |
227 | dst->decrypt = src->decrypt; | |
f830c68f | 228 | |
0f113f3e MC |
229 | dst->derive_init = src->derive_init; |
230 | dst->derive = src->derive; | |
f830c68f | 231 | |
0f113f3e MC |
232 | dst->ctrl = src->ctrl; |
233 | dst->ctrl_str = src->ctrl_str; | |
2aee35d3 PY |
234 | |
235 | dst->check = src->check; | |
0f113f3e | 236 | } |
f830c68f | 237 | |
ba30bad5 | 238 | void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth) |
0f113f3e MC |
239 | { |
240 | if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC)) | |
241 | OPENSSL_free(pmeth); | |
242 | } | |
ba30bad5 | 243 | |
f5cda4cb | 244 | EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e) |
0f113f3e MC |
245 | { |
246 | return int_ctx_new(pkey, e, -1); | |
247 | } | |
f5cda4cb DSH |
248 | |
249 | EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e) | |
0f113f3e MC |
250 | { |
251 | return int_ctx_new(NULL, e, id); | |
252 | } | |
f5cda4cb | 253 | |
9fdcc21f | 254 | EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx) |
0f113f3e MC |
255 | { |
256 | EVP_PKEY_CTX *rctx; | |
ff64702b MC |
257 | |
258 | if (((pctx->pmeth == NULL) || (pctx->pmeth->copy == NULL)) | |
259 | && pctx->exchprovctx == NULL) | |
0f113f3e | 260 | return NULL; |
c9777d26 | 261 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
262 | /* Make sure it's safe to copy a pkey context using an ENGINE */ |
263 | if (pctx->engine && !ENGINE_init(pctx->engine)) { | |
264 | EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB); | |
265 | return 0; | |
266 | } | |
c9777d26 | 267 | #endif |
ff64702b | 268 | rctx = OPENSSL_zalloc(sizeof(*rctx)); |
3484236d F |
269 | if (rctx == NULL) { |
270 | EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 271 | return NULL; |
3484236d | 272 | } |
8bdcef40 | 273 | |
ff64702b MC |
274 | if (pctx->pkey != NULL) |
275 | EVP_PKEY_up_ref(pctx->pkey); | |
276 | rctx->pkey = pctx->pkey; | |
277 | rctx->operation = pctx->operation; | |
278 | ||
279 | if (pctx->exchprovctx != NULL) { | |
280 | if (!ossl_assert(pctx->exchange != NULL)) | |
281 | return NULL; | |
282 | rctx->exchange = pctx->exchange; | |
283 | if (!EVP_KEYEXCH_up_ref(rctx->exchange)) { | |
284 | OPENSSL_free(rctx); | |
285 | return NULL; | |
286 | } | |
287 | rctx->exchprovctx = pctx->exchange->dupctx(pctx->exchprovctx); | |
288 | if (rctx->exchprovctx == NULL) { | |
289 | EVP_KEYEXCH_free(rctx->exchange); | |
290 | OPENSSL_free(rctx); | |
291 | return NULL; | |
292 | } | |
293 | return rctx; | |
294 | } | |
295 | ||
0f113f3e | 296 | rctx->pmeth = pctx->pmeth; |
c9777d26 | 297 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e | 298 | rctx->engine = pctx->engine; |
c9777d26 | 299 | #endif |
8bdcef40 | 300 | |
0f113f3e | 301 | if (pctx->peerkey) |
03273d61 | 302 | EVP_PKEY_up_ref(pctx->peerkey); |
0f113f3e | 303 | rctx->peerkey = pctx->peerkey; |
8bdcef40 | 304 | |
0f113f3e MC |
305 | if (pctx->pmeth->copy(rctx, pctx) > 0) |
306 | return rctx; | |
8bdcef40 | 307 | |
83b4049a | 308 | rctx->pmeth = NULL; |
0f113f3e MC |
309 | EVP_PKEY_CTX_free(rctx); |
310 | return NULL; | |
8bdcef40 | 311 | |
0f113f3e | 312 | } |
8bdcef40 | 313 | |
ba30bad5 | 314 | int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth) |
0f113f3e MC |
315 | { |
316 | if (app_pkey_methods == NULL) { | |
317 | app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp); | |
3484236d F |
318 | if (app_pkey_methods == NULL){ |
319 | EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 320 | return 0; |
3484236d | 321 | } |
0f113f3e | 322 | } |
3484236d F |
323 | if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) { |
324 | EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 325 | return 0; |
3484236d | 326 | } |
0f113f3e MC |
327 | sk_EVP_PKEY_METHOD_sort(app_pkey_methods); |
328 | return 1; | |
329 | } | |
ba30bad5 | 330 | |
0822e89a PY |
331 | void evp_app_cleanup_int(void) |
332 | { | |
333 | if (app_pkey_methods != NULL) | |
334 | sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free); | |
335 | } | |
336 | ||
337 | int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth) | |
338 | { | |
339 | const EVP_PKEY_METHOD *ret; | |
340 | ||
341 | ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth); | |
342 | ||
343 | return ret == NULL ? 0 : 1; | |
344 | } | |
345 | ||
48ed9c23 DSH |
346 | size_t EVP_PKEY_meth_get_count(void) |
347 | { | |
348 | size_t rv = OSSL_NELEM(standard_methods); | |
349 | ||
350 | if (app_pkey_methods) | |
351 | rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods); | |
352 | return rv; | |
353 | } | |
354 | ||
355 | const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx) | |
356 | { | |
357 | if (idx < OSSL_NELEM(standard_methods)) | |
358 | return standard_methods[idx]; | |
359 | if (app_pkey_methods == NULL) | |
360 | return NULL; | |
361 | idx -= OSSL_NELEM(standard_methods); | |
362 | if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods)) | |
363 | return NULL; | |
364 | return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx); | |
365 | } | |
366 | ||
5da98aa6 | 367 | void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
368 | { |
369 | if (ctx == NULL) | |
370 | return; | |
371 | if (ctx->pmeth && ctx->pmeth->cleanup) | |
372 | ctx->pmeth->cleanup(ctx); | |
ff64702b MC |
373 | |
374 | if (ctx->exchprovctx != NULL && ctx->exchange != NULL) | |
375 | ctx->exchange->freectx(ctx->exchprovctx); | |
376 | ||
377 | EVP_KEYEXCH_free(ctx->exchange); | |
378 | ||
c5ba2d99 RS |
379 | EVP_PKEY_free(ctx->pkey); |
380 | EVP_PKEY_free(ctx->peerkey); | |
c9777d26 | 381 | #ifndef OPENSSL_NO_ENGINE |
7c96dbcd | 382 | ENGINE_finish(ctx->engine); |
c9777d26 | 383 | #endif |
0f113f3e MC |
384 | OPENSSL_free(ctx); |
385 | } | |
5da98aa6 | 386 | |
0b6f3c66 | 387 | int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, |
0f113f3e MC |
388 | int cmd, int p1, void *p2) |
389 | { | |
390 | int ret; | |
4803717f | 391 | |
0f113f3e MC |
392 | if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) { |
393 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED); | |
394 | return -2; | |
395 | } | |
396 | if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype)) | |
397 | return -1; | |
398 | ||
4803717f PY |
399 | /* Skip the operation checks since this is called in a very early stage */ |
400 | if (ctx->pmeth->digest_custom != NULL) | |
401 | goto doit; | |
402 | ||
0f113f3e MC |
403 | if (ctx->operation == EVP_PKEY_OP_UNDEFINED) { |
404 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET); | |
405 | return -1; | |
406 | } | |
407 | ||
408 | if ((optype != -1) && !(ctx->operation & optype)) { | |
409 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION); | |
410 | return -1; | |
411 | } | |
412 | ||
4803717f | 413 | doit: |
0f113f3e MC |
414 | ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2); |
415 | ||
416 | if (ret == -2) | |
417 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED); | |
418 | ||
419 | return ret; | |
0f113f3e | 420 | } |
0b6f3c66 | 421 | |
cefa762e | 422 | int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype, |
64bf1016 | 423 | int cmd, uint64_t value) |
cefa762e JB |
424 | { |
425 | return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value); | |
426 | } | |
427 | ||
4a3dc3c0 | 428 | int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx, |
0f113f3e MC |
429 | const char *name, const char *value) |
430 | { | |
431 | if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) { | |
432 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED); | |
433 | return -2; | |
434 | } | |
410877ba DSH |
435 | if (strcmp(name, "digest") == 0) |
436 | return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD, | |
437 | value); | |
0f113f3e MC |
438 | return ctx->pmeth->ctrl_str(ctx, name, value); |
439 | } | |
f5cda4cb | 440 | |
99119000 DSH |
441 | /* Utility functions to send a string of hex string to a ctrl */ |
442 | ||
443 | int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str) | |
444 | { | |
445 | size_t len; | |
446 | ||
447 | len = strlen(str); | |
448 | if (len > INT_MAX) | |
449 | return -1; | |
450 | return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str); | |
451 | } | |
452 | ||
453 | int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex) | |
454 | { | |
455 | unsigned char *bin; | |
456 | long binlen; | |
457 | int rv = -1; | |
458 | ||
14f051a0 | 459 | bin = OPENSSL_hexstr2buf(hex, &binlen); |
99119000 DSH |
460 | if (bin == NULL) |
461 | return 0; | |
462 | if (binlen <= INT_MAX) | |
463 | rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin); | |
464 | OPENSSL_free(bin); | |
465 | return rv; | |
466 | } | |
52ad523c | 467 | |
410877ba DSH |
468 | /* Pass a message digest to a ctrl */ |
469 | int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md) | |
470 | { | |
471 | const EVP_MD *m; | |
c82bafc5 | 472 | |
410877ba DSH |
473 | if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) { |
474 | EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST); | |
475 | return 0; | |
476 | } | |
477 | return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m); | |
478 | } | |
99119000 | 479 | |
b28dea4e | 480 | int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
481 | { |
482 | return ctx->operation; | |
483 | } | |
b28dea4e DSH |
484 | |
485 | void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen) | |
0f113f3e MC |
486 | { |
487 | ctx->keygen_info = dat; | |
488 | ctx->keygen_info_count = datlen; | |
489 | } | |
b28dea4e | 490 | |
f5cda4cb | 491 | void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data) |
0f113f3e MC |
492 | { |
493 | ctx->data = data; | |
494 | } | |
f5cda4cb | 495 | |
9fdcc21f | 496 | void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx) |
0f113f3e MC |
497 | { |
498 | return ctx->data; | |
499 | } | |
f5cda4cb | 500 | |
81cebb8b | 501 | EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
502 | { |
503 | return ctx->pkey; | |
504 | } | |
81cebb8b | 505 | |
0e1dba93 | 506 | EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
507 | { |
508 | return ctx->peerkey; | |
509 | } | |
510 | ||
f5cda4cb | 511 | void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data) |
0f113f3e MC |
512 | { |
513 | ctx->app_data = data; | |
514 | } | |
f5cda4cb DSH |
515 | |
516 | void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx) | |
0f113f3e MC |
517 | { |
518 | return ctx->app_data; | |
519 | } | |
ba30bad5 DSH |
520 | |
521 | void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
522 | int (*init) (EVP_PKEY_CTX *ctx)) |
523 | { | |
524 | pmeth->init = init; | |
525 | } | |
8bdcef40 DSH |
526 | |
527 | void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth, | |
0f113f3e | 528 | int (*copy) (EVP_PKEY_CTX *dst, |
9fdcc21f | 529 | const EVP_PKEY_CTX *src)) |
0f113f3e MC |
530 | { |
531 | pmeth->copy = copy; | |
532 | } | |
ba30bad5 DSH |
533 | |
534 | void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
535 | void (*cleanup) (EVP_PKEY_CTX *ctx)) |
536 | { | |
537 | pmeth->cleanup = cleanup; | |
538 | } | |
ba30bad5 DSH |
539 | |
540 | void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
541 | int (*paramgen_init) (EVP_PKEY_CTX *ctx), |
542 | int (*paramgen) (EVP_PKEY_CTX *ctx, | |
543 | EVP_PKEY *pkey)) | |
544 | { | |
545 | pmeth->paramgen_init = paramgen_init; | |
546 | pmeth->paramgen = paramgen; | |
547 | } | |
ba30bad5 DSH |
548 | |
549 | void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
550 | int (*keygen_init) (EVP_PKEY_CTX *ctx), |
551 | int (*keygen) (EVP_PKEY_CTX *ctx, | |
552 | EVP_PKEY *pkey)) | |
553 | { | |
554 | pmeth->keygen_init = keygen_init; | |
555 | pmeth->keygen = keygen; | |
556 | } | |
ba30bad5 DSH |
557 | |
558 | void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
559 | int (*sign_init) (EVP_PKEY_CTX *ctx), |
560 | int (*sign) (EVP_PKEY_CTX *ctx, | |
561 | unsigned char *sig, size_t *siglen, | |
562 | const unsigned char *tbs, | |
563 | size_t tbslen)) | |
564 | { | |
565 | pmeth->sign_init = sign_init; | |
566 | pmeth->sign = sign; | |
567 | } | |
ba30bad5 DSH |
568 | |
569 | void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
570 | int (*verify_init) (EVP_PKEY_CTX *ctx), |
571 | int (*verify) (EVP_PKEY_CTX *ctx, | |
572 | const unsigned char *sig, | |
573 | size_t siglen, | |
574 | const unsigned char *tbs, | |
575 | size_t tbslen)) | |
576 | { | |
577 | pmeth->verify_init = verify_init; | |
578 | pmeth->verify = verify; | |
579 | } | |
ba30bad5 DSH |
580 | |
581 | void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
582 | int (*verify_recover_init) (EVP_PKEY_CTX |
583 | *ctx), | |
584 | int (*verify_recover) (EVP_PKEY_CTX | |
585 | *ctx, | |
586 | unsigned char | |
587 | *sig, | |
588 | size_t *siglen, | |
589 | const unsigned | |
590 | char *tbs, | |
591 | size_t tbslen)) | |
592 | { | |
593 | pmeth->verify_recover_init = verify_recover_init; | |
594 | pmeth->verify_recover = verify_recover; | |
595 | } | |
ba30bad5 DSH |
596 | |
597 | void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
598 | int (*signctx_init) (EVP_PKEY_CTX *ctx, |
599 | EVP_MD_CTX *mctx), | |
600 | int (*signctx) (EVP_PKEY_CTX *ctx, | |
601 | unsigned char *sig, | |
602 | size_t *siglen, | |
603 | EVP_MD_CTX *mctx)) | |
604 | { | |
605 | pmeth->signctx_init = signctx_init; | |
606 | pmeth->signctx = signctx; | |
607 | } | |
ba30bad5 DSH |
608 | |
609 | void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
610 | int (*verifyctx_init) (EVP_PKEY_CTX *ctx, |
611 | EVP_MD_CTX *mctx), | |
612 | int (*verifyctx) (EVP_PKEY_CTX *ctx, | |
613 | const unsigned char *sig, | |
614 | int siglen, | |
615 | EVP_MD_CTX *mctx)) | |
616 | { | |
617 | pmeth->verifyctx_init = verifyctx_init; | |
618 | pmeth->verifyctx = verifyctx; | |
619 | } | |
ba30bad5 DSH |
620 | |
621 | void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
622 | int (*encrypt_init) (EVP_PKEY_CTX *ctx), |
623 | int (*encryptfn) (EVP_PKEY_CTX *ctx, | |
624 | unsigned char *out, | |
625 | size_t *outlen, | |
626 | const unsigned char *in, | |
627 | size_t inlen)) | |
628 | { | |
629 | pmeth->encrypt_init = encrypt_init; | |
630 | pmeth->encrypt = encryptfn; | |
631 | } | |
ba30bad5 DSH |
632 | |
633 | void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
634 | int (*decrypt_init) (EVP_PKEY_CTX *ctx), |
635 | int (*decrypt) (EVP_PKEY_CTX *ctx, | |
636 | unsigned char *out, | |
637 | size_t *outlen, | |
638 | const unsigned char *in, | |
639 | size_t inlen)) | |
640 | { | |
641 | pmeth->decrypt_init = decrypt_init; | |
642 | pmeth->decrypt = decrypt; | |
643 | } | |
ba30bad5 DSH |
644 | |
645 | void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
646 | int (*derive_init) (EVP_PKEY_CTX *ctx), |
647 | int (*derive) (EVP_PKEY_CTX *ctx, | |
648 | unsigned char *key, | |
649 | size_t *keylen)) | |
650 | { | |
651 | pmeth->derive_init = derive_init; | |
652 | pmeth->derive = derive; | |
653 | } | |
ba30bad5 DSH |
654 | |
655 | void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
656 | int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1, |
657 | void *p2), | |
658 | int (*ctrl_str) (EVP_PKEY_CTX *ctx, | |
659 | const char *type, | |
660 | const char *value)) | |
661 | { | |
662 | pmeth->ctrl = ctrl; | |
663 | pmeth->ctrl_str = ctrl_str; | |
664 | } | |
e7451ed1 | 665 | |
2aee35d3 PY |
666 | void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth, |
667 | int (*check) (EVP_PKEY *pkey)) | |
668 | { | |
669 | pmeth->check = check; | |
670 | } | |
671 | ||
b0004708 PY |
672 | void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth, |
673 | int (*check) (EVP_PKEY *pkey)) | |
674 | { | |
675 | pmeth->public_check = check; | |
676 | } | |
677 | ||
678 | void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth, | |
679 | int (*check) (EVP_PKEY *pkey)) | |
680 | { | |
681 | pmeth->param_check = check; | |
682 | } | |
683 | ||
0a8fdef7 PY |
684 | void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth, |
685 | int (*digest_custom) (EVP_PKEY_CTX *ctx, | |
686 | EVP_MD_CTX *mctx)) | |
687 | { | |
688 | pmeth->digest_custom = digest_custom; | |
689 | } | |
690 | ||
693be9a2 | 691 | void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
692 | int (**pinit) (EVP_PKEY_CTX *ctx)) |
693 | { | |
694 | *pinit = pmeth->init; | |
695 | } | |
696 | ||
693be9a2 | 697 | void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 | 698 | int (**pcopy) (EVP_PKEY_CTX *dst, |
9fdcc21f | 699 | const EVP_PKEY_CTX *src)) |
e7451ed1 DSH |
700 | { |
701 | *pcopy = pmeth->copy; | |
702 | } | |
703 | ||
693be9a2 | 704 | void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
705 | void (**pcleanup) (EVP_PKEY_CTX *ctx)) |
706 | { | |
707 | *pcleanup = pmeth->cleanup; | |
708 | } | |
709 | ||
693be9a2 | 710 | void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
711 | int (**pparamgen_init) (EVP_PKEY_CTX *ctx), |
712 | int (**pparamgen) (EVP_PKEY_CTX *ctx, | |
713 | EVP_PKEY *pkey)) | |
714 | { | |
715 | if (pparamgen_init) | |
716 | *pparamgen_init = pmeth->paramgen_init; | |
717 | if (pparamgen) | |
718 | *pparamgen = pmeth->paramgen; | |
719 | } | |
720 | ||
693be9a2 | 721 | void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
722 | int (**pkeygen_init) (EVP_PKEY_CTX *ctx), |
723 | int (**pkeygen) (EVP_PKEY_CTX *ctx, | |
724 | EVP_PKEY *pkey)) | |
725 | { | |
726 | if (pkeygen_init) | |
727 | *pkeygen_init = pmeth->keygen_init; | |
728 | if (pkeygen) | |
729 | *pkeygen = pmeth->keygen; | |
730 | } | |
731 | ||
693be9a2 | 732 | void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
733 | int (**psign_init) (EVP_PKEY_CTX *ctx), |
734 | int (**psign) (EVP_PKEY_CTX *ctx, | |
735 | unsigned char *sig, size_t *siglen, | |
736 | const unsigned char *tbs, | |
737 | size_t tbslen)) | |
738 | { | |
739 | if (psign_init) | |
740 | *psign_init = pmeth->sign_init; | |
741 | if (psign) | |
742 | *psign = pmeth->sign; | |
743 | } | |
744 | ||
693be9a2 | 745 | void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
746 | int (**pverify_init) (EVP_PKEY_CTX *ctx), |
747 | int (**pverify) (EVP_PKEY_CTX *ctx, | |
748 | const unsigned char *sig, | |
749 | size_t siglen, | |
750 | const unsigned char *tbs, | |
751 | size_t tbslen)) | |
752 | { | |
753 | if (pverify_init) | |
754 | *pverify_init = pmeth->verify_init; | |
755 | if (pverify) | |
756 | *pverify = pmeth->verify; | |
757 | } | |
758 | ||
693be9a2 | 759 | void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
760 | int (**pverify_recover_init) (EVP_PKEY_CTX |
761 | *ctx), | |
762 | int (**pverify_recover) (EVP_PKEY_CTX | |
763 | *ctx, | |
764 | unsigned char | |
765 | *sig, | |
766 | size_t *siglen, | |
767 | const unsigned | |
768 | char *tbs, | |
769 | size_t tbslen)) | |
770 | { | |
771 | if (pverify_recover_init) | |
772 | *pverify_recover_init = pmeth->verify_recover_init; | |
773 | if (pverify_recover) | |
774 | *pverify_recover = pmeth->verify_recover; | |
775 | } | |
776 | ||
693be9a2 | 777 | void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
778 | int (**psignctx_init) (EVP_PKEY_CTX *ctx, |
779 | EVP_MD_CTX *mctx), | |
780 | int (**psignctx) (EVP_PKEY_CTX *ctx, | |
781 | unsigned char *sig, | |
782 | size_t *siglen, | |
783 | EVP_MD_CTX *mctx)) | |
784 | { | |
785 | if (psignctx_init) | |
786 | *psignctx_init = pmeth->signctx_init; | |
787 | if (psignctx) | |
788 | *psignctx = pmeth->signctx; | |
789 | } | |
790 | ||
693be9a2 | 791 | void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
792 | int (**pverifyctx_init) (EVP_PKEY_CTX *ctx, |
793 | EVP_MD_CTX *mctx), | |
794 | int (**pverifyctx) (EVP_PKEY_CTX *ctx, | |
795 | const unsigned char *sig, | |
796 | int siglen, | |
797 | EVP_MD_CTX *mctx)) | |
798 | { | |
799 | if (pverifyctx_init) | |
800 | *pverifyctx_init = pmeth->verifyctx_init; | |
801 | if (pverifyctx) | |
802 | *pverifyctx = pmeth->verifyctx; | |
803 | } | |
804 | ||
693be9a2 | 805 | void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
806 | int (**pencrypt_init) (EVP_PKEY_CTX *ctx), |
807 | int (**pencryptfn) (EVP_PKEY_CTX *ctx, | |
808 | unsigned char *out, | |
809 | size_t *outlen, | |
810 | const unsigned char *in, | |
811 | size_t inlen)) | |
812 | { | |
813 | if (pencrypt_init) | |
814 | *pencrypt_init = pmeth->encrypt_init; | |
815 | if (pencryptfn) | |
816 | *pencryptfn = pmeth->encrypt; | |
817 | } | |
818 | ||
693be9a2 | 819 | void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
820 | int (**pdecrypt_init) (EVP_PKEY_CTX *ctx), |
821 | int (**pdecrypt) (EVP_PKEY_CTX *ctx, | |
822 | unsigned char *out, | |
823 | size_t *outlen, | |
824 | const unsigned char *in, | |
825 | size_t inlen)) | |
826 | { | |
827 | if (pdecrypt_init) | |
828 | *pdecrypt_init = pmeth->decrypt_init; | |
829 | if (pdecrypt) | |
830 | *pdecrypt = pmeth->decrypt; | |
831 | } | |
832 | ||
693be9a2 | 833 | void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
834 | int (**pderive_init) (EVP_PKEY_CTX *ctx), |
835 | int (**pderive) (EVP_PKEY_CTX *ctx, | |
836 | unsigned char *key, | |
837 | size_t *keylen)) | |
838 | { | |
839 | if (pderive_init) | |
840 | *pderive_init = pmeth->derive_init; | |
841 | if (pderive) | |
842 | *pderive = pmeth->derive; | |
843 | } | |
844 | ||
693be9a2 | 845 | void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
846 | int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1, |
847 | void *p2), | |
848 | int (**pctrl_str) (EVP_PKEY_CTX *ctx, | |
849 | const char *type, | |
850 | const char *value)) | |
851 | { | |
852 | if (pctrl) | |
853 | *pctrl = pmeth->ctrl; | |
854 | if (pctrl_str) | |
855 | *pctrl_str = pmeth->ctrl_str; | |
856 | } | |
2aee35d3 | 857 | |
693be9a2 | 858 | void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth, |
2aee35d3 PY |
859 | int (**pcheck) (EVP_PKEY *pkey)) |
860 | { | |
34f5c8b1 | 861 | if (pcheck != NULL) |
2aee35d3 PY |
862 | *pcheck = pmeth->check; |
863 | } | |
b0004708 | 864 | |
693be9a2 | 865 | void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth, |
b0004708 PY |
866 | int (**pcheck) (EVP_PKEY *pkey)) |
867 | { | |
34f5c8b1 | 868 | if (pcheck != NULL) |
b0004708 PY |
869 | *pcheck = pmeth->public_check; |
870 | } | |
871 | ||
693be9a2 | 872 | void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth, |
b0004708 PY |
873 | int (**pcheck) (EVP_PKEY *pkey)) |
874 | { | |
34f5c8b1 | 875 | if (pcheck != NULL) |
b0004708 PY |
876 | *pcheck = pmeth->param_check; |
877 | } | |
0a8fdef7 PY |
878 | |
879 | void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth, | |
880 | int (**pdigest_custom) (EVP_PKEY_CTX *ctx, | |
881 | EVP_MD_CTX *mctx)) | |
882 | { | |
675f4cee | 883 | if (pdigest_custom != NULL) |
0a8fdef7 PY |
884 | *pdigest_custom = pmeth->digest_custom; |
885 | } |