Coverage Report

Created: 2026-05-30 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/evp/m_sigver.c
Line
Count
Source
1
/*
2
 * Copyright 2006-2026 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 "internal/common.h"
18
#include "evp_local.h"
19
20
/*
21
 * If we get the "NULL" md then the name comes back as "UNDEF". We want to use
22
 * NULL for this.
23
 */
24
static const char *canon_mdname(const char *mdname)
25
0
{
26
0
    if (mdname != NULL && strcmp(mdname, "UNDEF") == 0)
27
0
        return NULL;
28
29
0
    return mdname;
30
0
}
31
32
static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
33
    const EVP_MD *type, const char *mdname,
34
    OSSL_LIB_CTX *libctx, const char *props,
35
    EVP_PKEY *pkey, int ver,
36
    const OSSL_PARAM params[])
37
0
{
38
0
    EVP_PKEY_CTX *locpctx = NULL;
39
0
    EVP_SIGNATURE *signature = NULL;
40
0
    const char *desc;
41
0
    EVP_KEYMGMT *tmp_keymgmt = NULL;
42
0
    const OSSL_PROVIDER *tmp_prov = NULL;
43
0
    const char *supported_sig = NULL;
44
0
    char locmdname[80] = ""; /* 80 chars should be enough */
45
0
    void *provkey = NULL;
46
0
    int ret, iter, reinit = 1;
47
48
0
    if (!evp_md_ctx_free_algctx(ctx))
49
0
        return 0;
50
51
0
    if (ctx->pctx == NULL) {
52
0
        reinit = 0;
53
0
        ctx->pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, props);
54
0
    }
55
0
    if (ctx->pctx == NULL)
56
0
        return 0;
57
58
0
    EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_FINALISED);
59
60
0
    locpctx = ctx->pctx;
61
0
    ERR_set_mark();
62
63
0
    if (evp_pkey_ctx_is_legacy(locpctx))
64
0
        goto notsupported;
65
66
    /* do not reinitialize if pkey is set or operation is different */
67
0
    if (reinit
68
0
        && (pkey != NULL
69
0
            || locpctx->operation != (ver ? EVP_PKEY_OP_VERIFYCTX : EVP_PKEY_OP_SIGNCTX)
70
0
            || (signature = locpctx->op.sig.signature) == NULL
71
0
            || locpctx->op.sig.algctx == NULL))
72
0
        reinit = 0;
73
74
0
    if (props == NULL)
75
0
        props = locpctx->propquery;
76
77
0
    if (locpctx->pkey == NULL) {
78
0
        ERR_clear_last_mark();
79
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
80
0
        goto err;
81
0
    }
82
83
0
    if (!reinit) {
84
0
        evp_pkey_ctx_free_old_ops(locpctx);
85
0
    } else {
86
0
        if (mdname == NULL && type == NULL)
87
0
            mdname = canon_mdname(EVP_MD_get0_name(ctx->reqdigest));
88
0
        goto reinitialize;
89
0
    }
90
91
    /*
92
     * Try to derive the supported signature from |locpctx->keymgmt|.
93
     */
94
0
    if (!ossl_assert(locpctx->pkey->keymgmt == NULL
95
0
            || locpctx->pkey->keymgmt == locpctx->keymgmt)) {
96
0
        ERR_clear_last_mark();
97
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
98
0
        goto err;
99
0
    }
100
0
    supported_sig = evp_keymgmt_util_query_operation_name(locpctx->keymgmt,
101
0
        OSSL_OP_SIGNATURE);
102
0
    if (supported_sig == NULL) {
103
0
        ERR_clear_last_mark();
104
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
105
0
        goto err;
106
0
    }
107
108
    /*
109
     * We perform two iterations:
110
     *
111
     * 1.  Do the normal signature fetch, using the fetching data given by
112
     *     the EVP_PKEY_CTX.
113
     * 2.  Do the provider specific signature fetch, from the same provider
114
     *     as |ctx->keymgmt|
115
     *
116
     * We then try to fetch the keymgmt from the same provider as the
117
     * signature, and try to export |ctx->pkey| to that keymgmt (when
118
     * this keymgmt happens to be the same as |ctx->keymgmt|, the export
119
     * is a no-op, but we call it anyway to not complicate the code even
120
     * more).
121
     * If the export call succeeds (returns a non-NULL provider key pointer),
122
     * we're done and can perform the operation itself.  If not, we perform
123
     * the second iteration, or jump to legacy.
124
     */
