Write a test provider to test the TLS-GROUPS capability
[openssl.git] / test / ssl_test.c
1 /*
2  * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 #include <openssl/provider.h>
17
18 #include "handshake_helper.h"
19 #include "ssl_test_ctx.h"
20 #include "testutil.h"
21
22 DEFINE_STACK_OF(X509_NAME)
23
24 static CONF *conf = NULL;
25 static OSSL_PROVIDER *defctxnull = NULL, *thisprov = NULL;
26 static OPENSSL_CTX *libctx = NULL;
27
28 /* Currently the section names are of the form test-<number>, e.g. test-15. */
29 #define MAX_TESTCASE_NAME_LENGTH 100
30
31 static const char *print_alert(int alert)
32 {
33     return alert ? SSL_alert_desc_string_long(alert) : "no alert";
34 }
35
36 static int check_result(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
37 {
38     if (!TEST_int_eq(result->result, test_ctx->expected_result)) {
39         TEST_info("ExpectedResult mismatch: expected %s, got %s.",
40                   ssl_test_result_name(test_ctx->expected_result),
41                   ssl_test_result_name(result->result));
42         return 0;
43     }
44     return 1;
45 }
46
47 static int check_alerts(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
48 {
49     if (!TEST_int_eq(result->client_alert_sent,
50                      result->client_alert_received)) {
51         TEST_info("Client sent alert %s but server received %s.",
52                   print_alert(result->client_alert_sent),
53                   print_alert(result->client_alert_received));
54         /*
55          * We can't bail here because the peer doesn't always get far enough
56          * to process a received alert. Specifically, in protocol version
57          * negotiation tests, we have the following scenario.
58          * Client supports TLS v1.2 only; Server supports TLS v1.1.
59          * Client proposes TLS v1.2; server responds with 1.1;
60          * Client now sends a protocol alert, using TLS v1.2 in the header.
61          * The server, however, rejects the alert because of version mismatch
62          * in the record layer; therefore, the server appears to never
63          * receive the alert.
64          */
65         /* return 0; */
66     }
67
68     if (!TEST_int_eq(result->server_alert_sent,
69                      result->server_alert_received)) {
70         TEST_info("Server sent alert %s but client received %s.",
71                   print_alert(result->server_alert_sent),
72                   print_alert(result->server_alert_received));
73         /* return 0; */
74     }
75
76     /* Tolerate an alert if one wasn't explicitly specified in the test. */
77     if (test_ctx->expected_client_alert
78         /*
79          * The info callback alert value is computed as
80          * (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]
81          * where the low byte is the alert code and the high byte is other stuff.
82          */
83         && (result->client_alert_sent & 0xff) != test_ctx->expected_client_alert) {
84         TEST_error("ClientAlert mismatch: expected %s, got %s.",
85                    print_alert(test_ctx->expected_client_alert),
86                    print_alert(result->client_alert_sent));
87         return 0;
88     }
89
90     if (test_ctx->expected_server_alert
91         && (result->server_alert_sent & 0xff) != test_ctx->expected_server_alert) {
92         TEST_error("ServerAlert mismatch: expected %s, got %s.",
93                    print_alert(test_ctx->expected_server_alert),
94                    print_alert(result->server_alert_sent));
95         return 0;
96     }
97
98     if (!TEST_int_le(result->client_num_fatal_alerts_sent, 1))
99         return 0;
100     if (!TEST_int_le(result->server_num_fatal_alerts_sent, 1))
101         return 0;
102     return 1;
103 }
104
105 static int check_protocol(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
106 {
107     if (!TEST_int_eq(result->client_protocol, result->server_protocol)) {
108         TEST_info("Client has protocol %s but server has %s.",
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 (!TEST_int_eq(result->client_protocol,
116                          test_ctx->expected_protocol)) {
117             TEST_info("Protocol mismatch: expected %s, got %s.\n",
118                       ssl_protocol_name(test_ctx->expected_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_int_eq(result->servername, test_ctx->expected_servername)) {
129       TEST_info("Client ServerName mismatch, expected %s, got %s.",
130                 ssl_servername_name(test_ctx->expected_servername),
131                 ssl_servername_name(result->servername));
132       return 0;
133     }
134   return 1;
135 }
136
137 static int check_session_ticket(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
138 {
139     if (test_ctx->session_ticket_expected == SSL_TEST_SESSION_TICKET_IGNORE)
140         return 1;
141     if (!TEST_int_eq(result->session_ticket,
142                      test_ctx->session_ticket_expected)) {
143         TEST_info("Client SessionTicketExpected mismatch, expected %s, got %s.",
144                   ssl_session_ticket_name(test_ctx->session_ticket_expected),
145                   ssl_session_ticket_name(result->session_ticket));
146         return 0;
147     }
148     return 1;
149 }
150
151 static int check_session_id(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
152 {
153     if (test_ctx->session_id_expected == SSL_TEST_SESSION_ID_IGNORE)
154         return 1;
155     if (!TEST_int_eq(result->session_id, test_ctx->session_id_expected)) {
156         TEST_info("Client SessionIdExpected mismatch, expected %s, got %s\n.",
157                 ssl_session_id_name(test_ctx->session_id_expected),
158                 ssl_session_id_name(result->session_id));
159         return 0;
160     }
161     return 1;
162 }
163
164 static int check_compression(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
165 {
166     if (!TEST_int_eq(result->compression, test_ctx->compression_expected))
167         return 0;
168     return 1;
169 }
170 #ifndef OPENSSL_NO_NEXTPROTONEG
171 static int check_npn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
172 {
173     int ret = 1;
174     if (!TEST_str_eq(result->client_npn_negotiated,
175                      result->server_npn_negotiated))
176         ret = 0;
177     if (!TEST_str_eq(test_ctx->expected_npn_protocol,
178                      result->client_npn_negotiated))
179         ret = 0;
180     return ret;
181 }
182 #endif
183
184 static int check_alpn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
185 {
186     int ret = 1;
187     if (!TEST_str_eq(result->client_alpn_negotiated,
188                      result->server_alpn_negotiated))
189         ret = 0;
190     if (!TEST_str_eq(test_ctx->expected_alpn_protocol,
191                      result->client_alpn_negotiated))
192         ret = 0;
193     return ret;
194 }
195
196 static int check_session_ticket_app_data(HANDSHAKE_RESULT *result,
197                                          SSL_TEST_CTX *test_ctx)
198 {
199     size_t result_len = 0;
200     size_t expected_len = 0;
201
202     /* consider empty and NULL strings to be the same */
203     if (result->result_session_ticket_app_data != NULL)
204         result_len = strlen(result->result_session_ticket_app_data);
205     if (test_ctx->expected_session_ticket_app_data != NULL)
206         expected_len = strlen(test_ctx->expected_session_ticket_app_data);
207     if (result_len == 0 && expected_len == 0)
208         return 1;
209
210     if (!TEST_str_eq(result->result_session_ticket_app_data,
211                      test_ctx->expected_session_ticket_app_data))
212         return 0;
213
214     return 1;
215 }
216
217 static int check_resumption(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
218 {
219     if (!TEST_int_eq(result->client_resumed, result->server_resumed))
220         return 0;
221     if (!TEST_int_eq(result->client_resumed, test_ctx->resumption_expected))
222         return 0;
223     return 1;
224 }
225
226 static int check_nid(const char *name, int expected_nid, int nid)
227 {
228     if (expected_nid == 0 || expected_nid == nid)
229         return 1;
230     TEST_error("%s type mismatch, %s vs %s\n",
231                name, OBJ_nid2ln(expected_nid),
232                nid == NID_undef ? "absent" : OBJ_nid2ln(nid));
233     return 0;
234 }
235
236 static void print_ca_names(STACK_OF(X509_NAME) *names)
237 {
238     int i;
239
240     if (names == NULL || sk_X509_NAME_num(names) == 0) {
241         TEST_note("    <empty>");
242         return;
243     }
244     for (i = 0; i < sk_X509_NAME_num(names); i++) {
245         X509_NAME_print_ex(bio_err, sk_X509_NAME_value(names, i), 4,
246                            XN_FLAG_ONELINE);
247         BIO_puts(bio_err, "\n");
248     }
249 }
250
251 static int check_ca_names(const char *name,
252                           STACK_OF(X509_NAME) *expected_names,
253                           STACK_OF(X509_NAME) *names)
254 {
255     int i;
256
257     if (expected_names == NULL)
258         return 1;
259     if (names == NULL || sk_X509_NAME_num(names) == 0) {
260         if (TEST_int_eq(sk_X509_NAME_num(expected_names), 0))
261             return 1;
262         goto err;
263     }
264     if (sk_X509_NAME_num(names) != sk_X509_NAME_num(expected_names))
265         goto err;
266     for (i = 0; i < sk_X509_NAME_num(names); i++) {
267         if (!TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(names, i),
268                                        sk_X509_NAME_value(expected_names, i)),
269                          0)) {
270             goto err;
271         }
272     }
273     return 1;
274 err:
275     TEST_info("%s: list mismatch", name);
276     TEST_note("Expected Names:");
277     print_ca_names(expected_names);
278     TEST_note("Received Names:");
279     print_ca_names(names);
280     return 0;
281 }
282
283 static int check_tmp_key(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
284 {
285     return check_nid("Tmp key", test_ctx->expected_tmp_key_type,
286                      result->tmp_key_type);
287 }
288
289 static int check_server_cert_type(HANDSHAKE_RESULT *result,
290                                   SSL_TEST_CTX *test_ctx)
291 {
292     return check_nid("Server certificate", test_ctx->expected_server_cert_type,
293                      result->server_cert_type);
294 }
295
296 static int check_server_sign_hash(HANDSHAKE_RESULT *result,
297                                   SSL_TEST_CTX *test_ctx)
298 {
299     return check_nid("Server signing hash", test_ctx->expected_server_sign_hash,
300                      result->server_sign_hash);
301 }
302
303 static int check_server_sign_type(HANDSHAKE_RESULT *result,
304                                   SSL_TEST_CTX *test_ctx)
305 {
306     return check_nid("Server signing", test_ctx->expected_server_sign_type,
307                      result->server_sign_type);
308 }
309
310 static int check_server_ca_names(HANDSHAKE_RESULT *result,
311                                  SSL_TEST_CTX *test_ctx)
312 {
313     return check_ca_names("Server CA names",
314                           test_ctx->expected_server_ca_names,
315                           result->server_ca_names);
316 }
317
318 static int check_client_cert_type(HANDSHAKE_RESULT *result,
319                                   SSL_TEST_CTX *test_ctx)
320 {
321     return check_nid("Client certificate", test_ctx->expected_client_cert_type,
322                      result->client_cert_type);
323 }
324
325 static int check_client_sign_hash(HANDSHAKE_RESULT *result,
326                                   SSL_TEST_CTX *test_ctx)
327 {
328     return check_nid("Client signing hash", test_ctx->expected_client_sign_hash,
329                      result->client_sign_hash);
330 }
331
332 static int check_client_sign_type(HANDSHAKE_RESULT *result,
333                                   SSL_TEST_CTX *test_ctx)
334 {
335     return check_nid("Client signing", test_ctx->expected_client_sign_type,
336                      result->client_sign_type);
337 }
338
339 static int check_client_ca_names(HANDSHAKE_RESULT *result,
340                                  SSL_TEST_CTX *test_ctx)
341 {
342     return check_ca_names("Client CA names",
343                           test_ctx->expected_client_ca_names,
344                           result->client_ca_names);
345 }
346
347 static int check_cipher(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
348 {
349     if (test_ctx->expected_cipher == NULL)
350         return 1;
351     if (!TEST_ptr(result->cipher))
352         return 0;
353     if (!TEST_str_eq(test_ctx->expected_cipher,
354                      result->cipher))
355         return 0;
356     return 1;
357 }
358
359 /*
360  * This could be further simplified by constructing an expected
361  * HANDSHAKE_RESULT, and implementing comparison methods for
362  * its fields.
363  */
364 static int check_test(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
365 {
366     int ret = 1;
367     ret &= check_result(result, test_ctx);
368     ret &= check_alerts(result, test_ctx);
369     if (result->result == SSL_TEST_SUCCESS) {
370         ret &= check_protocol(result, test_ctx);
371         ret &= check_servername(result, test_ctx);
372         ret &= check_session_ticket(result, test_ctx);
373         ret &= check_compression(result, test_ctx);
374         ret &= check_session_id(result, test_ctx);
375         ret &= (result->session_ticket_do_not_call == 0);
376 #ifndef OPENSSL_NO_NEXTPROTONEG
377         ret &= check_npn(result, test_ctx);
378 #endif
379         ret &= check_cipher(result, test_ctx);
380         ret &= check_alpn(result, test_ctx);
381         ret &= check_session_ticket_app_data(result, test_ctx);
382         ret &= check_resumption(result, test_ctx);
383         ret &= check_tmp_key(result, test_ctx);
384         ret &= check_server_cert_type(result, test_ctx);
385         ret &= check_server_sign_hash(result, test_ctx);
386         ret &= check_server_sign_type(result, test_ctx);
387         ret &= check_server_ca_names(result, test_ctx);
388         ret &= check_client_cert_type(result, test_ctx);
389         ret &= check_client_sign_hash(result, test_ctx);
390         ret &= check_client_sign_type(result, test_ctx);
391         ret &= check_client_ca_names(result, test_ctx);
392     }
393     return ret;
394 }
395
396 static int test_handshake(int idx)
397 {
398     int ret = 0;
399     SSL_CTX *server_ctx = NULL, *server2_ctx = NULL, *client_ctx = NULL,
400         *resume_server_ctx = NULL, *resume_client_ctx = NULL;
401     SSL_TEST_CTX *test_ctx = NULL;
402     HANDSHAKE_RESULT *result = NULL;
403     char test_app[MAX_TESTCASE_NAME_LENGTH];
404
405     BIO_snprintf(test_app, sizeof(test_app), "test-%d", idx);
406
407     test_ctx = SSL_TEST_CTX_create(conf, test_app);
408     if (!TEST_ptr(test_ctx))
409         goto err;
410
411 #ifndef OPENSSL_NO_DTLS
412     if (test_ctx->method == SSL_TEST_METHOD_DTLS) {
413         server_ctx = SSL_CTX_new_with_libctx(libctx, NULL, DTLS_server_method());
414         if (!TEST_true(SSL_CTX_set_max_proto_version(server_ctx, 0)))
415             goto err;
416         if (test_ctx->extra.server.servername_callback !=
417             SSL_TEST_SERVERNAME_CB_NONE) {
418             if (!TEST_ptr(server2_ctx =
419                             SSL_CTX_new_with_libctx(libctx, NULL,
420                                                     DTLS_server_method())))
421                 goto err;
422         }
423         client_ctx = SSL_CTX_new_with_libctx(libctx, NULL, DTLS_client_method());
424         if (!TEST_true(SSL_CTX_set_max_proto_version(client_ctx, 0)))
425             goto err;
426         if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
427             resume_server_ctx = SSL_CTX_new_with_libctx(libctx, NULL,
428                                                         DTLS_server_method());
429             if (!TEST_true(SSL_CTX_set_max_proto_version(resume_server_ctx, 0)))
430                 goto err;
431             resume_client_ctx = SSL_CTX_new_with_libctx(libctx, NULL,
432                                                         DTLS_client_method());
433             if (!TEST_true(SSL_CTX_set_max_proto_version(resume_client_ctx, 0)))
434                 goto err;
435             if (!TEST_ptr(resume_server_ctx)
436                     || !TEST_ptr(resume_client_ctx))
437                 goto err;
438         }
439     }
440 #endif
441     if (test_ctx->method == SSL_TEST_METHOD_TLS) {
442         server_ctx = SSL_CTX_new_with_libctx(libctx, NULL, TLS_server_method());
443         if (!TEST_true(SSL_CTX_set_max_proto_version(server_ctx, 0)))
444             goto err;
445         /* SNI on resumption isn't supported/tested yet. */
446         if (test_ctx->extra.server.servername_callback !=
447             SSL_TEST_SERVERNAME_CB_NONE) {
448             if (!TEST_ptr(server2_ctx =
449                             SSL_CTX_new_with_libctx(libctx, NULL,
450                                                     TLS_server_method())))
451                 goto err;
452             if (!TEST_true(SSL_CTX_set_max_proto_version(server2_ctx, 0)))
453                 goto err;
454         }
455         client_ctx = SSL_CTX_new_with_libctx(libctx, NULL, TLS_client_method());
456         if (!TEST_true(SSL_CTX_set_max_proto_version(client_ctx, 0)))
457             goto err;
458
459         if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
460             resume_server_ctx = SSL_CTX_new_with_libctx(libctx, NULL,
461                                                         TLS_server_method());
462             if (!TEST_true(SSL_CTX_set_max_proto_version(resume_server_ctx, 0)))
463                 goto err;
464             resume_client_ctx = SSL_CTX_new_with_libctx(libctx, NULL,
465                                                         TLS_client_method());
466             if (!TEST_true(SSL_CTX_set_max_proto_version(resume_client_ctx, 0)))
467                 goto err;
468             if (!TEST_ptr(resume_server_ctx)
469                     || !TEST_ptr(resume_client_ctx))
470                 goto err;
471         }
472     }
473
474 #ifdef OPENSSL_NO_AUTOLOAD_CONFIG
475     if (!TEST_true(OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL)))
476         goto err;
477 #endif
478
479     if (!TEST_ptr(server_ctx)
480             || !TEST_ptr(client_ctx)
481             || !TEST_int_gt(CONF_modules_load(conf, test_app, 0),  0))
482         goto err;
483
484     if (!SSL_CTX_config(server_ctx, "server")
485         || !SSL_CTX_config(client_ctx, "client")) {
486         goto err;
487     }
488
489     if (server2_ctx != NULL && !SSL_CTX_config(server2_ctx, "server2"))
490         goto err;
491     if (resume_server_ctx != NULL
492         && !SSL_CTX_config(resume_server_ctx, "resume-server"))
493         goto err;
494     if (resume_client_ctx != NULL
495         && !SSL_CTX_config(resume_client_ctx, "resume-client"))
496         goto err;
497
498     result = do_handshake(server_ctx, server2_ctx, client_ctx,
499                           resume_server_ctx, resume_client_ctx, test_ctx);
500
501     if (result != NULL)
502         ret = check_test(result, test_ctx);
503
504 err:
505     CONF_modules_unload(0);
506     SSL_CTX_free(server_ctx);
507     SSL_CTX_free(server2_ctx);
508     SSL_CTX_free(client_ctx);
509     SSL_CTX_free(resume_server_ctx);
510     SSL_CTX_free(resume_client_ctx);
511     SSL_TEST_CTX_free(test_ctx);
512     HANDSHAKE_RESULT_free(result);
513     return ret;
514 }
515
516 OPT_TEST_DECLARE_USAGE("conf_file modulename [fips_conf_file]\n")
517
518 int setup_tests(void)
519 {
520     long num_tests;
521     const char *modulename;
522
523     if (!test_skip_common_options()) {
524         TEST_error("Error parsing test options\n");
525         return 0;
526     }
527
528     if (!TEST_ptr(conf = NCONF_new(NULL))
529             /* argv[1] should point to the test conf file */
530             || !TEST_int_gt(NCONF_load(conf, test_get_argument(0), NULL), 0)
531             || !TEST_int_ne(NCONF_get_number_e(conf, NULL, "num_tests",
532                                                &num_tests), 0))
533         return 0;
534
535     if (!TEST_ptr(modulename = test_get_argument(1)))
536         return 0;
537
538     if (strcmp(modulename, "none") != 0) {
539         const char *configfile = test_get_argument(2);
540
541         defctxnull = OSSL_PROVIDER_load(NULL, "null");
542         libctx = OPENSSL_CTX_new();
543         if (!TEST_ptr(libctx))
544             return 0;
545
546         if (configfile != NULL
547                 && !TEST_true(OPENSSL_CTX_load_config(libctx, configfile)))
548             return 0;
549
550         thisprov = OSSL_PROVIDER_load(libctx, modulename);
551         if (!TEST_ptr(thisprov))
552             return 0;
553     }
554
555     ADD_ALL_TESTS(test_handshake, (int)num_tests);
556     return 1;
557 }
558
559 void cleanup_tests(void)
560 {
561     NCONF_free(conf);
562     OSSL_PROVIDER_unload(defctxnull);
563     OSSL_PROVIDER_unload(thisprov);
564     OPENSSL_CTX_free(libctx);
565 }