Coverage Report

Created: 2025-06-13 06:58

/src/openssl31/crypto/evp/m_sigver.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2006-2022 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stdio.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/evp.h>
13
#include <openssl/objects.h>
14
#include "crypto/evp.h"
15
#include "internal/provider.h"
16
#include "internal/numbers.h"   /* includes SIZE_MAX */
17
#include "evp_local.h"
18
19
#ifndef FIPS_MODULE
20
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
7.61k
{
33
7.61k
    if (mdname != NULL && strcmp(mdname, "UNDEF") == 0)
34
340
        return NULL;
35
36
7.27k
    return mdname;
37
7.61k
}
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
32.9k
{
45
32.9k
    EVP_PKEY_CTX *locpctx = NULL;
46
32.9k
    EVP_SIGNATURE *signature = NULL;
47
32.9k
    EVP_KEYMGMT *tmp_keymgmt = NULL;
48
32.9k
    const OSSL_PROVIDER *tmp_prov = NULL;
49
32.9k
    const char *supported_sig = NULL;
50
32.9k
    char locmdname[80] = "";     /* 80 chars should be enough */
51
32.9k
    void *provkey = NULL;
52
32.9k
    int ret, iter, reinit = 1;
53
54
32.9k
    if (!evp_md_ctx_free_algctx(ctx))
55
0
        return 0;
56
57
32.9k
    if (ctx->pctx == NULL) {
58
29.2k
        reinit = 0;
59
29.2k
        if (e == NULL)
60
29.2k
            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
29.2k
    }
64
32.9k
    if (ctx->pctx == NULL)
65
0
        return 0;
66
67
32.9k
    locpctx = ctx->pctx;
68
32.9k
    ERR_set_mark();
69
70
32.9k
    if (evp_pkey_ctx_is_legacy(locpctx))
71
0
        goto legacy;
72
73
    /* do not reinitialize if pkey is set or operation is different */
74
32.9k
    if (reinit
75
32.9k
        && (pkey != NULL
76
3.70k
            || locpctx->operation != (ver ? EVP_PKEY_OP_VERIFYCTX
77
0
                                          : EVP_PKEY_OP_SIGNCTX)
78
3.70k
            || (signature = locpctx->op.sig.signature) == NULL
79
3.70k
            || locpctx->op.sig.algctx == NULL))
80
3.70k
        reinit = 0;
81
82
32.9k
    if (props == NULL)
83
32.9k
        props = locpctx->propquery;
84
85
32.9k
    if (locpctx->pkey == NULL) {
86
0
        ERR_clear_last_mark();
87
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
88
0
        goto err;
89
0
    }
90
91
32.9k
    if (!reinit) {
92
32.9k
        evp_pkey_ctx_free_old_ops(locpctx);
93
32.9k
    } else {
94
0
        if (mdname == NULL && type == NULL)
95
0
            mdname = canon_mdname(EVP_MD_get0_name(ctx->reqdigest));
96
0
        goto reinitialize;
97
0
    }
98
99
    /*
100
     * Try to derive the supported signature from |locpctx->keymgmt|.
101
     */
102
32.9k
    if (!ossl_assert(locpctx->pkey->keymgmt == NULL
103
32.9k
                     || locpctx->pkey->keymgmt == locpctx->keymgmt)) {
104
0
        ERR_clear_last_mark();
105
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
106
0
        goto err;
107
0
    }
108
32.9k
    supported_sig = evp_keymgmt_util_query_operation_name(locpctx->keymgmt,
109
32.9k
                                                          OSSL_OP_SIGNATURE);
110
32.9k
    if (supported_sig == NULL) {
111
0
        ERR_clear_last_mark();
112
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
113
0
        goto err;
114
0
    }
115
116
    /*
117
     * We perform two iterations:
118
     *
119
     * 1.  Do the normal signature fetch, using the fetching data given by
120
     *     the EVP_PKEY_CTX.
121
     * 2.  Do the provider specific signature fetch, from the same provider
122
     *     as |ctx->keymgmt|
123
     *
124
     * We then try to fetch the keymgmt from the same provider as the
125
     * signature, and try to export |ctx->pkey| to that keymgmt (when
126
     * this keymgmt happens to be the same as |ctx->keymgmt|, the export
127
     * is a no-op, but we call it anyway to not complicate the code even
128
     * more).
129
     * If the export call succeeds (returns a non-NULL provider key pointer),
130
     * we're done and can perform the operation itself.  If not, we perform
131
     * the second iteration, or jump to legacy.
132
     */
133
65.9k
    for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) {
134
32.9k
        EVP_KEYMGMT *tmp_keymgmt_tofree = NULL;
135
136
        /*
137
         * If we're on the second iteration, free the results from the first.
138
         * They are NULL on the first iteration, so no need to check what
139
         * iteration we're on.
140
         */
141
32.9k
        EVP_SIGNATURE_free(signature);
142
32.9k
        EVP_KEYMGMT_free(tmp_keymgmt);
143
144
32.9k
        switch (iter) {
145
32.9k
        case 1:
146
32.9k
            signature = EVP_SIGNATURE_fetch(locpctx->libctx, supported_sig,
147
32.9k
                                            locpctx->propquery);
148
32.9k
            if (signature != NULL)
149
32.9k
                tmp_prov = EVP_SIGNATURE_get0_provider(signature);
150
32.9k
            break;
151
0
        case 2:
152
0
            tmp_prov = EVP_KEYMGMT_get0_provider(locpctx->keymgmt);
153
0
            signature =
154
0
                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
32.9k
        }
160
32.9k
        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
32.9k
        tmp_keymgmt_tofree = tmp_keymgmt =
174
32.9k
            evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
175
32.9k
                                        EVP_KEYMGMT_get0_name(locpctx->keymgmt),
176
32.9k
                                        locpctx->propquery);
