2 * Copyright 2007-2020 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 "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_CMP_MSG
*request
= NULL
;
23 static void tear_down(CMP_SRV_TEST_FIXTURE
*fixture
)
25 OSSL_CMP_SRV_CTX_free(fixture
->srv_ctx
);
26 OPENSSL_free(fixture
);
29 static CMP_SRV_TEST_FIXTURE
*set_up(const char *const test_case_name
)
31 CMP_SRV_TEST_FIXTURE
*fixture
;
33 if (!TEST_ptr(fixture
= OPENSSL_zalloc(sizeof(*fixture
))))
35 fixture
->test_case_name
= test_case_name
;
36 if (!TEST_ptr(fixture
->srv_ctx
= OSSL_CMP_SRV_CTX_new()))
45 static int dummy_errorCode
= CMP_R_MULTIPLE_SAN_SOURCES
; /* any reason code */
47 static OSSL_CMP_PKISI
*process_cert_request(OSSL_CMP_SRV_CTX
*srv_ctx
,
48 const OSSL_CMP_MSG
*cert_req
,
50 const OSSL_CRMF_MSG
*crm
,
51 const X509_REQ
*p10cr
,
53 STACK_OF(X509
) **chainOut
,
54 STACK_OF(X509
) **caPubs
)
56 CMPerr(0, dummy_errorCode
);
60 static int execute_test_handle_request(CMP_SRV_TEST_FIXTURE
*fixture
)
62 OSSL_CMP_SRV_CTX
*ctx
= fixture
->srv_ctx
;
63 OSSL_CMP_CTX
*client_ctx
;
64 OSSL_CMP_CTX
*cmp_ctx
;
65 char *dummy_custom_ctx
= "@test_dummy", *custom_ctx
;
66 OSSL_CMP_MSG
*rsp
= NULL
;
67 OSSL_CMP_ERRORMSGCONTENT
*errorContent
;
70 if (!TEST_ptr(client_ctx
= OSSL_CMP_CTX_new())
71 || !TEST_true(OSSL_CMP_CTX_set_transfer_cb_arg(client_ctx
, ctx
)))
74 if (!TEST_true(OSSL_CMP_SRV_CTX_init(ctx
, dummy_custom_ctx
,
75 process_cert_request
, NULL
, NULL
,
77 || !TEST_ptr(custom_ctx
= OSSL_CMP_SRV_CTX_get0_custom_ctx(ctx
))
78 || !TEST_int_eq(strcmp(custom_ctx
, dummy_custom_ctx
), 0))
81 if (!TEST_true(OSSL_CMP_SRV_CTX_set_send_unprotected_errors(ctx
, 0))
82 || !TEST_true(OSSL_CMP_SRV_CTX_set_accept_unprotected(ctx
, 0))
83 || !TEST_true(OSSL_CMP_SRV_CTX_set_accept_raverified(ctx
, 1))
84 || !TEST_true(OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(ctx
, 1)))
87 if (!TEST_ptr(cmp_ctx
= OSSL_CMP_SRV_CTX_get0_cmp_ctx(ctx
))
88 || !OSSL_CMP_CTX_set1_referenceValue(cmp_ctx
,
89 (unsigned char *)"server", 6)
90 || !OSSL_CMP_CTX_set1_secretValue(cmp_ctx
,
91 (unsigned char *)"1234", 4))
94 if (!TEST_ptr(rsp
= OSSL_CMP_CTX_server_perform(client_ctx
, fixture
->req
))
95 || !TEST_int_eq(ossl_cmp_msg_get_bodytype(rsp
),
96 OSSL_CMP_PKIBODY_ERROR
)
97 || !TEST_ptr(errorContent
= rsp
->body
->value
.error
)
98 || !TEST_int_eq(ASN1_INTEGER_get(errorContent
->errorCode
),
105 OSSL_CMP_MSG_free(rsp
);
106 OSSL_CMP_CTX_free(client_ctx
);
110 static int test_handle_request(void)
112 SETUP_TEST_FIXTURE(CMP_SRV_TEST_FIXTURE
, set_up
);
113 fixture
->req
= request
;
114 fixture
->expected
= 1;
115 EXECUTE_TEST(execute_test_handle_request
, tear_down
);
119 void cleanup_tests(void)
121 OSSL_CMP_MSG_free(request
);
125 int setup_tests(void)
127 const char *request_f
;
129 if (!test_skip_common_options()) {
130 TEST_error("Error parsing test options\n");
134 if (!TEST_ptr(request_f
= test_get_argument(0))) {
135 TEST_error("usage: cmp_server_test CR_protected_PBM_1234.der\n");
139 if (!TEST_ptr(request
= load_pkimsg(request_f
))) {
145 * this (indirectly) calls
146 * OSSL_CMP_SRV_CTX_new(),
147 * OSSL_CMP_SRV_CTX_free(),
148 * OSSL_CMP_CTX_server_perform(),
149 * OSSL_CMP_SRV_process_request(),
150 * OSSL_CMP_SRV_CTX_init(),
151 * OSSL_CMP_SRV_CTX_get0_cmp_ctx(),
152 * OSSL_CMP_SRV_CTX_get0_custom_ctx(),
153 * OSSL_CMP_SRV_CTX_set_send_unprotected_errors(),
154 * OSSL_CMP_SRV_CTX_set_accept_unprotected(),
155 * OSSL_CMP_SRV_CTX_set_accept_raverified(), and
156 * OSSL_CMP_SRV_CTX_set_grant_implicit_confirm()
158 ADD_TEST(test_handle_request
);