Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/crypto/evp/signature.c
Line
Count
Source
1
/*
2
 * Copyright 2006-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <openssl/err.h>
11
#include <stdio.h>
12
#include <stdlib.h>
13
#include <openssl/core_names.h>
14
#include <openssl/objects.h>
15
#include <openssl/evp.h>
16
#include "internal/numbers.h" /* includes SIZE_MAX */
17
#include "internal/cryptlib.h"
18
#include "internal/provider.h"
19
#include "internal/core.h"
20
#include "crypto/evp.h"
21
#include "evp_local.h"
22
23
static EVP_SIGNATURE *evp_signature_new(OSSL_PROVIDER *prov)
24
466
{
25
466
    EVP_SIGNATURE *signature = OPENSSL_zalloc(sizeof(EVP_SIGNATURE));
26
27
466
    if (signature == NULL)
28
0
        return NULL;
29
30
466
    if (!CRYPTO_NEW_REF(&signature->refcnt, 1)) {
31
0
        OPENSSL_free(signature);
32
0
        return NULL;
33
0
    }
34
35
466
    signature->prov = prov;
36
466
    ossl_provider_up_ref(prov);
37
38
466
    return signature;
39
466
}
40
41
static void *evp_signature_from_algorithm(int name_id,
42
    const OSSL_ALGORITHM *algodef,
43
    OSSL_PROVIDER *prov)
