2 * Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright Nokia 2007-2019
4 * Copyright Siemens AG 2015-2019
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
12 #include "helpers/cmp_testlib.h"
14 static const char *newkey_f
;
15 static const char *server_cert_f
;
16 static const char *pkcs10_f
;
18 typedef struct test_fixture
{
19 const char *test_case_name
;
20 OSSL_CMP_CTX
*cmp_ctx
;
21 /* for msg create tests */
26 /* for protection tests */
29 /* for error and response messages */
31 } CMP_MSG_TEST_FIXTURE
;
33 static OSSL_LIB_CTX
*libctx
= NULL
;
34 static OSSL_PROVIDER
*default_null_provider
= NULL
, *provider
= NULL
;
36 static unsigned char ref
[CMP_TEST_REFVALUE_LENGTH
];
38 static void tear_down(CMP_MSG_TEST_FIXTURE
*fixture
)
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
);
46 #define SET_OPT_UNPROTECTED_SEND(ctx, val) \
47 OSSL_CMP_CTX_set_option((ctx), OSSL_CMP_OPT_UNPROTECTED_SEND, (val))
48 static CMP_MSG_TEST_FIXTURE
*set_up(const char *const test_case_name
)
50 CMP_MSG_TEST_FIXTURE
*fixture
;
52 if (!TEST_ptr(fixture
= OPENSSL_zalloc(sizeof(*fixture
))))
54 fixture
->test_case_name
= test_case_name
;
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
,
66 static EVP_PKEY
*newkey
= NULL
;
67 static X509
*cert
= NULL
;
69 #define EXECUTE_MSG_CREATION_TEST(expr) \
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)); \
76 OSSL_CMP_MSG_free(msg); \
77 ERR_print_errors_fp(stderr); \
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
88 static int execute_certreq_create_test(CMP_MSG_TEST_FIXTURE
*fixture
)
90 EXECUTE_MSG_CREATION_TEST(ossl_cmp_certreq_new(fixture
->cmp_ctx
,
95 static int execute_errormsg_create_test(CMP_MSG_TEST_FIXTURE
*fixture
)
97 EXECUTE_MSG_CREATION_TEST(ossl_cmp_error_new(fixture
->cmp_ctx
, fixture
->si
,
102 static int execute_rr_create_test(CMP_MSG_TEST_FIXTURE
*fixture
)
104 EXECUTE_MSG_CREATION_TEST(ossl_cmp_rr_new(fixture
->cmp_ctx
));
107 static int execute_certconf_create_test(CMP_MSG_TEST_FIXTURE
*fixture
)
109 EXECUTE_MSG_CREATION_TEST(ossl_cmp_certConf_new
110 (fixture
->cmp_ctx
, OSSL_CMP_CERTREQID
,
111 fixture
->fail_info
, NULL
));
114 static int execute_genm_create_test(CMP_MSG_TEST_FIXTURE
*fixture
)
116 EXECUTE_MSG_CREATION_TEST(ossl_cmp_genm_new(fixture
->cmp_ctx
));
119 static int execute_pollreq_create_test(CMP_MSG_TEST_FIXTURE
*fixture
)
121 EXECUTE_MSG_CREATION_TEST(ossl_cmp_pollReq_new(fixture
->cmp_ctx
, 4711));
124 static int execute_pkimessage_create_test(CMP_MSG_TEST_FIXTURE
*fixture
)
126 EXECUTE_MSG_CREATION_TEST(ossl_cmp_msg_create
127 (fixture
->cmp_ctx
, fixture
->bodytype
));
130 static int set1_newPkey(OSSL_CMP_CTX
*ctx
, EVP_PKEY
*pkey
)
132 if (!EVP_PKEY_up_ref(pkey
))
135 if (!OSSL_CMP_CTX_set0_newPkey(ctx
, 1, pkey
)) {
142 static int test_cmp_create_ir_protection_set(void)
145 unsigned char secret
[16];
147 SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE
, set_up
);
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
,
161 EXECUTE_TEST(execute_certreq_create_test
, tear_down
);
165 static int test_cmp_create_ir_protection_fails(void)
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
))) {
178 EXECUTE_TEST(execute_certreq_create_test
, tear_down
);
182 static int test_cmp_create_cr_without_key(void)
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
);
192 static int test_cmp_create_cr(void)
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
))) {
202 EXECUTE_TEST(execute_certreq_create_test
, tear_down
);
206 static int test_cmp_create_certreq_with_invalid_bodytype(void)
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
))) {
216 EXECUTE_TEST(execute_certreq_create_test
, tear_down
);
220 static int test_cmp_create_p10cr(void)
223 X509_REQ
*p10cr
= NULL
;
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
))) {
236 X509_REQ_free(p10cr
);
237 EXECUTE_TEST(execute_certreq_create_test
, tear_down
);
241 static int test_cmp_create_p10cr_null(void)
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
))) {
251 EXECUTE_TEST(execute_certreq_create_test
, tear_down
);
255 static int test_cmp_create_kur(void)
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
))) {
266 EXECUTE_TEST(execute_certreq_create_test
, tear_down
);
270 static int test_cmp_create_kur_without_oldcert(void)
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
))) {
280 EXECUTE_TEST(execute_certreq_create_test
, tear_down
);
284 static int test_cmp_create_certconf(void)
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
,
294 EXECUTE_TEST(execute_certconf_create_test
, tear_down
);
298 static int test_cmp_create_certconf_badAlg(void)
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
,
308 EXECUTE_TEST(execute_certconf_create_test
, tear_down
);
312 static int test_cmp_create_certconf_fail_info_max(void)
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
,
322 EXECUTE_TEST(execute_certconf_create_test
, tear_down
);
326 static int test_cmp_create_error_msg(void)
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
,
332 fixture
->err_code
= -1;
333 fixture
->expected
= 1; /* expected: message creation is successful */
334 if (!TEST_true(set1_newPkey(fixture
->cmp_ctx
, newkey
))) {
338 EXECUTE_TEST(execute_errormsg_create_test
, tear_down
);
342 static int test_cmp_create_pollreq(void)
344 SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE
, set_up
);
345 fixture
->expected
= 1;
346 EXECUTE_TEST(execute_pollreq_create_test
, tear_down
);
350 static int test_cmp_create_rr(void)
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
))) {
358 EXECUTE_TEST(execute_rr_create_test
, tear_down
);
362 static int test_cmp_create_genm(void)
364 OSSL_CMP_ITAV
*iv
= NULL
;
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
);
370 || !TEST_true(OSSL_CMP_CTX_push0_genm_ITAV(fixture
->cmp_ctx
, iv
))) {
371 OSSL_CMP_ITAV_free(iv
);
376 EXECUTE_TEST(execute_genm_create_test
, tear_down
);
380 static int execute_certrep_create(CMP_MSG_TEST_FIXTURE
*fixture
)
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
;
388 if (crepmsg
== NULL
|| cresp
== NULL
)
390 if (!ASN1_INTEGER_set(cresp
->certReqId
, 99))
392 if ((cresp
->certifiedKeyPair
= OSSL_CMP_CERTIFIEDKEYPAIR_new()) == NULL
)
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
))
401 read_cresp
= ossl_cmp_certrepmessage_get0_certresponse(crepmsg
, 99);
402 if (!TEST_ptr(read_cresp
))
404 if (!TEST_ptr_null(ossl_cmp_certrepmessage_get0_certresponse(crepmsg
, 88)))
406 certfromresp
= ossl_cmp_certresponse_get1_cert(ctx
, read_cresp
);
407 if (certfromresp
== NULL
|| !TEST_int_eq(X509_cmp(cert
, certfromresp
), 0))
412 X509_free(certfromresp
);
413 OSSL_CMP_CERTRESPONSE_free(cresp
);
414 OSSL_CMP_CERTREPMESSAGE_free(crepmsg
);
418 static int test_cmp_create_certrep(void)
420 SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE
, set_up
);
421 EXECUTE_TEST(execute_certrep_create
, tear_down
);
425 static int execute_rp_create(CMP_MSG_TEST_FIXTURE
*fixture
)
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
;
434 if (si
== NULL
|| issuer
== NULL
|| serial
== NULL
)
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
)
444 if (!TEST_ptr(ossl_cmp_revrepcontent_get_CertId(rpmsg
->body
->value
.rp
, 0)))
447 if (!TEST_ptr(ossl_cmp_revrepcontent_get_pkisi(rpmsg
->body
->value
.rp
, 0)))
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
);
460 static int test_cmp_create_rp(void)
462 SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE
, set_up
);
463 EXECUTE_TEST(execute_rp_create
, tear_down
);
467 static int execute_pollrep_create(CMP_MSG_TEST_FIXTURE
*fixture
)
469 OSSL_CMP_MSG
*pollrep
;
472 pollrep
= ossl_cmp_pollRep_new(fixture
->cmp_ctx
, 77, 2000);
473 if (!TEST_ptr(pollrep
))
475 if (!TEST_ptr(ossl_cmp_pollrepcontent_get0_pollrep(pollrep
->body
->
478 if (!TEST_ptr_null(ossl_cmp_pollrepcontent_get0_pollrep(pollrep
->body
->
484 OSSL_CMP_MSG_free(pollrep
);
488 static int test_cmp_create_pollrep(void)
490 SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE
, set_up
);
491 EXECUTE_TEST(execute_pollrep_create
, tear_down
);
495 static int test_cmp_pkimessage_create(int bodytype
)
497 X509_REQ
*p10cr
= NULL
;
499 SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE
, set_up
);
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
))) {
509 X509_REQ_free(p10cr
);
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;
529 fixture
->expected
= 0;
533 EXECUTE_TEST(execute_pkimessage_create_test
, tear_down
);
537 void cleanup_tests(void)
539 EVP_PKEY_free(newkey
);
541 OSSL_PROVIDER_unload(default_null_provider
);
542 OSSL_PROVIDER_unload(provider
);
543 OSSL_LIB_CTX_free(libctx
);
546 #define USAGE "new.key server.crt pkcs10.der module_name [module_conf_file]\n"
547 OPT_TEST_DECLARE_USAGE(USAGE
)
549 int setup_tests(void)
551 if (!test_skip_common_options()) {
552 TEST_error("Error parsing test options\n");
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
);
563 if (!test_arg_libctx(&libctx
, &default_null_provider
, &provider
, 3, USAGE
))
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))) {
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);