177
32.9k
        if (tmp_keymgmt != NULL)
178
32.9k
            provkey = evp_pkey_export_to_provider(locpctx->pkey, locpctx->libctx,
179
32.9k
                                                  &tmp_keymgmt, locpctx->propquery);
180
32.9k
        if (tmp_keymgmt == NULL)
181
0
            EVP_KEYMGMT_free(tmp_keymgmt_tofree);
182
32.9k
    }
183
184
32.9k
    if (provkey == NULL) {
185
0
        EVP_SIGNATURE_free(signature);
186
0
        ERR_clear_last_mark();
187
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
188
0
        goto err;
189
0
    }
190
191
32.9k
    ERR_pop_to_mark();
192
193
    /* No more legacy from here down to legacy: */
194
195
32.9k
    locpctx->op.sig.signature = signature;
196
32.9k
    locpctx->operation = ver ? EVP_PKEY_OP_VERIFYCTX
197
32.9k
                             : EVP_PKEY_OP_SIGNCTX;
198
32.9k
    locpctx->op.sig.algctx
199
32.9k
        = signature->newctx(ossl_provider_ctx(signature->prov), props);
200
32.9k
    if (locpctx->op.sig.algctx == NULL) {
201
0
        ERR_raise(ERR_LIB_EVP,  EVP_R_INITIALIZATION_ERROR);
202
0
        goto err;
203
0
    }
204
205
32.9k
 reinitialize:
206
32.9k
    if (pctx != NULL)
207
9.46k
        *pctx = locpctx;
208
209
32.9k
    if (type != NULL) {
210
3.47k
        ctx->reqdigest = type;
211
3.47k
        if (mdname == NULL)
212
3.47k
            mdname = canon_mdname(EVP_MD_get0_name(type));
213
29.5k
    } else {
214
29.5k
        if (mdname == NULL && !reinit) {
215
229
            if (evp_keymgmt_util_get_deflt_digest_name(tmp_keymgmt, provkey,
216
229
                                                       locmdname,
217
229
                                                       sizeof(locmdname)) > 0) {
218
229
                mdname = canon_mdname(locmdname);
219
229
            }
220
229
        }
221
222
29.5k
        if (mdname != NULL) {
223
            /*
224
             * We're about to get a new digest so clear anything associated with
225
             * an old digest.
226
             */
227
29.3k
            evp_md_ctx_clear_digest(ctx, 1, 0);
228
229
            /* legacy code support for engines */
230
29.3k
            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
29.3k
            ctx->fetched_digest = EVP_MD_fetch(locpctx->libctx, mdname, props);
239
29.3k
            if (ctx->fetched_digest != NULL) {
240
29.3k
                ctx->digest = ctx->reqdigest = ctx->fetched_digest;
241
29.3k
            } 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
29.3k
            (void)ERR_pop_to_mark();
251
29.3k
        }
252
29.5k
    }