44
396
{
45
396
    const OSSL_DISPATCH *fns = algodef->implementation;
46
396
    EVP_SIGNATURE *signature = NULL;
47
    /* Counts newctx / freectx */
48
396
    int ctxfncnt = 0;
49
    /* Counts all init functions  */
50
396
    int initfncnt = 0;
51
    /* Counts all parameter functions */
52
396
    int gparamfncnt = 0, sparamfncnt = 0, gmdparamfncnt = 0, smdparamfncnt = 0;
53
396
    int valid = 0;
54
55
396
    if ((signature = evp_signature_new(prov)) == NULL) {
56
0
        ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
57
0
        goto err;
58
0
    }
59
60
396
    signature->name_id = name_id;
61
396
    if ((signature->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
62
0
        goto err;
63
396
    signature->description = algodef->algorithm_description;
64
65
7.45k
    for (; fns->function_id != 0; fns++) {
66
7.05k
        switch (fns->function_id) {
67
396
        case OSSL_FUNC_SIGNATURE_NEWCTX:
68
396
            if (signature->newctx != NULL)
69
0
                break;
70
396
            signature->newctx = OSSL_FUNC_signature_newctx(fns);
71
396
            ctxfncnt++;
72
396
            break;
73
378
        case OSSL_FUNC_SIGNATURE_SIGN_INIT:
74
378
            if (signature->sign_init != NULL)
75
18
                break;
76
360
            signature->sign_init = OSSL_FUNC_signature_sign_init(fns);
77
360
            initfncnt++;
78
360
            break;
79
360
        case OSSL_FUNC_SIGNATURE_SIGN:
80
360
            if (signature->sign != NULL)
81
0
                break;
82
360
            signature->sign = OSSL_FUNC_signature_sign(fns);
83
360
            break;
84
324
        case OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_INIT:
85
324
            if (signature->sign_message_init != NULL)
86
0
                break;
87
324
            signature->sign_message_init
88
324
                = OSSL_FUNC_signature_sign_message_init(fns);
89
324
            initfncnt++;
90
324
            break;
91
279
        case OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_UPDATE:
92
279
            if (signature->sign_message_update != NULL)
93
0
                break;
94
279
            signature->sign_message_update
95
279
                = OSSL_FUNC_signature_sign_message_update(fns);
96
279
            break;
97
279
        case OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_FINAL:
98
279
            if (signature->sign_message_final != NULL)
99
0
                break;
100
279
            signature->sign_message_final
101
279
                = OSSL_FUNC_signature_sign_message_final(fns);
102
279
            break;
103
378
        case OSSL_FUNC_SIGNATURE_VERIFY_INIT:
104
378
            if (signature->verify_init != NULL)
105
18
                break;
106
360
            signature->verify_init = OSSL_FUNC_signature_verify_init(fns);
107
360
            initfncnt++;
108
360
            break;
109
360
        case OSSL_FUNC_SIGNATURE_VERIFY:
110
360
            if (signature->verify != NULL)
111
0
                break;
112
360
            signature->verify = OSSL_FUNC_signature_verify(fns);
113
360
            break;
114
324
        case OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_INIT:
115
324
            if (signature->verify_message_init != NULL)
116
0
                break;
117
324
            signature->verify_message_init
118
324
                = OSSL_FUNC_signature_verify_message_init(fns);
119
324
            initfncnt++;
120
324
            break;
121
279
        case OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_UPDATE:
122
279
            if (signature->verify_message_update != NULL)
123
0
                break;
124
279
            signature->verify_message_update
125
279
                = OSSL_FUNC_signature_verify_message_update(fns);
126
279
            break;
127
279
        case OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_FINAL:
128
279
            if (signature->verify_message_final != NULL)
129
0
                break;
130
279
            signature->verify_message_final
131
279
                = OSSL_FUNC_signature_verify_message_final(fns);
132
279
            break;
133
126
        case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT:
134
126
            if (signature->verify_recover_init != NULL)
135
0
                break;
136
126
            signature->verify_recover_init
137
126
                = OSSL_FUNC_signature_verify_recover_init(fns);
138
126
            initfncnt++;
139
126
            break;
140
126
        case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER:
141
126
            if (signature->verify_recover != NULL)
142
0
                break;
143
126
            signature->verify_recover
144
126
                = OSSL_FUNC_signature_verify_recover(fns);
145
126
            break;
146
90
        case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT:
147
90
            if (signature->digest_sign_init != NULL)
148
0
                break;
149
90
            signature->digest_sign_init
150
90
                = OSSL_FUNC_signature_digest_sign_init(fns);
151
90
            initfncnt++;
152
90
            break;
153
72
        case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE:
154
72
            if (signature->digest_sign_update != NULL)
155
0
                break;
156
72
            signature->digest_sign_update
157
72
                = OSSL_FUNC_signature_digest_sign_update(fns);
158
72
            break;
159
72
        case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL:
160
72
            if (signature->digest_sign_final != NULL)
161
0
                break;
162
72
            signature->digest_sign_final
163
72
                = OSSL_FUNC_signature_digest_sign_final(fns);
164
72
            break;
165
18
        case OSSL_FUNC_SIGNATURE_DIGEST_SIGN:
166
18
            if (signature->digest_sign != NULL)
167
0
                break;
168
18
            signature->digest_sign
169
18
                = OSSL_FUNC_signature_digest_sign(fns);
170
18
            break;
171
54
        case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT:
172
54
            if (signature->digest_verify_init != NULL)
173
0
                break;
174
54
            signature->digest_verify_init
175
54
                = OSSL_FUNC_signature_digest_verify_init(fns);
176
54
            initfncnt++;
177
54
            break;
178
36
        case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE:
179
36
            if (signature->digest_verify_update != NULL)
180
0
                break;
181
36
            signature->digest_verify_update
182
36
                = OSSL_FUNC_signature_digest_verify_update(fns);
183
36
            break;
184
36
        case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL:
185
36
            if (signature->digest_verify_final != NULL)
186
0
                break;
187
36
            signature->digest_verify_final
188
36
                = OSSL_FUNC_signature_digest_verify_final(fns);
189
36
            break;
190
18
        case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY:
191
18
            if (signature->digest_verify != NULL)
192
0
                break;
193
18
            signature->digest_verify
194
18
                = OSSL_FUNC_signature_digest_verify(fns);
195
18
            break;
196
396
        case OSSL_FUNC_SIGNATURE_FREECTX:
197
396
            if (signature->freectx != NULL)
198
0
                break;
199
396
            signature->freectx = OSSL_FUNC_signature_freectx(fns);
200
396
            ctxfncnt++;
201
396
            break;
202
396
        case OSSL_FUNC_SIGNATURE_DUPCTX:
203
396
            if (signature->dupctx != NULL)
204
0
                break;
205
396
            signature->dupctx = OSSL_FUNC_signature_dupctx(fns);
206
396
            break;
207
360
        case OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS:
208
360
            if (signature->get_ctx_params != NULL)
209
0
                break;
210
360
            signature->get_ctx_params
211
360
                = OSSL_FUNC_signature_get_ctx_params(fns);
212
360
            gparamfncnt++;
213
360
            break;
214
360
        case OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS:
215
360
            if (signature->gettable_ctx_params != NULL)
216
0
                break;
217
360
            signature->gettable_ctx_params
218
360
                = OSSL_FUNC_signature_gettable_ctx_params(fns);
219
360
            gparamfncnt++;
220
360
            break;
221
396
        case OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS:
222
396
            if (signature->set_ctx_params != NULL)
223
0
                break;
224
396
            signature->set_ctx_params
225
396
                = OSSL_FUNC_signature_set_ctx_params(fns);
226
396
            sparamfncnt++;
227
396
            break;
228
396
        case OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS:
229
396
            if (signature->settable_ctx_params != NULL)
230
0
                break;
231
396
            signature->settable_ctx_params
232
396
                = OSSL_FUNC_signature_settable_ctx_params(fns);
233
396
            sparamfncnt++;
234
396
            break;
235
36
        case OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS:
236
36
            if (signature->get_ctx_md_params != NULL)
237
0
                break;
238
36
            signature->get_ctx_md_params
239
36
                = OSSL_FUNC_signature_get_ctx_md_params(fns);
240
36
            gmdparamfncnt++;
241
36
            break;
242
36
        case OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS:
243
36
            if (signature->gettable_ctx_md_params != NULL)
244
0
                break;
245
36
            signature->gettable_ctx_md_params
246
36
                = OSSL_FUNC_signature_gettable_ctx_md_params(fns);
247
36
            gmdparamfncnt++;
248
36
            break;
249
36
        case OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS:
250
36
            if (signature->set_ctx_md_params != NULL)
251
0
                break;
252
36
            signature->set_ctx_md_params
253
36
                = OSSL_FUNC_signature_set_ctx_md_params(fns);
254
36
            smdparamfncnt++;
255
36
            break;
256
36
        case OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS:
257
36
            if (signature->settable_ctx_md_params != NULL)
258
0
                break;
259
36
            signature->settable_ctx_md_params
260
36
                = OSSL_FUNC_signature_settable_ctx_md_params(fns);
261
36
            smdparamfncnt++;
262
36
            break;
263
324
        case OSSL_FUNC_SIGNATURE_QUERY_KEY_TYPES:
264
324
            if (signature->query_key_types != NULL)
265
0
                break;
266
324
            signature->query_key_types
267
324
                = OSSL_FUNC_signature_query_key_types(fns);
268
324
            break;
269
7.05k
        }
270
7.05k
    }
271
    /*
272
     * In order to be a consistent set of functions we must have at least
273
     * a set of context functions (newctx and freectx) as well as a set of
274
     * "signature" functions.  Because there's an overlap between some sets
275
     * of functions, counters don't always cut it, we must test known
276
     * combinations.
277
     * We start by assuming the implementation is valid, and then look for
278
     * reasons it's not.
279
     */
280
396
    valid = 1;
281
    /* Start with the ones where counters say enough */
282
396
    if (ctxfncnt != 2)
283
        /* newctx or freectx missing */
284
0
        valid = 0;
285
396
    if (valid
286
396
        && ((gparamfncnt != 0 && gparamfncnt != 2)
287
396
            || (sparamfncnt != 0 && sparamfncnt != 2)
288
396
            || (gmdparamfncnt != 0 && gmdparamfncnt != 2)
289
396
            || (smdparamfncnt != 0 && smdparamfncnt != 2)))
290
        /*
291
         * Params functions are optional, but if defined, they must
292
         * be pairwise complete sets, i.e. a getter must have an
293
         * associated gettable, etc
294
         */
295
0
        valid = 0;
296
396
    if (valid && initfncnt == 0)
297
        /* No init functions */
298
0
        valid = 0;
299
300
    /* Now we check for function combinations */
301
396
    if (valid
302
396
        && ((signature->sign_init != NULL
303
360
                && signature->sign == NULL)
304
396
            || (signature->sign_message_init != NULL
305
324
                && signature->sign == NULL
306
0
                && (signature->sign_message_update == NULL
307
0
                    || signature->sign_message_final == NULL))))
308
        /* sign_init functions with no signing function?  That's weird */
309
0
        valid = 0;
310
396
    if (valid
311
396
        && (signature->sign != NULL
312
36
            || signature->sign_message_update != NULL
313
36
            || signature->sign_message_final != NULL)
314
360
        && signature->sign_init == NULL
315
0
        && signature->sign_message_init == NULL)
316
        /* signing functions with no sign_init? That's odd */
317
0
        valid = 0;
318
319
396
    if (valid
320
396
        && ((signature->verify_init != NULL
321
360
                && signature->verify == NULL)
322
396
            || (signature->verify_message_init != NULL
323
324
                && signature->verify == NULL
324
0
                && (signature->verify_message_update == NULL
325
0
                    || signature->verify_message_final == NULL))))
326
        /* verify_init functions with no verification function?  That's weird */
327
0
        valid = 0;
328
396
    if (valid
329
396
        && (signature->verify != NULL
330
36
            || signature->verify_message_update != NULL
331
36
            || signature->verify_message_final != NULL)
332
360
        && signature->verify_init == NULL
333
0
        && signature->verify_message_init == NULL)
334
        /* verification functions with no verify_init? That's odd */
335
0
        valid = 0;
336
337
396
    if (valid
338
396
        && (signature->verify_recover_init != NULL)
339
126
        && (signature->verify_recover == NULL))
340
        /* verify_recover_init functions with no verify_recover?  How quaint */
341
0
        valid = 0;
342
343
396
    if (valid
344
396
        && (signature->digest_sign_init != NULL
345
90
            && signature->digest_sign == NULL
346
72
            && (signature->digest_sign_update == NULL
347
72
                || signature->digest_sign_final == NULL)))
348
        /*
349
         * You can't have a digest_sign_init without *some* performing functions
350
         */
351
0
        valid = 0;
352
353
396
    if (valid
354
396
        && ((signature->digest_verify_init != NULL
355
54
            && signature->digest_verify == NULL
356
36
            && (signature->digest_verify_update == NULL
357
36
                || signature->digest_verify_final == NULL))))
358
        /*
359
         * You can't have a digest_verify_init without *some* performing functions
360
         */
361
0
        valid = 0;
362
363
396
    if (!valid) {
364
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
365
0
        goto err;
366
0
    }
367
368
396
    return signature;
369
0
err:
370
0
    EVP_SIGNATURE_free(signature);
371
0
    return NULL;
372
396
}
373
374
void EVP_SIGNATURE_free(EVP_SIGNATURE *signature)
375
1.70M
{
376
1.70M
    int i;
377
378
1.70M
    if (signature == NULL)
379
113k
        return;
380
1.59M
    CRYPTO_DOWN_REF(&signature->refcnt, &i);
381
1.59M
    if (i > 0)
382
1.59M
        return;
383
1.59k
    OPENSSL_free(signature->type_name);
384
1.59k
    ossl_provider_free(signature->prov);
385
1.59k
    CRYPTO_FREE_REF(&signature->refcnt);
386
1.59k
    OPENSSL_free(signature);
387
1.59k
}
388
389
int EVP_SIGNATURE_up_ref(EVP_SIGNATURE *signature)
390
1.59M
{
391
1.59M
    int ref = 0;
392
393
1.59M
    CRYPTO_UP_REF(&signature->refcnt, &ref);
394
1.59M
    return 1;
395
1.59M
}
396
397
OSSL_PROVIDER *EVP_SIGNATURE_get0_provider(const EVP_SIGNATURE *signature)
398
168k
{
399
168k
    return signature->prov;
400
168k
}
401
402
EVP_SIGNATURE *EVP_SIGNATURE_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
403
    const char *properties)
404
492k
{
405
492k
    return evp_generic_fetch(ctx, OSSL_OP_SIGNATURE, algorithm, properties,
406
492k
        evp_signature_from_algorithm,
407
492k
        (int (*)(void *))EVP_SIGNATURE_up_ref,
408
492k
        (void (*)(void *))EVP_SIGNATURE_free);
409
492k
}
410
411
EVP_SIGNATURE *evp_signature_fetch_from_prov(OSSL_PROVIDER *prov,
412
    const char *algorithm,
413
    const char *properties)
414
0
{
415
0
    return evp_generic_fetch_from_prov(prov, OSSL_OP_SIGNATURE,
416
0
        algorithm, properties,
417
0
        evp_signature_from_algorithm,
418
0
        (int (*)(void *))EVP_SIGNATURE_up_ref,
419
0
        (void (*)(void *))EVP_SIGNATURE_free);
420
0
}
421
422
int EVP_SIGNATURE_is_a(const EVP_SIGNATURE *signature, const char *name)
423
2.61k
{
424
2.61k
    return signature != NULL
425
2.61k
        && evp_is_a(signature->prov, signature->name_id, NULL, name);
426
2.61k
}
427
428
int evp_signature_get_number(const EVP_SIGNATURE *signature)
429
0
{
430
0
    return signature->name_id;
431
0
}
432
433
const char *EVP_SIGNATURE_get0_name(const EVP_SIGNATURE *signature)
434
0
{
435
0
    return signature->type_name;
436
0
}
437
438
const char *EVP_SIGNATURE_get0_description(const EVP_SIGNATURE *signature)
439
0
{
440
0
    return signature->description;
441
0
}
442
443
void EVP_SIGNATURE_do_all_provided(OSSL_LIB_CTX *libctx,
444
    void (*fn)(EVP_SIGNATURE *signature,
445
        void *arg),
446
    void *arg)
447
8
{
448
8
    evp_generic_do_all(libctx, OSSL_OP_SIGNATURE,
449
8
        (void (*)(void *, void *))fn, arg,
450
8
        evp_signature_from_algorithm,
451
8
        (int (*)(void *))EVP_SIGNATURE_up_ref,
452
8
        (void (*)(void *))EVP_SIGNATURE_free);
453
8
}
454
455
int EVP_SIGNATURE_names_do_all(const EVP_SIGNATURE *signature,
456
    void (*fn)(const char *name, void *data),
457
    void *data)
458
0
{
459
0
    if (signature->prov != NULL)
460
0
        return evp_names_do_all(signature->prov, signature->name_id, fn, data);
461
462
0
    return 1;
463
0
}
464
465
const OSSL_PARAM *EVP_SIGNATURE_gettable_ctx_params(const EVP_SIGNATURE *sig)
466
0
{
467
0
    void *provctx;
468
469
0
    if (sig == NULL || sig->gettable_ctx_params == NULL)
470
0
        return NULL;
471
472
0
    provctx = ossl_provider_ctx(EVP_SIGNATURE_get0_provider(sig));
473
0
    return sig->gettable_ctx_params(NULL, provctx);
474
0
}
475
476
const OSSL_PARAM *EVP_SIGNATURE_settable_ctx_params(const EVP_SIGNATURE *sig)
477
113
{
478
113
    void *provctx;
479
480
113
    if (sig == NULL || sig->settable_ctx_params == NULL)
481
0
        return NULL;
482
483
113
    provctx = ossl_provider_ctx(EVP_SIGNATURE_get0_provider(sig));
484
113
    return sig->settable_ctx_params(NULL, provctx);
485
113
}
486
487
static int evp_pkey_signature_init(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature,
488
    int operation, const OSSL_PARAM params[])
489
{
490
    int ret = 0;
491
    void *provkey = NULL;
492
    EVP_KEYMGMT *tmp_keymgmt = NULL;
493
    const OSSL_PROVIDER *tmp_prov = NULL;
494
    const char *supported_sig = NULL;
495
    int iter;
496
497
    if (ctx == NULL) {
498
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
499
        return -1;
500
    }
501
502
    evp_pkey_ctx_free_old_ops(ctx);
503
    ctx->operation = operation;
504
505
    if (signature != NULL) {
506
        /*
507
         * It's important to figure out what the key type should be, and if
508
         * that is what we have in ctx.
509
         */
510
511
        EVP_KEYMGMT *tmp_keymgmt_tofree = NULL;
512
513
        if (ctx->pkey == NULL) {
514
            ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
515
            goto err;
516
        }
517
518
        /*
519
         * Ensure that the key is provided, either natively, or as a
520
         * cached export.  We start by fetching the keymgmt with the same
521
         * name as |ctx->pkey|, but from the provider of the signature
522
         * method, using the same property query as when fetching the
523
         * signature method.  With the keymgmt we found (if we did), we
524
         * try to export |ctx->pkey| to it (evp_pkey_export_to_provider()
525
         * is smart enough to only actually export it if |tmp_keymgmt|
526
         * is different from |ctx->pkey|'s keymgmt)
527
         */
528
        tmp_prov = EVP_SIGNATURE_get0_provider(signature);
529
        tmp_keymgmt_tofree = tmp_keymgmt = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
530
            EVP_KEYMGMT_get0_name(ctx->keymgmt),
531
            ctx->propquery);
532
        if (tmp_keymgmt != NULL)
533
            provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
534
                &tmp_keymgmt, ctx->propquery);
535
        if (tmp_keymgmt == NULL)
536
            EVP_KEYMGMT_free(tmp_keymgmt_tofree);
537
538
        if (provkey == NULL)
539
            goto end;
540
541
        /*
542
         * Check that the signature matches the given key.  This is not
543
         * designed to work with legacy keys, so has to be done after we've
544
         * ensured that the key is at least exported to a provider (above).
545
         */
546
        if (signature->query_key_types != NULL) {
547
            /* This is expect to be a NULL terminated array */
548
            const char **keytypes;
549
550
            keytypes = signature->query_key_types();
551
            for (; *keytypes != NULL; keytypes++)
552
                if (EVP_PKEY_CTX_is_a(ctx, *keytypes))
553
                    break;
554
            if (*keytypes == NULL) {
555
                ERR_raise(ERR_LIB_EVP, EVP_R_SIGNATURE_TYPE_AND_KEY_TYPE_INCOMPATIBLE);
556
                ret = -2;
557
                goto end;
558
            }
559
        } else {
560
            /*
561
             * Fallback 1:
562
             * check if the keytype is the same as the signature algorithm name
563
             */
564
            const char *keytype = EVP_KEYMGMT_get0_name(ctx->keymgmt);
565
            int ok = EVP_SIGNATURE_is_a(signature, keytype);
566
567
            /*
568
             * Fallback 2:
569
             * query the pkey for a default signature algorithm name, and check
570
             * if it matches the signature implementation
571
             */
572
            if (!ok) {
573
                const char *signame
574
                    = evp_keymgmt_util_query_operation_name(ctx->keymgmt,
575
                        OSSL_OP_SIGNATURE);
576
577
                ok = EVP_SIGNATURE_is_a(signature, signame);
578
            }
579
580
            /* If none of the fallbacks helped, we're lost */
581
            if (!ok) {
582
                ERR_raise(ERR_LIB_EVP, EVP_R_SIGNATURE_TYPE_AND_KEY_TYPE_INCOMPATIBLE);
583
                ret = -2;
584
                goto end;
585
            }
586
        }
587
588
        if (!EVP_SIGNATURE_up_ref(signature))
589
            goto err;
590
    } else {
591
        /* Without a pre-fetched signature, it must be figured out somehow */
592
        ERR_set_mark();
593
594
        if (evp_pkey_ctx_is_legacy(ctx))
595
            goto legacy;
596
597
        if (ctx->pkey == NULL) {
598
            ERR_clear_last_mark();
599
            ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
600
            goto err;
601
        }
602
603
        /*
604
         * Try to derive the supported signature from |ctx->keymgmt|.
605
         */
606
        if (!ossl_assert(ctx->pkey->keymgmt == NULL
607
                || ctx->pkey->keymgmt == ctx->keymgmt)) {
608
            ERR_clear_last_mark();
609
            ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
610
            goto err;
611
        }
612
        supported_sig
613
            = evp_keymgmt_util_query_operation_name(ctx->keymgmt,
614
                OSSL_OP_SIGNATURE);
615
        if (supported_sig == NULL) {
616
            ERR_clear_last_mark();
617
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
618
            goto err;
619
        }
620
621
        /*
622
         * We perform two iterations:
623
         *
624
         * 1.  Do the normal signature fetch, using the fetching data given by
625
         *     the EVP_PKEY_CTX.
626
         * 2.  Do the provider specific signature fetch, from the same provider
627
         *     as |ctx->keymgmt|
628
         *
629
         * We then try to fetch the keymgmt from the same provider as the
630
         * signature, and try to export |ctx->pkey| to that keymgmt (when
631
         * this keymgmt happens to be the same as |ctx->keymgmt|, the export
632
         * is a no-op, but we call it anyway to not complicate the code even
633
         * more).
634
         * If the export call succeeds (returns a non-NULL provider key pointer),
635
         * we're done and can perform the operation itself.  If not, we perform
636
         * the second iteration, or jump to legacy.
637
         */
638
        for (iter = 1; iter < 3 && provkey == NULL; iter++) {
639
            EVP_KEYMGMT *tmp_keymgmt_tofree = NULL;
640
641
            /*
642
             * If we're on the second iteration, free the results from the first.
643
             * They are NULL on the first iteration, so no need to check what
644
             * iteration we're on.
645
             */
646
            EVP_SIGNATURE_free(signature);
647
            EVP_KEYMGMT_free(tmp_keymgmt);
648
649
            switch (iter) {
650
            case 1:
651
                signature = EVP_SIGNATURE_fetch(ctx->libctx, supported_sig, ctx->propquery);
652
                if (signature != NULL)
653
                    tmp_prov = EVP_SIGNATURE_get0_provider(signature);
654
                break;
655
            case 2:
656
                tmp_prov = EVP_KEYMGMT_get0_provider(ctx->keymgmt);
657
                signature = evp_signature_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
658
                    supported_sig, ctx->propquery);
659
                if (signature == NULL)
660
                    goto legacy;
661
                break;
662
            }
663
            if (signature == NULL)
664
                continue;
665
666
            /*
667
             * Ensure that the key is provided, either natively, or as a
668
             * cached export.  We start by fetching the keymgmt with the same
669
             * name as |ctx->pkey|, but from the provider of the signature
670
             * method, using the same property query as when fetching the
671
             * signature method.  With the keymgmt we found (if we did), we
672
             * try to export |ctx->pkey| to it (evp_pkey_export_to_provider()
673
             * is smart enough to only actually export it if |tmp_keymgmt|
674
             * is different from |ctx->pkey|'s keymgmt)
675
             */
676
            tmp_keymgmt_tofree = tmp_keymgmt = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
677
                EVP_KEYMGMT_get0_name(ctx->keymgmt),
678
                ctx->propquery);
