Coverage Report

Created: 2026-09-12 06:55

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