]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/signature.c
Rename OPENSSL_CTX prefix to OSSL_LIB_CTX
[thirdparty/openssl.git] / crypto / evp / signature.c
CommitLineData
e683582b 1/*
33388b44 2 * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
e683582b
SL
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
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
8 */
9
10#include <stdio.h>
11#include <stdlib.h>
12#include <openssl/objects.h>
13#include <openssl/evp.h>
14#include "internal/cryptlib.h"
15#include "crypto/evp.h"
16#include "internal/provider.h"
17#include "evp_local.h"
18
19static EVP_SIGNATURE *evp_signature_new(OSSL_PROVIDER *prov)
20{
21 EVP_SIGNATURE *signature = OPENSSL_zalloc(sizeof(EVP_SIGNATURE));
22
23 if (signature == NULL) {
24 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
25 return NULL;
26 }
27
28 signature->lock = CRYPTO_THREAD_lock_new();
29 if (signature->lock == NULL) {
30 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
31 OPENSSL_free(signature);
32 return NULL;
33 }
34 signature->prov = prov;
35 ossl_provider_up_ref(prov);
36 signature->refcnt = 1;
37
38 return signature;
39}
40
41static void *evp_signature_from_dispatch(int name_id,
42 const OSSL_DISPATCH *fns,
43 OSSL_PROVIDER *prov)
44{
45 EVP_SIGNATURE *signature = NULL;
46 int ctxfncnt = 0, signfncnt = 0, verifyfncnt = 0, verifyrecfncnt = 0;
47 int digsignfncnt = 0, digverifyfncnt = 0;
48 int gparamfncnt = 0, sparamfncnt = 0, gmdparamfncnt = 0, smdparamfncnt = 0;
49
50 if ((signature = evp_signature_new(prov)) == NULL) {
51 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
52 goto err;
53 }
54
55 signature->name_id = name_id;
56
57 for (; fns->function_id != 0; fns++) {
58 switch (fns->function_id) {
59 case OSSL_FUNC_SIGNATURE_NEWCTX:
60 if (signature->newctx != NULL)
61 break;
363b1e5d 62 signature->newctx = OSSL_FUNC_signature_newctx(fns);
e683582b
SL
63 ctxfncnt++;
64 break;
65 case OSSL_FUNC_SIGNATURE_SIGN_INIT:
66 if (signature->sign_init != NULL)
67 break;
363b1e5d 68 signature->sign_init = OSSL_FUNC_signature_sign_init(fns);
e683582b
SL
69 signfncnt++;
70 break;
71 case OSSL_FUNC_SIGNATURE_SIGN:
72 if (signature->sign != NULL)
73 break;
363b1e5d 74 signature->sign = OSSL_FUNC_signature_sign(fns);
e683582b
SL
75 signfncnt++;
76 break;
77 case OSSL_FUNC_SIGNATURE_VERIFY_INIT:
78 if (signature->verify_init != NULL)
79 break;
363b1e5d 80 signature->verify_init = OSSL_FUNC_signature_verify_init(fns);
e683582b
SL
81 verifyfncnt++;
82 break;
83 case OSSL_FUNC_SIGNATURE_VERIFY:
84 if (signature->verify != NULL)
85 break;
363b1e5d 86 signature->verify = OSSL_FUNC_signature_verify(fns);
e683582b
SL
87 verifyfncnt++;
88 break;
89 case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT:
90 if (signature->verify_recover_init != NULL)
91 break;
92 signature->verify_recover_init
363b1e5d 93 = OSSL_FUNC_signature_verify_recover_init(fns);
e683582b
SL
94 verifyrecfncnt++;
95 break;
96 case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER:
97 if (signature->verify_recover != NULL)
98 break;
99 signature->verify_recover
363b1e5d 100 = OSSL_FUNC_signature_verify_recover(fns);
e683582b
SL
101 verifyrecfncnt++;
102 break;
103 case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT:
104 if (signature->digest_sign_init != NULL)
105 break;
106 signature->digest_sign_init
363b1e5d 107 = OSSL_FUNC_signature_digest_sign_init(fns);
e683582b
SL
108 break;
109 case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE:
110 if (signature->digest_sign_update != NULL)
111 break;
112 signature->digest_sign_update
363b1e5d 113 = OSSL_FUNC_signature_digest_sign_update(fns);
e683582b
SL
114 digsignfncnt++;
115 break;
116 case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL:
117 if (signature->digest_sign_final != NULL)
118 break;
119 signature->digest_sign_final
363b1e5d 120 = OSSL_FUNC_signature_digest_sign_final(fns);
e683582b
SL
121 digsignfncnt++;
122 break;
eea1e780
MC
123 case OSSL_FUNC_SIGNATURE_DIGEST_SIGN:
124 if (signature->digest_sign != NULL)
125 break;
126 signature->digest_sign
363b1e5d 127 = OSSL_FUNC_signature_digest_sign(fns);
eea1e780 128 break;
e683582b
SL
129 case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT:
130 if (signature->digest_verify_init != NULL)
131 break;
132 signature->digest_verify_init
363b1e5d 133 = OSSL_FUNC_signature_digest_verify_init(fns);
e683582b
SL
134 break;
135 case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE:
136 if (signature->digest_verify_update != NULL)
137 break;
138 signature->digest_verify_update
363b1e5d 139 = OSSL_FUNC_signature_digest_verify_update(fns);
e683582b
SL
140 digverifyfncnt++;
141 break;
142 case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL:
143 if (signature->digest_verify_final != NULL)
144 break;
145 signature->digest_verify_final
363b1e5d 146 = OSSL_FUNC_signature_digest_verify_final(fns);
e683582b
SL
147 digverifyfncnt++;
148 break;
eea1e780
MC
149 case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY:
150 if (signature->digest_verify != NULL)
151 break;
152 signature->digest_verify
363b1e5d 153 = OSSL_FUNC_signature_digest_verify(fns);
eea1e780 154 break;
e683582b
SL
155 case OSSL_FUNC_SIGNATURE_FREECTX:
156 if (signature->freectx != NULL)
157 break;
363b1e5d 158 signature->freectx = OSSL_FUNC_signature_freectx(fns);
e683582b
SL
159 ctxfncnt++;
160 break;
161 case OSSL_FUNC_SIGNATURE_DUPCTX:
162 if (signature->dupctx != NULL)
163 break;
363b1e5d 164 signature->dupctx = OSSL_FUNC_signature_dupctx(fns);
e683582b
SL
165 break;
166 case OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS:
167 if (signature->get_ctx_params != NULL)
168 break;
169 signature->get_ctx_params
363b1e5d 170 = OSSL_FUNC_signature_get_ctx_params(fns);
e683582b
SL
171 gparamfncnt++;
172 break;
173 case OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS:
174 if (signature->gettable_ctx_params != NULL)
175 break;
176 signature->gettable_ctx_params
363b1e5d 177 = OSSL_FUNC_signature_gettable_ctx_params(fns);
e683582b
SL
178 gparamfncnt++;
179 break;
180 case OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS:
181 if (signature->set_ctx_params != NULL)
182 break;
183 signature->set_ctx_params
363b1e5d 184 = OSSL_FUNC_signature_set_ctx_params(fns);
e683582b
SL
185 sparamfncnt++;
186 break;
187 case OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS:
188 if (signature->settable_ctx_params != NULL)
189 break;
190 signature->settable_ctx_params
363b1e5d 191 = OSSL_FUNC_signature_settable_ctx_params(fns);
e683582b
SL
192 sparamfncnt++;
193 break;
194 case OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS:
195 if (signature->get_ctx_md_params != NULL)
196 break;
197 signature->get_ctx_md_params
363b1e5d 198 = OSSL_FUNC_signature_get_ctx_md_params(fns);
e683582b
SL
199 gmdparamfncnt++;
200 break;
201 case OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS:
202 if (signature->gettable_ctx_md_params != NULL)
203 break;
204 signature->gettable_ctx_md_params
363b1e5d 205 = OSSL_FUNC_signature_gettable_ctx_md_params(fns);
e683582b
SL
206 gmdparamfncnt++;
207 break;
208 case OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS:
209 if (signature->set_ctx_md_params != NULL)
210 break;
211 signature->set_ctx_md_params
363b1e5d 212 = OSSL_FUNC_signature_set_ctx_md_params(fns);
e683582b
SL
213 smdparamfncnt++;
214 break;
215 case OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS:
216 if (signature->settable_ctx_md_params != NULL)
217 break;
218 signature->settable_ctx_md_params
363b1e5d 219 = OSSL_FUNC_signature_settable_ctx_md_params(fns);
e683582b
SL
220 smdparamfncnt++;
221 break;
222 }
223 }
224 if (ctxfncnt != 2
225 || (signfncnt == 0
226 && verifyfncnt == 0
227 && verifyrecfncnt == 0
228 && digsignfncnt == 0
eea1e780
MC
229 && digverifyfncnt == 0
230 && signature->digest_sign == NULL
231 && signature->digest_verify == NULL)
e683582b
SL
232 || (signfncnt != 0 && signfncnt != 2)
233 || (verifyfncnt != 0 && verifyfncnt != 2)
234 || (verifyrecfncnt != 0 && verifyrecfncnt != 2)
eea1e780
MC
235 || (digsignfncnt != 0 && digsignfncnt != 2)
236 || (digsignfncnt == 2 && signature->digest_sign_init == NULL)
237 || (digverifyfncnt != 0 && digverifyfncnt != 2)
238 || (digverifyfncnt == 2 && signature->digest_verify_init == NULL)
239 || (signature->digest_sign != NULL
240 && signature->digest_sign_init == NULL)
241 || (signature->digest_verify != NULL
242 && signature->digest_verify_init == NULL)
e683582b
SL
243 || (gparamfncnt != 0 && gparamfncnt != 2)
244 || (sparamfncnt != 0 && sparamfncnt != 2)
245 || (gmdparamfncnt != 0 && gmdparamfncnt != 2)
246 || (smdparamfncnt != 0 && smdparamfncnt != 2)) {
247 /*
248 * In order to be a consistent set of functions we must have at least
249 * a set of context functions (newctx and freectx) as well as a set of
250 * "signature" functions:
251 * (sign_init, sign) or
252 * (verify_init verify) or
253 * (verify_recover_init, verify_recover) or
254 * (digest_sign_init, digest_sign_update, digest_sign_final) or
eea1e780
MC
255 * (digest_verify_init, digest_verify_update, digest_verify_final) or
256 * (digest_sign_init, digest_sign) or
257 * (digest_verify_init, digest_verify).
e683582b
SL
258 *
259 * set_ctx_params and settable_ctx_params are optional, but if one of
260 * them is present then the other one must also be present. The same
261 * applies to get_ctx_params and gettable_ctx_params. The same rules
262 * apply to the "md_params" functions. The dupctx function is optional.
263 */
264 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
265 goto err;
266 }
267
268 return signature;
269 err:
270 EVP_SIGNATURE_free(signature);
271 return NULL;
272}
273
274void EVP_SIGNATURE_free(EVP_SIGNATURE *signature)
275{
276 if (signature != NULL) {
277 int i;
278
279 CRYPTO_DOWN_REF(&signature->refcnt, &i, signature->lock);
280 if (i > 0)
281 return;
282 ossl_provider_free(signature->prov);
283 CRYPTO_THREAD_lock_free(signature->lock);
284 OPENSSL_free(signature);
285 }
286}
287
288int EVP_SIGNATURE_up_ref(EVP_SIGNATURE *signature)
289{
290 int ref = 0;
291
292 CRYPTO_UP_REF(&signature->refcnt, &ref, signature->lock);
293 return 1;
294}
295
296OSSL_PROVIDER *EVP_SIGNATURE_provider(const EVP_SIGNATURE *signature)
297{
298 return signature->prov;
299}
300
b4250010 301EVP_SIGNATURE *EVP_SIGNATURE_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
e683582b
SL
302 const char *properties)
303{
304 return evp_generic_fetch(ctx, OSSL_OP_SIGNATURE, algorithm, properties,
305 evp_signature_from_dispatch,
306 (int (*)(void *))EVP_SIGNATURE_up_ref,
307 (void (*)(void *))EVP_SIGNATURE_free);
308}
309
310int EVP_SIGNATURE_is_a(const EVP_SIGNATURE *signature, const char *name)
311{
e4a1d023 312 return evp_is_a(signature->prov, signature->name_id, NULL, name);
e683582b
SL
313}
314
315int EVP_SIGNATURE_number(const EVP_SIGNATURE *signature)
316{
317 return signature->name_id;
318}
319
b4250010 320void EVP_SIGNATURE_do_all_provided(OSSL_LIB_CTX *libctx,
e683582b
SL
321 void (*fn)(EVP_SIGNATURE *signature,
322 void *arg),
323 void *arg)
324{
325 evp_generic_do_all(libctx, OSSL_OP_SIGNATURE,
326 (void (*)(void *, void *))fn, arg,
327 evp_signature_from_dispatch,
328 (void (*)(void *))EVP_SIGNATURE_free);
329}
330
331
332void EVP_SIGNATURE_names_do_all(const EVP_SIGNATURE *signature,
333 void (*fn)(const char *name, void *data),
334 void *data)
335{
336 if (signature->prov != NULL)
337 evp_names_do_all(signature->prov, signature->name_id, fn, data);
338}
339
e3efe7a5
SL
340const OSSL_PARAM *EVP_SIGNATURE_gettable_ctx_params(const EVP_SIGNATURE *sig)
341{
342 void *provctx;
343
344 if (sig == NULL || sig->gettable_ctx_params == NULL)
345 return NULL;
346
347 provctx = ossl_provider_ctx(EVP_SIGNATURE_provider(sig));
348 return sig->gettable_ctx_params(provctx);
349}
350
351const OSSL_PARAM *EVP_SIGNATURE_settable_ctx_params(const EVP_SIGNATURE *sig)
352{
353 void *provctx;
354
355 if (sig == NULL || sig->settable_ctx_params == NULL)
356 return NULL;
357
358 provctx = ossl_provider_ctx(EVP_SIGNATURE_provider(sig));
359 return sig->settable_ctx_params(provctx);
360}
361
e683582b
SL
362static int evp_pkey_signature_init(EVP_PKEY_CTX *ctx, int operation)
363{
364 int ret = 0;
365 void *provkey = NULL;
366 EVP_SIGNATURE *signature = NULL;
f6aa5774
RL
367 EVP_KEYMGMT *tmp_keymgmt = NULL;
368 const char *supported_sig = NULL;
e683582b
SL
369
370 if (ctx == NULL) {
371 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
372 return -2;
373 }
374
375 evp_pkey_ctx_free_old_ops(ctx);
376 ctx->operation = operation;
377
0b9dd384
RL
378 /*
379 * TODO when we stop falling back to legacy, this and the ERR_pop_to_mark()
380 * calls can be removed.
381 */
382 ERR_set_mark();
383
f21c9c64 384 if (evp_pkey_ctx_is_legacy(ctx))
e683582b
SL
385 goto legacy;
386
3c6ed955
RL
387 /*
388 * Ensure that the key is provided, either natively, or as a cached export.
389 * If not, go legacy
390 */
f6aa5774 391 tmp_keymgmt = ctx->keymgmt;
3c6ed955
RL
392 provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
393 &tmp_keymgmt, ctx->propquery);
d0ddf9b4 394 if (tmp_keymgmt == NULL)
f6aa5774
RL
395 goto legacy;
396 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) {
0b9dd384 397 ERR_clear_last_mark();
f6aa5774
RL
398 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
399 goto err;
e683582b 400 }
f6aa5774
RL
401 EVP_KEYMGMT_free(ctx->keymgmt);
402 ctx->keymgmt = tmp_keymgmt;
403
404 if (ctx->keymgmt->query_operation_name != NULL)
405 supported_sig = ctx->keymgmt->query_operation_name(OSSL_OP_SIGNATURE);
406
407 /*
408 * If we didn't get a supported sig, assume there is one with the
409 * same name as the key type.
410 */
411 if (supported_sig == NULL)
412 supported_sig = ctx->keytype;
413
414 /*
415 * Because we cleared out old ops, we shouldn't need to worry about
416 * checking if signature is already there.
417 */
418 signature =
419 EVP_SIGNATURE_fetch(ctx->libctx, supported_sig, ctx->propquery);
420
421 if (signature == NULL
e683582b
SL
422 || (EVP_KEYMGMT_provider(ctx->keymgmt)
423 != EVP_SIGNATURE_provider(signature))) {
424 /*
0b9dd384
RL
425 * We don't need to free ctx->keymgmt here, as it's not necessarily
426 * tied to this operation. It will be freed by EVP_PKEY_CTX_free().
e683582b
SL
427 */
428 EVP_SIGNATURE_free(signature);
429 goto legacy;
430 }
431
0b9dd384
RL
432 /*
433 * TODO remove this when legacy is gone
434 * If we don't have the full support we need with provided methods,
435 * let's go see if legacy does.
436 */
437 ERR_pop_to_mark();
438
439 /* No more legacy from here down to legacy: */
440
e683582b 441 ctx->op.sig.signature = signature;
2c6094ba
RL
442 ctx->op.sig.sigprovctx =
443 signature->newctx(ossl_provider_ctx(signature->prov), ctx->propquery);
e683582b
SL
444 if (ctx->op.sig.sigprovctx == NULL) {
445 /* The provider key can stay in the cache */
446 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
447 goto err;
448 }
449
450 switch (operation) {
451 case EVP_PKEY_OP_SIGN:
452 if (signature->sign_init == NULL) {
453 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
454 ret = -2;
455 goto err;
456 }
457 ret = signature->sign_init(ctx->op.sig.sigprovctx, provkey);
458 break;
459 case EVP_PKEY_OP_VERIFY:
460 if (signature->verify_init == NULL) {
461 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
462 ret = -2;
463 goto err;
464 }
465 ret = signature->verify_init(ctx->op.sig.sigprovctx, provkey);
466 break;
467 case EVP_PKEY_OP_VERIFYRECOVER:
468 if (signature->verify_recover_init == NULL) {
469 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
470 ret = -2;
471 goto err;
472 }
473 ret = signature->verify_recover_init(ctx->op.sig.sigprovctx, provkey);
474 break;
475 default:
476 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
477 goto err;
478 }
479
480 if (ret <= 0) {
481 signature->freectx(ctx->op.sig.sigprovctx);
482 ctx->op.sig.sigprovctx = NULL;
483 goto err;
484 }
86df26b3 485 goto end;
e683582b
SL
486
487 legacy:
0b9dd384
RL
488 /*
489 * TODO remove this when legacy is gone
490 * If we don't have the full support we need with provided methods,
491 * let's go see if legacy does.
492 */
493 ERR_pop_to_mark();
494
e683582b
SL
495 if (ctx->pmeth == NULL
496 || (operation == EVP_PKEY_OP_SIGN && ctx->pmeth->sign == NULL)
497 || (operation == EVP_PKEY_OP_VERIFY && ctx->pmeth->verify == NULL)
498 || (operation == EVP_PKEY_OP_VERIFYRECOVER
499 && ctx->pmeth->verify_recover == NULL)) {
500 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
501 return -2;
502 }
503
504 switch (operation) {
505 case EVP_PKEY_OP_SIGN:
506 if (ctx->pmeth->sign_init == NULL)
507 return 1;
508 ret = ctx->pmeth->sign_init(ctx);
509 break;
510 case EVP_PKEY_OP_VERIFY:
511 if (ctx->pmeth->verify_init == NULL)
512 return 1;
513 ret = ctx->pmeth->verify_init(ctx);
514 break;
515 case EVP_PKEY_OP_VERIFYRECOVER:
516 if (ctx->pmeth->verify_recover_init == NULL)
517 return 1;
518 ret = ctx->pmeth->verify_recover_init(ctx);
519 break;
520 default:
521 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
522 goto err;
523 }
524 if (ret <= 0)
525 goto err;
86df26b3
RL
526 end:
527#ifndef FIPS_MODULE
528 if (ret > 0)
529 ret = evp_pkey_ctx_use_cached_data(ctx);
530#endif
e683582b 531
86df26b3 532 return ret;
e683582b 533 err:
c7fa9297 534 evp_pkey_ctx_free_old_ops(ctx);
e683582b
SL
535 ctx->operation = EVP_PKEY_OP_UNDEFINED;
536 return ret;
537}
538
539int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
540{
541 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_SIGN);
542}
543
544int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
545 unsigned char *sig, size_t *siglen,
546 const unsigned char *tbs, size_t tbslen)
547{
548 int ret;
549
550 if (ctx == NULL) {
551 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
552 return -2;
553 }
554
555 if (ctx->operation != EVP_PKEY_OP_SIGN) {
556 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
557 return -1;
558 }
559
560 if (ctx->op.sig.sigprovctx == NULL)
561 goto legacy;
562
563 ret = ctx->op.sig.signature->sign(ctx->op.sig.sigprovctx, sig, siglen,
564 SIZE_MAX, tbs, tbslen);
565
566 return ret;
567 legacy:
568
569 if (ctx->pmeth == NULL || ctx->pmeth->sign == NULL) {
570 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
571 return -2;
572 }
573
574 M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN)
575 return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
576}
577
578int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
579{
580 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_VERIFY);
581}
582
583int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
584 const unsigned char *sig, size_t siglen,
585 const unsigned char *tbs, size_t tbslen)
586{
587 int ret;
588
589 if (ctx == NULL) {
590 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
591 return -2;
592 }
593
594 if (ctx->operation != EVP_PKEY_OP_VERIFY) {
595 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
596 return -1;
597 }
598
599 if (ctx->op.sig.sigprovctx == NULL)
600 goto legacy;
601
602 ret = ctx->op.sig.signature->verify(ctx->op.sig.sigprovctx, sig, siglen,
603 tbs, tbslen);
604
605 return ret;
606 legacy:
607 if (ctx->pmeth == NULL || ctx->pmeth->verify == NULL) {
608 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
609 return -2;
610 }
611
612 return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
613}
614
615int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
616{
617 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_VERIFYRECOVER);
618}
619
620int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
621 unsigned char *rout, size_t *routlen,
622 const unsigned char *sig, size_t siglen)
623{
624 int ret;
625
626 if (ctx == NULL) {
627 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
628 return -2;
629 }
630
631 if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
632 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
633 return -1;
634 }
635
636 if (ctx->op.sig.sigprovctx == NULL)
637 goto legacy;
638
639 ret = ctx->op.sig.signature->verify_recover(ctx->op.sig.sigprovctx, rout,
640 routlen,
641 (rout == NULL ? 0 : *routlen),
642 sig, siglen);
643 return ret;
644 legacy:
645 if (ctx->pmeth == NULL || ctx->pmeth->verify_recover == NULL) {
646 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
647 return -2;
648 }
649 M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER)
650 return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
651}