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 */
26 ASN1_OCTET_STRING *secret;
32 STACK_OF(X509) *certs;
33 STACK_OF(X509) *chain;
36 } CMP_PROTECT_TEST_FIXTURE;
38 static void tear_down(CMP_PROTECT_TEST_FIXTURE *fixture)
40 OSSL_CMP_CTX_free(fixture->cmp_ctx);
41 OSSL_CMP_MSG_free(fixture->msg);
42 ASN1_OCTET_STRING_free(fixture->secret);
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(NULL, 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->msg, fixture->secret,
80 int res = TEST_ptr_null(protection);
82 ASN1_BIT_STRING_free(protection);
86 static int execute_calc_protection_pbmac_test(CMP_PROTECT_TEST_FIXTURE *fixture)
88 ASN1_BIT_STRING *protection =
89 ossl_cmp_calc_protection(fixture->msg, fixture->secret, NULL);
90 int res = TEST_ptr(protection)
91 && TEST_true(ASN1_STRING_cmp(protection,
92 fixture->msg->protection) == 0);
94 ASN1_BIT_STRING_free(protection);
99 * This function works similarly to parts of CMP_verify_signature in cmp_vfy.c,
100 * but without the need for a OSSL_CMP_CTX or a X509 certificate
102 static int verify_signature(OSSL_CMP_MSG *msg,
103 ASN1_BIT_STRING *protection,
104 EVP_PKEY *pkey, int digest_nid)
106 OSSL_CMP_PROTECTEDPART prot_part;
107 unsigned char *prot_part_der = NULL;
109 EVP_MD_CTX *ctx = NULL;
110 const EVP_MD *digest = EVP_get_digestbynid(digest_nid);
113 prot_part.header = OSSL_CMP_MSG_get0_header(msg);
114 prot_part.body = msg->body;
115 len = i2d_OSSL_CMP_PROTECTEDPART(&prot_part, &prot_part_der);
118 && TEST_ptr(ctx = EVP_MD_CTX_new())
119 && TEST_true(EVP_DigestVerifyInit(ctx, NULL, digest, NULL, pkey))
120 && TEST_int_eq(EVP_DigestVerify(ctx, protection->data,
122 prot_part_der, len), 1);
124 EVP_MD_CTX_free(ctx);
125 OPENSSL_free(prot_part_der);
129 /* Calls OSSL_CMP_calc_protection and compares and verifies signature */
130 static int execute_calc_protection_signature_test(CMP_PROTECT_TEST_FIXTURE *
133 ASN1_BIT_STRING *protection =
134 ossl_cmp_calc_protection(fixture->msg, NULL, fixture->privkey);
135 int ret = (TEST_ptr(protection)
136 && TEST_true(ASN1_STRING_cmp(protection,
137 fixture->msg->protection) == 0)
138 && TEST_true(verify_signature(fixture->msg, protection,
140 fixture->cmp_ctx->digest)));
142 ASN1_BIT_STRING_free(protection);
146 static int test_cmp_calc_protection_no_key_no_secret(void)
148 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
149 if (!TEST_ptr(fixture->msg = load_pkimsg(ir_unprotected_f))
150 || !TEST_ptr(fixture->msg->header->protectionAlg =
151 X509_ALGOR_new() /* no specific alg needed here */)) {
156 EXECUTE_TEST(execute_calc_protection_fails_test, tear_down);
160 static int test_cmp_calc_protection_pkey(void)
162 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
163 fixture->pubkey = loadedpubkey;
164 fixture->privkey = loadedprivkey;
165 if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_f))) {
169 EXECUTE_TEST(execute_calc_protection_signature_test, tear_down);
173 static int test_cmp_calc_protection_pbmac(void)
175 unsigned char sec_insta[] = { 'i', 'n', 's', 't', 'a' };
177 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
178 if (!TEST_ptr(fixture->secret = ASN1_OCTET_STRING_new())
179 || !TEST_true(ASN1_OCTET_STRING_set
180 (fixture->secret, sec_insta, sizeof(sec_insta)))
181 || !TEST_ptr(fixture->msg = load_pkimsg(ip_PBM_f))) {
185 EXECUTE_TEST(execute_calc_protection_pbmac_test, tear_down);
188 static int execute_MSG_protect_test(CMP_PROTECT_TEST_FIXTURE *fixture)
190 return TEST_int_eq(fixture->expected,
191 ossl_cmp_msg_protect(fixture->cmp_ctx, fixture->msg));
194 #define SET_OPT_UNPROTECTED_SEND(ctx, val) \
195 OSSL_CMP_CTX_set_option((ctx), OSSL_CMP_OPT_UNPROTECTED_SEND, (val))
196 static int test_MSG_protect_unprotected_request(void)
198 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
200 fixture->expected = 1;
201 if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected))
202 || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 1))) {
206 EXECUTE_TEST(execute_MSG_protect_test, tear_down);
210 static int test_MSG_protect_with_msg_sig_alg_protection_plus_rsa_key(void)
212 const size_t size = sizeof(rand_data) / 2;
214 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
215 fixture->expected = 1;
217 if (!TEST_ptr(fixture->msg =
218 OSSL_CMP_MSG_dup(ir_unprotected))
219 || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0))
221 * Use half of the 16 bytes of random input
222 * for each reference and secret value
224 || !TEST_true(OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx,
226 || !TEST_true(OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx,
232 EXECUTE_TEST(execute_MSG_protect_test, tear_down);
236 static int test_MSG_protect_with_certificate_and_key(void)
238 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
239 fixture->expected = 1;
241 if (!TEST_ptr(fixture->msg =
242 OSSL_CMP_MSG_dup(ir_unprotected))
243 || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0))
244 || !TEST_true(OSSL_CMP_CTX_set1_pkey(fixture->cmp_ctx, loadedkey))
245 || !TEST_true(OSSL_CMP_CTX_set1_cert(fixture->cmp_ctx, cert))) {
249 EXECUTE_TEST(execute_MSG_protect_test, tear_down);
253 static int test_MSG_protect_certificate_based_without_cert(void)
257 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
258 ctx = fixture->cmp_ctx;
259 fixture->expected = 0;
260 if (!TEST_ptr(fixture->msg =
261 OSSL_CMP_MSG_dup(ir_unprotected))
262 || !TEST_true(SET_OPT_UNPROTECTED_SEND(ctx, 0))
263 || !TEST_true(OSSL_CMP_CTX_set0_newPkey(ctx, 1, loadedkey))) {
267 EVP_PKEY_up_ref(loadedkey);
268 EXECUTE_TEST(execute_MSG_protect_test, tear_down);
272 static int test_MSG_protect_no_key_no_secret(void)
274 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
275 fixture->expected = 0;
276 if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected))
277 || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0))) {
281 EXECUTE_TEST(execute_MSG_protect_test, tear_down);
285 static int test_MSG_protect_pbmac_no_sender(int with_ref)
287 static unsigned char secret[] = { 47, 11, 8, 15 };
288 static unsigned char ref[] = { 0xca, 0xfe, 0xba, 0xbe };
290 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
291 fixture->expected = with_ref;
292 if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected))
293 || !SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0)
294 || !ossl_cmp_hdr_set1_sender(fixture->msg->header, NULL)
295 || !OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx,
296 secret, sizeof(secret))
297 || (!OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx,
298 with_ref ? ref : NULL,
303 EXECUTE_TEST(execute_MSG_protect_test, tear_down);
307 static int test_MSG_protect_pbmac_no_sender_with_ref(void)
309 return test_MSG_protect_pbmac_no_sender(1);
312 static int test_MSG_protect_pbmac_no_sender_no_ref(void)
314 return test_MSG_protect_pbmac_no_sender(0);
317 static int execute_MSG_add_extraCerts_test(CMP_PROTECT_TEST_FIXTURE *fixture)
319 return TEST_true(ossl_cmp_msg_add_extraCerts(fixture->cmp_ctx,
323 static int test_MSG_add_extraCerts(void)
325 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
326 if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_protected))) {
330 EXECUTE_TEST(execute_MSG_add_extraCerts_test, tear_down);
334 #ifndef OPENSSL_NO_EC
335 /* The cert chain tests use EC certs so we skip them in no-ec builds */
336 static int execute_cmp_build_cert_chain_test(CMP_PROTECT_TEST_FIXTURE *fixture)
339 OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
340 STACK_OF(X509) *chain =
341 ossl_cmp_build_cert_chain(ctx->libctx, ctx->propq,
342 fixture->certs, fixture->cert);
344 if (TEST_ptr(chain)) {
345 /* Check whether chain built is equal to the expected one */
346 ret = TEST_int_eq(0, STACK_OF_X509_cmp(chain, fixture->chain));
347 sk_X509_pop_free(chain, X509_free);
352 static int test_cmp_build_cert_chain(void)
354 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
355 fixture->cert = endentity2;
356 if (!TEST_ptr(fixture->certs = sk_X509_new_null())
357 || !TEST_ptr(fixture->chain = sk_X509_new_null())
358 || !TEST_true(sk_X509_push(fixture->certs, endentity1))
359 || !TEST_true(sk_X509_push(fixture->certs, root))
360 || !TEST_true(sk_X509_push(fixture->certs, intermediate))
361 || !TEST_true(sk_X509_push(fixture->chain, endentity2))
362 || !TEST_true(sk_X509_push(fixture->chain, intermediate))) {
366 EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down);
370 static int test_cmp_build_cert_chain_missing_intermediate(void)
372 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
373 fixture->cert = endentity2;
374 if (!TEST_ptr(fixture->certs = sk_X509_new_null())
375 || !TEST_ptr(fixture->chain = sk_X509_new_null())
376 || !TEST_true(sk_X509_push(fixture->certs, endentity1))
377 || !TEST_true(sk_X509_push(fixture->certs, root))
378 || !TEST_true(sk_X509_push(fixture->chain, endentity2))) {
382 EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down);
386 static int test_cmp_build_cert_chain_missing_root(void)
388 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
389 fixture->cert = endentity2;
390 if (!TEST_ptr(fixture->certs = sk_X509_new_null())
391 || !TEST_ptr(fixture->chain = sk_X509_new_null())
392 || !TEST_true(sk_X509_push(fixture->certs, endentity1))
393 || !TEST_true(sk_X509_push(fixture->certs, intermediate))
394 || !TEST_true(sk_X509_push(fixture->chain, endentity2))
395 || !TEST_true(sk_X509_push(fixture->chain, intermediate))) {
399 EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down);
403 static int test_cmp_build_cert_chain_no_certs(void)
405 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
406 fixture->cert = endentity2;
407 if (!TEST_ptr(fixture->certs = sk_X509_new_null())
408 || !TEST_ptr(fixture->chain = sk_X509_new_null())
409 || !TEST_true(sk_X509_push(fixture->chain, endentity2))) {
413 EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down);
416 #endif /* OPENSSL_NO_EC */
418 static int execute_X509_STORE_test(CMP_PROTECT_TEST_FIXTURE *fixture)
420 X509_STORE *store = X509_STORE_new();
421 STACK_OF(X509) *sk = NULL;
424 if (!TEST_true(ossl_cmp_X509_STORE_add1_certs(store,
426 fixture->callback_arg)))
428 sk = X509_STORE_get1_all_certs(store);
429 if (!TEST_int_eq(0, STACK_OF_X509_cmp(sk, fixture->chain)))
433 X509_STORE_free(store);
434 sk_X509_pop_free(sk, X509_free);
439 static int test_X509_STORE(void)
441 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
442 fixture->callback_arg = 0; /* self-issued allowed */
443 if (!TEST_ptr(fixture->certs = sk_X509_new_null())
444 || !sk_X509_push(fixture->certs, endentity1)
445 || !sk_X509_push(fixture->certs, endentity2)
446 || !sk_X509_push(fixture->certs, root)
447 || !sk_X509_push(fixture->certs, intermediate)
448 || !TEST_ptr(fixture->chain = sk_X509_dup(fixture->certs))) {
452 EXECUTE_TEST(execute_X509_STORE_test, tear_down);
456 static int test_X509_STORE_only_self_issued(void)
458 SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
459 fixture->certs = sk_X509_new_null();
460 fixture->chain = sk_X509_new_null();
461 fixture->callback_arg = 1; /* only self-issued */
462 if (!TEST_true(sk_X509_push(fixture->certs, endentity1))
463 || !TEST_true(sk_X509_push(fixture->certs, endentity2))
464 || !TEST_true(sk_X509_push(fixture->certs, root))
465 || !TEST_true(sk_X509_push(fixture->certs, intermediate))
466 || !TEST_true(sk_X509_push(fixture->chain, root))) {
470 EXECUTE_TEST(execute_X509_STORE_test, tear_down);
475 void cleanup_tests(void)
477 EVP_PKEY_free(loadedprivkey);
478 EVP_PKEY_free(loadedpubkey);
479 EVP_PKEY_free(loadedkey);
481 X509_free(endentity1);
482 X509_free(endentity2);
484 X509_free(intermediate);
485 OSSL_CMP_MSG_free(ir_protected);
486 OSSL_CMP_MSG_free(ir_unprotected);
490 int setup_tests(void)
498 char *intermediate_f;
500 if (!test_skip_common_options()) {
501 TEST_error("Error parsing test options\n");
505 RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH);
506 if (!TEST_ptr(server_f = test_get_argument(0))
507 || !TEST_ptr(ir_protected_f = test_get_argument(1))
508 || !TEST_ptr(ir_unprotected_f = test_get_argument(2))
509 || !TEST_ptr(ip_PBM_f = test_get_argument(3))
510 || !TEST_ptr(server_cert_f = test_get_argument(4))
511 || !TEST_ptr(server_key_f = test_get_argument(5))
512 || !TEST_ptr(endentity1_f = test_get_argument(6))
513 || !TEST_ptr(endentity2_f = test_get_argument(7))
514 || !TEST_ptr(root_f = test_get_argument(8))
515 || !TEST_ptr(intermediate_f = test_get_argument(9))) {
516 TEST_error("usage: cmp_protect_test server.pem "
517 "IR_protected.der IR_unprotected.der IP_PBM.der "
518 "server.crt server.pem"
519 "EndEntity1.crt EndEntity2.crt "
520 "Root_CA.crt Intermediate_CA.crt\n");
523 if (!TEST_ptr(loadedkey = load_pem_key(server_key_f))
524 || !TEST_ptr(cert = load_pem_cert(server_cert_f)))
527 if (!TEST_ptr(loadedprivkey = load_pem_key(server_f)))
529 if (TEST_true(EVP_PKEY_up_ref(loadedprivkey)))
530 loadedpubkey = loadedprivkey;
531 if (!TEST_ptr(ir_protected = load_pkimsg(ir_protected_f))
532 || !TEST_ptr(ir_unprotected = load_pkimsg(ir_unprotected_f)))
534 if (!TEST_ptr(endentity1 = load_pem_cert(endentity1_f))
535 || !TEST_ptr(endentity2 = load_pem_cert(endentity2_f))
536 || !TEST_ptr(root = load_pem_cert(root_f))
537 || !TEST_ptr(intermediate = load_pem_cert(intermediate_f)))
539 if (!TEST_int_eq(1, RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH)))
542 /* Message protection tests */
543 ADD_TEST(test_cmp_calc_protection_no_key_no_secret);
544 ADD_TEST(test_cmp_calc_protection_pkey);
545 ADD_TEST(test_cmp_calc_protection_pbmac);
547 ADD_TEST(test_MSG_protect_with_msg_sig_alg_protection_plus_rsa_key);
548 ADD_TEST(test_MSG_protect_with_certificate_and_key);
549 ADD_TEST(test_MSG_protect_certificate_based_without_cert);
550 ADD_TEST(test_MSG_protect_unprotected_request);
551 ADD_TEST(test_MSG_protect_no_key_no_secret);
552 ADD_TEST(test_MSG_protect_pbmac_no_sender_with_ref);
553 ADD_TEST(test_MSG_protect_pbmac_no_sender_no_ref);
554 ADD_TEST(test_MSG_add_extraCerts);
556 #ifndef OPENSSL_NO_EC
557 ADD_TEST(test_cmp_build_cert_chain);
558 ADD_TEST(test_cmp_build_cert_chain_missing_root);
559 ADD_TEST(test_cmp_build_cert_chain_missing_intermediate);
560 ADD_TEST(test_cmp_build_cert_chain_no_certs);
563 ADD_TEST(test_X509_STORE);
564 ADD_TEST(test_X509_STORE_only_self_issued);