Coverage Report

Created: 2025-12-08 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/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 "internal/common.h"
18
#include "evp_local.h"
19
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
26
/*
27
 * If we get the "NULL" md then the name comes back as "UNDEF". We want to use
28
 * NULL for this.
29
 */
30
static const char *canon_mdname(const char *mdname)
31
0
{
32
0
    if (mdname != NULL && strcmp(mdname, "UNDEF") == 0)
33
0
        return NULL;
34
35
0
    return mdname;
36
0
}
37
38
static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
39
                          const EVP_MD *type, const char *mdname,
40
                          OSSL_LIB_CTX *libctx, const char *props,
41
                          EVP_PKEY *pkey, int ver,
42
                          const OSSL_PARAM params[])
43
0
{
44
0
    EVP_PKEY_CTX *locpctx = NULL;
45
0
    EVP_SIGNATURE *signature = NULL;
46
0
    const char *desc;
47
0
    EVP_KEYMGMT *tmp_keymgmt = NULL;
48
0
    const OSSL_PROVIDER *tmp_prov = NULL;
49
0
    const char *supported_sig = NULL;
50
0
    char locmdname[80] = "";     /* 80 chars should be enough */
51
0
    void *provkey = NULL;
52
0
    int ret, iter, reinit = 1;
53
54
0
    if (!evp_md_ctx_free_algctx(ctx))
55
0
        return 0;
56
57
0
    if (ctx->pctx == NULL) {
58
0
        reinit = 0;
59
0
        ctx->pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, props);
60
0
    }
61
0
    if (ctx->pctx == NULL)
62
0
        return 0;
63
64
0
    EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_FINALISED);
65
66
0
    locpctx = ctx->pctx;
67
0
    ERR_set_mark();
68
69
0
    if (evp_pkey_ctx_is_legacy(locpctx))
70
0
        goto legacy;
71
72
    /* do not reinitialize if pkey is set or operation is different */
73
0
    if (reinit
74
0
        && (pkey != NULL
75
0
            || locpctx->operation != (ver ? EVP_PKEY_OP_VERIFYCTX
76
0
                                          : EVP_PKEY_OP_SIGNCTX)
77
0
            || (signature = locpctx->op.sig.signature) == NULL
78
0
            || locpctx->op.sig.algctx == NULL))
79
0
        reinit = 0;
80
81
0
    if (props == NULL)
82
0
        props = locpctx->propquery;
83
84
0
    if (locpctx->pkey == NULL) {
85
0
        ERR_clear_last_mark();
86
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
87
0
        goto err;
88
0
    }
89
90
0
    if (!reinit) {
91
0
        evp_pkey_ctx_free_old_ops(locpctx);
92
0
    } else {
93
0
        if (mdname == NULL && type == NULL)
94
0
            mdname = canon_mdname(EVP_MD_get0_name(ctx->reqdigest));
95
0
        goto reinitialize;
96
0
    }
97
98
    /*
99
     * Try to derive the supported signature from |locpctx->keymgmt|.
100
     */
101
0
    if (!ossl_assert(locpctx->pkey->keymgmt == NULL
102
0
                     || locpctx->pkey->keymgmt == locpctx->keymgmt)) {
103
0
        ERR_clear_last_mark();
104
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
105
0
        goto err;
106
0
    }
107
0
    supported_sig = evp_keymgmt_util_query_operation_name(locpctx->keymgmt,
108
0
                                                          OSSL_OP_SIGNATURE);
109
0
    if (supported_sig == NULL) {
110
0
        ERR_clear_last_mark();
111
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
112
0
        goto err;
113
0
    }
114
115
    /*
116
     * We perform two iterations:
117
     *
118
     * 1.  Do the normal signature fetch, using the fetching data given by
119
     *     the EVP_PKEY_CTX.
120
     * 2.  Do the provider specific signature fetch, from the same provider
121
     *     as |ctx->keymgmt|
122
     *
123
     * We then try to fetch the keymgmt from the same provider as the
124
     * signature, and try to export |ctx->pkey| to that keymgmt (when
125
     * this keymgmt happens to be the same as |ctx->keymgmt|, the export
126
     * is a no-op, but we call it anyway to not complicate the code even
127
     * more).
128
     * If the export call succeeds (returns a non-NULL provider key pointer),
129
     * we're done and can perform the operation itself.  If not, we perform
130
     * the second iteration, or jump to legacy.
131
     */
