Coverage Report

Created: 2026-07-23 06:28

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