125
0
    for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) {
126
0
        EVP_KEYMGMT *tmp_keymgmt_tofree = NULL;
127
128
        /*
129
         * If we're on the second iteration, free the results from the first.
130
         * They are NULL on the first iteration, so no need to check what
131
         * iteration we're on.
132
         */
133
0
        EVP_SIGNATURE_free(signature);
134
0
        signature = NULL;
135
0
        EVP_KEYMGMT_free(tmp_keymgmt);
136
0
        tmp_keymgmt = NULL;
137
138
0
        switch (iter) {
139
0
        case 1:
140
0
            signature = EVP_SIGNATURE_fetch(locpctx->libctx, supported_sig,
141
0
                locpctx->propquery);
142
0
            if (signature != NULL)
143
0
                tmp_prov = EVP_SIGNATURE_get0_provider(signature);
144
0
            break;
145
0
        case 2:
146
0
            tmp_prov = EVP_KEYMGMT_get0_provider(locpctx->keymgmt);
147
0
            signature = evp_signature_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
148
0
                supported_sig, locpctx->propquery);
149
0
            if (signature == NULL)
150
0
                goto notsupported;
151
0
            break;
152
0
        }
153
0
        if (signature == NULL)
154
0
            continue;
155
156
        /*
157
         * Ensure that the key is provided, either natively, or as a cached
158
         * export.  We start by fetching the keymgmt with the same name as
159
         * |locpctx->pkey|, but from the provider of the signature method, using
160
         * the same property query as when fetching the signature method.
161
         * With the keymgmt we found (if we did), we try to export |locpctx->pkey|
162
         * to it (evp_pkey_export_to_provider() is smart enough to only actually
163
164
         * export it if |tmp_keymgmt| is different from |locpctx->pkey|'s keymgmt)
165
         */
166
0
        tmp_keymgmt_tofree = tmp_keymgmt = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
167
0
            EVP_KEYMGMT_get0_name(locpctx->keymgmt),
168
0
            locpctx->propquery);
169
0
        if (tmp_keymgmt != NULL)
170
0
            provkey = evp_pkey_export_to_provider(locpctx->pkey, locpctx->libctx,
171
0
                &tmp_keymgmt, locpctx->propquery);
172
0
        if (tmp_keymgmt == NULL)
173
0
            EVP_KEYMGMT_free(tmp_keymgmt_tofree);
174
0
    }
175
176
0
    if (provkey == NULL) {
177
0
        EVP_SIGNATURE_free(signature);
178
0
        ERR_clear_last_mark();
179
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
180
0
        goto err;
181
0
    }
182
183
0
    ERR_pop_to_mark();
184
185
    /* No more legacy from here down to legacy: */
186
187
0
    locpctx->op.sig.signature = signature;
188
0
    locpctx->operation = ver ? EVP_PKEY_OP_VERIFYCTX
189
0
                             : EVP_PKEY_OP_SIGNCTX;
190
0
    locpctx->op.sig.algctx
191
0
        = signature->newctx(ossl_provider_ctx(signature->prov), props);
192
0
    if (locpctx->op.sig.algctx == NULL) {
193
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
194
0
        goto err;
195
0
    }
196
197
0
reinitialize:
198
0
    if (pctx != NULL)
199
0
        *pctx = locpctx;
200
201
0
    if (type != NULL) {
202
0
        ctx->reqdigest = type;
203
0
        if (mdname == NULL)
204
0
            mdname = canon_mdname(EVP_MD_get0_name(type));
205
0
    } else {
206
0
        if (mdname == NULL && !reinit) {
207
0
            if (evp_keymgmt_util_get_deflt_digest_name(tmp_keymgmt, provkey,
208
0
                    locmdname,
209
0
                    sizeof(locmdname))
210
0
                > 0) {
211
0
                mdname = canon_mdname(locmdname);
212
0
            }
213
0
        }
214
215
0
        if (mdname != NULL) {
216
            /*
217
             * We're about to get a new digest so clear anything associated with
218
             * an old digest.
219
             */
220
0
            evp_md_ctx_clear_digest(ctx, 1, 0);
221
222
            /*
223
             * This might be requested by a later call to EVP_MD_CTX_get0_md().
224
             * In that case the "explicit fetch" rules apply for that
225
             * function (as per man pages), i.e. the ref count is not updated
226
             * so the EVP_MD should not be used beyond the lifetime of the
227
             * EVP_MD_CTX.
228
             */
229
0
            ctx->fetched_digest = EVP_MD_fetch(locpctx->libctx, mdname, props);
230
0
            if (ctx->fetched_digest != NULL) {
231
0
                ctx->digest = ctx->reqdigest = ctx->fetched_digest;
232
0
                if (ctx->digest == NULL) {
233
0
                    ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
234
0
                    goto err;
235
0
                }
236
0
            }
237
0
        }
238
0
    }
