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