679
            if (tmp_keymgmt != NULL)
680
                provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
681
                    &tmp_keymgmt, ctx->propquery);
682
            if (tmp_keymgmt == NULL)
683
                EVP_KEYMGMT_free(tmp_keymgmt_tofree);
684
        }
685
686
        if (provkey == NULL) {
687
            EVP_SIGNATURE_free(signature);
688
            goto legacy;
689
        }
690
691
        ERR_pop_to_mark();
692
    }
693
694
    /* No more legacy from here down to legacy: */
695
696
    ctx->op.sig.signature = signature;
697
    ctx->op.sig.algctx = signature->newctx(ossl_provider_ctx(signature->prov), ctx->propquery);
698
    if (ctx->op.sig.algctx == NULL) {
699
        /* The provider key can stay in the cache */
700
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
701
        goto err;
702
    }
703
704
    switch (operation) {
705
    case EVP_PKEY_OP_SIGN:
706
        if (signature->sign_init == NULL) {
707
            ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
708
            ret = -2;
709
            goto err;
710
        }
711
        ret = signature->sign_init(ctx->op.sig.algctx, provkey, params);
712
        break;
713
    case EVP_PKEY_OP_SIGNMSG:
714
        if (signature->sign_message_init == NULL) {
715
            ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
716
            ret = -2;
717
            goto err;
718
        }
719
        ret = signature->sign_message_init(ctx->op.sig.algctx, provkey, params);
720
        break;
721
    case EVP_PKEY_OP_VERIFY:
722
        if (signature->verify_init == NULL) {
723
            ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
724
            ret = -2;
725
            goto err;
726
        }
727
        ret = signature->verify_init(ctx->op.sig.algctx, provkey, params);
728
        break;
729
    case EVP_PKEY_OP_VERIFYMSG:
730
        if (signature->verify_message_init == NULL) {
731
            ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
732
            ret = -2;
733
            goto err;
734
        }
735
        ret = signature->verify_message_init(ctx->op.sig.algctx, provkey, params);
736
        break;
737
    case EVP_PKEY_OP_VERIFYRECOVER:
738
        if (signature->verify_recover_init == NULL) {
739
            ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
740
            ret = -2;
741
            goto err;
742
        }
743
        ret = signature->verify_recover_init(ctx->op.sig.algctx, provkey, params);
744
        break;
745
    default:
746
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
747
        goto err;
748
    }
