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