Coverage Report

Created: 2025-12-31 06:58

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-2024 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <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.60M
{
376
1.60M
    int i;
377
378
1.60M
    if (signature == NULL)
379
115k
        return;
380
1.49M
    CRYPTO_DOWN_REF(&signature->refcnt, &i);
381
1.49M
    if (i > 0)
382
1.48M
        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.48M
{
391
1.48M
    int ref = 0;
392
393
1.48M
    CRYPTO_UP_REF(&signature->refcnt, &ref);
394
1.48M
    return 1;
395
1.48M
}
396
397
OSSL_PROVIDER *EVP_SIGNATURE_get0_provider(const EVP_SIGNATURE *signature)
398
169k
{
399
169k
    return signature->prov;
400
169k
}
401
402
EVP_SIGNATURE *EVP_SIGNATURE_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
403
    const char *properties)
404
494k
{
405
494k
    return evp_generic_fetch(ctx, OSSL_OP_SIGNATURE, algorithm, properties,
406
494k
        evp_signature_from_algorithm,
407
494k
        (int (*)(void *))EVP_SIGNATURE_up_ref,
408
494k
        (void (*)(void *))EVP_SIGNATURE_free);
409
494k
}
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.77k
{
424
2.77k
    return signature != NULL
425
2.77k
        && evp_is_a(signature->prov, signature->name_id, NULL, name);
426
2.77k
}
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
108
{
478
108
    void *provctx;
479
480
108
    if (sig == NULL || sig->settable_ctx_params == NULL)
481
0
        return NULL;
482
483
108
    provctx = ossl_provider_ctx(EVP_SIGNATURE_get0_provider(sig));
484
108
    return sig->settable_ctx_params(NULL, provctx);
485
108
}
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
                return -2;
557
            }
558
        } else {
559
            /*
560
             * Fallback 1:
561
             * check if the keytype is the same as the signature algorithm name
562
             */
563
            const char *keytype = EVP_KEYMGMT_get0_name(ctx->keymgmt);
564
            int ok = EVP_SIGNATURE_is_a(signature, keytype);
565
566
            /*
567
             * Fallback 2:
568
             * query the pkey for a default signature algorithm name, and check
569
             * if it matches the signature implementation
570
             */
571
            if (!ok) {
572
                const char *signame
573
                    = evp_keymgmt_util_query_operation_name(ctx->keymgmt,
574
                        OSSL_OP_SIGNATURE);
575
576
                ok = EVP_SIGNATURE_is_a(signature, signame);
577
            }
578
579
            /* If none of the fallbacks helped, we're lost */
580
            if (!ok) {
581
                ERR_raise(ERR_LIB_EVP, EVP_R_SIGNATURE_TYPE_AND_KEY_TYPE_INCOMPATIBLE);
582
                return -2;
583
            }
584
        }
585
586
        if (!EVP_SIGNATURE_up_ref(signature))
587
            return 0;
