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