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