132
0
    for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) {
133
0
        EVP_KEYMGMT *tmp_keymgmt_tofree = NULL;
134
135
        /*
136
         * If we're on the second iteration, free the results from the first.
137
         * They are NULL on the first iteration, so no need to check what
138
         * iteration we're on.
139
         */
140
0
        EVP_SIGNATURE_free(signature);
141
0
        EVP_KEYMGMT_free(tmp_keymgmt);
142
143
0
        switch (iter) {
144
0
        case 1:
145
0
            signature = EVP_SIGNATURE_fetch(locpctx->libctx, supported_sig,
146
0
                                            locpctx->propquery);
147
0
            if (signature != NULL)
148
0
                tmp_prov = EVP_SIGNATURE_get0_provider(signature);
149
0
            break;
150
0
        case 2:
151
0
            tmp_prov = EVP_KEYMGMT_get0_provider(locpctx->keymgmt);
152
0
            signature =
153
0
                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
0
        }
159
0
        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
0
        tmp_keymgmt_tofree = tmp_keymgmt =
173
0
            evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
174
0
                                        EVP_KEYMGMT_get0_name(locpctx->keymgmt),
175
0
                                        locpctx->propquery);
176
0
        if (tmp_keymgmt != NULL)
177
0
            provkey = evp_pkey_export_to_provider(locpctx->pkey, locpctx->libctx,
178
0
                                                  &tmp_keymgmt, locpctx->propquery);
179
0
        if (tmp_keymgmt == NULL)
180
0
            EVP_KEYMGMT_free(tmp_keymgmt_tofree);
181
0
    }
182
183
0
    if (provkey == NULL) {
184
0
        EVP_SIGNATURE_free(signature);
185
0
        ERR_clear_last_mark();
186
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
187
0
        goto err;
188
0
    }
189
190
0
    ERR_pop_to_mark();
191
192
    /* No more legacy from here down to legacy: */
193
194
0
    locpctx->op.sig.signature = signature;
195
0
    locpctx->operation = ver ? EVP_PKEY_OP_VERIFYCTX
196
0
                             : EVP_PKEY_OP_SIGNCTX;
197
0
    locpctx->op.sig.algctx
198
0
        = signature->newctx(ossl_provider_ctx(signature->prov), props);
199
0
    if (locpctx->op.sig.algctx == NULL) {
200
0
        ERR_raise(ERR_LIB_EVP,  EVP_R_INITIALIZATION_ERROR);
201
0
        goto err;
202
0
    }
203
204
0
 reinitialize:
205
0
    if (pctx != NULL)
206
0
        *pctx = locpctx;
207
208
0
    if (type != NULL) {
209
0
        ctx->reqdigest = type;
210
0
        if (mdname == NULL)
211
0
            mdname = canon_mdname(EVP_MD_get0_name(type));
212
0
    } else {
213
0
        if (mdname == NULL && !reinit) {
214
0
            if (evp_keymgmt_util_get_deflt_digest_name(tmp_keymgmt, provkey,
215
0
                                                       locmdname,
216
0
                                                       sizeof(locmdname)) > 0) {
217
0
                mdname = canon_mdname(locmdname);
218
0
            }
219
0
        }
220
221
0
        if (mdname != NULL) {
222
            /*
223
             * We're about to get a new digest so clear anything associated with
224
             * an old digest.
225
             */
226
0
            evp_md_ctx_clear_digest(ctx, 1, 0);
227
228
            /* legacy code support for engines */
229
0
            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
0
            ctx->fetched_digest = EVP_MD_fetch(locpctx->libctx, mdname, props);
238
0
            if (ctx->fetched_digest != NULL) {
239
0
                ctx->digest = ctx->reqdigest = ctx->fetched_digest;
240
0
            } 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
0
            (void)ERR_pop_to_mark();
250
0
        }
251
0
    }
