176f6a19aea054d384cd2b8fcd67160b54bb5f29
[openssl.git] / test / ssl_test.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12
13 #include <openssl/conf.h>
14 #include <openssl/err.h>
15 #include <openssl/ssl.h>
16
17 #include "handshake_helper.h"
18 #include "ssl_test_ctx.h"
19 #include "testutil.h"
20
21 static CONF *conf = NULL;
22
23 /* Currently the section names are of the form test-<number>, e.g. test-15. */
24 #define MAX_TESTCASE_NAME_LENGTH 100
25
26 typedef struct ssl_test_ctx_test_fixture {
27     const char *test_case_name;
28     char test_app[MAX_TESTCASE_NAME_LENGTH];
29 } SSL_TEST_FIXTURE;
30
31 static SSL_TEST_FIXTURE set_up(const char *const test_case_name)
32 {
33     SSL_TEST_FIXTURE fixture;
34     fixture.test_case_name = test_case_name;
35     return fixture;
36 }
37
38 static const char *print_alert(int alert)
39 {
40     return alert ? SSL_alert_desc_string_long(alert) : "no alert";
41 }
42
43 static int check_result(HANDSHAKE_RESULT result, SSL_TEST_CTX *test_ctx)
44 {
45     if (result.result != test_ctx->expected_result) {
46         fprintf(stderr, "ExpectedResult mismatch: expected %s, got %s.\n",
47                 ssl_test_result_name(test_ctx->expected_result),
48                 ssl_test_result_name(result.result));
49         return 0;
50     }
51     return 1;
52 }
53
54 static int check_alerts(HANDSHAKE_RESULT result, SSL_TEST_CTX *test_ctx)
55 {
56     if (result.client_alert_sent != result.client_alert_received) {
57         fprintf(stderr, "Client sent alert %s but server received %s\n.",
58                 print_alert(result.client_alert_sent),
59                 print_alert(result.client_alert_received));
60         /*
61          * We can't bail here because the peer doesn't always get far enough
62          * to process a received alert. Specifically, in protocol version
63          * negotiation tests, we have the following scenario.
64          * Client supports TLS v1.2 only; Server supports TLS v1.1.
65          * Client proposes TLS v1.2; server responds with 1.1;
66          * Client now sends a protocol alert, using TLS v1.2 in the header.
67          * The server, however, rejects the alert because of version mismatch
68          * in the record layer; therefore, the server appears to never
69          * receive the alert.
70          */
71         /* return 0; */
72     }
73
74     if (result.server_alert_sent != result.server_alert_received) {
75         fprintf(stderr, "Server sent alert %s but client received %s\n.",
76                 print_alert(result.server_alert_sent),
77                 print_alert(result.server_alert_received));
78         /* return 0; */
79     }
80
81     /* Tolerate an alert if one wasn't explicitly specified in the test. */
82     if (test_ctx->client_alert
83         /*
84          * The info callback alert value is computed as
85          * (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]
86          * where the low byte is the alert code and the high byte is other stuff.
87          */
88         && (result.client_alert_sent & 0xff) != test_ctx->client_alert) {
89         fprintf(stderr, "ClientAlert mismatch: expected %s, got %s.\n",
90                 print_alert(test_ctx->client_alert),
91                 print_alert(result.client_alert_sent));
92         return 0;
93     }
94
95     if (test_ctx->server_alert
96         && (result.server_alert_sent & 0xff) != test_ctx->server_alert) {
97         fprintf(stderr, "ServerAlert mismatch: expected %s, got %s.\n",
98                 print_alert(test_ctx->server_alert),
99                 print_alert(result.server_alert_sent));
100         return 0;
101     }
102
103     return 1;
104 }
105
106 static int check_protocol(HANDSHAKE_RESULT result, SSL_TEST_CTX *test_ctx)
107 {
108     if (result.client_protocol != result.server_protocol) {
109         fprintf(stderr, "Client has protocol %s but server has %s\n.",
110                 ssl_protocol_name(result.client_protocol),
111                 ssl_protocol_name(result.server_protocol));
112         return 0;
113     }
114
115     if (test_ctx->protocol) {
116         if (result.client_protocol != test_ctx->protocol) {
117             fprintf(stderr, "Protocol mismatch: expected %s, got %s.\n",
118                     ssl_protocol_name(test_ctx->protocol),
119                     ssl_protocol_name(result.client_protocol));
120             return 0;
121         }
122     }
123     return 1;
124 }
125
126 static int check_servername(HANDSHAKE_RESULT result, SSL_TEST_CTX *test_ctx)
127 {
128     if (test_ctx->servername != SSL_TEST_SERVERNAME_NONE
129         && result.servername != test_ctx->servername) {
130         fprintf(stderr, "Client ServerName mismatch, expected %s, got %s\n.",
131                 ssl_servername_name(test_ctx->servername),
132                 ssl_servername_name(result.servername));
133         return 0;
134     }
135     return 1;
136 }
137
138 static int check_session_ticket(HANDSHAKE_RESULT result, SSL_TEST_CTX *test_ctx)
139 {
140     if (test_ctx->session_ticket_expected == SSL_TEST_SESSION_TICKET_IGNORE)
141         return 1;
142     if (test_ctx->session_ticket_expected == SSL_TEST_SESSION_TICKET_BROKEN &&
143         result.session_ticket == SSL_TEST_SESSION_TICKET_NO)
144         return 1;
145     if (result.session_ticket != test_ctx->session_ticket_expected) {
146         fprintf(stderr, "Client SessionTicketExpected mismatch, expected %s, got %s\n.",
147                 ssl_session_ticket_name(test_ctx->session_ticket_expected),
148                 ssl_session_ticket_name(result.session_ticket));
149         return 0;
150     }
151     return 1;
152 }
153
154 /*
155  * This could be further simplified by constructing an expected
156  * HANDSHAKE_RESULT, and implementing comparison methods for
157  * its fields.
158  */
159 static int check_test(HANDSHAKE_RESULT result, SSL_TEST_CTX *test_ctx)
160 {
161     int ret = 1;
162     ret &= check_result(result, test_ctx);
163     ret &= check_alerts(result, test_ctx);
164     if (result.result == SSL_TEST_SUCCESS) {
165         ret &= check_protocol(result, test_ctx);
166         ret &= check_servername(result, test_ctx);
167         ret &= check_session_ticket(result, test_ctx);
168         ret &= (result.session_ticket_do_not_call == 0);
169     }
170     return ret;
171 }
172
173 static int execute_test(SSL_TEST_FIXTURE fixture)
174 {
175     int ret = 0;
176     SSL_CTX *server_ctx = NULL, *server2_ctx = NULL, *client_ctx = NULL;
177     SSL_TEST_CTX *test_ctx = NULL;
178     HANDSHAKE_RESULT result;
179
180     test_ctx = SSL_TEST_CTX_create(conf, fixture.test_app);
181     if (test_ctx == NULL)
182         goto err;
183
184 #ifndef OPENSSL_NO_DTLS
185     if (test_ctx->method == SSL_TEST_METHOD_DTLS) {
186         server_ctx = SSL_CTX_new(DTLS_server_method());
187         server2_ctx = SSL_CTX_new(DTLS_server_method());
188         client_ctx = SSL_CTX_new(DTLS_client_method());
189     }
190 #endif
191     if (test_ctx->method == SSL_TEST_METHOD_TLS) {
192         server_ctx = SSL_CTX_new(TLS_server_method());
193         server2_ctx = SSL_CTX_new(TLS_server_method());
194         client_ctx = SSL_CTX_new(TLS_client_method());
195     }
196
197     OPENSSL_assert(server_ctx != NULL && server2_ctx != NULL &&
198                    client_ctx != NULL);
199
200     OPENSSL_assert(CONF_modules_load(conf, fixture.test_app, 0) > 0);
201
202     if (!SSL_CTX_config(server_ctx, "server")
203         || !SSL_CTX_config(server2_ctx, "server2")
204         || !SSL_CTX_config(client_ctx, "client")) {
205         goto err;
206     }
207
208     test_ctx = SSL_TEST_CTX_create(conf, fixture.test_app);
209     if (test_ctx == NULL)
210         goto err;
211
212     result = do_handshake(server_ctx, server2_ctx, client_ctx, test_ctx);
213
214     ret = check_test(result, test_ctx);
215
216 err:
217     CONF_modules_unload(0);
218     SSL_CTX_free(server_ctx);
219     SSL_CTX_free(server2_ctx);
220     SSL_CTX_free(client_ctx);
221     SSL_TEST_CTX_free(test_ctx);
222     if (ret != 1)
223         ERR_print_errors_fp(stderr);
224     return ret;
225 }
226
227 static void tear_down(SSL_TEST_FIXTURE fixture)
228 {
229 }
230
231 #define SETUP_SSL_TEST_FIXTURE()                        \
232     SETUP_TEST_FIXTURE(SSL_TEST_FIXTURE, set_up)
233 #define EXECUTE_SSL_TEST()             \
234     EXECUTE_TEST(execute_test, tear_down)
235
236 static int test_handshake(int idx)
237 {
238     SETUP_SSL_TEST_FIXTURE();
239     BIO_snprintf(fixture.test_app, sizeof(fixture.test_app),
240                  "test-%d", idx);
241     EXECUTE_SSL_TEST();
242 }
243
244 int main(int argc, char **argv)
245 {
246     int result = 0;
247     long num_tests;
248
249     if (argc != 2)
250         return 1;
251
252     conf = NCONF_new(NULL);
253     OPENSSL_assert(conf != NULL);
254
255     /* argv[1] should point to the test conf file */
256     OPENSSL_assert(NCONF_load(conf, argv[1], NULL) > 0);
257
258     OPENSSL_assert(NCONF_get_number_e(conf, NULL, "num_tests", &num_tests));
259
260     ADD_ALL_TESTS(test_handshake, (int)(num_tests));
261     result = run_tests(argv[0]);
262
263     return result;
264 }