Coverage Report

Created: 2025-12-31 06:58

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