252
253
0
    desc = signature->description != NULL ? signature->description : "";
254
0
    if (ver) {
255
0
        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
0
        ret = signature->digest_verify_init(locpctx->op.sig.algctx,
261
0
                                            mdname, provkey, params);
262
0
    } else {
263
0
        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
0
        ret = signature->digest_sign_init(locpctx->op.sig.algctx,
269
0
                                          mdname, provkey, params);
270
0
    }
271
272
    /*
273
     * If the operation was not a success and no digest was found, an error
274
     * needs to be raised.
275
     */
276
0
    if (ret > 0 || mdname != NULL)
277
0
        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_data(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE,
304
0
                       ver ? "%s digest_verify_init" : "%s digest_sign_init",
305
0
                       EVP_PKEY_get0_type_name(locpctx->pkey));
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, NULL))
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
0
 end:
364
0
    if (ret > 0)
365
0
        ret = evp_pkey_ctx_use_cached_data(locpctx);
366
367
0
    EVP_KEYMGMT_free(tmp_keymgmt);
368
0
    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
0
{
376
0
    return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, pkey, 0,
377
0
                          params);
378
0
}
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
    if (!ossl_assert(e == NULL))
384
0
        return 0;
385
0
    return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, pkey, 0,
386
0
                          NULL);
387
0
}
388
389
int EVP_DigestVerifyInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
390
                            const char *mdname, OSSL_LIB_CTX *libctx,
391
                            const char *props, EVP_PKEY *pkey,
392
                            const OSSL_PARAM params[])
393
0
{
394
0
    return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, pkey, 1,
395
0
                          params);
396
0
}
397
398
int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
399
                         const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
400
0
{
401
0
    if (!ossl_assert(e == NULL))
402
0
        return 0;
403
0
    return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, pkey, 1,
404
0
                          NULL);
405
0
}
406
407
int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
408
0
{
409
0
    EVP_SIGNATURE *signature;
410
0
    const char *desc;
411
0
    EVP_PKEY_CTX *pctx = ctx->pctx;
412
0
    int ret;
413
414
0
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) {
415
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
416
0
        return 0;
417
0
    }
418
419
0
    if (pctx == NULL
420
0
            || pctx->operation != EVP_PKEY_OP_SIGNCTX
421
0
            || pctx->op.sig.algctx == NULL
422
0
            || pctx->op.sig.signature == NULL)
423
0
        goto legacy;
424
425
0
    signature = pctx->op.sig.signature;
426
0
    desc = signature->description != NULL ? signature->description : "";
427
0
    if (signature->digest_sign_update == NULL) {
428
0
        ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED,
429
0
                       "%s digest_sign_update:%s", signature->type_name, desc);
430
0
        return 0;
431
0
    }
432
433
0
    ERR_set_mark();
434
0
    ret = signature->digest_sign_update(pctx->op.sig.algctx, data, dsize);
435
0
    if (ret <= 0 && ERR_count_to_mark() == 0)
436
0
        ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE,
437
0
                       "%s digest_sign_update:%s", signature->type_name, desc);
438
0
    ERR_clear_last_mark();
439
0
    return ret;
440
441
0
 legacy:
442
0
    if (pctx != NULL) {
443
0
        if (pctx->pmeth == NULL) {
444
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
445
0
            return 0;
446
0
        }
447
        /* do_sigver_init() checked that |digest_custom| is non-NULL */
448
0
        if (pctx->flag_call_digest_custom
449
0
            && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
450
0
            return 0;
451
0
        pctx->flag_call_digest_custom = 0;
452
0
    }
453
454
0
    return EVP_DigestUpdate(ctx, data, dsize);
455
0
}
456
457
int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
458
0
{
459
0
    EVP_SIGNATURE *signature;
460
0
    const char *desc;
461
0
    EVP_PKEY_CTX *pctx = ctx->pctx;
462
0
    int ret;
463
464
0
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) {
465
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
466
0
        return 0;
467
0
    }
468
469
0
    if (pctx == NULL
470
0
            || pctx->operation != EVP_PKEY_OP_VERIFYCTX
471
0
            || pctx->op.sig.algctx == NULL
472
0
            || pctx->op.sig.signature == NULL)