749
750
    if (ret <= 0) {
751
        signature->freectx(ctx->op.sig.algctx);
752
        ctx->op.sig.algctx = NULL;
753
        goto err;
754
    }
755
    goto end;
756
757
legacy:
758
    /*
759
     * If we don't have the full support we need with provided methods,
760
     * let's go see if legacy does.
761
     */
762
    ERR_pop_to_mark();
763
    EVP_KEYMGMT_free(tmp_keymgmt);
764
    tmp_keymgmt = NULL;
765
766
    if (ctx->pmeth == NULL
767
        || (operation == EVP_PKEY_OP_SIGN && ctx->pmeth->sign == NULL)
768
        || (operation == EVP_PKEY_OP_VERIFY && ctx->pmeth->verify == NULL)
769
        || (operation == EVP_PKEY_OP_VERIFYRECOVER
770
            && ctx->pmeth->verify_recover == NULL)) {
771
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
772
        return -2;
773
    }
774
775
    switch (operation) {
776
    case EVP_PKEY_OP_SIGN:
777
        if (ctx->pmeth->sign_init == NULL)
778
            return 1;
779
        ret = ctx->pmeth->sign_init(ctx);
780
        break;
781
    case EVP_PKEY_OP_VERIFY:
782
        if (ctx->pmeth->verify_init == NULL)
783
            return 1;
784
        ret = ctx->pmeth->verify_init(ctx);
785
        break;
786
    case EVP_PKEY_OP_VERIFYRECOVER:
787
        if (ctx->pmeth->verify_recover_init == NULL)
788
            return 1;
789
        ret = ctx->pmeth->verify_recover_init(ctx);
790
        break;
791
    default:
792
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
793
        goto err;
794
    }
