1 /*
2 * Copyright 2006-2022 The OpenSSL Project Authors. All Rights Reserved.
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 "internal/cryptlib.h"
12 #include <openssl/evp.h>
13 #include <openssl/objects.h>
14 #include "crypto/evp.h"
15 #include "internal/provider.h"
16 #include "internal/numbers.h" /* includes SIZE_MAX */
17 #include "evp_local.h"
18
19 #ifndef FIPS_MODULE
20
update(EVP_MD_CTX * ctx,const void * data,size_t datalen)21 static int update(EVP_MD_CTX *ctx, const void *data, size_t datalen)
22 {
23 ERR_raise(ERR_LIB_EVP, EVP_R_ONLY_ONESHOT_SUPPORTED);
24 return 0;
25 }
26
27 /*
28 * If we get the "NULL" md then the name comes back as "UNDEF". We want to use
29 * NULL for this.
30 */
canon_mdname(const char * mdname)31 static const char *canon_mdname(const char *mdname)
32 {
33 if (mdname != NULL && strcmp(mdname, "UNDEF") == 0)
34 return NULL;
35
36 return mdname;
37 }
38
do_sigver_init(EVP_MD_CTX * ctx,EVP_PKEY_CTX ** pctx,const EVP_MD * type,const char * mdname,OSSL_LIB_CTX * libctx,const char * props,ENGINE * e,EVP_PKEY * pkey,int ver,const OSSL_PARAM params[])39 static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
40 const EVP_MD *type, const char *mdname,
41 OSSL_LIB_CTX *libctx, const char *props,
42 ENGINE *e, EVP_PKEY *pkey, int ver,
43 const OSSL_PARAM params[])
44 {
45 EVP_PKEY_CTX *locpctx = NULL;
46 EVP_SIGNATURE *signature = NULL;
47 EVP_KEYMGMT *tmp_keymgmt = NULL;
48 const OSSL_PROVIDER *tmp_prov = NULL;
49 const char *supported_sig = NULL;
50 char locmdname[80] = ""; /* 80 chars should be enough */
51 void *provkey = NULL;
52 int ret, iter, reinit = 1;
53
54 if (ctx->algctx != NULL) {
55 if (!ossl_assert(ctx->digest != NULL)) {
56 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
57 return 0;
58 }
59 if (ctx->digest->freectx != NULL)
60 ctx->digest->freectx(ctx->algctx);
61 ctx->algctx = NULL;
62 }
63
64 if (ctx->pctx == NULL) {
65 reinit = 0;
66 if (e == NULL)
67 ctx->pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, props);
68 else
69 ctx->pctx = EVP_PKEY_CTX_new(pkey, e);
70 }
71 if (ctx->pctx == NULL)
72 return 0;
73
74 locpctx = ctx->pctx;
75 ERR_set_mark();
76
77 if (evp_pkey_ctx_is_legacy(locpctx))
78 goto legacy;
79
80 /* do not reinitialize if pkey is set or operation is different */
81 if (reinit
82 && (pkey != NULL
83 || locpctx->operation != (ver ? EVP_PKEY_OP_VERIFYCTX
84 : EVP_PKEY_OP_SIGNCTX)
85 || (signature = locpctx->op.sig.signature) == NULL
86 || locpctx->op.sig.algctx == NULL))
87 reinit = 0;
88
89 if (props == NULL)
90 props = locpctx->propquery;
91
92 if (locpctx->pkey == NULL) {
93 ERR_clear_last_mark();
94 ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
95 goto err;
96 }
97
98 if (!reinit) {
99 evp_pkey_ctx_free_old_ops(locpctx);
100 } else {
101 if (mdname == NULL && type == NULL)
102 mdname = canon_mdname(EVP_MD_get0_name(ctx->reqdigest));
103 goto reinitialize;
104 }
105
106 /*
107 * Try to derive the supported signature from |locpctx->keymgmt|.
108 */
109 if (!ossl_assert(locpctx->pkey->keymgmt == NULL
110 || locpctx->pkey->keymgmt == locpctx->keymgmt)) {
111 ERR_clear_last_mark();
112 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
113 goto err;
114 }
115 supported_sig = evp_keymgmt_util_query_operation_name(locpctx->keymgmt,
116 OSSL_OP_SIGNATURE);
117 if (supported_sig == NULL) {
118 ERR_clear_last_mark();
119 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
120 goto err;
121 }
122
123 /*
124 * We perform two iterations:
125 *
126 * 1. Do the normal signature fetch, using the fetching data given by
127 * the EVP_PKEY_CTX.
128 * 2. Do the provider specific signature fetch, from the same provider
129 * as |ctx->keymgmt|
130 *
131 * We then try to fetch the keymgmt from the same provider as the
132 * signature, and try to export |ctx->pkey| to that keymgmt (when
133 * this keymgmt happens to be the same as |ctx->keymgmt|, the export
134 * is a no-op, but we call it anyway to not complicate the code even
135 * more).
136 * If the export call succeeds (returns a non-NULL provider key pointer),
137 * we're done and can perform the operation itself. If not, we perform
138 * the second iteration, or jump to legacy.
139 */
140 for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) {
141 EVP_KEYMGMT *tmp_keymgmt_tofree = NULL;
142
143 /*
144 * If we're on the second iteration, free the results from the first.
145 * They are NULL on the first iteration, so no need to check what
146 * iteration we're on.
147 */
148 EVP_SIGNATURE_free(signature);
149 EVP_KEYMGMT_free(tmp_keymgmt);
150
151 switch (iter) {
152 case 1:
153 signature = EVP_SIGNATURE_fetch(locpctx->libctx, supported_sig,
154 locpctx->propquery);
155 if (signature != NULL)
156 tmp_prov = EVP_SIGNATURE_get0_provider(signature);
157 break;
158 case 2:
159 tmp_prov = EVP_KEYMGMT_get0_provider(locpctx->keymgmt);
160 signature =
161 evp_signature_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
162 supported_sig, locpctx->propquery);
163 if (signature == NULL)
164 goto legacy;
165 break;
166 }
167 if (signature == NULL)
168 continue;
169
170 /*
171 * Ensure that the key is provided, either natively, or as a cached
172 * export. We start by fetching the keymgmt with the same name as
173 * |locpctx->pkey|, but from the provider of the signature method, using
174 * the same property query as when fetching the signature method.
175 * With the keymgmt we found (if we did), we try to export |locpctx->pkey|
176 * to it (evp_pkey_export_to_provider() is smart enough to only actually
177
178 * export it if |tmp_keymgmt| is different from |locpctx->pkey|'s keymgmt)
179 */
180 tmp_keymgmt_tofree = tmp_keymgmt =
181 evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
182 EVP_KEYMGMT_get0_name(locpctx->keymgmt),
183 locpctx->propquery);
184 if (tmp_keymgmt != NULL)
185 provkey = evp_pkey_export_to_provider(locpctx->pkey, locpctx->libctx,
186 &tmp_keymgmt, locpctx->propquery);
187 if (tmp_keymgmt == NULL)
188 EVP_KEYMGMT_free(tmp_keymgmt_tofree);
189 }
190
191 if (provkey == NULL) {
192 EVP_SIGNATURE_free(signature);
193 ERR_clear_last_mark();
194 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
195 goto err;
196 }
197
198 ERR_pop_to_mark();
199
200 /* No more legacy from here down to legacy: */
201
202 locpctx->op.sig.signature = signature;
203 locpctx->operation = ver ? EVP_PKEY_OP_VERIFYCTX
204 : EVP_PKEY_OP_SIGNCTX;
205 locpctx->op.sig.algctx
206 = signature->newctx(ossl_provider_ctx(signature->prov), props);
207 if (locpctx->op.sig.algctx == NULL) {
208 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
209 goto err;
210 }
211
212 reinitialize:
213 if (pctx != NULL)
214 *pctx = locpctx;
215
216 if (type != NULL) {
217 ctx->reqdigest = type;
218 if (mdname == NULL)
219 mdname = canon_mdname(EVP_MD_get0_name(type));
220 } else {
221 if (mdname == NULL && !reinit) {
222 if (evp_keymgmt_util_get_deflt_digest_name(tmp_keymgmt, provkey,
223 locmdname,
224 sizeof(locmdname)) > 0) {
225 mdname = canon_mdname(locmdname);
226 }
227 }
228
229 if (mdname != NULL) {
230 /*
231 * We're about to get a new digest so clear anything associated with
232 * an old digest.
233 */
234 evp_md_ctx_clear_digest(ctx, 1, 0);
235
236 /* legacy code support for engines */
237 ERR_set_mark();
238 /*
239 * This might be requested by a later call to EVP_MD_CTX_get0_md().
240 * In that case the "explicit fetch" rules apply for that
241 * function (as per man pages), i.e. the ref count is not updated
242 * so the EVP_MD should not be used beyound the lifetime of the
243 * EVP_MD_CTX.
244 */
245 ctx->fetched_digest = EVP_MD_fetch(locpctx->libctx, mdname, props);
246 if (ctx->fetched_digest != NULL) {
247 ctx->digest = ctx->reqdigest = ctx->fetched_digest;
248 } else {
249 /* legacy engine support : remove the mark when this is deleted */
250 ctx->reqdigest = ctx->digest = EVP_get_digestbyname(mdname);
251 if (ctx->digest == NULL) {
252 (void)ERR_clear_last_mark();
253 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
254 goto err;
255 }
256 }
257 (void)ERR_pop_to_mark();
258 }
259 }
260
261 if (ver) {
262 if (signature->digest_verify_init == NULL) {
263 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
264 goto err;
265 }
266 ret = signature->digest_verify_init(locpctx->op.sig.algctx,
267 mdname, provkey, params);
268 } else {
269 if (signature->digest_sign_init == NULL) {
270 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
271 goto err;
272 }
273 ret = signature->digest_sign_init(locpctx->op.sig.algctx,
274 mdname, provkey, params);
275 }
276
277 /*
278 * If the operation was not a success and no digest was found, an error
279 * needs to be raised.
280 */
281 if (ret > 0 || mdname != NULL)
282 goto end;
283 if (type == NULL) /* This check is redundant but clarifies matters */
284 ERR_raise(ERR_LIB_EVP, EVP_R_NO_DEFAULT_DIGEST);
285
286 err:
287 evp_pkey_ctx_free_old_ops(locpctx);
288 locpctx->operation = EVP_PKEY_OP_UNDEFINED;
289 EVP_KEYMGMT_free(tmp_keymgmt);
290 return 0;
291
292 legacy:
293 /*
294 * If we don't have the full support we need with provided methods,
295 * let's go see if legacy does.
296 */
297 ERR_pop_to_mark();
298 EVP_KEYMGMT_free(tmp_keymgmt);
299 tmp_keymgmt = NULL;
300
301 if (type == NULL && mdname != NULL)
302 type = evp_get_digestbyname_ex(locpctx->libctx, mdname);
303
304 if (ctx->pctx->pmeth == NULL) {
305 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
306 return 0;
307 }
308
309 if (!(ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)) {
310
311 if (type == NULL) {
312 int def_nid;
313 if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) > 0)
314 type = EVP_get_digestbynid(def_nid);
315 }
316
317 if (type == NULL) {
318 ERR_raise(ERR_LIB_EVP, EVP_R_NO_DEFAULT_DIGEST);
319 return 0;
320 }
321 }
322
323 if (ver) {
324 if (ctx->pctx->pmeth->verifyctx_init) {
325 if (ctx->pctx->pmeth->verifyctx_init(ctx->pctx, ctx) <= 0)
326 return 0;
327 ctx->pctx->operation = EVP_PKEY_OP_VERIFYCTX;
328 } else if (ctx->pctx->pmeth->digestverify != 0) {
329 ctx->pctx->operation = EVP_PKEY_OP_VERIFY;
330 ctx->update = update;
331 } else if (EVP_PKEY_verify_init(ctx->pctx) <= 0) {
332 return 0;
333 }
334 } else {
335 if (ctx->pctx->pmeth->signctx_init) {
336 if (ctx->pctx->pmeth->signctx_init(ctx->pctx, ctx) <= 0)
337 return 0;
338 ctx->pctx->operation = EVP_PKEY_OP_SIGNCTX;
339 } else if (ctx->pctx->pmeth->digestsign != 0) {
340 ctx->pctx->operation = EVP_PKEY_OP_SIGN;
341 ctx->update = update;
342 } else if (EVP_PKEY_sign_init(ctx->pctx) <= 0) {
343 return 0;
344 }
345 }
346 if (EVP_PKEY_CTX_set_signature_md(ctx->pctx, type) <= 0)
347 return 0;
348 if (pctx)
349 *pctx = ctx->pctx;
350 if (ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)
351 return 1;
352 if (!EVP_DigestInit_ex(ctx, type, e))
353 return 0;
354 /*
355 * This indicates the current algorithm requires
356 * special treatment before hashing the tbs-message.
357 */
358 ctx->pctx->flag_call_digest_custom = 0;
359 if (ctx->pctx->pmeth->digest_custom != NULL)
360 ctx->pctx->flag_call_digest_custom = 1;
361
362 ret = 1;
363
364 end:
365 #ifndef FIPS_MODULE
366 if (ret > 0)
367 ret = evp_pkey_ctx_use_cached_data(locpctx);
368 #endif
369
370 EVP_KEYMGMT_free(tmp_keymgmt);
371 return ret > 0 ? 1 : 0;
372 }
373
EVP_DigestSignInit_ex(EVP_MD_CTX * ctx,EVP_PKEY_CTX ** pctx,const char * mdname,OSSL_LIB_CTX * libctx,const char * props,EVP_PKEY * pkey,const OSSL_PARAM params[])374 int EVP_DigestSignInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
375 const char *mdname, OSSL_LIB_CTX *libctx,
376 const char *props, EVP_PKEY *pkey,
377 const OSSL_PARAM params[])
378 {
379 return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, NULL, pkey, 0,
380 params);
381 }
382
EVP_DigestSignInit(EVP_MD_CTX * ctx,EVP_PKEY_CTX ** pctx,const EVP_MD * type,ENGINE * e,EVP_PKEY * pkey)383 int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
384 const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
385 {
386 return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, e, pkey, 0,
387 NULL);
388 }
389
EVP_DigestVerifyInit_ex(EVP_MD_CTX * ctx,EVP_PKEY_CTX ** pctx,const char * mdname,OSSL_LIB_CTX * libctx,const char * props,EVP_PKEY * pkey,const OSSL_PARAM params[])390 int EVP_DigestVerifyInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
391 const char *mdname, OSSL_LIB_CTX *libctx,
392 const char *props, EVP_PKEY *pkey,
393 const OSSL_PARAM params[])
394 {
395 return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, NULL, pkey, 1,
396 params);
397 }
398
EVP_DigestVerifyInit(EVP_MD_CTX * ctx,EVP_PKEY_CTX ** pctx,const EVP_MD * type,ENGINE * e,EVP_PKEY * pkey)399 int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
400 const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
401 {
402 return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, e, pkey, 1,
403 NULL);
404 }
405 #endif /* FIPS_MDOE */
406
EVP_DigestSignUpdate(EVP_MD_CTX * ctx,const void * data,size_t dsize)407 int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
408 {
409 EVP_PKEY_CTX *pctx = ctx->pctx;
410
411 if (pctx == NULL
412 || pctx->operation != EVP_PKEY_OP_SIGNCTX
413 || pctx->op.sig.algctx == NULL
414 || pctx->op.sig.signature == NULL)
415 goto legacy;
416
417 if (pctx->op.sig.signature->digest_sign_update == NULL) {
418 ERR_raise(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
419 return 0;
420 }
421
422 return pctx->op.sig.signature->digest_sign_update(pctx->op.sig.algctx,
423 data, dsize);
424
425 legacy:
426 if (pctx != NULL) {
427 /* do_sigver_init() checked that |digest_custom| is non-NULL */
428 if (pctx->flag_call_digest_custom
429 && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
430 return 0;
431 pctx->flag_call_digest_custom = 0;
432 }
433
434 return EVP_DigestUpdate(ctx, data, dsize);
435 }
436
EVP_DigestVerifyUpdate(EVP_MD_CTX * ctx,const void * data,size_t dsize)437 int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
438 {
439 EVP_PKEY_CTX *pctx = ctx->pctx;
440
441 if (pctx == NULL
442 || pctx->operation != EVP_PKEY_OP_VERIFYCTX
443 || pctx->op.sig.algctx == NULL
444 || pctx->op.sig.signature == NULL)
445 goto legacy;
446
447 if (pctx->op.sig.signature->digest_verify_update == NULL) {
448 ERR_raise(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
449 return 0;
450 }
451
452 return pctx->op.sig.signature->digest_verify_update(pctx->op.sig.algctx,
453 data, dsize);
454
455 legacy:
456 if (pctx != NULL) {
457 /* do_sigver_init() checked that |digest_custom| is non-NULL */
458 if (pctx->flag_call_digest_custom
459 && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
460 return 0;
461 pctx->flag_call_digest_custom = 0;
462 }
463
464 return EVP_DigestUpdate(ctx, data, dsize);
465 }
466
467 #ifndef FIPS_MODULE
EVP_DigestSignFinal(EVP_MD_CTX * ctx,unsigned char * sigret,size_t * siglen)468 int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
469 size_t *siglen)
470 {
471 int sctx = 0, r = 0;
472 EVP_PKEY_CTX *dctx, *pctx = ctx->pctx;
473
474 if (pctx == NULL
475 || pctx->operation != EVP_PKEY_OP_SIGNCTX
476 || pctx->op.sig.algctx == NULL
477 || pctx->op.sig.signature == NULL)
478 goto legacy;
479
480 if (sigret == NULL || (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) != 0)
481 return pctx->op.sig.signature->digest_sign_final(pctx->op.sig.algctx,
482 sigret, siglen,
483 sigret == NULL ? 0 : *siglen);
484 dctx = EVP_PKEY_CTX_dup(pctx);
485 if (dctx == NULL)
486 return 0;
487
488 r = dctx->op.sig.signature->digest_sign_final(dctx->op.sig.algctx,
489 sigret, siglen,
490 *siglen);
491 EVP_PKEY_CTX_free(dctx);
492 return r;
493
494 legacy:
495 if (pctx == NULL || pctx->pmeth == NULL) {
496 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
497 return 0;
498 }
499
500 /* do_sigver_init() checked that |digest_custom| is non-NULL */
501 if (pctx->flag_call_digest_custom
502 && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
503 return 0;
504 pctx->flag_call_digest_custom = 0;
505
506 if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) {
507 if (sigret == NULL)
508 return pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
509 if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE)
510 r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
511 else {
512 dctx = EVP_PKEY_CTX_dup(pctx);
513 if (dctx == NULL)
514 return 0;
515 r = dctx->pmeth->signctx(dctx, sigret, siglen, ctx);
516 EVP_PKEY_CTX_free(dctx);
517 }
518 return r;
519 }
520 if (pctx->pmeth->signctx != NULL)
521 sctx = 1;
522 else
523 sctx = 0;
524 if (sigret != NULL) {
525 unsigned char md[EVP_MAX_MD_SIZE];
526 unsigned int mdlen = 0;
527
528 if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
529 if (sctx)
530 r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
531 else
532 r = EVP_DigestFinal_ex(ctx, md, &mdlen);
533 } else {
534 EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
535
536 if (tmp_ctx == NULL)
537 return 0;
538 if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
539 EVP_MD_CTX_free(tmp_ctx);
540 return 0;
541 }
542 if (sctx)
543 r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx,
544 sigret, siglen, tmp_ctx);
545 else
546 r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
547 EVP_MD_CTX_free(tmp_ctx);
548 }
549 if (sctx || !r)
550 return r;
551 if (EVP_PKEY_sign(pctx, sigret, siglen, md, mdlen) <= 0)
552 return 0;
553 } else {
554 if (sctx) {
555 if (pctx->pmeth->signctx(pctx, sigret, siglen, ctx) <= 0)
556 return 0;
557 } else {
558 int s = EVP_MD_get_size(ctx->digest);
559
560 if (s < 0 || EVP_PKEY_sign(pctx, sigret, siglen, NULL, s) <= 0)
561 return 0;
562 }
563 }
564 return 1;
565 }
566
EVP_DigestSign(EVP_MD_CTX * ctx,unsigned char * sigret,size_t * siglen,const unsigned char * tbs,size_t tbslen)567 int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen,
568 const unsigned char *tbs, size_t tbslen)
569 {
570 EVP_PKEY_CTX *pctx = ctx->pctx;
571
572 if (pctx != NULL
573 && pctx->operation == EVP_PKEY_OP_SIGNCTX
574 && pctx->op.sig.algctx != NULL
575 && pctx->op.sig.signature != NULL) {
576 if (pctx->op.sig.signature->digest_sign != NULL)
577 return pctx->op.sig.signature->digest_sign(pctx->op.sig.algctx,
578 sigret, siglen,
579 sigret == NULL ? 0 : *siglen,
580 tbs, tbslen);
581 } else {
582 /* legacy */
583 if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestsign != NULL)
584 return ctx->pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen);
585 }
586
587 if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0)
588 return 0;
589 return EVP_DigestSignFinal(ctx, sigret, siglen);
590 }
591
EVP_DigestVerifyFinal(EVP_MD_CTX * ctx,const unsigned char * sig,size_t siglen)592 int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,
593 size_t siglen)
594 {
595 unsigned char md[EVP_MAX_MD_SIZE];
596 int r = 0;
597 unsigned int mdlen = 0;
598 int vctx = 0;
599 EVP_PKEY_CTX *dctx, *pctx = ctx->pctx;
600
601 if (pctx == NULL
602 || pctx->operation != EVP_PKEY_OP_VERIFYCTX
603 || pctx->op.sig.algctx == NULL
604 || pctx->op.sig.signature == NULL)
605 goto legacy;
606
607 if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISE) != 0)
608 return pctx->op.sig.signature->digest_verify_final(pctx->op.sig.algctx,
609 sig, siglen);
610 dctx = EVP_PKEY_CTX_dup(pctx);
611 if (dctx == NULL)
612 return 0;
613
614 r = dctx->op.sig.signature->digest_verify_final(dctx->op.sig.algctx,
615 sig, siglen);
616 EVP_PKEY_CTX_free(dctx);
617 return r;
618
619 legacy:
620 if (pctx == NULL || pctx->pmeth == NULL) {
621 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
622 return 0;
623 }
624
625 /* do_sigver_init() checked that |digest_custom| is non-NULL */
626 if (pctx->flag_call_digest_custom
627 && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
628 return 0;
629 pctx->flag_call_digest_custom = 0;
630
631 if (pctx->pmeth->verifyctx != NULL)
632 vctx = 1;
633 else
634 vctx = 0;
635 if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
636 if (vctx)
637 r = pctx->pmeth->verifyctx(pctx, sig, siglen, ctx);
638 else
639 r = EVP_DigestFinal_ex(ctx, md, &mdlen);
640 } else {
641 EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
642 if (tmp_ctx == NULL)
643 return -1;
644 if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
645 EVP_MD_CTX_free(tmp_ctx);
646 return -1;
647 }
648 if (vctx)
649 r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx,
650 sig, siglen, tmp_ctx);
651 else
652 r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
653 EVP_MD_CTX_free(tmp_ctx);
654 }
655 if (vctx || !r)
656 return r;
657 return EVP_PKEY_verify(pctx, sig, siglen, md, mdlen);
658 }
659
EVP_DigestVerify(EVP_MD_CTX * ctx,const unsigned char * sigret,size_t siglen,const unsigned char * tbs,size_t tbslen)660 int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret,
661 size_t siglen, const unsigned char *tbs, size_t tbslen)
662 {
663 EVP_PKEY_CTX *pctx = ctx->pctx;
664
665 if (pctx != NULL
666 && pctx->operation == EVP_PKEY_OP_VERIFYCTX
667 && pctx->op.sig.algctx != NULL
668 && pctx->op.sig.signature != NULL) {
669 if (pctx->op.sig.signature->digest_verify != NULL)
670 return pctx->op.sig.signature->digest_verify(pctx->op.sig.algctx,
671 sigret, siglen,
672 tbs, tbslen);
673 } else {
674 /* legacy */
675 if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestverify != NULL)
676 return ctx->pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen);
677 }
678
679 if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0)
680 return -1;
681 return EVP_DigestVerifyFinal(ctx, sigret, siglen);
682 }
683 #endif /* FIPS_MODULE */
684