473
0
        goto legacy;
474
475
0
    signature = pctx->op.sig.signature;
476
0
    desc = signature->description != NULL ? signature->description : "";
477
0
    if (signature->digest_verify_update == NULL) {
478
0
        ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED,
479
0
                       "%s digest_verify_update:%s", signature->type_name, desc);
480
0
        return 0;
481
0
    }
482
483
0
    ERR_set_mark();
484
0
    ret = signature->digest_verify_update(pctx->op.sig.algctx, data, dsize);
485
0
    if (ret <= 0 && ERR_count_to_mark() == 0)
486
0
        ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE,
487
0
                       "%s digest_verify_update:%s", signature->type_name, desc);
488
0
    ERR_clear_last_mark();
489
0
    return ret;
490
491
0
 legacy:
492
0
    if (pctx != NULL) {
493
        /* do_sigver_init() checked that |digest_custom| is non-NULL */
494
0
        if (pctx->flag_call_digest_custom
495
0
            && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
496
0
            return 0;
497
0
        pctx->flag_call_digest_custom = 0;
498
0
    }
499
500
0
    return EVP_DigestUpdate(ctx, data, dsize);
501
0
}
502
503
int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
504
                        size_t *siglen)
505
0
{
506
0
    EVP_SIGNATURE *signature;
507
0
    const char *desc;
508
0
    int sctx = 0;
509
0
    int r = 0;
510
0
    EVP_PKEY_CTX *dctx = NULL, *pctx = ctx->pctx;
511
512
0
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) {
513
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
514
0
        return 0;
515
0
    }
516
517
0
    if (pctx == NULL
518
0
            || pctx->operation != EVP_PKEY_OP_SIGNCTX
519
0
            || pctx->op.sig.algctx == NULL
520
0
            || pctx->op.sig.signature == NULL)
521
0
        goto legacy;
522
523
0
    signature = pctx->op.sig.signature;
524
0
    desc = signature->description != NULL ? signature->description : "";
525
0
    if (signature->digest_sign_final == NULL) {
526
0
        ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED,
527
0
                       "%s digest_sign_final:%s", signature->type_name, desc);
528
0
        return 0;
529
0
    }
530
531
0
    if (sigret != NULL && (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) == 0) {
532
        /* try dup */
533
0
        dctx = EVP_PKEY_CTX_dup(pctx);
534
0
        if (dctx != NULL)
535
0
            pctx = dctx;
536
0
    }
537
538
0
    ERR_set_mark();
539
0
    r = signature->digest_sign_final(pctx->op.sig.algctx, sigret, siglen,
540
0
                                     sigret == NULL ? 0 : *siglen);
541
0
    if (!r && ERR_count_to_mark() == 0)
542
0
        ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE,
543
0
                       "%s digest_sign_final:%s", signature->type_name, desc);
544
0
    ERR_clear_last_mark();
545
0
    if (dctx == NULL && sigret != NULL)
546
0
        ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
547
0
    else
548
0
        EVP_PKEY_CTX_free(dctx);
549
0
    return r;
550
551
0
 legacy:
552
0
    if (pctx == NULL || pctx->pmeth == NULL) {
553
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
554
0
        return 0;
555
0
    }
556
557
    /* do_sigver_init() checked that |digest_custom| is non-NULL */
558
0
    if (pctx->flag_call_digest_custom
559
0
        && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
560
0
        return 0;
561
0
    pctx->flag_call_digest_custom = 0;
562
563
0
    if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) {
564
0
        if (sigret == NULL)
565
0
            return pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
566
0
        if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISE) != 0) {
567
0
            r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
568
0
            ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
569
0
        } else {
570
0
            dctx = EVP_PKEY_CTX_dup(pctx);
571
0
            if (dctx == NULL)
572
0
                return 0;
573
0
            r = dctx->pmeth->signctx(dctx, sigret, siglen, ctx);
574
0
            EVP_PKEY_CTX_free(dctx);
575
0
        }
576
0
        return r;
577
0
    }
578
0
    if (pctx->pmeth->signctx != NULL)
579
0
        sctx = 1;
580
0
    else