795
    if (ret <= 0)
796
        goto err;
797
end:
798
#ifndef FIPS_MODULE
799
    if (ret > 0)
800
        ret = evp_pkey_ctx_use_cached_data(ctx);
801
#endif
802
803
    EVP_KEYMGMT_free(tmp_keymgmt);
804
    return ret;
805
err:
806
    evp_pkey_ctx_free_old_ops(ctx);
807
    ctx->operation = EVP_PKEY_OP_UNDEFINED;
808
    EVP_KEYMGMT_free(tmp_keymgmt);
809
    return ret;
810
}
811
812
int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
813
0
{
814
0
    return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_SIGN, NULL);
815
0
}
816
817
int EVP_PKEY_sign_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
818
0
{
819
0
    return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_SIGN, params);
820
0
}
821
822
int EVP_PKEY_sign_init_ex2(EVP_PKEY_CTX *ctx,
823
    EVP_SIGNATURE *algo, const OSSL_PARAM params[])
824
0
{
825
0
    return evp_pkey_signature_init(ctx, algo, EVP_PKEY_OP_SIGN, params);
826
0
}
827
828
int EVP_PKEY_sign_message_init(EVP_PKEY_CTX *ctx,
829
    EVP_SIGNATURE *algo, const OSSL_PARAM params[])
