Coverage Report

Created: 2025-12-31 06:58

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