2 * Copyright 2007-2021 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright Nokia 2007-2020
4 * Copyright Siemens AG 2015-2020
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 "helpers/cmp_testlib.h"
14 typedef struct test_fixture
{
15 const char *test_case_name
;
17 OSSL_CMP_SRV_CTX
*srv_ctx
;
19 } CMP_SRV_TEST_FIXTURE
;
21 static OSSL_LIB_CTX
*libctx
= NULL
;
22 static OSSL_PROVIDER
*default_null_provider
= NULL
, *provider
= NULL
;
23 static OSSL_CMP_MSG
*request
= NULL
;
25 static void tear_down(CMP_SRV_TEST_FIXTURE
*fixture
)
27 OSSL_CMP_SRV_CTX_free(fixture
->srv_ctx
);
28 OPENSSL_free(fixture
);
31 static CMP_SRV_TEST_FIXTURE
*set_up(const char *const test_case_name
)
33 CMP_SRV_TEST_FIXTURE
*fixture
;
35 if (!TEST_ptr(fixture
= OPENSSL_zalloc(sizeof(*fixture
))))
37 fixture
->test_case_name
= test_case_name
;
38 if (!TEST_ptr(fixture
->srv_ctx
= OSSL_CMP_SRV_CTX_new(libctx
, NULL
)))
47 static int dummy_errorCode
= CMP_R_MULTIPLE_SAN_SOURCES
; /* any reason code */
49 static OSSL_CMP_PKISI
*process_cert_request(OSSL_CMP_SRV_CTX
*srv_ctx
,
50 const OSSL_CMP_MSG
*cert_req
,
52 const OSSL_CRMF_MSG
*crm
,
53 const X509_REQ
*p10cr
,
55 STACK_OF(X509
) **chainOut
,
56 STACK_OF(X509
) **caPubs
)
58 ERR_raise(ERR_LIB_CMP
, dummy_errorCode
);
62 static int execute_test_handle_request(CMP_SRV_TEST_FIXTURE
*fixture
)
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
;
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
)))
76 if (!TEST_true(OSSL_CMP_SRV_CTX_init(ctx
, dummy_custom_ctx
,
77 process_cert_request
, 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))
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)))
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))
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
)))
107 OSSL_CMP_MSG_free(rsp
);
108 OSSL_CMP_CTX_free(client_ctx
);
112 static int test_handle_request(void)
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
);
121 void cleanup_tests(void)
123 OSSL_CMP_MSG_free(request
);
124 OSSL_PROVIDER_unload(default_null_provider
);
125 OSSL_PROVIDER_unload(provider
);
126 OSSL_LIB_CTX_free(libctx
);
131 "CR_protected_PBM_1234.der module_name [module_conf_file]\n"
132 OPT_TEST_DECLARE_USAGE(USAGE
)
134 int setup_tests(void)
136 const char *request_f
;
138 if (!test_skip_common_options()) {
139 TEST_error("Error parsing test options\n");
143 if (!TEST_ptr(request_f
= test_get_argument(0))) {
144 TEST_error("usage: cmp_server_test %s", USAGE
);
148 if (!test_arg_libctx(&libctx
, &default_null_provider
, &provider
, 1, USAGE
))
151 if (!TEST_ptr(request
= load_pkimsg(request_f
, libctx
))) {
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()
170 ADD_TEST(test_handle_request
);