Coverage Report

Created: 2025-12-31 06:58

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-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
258k
{
403
258k
    EVP_SIGNATURE *signature;
404
258k
    const char *desc;
405
258k
    EVP_PKEY_CTX *pctx = ctx->pctx;
406
258k
    int ret;
407
408
258k
    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
258k
    if (pctx == NULL
414
258k
        || pctx->operation != EVP_PKEY_OP_SIGNCTX
415
258k
        || pctx->op.sig.algctx == NULL
416
258k
        || pctx->op.sig.signature == NULL)
417
0
        goto legacy;
418
419
258k
    signature = pctx->op.sig.signature;
420
258k
    desc = signature->description != NULL ? signature->description : "";
421
258k
    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
258k
    ERR_set_mark();
428
258k
    ret = signature->digest_sign_update(pctx->op.sig.algctx, data, dsize);
429
258k
    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
258k
    ERR_clear_last_mark();
433
258k
    return ret;
434
435
0
legacy:
436
0
    if (pctx != NULL) {
437
        /* do_sigver_init() checked that |digest_custom| is non-NULL */
438
0
        if (pctx->flag_call_digest_custom
439
0
            && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
440
0
            return 0;
441
0
        pctx->flag_call_digest_custom = 0;
442
0
    }
443
444
0
    return EVP_DigestUpdate(ctx, data, dsize);
445
0
}
446
447
int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
448
13.7k
{
449
13.7k
    EVP_SIGNATURE *signature;
450
13.7k
    const char *desc;
451
13.7k
    EVP_PKEY_CTX *pctx = ctx->pctx;
452
13.7k
    int ret;
453
454
13.7k
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) {
455
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
456
0
        return 0;
457
0
    }
458
459
13.7k
    if (pctx == NULL
460
13.7k
        || pctx->operation != EVP_PKEY_OP_VERIFYCTX
461
13.7k
        || pctx->op.sig.algctx == NULL
462
13.7k
        || pctx->op.sig.signature == NULL)
463
0
        goto legacy;
464
465
13.7k
    signature = pctx->op.sig.signature;
466
13.7k
    desc = signature->description != NULL ? signature->description : "";
467
13.7k
    if (signature->digest_verify_update == NULL) {
468
0
        ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED,
469
0
            "%s digest_verify_update:%s", signature->type_name, desc);
470
0
        return 0;
471
0
    }
472
473
13.7k
    ERR_set_mark();
474
13.7k
    ret = signature->digest_verify_update(pctx->op.sig.algctx, data, dsize);
475
13.7k
    if (ret <= 0 && ERR_count_to_mark() == 0)
476
0
        ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE,
477
0
            "%s digest_verify_update:%s", signature->type_name, desc);
478
13.7k
    ERR_clear_last_mark();
479
13.7k
    return ret;
480
481
0
legacy:
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
}
492
493
int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
494
    size_t *siglen)
495
212k
{
496
212k
    EVP_SIGNATURE *signature;
497
212k
    const char *desc;
498
212k
    int sctx = 0;
499
212k
    int r = 0;
500
212k
    EVP_PKEY_CTX *dctx = NULL, *pctx = ctx->pctx;
501
502
212k
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) {
503
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
504
0
        return 0;
505
0
    }
506
507
212k
    if (pctx == NULL
508
212k
        || pctx->operation != EVP_PKEY_OP_SIGNCTX
509
212k
        || pctx->op.sig.algctx == NULL
510
212k
        || pctx->op.sig.signature == NULL)
511
0
        goto legacy;
512
513
212k
    signature = pctx->op.sig.signature;
514
212k
    desc = signature->description != NULL ? signature->description : "";
515
212k
    if (signature->digest_sign_final == NULL) {
516
0
        ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED,
517
0
            "%s digest_sign_final:%s", signature->type_name, desc);
518
0
        return 0;
519
0
    }
520
521
212k
    if (sigret != NULL && (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) == 0) {
522
        /* try dup */
523
208k
        dctx = EVP_PKEY_CTX_dup(pctx);
524
208k
        if (dctx != NULL)
525
208k
            pctx = dctx;
526
208k
    }
527
528
212k
    ERR_set_mark();
529
212k
    r = signature->digest_sign_final(pctx->op.sig.algctx, sigret, siglen,
530
212k
        sigret == NULL ? 0 : *siglen);
531
212k
    if (!r && ERR_count_to_mark() == 0)
532
0
        ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE,
533
0
            "%s digest_sign_final:%s", signature->type_name, desc);
534
212k
    ERR_clear_last_mark();
535
212k
    if (dctx == NULL && sigret != NULL)
536
0
        ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
537
212k
    else
538
212k
        EVP_PKEY_CTX_free(dctx);
539
212k
    return r;
540
541
0
legacy:
542
0
    if (pctx == NULL || pctx->pmeth == NULL) {
543
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
544
0
        return 0;
545
0
    }
