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