588
    } else {
589
        /* Without a pre-fetched signature, it must be figured out somehow */
590
        ERR_set_mark();
591
592
        if (evp_pkey_ctx_is_legacy(ctx))
593
            goto legacy;
594
595
        if (ctx->pkey == NULL) {
596
            ERR_clear_last_mark();
597
            ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
598
            goto err;
599
        }
600
601
        /*
602
         * Try to derive the supported signature from |ctx->keymgmt|.
603
         */
604
        if (!ossl_assert(ctx->pkey->keymgmt == NULL
605
                || ctx->pkey->keymgmt == ctx->keymgmt)) {
606
            ERR_clear_last_mark();
607
            ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
608
            goto err;
609
        }
610
        supported_sig
611
            = evp_keymgmt_util_query_operation_name(ctx->keymgmt,
612
                OSSL_OP_SIGNATURE);
613
        if (supported_sig == NULL) {
614
            ERR_clear_last_mark();
615
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
616
            goto err;
617
        }
618
619
        /*
620
         * We perform two iterations:
621
         *
622
         * 1.  Do the normal signature fetch, using the fetching data given by
623
         *     the EVP_PKEY_CTX.
624
         * 2.  Do the provider specific signature fetch, from the same provider
625
         *     as |ctx->keymgmt|
626
         *
627
         * We then try to fetch the keymgmt from the same provider as the
628
         * signature, and try to export |ctx->pkey| to that keymgmt (when
629
         * this keymgmt happens to be the same as |ctx->keymgmt|, the export
630
         * is a no-op, but we call it anyway to not complicate the code even
631
         * more).
632
         * If the export call succeeds (returns a non-NULL provider key pointer),
633
         * we're done and can perform the operation itself.  If not, we perform
634
         * the second iteration, or jump to legacy.
635
         */
636
        for (iter = 1; iter < 3 && provkey == NULL; iter++) {
637
            EVP_KEYMGMT *tmp_keymgmt_tofree = NULL;
638
639
            /*
640
             * If we're on the second iteration, free the results from the first.
641
             * They are NULL on the first iteration, so no need to check what
642
             * iteration we're on.
643
             */
644
            EVP_SIGNATURE_free(signature);
645
            EVP_KEYMGMT_free(tmp_keymgmt);
646
647
            switch (iter) {
648
            case 1:
649
                signature = EVP_SIGNATURE_fetch(ctx->libctx, supported_sig, ctx->propquery);
650
                if (signature != NULL)
651
                    tmp_prov = EVP_SIGNATURE_get0_provider(signature);
652
                break;
653
            case 2:
654
                tmp_prov = EVP_KEYMGMT_get0_provider(ctx->keymgmt);
655
                signature = evp_signature_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
656
                    supported_sig, ctx->propquery);
657
                if (signature == NULL)
658
                    goto legacy;
659
                break;
660
            }
661
            if (signature == NULL)
662
                continue;
663
664
            /*
665
             * Ensure that the key is provided, either natively, or as a
666
             * cached export.  We start by fetching the keymgmt with the same
667
             * name as |ctx->pkey|, but from the provider of the signature
668
             * method, using the same property query as when fetching the
669
             * signature method.  With the keymgmt we found (if we did), we
670
             * try to export |ctx->pkey| to it (evp_pkey_export_to_provider()
671
             * is smart enough to only actually export it if |tmp_keymgmt|
672
             * is different from |ctx->pkey|'s keymgmt)
673
             */
674
            tmp_keymgmt_tofree = tmp_keymgmt = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
675
                EVP_KEYMGMT_get0_name(ctx->keymgmt),
676
                ctx->propquery);
677
            if (tmp_keymgmt != NULL)
678
                provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
679
                    &tmp_keymgmt, ctx->propquery);
680
            if (tmp_keymgmt == NULL)
681
                EVP_KEYMGMT_free(tmp_keymgmt_tofree);
682
        }
683
684
        if (provkey == NULL) {
685
            EVP_SIGNATURE_free(signature);
686
            goto legacy;
687
        }
688
689
        ERR_pop_to_mark();
690
    }
691
692
    /* No more legacy from here down to legacy: */
693
694
    ctx->op.sig.signature = signature;
695
    ctx->op.sig.algctx = signature->newctx(ossl_provider_ctx(signature->prov), ctx->propquery);
696
    if (ctx->op.sig.algctx == NULL) {
697
        /* The provider key can stay in the cache */
698
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
699
        goto err;
700
    }
701
702
    switch (operation) {
703
    case EVP_PKEY_OP_SIGN:
704
        if (signature->sign_init == NULL) {
705
            ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
706
            ret = -2;
707
            goto err;
708
        }
709
        ret = signature->sign_init(ctx->op.sig.algctx, provkey, params);
710
        break;
711
    case EVP_PKEY_OP_SIGNMSG:
712
        if (signature->sign_message_init == NULL) {
713
            ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
714
            ret = -2;
715
            goto err;
716
        }
717
        ret = signature->sign_message_init(ctx->op.sig.algctx, provkey, params);
718
        break;
719
    case EVP_PKEY_OP_VERIFY:
720
        if (signature->verify_init == NULL) {
721
            ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
722
            ret = -2;
723
            goto err;
724
        }
725
        ret = signature->verify_init(ctx->op.sig.algctx, provkey, params);
726
        break;
727
    case EVP_PKEY_OP_VERIFYMSG:
728
        if (signature->verify_message_init == NULL) {
729
            ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
730
            ret = -2;
731
            goto err;
732
        }
733
        ret = signature->verify_message_init(ctx->op.sig.algctx, provkey, params);
734
        break;
735
    case EVP_PKEY_OP_VERIFYRECOVER:
736
        if (signature->verify_recover_init == NULL) {
737
            ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
738
            ret = -2;
739
            goto err;
740
        }
741
        ret = signature->verify_recover_init(ctx->op.sig.algctx, provkey, params);
742
        break;
743
    default:
744
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
745
        goto err;
746
    }