830
1.36k
{
831
1.36k
    return evp_pkey_signature_init(ctx, algo, EVP_PKEY_OP_SIGNMSG, params);
832
1.36k
}
833
834
int EVP_PKEY_sign_message_update(EVP_PKEY_CTX *ctx,
835
    const unsigned char *in, size_t inlen)
836
0
{
837
0
    if (ctx == NULL) {
838
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
839
0
        return -1;
840
0
    }
841
842
0
    if (ctx->operation != EVP_PKEY_OP_SIGNMSG) {
843
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
844
0
        return -1;
845
0
    }
846
847
0
    if (ctx->op.sig.signature->sign_message_update == NULL) {
848
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
849
0
        return -2;
850
0
    }
851
852
0
    return ctx->op.sig.signature->sign_message_update(ctx->op.sig.algctx,
853
0
        in, inlen);
854
0
}
855
856
int EVP_PKEY_sign_message_final(EVP_PKEY_CTX *ctx,
857
    unsigned char *sig, size_t *siglen)
858
0
{
859
0
    if (ctx == NULL) {
860
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
861
0
        return -1;
862
0
    }
863
864
0
    if (ctx->operation != EVP_PKEY_OP_SIGNMSG) {
865
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
866
0
        return -1;
867
0
    }
868
869
0
    if (ctx->op.sig.signature->sign_message_final == NULL) {
870
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
871
0
        return -2;
872
0
    }
873
874
0
    return ctx->op.sig.signature->sign_message_final(ctx->op.sig.algctx,
875
0
        sig, siglen,
876
0
        (sig == NULL) ? 0 : *siglen);
877
0
}
878
879
int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
880
    unsigned char *sig, size_t *siglen,
