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