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