xref: /openssl/test/cmp_server_test.c (revision bedffe17)
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 
tear_down(CMP_SRV_TEST_FIXTURE * fixture)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 
set_up(const char * const test_case_name)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 
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 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 
execute_test_handle_request(CMP_SRV_TEST_FIXTURE * fixture)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_true(OSSL_CMP_SRV_CTX_init_trans(ctx, NULL, NULL))
80         || !TEST_ptr(custom_ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(ctx))
81         || !TEST_int_eq(strcmp(custom_ctx, dummy_custom_ctx), 0))
82         goto end;
83 
84     if (!TEST_true(OSSL_CMP_SRV_CTX_set_send_unprotected_errors(ctx, 0))
85             || !TEST_true(OSSL_CMP_SRV_CTX_set_accept_unprotected(ctx, 0))
86             || !TEST_true(OSSL_CMP_SRV_CTX_set_accept_raverified(ctx, 1))
87             || !TEST_true(OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(ctx, 1)))
88         goto end;
89 
90     if (!TEST_ptr(cmp_ctx = OSSL_CMP_SRV_CTX_get0_cmp_ctx(ctx))
91             || !OSSL_CMP_CTX_set1_referenceValue(cmp_ctx,
92                                                  (unsigned char *)"server", 6)
93             || !OSSL_CMP_CTX_set1_secretValue(cmp_ctx,
94                                               (unsigned char *)"1234", 4))
95         goto end;
96 
97     if (!TEST_ptr(rsp = OSSL_CMP_CTX_server_perform(client_ctx, fixture->req))
98             || !TEST_int_eq(OSSL_CMP_MSG_get_bodytype(rsp),
99                             OSSL_CMP_PKIBODY_ERROR)
100             || !TEST_ptr(errorContent = rsp->body->value.error)
101             || !TEST_int_eq(ASN1_INTEGER_get(errorContent->errorCode),
102                             ERR_PACK(ERR_LIB_CMP, 0, dummy_errorCode)))
103         goto end;
104 
105     res = 1;
106 
107  end:
108     OSSL_CMP_MSG_free(rsp);
109     OSSL_CMP_CTX_free(client_ctx);
110     return res;
111 }
112 
test_handle_request(void)113 static int test_handle_request(void)
114 {
115     SETUP_TEST_FIXTURE(CMP_SRV_TEST_FIXTURE, set_up);
116     fixture->req = request;
117     fixture->expected = 1;
118     EXECUTE_TEST(execute_test_handle_request, tear_down);
119     return result;
120 }
121 
cleanup_tests(void)122 void cleanup_tests(void)
123 {
124     OSSL_CMP_MSG_free(request);
125     OSSL_PROVIDER_unload(default_null_provider);
126     OSSL_PROVIDER_unload(provider);
127     OSSL_LIB_CTX_free(libctx);
128     return;
129 }
130 
131 #define USAGE \
132     "CR_protected_PBM_1234.der module_name [module_conf_file]\n"
OPT_TEST_DECLARE_USAGE(USAGE)133 OPT_TEST_DECLARE_USAGE(USAGE)
134 
135 int setup_tests(void)
136 {
137     const char *request_f;
138 
139     if (!test_skip_common_options()) {
140         TEST_error("Error parsing test options\n");
141         return 0;
142     }
143 
144     if (!TEST_ptr(request_f = test_get_argument(0))) {
145         TEST_error("usage: cmp_server_test %s", USAGE);
146         return 0;
147     }
148 
149     if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 1, USAGE))
150         return 0;
151 
152     if (!TEST_ptr(request = load_pkimsg(request_f, libctx))) {
153         cleanup_tests();
154         return 0;
155     }
156 
157     /*
158      * this (indirectly) calls
159      * OSSL_CMP_SRV_CTX_new(),
160      * OSSL_CMP_SRV_CTX_free(),
161      * OSSL_CMP_CTX_server_perform(),
162      * OSSL_CMP_SRV_process_request(),
163      * OSSL_CMP_SRV_CTX_init(),
164      * OSSL_CMP_SRV_CTX_get0_cmp_ctx(),
165      * OSSL_CMP_SRV_CTX_get0_custom_ctx(),
166      * OSSL_CMP_SRV_CTX_set_send_unprotected_errors(),
167      * OSSL_CMP_SRV_CTX_set_accept_unprotected(),
168      * OSSL_CMP_SRV_CTX_set_accept_raverified(), and
169      * OSSL_CMP_SRV_CTX_set_grant_implicit_confirm()
170      */
171     ADD_TEST(test_handle_request);
172     return 1;
173 }
174