2 * Copyright 2007-2021 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright Nokia 2007-2019
4 * Copyright Siemens AG 2015-2019
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
12 #include "helpers/cmp_testlib.h"
14 static unsigned char rand_data[OSSL_CMP_TRANSACTIONID_LENGTH];
16 typedef struct test_fixture {
17 const char *test_case_name;
19 OSSL_CMP_CTX *cmp_ctx;
20 OSSL_CMP_PKIHEADER *hdr;
22 } CMP_HDR_TEST_FIXTURE;
24 static void tear_down(CMP_HDR_TEST_FIXTURE *fixture)
26 OSSL_CMP_PKIHEADER_free(fixture->hdr);
27 OSSL_CMP_CTX_free(fixture->cmp_ctx);
28 OPENSSL_free(fixture);
31 static CMP_HDR_TEST_FIXTURE *set_up(const char *const test_case_name)
33 CMP_HDR_TEST_FIXTURE *fixture;
35 if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
37 fixture->test_case_name = test_case_name;
38 if (!TEST_ptr(fixture->cmp_ctx = OSSL_CMP_CTX_new(NULL, NULL)))
40 if (!TEST_ptr(fixture->hdr = OSSL_CMP_PKIHEADER_new()))
49 static int execute_HDR_set_get_pvno_test(CMP_HDR_TEST_FIXTURE *fixture)
53 if (!TEST_int_eq(ossl_cmp_hdr_set_pvno(fixture->hdr, pvno), 1))
55 if (!TEST_int_eq(ossl_cmp_hdr_get_pvno(fixture->hdr), pvno))
60 static int test_HDR_set_get_pvno(void)
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);
68 #define X509_NAME_ADD(n, rd, s) \
69 X509_NAME_add_entry_by_txt((n), (rd), MBSTRING_ASC, (unsigned char *)(s), \
72 static int execute_HDR_get0_senderNonce_test(CMP_HDR_TEST_FIXTURE *fixture)
74 X509_NAME *sender = X509_NAME_new();
75 ASN1_OCTET_STRING *sn;
77 if (!TEST_ptr(sender))
80 X509_NAME_ADD(sender, "CN", "A common sender name");
81 if (!TEST_int_eq(OSSL_CMP_CTX_set1_subjectName(fixture->cmp_ctx, sender),
84 if (!TEST_int_eq(ossl_cmp_hdr_init(fixture->cmp_ctx, fixture->hdr),
87 sn = ossl_cmp_hdr_get0_senderNonce(fixture->hdr);
88 if (!TEST_int_eq(ASN1_OCTET_STRING_cmp(fixture->cmp_ctx->senderNonce, sn),
91 X509_NAME_free(sender);
95 static int test_HDR_get0_senderNonce(void)
97 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
98 fixture->expected = 1;
99 EXECUTE_TEST(execute_HDR_get0_senderNonce_test, tear_down);
103 static int execute_HDR_set1_sender_test(CMP_HDR_TEST_FIXTURE *fixture)
105 X509_NAME *x509name = X509_NAME_new();
107 if (!TEST_ptr(x509name))
110 X509_NAME_ADD(x509name, "CN", "A common sender name");
111 if (!TEST_int_eq(ossl_cmp_hdr_set1_sender(fixture->hdr, x509name), 1))
113 if (!TEST_int_eq(fixture->hdr->sender->type, GEN_DIRNAME))
116 if (!TEST_int_eq(X509_NAME_cmp(fixture->hdr->sender->d.directoryName,
120 X509_NAME_free(x509name);
124 static int test_HDR_set1_sender(void)
126 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
127 fixture->expected = 1;
128 EXECUTE_TEST(execute_HDR_set1_sender_test, tear_down);
132 static int execute_HDR_set1_recipient_test(CMP_HDR_TEST_FIXTURE *fixture)
134 X509_NAME *x509name = X509_NAME_new();
136 if (!TEST_ptr(x509name))
139 X509_NAME_ADD(x509name, "CN", "A common recipient name");
140 if (!TEST_int_eq(ossl_cmp_hdr_set1_recipient(fixture->hdr, x509name), 1))
143 if (!TEST_int_eq(fixture->hdr->recipient->type, GEN_DIRNAME))
146 if (!TEST_int_eq(X509_NAME_cmp(fixture->hdr->recipient->d.directoryName,
150 X509_NAME_free(x509name);
154 static int test_HDR_set1_recipient(void)
156 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
157 fixture->expected = 1;
158 EXECUTE_TEST(execute_HDR_set1_recipient_test, tear_down);
162 static int execute_HDR_update_messageTime_test(CMP_HDR_TEST_FIXTURE *fixture)
165 time_t hdrtime, before, after, now;
168 before = mktime(gmtime(&now));
169 if (!TEST_true(ossl_cmp_hdr_update_messageTime(fixture->hdr)))
171 if (!TEST_true(ASN1_TIME_to_tm(fixture->hdr->messageTime, &hdrtm)))
174 hdrtime = mktime(&hdrtm);
175 if (!TEST_true(before <= hdrtime))
178 after = mktime(gmtime(&now));
179 return TEST_true(hdrtime <= after);
182 static int test_HDR_update_messageTime(void)
184 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
185 fixture->expected = 1;
186 EXECUTE_TEST(execute_HDR_update_messageTime_test, tear_down);
190 static int execute_HDR_set1_senderKID_test(CMP_HDR_TEST_FIXTURE *fixture)
192 ASN1_OCTET_STRING *senderKID = ASN1_OCTET_STRING_new();
195 if (!TEST_ptr(senderKID))
198 if (!TEST_int_eq(ASN1_OCTET_STRING_set(senderKID, rand_data,
199 sizeof(rand_data)), 1))
201 if (!TEST_int_eq(ossl_cmp_hdr_set1_senderKID(fixture->hdr, senderKID), 1))
203 if (!TEST_int_eq(ASN1_OCTET_STRING_cmp(fixture->hdr->senderKID,
208 ASN1_OCTET_STRING_free(senderKID);
212 static int test_HDR_set1_senderKID(void)
214 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
215 fixture->expected = 1;
216 EXECUTE_TEST(execute_HDR_set1_senderKID_test, tear_down);
220 static int execute_HDR_push0_freeText_test(CMP_HDR_TEST_FIXTURE *fixture)
222 ASN1_UTF8STRING *text = ASN1_UTF8STRING_new();
227 if (!ASN1_STRING_set(text, "A free text", -1))
230 if (!TEST_int_eq(ossl_cmp_hdr_push0_freeText(fixture->hdr, text), 1))
233 if (!TEST_true(text == sk_ASN1_UTF8STRING_value(fixture->hdr->freeText, 0)))
239 ASN1_UTF8STRING_free(text);
243 static int test_HDR_push0_freeText(void)
245 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
246 fixture->expected = 1;
247 EXECUTE_TEST(execute_HDR_push0_freeText_test, tear_down);
251 static int execute_HDR_push1_freeText_test(CMP_HDR_TEST_FIXTURE *fixture)
253 ASN1_UTF8STRING *text = ASN1_UTF8STRING_new();
254 ASN1_UTF8STRING *pushed_text;
260 if (!ASN1_STRING_set(text, "A free text", -1))
263 if (!TEST_int_eq(ossl_cmp_hdr_push1_freeText(fixture->hdr, text), 1))
266 pushed_text = sk_ASN1_UTF8STRING_value(fixture->hdr->freeText, 0);
267 if (!TEST_int_eq(ASN1_STRING_cmp(text, pushed_text), 0))
272 ASN1_UTF8STRING_free(text);
276 static int test_HDR_push1_freeText(void)
278 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
279 fixture->expected = 1;
280 EXECUTE_TEST(execute_HDR_push1_freeText_test, tear_down);
285 execute_HDR_generalInfo_push0_item_test(CMP_HDR_TEST_FIXTURE *fixture)
287 OSSL_CMP_ITAV *itav = OSSL_CMP_ITAV_new();
292 if (!TEST_int_eq(ossl_cmp_hdr_generalInfo_push0_item(fixture->hdr, itav),
296 if (!TEST_true(itav == sk_OSSL_CMP_ITAV_value(fixture->hdr->generalInfo,
303 static int test_HDR_generalInfo_push0_item(void)
305 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
306 fixture->expected = 1;
307 EXECUTE_TEST(execute_HDR_generalInfo_push0_item_test, tear_down);
312 execute_HDR_generalInfo_push1_items_test(CMP_HDR_TEST_FIXTURE *fixture)
314 const char oid[] = "1.2.3.4";
316 OSSL_CMP_ITAV *itav, *pushed_itav;
317 STACK_OF(OSSL_CMP_ITAV) *itavs = NULL, *ginfo;
318 ASN1_INTEGER *asn1int = ASN1_INTEGER_new();
319 ASN1_TYPE *val = ASN1_TYPE_new();
320 ASN1_TYPE *pushed_val;
323 if (!TEST_ptr(asn1int))
326 if (!TEST_ptr(val)) {
327 ASN1_INTEGER_free(asn1int);
331 ASN1_INTEGER_set(asn1int, 88);
332 ASN1_TYPE_set(val, V_ASN1_INTEGER, asn1int);
333 if (!TEST_ptr(itav = OSSL_CMP_ITAV_create(OBJ_txt2obj(oid, 1), val))) {
337 if (!TEST_true(OSSL_CMP_ITAV_push0_stack_item(&itavs, itav))) {
338 OSSL_CMP_ITAV_free(itav);
342 if (!TEST_int_eq(ossl_cmp_hdr_generalInfo_push1_items(fixture->hdr, itavs),
345 ginfo = fixture->hdr->generalInfo;
346 pushed_itav = sk_OSSL_CMP_ITAV_value(ginfo, 0);
347 OBJ_obj2txt(buf, sizeof(buf), OSSL_CMP_ITAV_get0_type(pushed_itav), 0);
348 if (!TEST_int_eq(memcmp(oid, buf, sizeof(oid)), 0))
351 pushed_val = OSSL_CMP_ITAV_get0_value(sk_OSSL_CMP_ITAV_value(ginfo, 0));
352 if (!TEST_int_eq(ASN1_TYPE_cmp(itav->infoValue.other, pushed_val), 0))
358 sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
362 static int test_HDR_generalInfo_push1_items(void)
364 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
365 fixture->expected = 1;
366 EXECUTE_TEST(execute_HDR_generalInfo_push1_items_test, tear_down);
371 execute_HDR_set_and_check_implicitConfirm_test(CMP_HDR_TEST_FIXTURE
374 return TEST_false(ossl_cmp_hdr_has_implicitConfirm(fixture->hdr))
375 && TEST_true(ossl_cmp_hdr_set_implicitConfirm(fixture->hdr))
376 && TEST_true(ossl_cmp_hdr_has_implicitConfirm(fixture->hdr));
379 static int test_HDR_set_and_check_implicit_confirm(void)
381 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
382 EXECUTE_TEST(execute_HDR_set_and_check_implicitConfirm_test, tear_down);
387 static int execute_HDR_init_test(CMP_HDR_TEST_FIXTURE *fixture)
389 ASN1_OCTET_STRING *header_nonce, *header_transactionID;
390 ASN1_OCTET_STRING *ctx_nonce;
392 if (!TEST_int_eq(fixture->expected,
393 ossl_cmp_hdr_init(fixture->cmp_ctx, fixture->hdr)))
395 if (fixture->expected == 0)
398 if (!TEST_int_eq(ossl_cmp_hdr_get_pvno(fixture->hdr), OSSL_CMP_PVNO))
401 header_nonce = ossl_cmp_hdr_get0_senderNonce(fixture->hdr);
402 if (!TEST_int_eq(0, ASN1_OCTET_STRING_cmp(header_nonce,
403 fixture->cmp_ctx->senderNonce)))
405 header_transactionID = OSSL_CMP_HDR_get0_transactionID(fixture->hdr);
406 if (!TEST_true(0 == ASN1_OCTET_STRING_cmp(header_transactionID,
407 fixture->cmp_ctx->transactionID)))
410 header_nonce = OSSL_CMP_HDR_get0_recipNonce(fixture->hdr);
411 ctx_nonce = fixture->cmp_ctx->recipNonce;
412 if (ctx_nonce != NULL
413 && (!TEST_ptr(header_nonce)
414 || !TEST_int_eq(0, ASN1_OCTET_STRING_cmp(header_nonce,
421 static int test_HDR_init_with_ref(void)
423 unsigned char ref[CMP_TEST_REFVALUE_LENGTH];
425 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
427 fixture->expected = 1;
428 if (!TEST_int_eq(1, RAND_bytes(ref, sizeof(ref)))
429 || !TEST_true(OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx,
430 ref, sizeof(ref)))) {
434 EXECUTE_TEST(execute_HDR_init_test, tear_down);
438 static int test_HDR_init_with_subject(void)
440 X509_NAME *subject = NULL;
442 SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
443 fixture->expected = 1;
444 if (!TEST_ptr(subject = X509_NAME_new())
445 || !TEST_true(X509_NAME_ADD(subject, "CN", "Common Name"))
446 || !TEST_true(OSSL_CMP_CTX_set1_subjectName(fixture->cmp_ctx,
451 X509_NAME_free(subject);
452 EXECUTE_TEST(execute_HDR_init_test, tear_down);
457 void cleanup_tests(void)
462 int setup_tests(void)
464 RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH);
465 /* Message header tests */
466 ADD_TEST(test_HDR_set_get_pvno);
467 ADD_TEST(test_HDR_get0_senderNonce);
468 ADD_TEST(test_HDR_set1_sender);
469 ADD_TEST(test_HDR_set1_recipient);
470 ADD_TEST(test_HDR_update_messageTime);
471 ADD_TEST(test_HDR_set1_senderKID);
472 ADD_TEST(test_HDR_push0_freeText);
473 /* indirectly tests ossl_cmp_pkifreetext_push_str(): */
474 ADD_TEST(test_HDR_push1_freeText);
475 ADD_TEST(test_HDR_generalInfo_push0_item);
476 ADD_TEST(test_HDR_generalInfo_push1_items);
477 ADD_TEST(test_HDR_set_and_check_implicit_confirm);
478 /* also tests public function OSSL_CMP_HDR_get0_transactionID(): */
479 /* also tests public function OSSL_CMP_HDR_get0_recipNonce(): */
480 /* also tests internal function ossl_cmp_hdr_get_pvno(): */
481 ADD_TEST(test_HDR_init_with_ref);
482 ADD_TEST(test_HDR_init_with_subject);
484 * TODO make sure that total number of tests (here currently 24) is shown,
485 * also for other cmp_*text.c. Currently the test drivers always show 1.