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