581
0
        sctx = 0;
582
0
    if (sigret != NULL) {
583
0
        unsigned char md[EVP_MAX_MD_SIZE];
584
0
        unsigned int mdlen = 0;
585
586
0
        if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
587
0
            if (sctx)
588
0
                r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
589
0
            else
590
0
                r = EVP_DigestFinal_ex(ctx, md, &mdlen);
591
0
        } else {
592
0
            EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
593
594
0
            if (tmp_ctx == NULL)
595
0
                return 0;
596
0
            if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
597
0
                EVP_MD_CTX_free(tmp_ctx);
598
0
                return 0;
599
0
            }
600
0
            if (sctx)
601
0
                r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx,
602
0
                                                  sigret, siglen, tmp_ctx);
603
0
            else
604
0
                r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
605
0
            EVP_MD_CTX_free(tmp_ctx);
606
0
        }
607
0
        if (sctx || !r)
608
0
            return r;
609
0
        if (EVP_PKEY_sign(pctx, sigret, siglen, md, mdlen) <= 0)
610
0
            return 0;
611
0
    } else {
612
0
        if (sctx) {
613
0
            if (pctx->pmeth->signctx(pctx, sigret, siglen, ctx) <= 0)
614
0
                return 0;
615
0
        } else {
616
0
            int s = EVP_MD_get_size(ctx->digest);
617
618
0
            if (s <= 0 || EVP_PKEY_sign(pctx, sigret, siglen, NULL, s) <= 0)
619
0
                return 0;
620
0
        }
621
0
    }
622
0
    return 1;
623
0
}
624
625
int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen,
626
                   const unsigned char *tbs, size_t tbslen)
627
0
{
628
0
    EVP_PKEY_CTX *pctx = ctx->pctx;
629
0
    int ret;
630
631
0
    if (pctx == NULL) {
632
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
633
0
        return 0;
634
0
    }
635
636
0
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) {
637
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
638
0
        return 0;
639
0
    }
640
641
0
    if (pctx->operation == EVP_PKEY_OP_SIGNCTX
642
0
            && pctx->op.sig.algctx != NULL
643
0
            && pctx->op.sig.signature != NULL) {
644
0
        EVP_SIGNATURE *signature = pctx->op.sig.signature;
645
646
0
        if (signature->digest_sign != NULL) {
647
0
            const char *desc = signature->description != NULL ? signature->description : "";
648
649
0
            if (sigret != NULL)
650
0
                ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
651
0
            ERR_set_mark();
652
0
            ret = signature->digest_sign(pctx->op.sig.algctx, sigret, siglen,
653
0
                                         sigret == NULL ? 0 : *siglen, tbs, tbslen);
654
0
            if (ret <= 0 && ERR_count_to_mark() == 0)
655
0
                ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE,
656
0
                               "%s digest_sign:%s", signature->type_name, desc);
657
0
            ERR_clear_last_mark();
658
0
            return ret;
659
0
        }
660
0
    } else {
661
        /* legacy */
662
0
        if (pctx->pmeth != NULL && pctx->pmeth->digestsign != NULL)
663
0
            return pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen);
664
0
    }
665
666
0
    if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0)
667
0
        return 0;
668
0
    return EVP_DigestSignFinal(ctx, sigret, siglen);
669
0
}
670
671
int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,
672
                          size_t siglen)
673
0
{
674
0
    EVP_SIGNATURE *signature;
675
0
    const char *desc;
676
0
    int vctx = 0;
677
0
    unsigned int mdlen = 0;
678
0
    unsigned char md[EVP_MAX_MD_SIZE];
679
0
    int r = 0;
680
0
    EVP_PKEY_CTX *dctx = NULL, *pctx = ctx->pctx;
681
682
0
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) {
683
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
684
0
        return 0;
685
0
    }
686
687
0
    if (pctx == NULL
688
0
            || pctx->operation != EVP_PKEY_OP_VERIFYCTX
689
0
            || pctx->op.sig.algctx == NULL
690
0
            || pctx->op.sig.signature == NULL)
691
0
        goto legacy;
692
693
0
    signature = pctx->op.sig.signature;
