1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 2007-2021 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * Copyright Nokia 2007-2020 4e1051a39Sopenharmony_ci * Copyright Siemens AG 2015-2020 5e1051a39Sopenharmony_ci * 6e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 7e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 8e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 9e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 10e1051a39Sopenharmony_ci */ 11e1051a39Sopenharmony_ci 12e1051a39Sopenharmony_ci#include "helpers/cmp_testlib.h" 13e1051a39Sopenharmony_ci 14e1051a39Sopenharmony_citypedef struct test_fixture { 15e1051a39Sopenharmony_ci const char *test_case_name; 16e1051a39Sopenharmony_ci int expected; 17e1051a39Sopenharmony_ci OSSL_CMP_SRV_CTX *srv_ctx; 18e1051a39Sopenharmony_ci OSSL_CMP_MSG *req; 19e1051a39Sopenharmony_ci} CMP_SRV_TEST_FIXTURE; 20e1051a39Sopenharmony_ci 21e1051a39Sopenharmony_cistatic OSSL_LIB_CTX *libctx = NULL; 22e1051a39Sopenharmony_cistatic OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL; 23e1051a39Sopenharmony_cistatic OSSL_CMP_MSG *request = NULL; 24e1051a39Sopenharmony_ci 25e1051a39Sopenharmony_cistatic void tear_down(CMP_SRV_TEST_FIXTURE *fixture) 26e1051a39Sopenharmony_ci{ 27e1051a39Sopenharmony_ci OSSL_CMP_SRV_CTX_free(fixture->srv_ctx); 28e1051a39Sopenharmony_ci OPENSSL_free(fixture); 29e1051a39Sopenharmony_ci} 30e1051a39Sopenharmony_ci 31e1051a39Sopenharmony_cistatic CMP_SRV_TEST_FIXTURE *set_up(const char *const test_case_name) 32e1051a39Sopenharmony_ci{ 33e1051a39Sopenharmony_ci CMP_SRV_TEST_FIXTURE *fixture; 34e1051a39Sopenharmony_ci 35e1051a39Sopenharmony_ci if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture)))) 36e1051a39Sopenharmony_ci return NULL; 37e1051a39Sopenharmony_ci fixture->test_case_name = test_case_name; 38e1051a39Sopenharmony_ci if (!TEST_ptr(fixture->srv_ctx = OSSL_CMP_SRV_CTX_new(libctx, NULL))) 39e1051a39Sopenharmony_ci goto err; 40e1051a39Sopenharmony_ci return fixture; 41e1051a39Sopenharmony_ci 42e1051a39Sopenharmony_ci err: 43e1051a39Sopenharmony_ci tear_down(fixture); 44e1051a39Sopenharmony_ci return NULL; 45e1051a39Sopenharmony_ci} 46e1051a39Sopenharmony_ci 47e1051a39Sopenharmony_cistatic int dummy_errorCode = CMP_R_MULTIPLE_SAN_SOURCES; /* any reason code */ 48e1051a39Sopenharmony_ci 49e1051a39Sopenharmony_cistatic OSSL_CMP_PKISI *process_cert_request(OSSL_CMP_SRV_CTX *srv_ctx, 50e1051a39Sopenharmony_ci const OSSL_CMP_MSG *cert_req, 51e1051a39Sopenharmony_ci int certReqId, 52e1051a39Sopenharmony_ci const OSSL_CRMF_MSG *crm, 53e1051a39Sopenharmony_ci const X509_REQ *p10cr, 54e1051a39Sopenharmony_ci X509 **certOut, 55e1051a39Sopenharmony_ci STACK_OF(X509) **chainOut, 56e1051a39Sopenharmony_ci STACK_OF(X509) **caPubs) 57e1051a39Sopenharmony_ci{ 58e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_CMP, dummy_errorCode); 59e1051a39Sopenharmony_ci return NULL; 60e1051a39Sopenharmony_ci} 61e1051a39Sopenharmony_ci 62e1051a39Sopenharmony_cistatic int execute_test_handle_request(CMP_SRV_TEST_FIXTURE *fixture) 63e1051a39Sopenharmony_ci{ 64e1051a39Sopenharmony_ci OSSL_CMP_SRV_CTX *ctx = fixture->srv_ctx; 65e1051a39Sopenharmony_ci OSSL_CMP_CTX *client_ctx; 66e1051a39Sopenharmony_ci OSSL_CMP_CTX *cmp_ctx; 67e1051a39Sopenharmony_ci char *dummy_custom_ctx = "@test_dummy", *custom_ctx; 68e1051a39Sopenharmony_ci OSSL_CMP_MSG *rsp = NULL; 69e1051a39Sopenharmony_ci OSSL_CMP_ERRORMSGCONTENT *errorContent; 70e1051a39Sopenharmony_ci int res = 0; 71e1051a39Sopenharmony_ci 72e1051a39Sopenharmony_ci if (!TEST_ptr(client_ctx = OSSL_CMP_CTX_new(libctx, NULL)) 73e1051a39Sopenharmony_ci || !TEST_true(OSSL_CMP_CTX_set_transfer_cb_arg(client_ctx, ctx))) 74e1051a39Sopenharmony_ci goto end; 75e1051a39Sopenharmony_ci 76e1051a39Sopenharmony_ci if (!TEST_true(OSSL_CMP_SRV_CTX_init(ctx, dummy_custom_ctx, 77e1051a39Sopenharmony_ci process_cert_request, NULL, NULL, 78e1051a39Sopenharmony_ci NULL, NULL, NULL)) 79e1051a39Sopenharmony_ci || !TEST_ptr(custom_ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(ctx)) 80e1051a39Sopenharmony_ci || !TEST_int_eq(strcmp(custom_ctx, dummy_custom_ctx), 0)) 81e1051a39Sopenharmony_ci goto end; 82e1051a39Sopenharmony_ci 83e1051a39Sopenharmony_ci if (!TEST_true(OSSL_CMP_SRV_CTX_set_send_unprotected_errors(ctx, 0)) 84e1051a39Sopenharmony_ci || !TEST_true(OSSL_CMP_SRV_CTX_set_accept_unprotected(ctx, 0)) 85e1051a39Sopenharmony_ci || !TEST_true(OSSL_CMP_SRV_CTX_set_accept_raverified(ctx, 1)) 86e1051a39Sopenharmony_ci || !TEST_true(OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(ctx, 1))) 87e1051a39Sopenharmony_ci goto end; 88e1051a39Sopenharmony_ci 89e1051a39Sopenharmony_ci if (!TEST_ptr(cmp_ctx = OSSL_CMP_SRV_CTX_get0_cmp_ctx(ctx)) 90e1051a39Sopenharmony_ci || !OSSL_CMP_CTX_set1_referenceValue(cmp_ctx, 91e1051a39Sopenharmony_ci (unsigned char *)"server", 6) 92e1051a39Sopenharmony_ci || !OSSL_CMP_CTX_set1_secretValue(cmp_ctx, 93e1051a39Sopenharmony_ci (unsigned char *)"1234", 4)) 94e1051a39Sopenharmony_ci goto end; 95e1051a39Sopenharmony_ci 96e1051a39Sopenharmony_ci if (!TEST_ptr(rsp = OSSL_CMP_CTX_server_perform(client_ctx, fixture->req)) 97e1051a39Sopenharmony_ci || !TEST_int_eq(OSSL_CMP_MSG_get_bodytype(rsp), 98e1051a39Sopenharmony_ci OSSL_CMP_PKIBODY_ERROR) 99e1051a39Sopenharmony_ci || !TEST_ptr(errorContent = rsp->body->value.error) 100e1051a39Sopenharmony_ci || !TEST_int_eq(ASN1_INTEGER_get(errorContent->errorCode), 101e1051a39Sopenharmony_ci ERR_PACK(ERR_LIB_CMP, 0, dummy_errorCode))) 102e1051a39Sopenharmony_ci goto end; 103e1051a39Sopenharmony_ci 104e1051a39Sopenharmony_ci res = 1; 105e1051a39Sopenharmony_ci 106e1051a39Sopenharmony_ci end: 107e1051a39Sopenharmony_ci OSSL_CMP_MSG_free(rsp); 108e1051a39Sopenharmony_ci OSSL_CMP_CTX_free(client_ctx); 109e1051a39Sopenharmony_ci return res; 110e1051a39Sopenharmony_ci} 111e1051a39Sopenharmony_ci 112e1051a39Sopenharmony_cistatic int test_handle_request(void) 113e1051a39Sopenharmony_ci{ 114e1051a39Sopenharmony_ci SETUP_TEST_FIXTURE(CMP_SRV_TEST_FIXTURE, set_up); 115e1051a39Sopenharmony_ci fixture->req = request; 116e1051a39Sopenharmony_ci fixture->expected = 1; 117e1051a39Sopenharmony_ci EXECUTE_TEST(execute_test_handle_request, tear_down); 118e1051a39Sopenharmony_ci return result; 119e1051a39Sopenharmony_ci} 120e1051a39Sopenharmony_ci 121e1051a39Sopenharmony_civoid cleanup_tests(void) 122e1051a39Sopenharmony_ci{ 123e1051a39Sopenharmony_ci OSSL_CMP_MSG_free(request); 124e1051a39Sopenharmony_ci OSSL_PROVIDER_unload(default_null_provider); 125e1051a39Sopenharmony_ci OSSL_PROVIDER_unload(provider); 126e1051a39Sopenharmony_ci OSSL_LIB_CTX_free(libctx); 127e1051a39Sopenharmony_ci return; 128e1051a39Sopenharmony_ci} 129e1051a39Sopenharmony_ci 130e1051a39Sopenharmony_ci#define USAGE \ 131e1051a39Sopenharmony_ci "CR_protected_PBM_1234.der module_name [module_conf_file]\n" 132e1051a39Sopenharmony_ciOPT_TEST_DECLARE_USAGE(USAGE) 133e1051a39Sopenharmony_ci 134e1051a39Sopenharmony_ciint setup_tests(void) 135e1051a39Sopenharmony_ci{ 136e1051a39Sopenharmony_ci const char *request_f; 137e1051a39Sopenharmony_ci 138e1051a39Sopenharmony_ci if (!test_skip_common_options()) { 139e1051a39Sopenharmony_ci TEST_error("Error parsing test options\n"); 140e1051a39Sopenharmony_ci return 0; 141e1051a39Sopenharmony_ci } 142e1051a39Sopenharmony_ci 143e1051a39Sopenharmony_ci if (!TEST_ptr(request_f = test_get_argument(0))) { 144e1051a39Sopenharmony_ci TEST_error("usage: cmp_server_test %s", USAGE); 145e1051a39Sopenharmony_ci return 0; 146e1051a39Sopenharmony_ci } 147e1051a39Sopenharmony_ci 148e1051a39Sopenharmony_ci if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 1, USAGE)) 149e1051a39Sopenharmony_ci return 0; 150e1051a39Sopenharmony_ci 151e1051a39Sopenharmony_ci if (!TEST_ptr(request = load_pkimsg(request_f, libctx))) { 152e1051a39Sopenharmony_ci cleanup_tests(); 153e1051a39Sopenharmony_ci return 0; 154e1051a39Sopenharmony_ci } 155e1051a39Sopenharmony_ci 156e1051a39Sopenharmony_ci /* 157e1051a39Sopenharmony_ci * this (indirectly) calls 158e1051a39Sopenharmony_ci * OSSL_CMP_SRV_CTX_new(), 159e1051a39Sopenharmony_ci * OSSL_CMP_SRV_CTX_free(), 160e1051a39Sopenharmony_ci * OSSL_CMP_CTX_server_perform(), 161e1051a39Sopenharmony_ci * OSSL_CMP_SRV_process_request(), 162e1051a39Sopenharmony_ci * OSSL_CMP_SRV_CTX_init(), 163e1051a39Sopenharmony_ci * OSSL_CMP_SRV_CTX_get0_cmp_ctx(), 164e1051a39Sopenharmony_ci * OSSL_CMP_SRV_CTX_get0_custom_ctx(), 165e1051a39Sopenharmony_ci * OSSL_CMP_SRV_CTX_set_send_unprotected_errors(), 166e1051a39Sopenharmony_ci * OSSL_CMP_SRV_CTX_set_accept_unprotected(), 167e1051a39Sopenharmony_ci * OSSL_CMP_SRV_CTX_set_accept_raverified(), and 168e1051a39Sopenharmony_ci * OSSL_CMP_SRV_CTX_set_grant_implicit_confirm() 169e1051a39Sopenharmony_ci */ 170e1051a39Sopenharmony_ci ADD_TEST(test_handle_request); 171e1051a39Sopenharmony_ci return 1; 172e1051a39Sopenharmony_ci} 173