253
254
32.9k
    if (ver) {
255
8.70k
        if (signature->digest_verify_init == NULL) {
256
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
257
0
            goto err;
258
0
        }
259
8.70k
        ret = signature->digest_verify_init(locpctx->op.sig.algctx,
260
8.70k
                                            mdname, provkey, params);
261
24.2k
    } else {
262
24.2k
        if (signature->digest_sign_init == NULL) {
263
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
264
0
            goto err;
265
0
        }
266
24.2k
        ret = signature->digest_sign_init(locpctx->op.sig.algctx,
267
24.2k
                                          mdname, provkey, params);
268
24.2k
    }
269
270
    /*
271
     * If the operation was not a success and no digest was found, an error
272
     * needs to be raised.
273
     */
274
32.9k
    if (ret > 0 || mdname != NULL)
275
32.9k
        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
32.9k
 end:
358
32.9k
#ifndef FIPS_MODULE
359
32.9k
    if (ret > 0)
360
32.9k
        ret = evp_pkey_ctx_use_cached_data(locpctx);
361
32.9k
#endif
362
363
32.9k
    EVP_KEYMGMT_free(tmp_keymgmt);
364
32.9k
    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
53.1k
{
372
53.1k
    return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, NULL, pkey, 0,
373
53.1k
                          params);
374
53.1k
}
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
9.76k
{
388
9.76k
    return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, NULL, pkey, 1,
389
9.76k
                          params);
390
9.76k
}
391
392
int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
393
                         const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
394
7.47k
{
395
7.47k
    return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, e, pkey, 1,
396
7.47k
                          NULL);