239
240
0
    desc = signature->description != NULL ? signature->description : "";
241
0
    if (ver) {
242
0
        if (signature->digest_verify_init == NULL) {
243
0
            ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED,
244
0
                "%s digest_verify_init:%s", signature->type_name, desc);
245
0
            goto err;
246
0
        }
247
0
        ret = signature->digest_verify_init(locpctx->op.sig.algctx,
248
0
            mdname, provkey, params);
249
0
    } else {
250
0
        if (signature->digest_sign_init == NULL) {
251
0
            ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED,
252
0
                "%s digest_sign_init:%s", signature->type_name, desc);
253
0
            goto err;
254
0
        }
255
0
        ret = signature->digest_sign_init(locpctx->op.sig.algctx,
256
0
            mdname, provkey, params);
257
0
    }
258
259
    /*
260
     * If the operation was not a success and no digest was found, an error
261
     * needs to be raised.
262
     */
263
0
    if (ret > 0 || mdname != NULL) {
264
0
        if (ret > 0)
265
0
            ret = evp_pkey_ctx_use_cached_data(locpctx);
266
267
0
        EVP_KEYMGMT_free(tmp_keymgmt);
268
0
        return ret > 0 ? 1 : 0;
269
0
    }
270
0
    if (type == NULL) /* This check is redundant but clarifies matters */
271
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_DEFAULT_DIGEST);
272
0
    ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE,
273
0
        ver ? "%s digest_verify_init:%s" : "%s digest_sign_init:%s",
274
0
        signature->type_name, desc);
275
276
0
err:
277
0
    evp_pkey_ctx_free_old_ops(locpctx);
278
0
    locpctx->operation = EVP_PKEY_OP_UNDEFINED;
279
0
    EVP_KEYMGMT_free(tmp_keymgmt);
280
0
    return 0;
281
282
0
notsupported:
283
0
    ERR_pop_to_mark();
284
0
    EVP_KEYMGMT_free(tmp_keymgmt);
285
286
0
    ERR_raise_data(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE,
287
0
        ver ? "%s digest_verify_init" : "%s digest_sign_init",
288
0
        EVP_PKEY_get0_type_name(locpctx->pkey));
289
0
    return 0;
290
0
}
291
292
int EVP_DigestSignInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
293
    const char *mdname, OSSL_LIB_CTX *libctx,
294
    const char *props, EVP_PKEY *pkey,
295
    const OSSL_PARAM params[])
296
0
{
297
0
    return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, pkey, 0,
298
0
        params);
299
0
}
300
301
int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
302
    const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
303
0
{
304
0
    if (!ossl_assert(e == NULL))
305
0
        return 0;
306
0
    return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, pkey, 0,
307
0
        NULL);
308
0
}
309
310
int EVP_DigestVerifyInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
311
    const char *mdname, OSSL_LIB_CTX *libctx,
312
    const char *props, EVP_PKEY *pkey,
313
    const OSSL_PARAM params[])
314
0
{
315
0
    return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, pkey, 1,
316
0
        params);
317
0
}
318
319
int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
320
    const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
321
0
{
322
0
    if (!ossl_assert(e == NULL))
323
0
        return 0;
324
0
    return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, pkey, 1,
325
0
        NULL);
326
0
}
327
328
int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
329
0
{
330
0
    EVP_SIGNATURE *signature;
331
0
    const char *desc;
332
0
    EVP_PKEY_CTX *pctx = ctx->pctx;
333
0
    int ret;
334
335
0
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) {
336
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
337
0
        return 0;
338
0
    }
339
340
0
    if (pctx == NULL)
341
0
        return EVP_DigestUpdate(ctx, data, dsize);
342
343
0
    if (pctx->operation != EVP_PKEY_OP_SIGNCTX
344
0
        || pctx->op.sig.algctx == NULL
345
0
        || pctx->op.sig.signature == NULL) {
346
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
347
0
        return 0;
348
0
    }
