Simplify tests part 2
[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 static const char *print_alert(int alert)
27 {
28     return alert ? SSL_alert_desc_string_long(alert) : "no alert";
29 }
30
31 static int check_result(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
32 {
33     if (result->result != test_ctx->expected_result) {
34         fprintf(stderr, "ExpectedResult mismatch: expected %s, got %s.\n",
35                 ssl_test_result_name(test_ctx->expected_result),
36                 ssl_test_result_name(result->result));
37         return 0;
38     }
39     return 1;
40 }
41
42 static int check_alerts(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
43 {
44     if (result->client_alert_sent != result->client_alert_received) {
45         fprintf(stderr, "Client sent alert %s but server received %s\n.",
46                 print_alert(result->client_alert_sent),
47                 print_alert(result->client_alert_received));
48         /*
49          * We can't bail here because the peer doesn't always get far enough
50          * to process a received alert. Specifically, in protocol version
51          * negotiation tests, we have the following scenario.
52          * Client supports TLS v1.2 only; Server supports TLS v1.1.
53          * Client proposes TLS v1.2; server responds with 1.1;
54          * Client now sends a protocol alert, using TLS v1.2 in the header.
55          * The server, however, rejects the alert because of version mismatch
56          * in the record layer; therefore, the server appears to never
57          * receive the alert.
58          */
59         /* return 0; */
60     }
61
62     if (result->server_alert_sent != result->server_alert_received) {
63         fprintf(stderr, "Server sent alert %s but client received %s\n.",
64                 print_alert(result->server_alert_sent),
65                 print_alert(result->server_alert_received));
66         /* return 0; */
67     }
68
69     /* Tolerate an alert if one wasn't explicitly specified in the test. */
70     if (test_ctx->expected_client_alert
71         /*
72          * The info callback alert value is computed as
73          * (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]
74          * where the low byte is the alert code and the high byte is other stuff.
75          */
76         && (result->client_alert_sent & 0xff) != test_ctx->expected_client_alert) {
77         fprintf(stderr, "ClientAlert mismatch: expected %s, got %s.\n",
78                 print_alert(test_ctx->expected_client_alert),
79                 print_alert(result->client_alert_sent));
80         return 0;
81     }
82
83     if (test_ctx->expected_server_alert
84         && (result->server_alert_sent & 0xff) != test_ctx->expected_server_alert) {
85         fprintf(stderr, "ServerAlert mismatch: expected %s, got %s.\n",
86                 print_alert(test_ctx->expected_server_alert),
87                 print_alert(result->server_alert_sent));
88         return 0;
89     }
90
91     if (result->client_num_fatal_alerts_sent > 1) {
92         fprintf(stderr, "Client sent %d fatal alerts.\n",
93                 result->client_num_fatal_alerts_sent);
94         return 0;
95     }
96     if (result->server_num_fatal_alerts_sent > 1) {
97         fprintf(stderr, "Server sent %d alerts.\n",
98                 result->server_num_fatal_alerts_sent);
99         return 0;
100     }
101     return 1;
102 }
103
104 static int check_protocol(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
105 {
106     if (result->client_protocol != result->server_protocol) {
107         fprintf(stderr, "Client has protocol %s but server has %s\n.",
108                 ssl_protocol_name(result->client_protocol),
109                 ssl_protocol_name(result->server_protocol));
110         return 0;
111     }
112
113     if (test_ctx->expected_protocol) {
114         if (result->client_protocol != test_ctx->expected_protocol) {
115             fprintf(stderr, "Protocol mismatch: expected %s, got %s.\n",
116                     ssl_protocol_name(test_ctx->expected_protocol),
117                     ssl_protocol_name(result->client_protocol));
118             return 0;
119         }
120     }
121     return 1;
122 }
123
124 static int check_servername(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
125 {
126     if (result->servername != test_ctx->expected_servername) {
127       fprintf(stderr, "Client ServerName mismatch, expected %s, got %s\n.",
128               ssl_servername_name(test_ctx->expected_servername),
129               ssl_servername_name(result->servername));
130       return 0;
131     }
132   return 1;
133 }
134
135 static int check_session_ticket(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
136 {
137     if (test_ctx->session_ticket_expected == SSL_TEST_SESSION_TICKET_IGNORE)
138         return 1;
139     if (result->session_ticket != test_ctx->session_ticket_expected) {
140         fprintf(stderr, "Client SessionTicketExpected mismatch, expected %s, got %s\n.",
141                 ssl_session_ticket_name(test_ctx->session_ticket_expected),
142                 ssl_session_ticket_name(result->session_ticket));
143         return 0;
144     }
145     return 1;
146 }
147
148 #ifndef OPENSSL_NO_NEXTPROTONEG
149 static int check_npn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
150 {
151     int ret = 1;
152     ret &= strings_equal("NPN Negotiated (client vs server)",
153                          result->client_npn_negotiated,
154                          result->server_npn_negotiated);
155     ret &= strings_equal("ExpectedNPNProtocol",
156                          test_ctx->expected_npn_protocol,
157                          result->client_npn_negotiated);
158     return ret;
159 }
160 #endif
161
162 static int check_alpn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
163 {
164     int ret = 1;
165     ret &= strings_equal("ALPN Negotiated (client vs server)",
166                          result->client_alpn_negotiated,
167                          result->server_alpn_negotiated);
168     ret &= strings_equal("ExpectedALPNProtocol",
169                          test_ctx->expected_alpn_protocol,
170                          result->client_alpn_negotiated);
171     return ret;
172 }
173
174 static int check_resumption(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
175 {
176     if (result->client_resumed != result->server_resumed) {
177         fprintf(stderr, "Resumption mismatch (client vs server): %d vs %d\n",
178                 result->client_resumed, result->server_resumed);
179         return 0;
180     }
181     if (result->client_resumed != test_ctx->resumption_expected) {
182         fprintf(stderr, "ResumptionExpected mismatch: %d vs %d\n",
183                 test_ctx->resumption_expected, result->client_resumed);
184         return 0;
185     }
186     return 1;
187 }
188
189 /*
190  * This could be further simplified by constructing an expected
191  * HANDSHAKE_RESULT, and implementing comparison methods for
192  * its fields.
193  */
194 static int check_test(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
195 {
196     int ret = 1;
197     ret &= check_result(result, test_ctx);
198     ret &= check_alerts(result, test_ctx);
199     if (result->result == SSL_TEST_SUCCESS) {
200         ret &= check_protocol(result, test_ctx);
201         ret &= check_servername(result, test_ctx);
202         ret &= check_session_ticket(result, test_ctx);
203         ret &= (result->session_ticket_do_not_call == 0);
204 #ifndef OPENSSL_NO_NEXTPROTONEG
205         ret &= check_npn(result, test_ctx);
206 #endif
207         ret &= check_alpn(result, test_ctx);
208         ret &= check_resumption(result, test_ctx);
209     }
210     return ret;
211 }
212
213 static int test_handshake(int idx)
214 {
215     int ret = 0;
216     SSL_CTX *server_ctx = NULL, *server2_ctx = NULL, *client_ctx = NULL,
217         *resume_server_ctx = NULL, *resume_client_ctx = NULL;
218     SSL_TEST_CTX *test_ctx = NULL;
219     HANDSHAKE_RESULT *result = NULL;
220     char test_app[MAX_TESTCASE_NAME_LENGTH];
221
222     BIO_snprintf(test_app, sizeof(test_app), "test-%d", idx);
223
224     test_ctx = SSL_TEST_CTX_create(conf, test_app);
225     if (test_ctx == NULL)
226         goto err;
227
228 #ifndef OPENSSL_NO_DTLS
229     if (test_ctx->method == SSL_TEST_METHOD_DTLS) {
230         server_ctx = SSL_CTX_new(DTLS_server_method());
231         if (test_ctx->extra.server.servername_callback !=
232             SSL_TEST_SERVERNAME_CB_NONE) {
233             server2_ctx = SSL_CTX_new(DTLS_server_method());
234             TEST_check(server2_ctx != NULL);
235         }
236         client_ctx = SSL_CTX_new(DTLS_client_method());
237         if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
238             resume_server_ctx = SSL_CTX_new(DTLS_server_method());
239             resume_client_ctx = SSL_CTX_new(DTLS_client_method());
240             TEST_check(resume_server_ctx != NULL);
241             TEST_check(resume_client_ctx != NULL);
242         }
243     }
244 #endif
245     if (test_ctx->method == SSL_TEST_METHOD_TLS) {
246         server_ctx = SSL_CTX_new(TLS_server_method());
247         /* SNI on resumption isn't supported/tested yet. */
248         if (test_ctx->extra.server.servername_callback !=
249             SSL_TEST_SERVERNAME_CB_NONE) {
250             server2_ctx = SSL_CTX_new(TLS_server_method());
251             TEST_check(server2_ctx != NULL);
252         }
253         client_ctx = SSL_CTX_new(TLS_client_method());
254
255         if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
256             resume_server_ctx = SSL_CTX_new(TLS_server_method());
257             resume_client_ctx = SSL_CTX_new(TLS_client_method());
258             TEST_check(resume_server_ctx != NULL);
259             TEST_check(resume_client_ctx != NULL);
260         }
261     }
262
263     TEST_check(server_ctx != NULL);
264     TEST_check(client_ctx != NULL);
265
266     TEST_check(CONF_modules_load(conf, test_app, 0) > 0);
267
268     if (!SSL_CTX_config(server_ctx, "server")
269         || !SSL_CTX_config(client_ctx, "client")) {
270         goto err;
271     }
272
273     if (server2_ctx != NULL && !SSL_CTX_config(server2_ctx, "server2"))
274         goto err;
275     if (resume_server_ctx != NULL
276         && !SSL_CTX_config(resume_server_ctx, "resume-server"))
277         goto err;
278     if (resume_client_ctx != NULL
279         && !SSL_CTX_config(resume_client_ctx, "resume-client"))
280         goto err;
281
282     result = do_handshake(server_ctx, server2_ctx, client_ctx,
283                           resume_server_ctx, resume_client_ctx, test_ctx);
284
285     ret = check_test(result, test_ctx);
286
287 err:
288     CONF_modules_unload(0);
289     SSL_CTX_free(server_ctx);
290     SSL_CTX_free(server2_ctx);
291     SSL_CTX_free(client_ctx);
292     SSL_CTX_free(resume_server_ctx);
293     SSL_CTX_free(resume_client_ctx);
294     SSL_TEST_CTX_free(test_ctx);
295     HANDSHAKE_RESULT_free(result);
296     return ret;
297 }
298
299 int main(int argc, char **argv)
300 {
301     int result = 0;
302     long num_tests;
303
304     if (argc != 2)
305         return 1;
306
307     conf = NCONF_new(NULL);
308     TEST_check(conf != NULL);
309
310     /* argv[1] should point to the test conf file */
311     TEST_check(NCONF_load(conf, argv[1], NULL) > 0);
312
313     TEST_check(NCONF_get_number_e(conf, NULL, "num_tests", &num_tests));
314
315     ADD_ALL_TESTS(test_handshake, (int)(num_tests));
316     result = run_tests(argv[0]);
317
318     return result;
319 }