546
547
    /* do_sigver_init() checked that |digest_custom| is non-NULL */
548
0
    if (pctx->flag_call_digest_custom
549
0
        && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
550
0
        return 0;
551
0
    pctx->flag_call_digest_custom = 0;
552
553
0
    if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) {
554
0
        if (sigret == NULL)
555
0
            return pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
556
0
        if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISE) != 0) {
557
0
            r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
558
0
            ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
559
0
        } else {
560
0
            dctx = EVP_PKEY_CTX_dup(pctx);
561
0
            if (dctx == NULL)
562
0
                return 0;
563
0
            r = dctx->pmeth->signctx(dctx, sigret, siglen, ctx);
564
0
            EVP_PKEY_CTX_free(dctx);
565
0
        }
566
0
        return r;
567
0
    }
568
0
    if (pctx->pmeth->signctx != NULL)
569
0
        sctx = 1;
570
0
    else
571
0
        sctx = 0;
572
0
    if (sigret != NULL) {
573
0
        unsigned char md[EVP_MAX_MD_SIZE];
574
0
        unsigned int mdlen = 0;
575
576
0
        if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
577
0
            if (sctx)
578
0
                r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
579
0
            else
580
0
                r = EVP_DigestFinal_ex(ctx, md, &mdlen);
581
0
        } else {
582
0
            EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
583
584
0
            if (tmp_ctx == NULL)
585
0
                return 0;
586
0
            if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
587
0
                EVP_MD_CTX_free(tmp_ctx);
588
0
                return 0;
589
0
            }
590
0
            if (sctx)
591
0
                r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx,
592
0
                    sigret, siglen, tmp_ctx);
593
0
            else
594
0
                r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
595
0
            EVP_MD_CTX_free(tmp_ctx);
596
0
        }
597
0
        if (sctx || !r)
598
0
            return r;
599
0
        if (EVP_PKEY_sign(pctx, sigret, siglen, md, mdlen) <= 0)
600
0
            return 0;
601
0
    } else {
602
0
        if (sctx) {
603
0
            if (pctx->pmeth->signctx(pctx, sigret, siglen, ctx) <= 0)
604
0
                return 0;
605
0
        } else {
606
0
            int s = EVP_MD_get_size(ctx->digest);
607
608
0
            if (s <= 0 || EVP_PKEY_sign(pctx, sigret, siglen, NULL, s) <= 0)
609
0
                return 0;
610
0
        }
611
0
    }
612
0
    return 1;
613
0
}
614
615
int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen,
616
    const unsigned char *tbs, size_t tbslen)
617
8.37k
{
618
8.37k
    EVP_PKEY_CTX *pctx = ctx->pctx;
619
8.37k
    int ret;
620
621
8.37k
    if (pctx == NULL) {
622
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
623
0
        return 0;
624
0
    }
625
626
8.37k
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) {
627
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
628
0
        return 0;
629
0
    }
630
631
8.37k
    if (pctx->operation == EVP_PKEY_OP_SIGNCTX
632
8.37k
        && pctx->op.sig.algctx != NULL
633
8.37k
        && pctx->op.sig.signature != NULL) {
634
8.37k
        EVP_SIGNATURE *signature = pctx->op.sig.signature;
635
636
8.37k
        if (signature->digest_sign != NULL) {
637
318
            const char *desc = signature->description != NULL ? signature->description : "";
638
639
318
            if (sigret != NULL)
640
159
                ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
641
318
            ERR_set_mark();
642
318
            ret = signature->digest_sign(pctx->op.sig.algctx, sigret, siglen,
643
318
                sigret == NULL ? 0 : *siglen, tbs, tbslen);
644
318
            if (ret <= 0 && ERR_count_to_mark() == 0)
645
0
                ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE,
646
0
                    "%s digest_sign:%s", signature->type_name, desc);
647
318
            ERR_clear_last_mark();
648
318
            return ret;
649
318
        }
650
8.37k
    } else {
651
        /* legacy */
652
0
        if (pctx->pmeth != NULL && pctx->pmeth->digestsign != NULL)
653
0
            return pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen);
654
0
    }
655
656
8.05k
    if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0)
657
0
        return 0;
658
8.05k
    return EVP_DigestSignFinal(ctx, sigret, siglen);
659
8.05k
}
660
661
int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,
662
    size_t siglen)
663
13.7k
{
664
13.7k
    EVP_SIGNATURE *signature;
665
13.7k
    const char *desc;
666
13.7k
    int vctx = 0;
667
13.7k
    unsigned int mdlen = 0;
668
13.7k
    unsigned char md[EVP_MAX_MD_SIZE];
669
13.7k
    int r = 0;
670
13.7k
    EVP_PKEY_CTX *dctx = NULL, *pctx = ctx->pctx;
671
672
13.7k
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) {
673
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
674
0
        return 0;
675
0
    }
