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