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"
14 static const char *ir_protected_f;
15 static const char *ir_unprotected_f;
16 static const char *ip_PBM_f;
18 typedef struct test_fixture {
19 const char *test_case_name;
20 OSSL_CMP_CTX *cmp_ctx;
21 /* for protection tests */
23 OSSL_CMP_PKISI *si; /* for error and response messages */
28 STACK_OF(X509) *certs;
29 STACK_OF(X509) *chain;
32 } CMP_PROTECT_TEST_FIXTURE;
34 static OSSL_LIB_CTX *libctx = NULL;
35 static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL;
37 static void tear_down(CMP_PROTECT_TEST_FIXTURE *fixture)
39 OSSL_CMP_CTX_free(fixture->cmp_ctx);
40 OSSL_CMP_MSG_free(fixture->msg);
41 OSSL_CMP_PKISI_free(fixture->si);
43 OPENSSL_free(fixture->mem);
44 sk_X509_free(fixture->certs);
45 sk_X509_free(fixture->chain);
47 OPENSSL_free(fixture);
50 static CMP_PROTECT_TEST_FIXTURE *set_up(const char *const test_case_name)
52 CMP_PROTECT_TEST_FIXTURE *fixture;
54 if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
56 fixture->test_case_name = test_case_name;
57 if (!TEST_ptr(fixture->cmp_ctx = OSSL_CMP_CTX_new(libctx, NULL))) {
64 static EVP_PKEY *loadedprivkey = NULL;
65 static EVP_PKEY *loadedpubkey = NULL;
66 static EVP_PKEY *loadedkey = NULL;
67 static X509 *cert = NULL;
68 static unsigned char rand_data[OSSL_CMP_TRANSACTIONID_LENGTH];
69 static OSSL_CMP_MSG *ir_unprotected, *ir_protected;
70 static X509 *endentity1 = NULL, *endentity2 = NULL,
71 *root = NULL, *intermediate = NULL;
73 static int execute_calc_protection_fails_test(CMP_PROTECT_TEST_FIXTURE *fixture)
75 ASN1_BIT_STRING *protection =
76 ossl_cmp_calc_protection(fixture->cmp_ctx, fixture->msg);
77 int res = TEST_ptr_null(protection);
79 ASN1_BIT_STRING_free(protection);
83 static int execute_calc_protection_pbmac_test(CMP_PROTECT_TEST_FIXTURE *fixture)
85 ASN1_BIT_STRING *protection =
86 ossl_cmp_calc_protection(fixture->cmp_ctx, fixture->msg);
87 int res = TEST_ptr(protection)
88 && TEST_true(ASN1_STRING_cmp(protection,
89 fixture->msg->protection) == 0);
91 ASN1_BIT_STRING_free(protection);
96 * This function works similarly to parts of CMP_verify_signature in cmp_vfy.c,
97 * but without the need for a OSSL_CMP_CTX or a X509 certificate
99 static int verify_signature(OSSL_CMP_MSG *msg,
100 ASN1_BIT_STRING *protection,
101 EVP_PKEY *pkey, EVP_MD *digest)
103 OSSL_CMP_PROTECTEDPART prot_part;
104 unsigned char *prot_part_der = NULL;
106 EVP_MD_CTX *ctx = NULL;
109 prot_part.header = OSSL_CMP_MSG_get0_header(msg);
110 prot_part.body = msg->body;
111 len = i2d_OSSL_CMP_PROTECTEDPART(&prot_part, &prot_part_der);
114 && TEST_ptr(ctx = EVP_MD_CTX_new())
115 && TEST_true(EVP_DigestVerifyInit(ctx, NULL, digest, NULL, pkey))
116 && TEST_int_eq(EVP_DigestVerify(ctx, protection->data,
118 prot_part_der, len), 1);
120 EVP_MD_CTX_free(ctx);
121 OPENSSL_free(prot_part_der);
125 /* Calls OSSL_CMP_calc_protection and compares and verifies signature */
126 static int execute_calc_protection_signature_test(CMP_PROTECT_TEST_FIXTURE *
129 ASN1_BIT_STRING *protection =
130 ossl_cmp_calc_protection(fixture->cmp_ctx, fixture->msg);
131 int ret = (TEST_ptr(protection)
132 && TEST_true(ASN1_STRING_cmp(protection,
133 fixture->msg->protection) == 0)
134 && TEST_true(verify_signature(fixture->msg, protection,
136 fixture->cmp_ctx->digest)));
138 ASN1_BIT_STRING_free(protection);
142 static int test_cmp_calc_protection_no_key_no_secret(void)
144 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
145 if (!TEST_ptr(fixture->msg = load_pkimsg(ir_unprotected_f))
146 || !TEST_ptr(fixture->msg->header->protectionAlg =
147 X509_ALGOR_new() /* no specific alg needed here */)) {
152 EXECUTE_TEST(execute_calc_protection_fails_test, tear_down);
156 static int test_cmp_calc_protection_pkey(void)
158 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
159 fixture->pubkey = loadedpubkey;
160 if (!TEST_true(OSSL_CMP_CTX_set1_pkey(fixture->cmp_ctx, loadedprivkey))
161 || !TEST_ptr(fixture->msg = load_pkimsg(ir_protected_f))) {
165 EXECUTE_TEST(execute_calc_protection_signature_test, tear_down);
169 static int test_cmp_calc_protection_pbmac(void)
171 unsigned char sec_insta[] = { 'i', 'n', 's', 't', 'a' };
173 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
174 if (!TEST_true(OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx,
175 sec_insta, sizeof(sec_insta)))
176 || !TEST_ptr(fixture->msg = load_pkimsg(ip_PBM_f))) {
180 EXECUTE_TEST(execute_calc_protection_pbmac_test, tear_down);
183 static int execute_MSG_protect_test(CMP_PROTECT_TEST_FIXTURE *fixture)
185 return TEST_int_eq(fixture->expected,
186 ossl_cmp_msg_protect(fixture->cmp_ctx, fixture->msg));
189 #define SET_OPT_UNPROTECTED_SEND(ctx, val) \
190 OSSL_CMP_CTX_set_option((ctx), OSSL_CMP_OPT_UNPROTECTED_SEND, (val))
191 static int test_MSG_protect_unprotected_request(void)
193 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
195 fixture->expected = 1;
196 if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected))
197 || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 1))) {
201 EXECUTE_TEST(execute_MSG_protect_test, tear_down);
205 static int test_MSG_protect_with_msg_sig_alg_protection_plus_rsa_key(void)
207 const size_t size = sizeof(rand_data) / 2;
209 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
210 fixture->expected = 1;
212 if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected))
213 || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0))
215 * Use half of the 16 bytes of random input
216 * for each reference and secret value
218 || !TEST_true(OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx,
220 || !TEST_true(OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx,
226 EXECUTE_TEST(execute_MSG_protect_test, tear_down);
230 static int test_MSG_protect_with_certificate_and_key(void)
232 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
233 fixture->expected = 1;
235 if (!TEST_ptr(fixture->msg =
236 OSSL_CMP_MSG_dup(ir_unprotected))
237 || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0))
238 || !TEST_true(OSSL_CMP_CTX_set1_pkey(fixture->cmp_ctx, loadedkey))
239 || !TEST_true(OSSL_CMP_CTX_set1_cert(fixture->cmp_ctx, cert))) {
243 EXECUTE_TEST(execute_MSG_protect_test, tear_down);
247 static int test_MSG_protect_certificate_based_without_cert(void)
251 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
252 ctx = fixture->cmp_ctx;
253 fixture->expected = 0;
254 if (!TEST_ptr(fixture->msg =
255 OSSL_CMP_MSG_dup(ir_unprotected))
256 || !TEST_true(SET_OPT_UNPROTECTED_SEND(ctx, 0))
257 || !TEST_true(OSSL_CMP_CTX_set0_newPkey(ctx, 1, loadedkey))) {
261 EVP_PKEY_up_ref(loadedkey);
262 EXECUTE_TEST(execute_MSG_protect_test, tear_down);
266 static int test_MSG_protect_no_key_no_secret(void)
268 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
269 fixture->expected = 0;
270 if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected))
271 || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0))) {
275 EXECUTE_TEST(execute_MSG_protect_test, tear_down);
279 static int test_MSG_protect_pbmac_no_sender(int with_ref)
281 static unsigned char secret[] = { 47, 11, 8, 15 };
282 static unsigned char ref[] = { 0xca, 0xfe, 0xba, 0xbe };
284 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
285 fixture->expected = with_ref;
286 if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected))
287 || !SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0)
288 || !ossl_cmp_hdr_set1_sender(fixture->msg->header, NULL)
289 || !OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx,
290 secret, sizeof(secret))
291 || (!OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx,
292 with_ref ? ref : NULL,
297 EXECUTE_TEST(execute_MSG_protect_test, tear_down);
301 static int test_MSG_protect_pbmac_no_sender_with_ref(void)
303 return test_MSG_protect_pbmac_no_sender(1);
306 static int test_MSG_protect_pbmac_no_sender_no_ref(void)
308 return test_MSG_protect_pbmac_no_sender(0);
311 static int execute_MSG_add_extraCerts_test(CMP_PROTECT_TEST_FIXTURE *fixture)
313 return TEST_true(ossl_cmp_msg_add_extraCerts(fixture->cmp_ctx,
317 static int test_MSG_add_extraCerts(void)
319 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
320 if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_protected))) {
324 EXECUTE_TEST(execute_MSG_add_extraCerts_test, tear_down);
328 #ifndef OPENSSL_NO_EC
329 /* The cert chain tests use EC certs so we skip them in no-ec builds */
330 static int execute_cmp_build_cert_chain_test(CMP_PROTECT_TEST_FIXTURE *fixture)
333 OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
335 STACK_OF(X509) *chain =
336 ossl_cmp_build_cert_chain(ctx->libctx, ctx->propq, NULL,
337 fixture->certs, fixture->cert);
339 if (TEST_ptr(chain)) {
340 /* Check whether chain built is equal to the expected one */
341 ret = TEST_int_eq(0, STACK_OF_X509_cmp(chain, fixture->chain));
342 sk_X509_pop_free(chain, X509_free);
347 if (TEST_ptr(store = X509_STORE_new())
348 && TEST_true(X509_STORE_add_cert(store, root))) {
349 X509_VERIFY_PARAM_set_flags(X509_STORE_get0_param(store),
350 X509_V_FLAG_NO_CHECK_TIME);
351 chain = ossl_cmp_build_cert_chain(ctx->libctx, ctx->propq,
352 store, fixture->certs, fixture->cert);
353 ret = TEST_int_eq(fixture->expected, chain != NULL);
354 if (ret && chain != NULL) {
355 /* Check whether chain built is equal to the expected one */
356 ret = TEST_int_eq(0, STACK_OF_X509_cmp(chain, fixture->chain));
357 sk_X509_pop_free(chain, X509_free);
360 X509_STORE_free(store);
364 static int test_cmp_build_cert_chain(void)
366 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
367 fixture->expected = 1;
368 fixture->cert = endentity2;
369 if (!TEST_ptr(fixture->certs = sk_X509_new_null())
370 || !TEST_ptr(fixture->chain = sk_X509_new_null())
371 || !TEST_true(sk_X509_push(fixture->certs, endentity1))
372 || !TEST_true(sk_X509_push(fixture->certs, root))
373 || !TEST_true(sk_X509_push(fixture->certs, intermediate))
374 || !TEST_true(sk_X509_push(fixture->chain, endentity2))
375 || !TEST_true(sk_X509_push(fixture->chain, intermediate))) {
379 EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down);
383 static int test_cmp_build_cert_chain_missing_intermediate(void)
385 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
386 fixture->expected = 0;
387 fixture->cert = endentity2;
388 if (!TEST_ptr(fixture->certs = sk_X509_new_null())
389 || !TEST_ptr(fixture->chain = sk_X509_new_null())
390 || !TEST_true(sk_X509_push(fixture->certs, endentity1))
391 || !TEST_true(sk_X509_push(fixture->certs, root))
392 || !TEST_true(sk_X509_push(fixture->chain, endentity2))) {
396 EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down);
400 static int test_cmp_build_cert_chain_no_root(void)
402 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
403 fixture->expected = 1;
404 fixture->cert = endentity2;
405 if (!TEST_ptr(fixture->certs = sk_X509_new_null())
406 || !TEST_ptr(fixture->chain = sk_X509_new_null())
407 || !TEST_true(sk_X509_push(fixture->certs, endentity1))
408 || !TEST_true(sk_X509_push(fixture->certs, intermediate))
409 || !TEST_true(sk_X509_push(fixture->chain, endentity2))
410 || !TEST_true(sk_X509_push(fixture->chain, intermediate))) {
414 EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down);
418 static int test_cmp_build_cert_chain_no_certs(void)
420 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
421 fixture->expected = 0;
422 fixture->cert = endentity2;
423 if (!TEST_ptr(fixture->certs = sk_X509_new_null())
424 || !TEST_ptr(fixture->chain = sk_X509_new_null())
425 || !TEST_true(sk_X509_push(fixture->chain, endentity2))) {
429 EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down);
432 #endif /* OPENSSL_NO_EC */
434 static int execute_X509_STORE_test(CMP_PROTECT_TEST_FIXTURE *fixture)
436 X509_STORE *store = X509_STORE_new();
437 STACK_OF(X509) *sk = NULL;
440 if (!TEST_true(ossl_cmp_X509_STORE_add1_certs(store,
442 fixture->callback_arg)))
444 sk = X509_STORE_get1_all_certs(store);
445 if (!TEST_int_eq(0, STACK_OF_X509_cmp(sk, fixture->chain)))
449 X509_STORE_free(store);
450 sk_X509_pop_free(sk, X509_free);
455 static int test_X509_STORE(void)
457 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
458 fixture->callback_arg = 0; /* self-issued allowed */
459 if (!TEST_ptr(fixture->certs = sk_X509_new_null())
460 || !sk_X509_push(fixture->certs, endentity1)
461 || !sk_X509_push(fixture->certs, endentity2)
462 || !sk_X509_push(fixture->certs, root)
463 || !sk_X509_push(fixture->certs, intermediate)
464 || !TEST_ptr(fixture->chain = sk_X509_dup(fixture->certs))) {
468 EXECUTE_TEST(execute_X509_STORE_test, tear_down);
472 static int test_X509_STORE_only_self_issued(void)
474 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
475 fixture->certs = sk_X509_new_null();
476 fixture->chain = sk_X509_new_null();
477 fixture->callback_arg = 1; /* only self-issued */
478 if (!TEST_true(sk_X509_push(fixture->certs, endentity1))
479 || !TEST_true(sk_X509_push(fixture->certs, endentity2))
480 || !TEST_true(sk_X509_push(fixture->certs, root))
481 || !TEST_true(sk_X509_push(fixture->certs, intermediate))
482 || !TEST_true(sk_X509_push(fixture->chain, root))) {
486 EXECUTE_TEST(execute_X509_STORE_test, tear_down);
491 void cleanup_tests(void)
493 EVP_PKEY_free(loadedprivkey);
494 EVP_PKEY_free(loadedpubkey);
495 EVP_PKEY_free(loadedkey);
497 X509_free(endentity1);
498 X509_free(endentity2);
500 X509_free(intermediate);
501 OSSL_CMP_MSG_free(ir_protected);
502 OSSL_CMP_MSG_free(ir_unprotected);
503 OSSL_LIB_CTX_free(libctx);
506 #define USAGE "server.pem IR_protected.der IR_unprotected.der IP_PBM.der " \
507 "server.crt server.pem EndEntity1.crt EndEntity2.crt Root_CA.crt " \
508 "Intermediate_CA.crt module_name [module_conf_file]\n"
509 OPT_TEST_DECLARE_USAGE(USAGE)
511 int setup_tests(void)
519 char *intermediate_f;
521 if (!test_skip_common_options()) {
522 TEST_error("Error parsing test options\n");
526 RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH);
527 if (!TEST_ptr(server_f = test_get_argument(0))
528 || !TEST_ptr(ir_protected_f = test_get_argument(1))
529 || !TEST_ptr(ir_unprotected_f = test_get_argument(2))
530 || !TEST_ptr(ip_PBM_f = test_get_argument(3))
531 || !TEST_ptr(server_cert_f = test_get_argument(4))
532 || !TEST_ptr(server_key_f = test_get_argument(5))
533 || !TEST_ptr(endentity1_f = test_get_argument(6))
534 || !TEST_ptr(endentity2_f = test_get_argument(7))
535 || !TEST_ptr(root_f = test_get_argument(8))
536 || !TEST_ptr(intermediate_f = test_get_argument(9))) {
537 TEST_error("usage: cmp_protect_test %s", USAGE);
541 if (!test_get_libctx(&libctx, &default_null_provider, &provider, 10, USAGE))
544 if (!TEST_ptr(loadedkey = load_pem_key(server_key_f, libctx))
545 || !TEST_ptr(cert = load_pem_cert(server_cert_f, libctx)))
548 if (!TEST_ptr(loadedprivkey = load_pem_key(server_f, libctx)))
550 if (TEST_true(EVP_PKEY_up_ref(loadedprivkey)))
551 loadedpubkey = loadedprivkey;
552 if (!TEST_ptr(ir_protected = load_pkimsg(ir_protected_f))
553 || !TEST_ptr(ir_unprotected = load_pkimsg(ir_unprotected_f)))
555 if (!TEST_ptr(endentity1 = load_pem_cert(endentity1_f, libctx))
556 || !TEST_ptr(endentity2 = load_pem_cert(endentity2_f, libctx))
557 || !TEST_ptr(root = load_pem_cert(root_f, libctx))
558 || !TEST_ptr(intermediate = load_pem_cert(intermediate_f, libctx)))
560 if (!TEST_int_eq(1, RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH)))
563 /* Message protection tests */
564 ADD_TEST(test_cmp_calc_protection_no_key_no_secret);
565 ADD_TEST(test_cmp_calc_protection_pkey);
566 ADD_TEST(test_cmp_calc_protection_pbmac);
568 ADD_TEST(test_MSG_protect_with_msg_sig_alg_protection_plus_rsa_key);
569 ADD_TEST(test_MSG_protect_with_certificate_and_key);
570 ADD_TEST(test_MSG_protect_certificate_based_without_cert);
571 ADD_TEST(test_MSG_protect_unprotected_request);
572 ADD_TEST(test_MSG_protect_no_key_no_secret);
573 ADD_TEST(test_MSG_protect_pbmac_no_sender_with_ref);
574 ADD_TEST(test_MSG_protect_pbmac_no_sender_no_ref);
575 ADD_TEST(test_MSG_add_extraCerts);
577 #ifndef OPENSSL_NO_EC
578 ADD_TEST(test_cmp_build_cert_chain);
579 ADD_TEST(test_cmp_build_cert_chain_no_root);
580 ADD_TEST(test_cmp_build_cert_chain_missing_intermediate);
581 ADD_TEST(test_cmp_build_cert_chain_no_certs);
584 ADD_TEST(test_X509_STORE);
585 ADD_TEST(test_X509_STORE_only_self_issued);