xref: /openssl/test/cmp_msg_test.c (revision da1c088f)
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 
14 static const char *newkey_f;
15 static const char *server_cert_f;
16 static const char *pkcs10_f;
17 
18 typedef struct test_fixture {
19     const char *test_case_name;
20     OSSL_CMP_CTX *cmp_ctx;
21     /* for msg create tests */
22     int bodytype;
23     int err_code;
24     /* for certConf */
25     int fail_info;
26     /* for protection tests */
27     OSSL_CMP_MSG *msg;
28     int expected;
29     /* for error and response messages */
30     OSSL_CMP_PKISI *si;
31 } CMP_MSG_TEST_FIXTURE;
32 
33 static OSSL_LIB_CTX *libctx = NULL;
34 static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL;
35 
36 static unsigned char ref[CMP_TEST_REFVALUE_LENGTH];
37 
tear_down(CMP_MSG_TEST_FIXTURE * fixture)38 static void tear_down(CMP_MSG_TEST_FIXTURE *fixture)
39 {
40     OSSL_CMP_CTX_free(fixture->cmp_ctx);
41     OSSL_CMP_MSG_free(fixture->msg);
42     OSSL_CMP_PKISI_free(fixture->si);
43     OPENSSL_free(fixture);
44 }
45 
46 #define SET_OPT_UNPROTECTED_SEND(ctx, val) \
47     OSSL_CMP_CTX_set_option((ctx), OSSL_CMP_OPT_UNPROTECTED_SEND, (val))
set_up(const char * const test_case_name)48 static CMP_MSG_TEST_FIXTURE *set_up(const char *const test_case_name)
49 {
50     CMP_MSG_TEST_FIXTURE *fixture;
51 
52     if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
53         return NULL;
54     fixture->test_case_name = test_case_name;
55 
56     if (!TEST_ptr(fixture->cmp_ctx = OSSL_CMP_CTX_new(libctx, NULL))
57             || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 1))
58             || !TEST_true(OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx,
59                                                            ref, sizeof(ref)))) {
60         tear_down(fixture);
61         return NULL;
62     }
63     return fixture;
64 }
65 
66 static EVP_PKEY *newkey = NULL;
67 static X509 *cert = NULL;
68 
69 #define EXECUTE_MSG_CREATION_TEST(expr) \
70     do { \
71         OSSL_CMP_MSG *msg = NULL; \
72         int good = fixture->expected != 0 ? \
73             TEST_ptr(msg = (expr)) && TEST_true(valid_asn1_encoding(msg)) : \
74             TEST_ptr_null(msg = (expr)); \
75  \
76         OSSL_CMP_MSG_free(msg); \
77         ERR_print_errors_fp(stderr); \
78         return good; \
79     } while (0)
80 
81 /*-
82  * The following tests call a cmp message creation function.
83  * if fixture->expected != 0:
84  *         returns 1 if the message is created and syntactically correct.
85  * if fixture->expected == 0
86  *         returns 1 if message creation returns NULL
87  */
execute_certreq_create_test(CMP_MSG_TEST_FIXTURE * fixture)88 static int execute_certreq_create_test(CMP_MSG_TEST_FIXTURE *fixture)
89 {
90     EXECUTE_MSG_CREATION_TEST(ossl_cmp_certreq_new(fixture->cmp_ctx,
91                                                    fixture->bodytype,
92                                                    NULL));
93 }
94 
execute_errormsg_create_test(CMP_MSG_TEST_FIXTURE * fixture)95 static int execute_errormsg_create_test(CMP_MSG_TEST_FIXTURE *fixture)
96 {
97     EXECUTE_MSG_CREATION_TEST(ossl_cmp_error_new(fixture->cmp_ctx, fixture->si,
98                                                  fixture->err_code,
99                                                  "details", 0));
100 }
101 
execute_rr_create_test(CMP_MSG_TEST_FIXTURE * fixture)102 static int execute_rr_create_test(CMP_MSG_TEST_FIXTURE *fixture)
103 {
104     EXECUTE_MSG_CREATION_TEST(ossl_cmp_rr_new(fixture->cmp_ctx));
105 }
106 
execute_certconf_create_test(CMP_MSG_TEST_FIXTURE * fixture)107 static int execute_certconf_create_test(CMP_MSG_TEST_FIXTURE *fixture)
108 {
109     EXECUTE_MSG_CREATION_TEST(ossl_cmp_certConf_new
110                               (fixture->cmp_ctx, OSSL_CMP_CERTREQID,
111                                fixture->fail_info, NULL));
112 }
113 
execute_genm_create_test(CMP_MSG_TEST_FIXTURE * fixture)114 static int execute_genm_create_test(CMP_MSG_TEST_FIXTURE *fixture)
115 {
116     EXECUTE_MSG_CREATION_TEST(ossl_cmp_genm_new(fixture->cmp_ctx));
117 }
118 
execute_pollreq_create_test(CMP_MSG_TEST_FIXTURE * fixture)119 static int execute_pollreq_create_test(CMP_MSG_TEST_FIXTURE *fixture)
120 {
121     EXECUTE_MSG_CREATION_TEST(ossl_cmp_pollReq_new(fixture->cmp_ctx, 4711));
122 }
123 
execute_pkimessage_create_test(CMP_MSG_TEST_FIXTURE * fixture)124 static int execute_pkimessage_create_test(CMP_MSG_TEST_FIXTURE *fixture)
125 {
126     EXECUTE_MSG_CREATION_TEST(ossl_cmp_msg_create
127                               (fixture->cmp_ctx, fixture->bodytype));
128 }
129 
set1_newPkey(OSSL_CMP_CTX * ctx,EVP_PKEY * pkey)130 static int set1_newPkey(OSSL_CMP_CTX *ctx, EVP_PKEY *pkey)
131 {
132     if (!EVP_PKEY_up_ref(pkey))
133         return 0;
134 
135     if (!OSSL_CMP_CTX_set0_newPkey(ctx, 1, pkey)) {
136         EVP_PKEY_free(pkey);
137         return 0;
138     }
139     return 1;
140 }
141 
test_cmp_create_ir_protection_set(void)142 static int test_cmp_create_ir_protection_set(void)
143 {
144     OSSL_CMP_CTX *ctx;
145     unsigned char secret[16];
146 
147     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
148 
149     ctx = fixture->cmp_ctx;
150     fixture->bodytype = OSSL_CMP_PKIBODY_IR;
151     fixture->err_code = -1;
152     fixture->expected = 1;
153     if (!TEST_int_eq(1, RAND_bytes_ex(libctx, secret, sizeof(secret), 0))
154             || !TEST_true(SET_OPT_UNPROTECTED_SEND(ctx, 0))
155             || !TEST_true(set1_newPkey(ctx, newkey))
156             || !TEST_true(OSSL_CMP_CTX_set1_secretValue(ctx, secret,
157                                                         sizeof(secret)))) {
158         tear_down(fixture);
159         fixture = NULL;
160     }
161     EXECUTE_TEST(execute_certreq_create_test, tear_down);
162     return result;
163 }
164 
test_cmp_create_ir_protection_fails(void)165 static int test_cmp_create_ir_protection_fails(void)
166 {
167     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
168     fixture->bodytype = OSSL_CMP_PKIBODY_IR;
169     fixture->err_code = -1;
170     fixture->expected = 0;
171     if (!TEST_true(OSSL_CMP_CTX_set1_pkey(fixture->cmp_ctx, newkey))
172             || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0))
173             /* newkey used by default for signing does not match cert: */
174             || !TEST_true(OSSL_CMP_CTX_set1_cert(fixture->cmp_ctx, cert))) {
175         tear_down(fixture);
176         fixture = NULL;
177     }
178     EXECUTE_TEST(execute_certreq_create_test, tear_down);
179     return result;
180 }
181 
test_cmp_create_cr_without_key(void)182 static int test_cmp_create_cr_without_key(void)
183 {
184     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
185     fixture->bodytype = OSSL_CMP_PKIBODY_CR;
186     fixture->err_code = -1;
187     fixture->expected = 0;
188     EXECUTE_TEST(execute_certreq_create_test, tear_down);
189     return result;
190 }
191 
test_cmp_create_cr(void)192 static int test_cmp_create_cr(void)
193 {
194     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
195     fixture->bodytype = OSSL_CMP_PKIBODY_CR;
196     fixture->err_code = -1;
197     fixture->expected = 1;
198     if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
199         tear_down(fixture);
200         fixture = NULL;
201     }
202     EXECUTE_TEST(execute_certreq_create_test, tear_down);
203     return result;
204 }
205 
test_cmp_create_certreq_with_invalid_bodytype(void)206 static int test_cmp_create_certreq_with_invalid_bodytype(void)
207 {
208     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
209     fixture->bodytype = OSSL_CMP_PKIBODY_RR;
210     fixture->err_code = -1;
211     fixture->expected = 0;
212     if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
213         tear_down(fixture);
214         fixture = NULL;
215     }
216     EXECUTE_TEST(execute_certreq_create_test, tear_down);
217     return result;
218 }
219 
test_cmp_create_p10cr(void)220 static int test_cmp_create_p10cr(void)
221 {
222     OSSL_CMP_CTX *ctx;
223     X509_REQ *p10cr = NULL;
224 
225     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
226     ctx = fixture->cmp_ctx;
227     fixture->bodytype = OSSL_CMP_PKIBODY_P10CR;
228     fixture->err_code = CMP_R_ERROR_CREATING_CERTREQ;
229     fixture->expected = 1;
230     if (!TEST_ptr(p10cr = load_csr_der(pkcs10_f, libctx))
231             || !TEST_true(set1_newPkey(ctx, newkey))
232             || !TEST_true(OSSL_CMP_CTX_set1_p10CSR(ctx, p10cr))) {
233         tear_down(fixture);
234         fixture = NULL;
235     }
236     X509_REQ_free(p10cr);
237     EXECUTE_TEST(execute_certreq_create_test, tear_down);
238     return result;
239 }
240 
test_cmp_create_p10cr_null(void)241 static int test_cmp_create_p10cr_null(void)
242 {
243     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
244     fixture->bodytype = OSSL_CMP_PKIBODY_P10CR;
245     fixture->err_code = CMP_R_ERROR_CREATING_CERTREQ;
246     fixture->expected = 0;
247     if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
248         tear_down(fixture);
249         fixture = NULL;
250     }
251     EXECUTE_TEST(execute_certreq_create_test, tear_down);
252     return result;
253 }
254 
test_cmp_create_kur(void)255 static int test_cmp_create_kur(void)
256 {
257     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
258     fixture->bodytype = OSSL_CMP_PKIBODY_KUR;
259     fixture->err_code = -1;
260     fixture->expected = 1;
261     if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))
262             || !TEST_true(OSSL_CMP_CTX_set1_oldCert(fixture->cmp_ctx, cert))) {
263         tear_down(fixture);
264         fixture = NULL;
265     }
266     EXECUTE_TEST(execute_certreq_create_test, tear_down);
267     return result;
268 }
269 
test_cmp_create_kur_without_oldcert(void)270 static int test_cmp_create_kur_without_oldcert(void)
271 {
272     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
273     fixture->bodytype = OSSL_CMP_PKIBODY_KUR;
274     fixture->err_code = -1;
275     fixture->expected = 0;
276     if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
277         tear_down(fixture);
278         fixture = NULL;
279     }
280     EXECUTE_TEST(execute_certreq_create_test, tear_down);
281     return result;
282 }
283 
test_cmp_create_certconf(void)284 static int test_cmp_create_certconf(void)
285 {
286     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
287     fixture->fail_info = 0;
288     fixture->expected = 1;
289     if (!TEST_true(ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx,
290                                              X509_dup(cert)))) {
291         tear_down(fixture);
292         fixture = NULL;
293     }
294     EXECUTE_TEST(execute_certconf_create_test, tear_down);
295     return result;
296 }
297 
test_cmp_create_certconf_badAlg(void)298 static int test_cmp_create_certconf_badAlg(void)
299 {
300     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
301     fixture->fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_badAlg;
302     fixture->expected = 1;
303     if (!TEST_true(ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx,
304                                              X509_dup(cert)))) {
305         tear_down(fixture);
306         fixture = NULL;
307     }
308     EXECUTE_TEST(execute_certconf_create_test, tear_down);
309     return result;
310 }
311 
test_cmp_create_certconf_fail_info_max(void)312 static int test_cmp_create_certconf_fail_info_max(void)
313 {
314     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
315     fixture->fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_MAX;
316     fixture->expected = 1;
317     if (!TEST_true(ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx,
318                                              X509_dup(cert)))) {
319         tear_down(fixture);
320         fixture = NULL;
321     }
322     EXECUTE_TEST(execute_certconf_create_test, tear_down);
323     return result;
324 }
325 
test_cmp_create_error_msg(void)326 static int test_cmp_create_error_msg(void)
327 {
328     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
329     fixture->si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
330                                           OSSL_CMP_PKIFAILUREINFO_systemFailure,
331                                           NULL);
332     fixture->err_code = -1;
333     fixture->expected = 1; /* expected: message creation is successful */
334     if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
335         tear_down(fixture);
336         fixture = NULL;
337     }
338     EXECUTE_TEST(execute_errormsg_create_test, tear_down);
339     return result;
340 }
341 
test_cmp_create_pollreq(void)342 static int test_cmp_create_pollreq(void)
343 {
344     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
345     fixture->expected = 1;
346     EXECUTE_TEST(execute_pollreq_create_test, tear_down);
347     return result;
348 }
349 
test_cmp_create_rr(void)350 static int test_cmp_create_rr(void)
351 {
352     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
353     fixture->expected = 1;
354     if (!TEST_true(OSSL_CMP_CTX_set1_oldCert(fixture->cmp_ctx, cert))) {
355         tear_down(fixture);
356         fixture = NULL;
357     }
358     EXECUTE_TEST(execute_rr_create_test, tear_down);
359     return result;
360 }
361 
test_cmp_create_genm(void)362 static int test_cmp_create_genm(void)
363 {
364     OSSL_CMP_ITAV *iv = NULL;
365 
366     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
367     fixture->expected = 1;
368     iv = OSSL_CMP_ITAV_create(OBJ_nid2obj(NID_id_it_implicitConfirm), NULL);
369     if (!TEST_ptr(iv)
370             || !TEST_true(OSSL_CMP_CTX_push0_genm_ITAV(fixture->cmp_ctx, iv))) {
371         OSSL_CMP_ITAV_free(iv);
372         tear_down(fixture);
373         fixture = NULL;
374     }
375 
376     EXECUTE_TEST(execute_genm_create_test, tear_down);
377     return result;
378 }
379 
execute_certrep_create(CMP_MSG_TEST_FIXTURE * fixture)380 static int execute_certrep_create(CMP_MSG_TEST_FIXTURE *fixture)
381 {
382     OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
383     OSSL_CMP_CERTREPMESSAGE *crepmsg = OSSL_CMP_CERTREPMESSAGE_new();
384     OSSL_CMP_CERTRESPONSE *read_cresp, *cresp = OSSL_CMP_CERTRESPONSE_new();
385     X509 *certfromresp = NULL;
386     int res = 0;
387 
388     if (crepmsg == NULL || cresp == NULL)
389         goto err;
390     if (!ASN1_INTEGER_set(cresp->certReqId, 99))
391         goto err;
392     if ((cresp->certifiedKeyPair = OSSL_CMP_CERTIFIEDKEYPAIR_new()) == NULL)
393         goto err;
394     cresp->certifiedKeyPair->certOrEncCert->type =
395         OSSL_CMP_CERTORENCCERT_CERTIFICATE;
396     if ((cresp->certifiedKeyPair->certOrEncCert->value.certificate =
397          X509_dup(cert)) == NULL
398             || !sk_OSSL_CMP_CERTRESPONSE_push(crepmsg->response, cresp))
399         goto err;
400     cresp = NULL;
401     read_cresp = ossl_cmp_certrepmessage_get0_certresponse(crepmsg, 99);
402     if (!TEST_ptr(read_cresp))
403         goto err;
404     if (!TEST_ptr_null(ossl_cmp_certrepmessage_get0_certresponse(crepmsg, 88)))
405         goto err;
406     certfromresp = ossl_cmp_certresponse_get1_cert(ctx, read_cresp);
407     if (certfromresp == NULL || !TEST_int_eq(X509_cmp(cert, certfromresp), 0))
408         goto err;
409 
410     res = 1;
411  err:
412     X509_free(certfromresp);
413     OSSL_CMP_CERTRESPONSE_free(cresp);
414     OSSL_CMP_CERTREPMESSAGE_free(crepmsg);
415     return res;
416 }
417 
test_cmp_create_certrep(void)418 static int test_cmp_create_certrep(void)
419 {
420     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
421     EXECUTE_TEST(execute_certrep_create, tear_down);
422     return result;
423 }
424 
execute_rp_create(CMP_MSG_TEST_FIXTURE * fixture)425 static int execute_rp_create(CMP_MSG_TEST_FIXTURE *fixture)
426 {
427     OSSL_CMP_PKISI *si = OSSL_CMP_STATUSINFO_new(33, 44, "a text");
428     X509_NAME *issuer = X509_NAME_new();
429     ASN1_INTEGER *serial = ASN1_INTEGER_new();
430     OSSL_CRMF_CERTID *cid = NULL;
431     OSSL_CMP_MSG *rpmsg = NULL;
432     int res = 0;
433 
434     if (si == NULL || issuer == NULL || serial == NULL)
435         goto err;
436 
437     if (!X509_NAME_add_entry_by_txt(issuer, "CN", MBSTRING_ASC,
438                                     (unsigned char *)"The Issuer", -1, -1, 0)
439             || !ASN1_INTEGER_set(serial, 99)
440             || (cid = OSSL_CRMF_CERTID_gen(issuer, serial)) == NULL
441             || (rpmsg = ossl_cmp_rp_new(fixture->cmp_ctx, si, cid, 1)) == NULL)
442         goto err;
443 
444     if (!TEST_ptr(ossl_cmp_revrepcontent_get_CertId(rpmsg->body->value.rp, 0)))
445         goto err;
446 
447     if (!TEST_ptr(ossl_cmp_revrepcontent_get_pkisi(rpmsg->body->value.rp, 0)))
448         goto err;
449 
450     res = 1;
451  err:
452     ASN1_INTEGER_free(serial);
453     X509_NAME_free(issuer);
454     OSSL_CRMF_CERTID_free(cid);
455     OSSL_CMP_PKISI_free(si);
456     OSSL_CMP_MSG_free(rpmsg);
457     return res;
458 }
459 
test_cmp_create_rp(void)460 static int test_cmp_create_rp(void)
461 {
462     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
463     EXECUTE_TEST(execute_rp_create, tear_down);
464     return result;
465 }
466 
execute_pollrep_create(CMP_MSG_TEST_FIXTURE * fixture)467 static int execute_pollrep_create(CMP_MSG_TEST_FIXTURE *fixture)
468 {
469     OSSL_CMP_MSG *pollrep;
470     int res = 0;
471 
472     pollrep = ossl_cmp_pollRep_new(fixture->cmp_ctx, 77, 2000);
473     if (!TEST_ptr(pollrep))
474         return 0;
475     if (!TEST_ptr(ossl_cmp_pollrepcontent_get0_pollrep(pollrep->body->
476                                                        value.pollRep, 77)))
477         goto err;
478     if (!TEST_ptr_null(ossl_cmp_pollrepcontent_get0_pollrep(pollrep->body->
479                                                             value.pollRep, 88)))
480         goto err;
481 
482     res = 1;
483  err:
484     OSSL_CMP_MSG_free(pollrep);
485     return res;
486 }
487 
test_cmp_create_pollrep(void)488 static int test_cmp_create_pollrep(void)
489 {
490     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
491     EXECUTE_TEST(execute_pollrep_create, tear_down);
492     return result;
493 }
494 
test_cmp_pkimessage_create(int bodytype)495 static int test_cmp_pkimessage_create(int bodytype)
496 {
497     X509_REQ *p10cr = NULL;
498 
499     SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
500 
501     switch (fixture->bodytype = bodytype) {
502     case OSSL_CMP_PKIBODY_P10CR:
503         fixture->expected = 1;
504         p10cr = load_csr_der(pkcs10_f, libctx);
505         if (!TEST_true(OSSL_CMP_CTX_set1_p10CSR(fixture->cmp_ctx, p10cr))) {
506             tear_down(fixture);
507             fixture = NULL;
508         }
509         X509_REQ_free(p10cr);
510         break;
511     case OSSL_CMP_PKIBODY_IR:
512     case OSSL_CMP_PKIBODY_IP:
513     case OSSL_CMP_PKIBODY_CR:
514     case OSSL_CMP_PKIBODY_CP:
515     case OSSL_CMP_PKIBODY_KUR:
516     case OSSL_CMP_PKIBODY_KUP:
517     case OSSL_CMP_PKIBODY_RR:
518     case OSSL_CMP_PKIBODY_RP:
519     case OSSL_CMP_PKIBODY_PKICONF:
520     case OSSL_CMP_PKIBODY_GENM:
521     case OSSL_CMP_PKIBODY_GENP:
522     case OSSL_CMP_PKIBODY_ERROR:
523     case OSSL_CMP_PKIBODY_CERTCONF:
524     case OSSL_CMP_PKIBODY_POLLREQ:
525     case OSSL_CMP_PKIBODY_POLLREP:
526         fixture->expected = 1;
527         break;
528     default:
529         fixture->expected = 0;
530         break;
531     }
532 
533     EXECUTE_TEST(execute_pkimessage_create_test, tear_down);
534     return result;
535 }
536 
cleanup_tests(void)537 void cleanup_tests(void)
538 {
539     EVP_PKEY_free(newkey);
540     X509_free(cert);
541     OSSL_PROVIDER_unload(default_null_provider);
542     OSSL_PROVIDER_unload(provider);
543     OSSL_LIB_CTX_free(libctx);
544 }
545 
546 #define USAGE "new.key server.crt pkcs10.der module_name [module_conf_file]\n"
OPT_TEST_DECLARE_USAGE(USAGE)547 OPT_TEST_DECLARE_USAGE(USAGE)
548 
549 int setup_tests(void)
550 {
551     if (!test_skip_common_options()) {
552         TEST_error("Error parsing test options\n");
553         return 0;
554     }
555 
556     if (!TEST_ptr(newkey_f = test_get_argument(0))
557             || !TEST_ptr(server_cert_f = test_get_argument(1))
558             || !TEST_ptr(pkcs10_f = test_get_argument(2))) {
559         TEST_error("usage: cmp_msg_test %s", USAGE);
560         return 0;
561     }
562 
563     if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 3, USAGE))
564         return 0;
565 
566     if (!TEST_ptr(newkey = load_pkey_pem(newkey_f, libctx))
567             || !TEST_ptr(cert = load_cert_pem(server_cert_f, libctx))
568             || !TEST_int_eq(1, RAND_bytes_ex(libctx, ref, sizeof(ref), 0))) {
569         cleanup_tests();
570         return 0;
571     }
572 
573     /* Message creation tests */
574     ADD_TEST(test_cmp_create_certreq_with_invalid_bodytype);
575     ADD_TEST(test_cmp_create_ir_protection_fails);
576     ADD_TEST(test_cmp_create_ir_protection_set);
577     ADD_TEST(test_cmp_create_error_msg);
578     ADD_TEST(test_cmp_create_certconf);
579     ADD_TEST(test_cmp_create_certconf_badAlg);
580     ADD_TEST(test_cmp_create_certconf_fail_info_max);
581     ADD_TEST(test_cmp_create_kur);
582     ADD_TEST(test_cmp_create_kur_without_oldcert);
583     ADD_TEST(test_cmp_create_cr);
584     ADD_TEST(test_cmp_create_cr_without_key);
585     ADD_TEST(test_cmp_create_p10cr);
586     ADD_TEST(test_cmp_create_p10cr_null);
587     ADD_TEST(test_cmp_create_pollreq);
588     ADD_TEST(test_cmp_create_rr);
589     ADD_TEST(test_cmp_create_rp);
590     ADD_TEST(test_cmp_create_genm);
591     ADD_TEST(test_cmp_create_certrep);
592     ADD_TEST(test_cmp_create_pollrep);
593     ADD_ALL_TESTS_NOSUBTEST(test_cmp_pkimessage_create,
594                             OSSL_CMP_PKIBODY_POLLREP + 1);
595     return 1;
596 }
597