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