349
350
0
    signature = pctx->op.sig.signature;
351
0
    desc = signature->description != NULL ? signature->description : "";
352
0
    if (signature->digest_sign_update == NULL) {
353
0
        ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED,
354
0
            "%s digest_sign_update:%s", signature->type_name, desc);
355
0
        return 0;
356
0
    }
357
358
0
    ERR_set_mark();
359
0
    ret = signature->digest_sign_update(pctx->op.sig.algctx, data, dsize);
360
0
    if (ret <= 0 && ERR_count_to_mark() == 0)
361
0
        ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE,
362
0
            "%s digest_sign_update:%s", signature->type_name, desc);
363
0
    ERR_clear_last_mark();
364
0
    return ret;
365
0
}
366
367
int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
368
0
{
369
0
    EVP_SIGNATURE *signature;
370
0
    const char *desc;
371
0
    EVP_PKEY_CTX *pctx = ctx->pctx;
372
0
    int ret;
373
374
0
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) {
375
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
376
0
        return 0;
377
0
    }
378
379
0
    if (pctx == NULL
380
0
        || pctx->operation != EVP_PKEY_OP_VERIFYCTX
381
0
        || pctx->op.sig.algctx == NULL
382
0
        || pctx->op.sig.signature == NULL)
383
0
        return EVP_DigestUpdate(ctx, data, dsize);
384
385
0
    signature = pctx->op.sig.signature;
386
0
    desc = signature->description != NULL ? signature->description : "";
387
0
    if (signature->digest_verify_update == NULL) {
388
0
        ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED,
389
0
            "%s digest_verify_update:%s", signature->type_name, desc);
390
0
        return 0;
391
0
    }
392
393
0
    ERR_set_mark();
394
0
    ret = signature->digest_verify_update(pctx->op.sig.algctx, data, dsize);
395
0
    if (ret <= 0 && ERR_count_to_mark() == 0)
396
0
        ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE,
397
0
            "%s digest_verify_update:%s", signature->type_name, desc);
398
0
    ERR_clear_last_mark();
399
0
    return ret;
400
0
}
401
402
int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
403
    size_t *siglen)
404
0
{
405
0
    EVP_SIGNATURE *signature;
406
0
    const char *desc;
407
0
    int r = 0;
408
0
    EVP_PKEY_CTX *dctx = NULL, *pctx = ctx->pctx;
409
410
0
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) {
411
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
412
0
        return 0;
413
0
    }
414
415
0
    if (pctx == NULL
416
0
        || pctx->operation != EVP_PKEY_OP_SIGNCTX
417
0
        || pctx->op.sig.algctx == NULL
418
0
        || pctx->op.sig.signature == NULL) {
419
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
420
0
        return 0;
421
0
    }
422
423
0
    signature = pctx->op.sig.signature;
424
0
    desc = signature->description != NULL ? signature->description : "";
425
0
    if (signature->digest_sign_final == NULL) {
426
0
        ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED,
427
0
            "%s digest_sign_final:%s", signature->type_name, desc);
428
0
        return 0;
429
0
    }
430
431
0
    if (sigret != NULL && (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) == 0) {
432
        /* try dup */
433
0
        dctx = EVP_PKEY_CTX_dup(pctx);
434
0
        if (dctx != NULL)
435
0
            pctx = dctx;
436
0
    }
437
438
0
    ERR_set_mark();
439
0
    r = signature->digest_sign_final(pctx->op.sig.algctx, sigret, siglen,
440
0
        sigret == NULL ? 0 : *siglen);
441
0
    if (!r && ERR_count_to_mark() == 0)
442
0
        ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE,
443
0
            "%s digest_sign_final:%s", signature->type_name, desc);
444
0
    ERR_clear_last_mark();
445
0
    if (dctx == NULL && sigret != NULL)
446
0
        ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
447
0
    else
448
0
        EVP_PKEY_CTX_free(dctx);
449
0
    return r;
450
0
}
451
452
int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen,
453
    const unsigned char *tbs, size_t tbslen)
454
0
{
455
0
    EVP_PKEY_CTX *pctx = ctx->pctx;
456
0
    int ret;
457
458
0
    if (pctx == NULL) {
459
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
460
0
        return 0;
461
0
    }
462
463
0
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) {
464
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
465
0
        return 0;
466
0
    }
