2 * Copyright 2007-2020 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 "cmp_testlib.h"
16 static const char *ir_protected_f
;
17 static const char *ir_unprotected_f
;
18 static const char *ip_PBM_f
;
20 typedef struct test_fixture
{
21 const char *test_case_name
;
22 OSSL_CMP_CTX
*cmp_ctx
;
23 /* for protection tests */
25 OSSL_CMP_PKISI
*si
; /* for error and response messages */
30 STACK_OF(X509
) *certs
;
31 STACK_OF(X509
) *chain
;
34 } CMP_PROTECT_TEST_FIXTURE
;
36 static OPENSSL_CTX
*libctx
= NULL
;
37 static OSSL_PROVIDER
*default_null_provider
= NULL
, *provider
= NULL
;
39 static void tear_down(CMP_PROTECT_TEST_FIXTURE
*fixture
)
41 OSSL_CMP_CTX_free(fixture
->cmp_ctx
);
42 OSSL_CMP_MSG_free(fixture
->msg
);
43 OSSL_CMP_PKISI_free(fixture
->si
);
45 OPENSSL_free(fixture
->mem
);
46 sk_X509_free(fixture
->certs
);
47 sk_X509_free(fixture
->chain
);
49 OPENSSL_free(fixture
);
52 static CMP_PROTECT_TEST_FIXTURE
*set_up(const char *const test_case_name
)
54 CMP_PROTECT_TEST_FIXTURE
*fixture
;
56 if (!TEST_ptr(fixture
= OPENSSL_zalloc(sizeof(*fixture
))))
58 fixture
->test_case_name
= test_case_name
;
59 if (!TEST_ptr(fixture
->cmp_ctx
= OSSL_CMP_CTX_new(libctx
, NULL
))) {
66 static EVP_PKEY
*loadedprivkey
= NULL
;
67 static EVP_PKEY
*loadedpubkey
= NULL
;
68 static EVP_PKEY
*loadedkey
= NULL
;
69 static X509
*cert
= NULL
;
70 static unsigned char rand_data
[OSSL_CMP_TRANSACTIONID_LENGTH
];
71 static OSSL_CMP_MSG
*ir_unprotected
, *ir_protected
;
72 static X509
*endentity1
= NULL
, *endentity2
= NULL
,
73 *root
= NULL
, *intermediate
= NULL
;
75 static int execute_calc_protection_fails_test(CMP_PROTECT_TEST_FIXTURE
*fixture
)
77 ASN1_BIT_STRING
*protection
=
78 ossl_cmp_calc_protection(fixture
->cmp_ctx
, fixture
->msg
);
79 int res
= TEST_ptr_null(protection
);
81 ASN1_BIT_STRING_free(protection
);
85 static int execute_calc_protection_pbmac_test(CMP_PROTECT_TEST_FIXTURE
*fixture
)
87 ASN1_BIT_STRING
*protection
=
88 ossl_cmp_calc_protection(fixture
->cmp_ctx
, fixture
->msg
);
89 int res
= TEST_ptr(protection
)
90 && TEST_true(ASN1_STRING_cmp(protection
,
91 fixture
->msg
->protection
) == 0);
93 ASN1_BIT_STRING_free(protection
);
98 * This function works similarly to parts of CMP_verify_signature in cmp_vfy.c,
99 * but without the need for a OSSL_CMP_CTX or a X509 certificate
101 static int verify_signature(OSSL_CMP_MSG
*msg
,
102 ASN1_BIT_STRING
*protection
,
103 EVP_PKEY
*pkey
, EVP_MD
*digest
)
105 OSSL_CMP_PROTECTEDPART prot_part
;
106 unsigned char *prot_part_der
= NULL
;
108 EVP_MD_CTX
*ctx
= NULL
;
111 prot_part
.header
= OSSL_CMP_MSG_get0_header(msg
);
112 prot_part
.body
= msg
->body
;
113 len
= i2d_OSSL_CMP_PROTECTEDPART(&prot_part
, &prot_part_der
);
116 && TEST_ptr(ctx
= EVP_MD_CTX_new())
117 && TEST_true(EVP_DigestVerifyInit(ctx
, NULL
, digest
, NULL
, pkey
))
118 && TEST_int_eq(EVP_DigestVerify(ctx
, protection
->data
,
120 prot_part_der
, len
), 1);
122 EVP_MD_CTX_free(ctx
);
123 OPENSSL_free(prot_part_der
);
127 /* Calls OSSL_CMP_calc_protection and compares and verifies signature */
128 static int execute_calc_protection_signature_test(CMP_PROTECT_TEST_FIXTURE
*
131 ASN1_BIT_STRING
*protection
=
132 ossl_cmp_calc_protection(fixture
->cmp_ctx
, fixture
->msg
);
133 int ret
= (TEST_ptr(protection
)
134 && TEST_true(ASN1_STRING_cmp(protection
,
135 fixture
->msg
->protection
) == 0)
136 && TEST_true(verify_signature(fixture
->msg
, protection
,
138 fixture
->cmp_ctx
->digest
)));
140 ASN1_BIT_STRING_free(protection
);
144 static int test_cmp_calc_protection_no_key_no_secret(void)
146 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE
, set_up
);
147 if (!TEST_ptr(fixture
->msg
= load_pkimsg(ir_unprotected_f
))
148 || !TEST_ptr(fixture
->msg
->header
->protectionAlg
=
149 X509_ALGOR_new() /* no specific alg needed here */)) {
154 EXECUTE_TEST(execute_calc_protection_fails_test
, tear_down
);
158 static int test_cmp_calc_protection_pkey(void)
160 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE
, set_up
);
161 fixture
->pubkey
= loadedpubkey
;
162 if (!TEST_true(OSSL_CMP_CTX_set1_pkey(fixture
->cmp_ctx
, loadedprivkey
))
163 || !TEST_ptr(fixture
->msg
= load_pkimsg(ir_protected_f
))) {
167 EXECUTE_TEST(execute_calc_protection_signature_test
, tear_down
);
171 static int test_cmp_calc_protection_pbmac(void)
173 unsigned char sec_insta
[] = { 'i', 'n', 's', 't', 'a' };
175 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE
, set_up
);
176 if (!TEST_true(OSSL_CMP_CTX_set1_secretValue(fixture
->cmp_ctx
,
177 sec_insta
, sizeof(sec_insta
)))
178 || !TEST_ptr(fixture
->msg
= load_pkimsg(ip_PBM_f
))) {
182 EXECUTE_TEST(execute_calc_protection_pbmac_test
, tear_down
);
185 static int execute_MSG_protect_test(CMP_PROTECT_TEST_FIXTURE
*fixture
)
187 return TEST_int_eq(fixture
->expected
,
188 ossl_cmp_msg_protect(fixture
->cmp_ctx
, fixture
->msg
));
191 #define SET_OPT_UNPROTECTED_SEND(ctx, val) \
192 OSSL_CMP_CTX_set_option((ctx), OSSL_CMP_OPT_UNPROTECTED_SEND, (val))
193 static int test_MSG_protect_unprotected_request(void)
195 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE
, set_up
);
197 fixture
->expected
= 1;
198 if (!TEST_ptr(fixture
->msg
= OSSL_CMP_MSG_dup(ir_unprotected
))
199 || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture
->cmp_ctx
, 1))) {
203 EXECUTE_TEST(execute_MSG_protect_test
, tear_down
);
207 static int test_MSG_protect_with_msg_sig_alg_protection_plus_rsa_key(void)
209 const size_t size
= sizeof(rand_data
) / 2;
211 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE
, set_up
);
212 fixture
->expected
= 1;
214 if (!TEST_ptr(fixture
->msg
= OSSL_CMP_MSG_dup(ir_unprotected
))
215 || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture
->cmp_ctx
, 0))
217 * Use half of the 16 bytes of random input
218 * for each reference and secret value
220 || !TEST_true(OSSL_CMP_CTX_set1_referenceValue(fixture
->cmp_ctx
,
222 || !TEST_true(OSSL_CMP_CTX_set1_secretValue(fixture
->cmp_ctx
,
228 EXECUTE_TEST(execute_MSG_protect_test
, tear_down
);
232 static int test_MSG_protect_with_certificate_and_key(void)
234 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE
, set_up
);
235 fixture
->expected
= 1;
237 if (!TEST_ptr(fixture
->msg
=
238 OSSL_CMP_MSG_dup(ir_unprotected
))
239 || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture
->cmp_ctx
, 0))
240 || !TEST_true(OSSL_CMP_CTX_set1_pkey(fixture
->cmp_ctx
, loadedkey
))
241 || !TEST_true(OSSL_CMP_CTX_set1_cert(fixture
->cmp_ctx
, cert
))) {
245 EXECUTE_TEST(execute_MSG_protect_test
, tear_down
);
249 static int test_MSG_protect_certificate_based_without_cert(void)
253 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE
, set_up
);
254 ctx
= fixture
->cmp_ctx
;
255 fixture
->expected
= 0;
256 if (!TEST_ptr(fixture
->msg
=
257 OSSL_CMP_MSG_dup(ir_unprotected
))
258 || !TEST_true(SET_OPT_UNPROTECTED_SEND(ctx
, 0))
259 || !TEST_true(OSSL_CMP_CTX_set0_newPkey(ctx
, 1, loadedkey
))) {
263 EVP_PKEY_up_ref(loadedkey
);
264 EXECUTE_TEST(execute_MSG_protect_test
, tear_down
);
268 static int test_MSG_protect_no_key_no_secret(void)
270 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE
, set_up
);
271 fixture
->expected
= 0;
272 if (!TEST_ptr(fixture
->msg
= OSSL_CMP_MSG_dup(ir_unprotected
))
273 || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture
->cmp_ctx
, 0))) {
277 EXECUTE_TEST(execute_MSG_protect_test
, tear_down
);
281 static int test_MSG_protect_pbmac_no_sender(int with_ref
)
283 static unsigned char secret
[] = { 47, 11, 8, 15 };
284 static unsigned char ref
[] = { 0xca, 0xfe, 0xba, 0xbe };
286 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE
, set_up
);
287 fixture
->expected
= with_ref
;
288 if (!TEST_ptr(fixture
->msg
= OSSL_CMP_MSG_dup(ir_unprotected
))
289 || !SET_OPT_UNPROTECTED_SEND(fixture
->cmp_ctx
, 0)
290 || !ossl_cmp_hdr_set1_sender(fixture
->msg
->header
, NULL
)
291 || !OSSL_CMP_CTX_set1_secretValue(fixture
->cmp_ctx
,
292 secret
, sizeof(secret
))
293 || (!OSSL_CMP_CTX_set1_referenceValue(fixture
->cmp_ctx
,
294 with_ref
? ref
: NULL
,
299 EXECUTE_TEST(execute_MSG_protect_test
, tear_down
);
303 static int test_MSG_protect_pbmac_no_sender_with_ref(void)
305 return test_MSG_protect_pbmac_no_sender(1);
308 static int test_MSG_protect_pbmac_no_sender_no_ref(void)
310 return test_MSG_protect_pbmac_no_sender(0);
313 static int execute_MSG_add_extraCerts_test(CMP_PROTECT_TEST_FIXTURE
*fixture
)
315 return TEST_true(ossl_cmp_msg_add_extraCerts(fixture
->cmp_ctx
,
319 static int test_MSG_add_extraCerts(void)
321 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE
, set_up
);
322 if (!TEST_ptr(fixture
->msg
= OSSL_CMP_MSG_dup(ir_protected
))) {
326 EXECUTE_TEST(execute_MSG_add_extraCerts_test
, tear_down
);
330 #ifndef OPENSSL_NO_EC
331 /* The cert chain tests use EC certs so we skip them in no-ec builds */
332 static int execute_cmp_build_cert_chain_test(CMP_PROTECT_TEST_FIXTURE
*fixture
)
335 OSSL_CMP_CTX
*ctx
= fixture
->cmp_ctx
;
336 STACK_OF(X509
) *chain
=
337 ossl_cmp_build_cert_chain(ctx
->libctx
, ctx
->propq
,
338 fixture
->certs
, fixture
->cert
);
340 if (TEST_ptr(chain
)) {
341 /* Check whether chain built is equal to the expected one */
342 ret
= TEST_int_eq(0, STACK_OF_X509_cmp(chain
, fixture
->chain
));
343 sk_X509_pop_free(chain
, X509_free
);
348 static int test_cmp_build_cert_chain(void)
350 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE
, set_up
);
351 fixture
->cert
= endentity2
;
352 if (!TEST_ptr(fixture
->certs
= sk_X509_new_null())
353 || !TEST_ptr(fixture
->chain
= sk_X509_new_null())
354 || !TEST_true(sk_X509_push(fixture
->certs
, endentity1
))
355 || !TEST_true(sk_X509_push(fixture
->certs
, root
))
356 || !TEST_true(sk_X509_push(fixture
->certs
, intermediate
))
357 || !TEST_true(sk_X509_push(fixture
->chain
, endentity2
))
358 || !TEST_true(sk_X509_push(fixture
->chain
, intermediate
))) {
362 EXECUTE_TEST(execute_cmp_build_cert_chain_test
, tear_down
);
366 static int test_cmp_build_cert_chain_missing_intermediate(void)
368 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE
, set_up
);
369 fixture
->cert
= endentity2
;
370 if (!TEST_ptr(fixture
->certs
= sk_X509_new_null())
371 || !TEST_ptr(fixture
->chain
= sk_X509_new_null())
372 || !TEST_true(sk_X509_push(fixture
->certs
, endentity1
))
373 || !TEST_true(sk_X509_push(fixture
->certs
, root
))
374 || !TEST_true(sk_X509_push(fixture
->chain
, endentity2
))) {
378 EXECUTE_TEST(execute_cmp_build_cert_chain_test
, tear_down
);
382 static int test_cmp_build_cert_chain_missing_root(void)
384 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE
, set_up
);
385 fixture
->cert
= endentity2
;
386 if (!TEST_ptr(fixture
->certs
= sk_X509_new_null())
387 || !TEST_ptr(fixture
->chain
= sk_X509_new_null())
388 || !TEST_true(sk_X509_push(fixture
->certs
, endentity1
))
389 || !TEST_true(sk_X509_push(fixture
->certs
, intermediate
))
390 || !TEST_true(sk_X509_push(fixture
->chain
, endentity2
))
391 || !TEST_true(sk_X509_push(fixture
->chain
, intermediate
))) {
395 EXECUTE_TEST(execute_cmp_build_cert_chain_test
, tear_down
);
399 static int test_cmp_build_cert_chain_no_certs(void)
401 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE
, set_up
);
402 fixture
->cert
= endentity2
;
403 if (!TEST_ptr(fixture
->certs
= sk_X509_new_null())
404 || !TEST_ptr(fixture
->chain
= sk_X509_new_null())
405 || !TEST_true(sk_X509_push(fixture
->chain
, endentity2
))) {
409 EXECUTE_TEST(execute_cmp_build_cert_chain_test
, tear_down
);
412 #endif /* OPENSSL_NO_EC */
414 static int execute_X509_STORE_test(CMP_PROTECT_TEST_FIXTURE
*fixture
)
416 X509_STORE
*store
= X509_STORE_new();
417 STACK_OF(X509
) *sk
= NULL
;
420 if (!TEST_true(ossl_cmp_X509_STORE_add1_certs(store
,
422 fixture
->callback_arg
)))
424 sk
= X509_STORE_get1_all_certs(store
);
425 if (!TEST_int_eq(0, STACK_OF_X509_cmp(sk
, fixture
->chain
)))
429 X509_STORE_free(store
);
430 sk_X509_pop_free(sk
, X509_free
);
435 static int test_X509_STORE(void)
437 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE
, set_up
);
438 fixture
->callback_arg
= 0; /* self-issued allowed */
439 if (!TEST_ptr(fixture
->certs
= sk_X509_new_null())
440 || !sk_X509_push(fixture
->certs
, endentity1
)
441 || !sk_X509_push(fixture
->certs
, endentity2
)
442 || !sk_X509_push(fixture
->certs
, root
)
443 || !sk_X509_push(fixture
->certs
, intermediate
)
444 || !TEST_ptr(fixture
->chain
= sk_X509_dup(fixture
->certs
))) {
448 EXECUTE_TEST(execute_X509_STORE_test
, tear_down
);
452 static int test_X509_STORE_only_self_issued(void)
454 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE
, set_up
);
455 fixture
->certs
= sk_X509_new_null();
456 fixture
->chain
= sk_X509_new_null();
457 fixture
->callback_arg
= 1; /* only self-issued */
458 if (!TEST_true(sk_X509_push(fixture
->certs
, endentity1
))
459 || !TEST_true(sk_X509_push(fixture
->certs
, endentity2
))
460 || !TEST_true(sk_X509_push(fixture
->certs
, root
))
461 || !TEST_true(sk_X509_push(fixture
->certs
, intermediate
))
462 || !TEST_true(sk_X509_push(fixture
->chain
, root
))) {
466 EXECUTE_TEST(execute_X509_STORE_test
, tear_down
);
471 void cleanup_tests(void)
473 EVP_PKEY_free(loadedprivkey
);
474 EVP_PKEY_free(loadedpubkey
);
475 EVP_PKEY_free(loadedkey
);
477 X509_free(endentity1
);
478 X509_free(endentity2
);
480 X509_free(intermediate
);
481 OSSL_CMP_MSG_free(ir_protected
);
482 OSSL_CMP_MSG_free(ir_unprotected
);
483 OPENSSL_CTX_free(libctx
);
486 #define USAGE "server.pem IR_protected.der IR_unprotected.der IP_PBM.der " \
487 "server.crt server.pem EndEntity1.crt EndEntity2.crt Root_CA.crt " \
488 "Intermediate_CA.crt module_name [module_conf_file]\n"
489 OPT_TEST_DECLARE_USAGE(USAGE
)
491 int setup_tests(void)
499 char *intermediate_f
;
501 if (!test_skip_common_options()) {
502 TEST_error("Error parsing test options\n");
506 RAND_bytes(rand_data
, OSSL_CMP_TRANSACTIONID_LENGTH
);
507 if (!TEST_ptr(server_f
= test_get_argument(0))
508 || !TEST_ptr(ir_protected_f
= test_get_argument(1))
509 || !TEST_ptr(ir_unprotected_f
= test_get_argument(2))
510 || !TEST_ptr(ip_PBM_f
= test_get_argument(3))
511 || !TEST_ptr(server_cert_f
= test_get_argument(4))
512 || !TEST_ptr(server_key_f
= test_get_argument(5))
513 || !TEST_ptr(endentity1_f
= test_get_argument(6))
514 || !TEST_ptr(endentity2_f
= test_get_argument(7))
515 || !TEST_ptr(root_f
= test_get_argument(8))
516 || !TEST_ptr(intermediate_f
= test_get_argument(9))) {
517 TEST_error("usage: cmp_protect_test %s", USAGE
);
521 if (!test_get_libctx(&libctx
, &default_null_provider
, &provider
, 10, USAGE
))
524 if (!TEST_ptr(loadedkey
= load_pem_key(server_key_f
))
525 || !TEST_ptr(cert
= load_pem_cert(server_cert_f
, libctx
)))
528 if (!TEST_ptr(loadedprivkey
= load_pem_key(server_f
)))
530 if (TEST_true(EVP_PKEY_up_ref(loadedprivkey
)))
531 loadedpubkey
= loadedprivkey
;
532 if (!TEST_ptr(ir_protected
= load_pkimsg(ir_protected_f
))
533 || !TEST_ptr(ir_unprotected
= load_pkimsg(ir_unprotected_f
)))
535 if (!TEST_ptr(endentity1
= load_pem_cert(endentity1_f
, libctx
))
536 || !TEST_ptr(endentity2
= load_pem_cert(endentity2_f
, libctx
))
537 || !TEST_ptr(root
= load_pem_cert(root_f
, libctx
))
538 || !TEST_ptr(intermediate
= load_pem_cert(intermediate_f
, libctx
)))
540 if (!TEST_int_eq(1, RAND_bytes(rand_data
, OSSL_CMP_TRANSACTIONID_LENGTH
)))
543 /* Message protection tests */
544 ADD_TEST(test_cmp_calc_protection_no_key_no_secret
);
545 ADD_TEST(test_cmp_calc_protection_pkey
);
546 ADD_TEST(test_cmp_calc_protection_pbmac
);
548 ADD_TEST(test_MSG_protect_with_msg_sig_alg_protection_plus_rsa_key
);
549 ADD_TEST(test_MSG_protect_with_certificate_and_key
);
550 ADD_TEST(test_MSG_protect_certificate_based_without_cert
);
551 ADD_TEST(test_MSG_protect_unprotected_request
);
552 ADD_TEST(test_MSG_protect_no_key_no_secret
);
553 ADD_TEST(test_MSG_protect_pbmac_no_sender_with_ref
);
554 ADD_TEST(test_MSG_protect_pbmac_no_sender_no_ref
);
555 ADD_TEST(test_MSG_add_extraCerts
);
557 #ifndef OPENSSL_NO_EC
558 ADD_TEST(test_cmp_build_cert_chain
);
559 ADD_TEST(test_cmp_build_cert_chain_missing_root
);
560 ADD_TEST(test_cmp_build_cert_chain_missing_intermediate
);
561 ADD_TEST(test_cmp_build_cert_chain_no_certs
);
564 ADD_TEST(test_X509_STORE
);
565 ADD_TEST(test_X509_STORE_only_self_issued
);