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