]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/signature.c
Don't hold a lock when calling a callback in ossl_namemap_doall_names
[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
d84f5515
MC
332int EVP_SIGNATURE_names_do_all(const EVP_SIGNATURE *signature,
333 void (*fn)(const char *name, void *data),
334 void *data)
e683582b
SL
335{
336 if (signature->prov != NULL)
d84f5515
MC
337 return evp_names_do_all(signature->prov, signature->name_id, fn, data);
338
339 return 1;
e683582b
SL
340}
341
e3efe7a5
SL
342const OSSL_PARAM *EVP_SIGNATURE_gettable_ctx_params(const EVP_SIGNATURE *sig)
343{
344 void *provctx;
345
346 if (sig == NULL || sig->gettable_ctx_params == NULL)
347 return NULL;
348
349 provctx = ossl_provider_ctx(EVP_SIGNATURE_provider(sig));
350 return sig->gettable_ctx_params(provctx);
351}
352
353const OSSL_PARAM *EVP_SIGNATURE_settable_ctx_params(const EVP_SIGNATURE *sig)
354{
355 void *provctx;
356
357 if (sig == NULL || sig->settable_ctx_params == NULL)
358 return NULL;
359
360 provctx = ossl_provider_ctx(EVP_SIGNATURE_provider(sig));
361 return sig->settable_ctx_params(provctx);
362}
363
e683582b
SL
364static int evp_pkey_signature_init(EVP_PKEY_CTX *ctx, int operation)
365{
366 int ret = 0;
367 void *provkey = NULL;
368 EVP_SIGNATURE *signature = NULL;
f6aa5774
RL
369 EVP_KEYMGMT *tmp_keymgmt = NULL;
370 const char *supported_sig = NULL;
e683582b
SL
371
372 if (ctx == NULL) {
9311d0c4 373 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
374 return -2;
375 }
376
377 evp_pkey_ctx_free_old_ops(ctx);
378 ctx->operation = operation;
379
0b9dd384
RL
380 /*
381 * TODO when we stop falling back to legacy, this and the ERR_pop_to_mark()
382 * calls can be removed.
383 */
384 ERR_set_mark();
385
f21c9c64 386 if (evp_pkey_ctx_is_legacy(ctx))
e683582b
SL
387 goto legacy;
388
3c6ed955
RL
389 /*
390 * Ensure that the key is provided, either natively, or as a cached export.
391 * If not, go legacy
392 */
f6aa5774 393 tmp_keymgmt = ctx->keymgmt;
3c6ed955
RL
394 provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
395 &tmp_keymgmt, ctx->propquery);
d0ddf9b4 396 if (tmp_keymgmt == NULL)
f6aa5774
RL
397 goto legacy;
398 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) {
0b9dd384 399 ERR_clear_last_mark();
f6aa5774
RL
400 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
401 goto err;
e683582b 402 }
f6aa5774
RL
403 EVP_KEYMGMT_free(ctx->keymgmt);
404 ctx->keymgmt = tmp_keymgmt;
405
406 if (ctx->keymgmt->query_operation_name != NULL)
407 supported_sig = ctx->keymgmt->query_operation_name(OSSL_OP_SIGNATURE);
408
409 /*
410 * If we didn't get a supported sig, assume there is one with the
411 * same name as the key type.
412 */
413 if (supported_sig == NULL)
414 supported_sig = ctx->keytype;
415
416 /*
417 * Because we cleared out old ops, we shouldn't need to worry about
418 * checking if signature is already there.
419 */
420 signature =
421 EVP_SIGNATURE_fetch(ctx->libctx, supported_sig, ctx->propquery);
422
423 if (signature == NULL
e683582b
SL
424 || (EVP_KEYMGMT_provider(ctx->keymgmt)
425 != EVP_SIGNATURE_provider(signature))) {
426 /*
0b9dd384
RL
427 * We don't need to free ctx->keymgmt here, as it's not necessarily
428 * tied to this operation. It will be freed by EVP_PKEY_CTX_free().
e683582b
SL
429 */
430 EVP_SIGNATURE_free(signature);
431 goto legacy;
432 }
433
0b9dd384
RL
434 /*
435 * TODO remove this when legacy is gone
436 * If we don't have the full support we need with provided methods,
437 * let's go see if legacy does.
438 */
439 ERR_pop_to_mark();
440
441 /* No more legacy from here down to legacy: */
442
e683582b 443 ctx->op.sig.signature = signature;
2c6094ba
RL
444 ctx->op.sig.sigprovctx =
445 signature->newctx(ossl_provider_ctx(signature->prov), ctx->propquery);
e683582b
SL
446 if (ctx->op.sig.sigprovctx == NULL) {
447 /* The provider key can stay in the cache */
9311d0c4 448 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
e683582b
SL
449 goto err;
450 }
451
452 switch (operation) {
453 case EVP_PKEY_OP_SIGN:
454 if (signature->sign_init == NULL) {
9311d0c4 455 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
456 ret = -2;
457 goto err;
458 }
459 ret = signature->sign_init(ctx->op.sig.sigprovctx, provkey);
460 break;
461 case EVP_PKEY_OP_VERIFY:
462 if (signature->verify_init == NULL) {
9311d0c4 463 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
464 ret = -2;
465 goto err;
466 }
467 ret = signature->verify_init(ctx->op.sig.sigprovctx, provkey);
468 break;
469 case EVP_PKEY_OP_VERIFYRECOVER:
470 if (signature->verify_recover_init == NULL) {
9311d0c4 471 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
472 ret = -2;
473 goto err;
474 }
475 ret = signature->verify_recover_init(ctx->op.sig.sigprovctx, provkey);
476 break;
477 default:
9311d0c4 478 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
e683582b
SL
479 goto err;
480 }
481
482 if (ret <= 0) {
483 signature->freectx(ctx->op.sig.sigprovctx);
484 ctx->op.sig.sigprovctx = NULL;
485 goto err;
486 }
86df26b3 487 goto end;
e683582b
SL
488
489 legacy:
0b9dd384
RL
490 /*
491 * TODO remove this when legacy is gone
492 * If we don't have the full support we need with provided methods,
493 * let's go see if legacy does.
494 */
495 ERR_pop_to_mark();
496
e683582b
SL
497 if (ctx->pmeth == NULL
498 || (operation == EVP_PKEY_OP_SIGN && ctx->pmeth->sign == NULL)
499 || (operation == EVP_PKEY_OP_VERIFY && ctx->pmeth->verify == NULL)
500 || (operation == EVP_PKEY_OP_VERIFYRECOVER
501 && ctx->pmeth->verify_recover == NULL)) {
9311d0c4 502 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
503 return -2;
504 }
505
506 switch (operation) {
507 case EVP_PKEY_OP_SIGN:
508 if (ctx->pmeth->sign_init == NULL)
509 return 1;
510 ret = ctx->pmeth->sign_init(ctx);
511 break;
512 case EVP_PKEY_OP_VERIFY:
513 if (ctx->pmeth->verify_init == NULL)
514 return 1;
515 ret = ctx->pmeth->verify_init(ctx);
516 break;
517 case EVP_PKEY_OP_VERIFYRECOVER:
518 if (ctx->pmeth->verify_recover_init == NULL)
519 return 1;
520 ret = ctx->pmeth->verify_recover_init(ctx);
521 break;
522 default:
9311d0c4 523 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
e683582b
SL
524 goto err;
525 }
526 if (ret <= 0)
527 goto err;
86df26b3
RL
528 end:
529#ifndef FIPS_MODULE
530 if (ret > 0)
531 ret = evp_pkey_ctx_use_cached_data(ctx);
532#endif
e683582b 533
86df26b3 534 return ret;
e683582b 535 err:
c7fa9297 536 evp_pkey_ctx_free_old_ops(ctx);
e683582b
SL
537 ctx->operation = EVP_PKEY_OP_UNDEFINED;
538 return ret;
539}
540
541int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
542{
543 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_SIGN);
544}
545
546int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
547 unsigned char *sig, size_t *siglen,
548 const unsigned char *tbs, size_t tbslen)
549{
550 int ret;
551
552 if (ctx == NULL) {
9311d0c4 553 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
554 return -2;
555 }
556
557 if (ctx->operation != EVP_PKEY_OP_SIGN) {
9311d0c4 558 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
e683582b
SL
559 return -1;
560 }
561
562 if (ctx->op.sig.sigprovctx == NULL)
563 goto legacy;
564
565 ret = ctx->op.sig.signature->sign(ctx->op.sig.sigprovctx, sig, siglen,
566 SIZE_MAX, tbs, tbslen);
567
568 return ret;
569 legacy:
570
571 if (ctx->pmeth == NULL || ctx->pmeth->sign == NULL) {
9311d0c4 572 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
573 return -2;
574 }
575
576 M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN)
577 return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
578}
579
580int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
581{
582 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_VERIFY);
583}
584
585int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
586 const unsigned char *sig, size_t siglen,
587 const unsigned char *tbs, size_t tbslen)
588{
589 int ret;
590
591 if (ctx == NULL) {
9311d0c4 592 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
593 return -2;
594 }
595
596 if (ctx->operation != EVP_PKEY_OP_VERIFY) {
9311d0c4 597 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
e683582b
SL
598 return -1;
599 }
600
601 if (ctx->op.sig.sigprovctx == NULL)
602 goto legacy;
603
604 ret = ctx->op.sig.signature->verify(ctx->op.sig.sigprovctx, sig, siglen,
605 tbs, tbslen);
606
607 return ret;
608 legacy:
609 if (ctx->pmeth == NULL || ctx->pmeth->verify == NULL) {
9311d0c4 610 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
611 return -2;
612 }
613
614 return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
615}
616
617int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
618{
619 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_VERIFYRECOVER);
620}
621
622int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
623 unsigned char *rout, size_t *routlen,
624 const unsigned char *sig, size_t siglen)
625{
626 int ret;
627
628 if (ctx == NULL) {
9311d0c4 629 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
630 return -2;
631 }
632
633 if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
9311d0c4 634 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
e683582b
SL
635 return -1;
636 }
637
638 if (ctx->op.sig.sigprovctx == NULL)
639 goto legacy;
640
641 ret = ctx->op.sig.signature->verify_recover(ctx->op.sig.sigprovctx, rout,
642 routlen,
643 (rout == NULL ? 0 : *routlen),
644 sig, siglen);
645 return ret;
646 legacy:
647 if (ctx->pmeth == NULL || ctx->pmeth->verify_recover == NULL) {
9311d0c4 648 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
649 return -2;
650 }
651 M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER)
652 return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
653}