397
7.47k
}
398
#endif /* FIPS_MDOE */
399
400
int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
401
5.83k
{
402
5.83k
    EVP_PKEY_CTX *pctx = ctx->pctx;
403
404
5.83k
    if (pctx == NULL
405
5.83k
            || pctx->operation != EVP_PKEY_OP_SIGNCTX
406
5.83k
            || pctx->op.sig.algctx == NULL
407
5.83k
            || pctx->op.sig.signature == NULL)
408
0
        goto legacy;
409
410
5.83k
    if (pctx->op.sig.signature->digest_sign_update == NULL) {
411
0
        ERR_raise(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
412
0
        return 0;
413
0
    }
414
415
5.83k
    return pctx->op.sig.signature->digest_sign_update(pctx->op.sig.algctx,
416
5.83k
                                                      data, dsize);
417
418
0
 legacy:
419
0
    if (pctx != NULL) {
420
        /* do_sigver_init() checked that |digest_custom| is non-NULL */
421
0
        if (pctx->flag_call_digest_custom
422
0
            && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
423
0
            return 0;
424
0
        pctx->flag_call_digest_custom = 0;
425
0
    }
426
427
0
    return EVP_DigestUpdate(ctx, data, dsize);
428
0
}
429
430
int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
431
4.99k
{
432
4.99k
    EVP_PKEY_CTX *pctx = ctx->pctx;
433
434
4.99k
    if (pctx == NULL
435
4.99k
            || pctx->operation != EVP_PKEY_OP_VERIFYCTX
436
4.99k
            || pctx->op.sig.algctx == NULL
437
4.99k
            || pctx->op.sig.signature == NULL)
438
0
        goto legacy;
439
440
4.99k
    if (pctx->op.sig.signature->digest_verify_update == NULL) {
441
0
        ERR_raise(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
442
0
        return 0;
443
0
    }
444
445
4.99k
    return pctx->op.sig.signature->digest_verify_update(pctx->op.sig.algctx,
446
4.99k
                                                        data, dsize);
447
448
0
 legacy:
449
0
    if (pctx != NULL) {
450
        /* do_sigver_init() checked that |digest_custom| is non-NULL */
451
0
        if (pctx->flag_call_digest_custom
452
0
            && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
453
0
            return 0;
454
0
        pctx->flag_call_digest_custom = 0;
455
0
    }
456
457
0
    return EVP_DigestUpdate(ctx, data, dsize);
458
0
}
459
460
#ifndef FIPS_MODULE
461
int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
462
                        size_t *siglen)
463
6.54k
{
464
6.54k
    int sctx = 0, r = 0;
465
6.54k
    EVP_PKEY_CTX *dctx, *pctx = ctx->pctx;
466
467
6.54k
    if (pctx == NULL
468
6.54k
            || pctx->operation != EVP_PKEY_OP_SIGNCTX
469
6.54k
            || pctx->op.sig.algctx == NULL
470
6.54k
            || pctx->op.sig.signature == NULL)
471
0
        goto legacy;
472
473
6.54k
    if (sigret == NULL || (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) != 0)
474
2.41k
        return pctx->op.sig.signature->digest_sign_final(pctx->op.sig.algctx,
475
2.41k
                                                         sigret, siglen,
476
2.41k
                                                         sigret == NULL ? 0 : *siglen);
477
4.13k
    dctx = EVP_PKEY_CTX_dup(pctx);
478
4.13k
    if (dctx == NULL)
479
0
        return 0;
480
481
4.13k
    r = dctx->op.sig.signature->digest_sign_final(dctx->op.sig.algctx,
482
4.13k
                                                  sigret, siglen,
483
4.13k
                                                  *siglen);
484
4.13k
    EVP_PKEY_CTX_free(dctx);
485
4.13k
    return r;
486
487
0
 legacy:
488
0
    if (pctx == NULL || pctx->pmeth == NULL) {
489
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
490
0
        return 0;
491
0
    }
492
493
    /* do_sigver_init() checked that |digest_custom| is non-NULL */
494
0
    if (pctx->flag_call_digest_custom
495
0
        && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
496
0
        return 0;
497
0
    pctx->flag_call_digest_custom = 0;
498
499
0
    if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) {
500
0
        if (sigret == NULL)
501
0
            return pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
502
0
        if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE)
503
0
            r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
504
0
        else {
505
0
            dctx = EVP_PKEY_CTX_dup(pctx);
506
0
            if (dctx == NULL)
507
0
                return 0;
508
0
            r = dctx->pmeth->signctx(dctx, sigret, siglen, ctx);
509
0
            EVP_PKEY_CTX_free(dctx);
510
0
        }
511
0
        return r;
512
0
    }
513
0
    if (pctx->pmeth->signctx != NULL)
514
0
        sctx = 1;
515
0
    else
516
0
        sctx = 0;
517
0
    if (sigret != NULL) {
518
0
        unsigned char md[EVP_MAX_MD_SIZE];
519
0
        unsigned int mdlen = 0;
520
521
0
        if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
522
0
            if (sctx)
523
0
                r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
524
0
            else
525
0
                r = EVP_DigestFinal_ex(ctx, md, &mdlen);
526
0
        } else {
527
0
            EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
528
529
0
            if (tmp_ctx == NULL)
530
0
                return 0;
531
0
            if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
532
0
                EVP_MD_CTX_free(tmp_ctx);
533
0
                return 0;
534
0
            }
535
0
            if (sctx)
536
0
                r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx,
537
0
                                                  sigret, siglen, tmp_ctx);
538
0
            else
539
0
                r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
540
0
            EVP_MD_CTX_free(tmp_ctx);
541
0
        }
542
0
        if (sctx || !r)
543
0
            return r;
544
0
        if (EVP_PKEY_sign(pctx, sigret, siglen, md, mdlen) <= 0)
545
0
            return 0;
546
0
    } else {
547
0
        if (sctx) {
548
0
            if (pctx->pmeth->signctx(pctx, sigret, siglen, ctx) <= 0)
549
0
                return 0;
550
0
        } else {
551
0
            int s = EVP_MD_get_size(ctx->digest);
552
553
0
            if (s < 0 || EVP_PKEY_sign(pctx, sigret, siglen, NULL, s) <= 0)
554
0
                return 0;
555
0
        }
556
0
    }
557
0
    return 1;
558
0
}
559
560
int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen,
561
                   const unsigned char *tbs, size_t tbslen)
562
4.82k
{
563
4.82k
    EVP_PKEY_CTX *pctx = ctx->pctx;
564
565
4.82k
    if (pctx != NULL
566
4.82k
            && pctx->operation == EVP_PKEY_OP_SIGNCTX
567
4.82k
            && pctx->op.sig.algctx != NULL
568
4.82k
            && pctx->op.sig.signature != NULL) {
569
4.82k
        if (pctx->op.sig.signature->digest_sign != NULL)
570
0
            return pctx->op.sig.signature->digest_sign(pctx->op.sig.algctx,
571
0
                                                       sigret, siglen,
572
0
                                                       sigret == NULL ? 0 : *siglen,
573
0
                                                       tbs, tbslen);
574
4.82k
    } else {
575
        /* legacy */
576
0
        if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestsign != NULL)
577
0
            return ctx->pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen);
