VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.7/test/cmp_hdr_test.c@ 107044

Last change on this file since 107044 was 105949, checked in by vboxsync, 3 months ago

openssl-3.1.7: Applied and adjusted our OpenSSL changes to 3.1.7. bugref:10757

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette