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 void tear_down(CMP_PROTECT_TEST_FIXTURE *fixture)
38 OSSL_CMP_CTX_free(fixture->cmp_ctx);
39 OSSL_CMP_MSG_free(fixture->msg);
40 OSSL_CMP_PKISI_free(fixture->si);
42 OPENSSL_free(fixture->mem);
43 sk_X509_free(fixture->certs);
44 sk_X509_free(fixture->chain);
46 OPENSSL_free(fixture);
49 static CMP_PROTECT_TEST_FIXTURE *set_up(const char *const test_case_name)
51 CMP_PROTECT_TEST_FIXTURE *fixture;
53 if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
55 fixture->test_case_name = test_case_name;
56 if (!TEST_ptr(fixture->cmp_ctx = OSSL_CMP_CTX_new(NULL, NULL))) {
63 static EVP_PKEY *loadedprivkey = NULL;
64 static EVP_PKEY *loadedpubkey = NULL;
65 static EVP_PKEY *loadedkey = NULL;
66 static X509 *cert = NULL;
67 static unsigned char rand_data[OSSL_CMP_TRANSACTIONID_LENGTH];
68 static OSSL_CMP_MSG *ir_unprotected, *ir_protected;
69 static X509 *endentity1 = NULL, *endentity2 = NULL,
70 *root = NULL, *intermediate = NULL;
72 static int execute_calc_protection_fails_test(CMP_PROTECT_TEST_FIXTURE *fixture)
74 ASN1_BIT_STRING *protection =
75 ossl_cmp_calc_protection(fixture->cmp_ctx, fixture->msg);
76 int res = TEST_ptr_null(protection);
78 ASN1_BIT_STRING_free(protection);
82 static int execute_calc_protection_pbmac_test(CMP_PROTECT_TEST_FIXTURE *fixture)
84 ASN1_BIT_STRING *protection =
85 ossl_cmp_calc_protection(fixture->cmp_ctx, fixture->msg);
86 int res = TEST_ptr(protection)
87 && TEST_true(ASN1_STRING_cmp(protection,
88 fixture->msg->protection) == 0);
90 ASN1_BIT_STRING_free(protection);
95 * This function works similarly to parts of CMP_verify_signature in cmp_vfy.c,
96 * but without the need for a OSSL_CMP_CTX or a X509 certificate
98 static int verify_signature(OSSL_CMP_MSG *msg,
99 ASN1_BIT_STRING *protection,
100 EVP_PKEY *pkey, EVP_MD *digest)
102 OSSL_CMP_PROTECTEDPART prot_part;
103 unsigned char *prot_part_der = NULL;
105 EVP_MD_CTX *ctx = NULL;
108 prot_part.header = OSSL_CMP_MSG_get0_header(msg);
109 prot_part.body = msg->body;
110 len = i2d_OSSL_CMP_PROTECTEDPART(&prot_part, &prot_part_der);
113 && TEST_ptr(ctx = EVP_MD_CTX_new())
114 && TEST_true(EVP_DigestVerifyInit(ctx, NULL, digest, NULL, pkey))
115 && TEST_int_eq(EVP_DigestVerify(ctx, protection->data,
117 prot_part_der, len), 1);
119 EVP_MD_CTX_free(ctx);
120 OPENSSL_free(prot_part_der);
124 /* Calls OSSL_CMP_calc_protection and compares and verifies signature */
125 static int execute_calc_protection_signature_test(CMP_PROTECT_TEST_FIXTURE *
128 ASN1_BIT_STRING *protection =
129 ossl_cmp_calc_protection(fixture->cmp_ctx, fixture->msg);
130 int ret = (TEST_ptr(protection)
131 && TEST_true(ASN1_STRING_cmp(protection,
132 fixture->msg->protection) == 0)
133 && TEST_true(verify_signature(fixture->msg, protection,
135 fixture->cmp_ctx->digest)));
137 ASN1_BIT_STRING_free(protection);
141 static int test_cmp_calc_protection_no_key_no_secret(void)
143 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
144 if (!TEST_ptr(fixture->msg = load_pkimsg(ir_unprotected_f))
145 || !TEST_ptr(fixture->msg->header->protectionAlg =
146 X509_ALGOR_new() /* no specific alg needed here */)) {
151 EXECUTE_TEST(execute_calc_protection_fails_test, tear_down);
155 static int test_cmp_calc_protection_pkey(void)
157 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
158 fixture->pubkey = loadedpubkey;
159 if (!TEST_true(OSSL_CMP_CTX_set1_pkey(fixture->cmp_ctx, loadedprivkey))
160 || !TEST_ptr(fixture->msg = load_pkimsg(ir_protected_f))) {
164 EXECUTE_TEST(execute_calc_protection_signature_test, tear_down);
168 static int test_cmp_calc_protection_pbmac(void)
170 unsigned char sec_insta[] = { 'i', 'n', 's', 't', 'a' };
172 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
173 if (!TEST_true(OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx,
174 sec_insta, sizeof(sec_insta)))
175 || !TEST_ptr(fixture->msg = load_pkimsg(ip_PBM_f))) {
179 EXECUTE_TEST(execute_calc_protection_pbmac_test, tear_down);
182 static int execute_MSG_protect_test(CMP_PROTECT_TEST_FIXTURE *fixture)
184 return TEST_int_eq(fixture->expected,
185 ossl_cmp_msg_protect(fixture->cmp_ctx, fixture->msg));
188 #define SET_OPT_UNPROTECTED_SEND(ctx, val) \
189 OSSL_CMP_CTX_set_option((ctx), OSSL_CMP_OPT_UNPROTECTED_SEND, (val))
190 static int test_MSG_protect_unprotected_request(void)
192 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
194 fixture->expected = 1;
195 if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected))
196 || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 1))) {
200 EXECUTE_TEST(execute_MSG_protect_test, tear_down);
204 static int test_MSG_protect_with_msg_sig_alg_protection_plus_rsa_key(void)
206 const size_t size = sizeof(rand_data) / 2;
208 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
209 fixture->expected = 1;
211 if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected))
212 || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0))
214 * Use half of the 16 bytes of random input
215 * for each reference and secret value
217 || !TEST_true(OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx,
219 || !TEST_true(OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx,
225 EXECUTE_TEST(execute_MSG_protect_test, tear_down);
229 static int test_MSG_protect_with_certificate_and_key(void)
231 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
232 fixture->expected = 1;
234 if (!TEST_ptr(fixture->msg =
235 OSSL_CMP_MSG_dup(ir_unprotected))
236 || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0))
237 || !TEST_true(OSSL_CMP_CTX_set1_pkey(fixture->cmp_ctx, loadedkey))
238 || !TEST_true(OSSL_CMP_CTX_set1_cert(fixture->cmp_ctx, cert))) {
242 EXECUTE_TEST(execute_MSG_protect_test, tear_down);
246 static int test_MSG_protect_certificate_based_without_cert(void)
250 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
251 ctx = fixture->cmp_ctx;
252 fixture->expected = 0;
253 if (!TEST_ptr(fixture->msg =
254 OSSL_CMP_MSG_dup(ir_unprotected))
255 || !TEST_true(SET_OPT_UNPROTECTED_SEND(ctx, 0))
256 || !TEST_true(OSSL_CMP_CTX_set0_newPkey(ctx, 1, loadedkey))) {
260 EVP_PKEY_up_ref(loadedkey);
261 EXECUTE_TEST(execute_MSG_protect_test, tear_down);
265 static int test_MSG_protect_no_key_no_secret(void)
267 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
268 fixture->expected = 0;
269 if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected))
270 || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0))) {
274 EXECUTE_TEST(execute_MSG_protect_test, tear_down);
278 static int test_MSG_protect_pbmac_no_sender(int with_ref)
280 static unsigned char secret[] = { 47, 11, 8, 15 };
281 static unsigned char ref[] = { 0xca, 0xfe, 0xba, 0xbe };
283 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
284 fixture->expected = with_ref;
285 if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected))
286 || !SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0)
287 || !ossl_cmp_hdr_set1_sender(fixture->msg->header, NULL)
288 || !OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx,
289 secret, sizeof(secret))
290 || (!OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx,
291 with_ref ? ref : NULL,
296 EXECUTE_TEST(execute_MSG_protect_test, tear_down);
300 static int test_MSG_protect_pbmac_no_sender_with_ref(void)
302 return test_MSG_protect_pbmac_no_sender(1);
305 static int test_MSG_protect_pbmac_no_sender_no_ref(void)
307 return test_MSG_protect_pbmac_no_sender(0);
310 static int execute_MSG_add_extraCerts_test(CMP_PROTECT_TEST_FIXTURE *fixture)
312 return TEST_true(ossl_cmp_msg_add_extraCerts(fixture->cmp_ctx,
316 static int test_MSG_add_extraCerts(void)
318 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
319 if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_protected))) {
323 EXECUTE_TEST(execute_MSG_add_extraCerts_test, tear_down);
327 #ifndef OPENSSL_NO_EC
328 /* The cert chain tests use EC certs so we skip them in no-ec builds */
329 static int execute_cmp_build_cert_chain_test(CMP_PROTECT_TEST_FIXTURE *fixture)
332 OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
333 STACK_OF(X509) *chain =
334 ossl_cmp_build_cert_chain(ctx->libctx, ctx->propq,
335 fixture->certs, fixture->cert);
337 if (TEST_ptr(chain)) {
338 /* Check whether chain built is equal to the expected one */
339 ret = TEST_int_eq(0, STACK_OF_X509_cmp(chain, fixture->chain));
340 sk_X509_pop_free(chain, X509_free);
345 static int test_cmp_build_cert_chain(void)
347 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
348 fixture->cert = endentity2;
349 if (!TEST_ptr(fixture->certs = sk_X509_new_null())
350 || !TEST_ptr(fixture->chain = sk_X509_new_null())
351 || !TEST_true(sk_X509_push(fixture->certs, endentity1))
352 || !TEST_true(sk_X509_push(fixture->certs, root))
353 || !TEST_true(sk_X509_push(fixture->certs, intermediate))
354 || !TEST_true(sk_X509_push(fixture->chain, endentity2))
355 || !TEST_true(sk_X509_push(fixture->chain, intermediate))) {
359 EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down);
363 static int test_cmp_build_cert_chain_missing_intermediate(void)
365 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
366 fixture->cert = endentity2;
367 if (!TEST_ptr(fixture->certs = sk_X509_new_null())
368 || !TEST_ptr(fixture->chain = sk_X509_new_null())
369 || !TEST_true(sk_X509_push(fixture->certs, endentity1))
370 || !TEST_true(sk_X509_push(fixture->certs, root))
371 || !TEST_true(sk_X509_push(fixture->chain, endentity2))) {
375 EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down);
379 static int test_cmp_build_cert_chain_missing_root(void)
381 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
382 fixture->cert = endentity2;
383 if (!TEST_ptr(fixture->certs = sk_X509_new_null())
384 || !TEST_ptr(fixture->chain = sk_X509_new_null())
385 || !TEST_true(sk_X509_push(fixture->certs, endentity1))
386 || !TEST_true(sk_X509_push(fixture->certs, intermediate))
387 || !TEST_true(sk_X509_push(fixture->chain, endentity2))
388 || !TEST_true(sk_X509_push(fixture->chain, intermediate))) {
392 EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down);
396 static int test_cmp_build_cert_chain_no_certs(void)
398 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
399 fixture->cert = endentity2;
400 if (!TEST_ptr(fixture->certs = sk_X509_new_null())
401 || !TEST_ptr(fixture->chain = sk_X509_new_null())
402 || !TEST_true(sk_X509_push(fixture->chain, endentity2))) {
406 EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down);
409 #endif /* OPENSSL_NO_EC */
411 static int execute_X509_STORE_test(CMP_PROTECT_TEST_FIXTURE *fixture)
413 X509_STORE *store = X509_STORE_new();
414 STACK_OF(X509) *sk = NULL;
417 if (!TEST_true(ossl_cmp_X509_STORE_add1_certs(store,
419 fixture->callback_arg)))
421 sk = X509_STORE_get1_all_certs(store);
422 if (!TEST_int_eq(0, STACK_OF_X509_cmp(sk, fixture->chain)))
426 X509_STORE_free(store);
427 sk_X509_pop_free(sk, X509_free);
432 static int test_X509_STORE(void)
434 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
435 fixture->callback_arg = 0; /* self-issued allowed */
436 if (!TEST_ptr(fixture->certs = sk_X509_new_null())
437 || !sk_X509_push(fixture->certs, endentity1)
438 || !sk_X509_push(fixture->certs, endentity2)
439 || !sk_X509_push(fixture->certs, root)
440 || !sk_X509_push(fixture->certs, intermediate)
441 || !TEST_ptr(fixture->chain = sk_X509_dup(fixture->certs))) {
445 EXECUTE_TEST(execute_X509_STORE_test, tear_down);
449 static int test_X509_STORE_only_self_issued(void)
451 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
452 fixture->certs = sk_X509_new_null();
453 fixture->chain = sk_X509_new_null();
454 fixture->callback_arg = 1; /* only self-issued */
455 if (!TEST_true(sk_X509_push(fixture->certs, endentity1))
456 || !TEST_true(sk_X509_push(fixture->certs, endentity2))
457 || !TEST_true(sk_X509_push(fixture->certs, root))
458 || !TEST_true(sk_X509_push(fixture->certs, intermediate))
459 || !TEST_true(sk_X509_push(fixture->chain, root))) {
463 EXECUTE_TEST(execute_X509_STORE_test, tear_down);
468 void cleanup_tests(void)
470 EVP_PKEY_free(loadedprivkey);
471 EVP_PKEY_free(loadedpubkey);
472 EVP_PKEY_free(loadedkey);
474 X509_free(endentity1);
475 X509_free(endentity2);
477 X509_free(intermediate);
478 OSSL_CMP_MSG_free(ir_protected);
479 OSSL_CMP_MSG_free(ir_unprotected);
483 int setup_tests(void)
491 char *intermediate_f;
493 if (!test_skip_common_options()) {
494 TEST_error("Error parsing test options\n");
498 RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH);
499 if (!TEST_ptr(server_f = test_get_argument(0))
500 || !TEST_ptr(ir_protected_f = test_get_argument(1))
501 || !TEST_ptr(ir_unprotected_f = test_get_argument(2))
502 || !TEST_ptr(ip_PBM_f = test_get_argument(3))
503 || !TEST_ptr(server_cert_f = test_get_argument(4))
504 || !TEST_ptr(server_key_f = test_get_argument(5))
505 || !TEST_ptr(endentity1_f = test_get_argument(6))
506 || !TEST_ptr(endentity2_f = test_get_argument(7))
507 || !TEST_ptr(root_f = test_get_argument(8))
508 || !TEST_ptr(intermediate_f = test_get_argument(9))) {
509 TEST_error("usage: cmp_protect_test server.pem "
510 "IR_protected.der IR_unprotected.der IP_PBM.der "
511 "server.crt server.pem"
512 "EndEntity1.crt EndEntity2.crt "
513 "Root_CA.crt Intermediate_CA.crt\n");
516 if (!TEST_ptr(loadedkey = load_pem_key(server_key_f))
517 || !TEST_ptr(cert = load_pem_cert(server_cert_f, NULL)))
520 if (!TEST_ptr(loadedprivkey = load_pem_key(server_f)))
522 if (TEST_true(EVP_PKEY_up_ref(loadedprivkey)))
523 loadedpubkey = loadedprivkey;
524 if (!TEST_ptr(ir_protected = load_pkimsg(ir_protected_f))
525 || !TEST_ptr(ir_unprotected = load_pkimsg(ir_unprotected_f)))
527 if (!TEST_ptr(endentity1 = load_pem_cert(endentity1_f, NULL))
528 || !TEST_ptr(endentity2 = load_pem_cert(endentity2_f, NULL))
529 || !TEST_ptr(root = load_pem_cert(root_f, NULL))
530 || !TEST_ptr(intermediate = load_pem_cert(intermediate_f, NULL)))
532 if (!TEST_int_eq(1, RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH)))
535 /* Message protection tests */
536 ADD_TEST(test_cmp_calc_protection_no_key_no_secret);
537 ADD_TEST(test_cmp_calc_protection_pkey);
538 ADD_TEST(test_cmp_calc_protection_pbmac);
540 ADD_TEST(test_MSG_protect_with_msg_sig_alg_protection_plus_rsa_key);
541 ADD_TEST(test_MSG_protect_with_certificate_and_key);
542 ADD_TEST(test_MSG_protect_certificate_based_without_cert);
543 ADD_TEST(test_MSG_protect_unprotected_request);
544 ADD_TEST(test_MSG_protect_no_key_no_secret);
545 ADD_TEST(test_MSG_protect_pbmac_no_sender_with_ref);
546 ADD_TEST(test_MSG_protect_pbmac_no_sender_no_ref);
547 ADD_TEST(test_MSG_add_extraCerts);
549 #ifndef OPENSSL_NO_EC
550 ADD_TEST(test_cmp_build_cert_chain);
551 ADD_TEST(test_cmp_build_cert_chain_missing_root);
552 ADD_TEST(test_cmp_build_cert_chain_missing_intermediate);
553 ADD_TEST(test_cmp_build_cert_chain_no_certs);
556 ADD_TEST(test_X509_STORE);
557 ADD_TEST(test_X509_STORE_only_self_issued);