578
0
    }
579
580
4.82k
    if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0)
581
0
        return 0;
582
4.82k
    return EVP_DigestSignFinal(ctx, sigret, siglen);
583
4.82k
}
584
585
int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,
586
                          size_t siglen)
587
4.99k
{
588
4.99k
    unsigned char md[EVP_MAX_MD_SIZE];
589
4.99k
    int r = 0;
590
4.99k
    unsigned int mdlen = 0;
591
4.99k
    int vctx = 0;
592
4.99k
    EVP_PKEY_CTX *dctx, *pctx = ctx->pctx;
593
594
4.99k
    if (pctx == NULL
595
4.99k
            || pctx->operation != EVP_PKEY_OP_VERIFYCTX
596
4.99k
            || pctx->op.sig.algctx == NULL
597
4.99k
            || pctx->op.sig.signature == NULL)
598
0
        goto legacy;
599
600
4.99k
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISE) != 0)
601
0
        return pctx->op.sig.signature->digest_verify_final(pctx->op.sig.algctx,
602
0
                                                           sig, siglen);
603
4.99k
    dctx = EVP_PKEY_CTX_dup(pctx);
604
4.99k
    if (dctx == NULL)
605
0
        return 0;
606
607
4.99k
    r = dctx->op.sig.signature->digest_verify_final(dctx->op.sig.algctx,
608
4.99k
                                                    sig, siglen);
609
4.99k
    EVP_PKEY_CTX_free(dctx);
610
4.99k
    return r;
611
612
0
 legacy:
613
0
    if (pctx == NULL || pctx->pmeth == NULL) {
614
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
615
0
        return 0;
616
0
    }
617
618
    /* do_sigver_init() checked that |digest_custom| is non-NULL */
619
0
    if (pctx->flag_call_digest_custom
620
0
        && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
621
0
        return 0;
622
0
    pctx->flag_call_digest_custom = 0;
623
624
0
    if (pctx->pmeth->verifyctx != NULL)
625
0
        vctx = 1;
626
0
    else
627
0
        vctx = 0;
628
0
    if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
629
0
        if (vctx)
630
0
            r = pctx->pmeth->verifyctx(pctx, sig, siglen, ctx);
631
0
        else
632
0
            r = EVP_DigestFinal_ex(ctx, md, &mdlen);
633
0
    } else {
634
0
        EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
635
0
        if (tmp_ctx == NULL)
636
0
            return -1;
637
0
        if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
638
0
            EVP_MD_CTX_free(tmp_ctx);
639
0
            return -1;
640
0
        }
641
0
        if (vctx)
642
0
            r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx,
643
0
                                                sig, siglen, tmp_ctx);
644
0
        else
645
0
            r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
646
0
        EVP_MD_CTX_free(tmp_ctx);
647
0
    }
648
0
    if (vctx || !r)
649
0
        return r;
650
0
    return EVP_PKEY_verify(pctx, sig, siglen, md, mdlen);
651
0
}
652
653
int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret,
654
                     size_t siglen, const unsigned char *tbs, size_t tbslen)
655
5.12k
{
656
5.12k
    EVP_PKEY_CTX *pctx = ctx->pctx;
657
658
5.12k
    if (pctx == NULL) {
659
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
660
0
        return -1;
661
0
    }
662
663
5.12k
    if (pctx->operation == EVP_PKEY_OP_VERIFYCTX
664
5.12k
            && pctx->op.sig.algctx != NULL
665
5.12k
            && pctx->op.sig.signature != NULL) {
666
5.12k
        if (pctx->op.sig.signature->digest_verify != NULL)
667
124
            return pctx->op.sig.signature->digest_verify(pctx->op.sig.algctx,
668
124
                                                         sigret, siglen,
669
124
                                                         tbs, tbslen);
670
5.12k
    } else {
671
        /* legacy */
672
0
        if (pctx->pmeth != NULL && pctx->pmeth->digestverify != NULL)
673
0
            return pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen);
674
0
    }
675
676
4.99k
    if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0)
677
0
        return -1;
678
4.99k
    return EVP_DigestVerifyFinal(ctx, sigret, siglen);
679
4.99k
}
680
#endif /* FIPS_MODULE */