]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/pmeth_lib.c
Reorganize local header files
[thirdparty/openssl.git] / crypto / evp / pmeth_lib.c
1
2 /*
3 * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
4 *
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
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
9 */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <openssl/engine.h>
14 #include <openssl/evp.h>
15 #include <openssl/x509v3.h>
16 #include <openssl/core_names.h>
17 #include <openssl/dh.h>
18 #include "internal/cryptlib.h"
19 #include "crypto/asn1.h"
20 #include "crypto/evp.h"
21 #include "internal/numbers.h"
22 #include "internal/provider.h"
23 #include "evp_local.h"
24
25 typedef const EVP_PKEY_METHOD *(*pmeth_fn)(void);
26 typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
27
28 static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
29
30 /* This array needs to be in order of NIDs */
31 static pmeth_fn standard_methods[] = {
32 #ifndef OPENSSL_NO_RSA
33 rsa_pkey_method,
34 #endif
35 #ifndef OPENSSL_NO_DH
36 dh_pkey_method,
37 #endif
38 #ifndef OPENSSL_NO_DSA
39 dsa_pkey_method,
40 #endif
41 #ifndef OPENSSL_NO_EC
42 ec_pkey_method,
43 #endif
44 hmac_pkey_method,
45 #ifndef OPENSSL_NO_CMAC
46 cmac_pkey_method,
47 #endif
48 #ifndef OPENSSL_NO_RSA
49 rsa_pss_pkey_method,
50 #endif
51 #ifndef OPENSSL_NO_DH
52 dhx_pkey_method,
53 #endif
54 #ifndef OPENSSL_NO_SCRYPT
55 scrypt_pkey_method,
56 #endif
57 tls1_prf_pkey_method,
58 #ifndef OPENSSL_NO_EC
59 ecx25519_pkey_method,
60 ecx448_pkey_method,
61 #endif
62 hkdf_pkey_method,
63 #ifndef OPENSSL_NO_POLY1305
64 poly1305_pkey_method,
65 #endif
66 #ifndef OPENSSL_NO_SIPHASH
67 siphash_pkey_method,
68 #endif
69 #ifndef OPENSSL_NO_EC
70 ed25519_pkey_method,
71 ed448_pkey_method,
72 #endif
73 #ifndef OPENSSL_NO_SM2
74 sm2_pkey_method,
75 #endif
76 };
77
78 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
79
80 static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b)
81 {
82 return ((*a)->pkey_id - ((**b)())->pkey_id);
83 }
84
85 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
86
87 static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
88 const EVP_PKEY_METHOD *const *b)
89 {
90 return ((*a)->pkey_id - (*b)->pkey_id);
91 }
92
93 const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
94 {
95 pmeth_fn *ret;
96 EVP_PKEY_METHOD tmp;
97 const EVP_PKEY_METHOD *t = &tmp;
98 tmp.pkey_id = type;
99 if (app_pkey_methods) {
100 int idx;
101 idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
102 if (idx >= 0)
103 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
104 }
105 ret = OBJ_bsearch_pmeth_func(&t, standard_methods,
106 sizeof(standard_methods) /
107 sizeof(pmeth_fn));
108 if (!ret || !*ret)
109 return NULL;
110 return (**ret)();
111 }
112
113 static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
114 {
115 EVP_PKEY_CTX *ret;
116 const EVP_PKEY_METHOD *pmeth = NULL;
117
118 /*
119 * When using providers, the context is bound to the algo implementation
120 * later.
121 */
122 if (pkey == NULL && e == NULL && id == -1)
123 goto common;
124
125 /* TODO(3.0) Legacy code should be removed when all is provider based */
126 /* BEGIN legacy */
127 if (id == -1) {
128 if (pkey == NULL)
129 return 0;
130 id = pkey->type;
131 }
132 #ifndef OPENSSL_NO_ENGINE
133 if (e == NULL && pkey != NULL)
134 e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
135 /* Try to find an ENGINE which implements this method */
136 if (e) {
137 if (!ENGINE_init(e)) {
138 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
139 return NULL;
140 }
141 } else {
142 e = ENGINE_get_pkey_meth_engine(id);
143 }
144
145 /*
146 * If an ENGINE handled this method look it up. Otherwise use internal
147 * tables.
148 */
149 if (e)
150 pmeth = ENGINE_get_pkey_meth(e, id);
151 else
152 #endif
153 pmeth = EVP_PKEY_meth_find(id);
154
155 if (pmeth == NULL) {
156 #ifndef OPENSSL_NO_ENGINE
157 ENGINE_finish(e);
158 #endif
159 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
160 return NULL;
161 }
162 /* END legacy */
163
164 common:
165 ret = OPENSSL_zalloc(sizeof(*ret));
166 if (ret == NULL) {
167 #ifndef OPENSSL_NO_ENGINE
168 ENGINE_finish(e);
169 #endif
170 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
171 return NULL;
172 }
173 ret->engine = e;
174 ret->pmeth = pmeth;
175 ret->operation = EVP_PKEY_OP_UNDEFINED;
176 ret->pkey = pkey;
177 if (pkey != NULL)
178 EVP_PKEY_up_ref(pkey);
179
180 if (pmeth != NULL && pmeth->init != NULL) {
181 if (pmeth->init(ret) <= 0) {
182 ret->pmeth = NULL;
183 EVP_PKEY_CTX_free(ret);
184 return NULL;
185 }
186 }
187
188 return ret;
189 }
190
191 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
192 {
193 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
194 if (ctx->op.kex.exchprovctx != NULL && ctx->op.kex.exchange != NULL)
195 ctx->op.kex.exchange->freectx(ctx->op.kex.exchprovctx);
196 EVP_KEYEXCH_free(ctx->op.kex.exchange);
197 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
198 if (ctx->op.sig.sigprovctx != NULL && ctx->op.sig.signature != NULL)
199 ctx->op.sig.signature->freectx(ctx->op.sig.sigprovctx);
200 EVP_SIGNATURE_free(ctx->op.sig.signature);
201 }
202 }
203
204 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
205 {
206 EVP_PKEY_METHOD *pmeth;
207
208 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
209 if (pmeth == NULL) {
210 EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE);
211 return NULL;
212 }
213
214 pmeth->pkey_id = id;
215 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
216 return pmeth;
217 }
218
219 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
220 const EVP_PKEY_METHOD *meth)
221 {
222 if (ppkey_id)
223 *ppkey_id = meth->pkey_id;
224 if (pflags)
225 *pflags = meth->flags;
226 }
227
228 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
229 {
230
231 dst->init = src->init;
232 dst->copy = src->copy;
233 dst->cleanup = src->cleanup;
234
235 dst->paramgen_init = src->paramgen_init;
236 dst->paramgen = src->paramgen;
237
238 dst->keygen_init = src->keygen_init;
239 dst->keygen = src->keygen;
240
241 dst->sign_init = src->sign_init;
242 dst->sign = src->sign;
243
244 dst->verify_init = src->verify_init;
245 dst->verify = src->verify;
246
247 dst->verify_recover_init = src->verify_recover_init;
248 dst->verify_recover = src->verify_recover;
249
250 dst->signctx_init = src->signctx_init;
251 dst->signctx = src->signctx;
252
253 dst->verifyctx_init = src->verifyctx_init;
254 dst->verifyctx = src->verifyctx;
255
256 dst->encrypt_init = src->encrypt_init;
257 dst->encrypt = src->encrypt;
258
259 dst->decrypt_init = src->decrypt_init;
260 dst->decrypt = src->decrypt;
261
262 dst->derive_init = src->derive_init;
263 dst->derive = src->derive;
264
265 dst->ctrl = src->ctrl;
266 dst->ctrl_str = src->ctrl_str;
267
268 dst->check = src->check;
269 }
270
271 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
272 {
273 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
274 OPENSSL_free(pmeth);
275 }
276
277 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
278 {
279 return int_ctx_new(pkey, e, -1);
280 }
281
282 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
283 {
284 return int_ctx_new(NULL, e, id);
285 }
286
287 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
288 {
289 EVP_PKEY_CTX *rctx;
290
291 if (((pctx->pmeth == NULL) || (pctx->pmeth->copy == NULL))
292 && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx)
293 && pctx->op.kex.exchprovctx == NULL)
294 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)
295 && pctx->op.sig.sigprovctx == NULL)))
296 return NULL;
297 #ifndef OPENSSL_NO_ENGINE
298 /* Make sure it's safe to copy a pkey context using an ENGINE */
299 if (pctx->engine && !ENGINE_init(pctx->engine)) {
300 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
301 return 0;
302 }
303 #endif
304 rctx = OPENSSL_zalloc(sizeof(*rctx));
305 if (rctx == NULL) {
306 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE);
307 return NULL;
308 }
309
310 if (pctx->pkey != NULL)
311 EVP_PKEY_up_ref(pctx->pkey);
312 rctx->pkey = pctx->pkey;
313 rctx->operation = pctx->operation;
314
315 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
316 if (pctx->op.kex.exchange != NULL) {
317 rctx->op.kex.exchange = pctx->op.kex.exchange;
318 if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) {
319 OPENSSL_free(rctx);
320 return NULL;
321 }
322 }
323 if (pctx->op.kex.exchprovctx != NULL) {
324 if (!ossl_assert(pctx->op.kex.exchange != NULL))
325 return NULL;
326 rctx->op.kex.exchprovctx
327 = pctx->op.kex.exchange->dupctx(pctx->op.kex.exchprovctx);
328 if (rctx->op.kex.exchprovctx == NULL) {
329 EVP_KEYEXCH_free(rctx->op.kex.exchange);
330 OPENSSL_free(rctx);
331 return NULL;
332 }
333 return rctx;
334 }
335 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
336 if (pctx->op.sig.signature != NULL) {
337 rctx->op.sig.signature = pctx->op.sig.signature;
338 if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) {
339 OPENSSL_free(rctx);
340 return NULL;
341 }
342 }
343 if (pctx->op.sig.sigprovctx != NULL) {
344 if (!ossl_assert(pctx->op.sig.signature != NULL))
345 return NULL;
346 rctx->op.sig.sigprovctx
347 = pctx->op.sig.signature->dupctx(pctx->op.sig.sigprovctx);
348 if (rctx->op.sig.sigprovctx == NULL) {
349 EVP_SIGNATURE_free(rctx->op.sig.signature);
350 OPENSSL_free(rctx);
351 return NULL;
352 }
353 return rctx;
354 }
355 }
356
357 rctx->pmeth = pctx->pmeth;
358 #ifndef OPENSSL_NO_ENGINE
359 rctx->engine = pctx->engine;
360 #endif
361
362 if (pctx->peerkey)
363 EVP_PKEY_up_ref(pctx->peerkey);
364 rctx->peerkey = pctx->peerkey;
365
366 if (pctx->pmeth->copy(rctx, pctx) > 0)
367 return rctx;
368
369 rctx->pmeth = NULL;
370 EVP_PKEY_CTX_free(rctx);
371 return NULL;
372
373 }
374
375 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
376 {
377 if (app_pkey_methods == NULL) {
378 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
379 if (app_pkey_methods == NULL){
380 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
381 return 0;
382 }
383 }
384 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
385 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
386 return 0;
387 }
388 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
389 return 1;
390 }
391
392 void evp_app_cleanup_int(void)
393 {
394 if (app_pkey_methods != NULL)
395 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
396 }
397
398 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
399 {
400 const EVP_PKEY_METHOD *ret;
401
402 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
403
404 return ret == NULL ? 0 : 1;
405 }
406
407 size_t EVP_PKEY_meth_get_count(void)
408 {
409 size_t rv = OSSL_NELEM(standard_methods);
410
411 if (app_pkey_methods)
412 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
413 return rv;
414 }
415
416 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
417 {
418 if (idx < OSSL_NELEM(standard_methods))
419 return (standard_methods[idx])();
420 if (app_pkey_methods == NULL)
421 return NULL;
422 idx -= OSSL_NELEM(standard_methods);
423 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
424 return NULL;
425 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
426 }
427
428 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
429 {
430 if (ctx == NULL)
431 return;
432 if (ctx->pmeth && ctx->pmeth->cleanup)
433 ctx->pmeth->cleanup(ctx);
434
435 evp_pkey_ctx_free_old_ops(ctx);
436
437 EVP_PKEY_free(ctx->pkey);
438 EVP_PKEY_free(ctx->peerkey);
439 #ifndef OPENSSL_NO_ENGINE
440 ENGINE_finish(ctx->engine);
441 #endif
442 OPENSSL_free(ctx);
443 }
444
445 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
446 {
447 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
448 && ctx->op.sig.sigprovctx != NULL
449 && ctx->op.sig.signature != NULL
450 && ctx->op.sig.signature->get_ctx_params != NULL)
451 return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.sigprovctx,
452 params);
453 return 0;
454 }
455
456 const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx)
457 {
458 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
459 && ctx->op.sig.signature != NULL
460 && ctx->op.sig.signature->gettable_ctx_params != NULL)
461 return ctx->op.sig.signature->gettable_ctx_params();
462
463 return NULL;
464 }
465
466 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
467 {
468 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
469 && ctx->op.kex.exchprovctx != NULL
470 && ctx->op.kex.exchange != NULL
471 && ctx->op.kex.exchange->set_ctx_params != NULL)
472 return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx,
473 params);
474 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
475 && ctx->op.sig.sigprovctx != NULL
476 && ctx->op.sig.signature != NULL
477 && ctx->op.sig.signature->set_ctx_params != NULL)
478 return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx,
479 params);
480 return 0;
481 }
482
483 const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx)
484 {
485 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
486 && ctx->op.kex.exchange != NULL
487 && ctx->op.kex.exchange->settable_ctx_params != NULL)
488 return ctx->op.kex.exchange->settable_ctx_params();
489 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
490 && ctx->op.sig.signature != NULL
491 && ctx->op.sig.signature->settable_ctx_params != NULL)
492 return ctx->op.sig.signature->settable_ctx_params();
493
494 return NULL;
495 }
496
497 #ifndef OPENSSL_NO_DH
498 int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad)
499 {
500 OSSL_PARAM dh_pad_params[2];
501 unsigned int upad = pad;
502
503 /* We use EVP_PKEY_CTX_ctrl return values */
504 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
505 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
506 return -2;
507 }
508
509 /* TODO(3.0): Remove this eventually when no more legacy */
510 if (ctx->op.kex.exchprovctx == NULL)
511 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE,
512 EVP_PKEY_CTRL_DH_PAD, pad, NULL);
513
514 dh_pad_params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &upad);
515 dh_pad_params[1] = OSSL_PARAM_construct_end();
516
517 return EVP_PKEY_CTX_set_params(ctx, dh_pad_params);
518 }
519 #endif
520
521 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
522 {
523 OSSL_PARAM sig_md_params[3], *p = sig_md_params;
524 /* 80 should be big enough */
525 char name[80] = "";
526 const EVP_MD *tmp;
527
528 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
529 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
530 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
531 return -2;
532 }
533
534 /* TODO(3.0): Remove this eventually when no more legacy */
535 if (ctx->op.sig.sigprovctx == NULL)
536 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
537 EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
538
539 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
540 name,
541 sizeof(name));
542 *p++ = OSSL_PARAM_construct_end();
543
544 if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
545 return 0;
546
547 tmp = EVP_get_digestbyname(name);
548 if (tmp == NULL)
549 return 0;
550
551 *md = tmp;
552
553 return 1;
554 }
555
556 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
557 {
558 OSSL_PARAM sig_md_params[3], *p = sig_md_params;
559 size_t mdsize;
560 const char *name;
561
562 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
563 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
564 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
565 return -2;
566 }
567
568 /* TODO(3.0): Remove this eventually when no more legacy */
569 if (ctx->op.sig.sigprovctx == NULL)
570 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
571 EVP_PKEY_CTRL_MD, 0, (void *)(md));
572
573 if (md == NULL) {
574 name = "";
575 mdsize = 0;
576 } else {
577 mdsize = EVP_MD_size(md);
578 name = EVP_MD_name(md);
579 }
580
581 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
582 /*
583 * Cast away the const. This is read
584 * only so should be safe
585 */
586 (char *)name,
587 strlen(name) + 1);
588 *p++ = OSSL_PARAM_construct_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE,
589 &mdsize);
590 *p++ = OSSL_PARAM_construct_end();
591
592 return EVP_PKEY_CTX_set_params(ctx, sig_md_params);
593 }
594
595 static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
596 int cmd, int p1, void *p2)
597 {
598 switch (cmd) {
599 #ifndef OPENSSL_NO_DH
600 case EVP_PKEY_CTRL_DH_PAD:
601 return EVP_PKEY_CTX_set_dh_pad(ctx, p1);
602 #endif
603 case EVP_PKEY_CTRL_MD:
604 return EVP_PKEY_CTX_set_signature_md(ctx, p2);
605 case EVP_PKEY_CTRL_GET_MD:
606 return EVP_PKEY_CTX_get_signature_md(ctx, p2);
607 }
608 return 0;
609 }
610
611 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
612 int cmd, int p1, void *p2)
613 {
614 int ret;
615
616 if (ctx == NULL) {
617 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
618 return -2;
619 }
620
621 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
622 || (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
623 && ctx->op.sig.sigprovctx != NULL))
624 return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
625
626 if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
627 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
628 return -2;
629 }
630 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
631 return -1;
632
633 /* Skip the operation checks since this is called in a very early stage */
634 if (ctx->pmeth->digest_custom != NULL)
635 goto doit;
636
637 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
638 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
639 return -1;
640 }
641
642 if ((optype != -1) && !(ctx->operation & optype)) {
643 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
644 return -1;
645 }
646
647 doit:
648 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
649
650 if (ret == -2)
651 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
652
653 return ret;
654 }
655
656 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
657 int cmd, uint64_t value)
658 {
659 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
660 }
661
662 static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
663 const char *value)
664 {
665 #ifndef OPENSSL_NO_DH
666 if (strcmp(name, "dh_pad") == 0) {
667 int pad;
668
669 pad = atoi(value);
670 return EVP_PKEY_CTX_set_dh_pad(ctx, pad);
671 }
672 #endif
673 if (strcmp(name, "digest") == 0) {
674 int ret;
675 EVP_MD *md;
676
677 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) || ctx->op.sig.signature == NULL)
678 return 0;
679 md = EVP_MD_fetch(ossl_provider_library_context(ctx->op.sig.signature->prov),
680 value, NULL);
681 if (md == NULL)
682 return 0;
683 ret = EVP_PKEY_CTX_set_signature_md(ctx, md);
684 EVP_MD_meth_free(md);
685 return ret;
686 }
687
688 return 0;
689 }
690
691 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
692 const char *name, const char *value)
693 {
694 if (ctx == NULL) {
695 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
696 return -2;
697 }
698
699 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
700 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
701 && ctx->op.sig.sigprovctx != NULL))
702 return legacy_ctrl_str_to_param(ctx, name, value);
703
704 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
705 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
706 return -2;
707 }
708 if (strcmp(name, "digest") == 0)
709 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
710 value);
711 return ctx->pmeth->ctrl_str(ctx, name, value);
712 }
713
714 /* Utility functions to send a string of hex string to a ctrl */
715
716 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
717 {
718 size_t len;
719
720 len = strlen(str);
721 if (len > INT_MAX)
722 return -1;
723 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
724 }
725
726 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
727 {
728 unsigned char *bin;
729 long binlen;
730 int rv = -1;
731
732 bin = OPENSSL_hexstr2buf(hex, &binlen);
733 if (bin == NULL)
734 return 0;
735 if (binlen <= INT_MAX)
736 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
737 OPENSSL_free(bin);
738 return rv;
739 }
740
741 /* Pass a message digest to a ctrl */
742 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
743 {
744 const EVP_MD *m;
745
746 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
747 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
748 return 0;
749 }
750 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
751 }
752
753 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
754 {
755 return ctx->operation;
756 }
757
758 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
759 {
760 ctx->keygen_info = dat;
761 ctx->keygen_info_count = datlen;
762 }
763
764 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
765 {
766 ctx->data = data;
767 }
768
769 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
770 {
771 return ctx->data;
772 }
773
774 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
775 {
776 return ctx->pkey;
777 }
778
779 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
780 {
781 return ctx->peerkey;
782 }
783
784 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
785 {
786 ctx->app_data = data;
787 }
788
789 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
790 {
791 return ctx->app_data;
792 }
793
794 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
795 int (*init) (EVP_PKEY_CTX *ctx))
796 {
797 pmeth->init = init;
798 }
799
800 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
801 int (*copy) (EVP_PKEY_CTX *dst,
802 const EVP_PKEY_CTX *src))
803 {
804 pmeth->copy = copy;
805 }
806
807 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
808 void (*cleanup) (EVP_PKEY_CTX *ctx))
809 {
810 pmeth->cleanup = cleanup;
811 }
812
813 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
814 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
815 int (*paramgen) (EVP_PKEY_CTX *ctx,
816 EVP_PKEY *pkey))
817 {
818 pmeth->paramgen_init = paramgen_init;
819 pmeth->paramgen = paramgen;
820 }
821
822 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
823 int (*keygen_init) (EVP_PKEY_CTX *ctx),
824 int (*keygen) (EVP_PKEY_CTX *ctx,
825 EVP_PKEY *pkey))
826 {
827 pmeth->keygen_init = keygen_init;
828 pmeth->keygen = keygen;
829 }
830
831 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
832 int (*sign_init) (EVP_PKEY_CTX *ctx),
833 int (*sign) (EVP_PKEY_CTX *ctx,
834 unsigned char *sig, size_t *siglen,
835 const unsigned char *tbs,
836 size_t tbslen))
837 {
838 pmeth->sign_init = sign_init;
839 pmeth->sign = sign;
840 }
841
842 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
843 int (*verify_init) (EVP_PKEY_CTX *ctx),
844 int (*verify) (EVP_PKEY_CTX *ctx,
845 const unsigned char *sig,
846 size_t siglen,
847 const unsigned char *tbs,
848 size_t tbslen))
849 {
850 pmeth->verify_init = verify_init;
851 pmeth->verify = verify;
852 }
853
854 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
855 int (*verify_recover_init) (EVP_PKEY_CTX
856 *ctx),
857 int (*verify_recover) (EVP_PKEY_CTX
858 *ctx,
859 unsigned char
860 *sig,
861 size_t *siglen,
862 const unsigned
863 char *tbs,
864 size_t tbslen))
865 {
866 pmeth->verify_recover_init = verify_recover_init;
867 pmeth->verify_recover = verify_recover;
868 }
869
870 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
871 int (*signctx_init) (EVP_PKEY_CTX *ctx,
872 EVP_MD_CTX *mctx),
873 int (*signctx) (EVP_PKEY_CTX *ctx,
874 unsigned char *sig,
875 size_t *siglen,
876 EVP_MD_CTX *mctx))
877 {
878 pmeth->signctx_init = signctx_init;
879 pmeth->signctx = signctx;
880 }
881
882 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
883 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
884 EVP_MD_CTX *mctx),
885 int (*verifyctx) (EVP_PKEY_CTX *ctx,
886 const unsigned char *sig,
887 int siglen,
888 EVP_MD_CTX *mctx))
889 {
890 pmeth->verifyctx_init = verifyctx_init;
891 pmeth->verifyctx = verifyctx;
892 }
893
894 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
895 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
896 int (*encryptfn) (EVP_PKEY_CTX *ctx,
897 unsigned char *out,
898 size_t *outlen,
899 const unsigned char *in,
900 size_t inlen))
901 {
902 pmeth->encrypt_init = encrypt_init;
903 pmeth->encrypt = encryptfn;
904 }
905
906 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
907 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
908 int (*decrypt) (EVP_PKEY_CTX *ctx,
909 unsigned char *out,
910 size_t *outlen,
911 const unsigned char *in,
912 size_t inlen))
913 {
914 pmeth->decrypt_init = decrypt_init;
915 pmeth->decrypt = decrypt;
916 }
917
918 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
919 int (*derive_init) (EVP_PKEY_CTX *ctx),
920 int (*derive) (EVP_PKEY_CTX *ctx,
921 unsigned char *key,
922 size_t *keylen))
923 {
924 pmeth->derive_init = derive_init;
925 pmeth->derive = derive;
926 }
927
928 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
929 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
930 void *p2),
931 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
932 const char *type,
933 const char *value))
934 {
935 pmeth->ctrl = ctrl;
936 pmeth->ctrl_str = ctrl_str;
937 }
938
939 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
940 int (*check) (EVP_PKEY *pkey))
941 {
942 pmeth->check = check;
943 }
944
945 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
946 int (*check) (EVP_PKEY *pkey))
947 {
948 pmeth->public_check = check;
949 }
950
951 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
952 int (*check) (EVP_PKEY *pkey))
953 {
954 pmeth->param_check = check;
955 }
956
957 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
958 int (*digest_custom) (EVP_PKEY_CTX *ctx,
959 EVP_MD_CTX *mctx))
960 {
961 pmeth->digest_custom = digest_custom;
962 }
963
964 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
965 int (**pinit) (EVP_PKEY_CTX *ctx))
966 {
967 *pinit = pmeth->init;
968 }
969
970 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
971 int (**pcopy) (EVP_PKEY_CTX *dst,
972 const EVP_PKEY_CTX *src))
973 {
974 *pcopy = pmeth->copy;
975 }
976
977 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
978 void (**pcleanup) (EVP_PKEY_CTX *ctx))
979 {
980 *pcleanup = pmeth->cleanup;
981 }
982
983 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
984 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
985 int (**pparamgen) (EVP_PKEY_CTX *ctx,
986 EVP_PKEY *pkey))
987 {
988 if (pparamgen_init)
989 *pparamgen_init = pmeth->paramgen_init;
990 if (pparamgen)
991 *pparamgen = pmeth->paramgen;
992 }
993
994 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
995 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
996 int (**pkeygen) (EVP_PKEY_CTX *ctx,
997 EVP_PKEY *pkey))
998 {
999 if (pkeygen_init)
1000 *pkeygen_init = pmeth->keygen_init;
1001 if (pkeygen)
1002 *pkeygen = pmeth->keygen;
1003 }
1004
1005 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
1006 int (**psign_init) (EVP_PKEY_CTX *ctx),
1007 int (**psign) (EVP_PKEY_CTX *ctx,
1008 unsigned char *sig, size_t *siglen,
1009 const unsigned char *tbs,
1010 size_t tbslen))
1011 {
1012 if (psign_init)
1013 *psign_init = pmeth->sign_init;
1014 if (psign)
1015 *psign = pmeth->sign;
1016 }
1017
1018 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
1019 int (**pverify_init) (EVP_PKEY_CTX *ctx),
1020 int (**pverify) (EVP_PKEY_CTX *ctx,
1021 const unsigned char *sig,
1022 size_t siglen,
1023 const unsigned char *tbs,
1024 size_t tbslen))
1025 {
1026 if (pverify_init)
1027 *pverify_init = pmeth->verify_init;
1028 if (pverify)
1029 *pverify = pmeth->verify;
1030 }
1031
1032 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
1033 int (**pverify_recover_init) (EVP_PKEY_CTX
1034 *ctx),
1035 int (**pverify_recover) (EVP_PKEY_CTX
1036 *ctx,
1037 unsigned char
1038 *sig,
1039 size_t *siglen,
1040 const unsigned
1041 char *tbs,
1042 size_t tbslen))
1043 {
1044 if (pverify_recover_init)
1045 *pverify_recover_init = pmeth->verify_recover_init;
1046 if (pverify_recover)
1047 *pverify_recover = pmeth->verify_recover;
1048 }
1049
1050 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
1051 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
1052 EVP_MD_CTX *mctx),
1053 int (**psignctx) (EVP_PKEY_CTX *ctx,
1054 unsigned char *sig,
1055 size_t *siglen,
1056 EVP_MD_CTX *mctx))
1057 {
1058 if (psignctx_init)
1059 *psignctx_init = pmeth->signctx_init;
1060 if (psignctx)
1061 *psignctx = pmeth->signctx;
1062 }
1063
1064 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
1065 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
1066 EVP_MD_CTX *mctx),
1067 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
1068 const unsigned char *sig,
1069 int siglen,
1070 EVP_MD_CTX *mctx))
1071 {
1072 if (pverifyctx_init)
1073 *pverifyctx_init = pmeth->verifyctx_init;
1074 if (pverifyctx)
1075 *pverifyctx = pmeth->verifyctx;
1076 }
1077
1078 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
1079 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
1080 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
1081 unsigned char *out,
1082 size_t *outlen,
1083 const unsigned char *in,
1084 size_t inlen))
1085 {
1086 if (pencrypt_init)
1087 *pencrypt_init = pmeth->encrypt_init;
1088 if (pencryptfn)
1089 *pencryptfn = pmeth->encrypt;
1090 }
1091
1092 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
1093 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
1094 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
1095 unsigned char *out,
1096 size_t *outlen,
1097 const unsigned char *in,
1098 size_t inlen))
1099 {
1100 if (pdecrypt_init)
1101 *pdecrypt_init = pmeth->decrypt_init;
1102 if (pdecrypt)
1103 *pdecrypt = pmeth->decrypt;
1104 }
1105
1106 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
1107 int (**pderive_init) (EVP_PKEY_CTX *ctx),
1108 int (**pderive) (EVP_PKEY_CTX *ctx,
1109 unsigned char *key,
1110 size_t *keylen))
1111 {
1112 if (pderive_init)
1113 *pderive_init = pmeth->derive_init;
1114 if (pderive)
1115 *pderive = pmeth->derive;
1116 }
1117
1118 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
1119 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1120 void *p2),
1121 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
1122 const char *type,
1123 const char *value))
1124 {
1125 if (pctrl)
1126 *pctrl = pmeth->ctrl;
1127 if (pctrl_str)
1128 *pctrl_str = pmeth->ctrl_str;
1129 }
1130
1131 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
1132 int (**pcheck) (EVP_PKEY *pkey))
1133 {
1134 if (pcheck != NULL)
1135 *pcheck = pmeth->check;
1136 }
1137
1138 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
1139 int (**pcheck) (EVP_PKEY *pkey))
1140 {
1141 if (pcheck != NULL)
1142 *pcheck = pmeth->public_check;
1143 }
1144
1145 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
1146 int (**pcheck) (EVP_PKEY *pkey))
1147 {
1148 if (pcheck != NULL)
1149 *pcheck = pmeth->param_check;
1150 }
1151
1152 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
1153 int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
1154 EVP_MD_CTX *mctx))
1155 {
1156 if (pdigest_custom != NULL)
1157 *pdigest_custom = pmeth->digest_custom;
1158 }