Make EVP_PKEY_CTX_[get|set]_group_name work for DH too
[openssl.git] / test / cmp_server_test.c
1 /*
2  * Copyright 2007-2020 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 "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_CMP_MSG *request = NULL;
22
23 static void tear_down(CMP_SRV_TEST_FIXTURE *fixture)
24 {
25     OSSL_CMP_SRV_CTX_free(fixture->srv_ctx);
26     OPENSSL_free(fixture);
27 }
28
29 static CMP_SRV_TEST_FIXTURE *set_up(const char *const test_case_name)
30 {
31     CMP_SRV_TEST_FIXTURE *fixture;
32
33     if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
34         return NULL;
35     fixture->test_case_name = test_case_name;
36     if (!TEST_ptr(fixture->srv_ctx = OSSL_CMP_SRV_CTX_new()))
37         goto err;
38     return fixture;
39
40  err:
41     tear_down(fixture);
42     return NULL;
43 }
44
45 static int dummy_errorCode = CMP_R_MULTIPLE_SAN_SOURCES; /* any reason code */
46
47 static OSSL_CMP_PKISI *process_cert_request(OSSL_CMP_SRV_CTX *srv_ctx,
48                                             const OSSL_CMP_MSG *cert_req,
49                                             int certReqId,
50                                             const OSSL_CRMF_MSG *crm,
51                                             const X509_REQ *p10cr,
52                                             X509 **certOut,
53                                             STACK_OF(X509) **chainOut,
54                                             STACK_OF(X509) **caPubs)
55 {
56     CMPerr(0, dummy_errorCode);
57     return NULL;
58 }
59
60 static int execute_test_handle_request(CMP_SRV_TEST_FIXTURE *fixture)
61 {
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;
68     int res = 0;
69
70     if (!TEST_ptr(client_ctx = OSSL_CMP_CTX_new())
71             || !TEST_true(OSSL_CMP_CTX_set_transfer_cb_arg(client_ctx, ctx)))
72         goto end;
73
74     if (!TEST_true(OSSL_CMP_SRV_CTX_init(ctx, dummy_custom_ctx,
75                                          process_cert_request, NULL, NULL,
76                                          NULL, 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))
79         goto end;
80
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)))
85         goto end;
86
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))
92         goto end;
93
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),
99                             dummy_errorCode))
100         goto end;
101
102     res = 1;
103
104  end:
105     OSSL_CMP_MSG_free(rsp);
106     OSSL_CMP_CTX_free(client_ctx);
107     return res;
108 }
109
110 static int test_handle_request(void)
111 {
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);
116     return result;
117 }
118
119 void cleanup_tests(void)
120 {
121     OSSL_CMP_MSG_free(request);
122     return;
123 }
124
125 int setup_tests(void)
126 {
127     const char *request_f;
128
129     if (!test_skip_common_options()) {
130         TEST_error("Error parsing test options\n");
131         return 0;
132     }
133
134     if (!TEST_ptr(request_f = test_get_argument(0))) {
135         TEST_error("usage: cmp_server_test CR_protected_PBM_1234.der\n");
136         return 0;
137     }
138
139     if (!TEST_ptr(request = load_pkimsg(request_f))) {
140         cleanup_tests();
141         return 0;
142     }
143
144     /*
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()
157      */
158     ADD_TEST(test_handle_request);
159     return 1;
160 }