747
748
    if (ret <= 0) {
749
        signature->freectx(ctx->op.sig.algctx);
750
        ctx->op.sig.algctx = NULL;
751
        goto err;
752
    }
753
    goto end;
754
755
legacy:
756
    /*
757
     * If we don't have the full support we need with provided methods,
758
     * let's go see if legacy does.
759
     */
760
    ERR_pop_to_mark();
761
    EVP_KEYMGMT_free(tmp_keymgmt);
762
    tmp_keymgmt = NULL;
763
764
    if (ctx->pmeth == NULL
765
        || (operation == EVP_PKEY_OP_SIGN && ctx->pmeth->sign == NULL)
766
        || (operation == EVP_PKEY_OP_VERIFY && ctx->pmeth->verify == NULL)
767
        || (operation == EVP_PKEY_OP_VERIFYRECOVER
768
            && ctx->pmeth->verify_recover == NULL)) {
769
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
770
        return -2;
771
    }
772
773
    switch (operation) {
774
    case EVP_PKEY_OP_SIGN:
775
        if (ctx->pmeth->sign_init == NULL)
776
            return 1;
777
        ret = ctx->pmeth->sign_init(ctx);
778
        break;
779
    case EVP_PKEY_OP_VERIFY:
780
        if (ctx->pmeth->verify_init == NULL)
781
            return 1;
782
        ret = ctx->pmeth->verify_init(ctx);
783
        break;
784
    case EVP_PKEY_OP_VERIFYRECOVER:
785
        if (ctx->pmeth->verify_recover_init == NULL)
786
            return 1;
787
        ret = ctx->pmeth->verify_recover_init(ctx);
788
        break;
789
    default:
790
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
791
        goto err;
792
    }
793
    if (ret <= 0)
794
        goto err;
795
end:
796
#ifndef FIPS_MODULE
797
    if (ret > 0)
798
        ret = evp_pkey_ctx_use_cached_data(ctx);
799
#endif
800
801
    EVP_KEYMGMT_free(tmp_keymgmt);
802
    return ret;
803
err:
804
    evp_pkey_ctx_free_old_ops(ctx);
805
    ctx->operation = EVP_PKEY_OP_UNDEFINED;
806
    EVP_KEYMGMT_free(tmp_keymgmt);
807
    return ret;
808
}
809
810
int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
811
0
{
812
0
    return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_SIGN, NULL);
813
0
}
814
815
int EVP_PKEY_sign_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
816
0
{
817
0
    return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_SIGN, params);
818
0
}
819
820
int EVP_PKEY_sign_init_ex2(EVP_PKEY_CTX *ctx,
821
    EVP_SIGNATURE *algo, const OSSL_PARAM params[])
822
0
{
823
0
    return evp_pkey_signature_init(ctx, algo, EVP_PKEY_OP_SIGN, params);
824
0
}
825
826
int EVP_PKEY_sign_message_init(EVP_PKEY_CTX *ctx,
827
    EVP_SIGNATURE *algo, const OSSL_PARAM params[])
828
1.44k
{
829
1.44k
    return evp_pkey_signature_init(ctx, algo, EVP_PKEY_OP_SIGNMSG, params);
830
1.44k
}
831
832
int EVP_PKEY_sign_message_update(EVP_PKEY_CTX *ctx,
833
    const unsigned char *in, size_t inlen)
