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