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