Coverage Report

Created: 2025-12-31 06:58

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