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