676
677
13.7k
    if (pctx == NULL
678
13.7k
        || pctx->operation != EVP_PKEY_OP_VERIFYCTX
679
13.7k
        || pctx->op.sig.algctx == NULL
680
13.7k
        || pctx->op.sig.signature == NULL)
681
0
        goto legacy;
682
683
13.7k
    signature = pctx->op.sig.signature;
684
13.7k
    desc = signature->description != NULL ? signature->description : "";
685
13.7k
    if (signature->digest_verify_final == NULL) {
686
0
        ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED,
687
0
            "%s digest_verify_final:%s", signature->type_name, desc);
688
0
        return 0;
689
0
    }
690
691
13.7k
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISE) == 0) {
692
        /* try dup */
693
13.7k
        dctx = EVP_PKEY_CTX_dup(pctx);
694
13.7k
        if (dctx != NULL)
695
13.7k
            pctx = dctx;
696
13.7k
    }
697
698
13.7k
    ERR_set_mark();
699
13.7k
    r = signature->digest_verify_final(pctx->op.sig.algctx, sig, siglen);
700
13.7k
    if (!r && ERR_count_to_mark() == 0)
701
2.17k
        ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE,
702
2.17k
            "%s digest_verify_final:%s", signature->type_name, desc);
703
13.7k
    ERR_clear_last_mark();
704
13.7k
    if (dctx == NULL)
705
0
        ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
706
13.7k
    else
707
13.7k
        EVP_PKEY_CTX_free(dctx);
708
13.7k
    return r;
709
710
0
legacy:
711
0
    if (pctx == NULL || pctx->pmeth == NULL) {
712
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
713
0
        return 0;
714
0
    }
715
716
    /* do_sigver_init() checked that |digest_custom| is non-NULL */
717
0
    if (pctx->flag_call_digest_custom
718
0
        && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
719
0
        return 0;
720
0
    pctx->flag_call_digest_custom = 0;
721
722
0
    if (pctx->pmeth->verifyctx != NULL)
723
0
        vctx = 1;
724
0
    else
725
0
        vctx = 0;
726
0
    if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
727
0
        if (vctx) {
728
0
            r = pctx->pmeth->verifyctx(pctx, sig, siglen, ctx);
729
0
            ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
730
0
        } else
731
0
            r = EVP_DigestFinal_ex(ctx, md, &mdlen);
732
0
    } else {
733
0
        EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
734
0
        if (tmp_ctx == NULL)
735
0
            return -1;
736
0
        if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
737
0
            EVP_MD_CTX_free(tmp_ctx);
738
0
            return -1;
739
0
        }
740
0
        if (vctx)
741
0
            r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx,
742
0
                sig, siglen, tmp_ctx);
743
0
        else
744
0
            r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
745
0
        EVP_MD_CTX_free(tmp_ctx);
746
0
    }
747
0
    if (vctx || !r)
748
0
        return r;
749
0
    return EVP_PKEY_verify(pctx, sig, siglen, md, mdlen);
750
0
}
751
752
int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret,
753
    size_t siglen, const unsigned char *tbs, size_t tbslen)
754
13.8k
{
755
13.8k
    EVP_PKEY_CTX *pctx = ctx->pctx;
756
757
13.8k
    if (pctx == NULL) {
758
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
759
0
        return -1;
760
0
    }
761
762
13.8k
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) {
763
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
764
0
        return 0;
765
0
    }
766
767
13.8k
    if (pctx->operation == EVP_PKEY_OP_VERIFYCTX
768
13.8k
        && pctx->op.sig.algctx != NULL
769
13.8k
        && pctx->op.sig.signature != NULL) {
770
13.8k
        if (pctx->op.sig.signature->digest_verify != NULL) {
771
159
            EVP_SIGNATURE *signature = pctx->op.sig.signature;
772
159
            const char *desc = signature->description != NULL ? signature->description : "";
773
159
            int ret;
774
775
159
            ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
776
159
            ERR_set_mark();
777
159
            ret = signature->digest_verify(pctx->op.sig.algctx, sigret, siglen, tbs, tbslen);
778
159
            if (ret <= 0 && ERR_count_to_mark() == 0)
779
0
                ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE,
780
0
                    "%s digest_verify:%s", signature->type_name, desc);
781
159
            ERR_clear_last_mark();
782
159
            return ret;
783
159
        }
784
13.8k
    } else {
785
        /* legacy */
786
0
        if (pctx->pmeth != NULL && pctx->pmeth->digestverify != NULL)
787
0
            return pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen);
788
0
    }
789
13.7k
    if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0)
790
0
        return -1;
791
13.7k
    return EVP_DigestVerifyFinal(ctx, sigret, siglen);
792
13.7k
}