Add compression tests
[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 #include "test_main_custom.h"
21
22 static CONF *conf = NULL;
23
24 /* Currently the section names are of the form test-<number>, e.g. test-15. */
25 #define MAX_TESTCASE_NAME_LENGTH 100
26
27 static const char *print_alert(int alert)
28 {
29     return alert ? SSL_alert_desc_string_long(alert) : "no alert";
30 }
31
32 static int check_result(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
33 {
34     if (result->result != test_ctx->expected_result) {
35         fprintf(stderr, "ExpectedResult mismatch: expected %s, got %s.\n",
36                 ssl_test_result_name(test_ctx->expected_result),
37                 ssl_test_result_name(result->result));
38         return 0;
39     }
40     return 1;
41 }
42
43 static int check_alerts(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
44 {
45     if (result->client_alert_sent != result->client_alert_received) {
46         fprintf(stderr, "Client sent alert %s but server received %s\n.",
47                 print_alert(result->client_alert_sent),
48                 print_alert(result->client_alert_received));
49         /*
50          * We can't bail here because the peer doesn't always get far enough
51          * to process a received alert. Specifically, in protocol version
52          * negotiation tests, we have the following scenario.
53          * Client supports TLS v1.2 only; Server supports TLS v1.1.
54          * Client proposes TLS v1.2; server responds with 1.1;
55          * Client now sends a protocol alert, using TLS v1.2 in the header.
56          * The server, however, rejects the alert because of version mismatch
57          * in the record layer; therefore, the server appears to never
58          * receive the alert.
59          */
60         /* return 0; */
61     }
62
63     if (result->server_alert_sent != result->server_alert_received) {
64         fprintf(stderr, "Server sent alert %s but client received %s\n.",
65                 print_alert(result->server_alert_sent),
66                 print_alert(result->server_alert_received));
67         /* return 0; */
68     }
69
70     /* Tolerate an alert if one wasn't explicitly specified in the test. */
71     if (test_ctx->expected_client_alert
72         /*
73          * The info callback alert value is computed as
74          * (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]
75          * where the low byte is the alert code and the high byte is other stuff.
76          */
77         && (result->client_alert_sent & 0xff) != test_ctx->expected_client_alert) {
78         fprintf(stderr, "ClientAlert mismatch: expected %s, got %s.\n",
79                 print_alert(test_ctx->expected_client_alert),
80                 print_alert(result->client_alert_sent));
81         return 0;
82     }
83
84     if (test_ctx->expected_server_alert
85         && (result->server_alert_sent & 0xff) != test_ctx->expected_server_alert) {
86         fprintf(stderr, "ServerAlert mismatch: expected %s, got %s.\n",
87                 print_alert(test_ctx->expected_server_alert),
88                 print_alert(result->server_alert_sent));
89         return 0;
90     }
91
92     if (result->client_num_fatal_alerts_sent > 1) {
93         fprintf(stderr, "Client sent %d fatal alerts.\n",
94                 result->client_num_fatal_alerts_sent);
95         return 0;
96     }
97     if (result->server_num_fatal_alerts_sent > 1) {
98         fprintf(stderr, "Server sent %d alerts.\n",
99                 result->server_num_fatal_alerts_sent);
100         return 0;
101     }
102     return 1;
103 }
104
105 static int check_protocol(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
106 {
107     if (result->client_protocol != result->server_protocol) {
108         fprintf(stderr, "Client has protocol %s but server has %s\n.",
109                 ssl_protocol_name(result->client_protocol),
110                 ssl_protocol_name(result->server_protocol));
111         return 0;
112     }
113
114     if (test_ctx->expected_protocol) {
115         if (result->client_protocol != test_ctx->expected_protocol) {
116             fprintf(stderr, "Protocol mismatch: expected %s, got %s.\n",
117                     ssl_protocol_name(test_ctx->expected_protocol),
118                     ssl_protocol_name(result->client_protocol));
119             return 0;
120         }
121     }
122     return 1;
123 }
124
125 static int check_servername(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
126 {
127     if (result->servername != test_ctx->expected_servername) {
128       fprintf(stderr, "Client ServerName mismatch, expected %s, got %s\n.",
129               ssl_servername_name(test_ctx->expected_servername),
130               ssl_servername_name(result->servername));
131       return 0;
132     }
133   return 1;
134 }
135
136 static int check_session_ticket(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
137 {
138     if (test_ctx->session_ticket_expected == SSL_TEST_SESSION_TICKET_IGNORE)
139         return 1;
140     if (result->session_ticket != test_ctx->session_ticket_expected) {
141         fprintf(stderr, "Client SessionTicketExpected mismatch, expected %s, got %s\n.",
142                 ssl_session_ticket_name(test_ctx->session_ticket_expected),
143                 ssl_session_ticket_name(result->session_ticket));
144         return 0;
145     }
146     return 1;
147 }
148
149 static int check_compression(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
150 {
151     if (result->compression != test_ctx->compression_expected) {
152         fprintf(stderr, "Client CompressionExpected mismatch, expected %s, got %s\n.",
153                 ssl_compression_name(test_ctx->compression_expected),
154                 ssl_compression_name(result->compression));
155         return 0;
156     }
157     return 1;
158 }
159 #ifndef OPENSSL_NO_NEXTPROTONEG
160 static int check_npn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
161 {
162     int ret = 1;
163     ret &= strings_equal("NPN Negotiated (client vs server)",
164                          result->client_npn_negotiated,
165                          result->server_npn_negotiated);
166     ret &= strings_equal("ExpectedNPNProtocol",
167                          test_ctx->expected_npn_protocol,
168                          result->client_npn_negotiated);
169     return ret;
170 }
171 #endif
172
173 static int check_alpn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
174 {
175     int ret = 1;
176     ret &= strings_equal("ALPN Negotiated (client vs server)",
177                          result->client_alpn_negotiated,
178                          result->server_alpn_negotiated);
179     ret &= strings_equal("ExpectedALPNProtocol",
180                          test_ctx->expected_alpn_protocol,
181                          result->client_alpn_negotiated);
182     return ret;
183 }
184
185 static int check_resumption(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
186 {
187     if (result->client_resumed != result->server_resumed) {
188         fprintf(stderr, "Resumption mismatch (client vs server): %d vs %d\n",
189                 result->client_resumed, result->server_resumed);
190         return 0;
191     }
192     if (result->client_resumed != test_ctx->resumption_expected) {
193         fprintf(stderr, "ResumptionExpected mismatch: %d vs %d\n",
194                 test_ctx->resumption_expected, result->client_resumed);
195         return 0;
196     }
197     return 1;
198 }
199
200 static int check_nid(const char *name, int expected_nid, int nid)
201 {
202     if (expected_nid == 0 || expected_nid == nid)
203         return 1;
204     fprintf(stderr, "%s type mismatch, %s vs %s\n",
205             name, OBJ_nid2ln(expected_nid),
206             nid == NID_undef ? "absent" : OBJ_nid2ln(nid));
207     return 0;
208 }
209
210 static int check_tmp_key(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
211 {
212     return check_nid("Tmp key", test_ctx->expected_tmp_key_type,
213                      result->tmp_key_type);
214 }
215
216 static int check_server_cert_type(HANDSHAKE_RESULT *result,
217                                   SSL_TEST_CTX *test_ctx)
218 {
219     return check_nid("Server certificate", test_ctx->expected_server_cert_type,
220                      result->server_cert_type);
221 }
222
223 static int check_server_sign_hash(HANDSHAKE_RESULT *result,
224                                   SSL_TEST_CTX *test_ctx)
225 {
226     return check_nid("Server signing hash", test_ctx->expected_server_sign_hash,
227                      result->server_sign_hash);
228 }
229
230 static int check_server_sign_type(HANDSHAKE_RESULT *result,
231                                   SSL_TEST_CTX *test_ctx)
232 {
233     return check_nid("Server signing", test_ctx->expected_server_sign_type,
234                      result->server_sign_type);
235 }
236
237 static int check_client_cert_type(HANDSHAKE_RESULT *result,
238                                   SSL_TEST_CTX *test_ctx)
239 {
240     return check_nid("Client certificate", test_ctx->expected_client_cert_type,
241                      result->client_cert_type);
242 }
243
244 static int check_client_sign_hash(HANDSHAKE_RESULT *result,
245                                   SSL_TEST_CTX *test_ctx)
246 {
247     return check_nid("Client signing hash", test_ctx->expected_client_sign_hash,
248                      result->client_sign_hash);
249 }
250
251 static int check_client_sign_type(HANDSHAKE_RESULT *result,
252                                   SSL_TEST_CTX *test_ctx)
253 {
254     return check_nid("Client signing", test_ctx->expected_client_sign_type,
255                      result->client_sign_type);
256 }
257
258 /*
259  * This could be further simplified by constructing an expected
260  * HANDSHAKE_RESULT, and implementing comparison methods for
261  * its fields.
262  */
263 static int check_test(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
264 {
265     int ret = 1;
266     ret &= check_result(result, test_ctx);
267     ret &= check_alerts(result, test_ctx);
268     if (result->result == SSL_TEST_SUCCESS) {
269         ret &= check_protocol(result, test_ctx);
270         ret &= check_servername(result, test_ctx);
271         ret &= check_session_ticket(result, test_ctx);
272         ret &= check_compression(result, test_ctx);
273         ret &= (result->session_ticket_do_not_call == 0);
274 #ifndef OPENSSL_NO_NEXTPROTONEG
275         ret &= check_npn(result, test_ctx);
276 #endif
277         ret &= check_alpn(result, test_ctx);
278         ret &= check_resumption(result, test_ctx);
279         ret &= check_tmp_key(result, test_ctx);
280         ret &= check_server_cert_type(result, test_ctx);
281         ret &= check_server_sign_hash(result, test_ctx);
282         ret &= check_server_sign_type(result, test_ctx);
283         ret &= check_client_cert_type(result, test_ctx);
284         ret &= check_client_sign_hash(result, test_ctx);
285         ret &= check_client_sign_type(result, test_ctx);
286     }
287     return ret;
288 }
289
290 static int test_handshake(int idx)
291 {
292     int ret = 0;
293     SSL_CTX *server_ctx = NULL, *server2_ctx = NULL, *client_ctx = NULL,
294         *resume_server_ctx = NULL, *resume_client_ctx = NULL;
295     SSL_TEST_CTX *test_ctx = NULL;
296     HANDSHAKE_RESULT *result = NULL;
297     char test_app[MAX_TESTCASE_NAME_LENGTH];
298
299     BIO_snprintf(test_app, sizeof(test_app), "test-%d", idx);
300
301     test_ctx = SSL_TEST_CTX_create(conf, test_app);
302     if (test_ctx == NULL)
303         goto err;
304
305 #ifndef OPENSSL_NO_DTLS
306     if (test_ctx->method == SSL_TEST_METHOD_DTLS) {
307         server_ctx = SSL_CTX_new(DTLS_server_method());
308         if (test_ctx->extra.server.servername_callback !=
309             SSL_TEST_SERVERNAME_CB_NONE) {
310             server2_ctx = SSL_CTX_new(DTLS_server_method());
311             TEST_check(server2_ctx != NULL);
312         }
313         client_ctx = SSL_CTX_new(DTLS_client_method());
314         if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
315             resume_server_ctx = SSL_CTX_new(DTLS_server_method());
316             resume_client_ctx = SSL_CTX_new(DTLS_client_method());
317             TEST_check(resume_server_ctx != NULL);
318             TEST_check(resume_client_ctx != NULL);
319         }
320     }
321 #endif
322     if (test_ctx->method == SSL_TEST_METHOD_TLS) {
323         server_ctx = SSL_CTX_new(TLS_server_method());
324         /* SNI on resumption isn't supported/tested yet. */
325         if (test_ctx->extra.server.servername_callback !=
326             SSL_TEST_SERVERNAME_CB_NONE) {
327             server2_ctx = SSL_CTX_new(TLS_server_method());
328             TEST_check(server2_ctx != NULL);
329         }
330         client_ctx = SSL_CTX_new(TLS_client_method());
331
332         if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
333             resume_server_ctx = SSL_CTX_new(TLS_server_method());
334             resume_client_ctx = SSL_CTX_new(TLS_client_method());
335             TEST_check(resume_server_ctx != NULL);
336             TEST_check(resume_client_ctx != NULL);
337         }
338     }
339
340     TEST_check(server_ctx != NULL);
341     TEST_check(client_ctx != NULL);
342
343     TEST_check(CONF_modules_load(conf, test_app, 0) > 0);
344
345     if (!SSL_CTX_config(server_ctx, "server")
346         || !SSL_CTX_config(client_ctx, "client")) {
347         goto err;
348     }
349
350     if (server2_ctx != NULL && !SSL_CTX_config(server2_ctx, "server2"))
351         goto err;
352     if (resume_server_ctx != NULL
353         && !SSL_CTX_config(resume_server_ctx, "resume-server"))
354         goto err;
355     if (resume_client_ctx != NULL
356         && !SSL_CTX_config(resume_client_ctx, "resume-client"))
357         goto err;
358
359     result = do_handshake(server_ctx, server2_ctx, client_ctx,
360                           resume_server_ctx, resume_client_ctx, test_ctx);
361
362     ret = check_test(result, test_ctx);
363
364 err:
365     CONF_modules_unload(0);
366     SSL_CTX_free(server_ctx);
367     SSL_CTX_free(server2_ctx);
368     SSL_CTX_free(client_ctx);
369     SSL_CTX_free(resume_server_ctx);
370     SSL_CTX_free(resume_client_ctx);
371     SSL_TEST_CTX_free(test_ctx);
372     HANDSHAKE_RESULT_free(result);
373     return ret;
374 }
375
376 int test_main(int argc, char **argv)
377 {
378     int result = 0;
379     long num_tests;
380
381     if (argc != 2)
382         return 1;
383
384     conf = NCONF_new(NULL);
385     TEST_check(conf != NULL);
386
387     /* argv[1] should point to the test conf file */
388     TEST_check(NCONF_load(conf, argv[1], NULL) > 0);
389
390     TEST_check(NCONF_get_number_e(conf, NULL, "num_tests", &num_tests));
391
392     ADD_ALL_TESTS(test_handshake, (int)(num_tests));
393     result = run_tests(argv[0]);
394
395     NCONF_free(conf);
396     return result;
397 }