Home | History | Annotate | Line # | Download | only in test
      1      1.1  christos /*
      2      1.1  christos  * Copyright 2007-2025 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  * Copyright Nokia 2007-2019
      4      1.1  christos  * Copyright Siemens AG 2015-2019
      5      1.1  christos  *
      6      1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      7      1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      8      1.1  christos  * in the file LICENSE in the source distribution or at
      9      1.1  christos  * https://www.openssl.org/source/license.html
     10      1.1  christos  */
     11      1.1  christos 
     12      1.1  christos #include "helpers/cmp_testlib.h"
     13      1.1  christos 
     14      1.1  christos #include "cmp_mock_srv.h"
     15      1.1  christos 
     16      1.1  christos static const char *server_key_f;
     17      1.1  christos static const char *server_cert_f;
     18      1.1  christos static const char *client_key_f;
     19      1.1  christos static const char *client_cert_f;
     20      1.1  christos static const char *pkcs10_f;
     21      1.1  christos 
     22      1.1  christos typedef struct test_fixture {
     23      1.1  christos     const char *test_case_name;
     24      1.1  christos     OSSL_CMP_CTX *cmp_ctx;
     25      1.1  christos     OSSL_CMP_SRV_CTX *srv_ctx;
     26      1.1  christos     int req_type;
     27      1.1  christos     int expected;
     28      1.1  christos     STACK_OF(X509) *caPubs;
     29      1.1  christos } CMP_SES_TEST_FIXTURE;
     30      1.1  christos 
     31      1.1  christos static OSSL_LIB_CTX *libctx = NULL;
     32      1.1  christos static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL;
     33      1.1  christos 
     34      1.1  christos static EVP_PKEY *server_key = NULL;
     35      1.1  christos static X509 *server_cert = NULL;
     36      1.1  christos static EVP_PKEY *client_key = NULL;
     37      1.1  christos static X509 *client_cert = NULL;
     38      1.1  christos static unsigned char ref[CMP_TEST_REFVALUE_LENGTH];
     39      1.1  christos 
     40      1.1  christos /*
     41      1.1  christos  * For these unit tests, the client abandons message protection, and for
     42      1.1  christos  * error messages the mock server does so as well.
     43      1.1  christos  * Message protection and verification is tested in cmp_lib_test.c
     44      1.1  christos  */
     45      1.1  christos 
     46      1.1  christos static void tear_down(CMP_SES_TEST_FIXTURE *fixture)
     47      1.1  christos {
     48      1.1  christos     OSSL_CMP_CTX_free(fixture->cmp_ctx);
     49      1.1  christos     ossl_cmp_mock_srv_free(fixture->srv_ctx);
     50      1.1  christos     sk_X509_free(fixture->caPubs);
     51      1.1  christos     OPENSSL_free(fixture);
     52      1.1  christos }
     53      1.1  christos 
     54      1.1  christos static CMP_SES_TEST_FIXTURE *set_up(const char *const test_case_name)
     55      1.1  christos {
     56      1.1  christos     CMP_SES_TEST_FIXTURE *fixture;
     57      1.1  christos     OSSL_CMP_CTX *srv_cmp_ctx = NULL;
     58      1.1  christos     OSSL_CMP_CTX *ctx = NULL; /* for client */
     59      1.1  christos 
     60      1.1  christos     if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
     61      1.1  christos         return NULL;
     62      1.1  christos     fixture->test_case_name = test_case_name;
     63      1.1  christos     if (!TEST_ptr(fixture->srv_ctx = ossl_cmp_mock_srv_new(libctx, NULL))
     64  1.1.1.2  christos         || !OSSL_CMP_SRV_CTX_set_accept_unprotected(fixture->srv_ctx, 1)
     65  1.1.1.2  christos         || !ossl_cmp_mock_srv_set1_refCert(fixture->srv_ctx, client_cert)
     66  1.1.1.2  christos         || !ossl_cmp_mock_srv_set1_certOut(fixture->srv_ctx, client_cert)
     67  1.1.1.2  christos         || (srv_cmp_ctx = OSSL_CMP_SRV_CTX_get0_cmp_ctx(fixture->srv_ctx)) == NULL
     68  1.1.1.2  christos         || !OSSL_CMP_CTX_set1_cert(srv_cmp_ctx, server_cert)
     69  1.1.1.2  christos         || !OSSL_CMP_CTX_set1_pkey(srv_cmp_ctx, server_key))
     70      1.1  christos         goto err;
     71      1.1  christos     if (!TEST_ptr(fixture->cmp_ctx = ctx = OSSL_CMP_CTX_new(libctx, NULL))
     72  1.1.1.2  christos         || !OSSL_CMP_CTX_set_log_cb(fixture->cmp_ctx, print_to_bio_out)
     73  1.1.1.2  christos         || !OSSL_CMP_CTX_set_transfer_cb(ctx, OSSL_CMP_CTX_server_perform)
     74  1.1.1.2  christos         || !OSSL_CMP_CTX_set_transfer_cb_arg(ctx, fixture->srv_ctx)
     75  1.1.1.2  christos         || !OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_SEND, 1)
     76  1.1.1.2  christos         || !OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS, 1)
     77  1.1.1.2  christos         || !OSSL_CMP_CTX_set1_oldCert(ctx, client_cert)
     78  1.1.1.2  christos         || !OSSL_CMP_CTX_set1_pkey(ctx, client_key)
     79  1.1.1.2  christos         /* client_key is by default used also for newPkey */
     80  1.1.1.2  christos         || !OSSL_CMP_CTX_set1_srvCert(ctx, server_cert)
     81  1.1.1.2  christos         || !OSSL_CMP_CTX_set1_referenceValue(ctx, ref, sizeof(ref)))
     82      1.1  christos         goto err;
     83      1.1  christos     fixture->req_type = -1;
     84      1.1  christos     return fixture;
     85      1.1  christos 
     86  1.1.1.2  christos err:
     87      1.1  christos     tear_down(fixture);
     88      1.1  christos     return NULL;
     89      1.1  christos }
     90      1.1  christos 
     91      1.1  christos static int execute_exec_RR_ses_test(CMP_SES_TEST_FIXTURE *fixt)
     92      1.1  christos {
     93      1.1  christos     return TEST_int_eq(OSSL_CMP_CTX_get_status(fixt->cmp_ctx),
     94  1.1.1.2  christos                OSSL_CMP_PKISTATUS_unspecified)
     95      1.1  christos         && TEST_int_eq(OSSL_CMP_exec_RR_ses(fixt->cmp_ctx),
     96  1.1.1.2  christos             fixt->expected == OSSL_CMP_PKISTATUS_accepted)
     97      1.1  christos         && TEST_int_eq(OSSL_CMP_CTX_get_status(fixt->cmp_ctx), fixt->expected);
     98      1.1  christos }
     99      1.1  christos 
    100      1.1  christos static int execute_exec_GENM_ses_test_single(CMP_SES_TEST_FIXTURE *fixture)
    101      1.1  christos {
    102      1.1  christos     OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
    103      1.1  christos     ASN1_OBJECT *type = OBJ_txt2obj("1.3.6.1.5.5.7.4.2", 1);
    104      1.1  christos     OSSL_CMP_ITAV *itav = OSSL_CMP_ITAV_create(type, NULL);
    105      1.1  christos     STACK_OF(OSSL_CMP_ITAV) *itavs;
    106      1.1  christos 
    107      1.1  christos     OSSL_CMP_CTX_push0_genm_ITAV(ctx, itav);
    108      1.1  christos     itavs = OSSL_CMP_exec_GENM_ses(ctx);
    109      1.1  christos 
    110      1.1  christos     sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
    111      1.1  christos     return TEST_int_eq(OSSL_CMP_CTX_get_status(ctx), fixture->expected)
    112  1.1.1.2  christos             && fixture->expected == OSSL_CMP_PKISTATUS_accepted
    113  1.1.1.2  christos         ? TEST_ptr(itavs)
    114  1.1.1.2  christos         : TEST_ptr_null(itavs);
    115      1.1  christos }
    116      1.1  christos 
    117      1.1  christos static int execute_exec_GENM_ses_test(CMP_SES_TEST_FIXTURE *fixture)
    118      1.1  christos {
    119      1.1  christos     return execute_exec_GENM_ses_test_single(fixture)
    120      1.1  christos         && OSSL_CMP_CTX_reinit(fixture->cmp_ctx)
    121      1.1  christos         && execute_exec_GENM_ses_test_single(fixture);
    122      1.1  christos }
    123      1.1  christos 
    124      1.1  christos static int execute_exec_certrequest_ses_test(CMP_SES_TEST_FIXTURE *fixture)
    125      1.1  christos {
    126      1.1  christos     OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
    127      1.1  christos     X509 *res = OSSL_CMP_exec_certreq(ctx, fixture->req_type, NULL);
    128      1.1  christos     int status = OSSL_CMP_CTX_get_status(ctx);
    129      1.1  christos 
    130      1.1  christos     OSSL_CMP_CTX_print_errors(ctx);
    131      1.1  christos     if (!TEST_int_eq(status, fixture->expected)
    132      1.1  christos         && !(fixture->expected == OSSL_CMP_PKISTATUS_waiting
    133  1.1.1.2  christos             && TEST_int_eq(status, OSSL_CMP_PKISTATUS_trans)))
    134      1.1  christos         return 0;
    135      1.1  christos     if (fixture->expected != OSSL_CMP_PKISTATUS_accepted)
    136      1.1  christos         return TEST_ptr_null(res);
    137      1.1  christos 
    138      1.1  christos     if (!TEST_ptr(res) || !TEST_int_eq(X509_cmp(res, client_cert), 0))
    139      1.1  christos         return 0;
    140      1.1  christos     if (fixture->caPubs != NULL) {
    141      1.1  christos         STACK_OF(X509) *caPubs = OSSL_CMP_CTX_get1_caPubs(fixture->cmp_ctx);
    142      1.1  christos         int ret = TEST_int_eq(STACK_OF_X509_cmp(fixture->caPubs, caPubs), 0);
    143      1.1  christos 
    144      1.1  christos         OSSL_STACK_OF_X509_free(caPubs);
    145      1.1  christos         return ret;
    146      1.1  christos     }
    147      1.1  christos     return 1;
    148      1.1  christos }
    149      1.1  christos 
    150      1.1  christos static int test_exec_RR_ses(int request_error)
    151      1.1  christos {
    152      1.1  christos     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
    153      1.1  christos     if (request_error)
    154      1.1  christos         OSSL_CMP_CTX_set1_oldCert(fixture->cmp_ctx, NULL);
    155      1.1  christos     fixture->expected = request_error ? OSSL_CMP_PKISTATUS_request
    156  1.1.1.2  christos                                       : OSSL_CMP_PKISTATUS_accepted;
    157      1.1  christos     EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
    158      1.1  christos     return result;
    159      1.1  christos }
    160      1.1  christos 
    161      1.1  christos static int test_exec_RR_ses_ok(void)
    162      1.1  christos {
    163      1.1  christos     return test_exec_RR_ses(0);
    164      1.1  christos }
    165      1.1  christos 
    166      1.1  christos static int test_exec_RR_ses_request_error(void)
    167      1.1  christos {
    168      1.1  christos     return test_exec_RR_ses(1);
    169      1.1  christos }
    170      1.1  christos 
    171      1.1  christos static int test_exec_RR_ses_receive_error(void)
    172      1.1  christos {
    173      1.1  christos     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
    174      1.1  christos     ossl_cmp_mock_srv_set_statusInfo(fixture->srv_ctx,
    175  1.1.1.2  christos         OSSL_CMP_PKISTATUS_rejection,
    176  1.1.1.2  christos         OSSL_CMP_CTX_FAILINFO_signerNotTrusted,
    177  1.1.1.2  christos         "test string");
    178      1.1  christos     ossl_cmp_mock_srv_set_sendError(fixture->srv_ctx, OSSL_CMP_PKIBODY_RR);
    179      1.1  christos     fixture->expected = OSSL_CMP_PKISTATUS_rejection;
    180      1.1  christos     EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
    181      1.1  christos     return result;
    182      1.1  christos }
    183      1.1  christos 
    184      1.1  christos static int test_exec_IR_ses(void)
    185      1.1  christos {
    186      1.1  christos     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
    187      1.1  christos     fixture->req_type = OSSL_CMP_PKIBODY_IR;
    188      1.1  christos     fixture->expected = OSSL_CMP_PKISTATUS_accepted;
    189      1.1  christos     fixture->caPubs = sk_X509_new_null();
    190      1.1  christos     if (!sk_X509_push(fixture->caPubs, server_cert)
    191      1.1  christos         || !sk_X509_push(fixture->caPubs, server_cert)) {
    192      1.1  christos         tear_down(fixture);
    193      1.1  christos         return 0;
    194      1.1  christos     }
    195      1.1  christos     ossl_cmp_mock_srv_set1_caPubsOut(fixture->srv_ctx, fixture->caPubs);
    196      1.1  christos     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
    197      1.1  christos     return result;
    198      1.1  christos }
    199      1.1  christos 
    200      1.1  christos static int test_exec_REQ_ses_poll(int req_type, int check_after,
    201  1.1.1.2  christos     int poll_count, int total_timeout,
    202  1.1.1.2  christos     int expect)
    203      1.1  christos {
    204      1.1  christos     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
    205      1.1  christos     fixture->req_type = req_type;
    206      1.1  christos     fixture->expected = expect;
    207      1.1  christos     ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, check_after);
    208      1.1  christos     ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, poll_count);
    209      1.1  christos     OSSL_CMP_CTX_set_option(fixture->cmp_ctx,
    210  1.1.1.2  christos         OSSL_CMP_OPT_TOTAL_TIMEOUT, total_timeout);
    211      1.1  christos 
    212      1.1  christos     if (req_type == OSSL_CMP_PKIBODY_IR) {
    213      1.1  christos         EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
    214      1.1  christos     } else if (req_type == OSSL_CMP_PKIBODY_GENM) {
    215      1.1  christos         EXECUTE_TEST(execute_exec_GENM_ses_test, tear_down);
    216      1.1  christos     }
    217      1.1  christos     return result;
    218      1.1  christos }
    219      1.1  christos 
    220      1.1  christos static int checkAfter = 1;
    221      1.1  christos static int test_exec_IR_ses_poll_ok(void)
    222      1.1  christos {
    223      1.1  christos     return test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_IR, checkAfter, 2, 0,
    224  1.1.1.2  christos         OSSL_CMP_PKISTATUS_accepted);
    225      1.1  christos }
    226      1.1  christos 
    227      1.1  christos static int test_exec_IR_ses_poll_no_timeout(void)
    228      1.1  christos {
    229      1.1  christos     return test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_IR, checkAfter,
    230  1.1.1.2  christos         2 /* pollCount */,
    231  1.1.1.2  christos         checkAfter + 14, /* usually 4 is sufficient */
    232  1.1.1.2  christos         OSSL_CMP_PKISTATUS_accepted);
    233      1.1  christos }
    234      1.1  christos 
    235      1.1  christos static int test_exec_IR_ses_poll_total_timeout(void)
    236      1.1  christos {
    237      1.1  christos     return !test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_IR, checkAfter + 1,
    238  1.1.1.2  christos         3 /* pollCount */, checkAfter + 6,
    239  1.1.1.2  christos         OSSL_CMP_PKISTATUS_waiting);
    240      1.1  christos }
    241      1.1  christos 
    242      1.1  christos static int test_exec_CR_ses(int implicit_confirm, int granted, int reject)
    243      1.1  christos {
    244      1.1  christos     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
    245      1.1  christos     fixture->req_type = OSSL_CMP_PKIBODY_CR;
    246      1.1  christos     OSSL_CMP_CTX_set_option(fixture->cmp_ctx,
    247  1.1.1.2  christos         OSSL_CMP_OPT_IMPLICIT_CONFIRM, implicit_confirm);
    248      1.1  christos     OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(fixture->srv_ctx, granted);
    249      1.1  christos     ossl_cmp_mock_srv_set_sendError(fixture->srv_ctx,
    250  1.1.1.2  christos         reject ? OSSL_CMP_PKIBODY_CERTCONF : -1);
    251      1.1  christos     fixture->expected = reject ? OSSL_CMP_PKISTATUS_rejection
    252  1.1.1.2  christos                                : OSSL_CMP_PKISTATUS_accepted;
    253      1.1  christos     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
    254      1.1  christos     return result;
    255      1.1  christos }
    256      1.1  christos 
    257      1.1  christos static int test_exec_CR_ses_explicit_confirm(void)
    258      1.1  christos {
    259      1.1  christos     return test_exec_CR_ses(0, 0, 0)
    260      1.1  christos         && test_exec_CR_ses(0, 0, 1 /* reject */);
    261      1.1  christos }
    262      1.1  christos 
    263      1.1  christos static int test_exec_CR_ses_implicit_confirm(void)
    264      1.1  christos {
    265      1.1  christos     return test_exec_CR_ses(1, 0, 0)
    266      1.1  christos         && test_exec_CR_ses(1, 1 /* granted */, 0);
    267      1.1  christos }
    268      1.1  christos 
    269      1.1  christos static int test_exec_KUR_ses(int transfer_error, int pubkey, int raverified)
    270      1.1  christos {
    271      1.1  christos     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
    272      1.1  christos     fixture->req_type = OSSL_CMP_PKIBODY_KUR;
    273      1.1  christos     /* ctx->oldCert has already been set */
    274      1.1  christos 
    275      1.1  christos     if (transfer_error)
    276      1.1  christos         OSSL_CMP_CTX_set_transfer_cb_arg(fixture->cmp_ctx, NULL);
    277      1.1  christos     if (pubkey) {
    278      1.1  christos         EVP_PKEY *key = raverified /* wrong key */ ? server_key : client_key;
    279      1.1  christos 
    280      1.1  christos         if (!EVP_PKEY_up_ref(key))
    281      1.1  christos             return 0;
    282      1.1  christos 
    283      1.1  christos         OSSL_CMP_CTX_set0_newPkey(fixture->cmp_ctx, 0 /* not priv */, key);
    284      1.1  christos         OSSL_CMP_SRV_CTX_set_accept_raverified(fixture->srv_ctx, 1);
    285      1.1  christos     }
    286      1.1  christos     if (pubkey || raverified)
    287      1.1  christos         OSSL_CMP_CTX_set_option(fixture->cmp_ctx, OSSL_CMP_OPT_POPO_METHOD,
    288  1.1.1.2  christos             OSSL_CRMF_POPO_RAVERIFIED);
    289  1.1.1.2  christos     fixture->expected = transfer_error ? OSSL_CMP_PKISTATUS_trans : raverified ? OSSL_CMP_PKISTATUS_rejection
    290  1.1.1.2  christos                                                                                : OSSL_CMP_PKISTATUS_accepted;
    291      1.1  christos     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
    292      1.1  christos     return result;
    293      1.1  christos }
    294      1.1  christos 
    295      1.1  christos static int test_exec_KUR_ses_ok(void)
    296      1.1  christos {
    297      1.1  christos     return test_exec_KUR_ses(0, 0, 0);
    298      1.1  christos }
    299      1.1  christos 
    300      1.1  christos static int test_exec_KUR_ses_transfer_error(void)
    301      1.1  christos {
    302      1.1  christos     return test_exec_KUR_ses(1, 0, 0);
    303      1.1  christos }
    304      1.1  christos 
    305      1.1  christos static int test_exec_KUR_ses_wrong_popo(void)
    306      1.1  christos {
    307      1.1  christos #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION /* cf ossl_cmp_verify_popo() */
    308      1.1  christos     return test_exec_KUR_ses(0, 0, 1);
    309      1.1  christos #else
    310      1.1  christos     return 1;
    311      1.1  christos #endif
    312      1.1  christos }
    313      1.1  christos 
    314      1.1  christos static int test_exec_KUR_ses_pub(void)
    315      1.1  christos {
    316      1.1  christos     return test_exec_KUR_ses(0, 1, 0);
    317      1.1  christos }
    318      1.1  christos 
    319      1.1  christos static int test_exec_KUR_ses_wrong_pub(void)
    320      1.1  christos {
    321      1.1  christos     return test_exec_KUR_ses(0, 1, 1);
    322      1.1  christos }
    323      1.1  christos 
    324      1.1  christos static int test_certConf_cb(OSSL_CMP_CTX *ctx, X509 *cert, int fail_info,
    325  1.1.1.2  christos     const char **txt)
    326      1.1  christos {
    327      1.1  christos     int *reject = OSSL_CMP_CTX_get_certConf_cb_arg(ctx);
    328      1.1  christos 
    329      1.1  christos     if (*reject) {
    330      1.1  christos         *txt = "not to my taste";
    331      1.1  christos         fail_info = OSSL_CMP_PKIFAILUREINFO_badCertTemplate;
    332      1.1  christos     }
    333      1.1  christos     return fail_info;
    334      1.1  christos }
    335      1.1  christos 
    336      1.1  christos static int test_exec_P10CR_ses(int reject)
    337      1.1  christos {
    338      1.1  christos     OSSL_CMP_CTX *ctx;
    339      1.1  christos     X509_REQ *csr = NULL;
    340      1.1  christos 
    341      1.1  christos     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
    342      1.1  christos     fixture->req_type = OSSL_CMP_PKIBODY_P10CR;
    343      1.1  christos     fixture->expected = reject ? OSSL_CMP_PKISTATUS_rejection
    344  1.1.1.2  christos                                : OSSL_CMP_PKISTATUS_accepted;
    345      1.1  christos     ctx = fixture->cmp_ctx;
    346      1.1  christos     if (!TEST_ptr(csr = load_csr_der(pkcs10_f, libctx))
    347      1.1  christos         || !TEST_true(OSSL_CMP_CTX_set1_p10CSR(ctx, csr))
    348      1.1  christos         || !TEST_true(OSSL_CMP_CTX_set_certConf_cb(ctx, test_certConf_cb))
    349      1.1  christos         || !TEST_true(OSSL_CMP_CTX_set_certConf_cb_arg(ctx, &reject))) {
    350      1.1  christos         tear_down(fixture);
    351      1.1  christos         fixture = NULL;
    352      1.1  christos     }
    353      1.1  christos     X509_REQ_free(csr);
    354      1.1  christos     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
    355      1.1  christos     return result;
    356      1.1  christos }
    357      1.1  christos 
    358      1.1  christos static int test_exec_P10CR_ses_ok(void)
    359      1.1  christos {
    360      1.1  christos     return test_exec_P10CR_ses(0);
    361      1.1  christos }
    362      1.1  christos 
    363      1.1  christos static int test_exec_P10CR_ses_reject(void)
    364      1.1  christos {
    365      1.1  christos     return test_exec_P10CR_ses(1);
    366      1.1  christos }
    367      1.1  christos 
    368      1.1  christos static int execute_try_certreq_poll_test(CMP_SES_TEST_FIXTURE *fixture)
    369      1.1  christos {
    370      1.1  christos     OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
    371      1.1  christos     int check_after;
    372      1.1  christos     const int CHECK_AFTER = 0;
    373      1.1  christos     const int TYPE = OSSL_CMP_PKIBODY_KUR;
    374      1.1  christos 
    375      1.1  christos     ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
    376      1.1  christos     ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
    377      1.1  christos     return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
    378      1.1  christos         && check_after == CHECK_AFTER
    379      1.1  christos         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
    380      1.1  christos         && TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
    381      1.1  christos         && check_after == CHECK_AFTER
    382      1.1  christos         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
    383      1.1  christos         && TEST_int_eq(fixture->expected,
    384  1.1.1.2  christos             OSSL_CMP_try_certreq(ctx, TYPE, NULL, NULL))
    385      1.1  christos         && TEST_int_eq(0,
    386  1.1.1.2  christos             X509_cmp(OSSL_CMP_CTX_get0_newCert(ctx), client_cert));
    387      1.1  christos }
    388      1.1  christos 
    389      1.1  christos static int test_try_certreq_poll(void)
    390      1.1  christos {
    391      1.1  christos     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
    392      1.1  christos     fixture->expected = 1;
    393      1.1  christos     EXECUTE_TEST(execute_try_certreq_poll_test, tear_down);
    394      1.1  christos     return result;
    395      1.1  christos }
    396      1.1  christos 
    397      1.1  christos static int execute_try_certreq_poll_abort_test(CMP_SES_TEST_FIXTURE *fixture)
    398      1.1  christos {
    399      1.1  christos     OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
    400      1.1  christos     int check_after;
    401      1.1  christos     const int CHECK_AFTER = 99;
    402      1.1  christos     const int TYPE = OSSL_CMP_PKIBODY_CR;
    403      1.1  christos 
    404      1.1  christos     ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
    405      1.1  christos     ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
    406      1.1  christos     return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
    407      1.1  christos         && check_after == CHECK_AFTER
    408      1.1  christos         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
    409      1.1  christos         && TEST_int_eq(fixture->expected,
    410  1.1.1.2  christos             OSSL_CMP_try_certreq(ctx, -1 /* abort */, NULL, NULL))
    411      1.1  christos         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(fixture->cmp_ctx), NULL);
    412      1.1  christos }
    413      1.1  christos 
    414      1.1  christos static int test_try_certreq_poll_abort(void)
    415      1.1  christos {
    416      1.1  christos     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
    417      1.1  christos     fixture->expected = 1;
    418      1.1  christos     EXECUTE_TEST(execute_try_certreq_poll_abort_test, tear_down);
    419      1.1  christos     return result;
    420      1.1  christos }
    421      1.1  christos 
    422      1.1  christos static int test_exec_GENM_ses_poll_ok(void)
    423      1.1  christos {
    424      1.1  christos     return test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_GENM, checkAfter, 2, 0,
    425  1.1.1.2  christos         OSSL_CMP_PKISTATUS_accepted);
    426      1.1  christos }
    427      1.1  christos 
    428      1.1  christos static int test_exec_GENM_ses_poll_no_timeout(void)
    429      1.1  christos {
    430      1.1  christos     return test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_GENM, checkAfter,
    431  1.1.1.2  christos         1 /* pollCount */, checkAfter + 1,
    432  1.1.1.2  christos         OSSL_CMP_PKISTATUS_accepted);
    433      1.1  christos }
    434      1.1  christos 
    435      1.1  christos static int test_exec_GENM_ses_poll_total_timeout(void)
    436      1.1  christos {
    437      1.1  christos     return test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_GENM, checkAfter + 1,
    438  1.1.1.2  christos         3 /* pollCount */, checkAfter + 2,
    439  1.1.1.2  christos         OSSL_CMP_PKISTATUS_waiting);
    440      1.1  christos }
    441      1.1  christos 
    442      1.1  christos static int test_exec_GENM_ses(int transfer_error, int total_timeout, int expect)
    443      1.1  christos {
    444      1.1  christos     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
    445      1.1  christos     if (transfer_error)
    446      1.1  christos         OSSL_CMP_CTX_set_transfer_cb_arg(fixture->cmp_ctx, NULL);
    447      1.1  christos     /*
    448      1.1  christos      * cannot use OSSL_CMP_CTX_set_option(... OSSL_CMP_OPT_TOTAL_TIMEOUT)
    449      1.1  christos      * here because this will correct total_timeout to be >= 0
    450      1.1  christos      */
    451      1.1  christos     fixture->cmp_ctx->total_timeout = total_timeout;
    452      1.1  christos     fixture->expected = expect;
    453      1.1  christos     EXECUTE_TEST(execute_exec_GENM_ses_test, tear_down);
    454      1.1  christos     return result;
    455      1.1  christos }
    456      1.1  christos 
    457      1.1  christos static int test_exec_GENM_ses_ok(void)
    458      1.1  christos {
    459      1.1  christos     return test_exec_GENM_ses(0, 0, OSSL_CMP_PKISTATUS_accepted);
    460      1.1  christos }
    461      1.1  christos 
    462      1.1  christos static int test_exec_GENM_ses_transfer_error(void)
    463      1.1  christos {
    464      1.1  christos     return test_exec_GENM_ses(1, 0, OSSL_CMP_PKISTATUS_trans);
    465      1.1  christos }
    466      1.1  christos 
    467      1.1  christos static int test_exec_GENM_ses_total_timeout(void)
    468      1.1  christos {
    469      1.1  christos     return test_exec_GENM_ses(0, -1, OSSL_CMP_PKISTATUS_trans);
    470      1.1  christos }
    471      1.1  christos 
    472      1.1  christos static int execute_exchange_certConf_test(CMP_SES_TEST_FIXTURE *fixture)
    473      1.1  christos {
    474  1.1.1.2  christos     int res = ossl_cmp_exchange_certConf(fixture->cmp_ctx, OSSL_CMP_CERTREQID,
    475  1.1.1.2  christos         OSSL_CMP_PKIFAILUREINFO_addInfoNotAvailable,
    476  1.1.1.2  christos         "abcdefg");
    477      1.1  christos 
    478      1.1  christos     return TEST_int_eq(fixture->expected, res);
    479      1.1  christos }
    480      1.1  christos 
    481      1.1  christos static int execute_exchange_error_test(CMP_SES_TEST_FIXTURE *fixture)
    482      1.1  christos {
    483  1.1.1.2  christos     int res = ossl_cmp_exchange_error(fixture->cmp_ctx,
    484  1.1.1.2  christos         OSSL_CMP_PKISTATUS_rejection,
    485  1.1.1.2  christos         1 << OSSL_CMP_PKIFAILUREINFO_unsupportedVersion,
    486  1.1.1.2  christos         "foo_status", 999, "foo_details");
    487      1.1  christos 
    488      1.1  christos     return TEST_int_eq(fixture->expected, res);
    489      1.1  christos }
    490      1.1  christos 
    491      1.1  christos static int test_exchange_certConf(void)
    492      1.1  christos {
    493      1.1  christos     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
    494      1.1  christos     fixture->expected = 0; /* client should not send certConf immediately */
    495      1.1  christos     if (!ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx, X509_dup(client_cert))) {
    496      1.1  christos         tear_down(fixture);
    497      1.1  christos         fixture = NULL;
    498      1.1  christos     }
    499      1.1  christos     EXECUTE_TEST(execute_exchange_certConf_test, tear_down);
    500      1.1  christos     return result;
    501      1.1  christos }
    502      1.1  christos 
    503      1.1  christos static int test_exchange_error(void)
    504      1.1  christos {
    505      1.1  christos     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
    506      1.1  christos     fixture->expected = 1; /* client may send error any time */
    507      1.1  christos     EXECUTE_TEST(execute_exchange_error_test, tear_down);
    508      1.1  christos     return result;
    509      1.1  christos }
    510      1.1  christos 
    511      1.1  christos void cleanup_tests(void)
    512      1.1  christos {
    513      1.1  christos     X509_free(server_cert);
    514      1.1  christos     EVP_PKEY_free(server_key);
    515      1.1  christos     X509_free(client_cert);
    516      1.1  christos     EVP_PKEY_free(client_key);
    517      1.1  christos     OSSL_PROVIDER_unload(default_null_provider);
    518      1.1  christos     OSSL_PROVIDER_unload(provider);
    519      1.1  christos     OSSL_LIB_CTX_free(libctx);
    520      1.1  christos     return;
    521      1.1  christos }
    522      1.1  christos 
    523      1.1  christos #define USAGE "server.key server.crt client.key client.crt client.csr module_name [module_conf_file]\n"
    524      1.1  christos OPT_TEST_DECLARE_USAGE(USAGE)
    525      1.1  christos 
    526      1.1  christos int setup_tests(void)
    527      1.1  christos {
    528      1.1  christos     if (!test_skip_common_options()) {
    529      1.1  christos         TEST_error("Error parsing test options\n");
    530      1.1  christos         return 0;
    531      1.1  christos     }
    532      1.1  christos 
    533      1.1  christos     if (!TEST_ptr(server_key_f = test_get_argument(0))
    534  1.1.1.2  christos         || !TEST_ptr(server_cert_f = test_get_argument(1))
    535  1.1.1.2  christos         || !TEST_ptr(client_key_f = test_get_argument(2))
    536  1.1.1.2  christos         || !TEST_ptr(client_cert_f = test_get_argument(3))
    537  1.1.1.2  christos         || !TEST_ptr(pkcs10_f = test_get_argument(4))) {
    538      1.1  christos         TEST_error("usage: cmp_client_test %s", USAGE);
    539      1.1  christos         return 0;
    540      1.1  christos     }
    541      1.1  christos 
    542      1.1  christos     if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 5, USAGE))
    543      1.1  christos         return 0;
    544      1.1  christos 
    545      1.1  christos     if (!TEST_ptr(server_key = load_pkey_pem(server_key_f, libctx))
    546  1.1.1.2  christos         || !TEST_ptr(server_cert = load_cert_pem(server_cert_f, libctx))
    547  1.1.1.2  christos         || !TEST_ptr(client_key = load_pkey_pem(client_key_f, libctx))
    548  1.1.1.2  christos         || !TEST_ptr(client_cert = load_cert_pem(client_cert_f, libctx))
    549  1.1.1.2  christos         || !TEST_int_eq(1, RAND_bytes_ex(libctx, ref, sizeof(ref), 0))) {
    550      1.1  christos         cleanup_tests();
    551      1.1  christos         return 0;
    552      1.1  christos     }
    553      1.1  christos 
    554      1.1  christos     ADD_TEST(test_exec_RR_ses_ok);
    555      1.1  christos     ADD_TEST(test_exec_RR_ses_request_error);
    556      1.1  christos     ADD_TEST(test_exec_RR_ses_receive_error);
    557      1.1  christos     ADD_TEST(test_exec_CR_ses_explicit_confirm);
    558      1.1  christos     ADD_TEST(test_exec_CR_ses_implicit_confirm);
    559      1.1  christos     ADD_TEST(test_exec_IR_ses);
    560      1.1  christos     ADD_TEST(test_exec_IR_ses_poll_ok);
    561      1.1  christos     ADD_TEST(test_exec_IR_ses_poll_no_timeout);
    562      1.1  christos     ADD_TEST(test_exec_IR_ses_poll_total_timeout);
    563      1.1  christos     ADD_TEST(test_exec_KUR_ses_ok);
    564      1.1  christos     ADD_TEST(test_exec_KUR_ses_transfer_error);
    565      1.1  christos     ADD_TEST(test_exec_KUR_ses_wrong_popo);
    566      1.1  christos     ADD_TEST(test_exec_KUR_ses_pub);
    567      1.1  christos     ADD_TEST(test_exec_KUR_ses_wrong_pub);
    568      1.1  christos     ADD_TEST(test_exec_P10CR_ses_ok);
    569      1.1  christos     ADD_TEST(test_exec_P10CR_ses_reject);
    570      1.1  christos     ADD_TEST(test_try_certreq_poll);
    571      1.1  christos     ADD_TEST(test_try_certreq_poll_abort);
    572      1.1  christos     ADD_TEST(test_exec_GENM_ses_ok);
    573      1.1  christos     ADD_TEST(test_exec_GENM_ses_transfer_error);
    574      1.1  christos     ADD_TEST(test_exec_GENM_ses_total_timeout);
    575      1.1  christos     ADD_TEST(test_exec_GENM_ses_poll_ok);
    576      1.1  christos     ADD_TEST(test_exec_GENM_ses_poll_no_timeout);
    577      1.1  christos     ADD_TEST(test_exec_GENM_ses_poll_total_timeout);
    578      1.1  christos     ADD_TEST(test_exchange_certConf);
    579      1.1  christos     ADD_TEST(test_exchange_error);
    580      1.1  christos     return 1;
    581      1.1  christos }
    582