467
468
0
    if (pctx->operation == EVP_PKEY_OP_SIGNCTX
469
0
        && pctx->op.sig.algctx != NULL
470
0
        && pctx->op.sig.signature != NULL) {
471
0
        EVP_SIGNATURE *signature = pctx->op.sig.signature;
472
473
0
        if (signature->digest_sign != NULL) {
474
0
            const char *desc = signature->description != NULL ? signature->description : "";
475
476
0
            if (sigret != NULL)
477
0
                ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
478
0
            ERR_set_mark();
479
0
            ret = signature->digest_sign(pctx->op.sig.algctx, sigret, siglen,
480
0
                sigret == NULL ? 0 : *siglen, tbs, tbslen);
481
0
            if (ret <= 0 && ERR_count_to_mark() == 0)
482
0
                ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE,
483
0
                    "%s digest_sign:%s", signature->type_name, desc);
484
0
            ERR_clear_last_mark();
485
0
            return ret;
486
0
        }
487
0
    }
488
489
0
    if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0)
490
0
        return 0;
491
0
    return EVP_DigestSignFinal(ctx, sigret, siglen);
492
0
}
493
494
int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,
495
    size_t siglen)
496
0
{
497
0
    EVP_SIGNATURE *signature;
498
0
    const char *desc;
499
0
    int r = 0;
500
0
    EVP_PKEY_CTX *dctx = NULL, *pctx = ctx->pctx;
501
502
0
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) {
503
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
504
0
        return 0;
505
0
    }
506
507
0
    if (pctx == NULL
508
0
        || pctx->operation != EVP_PKEY_OP_VERIFYCTX
509
0
        || pctx->op.sig.algctx == NULL
510
0
        || pctx->op.sig.signature == NULL) {
511
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
512
0
        return 0;
513
0
    }
514
515
0
    signature = pctx->op.sig.signature;
516
0
    desc = signature->description != NULL ? signature->description : "";
517
0
    if (signature->digest_verify_final == NULL) {
518
0
        ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED,
519
0
            "%s digest_verify_final:%s", signature->type_name, desc);
520
0
        return 0;
521
0
    }
522
523
0
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISE) == 0) {
524
        /* try dup */
525
0
        dctx = EVP_PKEY_CTX_dup(pctx);
526
0
        if (dctx != NULL)
527
0
            pctx = dctx;
528
0
    }
529
530
0
    ERR_set_mark();
531
0
    r = signature->digest_verify_final(pctx->op.sig.algctx, sig, siglen);
532
0
    if (!r && ERR_count_to_mark() == 0)
533
0
        ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE,
534
0
            "%s digest_verify_final:%s", signature->type_name, desc);
535
0
    ERR_clear_last_mark();
536
0
    if (dctx == NULL)
537
0
        ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
538
0
    else
539
0
        EVP_PKEY_CTX_free(dctx);
540
0
    return r;
541
0
}
542
543
int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret,
544
    size_t siglen, const unsigned char *tbs, size_t tbslen)
545
0
{
546
0
    EVP_PKEY_CTX *pctx = ctx->pctx;
547
548
0
    if (pctx == NULL) {
549
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
550
0
        return -1;
551
0
    }
552
553
0
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) {
554
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
555
0
        return 0;
556
0
    }
557
558
0
    if (pctx->operation == EVP_PKEY_OP_VERIFYCTX
559
0
        && pctx->op.sig.algctx != NULL
560
0
        && pctx->op.sig.signature != NULL) {
561
0
        if (pctx->op.sig.signature->digest_verify != NULL) {
562
0
            EVP_SIGNATURE *signature = pctx->op.sig.signature;
563
0
            const char *desc = signature->description != NULL ? signature->description : "";
564
0
            int ret;
565
566
0
            ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
567
0
            ERR_set_mark();
568
0
            ret = signature->digest_verify(pctx->op.sig.algctx, sigret, siglen, tbs, tbslen);
569
0
            if (ret <= 0 && ERR_count_to_mark() == 0)
570
0
                ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE,
571
0
                    "%s digest_verify:%s", signature->type_name, desc);
572
0
            ERR_clear_last_mark();
573
0
            return ret;
574
0
        }
575
0
    }
576
577
0
    if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0)
578
0
        return -1;
579
0
    return EVP_DigestVerifyFinal(ctx, sigret, siglen);
580
0
}