834
0
{
835
0
    if (ctx == NULL) {
836
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
837
0
        return -1;
838
0
    }
839
840
0
    if (ctx->operation != EVP_PKEY_OP_SIGNMSG) {
841
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
842
0
        return -1;
843
0
    }
844
845
0
    if (ctx->op.sig.signature->sign_message_update == NULL) {
846
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
847
0
        return -2;
848
0
    }
849
850
0
    return ctx->op.sig.signature->sign_message_update(ctx->op.sig.algctx,
851
0
        in, inlen);
852
0
}
853
854
int EVP_PKEY_sign_message_final(EVP_PKEY_CTX *ctx,
855
    unsigned char *sig, size_t *siglen)
856
0
{
857
0
    if (ctx == NULL) {
858
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
859
0
        return -1;
860
0
    }
861
862
0
    if (ctx->operation != EVP_PKEY_OP_SIGNMSG) {
863
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
864
0
        return -1;
865
0
    }
866
867
0
    if (ctx->op.sig.signature->sign_message_final == NULL) {
868
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
869
0
        return -2;
870
0
    }
871
872
0
    return ctx->op.sig.signature->sign_message_final(ctx->op.sig.algctx,
873
0
        sig, siglen,
874
0
        (sig == NULL) ? 0 : *siglen);
875
0
}
876
877
int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
878
    unsigned char *sig, size_t *siglen,
879
    const unsigned char *tbs, size_t tbslen)
880
{
881
    int ret;
882
883
    if (ctx == NULL) {
884
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
885
        return -1;
886
    }
887
888
    if (ctx->operation != EVP_PKEY_OP_SIGN
889
        && ctx->operation != EVP_PKEY_OP_SIGNMSG) {
890
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
891
        return -1;
892
    }
893
894
    if (ctx->op.sig.algctx == NULL)
895
        goto legacy;
896
897
    if (ctx->op.sig.signature->sign == NULL) {
898
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
899
        return -2;
900
    }
901
902
    ret = ctx->op.sig.signature->sign(ctx->op.sig.algctx, sig, siglen,
903
        (sig == NULL) ? 0 : *siglen, tbs, tbslen);
904
905
    return ret;
906
legacy:
907
908
    if (ctx->pmeth == NULL || ctx->pmeth->sign == NULL) {
909
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
910
        return -2;
911
    }
912
913
    M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN) return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
914
}
915
916
int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
917
0
{
918
0
    return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_VERIFY, NULL);
919
0
}
920
921
int EVP_PKEY_verify_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
922
0
{
923
0
    return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_VERIFY, params);
924
0
}
925
926
int EVP_PKEY_verify_init_ex2(EVP_PKEY_CTX *ctx,
927
    EVP_SIGNATURE *algo, const OSSL_PARAM params[])
928
0
{
929
0
    return evp_pkey_signature_init(ctx, algo, EVP_PKEY_OP_VERIFY, params);
930
0
}
931
932
int EVP_PKEY_verify_message_init(EVP_PKEY_CTX *ctx,
933
    EVP_SIGNATURE *algo, const OSSL_PARAM params[])
934
1.32k
{
935
1.32k
    return evp_pkey_signature_init(ctx, algo, EVP_PKEY_OP_VERIFYMSG, params);
936
1.32k
}
937
938
int EVP_PKEY_CTX_set_signature(EVP_PKEY_CTX *ctx,
939
    const unsigned char *sig, size_t siglen)
940
0
{
941
0
    OSSL_PARAM sig_params[2], *p = sig_params;
942
943
0
    if (ctx == NULL) {
944
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
945
0
        return 0;
946
0
    }
947
948
0
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_SIGNATURE_PARAM_SIGNATURE,
949
        /*
950
         * Cast away the const. This is
951
         * read only so should be safe
952
         */
953
0
        (char *)sig, siglen);
954
0
    *p = OSSL_PARAM_construct_end();
955
956
0
    return EVP_PKEY_CTX_set_params(ctx, sig_params);
957
0
}
958
959
int EVP_PKEY_verify_message_update(EVP_PKEY_CTX *ctx,
960
    const unsigned char *in, size_t inlen)
