Home | History | Annotate | Line # | Download | only in test
      1      1.1  christos /*
      2  1.1.1.2  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 static const char *newkey_f;
     15      1.1  christos static const char *server_cert_f;
     16      1.1  christos static const char *pkcs10_f;
     17      1.1  christos 
     18      1.1  christos typedef struct test_fixture {
     19      1.1  christos     const char *test_case_name;
     20      1.1  christos     OSSL_CMP_CTX *cmp_ctx;
     21      1.1  christos     /* for msg create tests */
     22      1.1  christos     int bodytype;
     23      1.1  christos     int err_code;
     24      1.1  christos     /* for certConf */
     25      1.1  christos     int fail_info;
     26      1.1  christos     /* for protection tests */
     27      1.1  christos     OSSL_CMP_MSG *msg;
     28      1.1  christos     int expected;
     29      1.1  christos     /* for error and response messages */
     30      1.1  christos     OSSL_CMP_PKISI *si;
     31      1.1  christos } CMP_MSG_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 unsigned char ref[CMP_TEST_REFVALUE_LENGTH];
     37      1.1  christos 
     38      1.1  christos static void tear_down(CMP_MSG_TEST_FIXTURE *fixture)
     39      1.1  christos {
     40      1.1  christos     OSSL_CMP_CTX_free(fixture->cmp_ctx);
     41      1.1  christos     OSSL_CMP_MSG_free(fixture->msg);
     42      1.1  christos     OSSL_CMP_PKISI_free(fixture->si);
     43      1.1  christos     OPENSSL_free(fixture);
     44      1.1  christos }
     45      1.1  christos 
     46      1.1  christos #define SET_OPT_UNPROTECTED_SEND(ctx, val) \
     47      1.1  christos     OSSL_CMP_CTX_set_option((ctx), OSSL_CMP_OPT_UNPROTECTED_SEND, (val))
     48      1.1  christos static CMP_MSG_TEST_FIXTURE *set_up(const char *const test_case_name)
     49      1.1  christos {
     50      1.1  christos     CMP_MSG_TEST_FIXTURE *fixture;
     51      1.1  christos 
     52      1.1  christos     if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
     53      1.1  christos         return NULL;
     54      1.1  christos     fixture->test_case_name = test_case_name;
     55      1.1  christos 
     56      1.1  christos     if (!TEST_ptr(fixture->cmp_ctx = OSSL_CMP_CTX_new(libctx, NULL))
     57      1.1  christos             || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 1))
     58      1.1  christos             || !TEST_true(OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx,
     59      1.1  christos                                                            ref, sizeof(ref)))) {
     60      1.1  christos         tear_down(fixture);
     61      1.1  christos         return NULL;
     62      1.1  christos     }
     63      1.1  christos     return fixture;
     64      1.1  christos }
     65      1.1  christos 
     66      1.1  christos static EVP_PKEY *newkey = NULL;
     67      1.1  christos static X509 *cert = NULL;
     68      1.1  christos 
     69      1.1  christos #define EXECUTE_MSG_CREATION_TEST(expr) \
     70      1.1  christos     do { \
     71      1.1  christos         OSSL_CMP_MSG *msg = NULL; \
     72      1.1  christos         int good = fixture->expected != 0 ? \
     73      1.1  christos             TEST_ptr(msg = (expr)) && TEST_true(valid_asn1_encoding(msg)) : \
     74      1.1  christos             TEST_ptr_null(msg = (expr)); \
     75      1.1  christos  \
     76      1.1  christos         OSSL_CMP_MSG_free(msg); \
     77      1.1  christos         ERR_print_errors_fp(stderr); \
     78      1.1  christos         return good; \
     79      1.1  christos     } while (0)
     80      1.1  christos 
     81      1.1  christos /*-
     82      1.1  christos  * The following tests call a cmp message creation function.
     83      1.1  christos  * if fixture->expected != 0:
     84      1.1  christos  *         returns 1 if the message is created and syntactically correct.
     85      1.1  christos  * if fixture->expected == 0
     86      1.1  christos  *         returns 1 if message creation returns NULL
     87      1.1  christos  */
     88      1.1  christos static int execute_certreq_create_test(CMP_MSG_TEST_FIXTURE *fixture)
     89      1.1  christos {
     90      1.1  christos     EXECUTE_MSG_CREATION_TEST(ossl_cmp_certreq_new(fixture->cmp_ctx,
     91      1.1  christos                                                    fixture->bodytype,
     92      1.1  christos                                                    NULL));
     93      1.1  christos }
     94      1.1  christos 
     95      1.1  christos static int execute_errormsg_create_test(CMP_MSG_TEST_FIXTURE *fixture)
     96      1.1  christos {
     97      1.1  christos     EXECUTE_MSG_CREATION_TEST(ossl_cmp_error_new(fixture->cmp_ctx, fixture->si,
     98      1.1  christos                                                  fixture->err_code,
     99      1.1  christos                                                  "details", 0));
    100      1.1  christos }
    101      1.1  christos 
    102      1.1  christos static int execute_rr_create_test(CMP_MSG_TEST_FIXTURE *fixture)
    103      1.1  christos {
    104      1.1  christos     EXECUTE_MSG_CREATION_TEST(ossl_cmp_rr_new(fixture->cmp_ctx));
    105      1.1  christos }
    106      1.1  christos 
    107      1.1  christos static int execute_certconf_create_test(CMP_MSG_TEST_FIXTURE *fixture)
    108      1.1  christos {
    109      1.1  christos     EXECUTE_MSG_CREATION_TEST(ossl_cmp_certConf_new
    110  1.1.1.2  christos                               (fixture->cmp_ctx, OSSL_CMP_CERTREQID,
    111  1.1.1.2  christos                                fixture->fail_info, NULL));
    112      1.1  christos }
    113      1.1  christos 
    114      1.1  christos static int execute_genm_create_test(CMP_MSG_TEST_FIXTURE *fixture)
    115      1.1  christos {
    116      1.1  christos     EXECUTE_MSG_CREATION_TEST(ossl_cmp_genm_new(fixture->cmp_ctx));
    117      1.1  christos }
    118      1.1  christos 
    119      1.1  christos static int execute_pollreq_create_test(CMP_MSG_TEST_FIXTURE *fixture)
    120      1.1  christos {
    121      1.1  christos     EXECUTE_MSG_CREATION_TEST(ossl_cmp_pollReq_new(fixture->cmp_ctx, 4711));
    122      1.1  christos }
    123      1.1  christos 
    124      1.1  christos static int execute_pkimessage_create_test(CMP_MSG_TEST_FIXTURE *fixture)
    125      1.1  christos {
    126      1.1  christos     EXECUTE_MSG_CREATION_TEST(ossl_cmp_msg_create
    127      1.1  christos                               (fixture->cmp_ctx, fixture->bodytype));
    128      1.1  christos }
    129      1.1  christos 
    130      1.1  christos static int set1_newPkey(OSSL_CMP_CTX *ctx, EVP_PKEY *pkey)
    131      1.1  christos {
    132      1.1  christos     if (!EVP_PKEY_up_ref(pkey))
    133      1.1  christos         return 0;
    134      1.1  christos 
    135      1.1  christos     if (!OSSL_CMP_CTX_set0_newPkey(ctx, 1, pkey)) {
    136      1.1  christos         EVP_PKEY_free(pkey);
    137      1.1  christos         return 0;
    138      1.1  christos     }
    139      1.1  christos     return 1;
    140      1.1  christos }
    141      1.1  christos 
    142      1.1  christos static int test_cmp_create_ir_protection_set(void)
    143      1.1  christos {
    144      1.1  christos     OSSL_CMP_CTX *ctx;
    145      1.1  christos     unsigned char secret[16];
    146      1.1  christos 
    147      1.1  christos     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
    148      1.1  christos 
    149      1.1  christos     ctx = fixture->cmp_ctx;
    150      1.1  christos     fixture->bodytype = OSSL_CMP_PKIBODY_IR;
    151      1.1  christos     fixture->err_code = -1;
    152      1.1  christos     fixture->expected = 1;
    153      1.1  christos     if (!TEST_int_eq(1, RAND_bytes_ex(libctx, secret, sizeof(secret), 0))
    154      1.1  christos             || !TEST_true(SET_OPT_UNPROTECTED_SEND(ctx, 0))
    155      1.1  christos             || !TEST_true(set1_newPkey(ctx, newkey))
    156      1.1  christos             || !TEST_true(OSSL_CMP_CTX_set1_secretValue(ctx, secret,
    157      1.1  christos                                                         sizeof(secret)))) {
    158      1.1  christos         tear_down(fixture);
    159      1.1  christos         fixture = NULL;
    160      1.1  christos     }
    161      1.1  christos     EXECUTE_TEST(execute_certreq_create_test, tear_down);
    162      1.1  christos     return result;
    163      1.1  christos }
    164      1.1  christos 
    165      1.1  christos static int test_cmp_create_ir_protection_fails(void)
    166      1.1  christos {
    167      1.1  christos     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
    168      1.1  christos     fixture->bodytype = OSSL_CMP_PKIBODY_IR;
    169      1.1  christos     fixture->err_code = -1;
    170      1.1  christos     fixture->expected = 0;
    171      1.1  christos     if (!TEST_true(OSSL_CMP_CTX_set1_pkey(fixture->cmp_ctx, newkey))
    172      1.1  christos             || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0))
    173      1.1  christos             /* newkey used by default for signing does not match cert: */
    174      1.1  christos             || !TEST_true(OSSL_CMP_CTX_set1_cert(fixture->cmp_ctx, cert))) {
    175      1.1  christos         tear_down(fixture);
    176      1.1  christos         fixture = NULL;
    177      1.1  christos     }
    178      1.1  christos     EXECUTE_TEST(execute_certreq_create_test, tear_down);
    179      1.1  christos     return result;
    180      1.1  christos }
    181      1.1  christos 
    182      1.1  christos static int test_cmp_create_cr_without_key(void)
    183      1.1  christos {
    184      1.1  christos     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
    185      1.1  christos     fixture->bodytype = OSSL_CMP_PKIBODY_CR;
    186      1.1  christos     fixture->err_code = -1;
    187      1.1  christos     fixture->expected = 0;
    188      1.1  christos     EXECUTE_TEST(execute_certreq_create_test, tear_down);
    189      1.1  christos     return result;
    190      1.1  christos }
    191      1.1  christos 
    192      1.1  christos static int test_cmp_create_cr(void)
    193      1.1  christos {
    194      1.1  christos     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
    195      1.1  christos     fixture->bodytype = OSSL_CMP_PKIBODY_CR;
    196      1.1  christos     fixture->err_code = -1;
    197      1.1  christos     fixture->expected = 1;
    198      1.1  christos     if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
    199      1.1  christos         tear_down(fixture);
    200      1.1  christos         fixture = NULL;
    201      1.1  christos     }
    202      1.1  christos     EXECUTE_TEST(execute_certreq_create_test, tear_down);
    203      1.1  christos     return result;
    204      1.1  christos }
    205      1.1  christos 
    206      1.1  christos static int test_cmp_create_certreq_with_invalid_bodytype(void)
    207      1.1  christos {
    208      1.1  christos     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
    209      1.1  christos     fixture->bodytype = OSSL_CMP_PKIBODY_RR;
    210      1.1  christos     fixture->err_code = -1;
    211      1.1  christos     fixture->expected = 0;
    212      1.1  christos     if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
    213      1.1  christos         tear_down(fixture);
    214      1.1  christos         fixture = NULL;
    215      1.1  christos     }
    216      1.1  christos     EXECUTE_TEST(execute_certreq_create_test, tear_down);
    217      1.1  christos     return result;
    218      1.1  christos }
    219      1.1  christos 
    220      1.1  christos static int test_cmp_create_p10cr(void)
    221      1.1  christos {
    222      1.1  christos     OSSL_CMP_CTX *ctx;
    223      1.1  christos     X509_REQ *p10cr = NULL;
    224      1.1  christos 
    225      1.1  christos     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
    226      1.1  christos     ctx = fixture->cmp_ctx;
    227      1.1  christos     fixture->bodytype = OSSL_CMP_PKIBODY_P10CR;
    228      1.1  christos     fixture->err_code = CMP_R_ERROR_CREATING_CERTREQ;
    229      1.1  christos     fixture->expected = 1;
    230      1.1  christos     if (!TEST_ptr(p10cr = load_csr_der(pkcs10_f, libctx))
    231      1.1  christos             || !TEST_true(set1_newPkey(ctx, newkey))
    232      1.1  christos             || !TEST_true(OSSL_CMP_CTX_set1_p10CSR(ctx, p10cr))) {
    233      1.1  christos         tear_down(fixture);
    234      1.1  christos         fixture = NULL;
    235      1.1  christos     }
    236      1.1  christos     X509_REQ_free(p10cr);
    237      1.1  christos     EXECUTE_TEST(execute_certreq_create_test, tear_down);
    238      1.1  christos     return result;
    239      1.1  christos }
    240      1.1  christos 
    241      1.1  christos static int test_cmp_create_p10cr_null(void)
    242      1.1  christos {
    243      1.1  christos     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
    244      1.1  christos     fixture->bodytype = OSSL_CMP_PKIBODY_P10CR;
    245      1.1  christos     fixture->err_code = CMP_R_ERROR_CREATING_CERTREQ;
    246      1.1  christos     fixture->expected = 0;
    247      1.1  christos     if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
    248      1.1  christos         tear_down(fixture);
    249      1.1  christos         fixture = NULL;
    250      1.1  christos     }
    251      1.1  christos     EXECUTE_TEST(execute_certreq_create_test, tear_down);
    252      1.1  christos     return result;
    253      1.1  christos }
    254      1.1  christos 
    255      1.1  christos static int test_cmp_create_kur(void)
    256      1.1  christos {
    257      1.1  christos     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
    258      1.1  christos     fixture->bodytype = OSSL_CMP_PKIBODY_KUR;
    259      1.1  christos     fixture->err_code = -1;
    260      1.1  christos     fixture->expected = 1;
    261      1.1  christos     if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))
    262      1.1  christos             || !TEST_true(OSSL_CMP_CTX_set1_oldCert(fixture->cmp_ctx, cert))) {
    263      1.1  christos         tear_down(fixture);
    264      1.1  christos         fixture = NULL;
    265      1.1  christos     }
    266      1.1  christos     EXECUTE_TEST(execute_certreq_create_test, tear_down);
    267      1.1  christos     return result;
    268      1.1  christos }
    269      1.1  christos 
    270      1.1  christos static int test_cmp_create_kur_without_oldcert(void)
    271      1.1  christos {
    272      1.1  christos     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
    273      1.1  christos     fixture->bodytype = OSSL_CMP_PKIBODY_KUR;
    274      1.1  christos     fixture->err_code = -1;
    275      1.1  christos     fixture->expected = 0;
    276      1.1  christos     if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
    277      1.1  christos         tear_down(fixture);
    278      1.1  christos         fixture = NULL;
    279      1.1  christos     }
    280      1.1  christos     EXECUTE_TEST(execute_certreq_create_test, tear_down);
    281      1.1  christos     return result;
    282      1.1  christos }
    283      1.1  christos 
    284      1.1  christos static int test_cmp_create_certconf(void)
    285      1.1  christos {
    286      1.1  christos     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
    287      1.1  christos     fixture->fail_info = 0;
    288      1.1  christos     fixture->expected = 1;
    289      1.1  christos     if (!TEST_true(ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx,
    290      1.1  christos                                              X509_dup(cert)))) {
    291      1.1  christos         tear_down(fixture);
    292      1.1  christos         fixture = NULL;
    293      1.1  christos     }
    294      1.1  christos     EXECUTE_TEST(execute_certconf_create_test, tear_down);
    295      1.1  christos     return result;
    296      1.1  christos }
    297      1.1  christos 
    298      1.1  christos static int test_cmp_create_certconf_badAlg(void)
    299      1.1  christos {
    300      1.1  christos     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
    301      1.1  christos     fixture->fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_badAlg;
    302      1.1  christos     fixture->expected = 1;
    303      1.1  christos     if (!TEST_true(ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx,
    304      1.1  christos                                              X509_dup(cert)))) {
    305      1.1  christos         tear_down(fixture);
    306      1.1  christos         fixture = NULL;
    307      1.1  christos     }
    308      1.1  christos     EXECUTE_TEST(execute_certconf_create_test, tear_down);
    309      1.1  christos     return result;
    310      1.1  christos }
    311      1.1  christos 
    312      1.1  christos static int test_cmp_create_certconf_fail_info_max(void)
    313      1.1  christos {
    314      1.1  christos     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
    315      1.1  christos     fixture->fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_MAX;
    316      1.1  christos     fixture->expected = 1;
    317      1.1  christos     if (!TEST_true(ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx,
    318      1.1  christos                                              X509_dup(cert)))) {
    319      1.1  christos         tear_down(fixture);
    320      1.1  christos         fixture = NULL;
    321      1.1  christos     }
    322      1.1  christos     EXECUTE_TEST(execute_certconf_create_test, tear_down);
    323      1.1  christos     return result;
    324      1.1  christos }
    325      1.1  christos 
    326      1.1  christos static int test_cmp_create_error_msg(void)
    327      1.1  christos {
    328      1.1  christos     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
    329      1.1  christos     fixture->si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
    330      1.1  christos                                           OSSL_CMP_PKIFAILUREINFO_systemFailure,
    331      1.1  christos                                           NULL);
    332      1.1  christos     fixture->err_code = -1;
    333      1.1  christos     fixture->expected = 1; /* expected: message creation is successful */
    334      1.1  christos     if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
    335      1.1  christos         tear_down(fixture);
    336      1.1  christos         fixture = NULL;
    337      1.1  christos     }
    338      1.1  christos     EXECUTE_TEST(execute_errormsg_create_test, tear_down);
    339      1.1  christos     return result;
    340      1.1  christos }
    341      1.1  christos 
    342      1.1  christos 
    343      1.1  christos static int test_cmp_create_pollreq(void)
    344      1.1  christos {
    345      1.1  christos     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
    346      1.1  christos     fixture->expected = 1;
    347      1.1  christos     EXECUTE_TEST(execute_pollreq_create_test, tear_down);
    348      1.1  christos     return result;
    349      1.1  christos }
    350      1.1  christos 
    351      1.1  christos static int test_cmp_create_rr(void)
    352      1.1  christos {
    353      1.1  christos     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
    354      1.1  christos     fixture->expected = 1;
    355      1.1  christos     if (!TEST_true(OSSL_CMP_CTX_set1_oldCert(fixture->cmp_ctx, cert))) {
    356      1.1  christos         tear_down(fixture);
    357      1.1  christos         fixture = NULL;
    358      1.1  christos     }
    359      1.1  christos     EXECUTE_TEST(execute_rr_create_test, tear_down);
    360      1.1  christos     return result;
    361      1.1  christos }
    362      1.1  christos 
    363      1.1  christos static int test_cmp_create_genm(void)
    364      1.1  christos {
    365      1.1  christos     OSSL_CMP_ITAV *iv = NULL;
    366      1.1  christos 
    367      1.1  christos     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
    368      1.1  christos     fixture->expected = 1;
    369      1.1  christos     iv = OSSL_CMP_ITAV_create(OBJ_nid2obj(NID_id_it_implicitConfirm), NULL);
    370      1.1  christos     if (!TEST_ptr(iv)
    371      1.1  christos             || !TEST_true(OSSL_CMP_CTX_push0_genm_ITAV(fixture->cmp_ctx, iv))) {
    372      1.1  christos         OSSL_CMP_ITAV_free(iv);
    373      1.1  christos         tear_down(fixture);
    374      1.1  christos         fixture = NULL;
    375      1.1  christos     }
    376      1.1  christos 
    377      1.1  christos     EXECUTE_TEST(execute_genm_create_test, tear_down);
    378      1.1  christos     return result;
    379      1.1  christos }
    380      1.1  christos 
    381      1.1  christos static int execute_certrep_create(CMP_MSG_TEST_FIXTURE *fixture)
    382      1.1  christos {
    383      1.1  christos     OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
    384      1.1  christos     OSSL_CMP_CERTREPMESSAGE *crepmsg = OSSL_CMP_CERTREPMESSAGE_new();
    385      1.1  christos     OSSL_CMP_CERTRESPONSE *read_cresp, *cresp = OSSL_CMP_CERTRESPONSE_new();
    386      1.1  christos     X509 *certfromresp = NULL;
    387      1.1  christos     int res = 0;
    388      1.1  christos 
    389      1.1  christos     if (crepmsg == NULL || cresp == NULL)
    390      1.1  christos         goto err;
    391      1.1  christos     if (!ASN1_INTEGER_set(cresp->certReqId, 99))
    392      1.1  christos         goto err;
    393      1.1  christos     if ((cresp->certifiedKeyPair = OSSL_CMP_CERTIFIEDKEYPAIR_new()) == NULL)
    394      1.1  christos         goto err;
    395      1.1  christos     cresp->certifiedKeyPair->certOrEncCert->type =
    396      1.1  christos         OSSL_CMP_CERTORENCCERT_CERTIFICATE;
    397      1.1  christos     if ((cresp->certifiedKeyPair->certOrEncCert->value.certificate =
    398      1.1  christos          X509_dup(cert)) == NULL
    399      1.1  christos             || !sk_OSSL_CMP_CERTRESPONSE_push(crepmsg->response, cresp))
    400      1.1  christos         goto err;
    401      1.1  christos     cresp = NULL;
    402      1.1  christos     read_cresp = ossl_cmp_certrepmessage_get0_certresponse(crepmsg, 99);
    403      1.1  christos     if (!TEST_ptr(read_cresp))
    404      1.1  christos         goto err;
    405      1.1  christos     if (!TEST_ptr_null(ossl_cmp_certrepmessage_get0_certresponse(crepmsg, 88)))
    406      1.1  christos         goto err;
    407  1.1.1.2  christos     certfromresp = ossl_cmp_certresponse_get1_cert(ctx, read_cresp);
    408      1.1  christos     if (certfromresp == NULL || !TEST_int_eq(X509_cmp(cert, certfromresp), 0))
    409      1.1  christos         goto err;
    410      1.1  christos 
    411      1.1  christos     res = 1;
    412      1.1  christos  err:
    413      1.1  christos     X509_free(certfromresp);
    414      1.1  christos     OSSL_CMP_CERTRESPONSE_free(cresp);
    415      1.1  christos     OSSL_CMP_CERTREPMESSAGE_free(crepmsg);
    416      1.1  christos     return res;
    417      1.1  christos }
    418      1.1  christos 
    419      1.1  christos static int test_cmp_create_certrep(void)
    420      1.1  christos {
    421      1.1  christos     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
    422      1.1  christos     EXECUTE_TEST(execute_certrep_create, tear_down);
    423      1.1  christos     return result;
    424      1.1  christos }
    425      1.1  christos 
    426      1.1  christos 
    427      1.1  christos static int execute_rp_create(CMP_MSG_TEST_FIXTURE *fixture)
    428      1.1  christos {
    429      1.1  christos     OSSL_CMP_PKISI *si = OSSL_CMP_STATUSINFO_new(33, 44, "a text");
    430      1.1  christos     X509_NAME *issuer = X509_NAME_new();
    431      1.1  christos     ASN1_INTEGER *serial = ASN1_INTEGER_new();
    432      1.1  christos     OSSL_CRMF_CERTID *cid = NULL;
    433      1.1  christos     OSSL_CMP_MSG *rpmsg = NULL;
    434      1.1  christos     int res = 0;
    435      1.1  christos 
    436      1.1  christos     if (si == NULL || issuer == NULL || serial == NULL)
    437      1.1  christos         goto err;
    438      1.1  christos 
    439      1.1  christos     if (!X509_NAME_add_entry_by_txt(issuer, "CN", MBSTRING_ASC,
    440      1.1  christos                                     (unsigned char *)"The Issuer", -1, -1, 0)
    441      1.1  christos             || !ASN1_INTEGER_set(serial, 99)
    442      1.1  christos             || (cid = OSSL_CRMF_CERTID_gen(issuer, serial)) == NULL
    443      1.1  christos             || (rpmsg = ossl_cmp_rp_new(fixture->cmp_ctx, si, cid, 1)) == NULL)
    444      1.1  christos         goto err;
    445      1.1  christos 
    446      1.1  christos     if (!TEST_ptr(ossl_cmp_revrepcontent_get_CertId(rpmsg->body->value.rp, 0)))
    447      1.1  christos         goto err;
    448      1.1  christos 
    449      1.1  christos     if (!TEST_ptr(ossl_cmp_revrepcontent_get_pkisi(rpmsg->body->value.rp, 0)))
    450      1.1  christos         goto err;
    451      1.1  christos 
    452      1.1  christos     res = 1;
    453      1.1  christos  err:
    454      1.1  christos     ASN1_INTEGER_free(serial);
    455      1.1  christos     X509_NAME_free(issuer);
    456      1.1  christos     OSSL_CRMF_CERTID_free(cid);
    457      1.1  christos     OSSL_CMP_PKISI_free(si);
    458      1.1  christos     OSSL_CMP_MSG_free(rpmsg);
    459      1.1  christos     return res;
    460      1.1  christos }
    461      1.1  christos 
    462      1.1  christos static int test_cmp_create_rp(void)
    463      1.1  christos {
    464      1.1  christos     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
    465      1.1  christos     EXECUTE_TEST(execute_rp_create, tear_down);
    466      1.1  christos     return result;
    467      1.1  christos }
    468      1.1  christos 
    469      1.1  christos static int execute_pollrep_create(CMP_MSG_TEST_FIXTURE *fixture)
    470      1.1  christos {
    471      1.1  christos     OSSL_CMP_MSG *pollrep;
    472      1.1  christos     int res = 0;
    473      1.1  christos 
    474      1.1  christos     pollrep = ossl_cmp_pollRep_new(fixture->cmp_ctx, 77, 2000);
    475      1.1  christos     if (!TEST_ptr(pollrep))
    476      1.1  christos         return 0;
    477      1.1  christos     if (!TEST_ptr(ossl_cmp_pollrepcontent_get0_pollrep(pollrep->body->
    478      1.1  christos                                                        value.pollRep, 77)))
    479      1.1  christos         goto err;
    480      1.1  christos     if (!TEST_ptr_null(ossl_cmp_pollrepcontent_get0_pollrep(pollrep->body->
    481      1.1  christos                                                             value.pollRep, 88)))
    482      1.1  christos         goto err;
    483      1.1  christos 
    484      1.1  christos     res = 1;
    485      1.1  christos  err:
    486      1.1  christos     OSSL_CMP_MSG_free(pollrep);
    487      1.1  christos     return res;
    488      1.1  christos }
    489      1.1  christos 
    490      1.1  christos static int test_cmp_create_pollrep(void)
    491      1.1  christos {
    492      1.1  christos     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
    493      1.1  christos     EXECUTE_TEST(execute_pollrep_create, tear_down);
    494      1.1  christos     return result;
    495      1.1  christos }
    496      1.1  christos 
    497      1.1  christos static int test_cmp_pkimessage_create(int bodytype)
    498      1.1  christos {
    499      1.1  christos     X509_REQ *p10cr = NULL;
    500      1.1  christos 
    501      1.1  christos     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
    502      1.1  christos 
    503      1.1  christos     switch (fixture->bodytype = bodytype) {
    504      1.1  christos     case OSSL_CMP_PKIBODY_P10CR:
    505      1.1  christos         fixture->expected = 1;
    506      1.1  christos         p10cr = load_csr_der(pkcs10_f, libctx);
    507      1.1  christos         if (!TEST_true(OSSL_CMP_CTX_set1_p10CSR(fixture->cmp_ctx, p10cr))) {
    508      1.1  christos             tear_down(fixture);
    509      1.1  christos             fixture = NULL;
    510      1.1  christos         }
    511      1.1  christos         X509_REQ_free(p10cr);
    512      1.1  christos         break;
    513      1.1  christos     case OSSL_CMP_PKIBODY_IR:
    514      1.1  christos     case OSSL_CMP_PKIBODY_IP:
    515      1.1  christos     case OSSL_CMP_PKIBODY_CR:
    516      1.1  christos     case OSSL_CMP_PKIBODY_CP:
    517      1.1  christos     case OSSL_CMP_PKIBODY_KUR:
    518      1.1  christos     case OSSL_CMP_PKIBODY_KUP:
    519      1.1  christos     case OSSL_CMP_PKIBODY_RR:
    520      1.1  christos     case OSSL_CMP_PKIBODY_RP:
    521      1.1  christos     case OSSL_CMP_PKIBODY_PKICONF:
    522      1.1  christos     case OSSL_CMP_PKIBODY_GENM:
    523      1.1  christos     case OSSL_CMP_PKIBODY_GENP:
    524      1.1  christos     case OSSL_CMP_PKIBODY_ERROR:
    525      1.1  christos     case OSSL_CMP_PKIBODY_CERTCONF:
    526      1.1  christos     case OSSL_CMP_PKIBODY_POLLREQ:
    527      1.1  christos     case OSSL_CMP_PKIBODY_POLLREP:
    528      1.1  christos         fixture->expected = 1;
    529      1.1  christos         break;
    530      1.1  christos     default:
    531      1.1  christos         fixture->expected = 0;
    532      1.1  christos         break;
    533      1.1  christos     }
    534      1.1  christos 
    535      1.1  christos     EXECUTE_TEST(execute_pkimessage_create_test, tear_down);
    536      1.1  christos     return result;
    537      1.1  christos }
    538      1.1  christos 
    539      1.1  christos void cleanup_tests(void)
    540      1.1  christos {
    541      1.1  christos     EVP_PKEY_free(newkey);
    542      1.1  christos     X509_free(cert);
    543  1.1.1.2  christos     OSSL_PROVIDER_unload(default_null_provider);
    544  1.1.1.2  christos     OSSL_PROVIDER_unload(provider);
    545      1.1  christos     OSSL_LIB_CTX_free(libctx);
    546      1.1  christos }
    547      1.1  christos 
    548      1.1  christos #define USAGE "new.key server.crt pkcs10.der module_name [module_conf_file]\n"
    549      1.1  christos OPT_TEST_DECLARE_USAGE(USAGE)
    550      1.1  christos 
    551      1.1  christos int setup_tests(void)
    552      1.1  christos {
    553      1.1  christos     if (!test_skip_common_options()) {
    554      1.1  christos         TEST_error("Error parsing test options\n");
    555      1.1  christos         return 0;
    556      1.1  christos     }
    557      1.1  christos 
    558      1.1  christos     if (!TEST_ptr(newkey_f = test_get_argument(0))
    559      1.1  christos             || !TEST_ptr(server_cert_f = test_get_argument(1))
    560      1.1  christos             || !TEST_ptr(pkcs10_f = test_get_argument(2))) {
    561      1.1  christos         TEST_error("usage: cmp_msg_test %s", USAGE);
    562      1.1  christos         return 0;
    563      1.1  christos     }
    564      1.1  christos 
    565      1.1  christos     if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 3, USAGE))
    566      1.1  christos         return 0;
    567      1.1  christos 
    568      1.1  christos     if (!TEST_ptr(newkey = load_pkey_pem(newkey_f, libctx))
    569      1.1  christos             || !TEST_ptr(cert = load_cert_pem(server_cert_f, libctx))
    570      1.1  christos             || !TEST_int_eq(1, RAND_bytes_ex(libctx, ref, sizeof(ref), 0))) {
    571      1.1  christos         cleanup_tests();
    572      1.1  christos         return 0;
    573      1.1  christos     }
    574      1.1  christos 
    575      1.1  christos     /* Message creation tests */
    576      1.1  christos     ADD_TEST(test_cmp_create_certreq_with_invalid_bodytype);
    577      1.1  christos     ADD_TEST(test_cmp_create_ir_protection_fails);
    578      1.1  christos     ADD_TEST(test_cmp_create_ir_protection_set);
    579      1.1  christos     ADD_TEST(test_cmp_create_error_msg);
    580      1.1  christos     ADD_TEST(test_cmp_create_certconf);
    581      1.1  christos     ADD_TEST(test_cmp_create_certconf_badAlg);
    582      1.1  christos     ADD_TEST(test_cmp_create_certconf_fail_info_max);
    583      1.1  christos     ADD_TEST(test_cmp_create_kur);
    584      1.1  christos     ADD_TEST(test_cmp_create_kur_without_oldcert);
    585      1.1  christos     ADD_TEST(test_cmp_create_cr);
    586      1.1  christos     ADD_TEST(test_cmp_create_cr_without_key);
    587      1.1  christos     ADD_TEST(test_cmp_create_p10cr);
    588      1.1  christos     ADD_TEST(test_cmp_create_p10cr_null);
    589      1.1  christos     ADD_TEST(test_cmp_create_pollreq);
    590      1.1  christos     ADD_TEST(test_cmp_create_rr);
    591      1.1  christos     ADD_TEST(test_cmp_create_rp);
    592      1.1  christos     ADD_TEST(test_cmp_create_genm);
    593      1.1  christos     ADD_TEST(test_cmp_create_certrep);
    594      1.1  christos     ADD_TEST(test_cmp_create_pollrep);
    595      1.1  christos     ADD_ALL_TESTS_NOSUBTEST(test_cmp_pkimessage_create,
    596      1.1  christos                             OSSL_CMP_PKIBODY_POLLREP + 1);
    597      1.1  christos     return 1;
    598      1.1  christos }
    599