Coverage Report

Created: 2025-06-13 06:58

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