Coverage Report

Created: 2026-10-28 06:28

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