1 /* 2 * Copyright 2007-2026 The OpenSSL Project Authors. All Rights Reserved. 3 * Copyright Nokia 2007-2019 4 * Copyright Siemens AG 2015-2019 5 * 6 * Licensed under the Apache License 2.0 (the "License"). You may not use 7 * this file except in compliance with the License. You can obtain a copy 8 * in the file LICENSE in the source distribution or at 9 * https://www.openssl.org/source/license.html 10 */ 11 12 #include "helpers/cmp_testlib.h" 13 14 static unsigned char rand_data[OSSL_CMP_TRANSACTIONID_LENGTH]; 15 16 typedef struct test_fixture { 17 const char *test_case_name; 18 int expected; 19 OSSL_CMP_CTX *cmp_ctx; 20 OSSL_CMP_PKIHEADER *hdr; 21 22 } CMP_HDR_TEST_FIXTURE; 23 24 static void tear_down(CMP_HDR_TEST_FIXTURE *fixture) 25 { 26 OSSL_CMP_PKIHEADER_free(fixture->hdr); 27 OSSL_CMP_CTX_free(fixture->cmp_ctx); 28 OPENSSL_free(fixture); 29 } 30 31 static CMP_HDR_TEST_FIXTURE *set_up(const char *const test_case_name) 32 { 33 CMP_HDR_TEST_FIXTURE *fixture; 34 35 if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture)))) 36 return NULL; 37 fixture->test_case_name = test_case_name; 38 if (!TEST_ptr(fixture->cmp_ctx = OSSL_CMP_CTX_new(NULL, NULL))) 39 goto err; 40 if (!TEST_ptr(fixture->hdr = OSSL_CMP_PKIHEADER_new())) 41 goto err; 42 return fixture; 43 44 err: 45 tear_down(fixture); 46 return NULL; 47 } 48 49 static int execute_HDR_set_get_pvno_test(CMP_HDR_TEST_FIXTURE *fixture) 50 { 51 int pvno = 77; 52 53 if (!TEST_int_eq(ossl_cmp_hdr_set_pvno(fixture->hdr, pvno), 1)) 54 return 0; 55 if (!TEST_int_eq(ossl_cmp_hdr_get_pvno(fixture->hdr), pvno)) 56 return 0; 57 return 1; 58 } 59 60 static int test_HDR_set_get_pvno(void) 61 { 62 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); 63 fixture->expected = 1; 64 EXECUTE_TEST(execute_HDR_set_get_pvno_test, tear_down); 65 return result; 66 } 67 68 #define X509_NAME_ADD(n, rd, s) \ 69 X509_NAME_add_entry_by_txt((n), (rd), MBSTRING_ASC, (unsigned char *)(s), \ 70 -1, -1, 0) 71 72 static int execute_HDR_get0_senderNonce_test(CMP_HDR_TEST_FIXTURE *fixture) 73 { 74 int res = 0; 75 X509_NAME *sender = X509_NAME_new(); 76 ASN1_OCTET_STRING *sn; 77 78 if (!TEST_ptr(sender)) 79 goto err; 80 81 if (!TEST_true(X509_NAME_ADD(sender, "CN", "A common sender name"))) 82 goto err; 83 if (!TEST_int_eq(OSSL_CMP_CTX_set1_subjectName(fixture->cmp_ctx, sender), 84 1)) 85 goto err; 86 if (!TEST_int_eq(ossl_cmp_hdr_init(fixture->cmp_ctx, fixture->hdr), 87 1)) 88 goto err; 89 sn = ossl_cmp_hdr_get0_senderNonce(fixture->hdr); 90 if (!TEST_int_eq(ASN1_OCTET_STRING_cmp(fixture->cmp_ctx->senderNonce, sn), 91 0)) 92 goto err; 93 94 res = 1; 95 err: 96 X509_NAME_free(sender); 97 98 return res; 99 } 100 101 static int test_HDR_get0_senderNonce(void) 102 { 103 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); 104 fixture->expected = 1; 105 EXECUTE_TEST(execute_HDR_get0_senderNonce_test, tear_down); 106 return result; 107 } 108 109 static int execute_HDR_set1_sender_test(CMP_HDR_TEST_FIXTURE *fixture) 110 { 111 int res = 0; 112 X509_NAME *x509name = X509_NAME_new(); 113 114 if (!TEST_ptr(x509name)) 115 goto err; 116 117 if (!TEST_true(X509_NAME_ADD(x509name, "CN", "A common sender name"))) 118 goto err; 119 if (!TEST_int_eq(ossl_cmp_hdr_set1_sender(fixture->hdr, x509name), 1)) 120 goto err; 121 122 if (!TEST_int_eq(fixture->hdr->sender->type, GEN_DIRNAME)) 123 goto err; 124 125 if (!TEST_int_eq(X509_NAME_cmp(fixture->hdr->sender->d.directoryName, 126 x509name), 127 0)) 128 goto err; 129 130 res = 1; 131 err: 132 X509_NAME_free(x509name); 133 134 return res; 135 } 136 137 static int test_HDR_set1_sender(void) 138 { 139 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); 140 fixture->expected = 1; 141 EXECUTE_TEST(execute_HDR_set1_sender_test, tear_down); 142 return result; 143 } 144 145 static int execute_HDR_set1_recipient_test(CMP_HDR_TEST_FIXTURE *fixture) 146 { 147 int res = 0; 148 X509_NAME *x509name = X509_NAME_new(); 149 150 if (!TEST_ptr(x509name)) 151 goto err; 152 153 if (!TEST_true(X509_NAME_ADD(x509name, "CN", "A common recipient name"))) 154 goto err; 155 if (!TEST_int_eq(ossl_cmp_hdr_set1_recipient(fixture->hdr, x509name), 1)) 156 goto err; 157 158 if (!TEST_int_eq(fixture->hdr->recipient->type, GEN_DIRNAME)) 159 goto err; 160 161 if (!TEST_int_eq(X509_NAME_cmp(fixture->hdr->recipient->d.directoryName, 162 x509name), 163 0)) 164 goto err; 165 166 res = 1; 167 err: 168 X509_NAME_free(x509name); 169 170 return res; 171 } 172 173 static int test_HDR_set1_recipient(void) 174 { 175 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); 176 fixture->expected = 1; 177 EXECUTE_TEST(execute_HDR_set1_recipient_test, tear_down); 178 return result; 179 } 180 181 static int execute_HDR_update_messageTime_test(CMP_HDR_TEST_FIXTURE *fixture) 182 { 183 struct tm hdrtm, tmptm; 184 time_t hdrtime, before, after, now; 185 186 now = time(NULL); 187 /* 188 * Trial and error reveals that passing the return value from gmtime 189 * directly to mktime in a mingw 32 bit build gives unexpected results. To 190 * work around this we take a copy of the return value first. 191 */ 192 tmptm = *gmtime(&now); 193 before = mktime(&tmptm); 194 195 if (!TEST_true(ossl_cmp_hdr_update_messageTime(fixture->hdr))) 196 return 0; 197 if (!TEST_true(ASN1_TIME_to_tm(fixture->hdr->messageTime, &hdrtm))) 198 return 0; 199 200 hdrtime = mktime(&hdrtm); 201 202 if (!TEST_time_t_le(before, hdrtime)) 203 return 0; 204 now = time(NULL); 205 tmptm = *gmtime(&now); 206 after = mktime(&tmptm); 207 208 return TEST_time_t_le(hdrtime, after); 209 } 210 211 static int test_HDR_update_messageTime(void) 212 { 213 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); 214 fixture->expected = 1; 215 EXECUTE_TEST(execute_HDR_update_messageTime_test, tear_down); 216 return result; 217 } 218 219 static int execute_HDR_set1_senderKID_test(CMP_HDR_TEST_FIXTURE *fixture) 220 { 221 ASN1_OCTET_STRING *senderKID = ASN1_OCTET_STRING_new(); 222 int res = 0; 223 224 if (!TEST_ptr(senderKID)) 225 goto err; 226 227 if (!TEST_int_eq(ASN1_OCTET_STRING_set(senderKID, rand_data, 228 sizeof(rand_data)), 229 1)) 230 goto err; 231 if (!TEST_int_eq(ossl_cmp_hdr_set1_senderKID(fixture->hdr, senderKID), 1)) 232 goto err; 233 if (!TEST_int_eq(ASN1_OCTET_STRING_cmp(fixture->hdr->senderKID, 234 senderKID), 235 0)) 236 goto err; 237 res = 1; 238 err: 239 ASN1_OCTET_STRING_free(senderKID); 240 return res; 241 } 242 243 static int test_HDR_set1_senderKID(void) 244 { 245 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); 246 fixture->expected = 1; 247 EXECUTE_TEST(execute_HDR_set1_senderKID_test, tear_down); 248 return result; 249 } 250 251 static int execute_HDR_push0_freeText_test(CMP_HDR_TEST_FIXTURE *fixture) 252 { 253 ASN1_UTF8STRING *text = ASN1_UTF8STRING_new(); 254 255 if (!TEST_ptr(text)) 256 return 0; 257 258 if (!ASN1_STRING_set(text, "A free text", -1)) 259 goto err; 260 261 if (!TEST_int_eq(ossl_cmp_hdr_push0_freeText(fixture->hdr, text), 1)) 262 goto err; 263 264 if (!TEST_true(text == sk_ASN1_UTF8STRING_value(fixture->hdr->freeText, 0))) 265 goto err; 266 267 return 1; 268 269 err: 270 ASN1_UTF8STRING_free(text); 271 return 0; 272 } 273 274 static int test_HDR_push0_freeText(void) 275 { 276 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); 277 fixture->expected = 1; 278 EXECUTE_TEST(execute_HDR_push0_freeText_test, tear_down); 279 return result; 280 } 281 282 static int execute_HDR_push1_freeText_test(CMP_HDR_TEST_FIXTURE *fixture) 283 { 284 ASN1_UTF8STRING *text = ASN1_UTF8STRING_new(); 285 ASN1_UTF8STRING *pushed_text; 286 int res = 0; 287 288 if (!TEST_ptr(text)) 289 goto err; 290 291 if (!ASN1_STRING_set(text, "A free text", -1)) 292 goto err; 293 294 if (!TEST_int_eq(ossl_cmp_hdr_push1_freeText(fixture->hdr, text), 1)) 295 goto err; 296 297 pushed_text = sk_ASN1_UTF8STRING_value(fixture->hdr->freeText, 0); 298 if (!TEST_int_eq(ASN1_STRING_cmp(text, pushed_text), 0)) 299 goto err; 300 301 res = 1; 302 err: 303 ASN1_UTF8STRING_free(text); 304 305 return res; 306 } 307 308 static int test_HDR_push1_freeText(void) 309 { 310 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); 311 fixture->expected = 1; 312 EXECUTE_TEST(execute_HDR_push1_freeText_test, tear_down); 313 return result; 314 } 315 316 static int 317 execute_HDR_generalInfo_push0_item_test(CMP_HDR_TEST_FIXTURE *fixture) 318 { 319 OSSL_CMP_ITAV *itav = OSSL_CMP_ITAV_new(); 320 321 if (!TEST_ptr(itav)) 322 return 0; 323 324 if (!TEST_int_eq(ossl_cmp_hdr_generalInfo_push0_item(fixture->hdr, itav), 325 1)) 326 return 0; 327 328 if (!TEST_true(itav == sk_OSSL_CMP_ITAV_value(fixture->hdr->generalInfo, 0))) 329 return 0; 330 331 return 1; 332 } 333 334 static int test_HDR_generalInfo_push0_item(void) 335 { 336 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); 337 fixture->expected = 1; 338 EXECUTE_TEST(execute_HDR_generalInfo_push0_item_test, tear_down); 339 return result; 340 } 341 342 static int 343 execute_HDR_generalInfo_push1_items_test(CMP_HDR_TEST_FIXTURE *fixture) 344 { 345 const char oid[] = "1.2.3.4"; 346 char buf[20]; 347 OSSL_CMP_ITAV *itav, *pushed_itav; 348 STACK_OF(OSSL_CMP_ITAV) *itavs = NULL, *ginfo; 349 ASN1_INTEGER *asn1int = ASN1_INTEGER_new(); 350 ASN1_TYPE *val = ASN1_TYPE_new(); 351 ASN1_TYPE *pushed_val; 352 int res = 0; 353 354 if (!TEST_ptr(asn1int)) 355 return 0; 356 357 if (!TEST_ptr(val) 358 || !TEST_true(ASN1_INTEGER_set(asn1int, 88))) { 359 ASN1_INTEGER_free(asn1int); 360 return 0; 361 } 362 363 ASN1_TYPE_set(val, V_ASN1_INTEGER, asn1int); 364 if (!TEST_ptr(itav = OSSL_CMP_ITAV_create(OBJ_txt2obj(oid, 1), val))) { 365 ASN1_TYPE_free(val); 366 return 0; 367 } 368 if (!TEST_true(OSSL_CMP_ITAV_push0_stack_item(&itavs, itav))) { 369 OSSL_CMP_ITAV_free(itav); 370 return 0; 371 } 372 373 if (!TEST_int_eq(ossl_cmp_hdr_generalInfo_push1_items(fixture->hdr, itavs), 374 1)) 375 goto err; 376 ginfo = fixture->hdr->generalInfo; 377 pushed_itav = sk_OSSL_CMP_ITAV_value(ginfo, 0); 378 OBJ_obj2txt(buf, sizeof(buf), OSSL_CMP_ITAV_get0_type(pushed_itav), 0); 379 if (!TEST_int_eq(memcmp(oid, buf, sizeof(oid)), 0)) 380 goto err; 381 382 pushed_val = OSSL_CMP_ITAV_get0_value(sk_OSSL_CMP_ITAV_value(ginfo, 0)); 383 if (!TEST_int_eq(ASN1_TYPE_cmp(itav->infoValue.other, pushed_val), 0)) 384 goto err; 385 386 res = 1; 387 388 err: 389 sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free); 390 return res; 391 } 392 393 static int test_HDR_generalInfo_push1_items(void) 394 { 395 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); 396 fixture->expected = 1; 397 EXECUTE_TEST(execute_HDR_generalInfo_push1_items_test, tear_down); 398 return result; 399 } 400 401 static int 402 execute_HDR_set_and_check_implicitConfirm_test(CMP_HDR_TEST_FIXTURE 403 *fixture) 404 { 405 return TEST_false(ossl_cmp_hdr_has_implicitConfirm(fixture->hdr)) 406 && TEST_true(ossl_cmp_hdr_set_implicitConfirm(fixture->hdr)) 407 && TEST_true(ossl_cmp_hdr_has_implicitConfirm(fixture->hdr)); 408 } 409 410 static int test_HDR_set_and_check_implicit_confirm(void) 411 { 412 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); 413 EXECUTE_TEST(execute_HDR_set_and_check_implicitConfirm_test, tear_down); 414 return result; 415 } 416 417 static int execute_HDR_init_test(CMP_HDR_TEST_FIXTURE *fixture) 418 { 419 ASN1_OCTET_STRING *header_nonce, *header_transactionID; 420 ASN1_OCTET_STRING *ctx_nonce; 421 422 if (!TEST_int_eq(fixture->expected, 423 ossl_cmp_hdr_init(fixture->cmp_ctx, fixture->hdr))) 424 return 0; 425 if (fixture->expected == 0) 426 return 1; 427 428 if (!TEST_int_eq(ossl_cmp_hdr_get_pvno(fixture->hdr), OSSL_CMP_PVNO)) 429 return 0; 430 431 header_nonce = ossl_cmp_hdr_get0_senderNonce(fixture->hdr); 432 if (!TEST_int_eq(0, ASN1_OCTET_STRING_cmp(header_nonce, fixture->cmp_ctx->senderNonce))) 433 return 0; 434 header_transactionID = OSSL_CMP_HDR_get0_transactionID(fixture->hdr); 435 if (!TEST_true(ASN1_OCTET_STRING_cmp(header_transactionID, 436 fixture->cmp_ctx->transactionID) 437 == 0)) 438 return 0; 439 440 header_nonce = OSSL_CMP_HDR_get0_recipNonce(fixture->hdr); 441 ctx_nonce = fixture->cmp_ctx->recipNonce; 442 if (ctx_nonce != NULL 443 && (!TEST_ptr(header_nonce) 444 || !TEST_int_eq(0, ASN1_OCTET_STRING_cmp(header_nonce, ctx_nonce)))) 445 return 0; 446 447 return 1; 448 } 449 450 static int test_HDR_init_with_ref(void) 451 { 452 unsigned char ref[CMP_TEST_REFVALUE_LENGTH]; 453 454 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); 455 456 fixture->expected = 1; 457 if (!TEST_int_eq(1, RAND_bytes(ref, sizeof(ref))) 458 || !TEST_true(OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx, 459 ref, sizeof(ref)))) { 460 tear_down(fixture); 461 fixture = NULL; 462 } 463 EXECUTE_TEST(execute_HDR_init_test, tear_down); 464 return result; 465 } 466 467 static int test_HDR_init_with_subject(void) 468 { 469 X509_NAME *subject = NULL; 470 471 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up); 472 fixture->expected = 1; 473 if (!TEST_ptr(subject = X509_NAME_new()) 474 || !TEST_true(X509_NAME_ADD(subject, "CN", "Common Name")) 475 || !TEST_true(OSSL_CMP_CTX_set1_subjectName(fixture->cmp_ctx, 476 subject))) { 477 tear_down(fixture); 478 fixture = NULL; 479 } 480 X509_NAME_free(subject); 481 EXECUTE_TEST(execute_HDR_init_test, tear_down); 482 return result; 483 } 484 485 void cleanup_tests(void) 486 { 487 return; 488 } 489 490 int setup_tests(void) 491 { 492 RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH); 493 /* Message header tests */ 494 ADD_TEST(test_HDR_set_get_pvno); 495 ADD_TEST(test_HDR_get0_senderNonce); 496 ADD_TEST(test_HDR_set1_sender); 497 ADD_TEST(test_HDR_set1_recipient); 498 ADD_TEST(test_HDR_update_messageTime); 499 ADD_TEST(test_HDR_set1_senderKID); 500 ADD_TEST(test_HDR_push0_freeText); 501 /* indirectly tests ossl_cmp_pkifreetext_push_str(): */ 502 ADD_TEST(test_HDR_push1_freeText); 503 ADD_TEST(test_HDR_generalInfo_push0_item); 504 ADD_TEST(test_HDR_generalInfo_push1_items); 505 ADD_TEST(test_HDR_set_and_check_implicit_confirm); 506 /* also tests public function OSSL_CMP_HDR_get0_transactionID(): */ 507 /* also tests public function OSSL_CMP_HDR_get0_recipNonce(): */ 508 /* also tests internal function ossl_cmp_hdr_get_pvno(): */ 509 ADD_TEST(test_HDR_init_with_ref); 510 ADD_TEST(test_HDR_init_with_subject); 511 return 1; 512 } 513