881
    const unsigned char *tbs, size_t tbslen)
882
{
883
    int ret;
884
885
    if (ctx == NULL) {
886
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
887
        return -1;
888
    }
889
890
    if (ctx->operation != EVP_PKEY_OP_SIGN
891
        && ctx->operation != EVP_PKEY_OP_SIGNMSG) {
892
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
893
        return -1;
894
    }
895
896
    if (ctx->op.sig.algctx == NULL)
897
        goto legacy;
898
899
    if (ctx->op.sig.signature->sign == NULL) {
900
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
901
        return -2;
902
    }
903
904
    ret = ctx->op.sig.signature->sign(ctx->op.sig.algctx, sig, siglen,
905
        (sig == NULL) ? 0 : *siglen, tbs, tbslen);
906
907
    return ret;
908
legacy:
909
910
    if (ctx->pmeth == NULL || ctx->pmeth->sign == NULL) {
911
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
912
        return -2;
913
    }
914
915
    M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN) return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
916
}
917
918
int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
919
0
{
920
0
    return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_VERIFY, NULL);
921
0
}
922
923
int EVP_PKEY_verify_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
924
0
{
925
0
    return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_VERIFY, params);
926
0
}
927
928
int EVP_PKEY_verify_init_ex2(EVP_PKEY_CTX *ctx,
929
    EVP_SIGNATURE *algo, const OSSL_PARAM params[])
930
0
{
931
0
    return evp_pkey_signature_init(ctx, algo, EVP_PKEY_OP_VERIFY, params);
932
0
}
933
934
int EVP_PKEY_verify_message_init(EVP_PKEY_CTX *ctx,
935
    EVP_SIGNATURE *algo, const OSSL_PARAM params[])