694
0
    desc = signature->description != NULL ? signature->description : "";
695
0
    if (signature->digest_verify_final == NULL) {
696
0
        ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED,
697
0
                       "%s digest_verify_final:%s", signature->type_name, desc);
698
0
        return 0;
699
0
    }
700
701
0
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISE) == 0) {
702
        /* try dup */
703
0
        dctx = EVP_PKEY_CTX_dup(pctx);
704
0
        if (dctx != NULL)
705
0
            pctx = dctx;
706
0
    }
707
708
0
    ERR_set_mark();
709
0
    r = signature->digest_verify_final(pctx->op.sig.algctx, sig, siglen);
710
0
    if (!r && ERR_count_to_mark() == 0)
711
0
        ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE,
712
0
                       "%s digest_verify_final:%s", signature->type_name, desc);
713
0
    ERR_clear_last_mark();
714
0
    if (dctx == NULL)
715
0
        ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
716
0
    else
717
0
        EVP_PKEY_CTX_free(dctx);
718
0
    return r;
719
720
0
 legacy:
721
0
    if (pctx == NULL || pctx->pmeth == NULL) {
722
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
723
0
        return 0;
724
0
    }
725
726
    /* do_sigver_init() checked that |digest_custom| is non-NULL */
727
0
    if (pctx->flag_call_digest_custom
728
0
        && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
729
0
        return 0;
730
0
    pctx->flag_call_digest_custom = 0;
731
732
0
    if (pctx->pmeth->verifyctx != NULL)
733
0
        vctx = 1;
734
0
    else
735
0
        vctx = 0;
736
0
    if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
737
0
        if (vctx) {
738
0
            r = pctx->pmeth->verifyctx(pctx, sig, (int)siglen, ctx);
739
0
            ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
740
0
        } else
741
0
            r = EVP_DigestFinal_ex(ctx, md, &mdlen);
742
0
    } else {
743
0
        EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
744
0
        if (tmp_ctx == NULL)
745
0
            return -1;
746
0
        if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
747
0
            EVP_MD_CTX_free(tmp_ctx);
748
0
            return -1;
749
0
        }
750
0
        if (vctx)
751
0
            r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx,
752
0
                                                sig, (int)siglen, tmp_ctx);
753
0
        else
754
0
            r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
755
0
        EVP_MD_CTX_free(tmp_ctx);
756
0
    }
757
0
    if (vctx || !r)
758
0
        return r;
759
0
    return EVP_PKEY_verify(pctx, sig, siglen, md, mdlen);
760
0
}
761
762
int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret,
763
                     size_t siglen, const unsigned char *tbs, size_t tbslen)
764
0
{
765
0
    EVP_PKEY_CTX *pctx = ctx->pctx;
766
767
0
    if (pctx == NULL) {
768
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
769
0
        return -1;
770
0
    }
771
772
0
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) {
773
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
774
0
        return 0;
775
0
    }
776
777
0
    if (pctx->operation == EVP_PKEY_OP_VERIFYCTX
778
0
            && pctx->op.sig.algctx != NULL
779
0
            && pctx->op.sig.signature != NULL) {
780
0
        if (pctx->op.sig.signature->digest_verify != NULL) {
781
0
            EVP_SIGNATURE *signature = pctx->op.sig.signature;
782
0
            const char *desc = signature->description != NULL ? signature->description : "";
783
0
            int ret;
784
785
0
            ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
786
0
            ERR_set_mark();
787
0
            ret = signature->digest_verify(pctx->op.sig.algctx, sigret, siglen, tbs, tbslen);
788
0
            if (ret <= 0 && ERR_count_to_mark() == 0)
789
0
                ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE,
790
0
                               "%s digest_verify:%s", signature->type_name, desc);
791
0
            ERR_clear_last_mark();
792
0
            return ret;
793
0
        }
794
0
    } else {
795
        /* legacy */
796
0
        if (pctx->pmeth != NULL && pctx->pmeth->digestverify != NULL)
797
0
            return pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen);
798
0
    }
799
0
    if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0)
800
0
        return -1;
801
0
    return EVP_DigestVerifyFinal(ctx, sigret, siglen);
802
0
}