961
0
{
962
0
    if (ctx == NULL) {
963
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
964
0
        return -1;
965
0
    }
966
967
0
    if (ctx->operation != EVP_PKEY_OP_VERIFYMSG) {
968
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
969
0
        return -1;
970
0
    }
971
972
0
    if (ctx->op.sig.signature->verify_message_update == NULL) {
973
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
974
0
        return -2;
975
0
    }
976
977
0
    return ctx->op.sig.signature->verify_message_update(ctx->op.sig.algctx,
978
0
        in, inlen);
979
0
}
980
981
int EVP_PKEY_verify_message_final(EVP_PKEY_CTX *ctx)
982
0
{
983
0
    if (ctx == NULL) {
984
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
985
0
        return -1;
986
0
    }
987
988
0
    if (ctx->operation != EVP_PKEY_OP_VERIFYMSG) {
989
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
990
0
        return -1;
991
0
    }
992
993
0
    if (ctx->op.sig.signature->verify_message_final == NULL) {
994
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
995
0
        return -2;
996
0
    }
997
998
    /* The signature must have been set with EVP_PKEY_CTX_set_signature() */
999
0
    return ctx->op.sig.signature->verify_message_final(ctx->op.sig.algctx);
1000
0
}
1001
1002
int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
1003
    const unsigned char *sig, size_t siglen,
1004
    const unsigned char *tbs, size_t tbslen)
1005
{
1006
    int ret;
1007
1008
    if (ctx == NULL) {
1009
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
1010
        return -1;
1011
    }
1012
1013
    if (ctx->operation != EVP_PKEY_OP_VERIFY
1014
        && ctx->operation != EVP_PKEY_OP_VERIFYMSG) {
1015
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
1016
        return -1;
1017
    }
1018
1019
    if (ctx->op.sig.algctx == NULL)
1020
        goto legacy;
1021
1022
    if (ctx->op.sig.signature->verify == NULL) {
1023
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
1024
        return -2;
1025
    }
1026
1027
    ret = ctx->op.sig.signature->verify(ctx->op.sig.algctx, sig, siglen,
1028
        tbs, tbslen);
1029
1030
    return ret;
1031
legacy:
1032
    if (ctx->pmeth == NULL || ctx->pmeth->verify == NULL) {
1033
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
1034
        return -2;
1035
    }
1036
1037
    return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
1038
}
1039
1040
int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
1041
0
{
1042
0
    return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_VERIFYRECOVER, NULL);
1043
0
}
1044
1045
int EVP_PKEY_verify_recover_init_ex(EVP_PKEY_CTX *ctx,
1046
    const OSSL_PARAM params[])
1047
0
{
1048
0
    return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_VERIFYRECOVER, params);
1049
0
}
1050
1051
int EVP_PKEY_verify_recover_init_ex2(EVP_PKEY_CTX *ctx,
1052
    EVP_SIGNATURE *algo, const OSSL_PARAM params[])
1053
0
{
1054
0
    return evp_pkey_signature_init(ctx, algo, EVP_PKEY_OP_VERIFYRECOVER, params);
1055
0
}
1056
1057
int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
1058
    unsigned char *rout, size_t *routlen,
1059
    const unsigned char *sig, size_t siglen)
1060
0
{
1061
0
    int ret;
1062
1063
0
    if (ctx == NULL) {
1064
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
1065
0
        return -1;
1066
0
    }
1067
1068
0
    if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
1069
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
1070
0
        return -1;
1071
0
    }
1072
1073
0
    if (ctx->op.sig.algctx == NULL)
1074
0
        goto legacy;
1075
1076
0
    if (ctx->op.sig.signature->verify_recover == NULL) {
1077
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
1078
0
        return -2;
1079
0
    }
1080
1081
0
    ret = ctx->op.sig.signature->verify_recover(ctx->op.sig.algctx, rout,
1082
0
        routlen,
1083
0
        (rout == NULL ? 0 : *routlen),
1084
0
        sig, siglen);
1085
0
    return ret;
1086
0
legacy:
1087
0
    if (ctx->pmeth == NULL || ctx->pmeth->verify_recover == NULL) {
1088
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
1089
0
        return -2;
1090
0
    }
1091
0
    M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER) return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
1092
0
}