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