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