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 #include "../crypto/crmf/crmf_local.h" /* for manipulating POPO signature */ 14 1.1 christos 15 1.1 christos static const char *server_f; 16 1.1 christos static const char *client_f; 17 1.1 christos static const char *endentity1_f; 18 1.1 christos static const char *endentity2_f; 19 1.1 christos static const char *root_f; 20 1.1 christos static const char *intermediate_f; 21 1.1 christos static const char *ir_protected_f; 22 1.1 christos static const char *ir_unprotected_f; 23 1.1 christos static const char *ir_rmprotection_f; 24 1.1 christos static const char *ip_waiting_f; 25 1.1 christos static const char *instacert_f; 26 1.1 christos static const char *instaca_f; 27 1.1 christos static const char *ir_protected_0_extracerts; 28 1.1 christos static const char *ir_protected_2_extracerts; 29 1.1 christos 30 1.1 christos typedef struct test_fixture { 31 1.1 christos const char *test_case_name; 32 1.1 christos int expected; 33 1.1 christos OSSL_CMP_CTX *cmp_ctx; 34 1.1 christos OSSL_CMP_MSG *msg; 35 1.1 christos X509 *cert; 36 1.1 christos ossl_cmp_allow_unprotected_cb_t allow_unprotected_cb; 37 1.1 christos int additional_arg; 38 1.1 christos } CMP_VFY_TEST_FIXTURE; 39 1.1 christos 40 1.1 christos static OSSL_LIB_CTX *libctx = NULL; 41 1.1 christos static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL; 42 1.1 christos 43 1.1 christos static void tear_down(CMP_VFY_TEST_FIXTURE *fixture) 44 1.1 christos { 45 1.1 christos OSSL_CMP_MSG_free(fixture->msg); 46 1.1 christos OSSL_CMP_CTX_free(fixture->cmp_ctx); 47 1.1 christos OPENSSL_free(fixture); 48 1.1 christos } 49 1.1 christos 50 1.1 christos static time_t test_time_valid = 0, test_time_after_expiration = 0; 51 1.1 christos 52 1.1 christos static CMP_VFY_TEST_FIXTURE *set_up(const char *const test_case_name) 53 1.1 christos { 54 1.1 christos X509_STORE *ts; 55 1.1 christos CMP_VFY_TEST_FIXTURE *fixture; 56 1.1 christos 57 1.1 christos if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture)))) 58 1.1 christos return NULL; 59 1.1 christos 60 1.1 christos ts = X509_STORE_new(); 61 1.1 christos fixture->test_case_name = test_case_name; 62 1.1 christos if (ts == NULL 63 1.1 christos || !TEST_ptr(fixture->cmp_ctx = OSSL_CMP_CTX_new(libctx, NULL)) 64 1.1 christos || !OSSL_CMP_CTX_set0_trustedStore(fixture->cmp_ctx, ts) 65 1.1 christos || !OSSL_CMP_CTX_set_log_cb(fixture->cmp_ctx, print_to_bio_out)) { 66 1.1 christos tear_down(fixture); 67 1.1 christos X509_STORE_free(ts); 68 1.1 christos return NULL; 69 1.1 christos } 70 1.1 christos X509_VERIFY_PARAM_set_time(X509_STORE_get0_param(ts), test_time_valid); 71 1.1 christos X509_STORE_set_verify_cb(ts, X509_STORE_CTX_print_verify_cb); 72 1.1 christos return fixture; 73 1.1 christos } 74 1.1 christos 75 1.1 christos static X509 *srvcert = NULL; 76 1.1 christos static X509 *clcert = NULL; 77 1.1 christos /* chain */ 78 1.1 christos static X509 *endentity1 = NULL, *endentity2 = NULL, 79 1.1 christos *intermediate = NULL, *root = NULL; 80 1.1 christos /* INSTA chain */ 81 1.1 christos static X509 *insta_cert = NULL, *instaca_cert = NULL; 82 1.1 christos 83 1.1 christos static unsigned char rand_data[OSSL_CMP_TRANSACTIONID_LENGTH]; 84 1.1 christos static OSSL_CMP_MSG *ir_unprotected, *ir_rmprotection; 85 1.1 christos 86 1.1 christos /* secret value used for IP_waitingStatus_PBM.der */ 87 1.1 christos static const unsigned char sec_1[] = { 88 1.1 christos '9', 'p', 'p', '8', '-', 'b', '3', '5', 'i', '-', 'X', 'd', '3', 89 1.1 christos 'Q', '-', 'u', 'd', 'N', 'R' 90 1.1 christos }; 91 1.1 christos 92 1.1 christos static int flip_bit(ASN1_BIT_STRING *bitstr) 93 1.1 christos { 94 1.1 christos int bit_num = 7; 95 1.1 christos int bit = ASN1_BIT_STRING_get_bit(bitstr, bit_num); 96 1.1 christos 97 1.1 christos return ASN1_BIT_STRING_set_bit(bitstr, bit_num, !bit); 98 1.1 christos } 99 1.1 christos 100 1.1 christos static int execute_verify_popo_test(CMP_VFY_TEST_FIXTURE *fixture) 101 1.1 christos { 102 1.1 christos if ((fixture->msg = load_pkimsg(ir_protected_f, libctx)) == NULL) 103 1.1 christos return 0; 104 1.1 christos if (fixture->expected == 0) { 105 1.1 christos const OSSL_CRMF_MSGS *reqs = fixture->msg->body->value.ir; 106 1.1 christos const OSSL_CRMF_MSG *req = sk_OSSL_CRMF_MSG_value(reqs, 0); 107 1.1 christos if (req == NULL || !flip_bit(req->popo->value.signature->signature)) 108 1.1 christos return 0; 109 1.1 christos } 110 1.1 christos return TEST_int_eq(fixture->expected, 111 1.1 christos ossl_cmp_verify_popo(fixture->cmp_ctx, fixture->msg, 112 1.1 christos fixture->additional_arg)); 113 1.1 christos } 114 1.1 christos 115 1.1 christos static int test_verify_popo(void) 116 1.1 christos { 117 1.1 christos SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); 118 1.1 christos fixture->expected = 1; 119 1.1 christos EXECUTE_TEST(execute_verify_popo_test, tear_down); 120 1.1 christos return result; 121 1.1 christos } 122 1.1 christos 123 1.1 christos #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 124 1.1 christos static int test_verify_popo_bad(void) 125 1.1 christos { 126 1.1 christos SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); 127 1.1 christos fixture->expected = 0; 128 1.1 christos EXECUTE_TEST(execute_verify_popo_test, tear_down); 129 1.1 christos return result; 130 1.1 christos } 131 1.1 christos #endif 132 1.1 christos 133 1.1 christos static int execute_validate_msg_test(CMP_VFY_TEST_FIXTURE *fixture) 134 1.1 christos { 135 1.1 christos return TEST_int_eq(fixture->expected, 136 1.1 christos ossl_cmp_msg_check_update(fixture->cmp_ctx, fixture->msg, 137 1.1 christos NULL, 0)); 138 1.1 christos } 139 1.1 christos 140 1.1 christos static int execute_validate_cert_path_test(CMP_VFY_TEST_FIXTURE *fixture) 141 1.1 christos { 142 1.1 christos X509_STORE *ts = OSSL_CMP_CTX_get0_trustedStore(fixture->cmp_ctx); 143 1.1 christos int res = TEST_int_eq(fixture->expected, 144 1.1 christos OSSL_CMP_validate_cert_path(fixture->cmp_ctx, 145 1.1 christos ts, fixture->cert)); 146 1.1 christos 147 1.1 christos OSSL_CMP_CTX_print_errors(fixture->cmp_ctx); 148 1.1 christos return res; 149 1.1 christos } 150 1.1 christos 151 1.1 christos static int test_validate_msg_mac_alg_protection(int miss, int wrong) 152 1.1 christos { 153 1.1 christos SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); 154 1.1 christos 155 1.1 christos fixture->expected = !miss && !wrong; 156 1.1 christos if (!TEST_true(miss ? OSSL_CMP_CTX_set0_trustedStore(fixture->cmp_ctx, NULL) 157 1.1 christos : OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx, sec_1, 158 1.1 christos wrong ? 4 : sizeof(sec_1))) 159 1.1 christos || !TEST_ptr(fixture->msg = load_pkimsg(ip_waiting_f, libctx))) { 160 1.1 christos tear_down(fixture); 161 1.1 christos fixture = NULL; 162 1.1 christos } 163 1.1 christos EXECUTE_TEST(execute_validate_msg_test, tear_down); 164 1.1 christos return result; 165 1.1 christos } 166 1.1 christos 167 1.1 christos static int test_validate_msg_mac_alg_protection_ok(void) 168 1.1 christos { 169 1.1 christos return test_validate_msg_mac_alg_protection(0, 0); 170 1.1 christos } 171 1.1 christos 172 1.1 christos static int test_validate_msg_mac_alg_protection_missing(void) 173 1.1 christos { 174 1.1 christos return test_validate_msg_mac_alg_protection(1, 0); 175 1.1 christos } 176 1.1 christos 177 1.1 christos static int test_validate_msg_mac_alg_protection_wrong(void) 178 1.1 christos { 179 1.1 christos return test_validate_msg_mac_alg_protection(0, 1); 180 1.1 christos } 181 1.1 christos 182 1.1 christos #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 183 1.1 christos static int test_validate_msg_mac_alg_protection_bad(void) 184 1.1 christos { 185 1.1 christos const unsigned char sec_bad[] = { 186 1.1 christos '9', 'p', 'p', '8', '-', 'b', '3', '5', 'i', '-', 'X', 'd', '3', 187 1.1 christos 'Q', '-', 'u', 'd', 'N', 'r' 188 1.1 christos }; 189 1.1 christos 190 1.1 christos SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); 191 1.1 christos fixture->expected = 0; 192 1.1 christos 193 1.1 christos if (!TEST_true(OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx, sec_bad, 194 1.1 christos sizeof(sec_bad))) 195 1.1 christos || !TEST_ptr(fixture->msg = load_pkimsg(ip_waiting_f, libctx))) { 196 1.1 christos tear_down(fixture); 197 1.1 christos fixture = NULL; 198 1.1 christos } 199 1.1 christos EXECUTE_TEST(execute_validate_msg_test, tear_down); 200 1.1 christos return result; 201 1.1 christos } 202 1.1 christos #endif 203 1.1 christos 204 1.1 christos static int add_trusted(OSSL_CMP_CTX *ctx, X509 *cert) 205 1.1 christos { 206 1.1 christos return X509_STORE_add_cert(OSSL_CMP_CTX_get0_trustedStore(ctx), cert); 207 1.1 christos } 208 1.1 christos 209 1.1 christos static int add_untrusted(OSSL_CMP_CTX *ctx, X509 *cert) 210 1.1 christos { 211 1.1 christos return X509_add_cert(OSSL_CMP_CTX_get0_untrusted(ctx), cert, 212 1.1 christos X509_ADD_FLAG_UP_REF); 213 1.1 christos } 214 1.1 christos 215 1.1 christos static int test_validate_msg_signature_partial_chain(int expired) 216 1.1 christos { 217 1.1 christos X509_STORE *ts; 218 1.1 christos 219 1.1 christos SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); 220 1.1 christos 221 1.1 christos ts = OSSL_CMP_CTX_get0_trustedStore(fixture->cmp_ctx); 222 1.1 christos fixture->expected = !expired; 223 1.1 christos if (ts == NULL 224 1.1 christos || !TEST_ptr(fixture->msg = load_pkimsg(ir_protected_f, libctx)) 225 1.1 christos || !add_trusted(fixture->cmp_ctx, srvcert)) { 226 1.1 christos tear_down(fixture); 227 1.1 christos fixture = NULL; 228 1.1 christos } else { 229 1.1 christos X509_VERIFY_PARAM *vpm = X509_STORE_get0_param(ts); 230 1.1 christos X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_PARTIAL_CHAIN); 231 1.1 christos if (expired) 232 1.1 christos X509_VERIFY_PARAM_set_time(vpm, test_time_after_expiration); 233 1.1 christos } 234 1.1 christos EXECUTE_TEST(execute_validate_msg_test, tear_down); 235 1.1 christos return result; 236 1.1 christos } 237 1.1 christos 238 1.1 christos static int test_validate_msg_signature_trusted_ok(void) 239 1.1 christos { 240 1.1 christos return test_validate_msg_signature_partial_chain(0); 241 1.1 christos } 242 1.1 christos 243 1.1 christos #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 244 1.1 christos static int test_validate_msg_signature_trusted_expired(void) 245 1.1 christos { 246 1.1 christos return test_validate_msg_signature_partial_chain(1); 247 1.1 christos } 248 1.1 christos #endif 249 1.1 christos 250 1.1 christos static int test_validate_msg_signature_srvcert(int bad_sig, int miss, int wrong) 251 1.1 christos { 252 1.1 christos SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); 253 1.1 christos fixture->cert = srvcert; 254 1.1 christos fixture->expected = !bad_sig && !wrong && !miss; 255 1.1 christos if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_f, libctx)) 256 1.1 christos || !TEST_true(miss ? OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx, 257 1.1 christos sec_1, sizeof(sec_1)) 258 1.1 christos : OSSL_CMP_CTX_set1_srvCert(fixture->cmp_ctx, 259 1.1 christos wrong? clcert : srvcert)) 260 1.1 christos || (bad_sig && !flip_bit(fixture->msg->protection))) { 261 1.1 christos tear_down(fixture); 262 1.1 christos fixture = NULL; 263 1.1 christos } 264 1.1 christos EXECUTE_TEST(execute_validate_msg_test, tear_down); 265 1.1 christos return result; 266 1.1 christos } 267 1.1 christos 268 1.1 christos static int test_validate_msg_signature_srvcert_missing(void) 269 1.1 christos { 270 1.1 christos return test_validate_msg_signature_srvcert(0, 1, 0); 271 1.1 christos } 272 1.1 christos 273 1.1 christos static int test_validate_msg_signature_srvcert_wrong(void) 274 1.1 christos { 275 1.1 christos return test_validate_msg_signature_srvcert(0, 0, 1); 276 1.1 christos } 277 1.1 christos 278 1.1 christos #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 279 1.1 christos static int test_validate_msg_signature_bad(void) 280 1.1 christos { 281 1.1 christos return test_validate_msg_signature_srvcert(1, 0, 0); 282 1.1 christos } 283 1.1 christos #endif 284 1.1 christos 285 1.1 christos static int test_validate_msg_signature_sender_cert_srvcert(void) 286 1.1 christos { 287 1.1 christos return test_validate_msg_signature_srvcert(0, 0, 0); 288 1.1 christos } 289 1.1 christos 290 1.1 christos static int test_validate_msg_signature_sender_cert_untrusted(void) 291 1.1 christos { 292 1.1 christos SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); 293 1.1 christos fixture->expected = 1; 294 1.1 christos if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_0_extracerts, libctx)) 295 1.1 christos || !add_trusted(fixture->cmp_ctx, instaca_cert) 296 1.1 christos || !add_untrusted(fixture->cmp_ctx, insta_cert)) { 297 1.1 christos tear_down(fixture); 298 1.1 christos fixture = NULL; 299 1.1 christos } 300 1.1 christos EXECUTE_TEST(execute_validate_msg_test, tear_down); 301 1.1 christos return result; 302 1.1 christos } 303 1.1 christos 304 1.1 christos static int test_validate_msg_signature_sender_cert_trusted(void) 305 1.1 christos { 306 1.1 christos SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); 307 1.1 christos fixture->expected = 1; 308 1.1 christos if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_0_extracerts, libctx)) 309 1.1 christos || !add_trusted(fixture->cmp_ctx, instaca_cert) 310 1.1 christos || !add_trusted(fixture->cmp_ctx, insta_cert)) { 311 1.1 christos tear_down(fixture); 312 1.1 christos fixture = NULL; 313 1.1 christos } 314 1.1 christos EXECUTE_TEST(execute_validate_msg_test, tear_down); 315 1.1 christos return result; 316 1.1 christos } 317 1.1 christos 318 1.1 christos static int test_validate_msg_signature_sender_cert_extracert(void) 319 1.1 christos { 320 1.1 christos SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); 321 1.1 christos fixture->expected = 1; 322 1.1 christos if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_2_extracerts, libctx)) 323 1.1 christos || !add_trusted(fixture->cmp_ctx, instaca_cert)) { 324 1.1 christos tear_down(fixture); 325 1.1 christos fixture = NULL; 326 1.1 christos } 327 1.1 christos EXECUTE_TEST(execute_validate_msg_test, tear_down); 328 1.1 christos return result; 329 1.1 christos } 330 1.1 christos 331 1.1 christos 332 1.1 christos #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 333 1.1 christos static int test_validate_msg_signature_sender_cert_absent(void) 334 1.1 christos { 335 1.1 christos SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); 336 1.1 christos fixture->expected = 0; 337 1.1 christos if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_0_extracerts, libctx))) { 338 1.1 christos tear_down(fixture); 339 1.1 christos fixture = NULL; 340 1.1 christos } 341 1.1 christos EXECUTE_TEST(execute_validate_msg_test, tear_down); 342 1.1 christos return result; 343 1.1 christos } 344 1.1 christos #endif 345 1.1 christos 346 1.1 christos static int test_validate_with_sender(const X509_NAME *name, int expected) 347 1.1 christos { 348 1.1 christos SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); 349 1.1 christos fixture->expected = expected; 350 1.1 christos if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_f, libctx)) 351 1.1 christos || !TEST_true(OSSL_CMP_CTX_set1_expected_sender(fixture->cmp_ctx, name)) 352 1.1 christos || !TEST_true(OSSL_CMP_CTX_set1_srvCert(fixture->cmp_ctx, srvcert))) { 353 1.1 christos tear_down(fixture); 354 1.1 christos fixture = NULL; 355 1.1 christos } 356 1.1 christos EXECUTE_TEST(execute_validate_msg_test, tear_down); 357 1.1 christos return result; 358 1.1 christos } 359 1.1 christos 360 1.1 christos static int test_validate_msg_signature_expected_sender(void) 361 1.1 christos { 362 1.1 christos return test_validate_with_sender(X509_get_subject_name(srvcert), 1); 363 1.1 christos } 364 1.1 christos 365 1.1 christos static int test_validate_msg_signature_unexpected_sender(void) 366 1.1 christos { 367 1.1 christos return test_validate_with_sender(X509_get_subject_name(root), 0); 368 1.1 christos } 369 1.1 christos 370 1.1 christos #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 371 1.1 christos static int test_validate_msg_unprotected_request(void) 372 1.1 christos { 373 1.1 christos SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); 374 1.1 christos fixture->expected = 0; 375 1.1 christos if (!TEST_ptr(fixture->msg = load_pkimsg(ir_unprotected_f, libctx))) { 376 1.1 christos tear_down(fixture); 377 1.1 christos fixture = NULL; 378 1.1 christos } 379 1.1 christos EXECUTE_TEST(execute_validate_msg_test, tear_down); 380 1.1 christos return result; 381 1.1 christos } 382 1.1 christos #endif 383 1.1 christos 384 1.1 christos static void setup_path(CMP_VFY_TEST_FIXTURE **fixture, X509 *wrong, int expired) 385 1.1 christos { 386 1.1 christos (*fixture)->cert = endentity2; 387 1.1 christos (*fixture)->expected = wrong == NULL && !expired; 388 1.1 christos if (expired) { 389 1.1 christos X509_STORE *ts = OSSL_CMP_CTX_get0_trustedStore((*fixture)->cmp_ctx); 390 1.1 christos X509_VERIFY_PARAM *vpm = X509_STORE_get0_param(ts); 391 1.1 christos X509_VERIFY_PARAM_set_time(vpm, test_time_after_expiration); 392 1.1 christos } 393 1.1 christos if (!add_trusted((*fixture)->cmp_ctx, wrong == NULL ? root : wrong) 394 1.1 christos || !add_untrusted((*fixture)->cmp_ctx, endentity1) 395 1.1 christos || !add_untrusted((*fixture)->cmp_ctx, intermediate)) { 396 1.1 christos tear_down((*fixture)); 397 1.1 christos (*fixture) = NULL; 398 1.1 christos } 399 1.1 christos } 400 1.1 christos 401 1.1 christos static int test_validate_cert_path_ok(void) 402 1.1 christos { 403 1.1 christos SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); 404 1.1 christos setup_path(&fixture, NULL, 0); 405 1.1 christos EXECUTE_TEST(execute_validate_cert_path_test, tear_down); 406 1.1 christos return result; 407 1.1 christos } 408 1.1 christos 409 1.1 christos static int test_validate_cert_path_wrong_anchor(void) 410 1.1 christos { 411 1.1 christos SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); 412 1.1 christos setup_path(&fixture, srvcert /* wrong/non-root cert */, 0); 413 1.1 christos EXECUTE_TEST(execute_validate_cert_path_test, tear_down); 414 1.1 christos return result; 415 1.1 christos } 416 1.1 christos 417 1.1 christos static int test_validate_cert_path_expired(void) 418 1.1 christos { 419 1.1 christos SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); 420 1.1 christos setup_path(&fixture, NULL, 1); 421 1.1 christos EXECUTE_TEST(execute_validate_cert_path_test, tear_down); 422 1.1 christos return result; 423 1.1 christos } 424 1.1 christos 425 1.1 christos static int execute_msg_check_test(CMP_VFY_TEST_FIXTURE *fixture) 426 1.1 christos { 427 1.1 christos const OSSL_CMP_PKIHEADER *hdr = OSSL_CMP_MSG_get0_header(fixture->msg); 428 1.1 christos const ASN1_OCTET_STRING *tid = OSSL_CMP_HDR_get0_transactionID(hdr); 429 1.1 christos 430 1.1 christos if (!TEST_int_eq(fixture->expected, 431 1.1 christos ossl_cmp_msg_check_update(fixture->cmp_ctx, 432 1.1 christos fixture->msg, 433 1.1 christos fixture->allow_unprotected_cb, 434 1.1 christos fixture->additional_arg))) 435 1.1 christos return 0; 436 1.1 christos 437 1.1 christos if (fixture->expected == 0) /* error expected aready during above check */ 438 1.1 christos return 1; 439 1.1 christos return 440 1.1 christos TEST_int_eq(0, 441 1.1 christos ASN1_OCTET_STRING_cmp(ossl_cmp_hdr_get0_senderNonce(hdr), 442 1.1 christos fixture->cmp_ctx->recipNonce)) 443 1.1 christos && TEST_int_eq(0, 444 1.1 christos ASN1_OCTET_STRING_cmp(tid, 445 1.1 christos fixture->cmp_ctx->transactionID)); 446 1.1 christos } 447 1.1 christos 448 1.1 christos static int allow_unprotected(const OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *msg, 449 1.1 christos int invalid_protection, int allow) 450 1.1 christos { 451 1.1 christos return allow; 452 1.1 christos } 453 1.1 christos 454 1.1 christos static void setup_check_update(CMP_VFY_TEST_FIXTURE **fixture, int expected, 455 1.1 christos ossl_cmp_allow_unprotected_cb_t cb, int arg, 456 1.1 christos const unsigned char *trid_data, 457 1.1 christos const unsigned char *nonce_data) 458 1.1 christos { 459 1.1 christos OSSL_CMP_CTX *ctx = (*fixture)->cmp_ctx; 460 1.1 christos int nonce_len = OSSL_CMP_SENDERNONCE_LENGTH; 461 1.1 christos 462 1.1 christos (*fixture)->expected = expected; 463 1.1 christos (*fixture)->allow_unprotected_cb = cb; 464 1.1 christos (*fixture)->additional_arg = arg; 465 1.1 christos (*fixture)->msg = OSSL_CMP_MSG_dup(ir_rmprotection); 466 1.1 christos if ((*fixture)->msg == NULL 467 1.1 christos || (nonce_data != NULL 468 1.1 christos && !ossl_cmp_asn1_octet_string_set1_bytes(&ctx->senderNonce, 469 1.1 christos nonce_data, nonce_len))) { 470 1.1 christos tear_down((*fixture)); 471 1.1 christos (*fixture) = NULL; 472 1.1 christos } else if (trid_data != NULL) { 473 1.1 christos ASN1_OCTET_STRING *trid = ASN1_OCTET_STRING_new(); 474 1.1 christos if (trid == NULL 475 1.1 christos || !ASN1_OCTET_STRING_set(trid, trid_data, 476 1.1 christos OSSL_CMP_TRANSACTIONID_LENGTH) 477 1.1 christos || !OSSL_CMP_CTX_set1_transactionID(ctx, trid)) { 478 1.1 christos tear_down((*fixture)); 479 1.1 christos (*fixture) = NULL; 480 1.1 christos } 481 1.1 christos ASN1_OCTET_STRING_free(trid); 482 1.1 christos } 483 1.1 christos } 484 1.1 christos 485 1.1 christos #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 486 1.1 christos static int test_msg_check_no_protection_no_cb(void) 487 1.1 christos { 488 1.1 christos SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); 489 1.1 christos setup_check_update(&fixture, 0, NULL, 0, NULL, NULL); 490 1.1 christos EXECUTE_TEST(execute_msg_check_test, tear_down); 491 1.1 christos return result; 492 1.1 christos } 493 1.1 christos 494 1.1 christos static int test_msg_check_no_protection_restrictive_cb(void) 495 1.1 christos { 496 1.1 christos SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); 497 1.1 christos setup_check_update(&fixture, 0, allow_unprotected, 0, NULL, NULL); 498 1.1 christos EXECUTE_TEST(execute_msg_check_test, tear_down); 499 1.1 christos return result; 500 1.1 christos } 501 1.1 christos #endif 502 1.1 christos 503 1.1 christos static int test_msg_check_no_protection_permissive_cb(void) 504 1.1 christos { 505 1.1 christos SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); 506 1.1 christos setup_check_update(&fixture, 1, allow_unprotected, 1, NULL, NULL); 507 1.1 christos EXECUTE_TEST(execute_msg_check_test, tear_down); 508 1.1 christos return result; 509 1.1 christos } 510 1.1 christos 511 1.1 christos static int test_msg_check_transaction_id(void) 512 1.1 christos { 513 1.1 christos /* Transaction id belonging to CMP_IR_rmprotection.der */ 514 1.1 christos const unsigned char trans_id[OSSL_CMP_TRANSACTIONID_LENGTH] = { 515 1.1 christos 0x39, 0xB6, 0x90, 0x28, 0xC4, 0xBC, 0x7A, 0xF6, 516 1.1 christos 0xBE, 0xC6, 0x4A, 0x88, 0x97, 0xA6, 0x95, 0x0B 517 1.1 christos }; 518 1.1 christos 519 1.1 christos SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); 520 1.1 christos setup_check_update(&fixture, 1, allow_unprotected, 1, trans_id, NULL); 521 1.1 christos EXECUTE_TEST(execute_msg_check_test, tear_down); 522 1.1 christos return result; 523 1.1 christos } 524 1.1 christos 525 1.1 christos #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 526 1.1 christos static int test_msg_check_transaction_id_bad(void) 527 1.1 christos { 528 1.1 christos SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); 529 1.1 christos setup_check_update(&fixture, 0, allow_unprotected, 1, rand_data, NULL); 530 1.1 christos EXECUTE_TEST(execute_msg_check_test, tear_down); 531 1.1 christos return result; 532 1.1 christos } 533 1.1 christos #endif 534 1.1 christos 535 1.1 christos static int test_msg_check_recipient_nonce(void) 536 1.1 christos { 537 1.1 christos /* Recipient nonce belonging to CMP_IP_ir_rmprotection.der */ 538 1.1 christos const unsigned char rec_nonce[OSSL_CMP_SENDERNONCE_LENGTH] = { 539 1.1 christos 0x48, 0xF1, 0x71, 0x1F, 0xE5, 0xAF, 0x1C, 0x8B, 540 1.1 christos 0x21, 0x97, 0x5C, 0x84, 0x74, 0x49, 0xBA, 0x32 541 1.1 christos }; 542 1.1 christos 543 1.1 christos SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); 544 1.1 christos setup_check_update(&fixture, 1, allow_unprotected, 1, NULL, rec_nonce); 545 1.1 christos EXECUTE_TEST(execute_msg_check_test, tear_down); 546 1.1 christos return result; 547 1.1 christos } 548 1.1 christos 549 1.1 christos #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 550 1.1 christos static int test_msg_check_recipient_nonce_bad(void) 551 1.1 christos { 552 1.1 christos SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up); 553 1.1 christos setup_check_update(&fixture, 0, allow_unprotected, 1, NULL, rand_data); 554 1.1 christos EXECUTE_TEST(execute_msg_check_test, tear_down); 555 1.1 christos return result; 556 1.1 christos } 557 1.1 christos #endif 558 1.1 christos 559 1.1 christos void cleanup_tests(void) 560 1.1 christos { 561 1.1 christos X509_free(srvcert); 562 1.1 christos X509_free(clcert); 563 1.1 christos X509_free(endentity1); 564 1.1 christos X509_free(endentity2); 565 1.1 christos X509_free(intermediate); 566 1.1 christos X509_free(root); 567 1.1 christos X509_free(insta_cert); 568 1.1 christos X509_free(instaca_cert); 569 1.1 christos OSSL_CMP_MSG_free(ir_unprotected); 570 1.1 christos OSSL_CMP_MSG_free(ir_rmprotection); 571 1.1.1.2 christos OSSL_PROVIDER_unload(default_null_provider); 572 1.1.1.2 christos OSSL_PROVIDER_unload(provider); 573 1.1 christos OSSL_LIB_CTX_free(libctx); 574 1.1 christos return; 575 1.1 christos } 576 1.1 christos 577 1.1 christos 578 1.1 christos #define USAGE "server.crt client.crt " \ 579 1.1 christos "EndEntity1.crt EndEntity2.crt " \ 580 1.1 christos "Root_CA.crt Intermediate_CA.crt " \ 581 1.1 christos "CMP_IR_protected.der CMP_IR_unprotected.der " \ 582 1.1 christos "IP_waitingStatus_PBM.der IR_rmprotection.der " \ 583 1.1 christos "insta.cert.pem insta_ca.cert.pem " \ 584 1.1 christos "IR_protected_0_extraCerts.der " \ 585 1.1 christos "IR_protected_2_extraCerts.der module_name [module_conf_file]\n" 586 1.1 christos OPT_TEST_DECLARE_USAGE(USAGE) 587 1.1 christos 588 1.1 christos int setup_tests(void) 589 1.1 christos { 590 1.1 christos /* Set test time stamps */ 591 1.1 christos struct tm ts = { 0 }; 592 1.1 christos 593 1.1 christos ts.tm_year = 2018 - 1900; /* 2018 */ 594 1.1 christos ts.tm_mon = 1; /* February */ 595 1.1 christos ts.tm_mday = 18; /* 18th */ 596 1.1 christos test_time_valid = mktime(&ts); /* February 18th 2018 */ 597 1.1 christos ts.tm_year += 10; /* February 18th 2028 */ 598 1.1 christos test_time_after_expiration = mktime(&ts); 599 1.1 christos 600 1.1 christos if (!test_skip_common_options()) { 601 1.1 christos TEST_error("Error parsing test options\n"); 602 1.1 christos return 0; 603 1.1 christos } 604 1.1 christos 605 1.1 christos RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH); 606 1.1 christos if (!TEST_ptr(server_f = test_get_argument(0)) 607 1.1 christos || !TEST_ptr(client_f = test_get_argument(1)) 608 1.1 christos || !TEST_ptr(endentity1_f = test_get_argument(2)) 609 1.1 christos || !TEST_ptr(endentity2_f = test_get_argument(3)) 610 1.1 christos || !TEST_ptr(root_f = test_get_argument(4)) 611 1.1 christos || !TEST_ptr(intermediate_f = test_get_argument(5)) 612 1.1 christos || !TEST_ptr(ir_protected_f = test_get_argument(6)) 613 1.1 christos || !TEST_ptr(ir_unprotected_f = test_get_argument(7)) 614 1.1 christos || !TEST_ptr(ip_waiting_f = test_get_argument(8)) 615 1.1 christos || !TEST_ptr(ir_rmprotection_f = test_get_argument(9)) 616 1.1 christos || !TEST_ptr(instacert_f = test_get_argument(10)) 617 1.1 christos || !TEST_ptr(instaca_f = test_get_argument(11)) 618 1.1 christos || !TEST_ptr(ir_protected_0_extracerts = test_get_argument(12)) 619 1.1 christos || !TEST_ptr(ir_protected_2_extracerts = test_get_argument(13))) { 620 1.1 christos TEST_error("usage: cmp_vfy_test %s", USAGE); 621 1.1 christos return 0; 622 1.1 christos } 623 1.1 christos 624 1.1 christos if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 14, USAGE)) 625 1.1 christos return 0; 626 1.1 christos 627 1.1 christos /* Load certificates for cert chain */ 628 1.1 christos if (!TEST_ptr(endentity1 = load_cert_pem(endentity1_f, libctx)) 629 1.1 christos || !TEST_ptr(endentity2 = load_cert_pem(endentity2_f, libctx)) 630 1.1 christos || !TEST_ptr(root = load_cert_pem(root_f, NULL)) 631 1.1 christos || !TEST_ptr(intermediate = load_cert_pem(intermediate_f, libctx))) 632 1.1 christos goto err; 633 1.1 christos 634 1.1 christos if (!TEST_ptr(insta_cert = load_cert_pem(instacert_f, libctx)) 635 1.1 christos || !TEST_ptr(instaca_cert = load_cert_pem(instaca_f, libctx))) 636 1.1 christos goto err; 637 1.1 christos 638 1.1 christos /* Load certificates for message validation */ 639 1.1 christos if (!TEST_ptr(srvcert = load_cert_pem(server_f, libctx)) 640 1.1 christos || !TEST_ptr(clcert = load_cert_pem(client_f, libctx))) 641 1.1 christos goto err; 642 1.1 christos if (!TEST_int_eq(1, RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH))) 643 1.1 christos goto err; 644 1.1 christos if (!TEST_ptr(ir_unprotected = load_pkimsg(ir_unprotected_f, libctx)) 645 1.1 christos || !TEST_ptr(ir_rmprotection = load_pkimsg(ir_rmprotection_f, libctx))) 646 1.1 christos goto err; 647 1.1 christos 648 1.1 christos /* Message validation tests */ 649 1.1 christos ADD_TEST(test_verify_popo); 650 1.1 christos #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 651 1.1 christos ADD_TEST(test_verify_popo_bad); 652 1.1 christos #endif 653 1.1 christos ADD_TEST(test_validate_msg_signature_trusted_ok); 654 1.1 christos #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 655 1.1 christos ADD_TEST(test_validate_msg_signature_trusted_expired); 656 1.1 christos ADD_TEST(test_validate_msg_signature_srvcert_missing); 657 1.1 christos #endif 658 1.1 christos ADD_TEST(test_validate_msg_signature_srvcert_wrong); 659 1.1 christos #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 660 1.1 christos ADD_TEST(test_validate_msg_signature_bad); 661 1.1 christos #endif 662 1.1 christos ADD_TEST(test_validate_msg_signature_sender_cert_srvcert); 663 1.1 christos ADD_TEST(test_validate_msg_signature_sender_cert_untrusted); 664 1.1 christos ADD_TEST(test_validate_msg_signature_sender_cert_trusted); 665 1.1 christos ADD_TEST(test_validate_msg_signature_sender_cert_extracert); 666 1.1 christos #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 667 1.1 christos ADD_TEST(test_validate_msg_signature_sender_cert_absent); 668 1.1 christos #endif 669 1.1 christos ADD_TEST(test_validate_msg_signature_expected_sender); 670 1.1 christos ADD_TEST(test_validate_msg_signature_unexpected_sender); 671 1.1 christos #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 672 1.1 christos ADD_TEST(test_validate_msg_unprotected_request); 673 1.1 christos #endif 674 1.1 christos ADD_TEST(test_validate_msg_mac_alg_protection_ok); 675 1.1 christos #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 676 1.1 christos ADD_TEST(test_validate_msg_mac_alg_protection_missing); 677 1.1 christos ADD_TEST(test_validate_msg_mac_alg_protection_wrong); 678 1.1 christos ADD_TEST(test_validate_msg_mac_alg_protection_bad); 679 1.1 christos #endif 680 1.1 christos 681 1.1 christos /* Cert path validation tests */ 682 1.1 christos ADD_TEST(test_validate_cert_path_ok); 683 1.1 christos ADD_TEST(test_validate_cert_path_expired); 684 1.1 christos ADD_TEST(test_validate_cert_path_wrong_anchor); 685 1.1 christos 686 1.1 christos #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 687 1.1 christos ADD_TEST(test_msg_check_no_protection_no_cb); 688 1.1 christos ADD_TEST(test_msg_check_no_protection_restrictive_cb); 689 1.1 christos #endif 690 1.1 christos ADD_TEST(test_msg_check_no_protection_permissive_cb); 691 1.1 christos ADD_TEST(test_msg_check_transaction_id); 692 1.1 christos #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 693 1.1 christos ADD_TEST(test_msg_check_transaction_id_bad); 694 1.1 christos #endif 695 1.1 christos ADD_TEST(test_msg_check_recipient_nonce); 696 1.1 christos #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 697 1.1 christos ADD_TEST(test_msg_check_recipient_nonce_bad); 698 1.1 christos #endif 699 1.1 christos 700 1.1 christos return 1; 701 1.1 christos 702 1.1 christos err: 703 1.1 christos cleanup_tests(); 704 1.1 christos return 0; 705 1.1 christos 706 1.1 christos } 707