936
1.24k
{
937
1.24k
    return evp_pkey_signature_init(ctx, algo, EVP_PKEY_OP_VERIFYMSG, params);
938
1.24k
}
939
940
int EVP_PKEY_CTX_set_signature(EVP_PKEY_CTX *ctx,
941
    const unsigned char *sig, size_t siglen)
942
0
{
943
0
    OSSL_PARAM sig_params[2], *p = sig_params;
944
945
0
    if (ctx == NULL) {
946
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
947
0
        return 0;
948
0
    }
949
950
0
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_SIGNATURE_PARAM_SIGNATURE,
951
        /*
952
         * Cast away the const. This is
953
         * read only so should be safe
954
         */
955
0
        (char *)sig, siglen);
956
0
    *p = OSSL_PARAM_construct_end();
957
958
0
    return EVP_PKEY_CTX_set_params(ctx, sig_params);
959
0
}
960
961
int EVP_PKEY_verify_message_update(EVP_PKEY_CTX *ctx,
962
    const unsigned char *in, size_t inlen)
963
0
{
964
0
    if (ctx == NULL) {
965
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
966
0
        return -1;
967
0
    }
968
969
0
    if (ctx->operation != EVP_PKEY_OP_VERIFYMSG) {
970
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
971
0
        return -1;
972
0
    }
973
974
0
    if (ctx->op.sig.signature->verify_message_update == NULL) {
975
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
976
0
        return -2;
977
0
    }
978
979
0
    return ctx->op.sig.signature->verify_message_update(ctx->op.sig.algctx,
980
0
        in, inlen);
981
0
}
982
983
int EVP_PKEY_verify_message_final(EVP_PKEY_CTX *ctx)
984
0
{
985
0
    if (ctx == NULL) {
986
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
987
0
        return -1;
988
0
    }
989
990
0
    if (ctx->operation != EVP_PKEY_OP_VERIFYMSG) {
991
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
992
0
        return -1;
993
0
    }
994
995
0
    if (ctx->op.sig.signature->verify_message_final == NULL) {
996
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
997
0
        return -2;
998
0
    }
999
1000
    /* The signature must have been set with EVP_PKEY_CTX_set_signature() */
1001
0
    return ctx->op.sig.signature->verify_message_final(ctx->op.sig.algctx);
1002
0
}
1003
1004
int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
1005
    const unsigned char *sig, size_t siglen,
1006
    const unsigned char *tbs, size_t tbslen)
1007
{
1008
    int ret;
1009
1010
    if (ctx == NULL) {
1011
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
1012
        return -1;
1013
    }
1014
1015
    if (ctx->operation != EVP_PKEY_OP_VERIFY
1016
        && ctx->operation != EVP_PKEY_OP_VERIFYMSG) {
1017
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
1018
        return -1;
1019
    }
1020
1021
    if (ctx->op.sig.algctx == NULL)
1022
        goto legacy;
1023
1024
    if (ctx->op.sig.signature->verify == NULL) {
1025
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
1026
        return -2;
1027
    }
1028
1029
    ret = ctx->op.sig.signature->verify(ctx->op.sig.algctx, sig, siglen,
1030
        tbs, tbslen);
1031
1032
    return ret;
1033
legacy:
1034
    if (ctx->pmeth == NULL || ctx->pmeth->verify == NULL) {
1035
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
1036
        return -2;
1037
    }
1038
1039
    return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
1040
}
1041
1042
int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
1043
0
{
1044
0
    return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_VERIFYRECOVER, NULL);
1045
0
}
1046
1047
int EVP_PKEY_verify_recover_init_ex(EVP_PKEY_CTX *ctx,
1048
    const OSSL_PARAM params[])
1049
0
{
1050
0
    return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_VERIFYRECOVER, params);
1051
0
}
1052
1053
int EVP_PKEY_verify_recover_init_ex2(EVP_PKEY_CTX *ctx,
1054
    EVP_SIGNATURE *algo, const OSSL_PARAM params[])
1055
0
{
1056
0
    return evp_pkey_signature_init(ctx, algo, EVP_PKEY_OP_VERIFYRECOVER, params);
1057
0
}
1058
1059
int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
1060
    unsigned char *rout, size_t *routlen,
1061
    const unsigned char *sig, size_t siglen)
1062
0
{
1063
0
    int ret;
1064
1065
0
    if (ctx == NULL) {
1066
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
1067
0
        return -1;
1068
0
    }
1069
1070
0
    if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
1071
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
1072
0
        return -1;
1073
0
    }
1074
1075
0
    if (ctx->op.sig.algctx == NULL)
1076
0
        goto legacy;
1077
1078
0
    if (ctx->op.sig.signature->verify_recover == NULL) {
1079
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
1080
0
        return -2;
1081
0
    }
1082
1083
0
    ret = ctx->op.sig.signature->verify_recover(ctx->op.sig.algctx, rout,
1084
0
        routlen,
1085
0
        (rout == NULL ? 0 : *routlen),
1086
0
        sig, siglen);
1087
0
    return ret;
1088
0
legacy:
1089
0
    if (ctx->pmeth == NULL || ctx->pmeth->verify_recover == NULL) {
1090
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
1091
0
        return -2;
1092
0
    }
1093
0
    M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER) return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
1094
0
}