| 
									
										
										
										
											2020-03-10 17:29:46 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-06-17 20:24:59 +08:00
										 |  |  |  * Copyright 2007-2021 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2020-03-10 17:29:46 +08:00
										 |  |  |  * Copyright Nokia 2007-2020 | 
					
						
							|  |  |  |  * Copyright Siemens AG 2015-2020 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
					
						
							|  |  |  |  * this file except in compliance with the License.  You can obtain a copy | 
					
						
							|  |  |  |  * in the file LICENSE in the source distribution or at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-29 19:40:10 +08:00
										 |  |  | #include "helpers/cmp_testlib.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-10 17:29:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | typedef struct test_fixture { | 
					
						
							|  |  |  |     const char *test_case_name; | 
					
						
							|  |  |  |     int expected; | 
					
						
							|  |  |  |     OSSL_CMP_SRV_CTX *srv_ctx; | 
					
						
							|  |  |  |     OSSL_CMP_MSG *req; | 
					
						
							|  |  |  | } CMP_SRV_TEST_FIXTURE; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-15 17:55:50 +08:00
										 |  |  | static OSSL_LIB_CTX *libctx = NULL; | 
					
						
							| 
									
										
										
										
											2020-05-14 10:32:44 +08:00
										 |  |  | static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL; | 
					
						
							| 
									
										
										
										
											2020-03-10 17:29:46 +08:00
										 |  |  | static OSSL_CMP_MSG *request = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void tear_down(CMP_SRV_TEST_FIXTURE *fixture) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     OSSL_CMP_SRV_CTX_free(fixture->srv_ctx); | 
					
						
							|  |  |  |     OPENSSL_free(fixture); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static CMP_SRV_TEST_FIXTURE *set_up(const char *const test_case_name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     CMP_SRV_TEST_FIXTURE *fixture; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture)))) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     fixture->test_case_name = test_case_name; | 
					
						
							| 
									
										
										
										
											2020-05-14 10:32:44 +08:00
										 |  |  |     if (!TEST_ptr(fixture->srv_ctx = OSSL_CMP_SRV_CTX_new(libctx, NULL))) | 
					
						
							| 
									
										
										
										
											2020-03-10 17:29:46 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     return fixture; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  err: | 
					
						
							|  |  |  |     tear_down(fixture); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int dummy_errorCode = CMP_R_MULTIPLE_SAN_SOURCES; /* any reason code */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static OSSL_CMP_PKISI *process_cert_request(OSSL_CMP_SRV_CTX *srv_ctx, | 
					
						
							|  |  |  |                                             const OSSL_CMP_MSG *cert_req, | 
					
						
							|  |  |  |                                             int certReqId, | 
					
						
							|  |  |  |                                             const OSSL_CRMF_MSG *crm, | 
					
						
							|  |  |  |                                             const X509_REQ *p10cr, | 
					
						
							|  |  |  |                                             X509 **certOut, | 
					
						
							|  |  |  |                                             STACK_OF(X509) **chainOut, | 
					
						
							|  |  |  |                                             STACK_OF(X509) **caPubs) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |     ERR_raise(ERR_LIB_CMP, dummy_errorCode); | 
					
						
							| 
									
										
										
										
											2020-03-10 17:29:46 +08:00
										 |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int execute_test_handle_request(CMP_SRV_TEST_FIXTURE *fixture) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     OSSL_CMP_SRV_CTX *ctx = fixture->srv_ctx; | 
					
						
							|  |  |  |     OSSL_CMP_CTX *client_ctx; | 
					
						
							|  |  |  |     OSSL_CMP_CTX *cmp_ctx; | 
					
						
							|  |  |  |     char *dummy_custom_ctx = "@test_dummy", *custom_ctx; | 
					
						
							|  |  |  |     OSSL_CMP_MSG *rsp = NULL; | 
					
						
							|  |  |  |     OSSL_CMP_ERRORMSGCONTENT *errorContent; | 
					
						
							|  |  |  |     int res = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 10:32:44 +08:00
										 |  |  |     if (!TEST_ptr(client_ctx = OSSL_CMP_CTX_new(libctx, NULL)) | 
					
						
							| 
									
										
										
										
											2020-03-10 17:29:46 +08:00
										 |  |  |             || !TEST_true(OSSL_CMP_CTX_set_transfer_cb_arg(client_ctx, ctx))) | 
					
						
							|  |  |  |         goto end; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!TEST_true(OSSL_CMP_SRV_CTX_init(ctx, dummy_custom_ctx, | 
					
						
							|  |  |  |                                          process_cert_request, NULL, NULL, | 
					
						
							|  |  |  |                                          NULL, NULL, NULL)) | 
					
						
							| 
									
										
										
										
											2023-04-28 19:45:21 +08:00
										 |  |  |         || !TEST_true(OSSL_CMP_SRV_CTX_init_trans(ctx, NULL, NULL)) | 
					
						
							| 
									
										
										
										
											2020-03-10 17:29:46 +08:00
										 |  |  |         || !TEST_ptr(custom_ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(ctx)) | 
					
						
							|  |  |  |         || !TEST_int_eq(strcmp(custom_ctx, dummy_custom_ctx), 0)) | 
					
						
							|  |  |  |         goto end; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!TEST_true(OSSL_CMP_SRV_CTX_set_send_unprotected_errors(ctx, 0)) | 
					
						
							|  |  |  |             || !TEST_true(OSSL_CMP_SRV_CTX_set_accept_unprotected(ctx, 0)) | 
					
						
							|  |  |  |             || !TEST_true(OSSL_CMP_SRV_CTX_set_accept_raverified(ctx, 1)) | 
					
						
							|  |  |  |             || !TEST_true(OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(ctx, 1))) | 
					
						
							|  |  |  |         goto end; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!TEST_ptr(cmp_ctx = OSSL_CMP_SRV_CTX_get0_cmp_ctx(ctx)) | 
					
						
							|  |  |  |             || !OSSL_CMP_CTX_set1_referenceValue(cmp_ctx, | 
					
						
							|  |  |  |                                                  (unsigned char *)"server", 6) | 
					
						
							|  |  |  |             || !OSSL_CMP_CTX_set1_secretValue(cmp_ctx, | 
					
						
							|  |  |  |                                               (unsigned char *)"1234", 4)) | 
					
						
							|  |  |  |         goto end; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!TEST_ptr(rsp = OSSL_CMP_CTX_server_perform(client_ctx, fixture->req)) | 
					
						
							| 
									
										
										
										
											2021-06-16 16:26:26 +08:00
										 |  |  |             || !TEST_int_eq(OSSL_CMP_MSG_get_bodytype(rsp), | 
					
						
							| 
									
										
										
										
											2020-03-10 17:29:46 +08:00
										 |  |  |                             OSSL_CMP_PKIBODY_ERROR) | 
					
						
							|  |  |  |             || !TEST_ptr(errorContent = rsp->body->value.error) | 
					
						
							|  |  |  |             || !TEST_int_eq(ASN1_INTEGER_get(errorContent->errorCode), | 
					
						
							| 
									
										
										
										
											2021-06-23 20:47:57 +08:00
										 |  |  |                             ERR_PACK(ERR_LIB_CMP, 0, dummy_errorCode))) | 
					
						
							| 
									
										
										
										
											2020-03-10 17:29:46 +08:00
										 |  |  |         goto end; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  end: | 
					
						
							|  |  |  |     OSSL_CMP_MSG_free(rsp); | 
					
						
							|  |  |  |     OSSL_CMP_CTX_free(client_ctx); | 
					
						
							|  |  |  |     return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int test_handle_request(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     SETUP_TEST_FIXTURE(CMP_SRV_TEST_FIXTURE, set_up); | 
					
						
							|  |  |  |     fixture->req = request; | 
					
						
							|  |  |  |     fixture->expected = 1; | 
					
						
							|  |  |  |     EXECUTE_TEST(execute_test_handle_request, tear_down); | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void cleanup_tests(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     OSSL_CMP_MSG_free(request); | 
					
						
							| 
									
										
										
										
											2020-05-14 10:32:44 +08:00
										 |  |  |     OSSL_PROVIDER_unload(default_null_provider); | 
					
						
							|  |  |  |     OSSL_PROVIDER_unload(provider); | 
					
						
							| 
									
										
										
										
											2020-10-15 17:55:50 +08:00
										 |  |  |     OSSL_LIB_CTX_free(libctx); | 
					
						
							| 
									
										
										
										
											2020-03-10 17:29:46 +08:00
										 |  |  |     return; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 10:32:44 +08:00
										 |  |  | #define USAGE \
 | 
					
						
							|  |  |  |     "CR_protected_PBM_1234.der module_name [module_conf_file]\n" | 
					
						
							|  |  |  | OPT_TEST_DECLARE_USAGE(USAGE) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-10 17:29:46 +08:00
										 |  |  | int setup_tests(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     const char *request_f; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!test_skip_common_options()) { | 
					
						
							|  |  |  |         TEST_error("Error parsing test options\n"); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!TEST_ptr(request_f = test_get_argument(0))) { | 
					
						
							| 
									
										
										
										
											2020-05-14 10:32:44 +08:00
										 |  |  |         TEST_error("usage: cmp_server_test %s", USAGE); | 
					
						
							| 
									
										
										
										
											2020-03-10 17:29:46 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-26 21:21:48 +08:00
										 |  |  |     if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 1, USAGE)) | 
					
						
							| 
									
										
										
										
											2020-05-14 10:32:44 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-27 00:18:13 +08:00
										 |  |  |     if (!TEST_ptr(request = load_pkimsg(request_f, libctx))) { | 
					
						
							| 
									
										
										
										
											2020-03-10 17:29:46 +08:00
										 |  |  |         cleanup_tests(); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * this (indirectly) calls | 
					
						
							|  |  |  |      * OSSL_CMP_SRV_CTX_new(), | 
					
						
							|  |  |  |      * OSSL_CMP_SRV_CTX_free(), | 
					
						
							|  |  |  |      * OSSL_CMP_CTX_server_perform(), | 
					
						
							|  |  |  |      * OSSL_CMP_SRV_process_request(), | 
					
						
							|  |  |  |      * OSSL_CMP_SRV_CTX_init(), | 
					
						
							|  |  |  |      * OSSL_CMP_SRV_CTX_get0_cmp_ctx(), | 
					
						
							|  |  |  |      * OSSL_CMP_SRV_CTX_get0_custom_ctx(), | 
					
						
							|  |  |  |      * OSSL_CMP_SRV_CTX_set_send_unprotected_errors(), | 
					
						
							|  |  |  |      * OSSL_CMP_SRV_CTX_set_accept_unprotected(), | 
					
						
							|  |  |  |      * OSSL_CMP_SRV_CTX_set_accept_raverified(), and | 
					
						
							|  |  |  |      * OSSL_CMP_SRV_CTX_set_grant_implicit_confirm() | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     ADD_TEST(test_handle_request); | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } |