Send and Receive a TLSv1.3 format ServerHello
[oweals/openssl.git] / test / recipes / 70-test_tls13messages.t
1 #! /usr/bin/env perl
2 # Copyright 2015-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 use strict;
10 use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
11 use OpenSSL::Test::Utils;
12 use File::Temp qw(tempfile);
13 use TLSProxy::Proxy;
14 my $test_name = "test_tls13messages";
15 setup($test_name);
16
17 plan skip_all => "TLSProxy isn't usable on $^O"
18     if $^O =~ /^(VMS|MSWin32)$/;
19
20 plan skip_all => "$test_name needs the dynamic engine feature enabled"
21     if disabled("engine") || disabled("dynamic-engine");
22
23 plan skip_all => "$test_name needs the sock feature enabled"
24     if disabled("sock");
25
26 plan skip_all => "$test_name needs TLSv1.3 enabled"
27     if disabled("tls1_3");
28
29 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
30
31 use constant {
32     DEFAULT_HANDSHAKE => 1,
33     OCSP_HANDSHAKE => 2,
34     RESUME_HANDSHAKE => 4,
35     CLIENT_AUTH_HANDSHAKE => 8,
36     ALL_HANDSHAKES => 15
37 };
38
39 my @handmessages = (
40     [TLSProxy::Message::MT_CLIENT_HELLO, ALL_HANDSHAKES],
41     [TLSProxy::Message::MT_SERVER_HELLO, ALL_HANDSHAKES],
42     [TLSProxy::Message::MT_CERTIFICATE_REQUEST, CLIENT_AUTH_HANDSHAKE],
43     [TLSProxy::Message::MT_CERTIFICATE, ALL_HANDSHAKES & ~RESUME_HANDSHAKE],
44     [TLSProxy::Message::MT_CERTIFICATE_STATUS, OCSP_HANDSHAKE],
45     [TLSProxy::Message::MT_FINISHED, ALL_HANDSHAKES],
46     [TLSProxy::Message::MT_CERTIFICATE, CLIENT_AUTH_HANDSHAKE],
47     [TLSProxy::Message::MT_CERTIFICATE_VERIFY, CLIENT_AUTH_HANDSHAKE],
48     [TLSProxy::Message::MT_FINISHED, ALL_HANDSHAKES],
49     [0, 0]
50 );
51
52 my $proxy = TLSProxy::Proxy->new(
53     undef,
54     cmdstr(app(["openssl"]), display => 1),
55     srctop_file("apps", "server.pem"),
56     (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
57 );
58
59 sub checkmessages($$);
60
61 #Test 1: Check we get all the right messages for a default handshake
62 (undef, my $session) = tempfile();
63 #$proxy->serverconnects(2);
64 $proxy->clientflags("-sess_out ".$session);
65 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
66 plan tests => 3;
67 checkmessages(DEFAULT_HANDSHAKE, "Default handshake test");
68
69 #TODO(TLS1.3): Test temporarily disabled until we implement TLS1.3 resumption
70 #Test 2: Resumption handshake
71 #$proxy->clearClient();
72 #$proxy->clientflags("-sess_in ".$session);
73 #$proxy->clientstart();
74 #checkmessages(RESUME_HANDSHAKE, "Resumption handshake test");
75 unlink $session;
76
77 #Test 3: A default handshake, but with a CertificateStatus message
78 #TODO(TLS1.3): TLS1.3 doesn't actually have CertificateStatus messages. This is
79 #a temporary test until such time as we do proper TLS1.3 style certificate
80 #status
81 $proxy->clear();
82 $proxy->clientflags("-status");
83 $proxy->serverflags("-status_file "
84                     .srctop_file("test", "recipes", "ocsp-response.der"));
85 $proxy->start();
86 checkmessages(OCSP_HANDSHAKE, "OCSP handshake test");
87
88 #Test 4: A client auth handshake
89 $proxy->clear();
90 $proxy->clientflags("-cert ".srctop_file("apps", "server.pem"));
91 $proxy->serverflags("-Verify 5");
92 $proxy->start();
93 checkmessages(CLIENT_AUTH_HANDSHAKE, "Client auth handshake test");
94
95 sub checkmessages($$)
96 {
97     my ($handtype, $testname) = @_;
98
99     subtest $testname => sub {
100         my $loop = 0;
101         my $numtests;
102
103         #First count the number of tests
104         for ($numtests = 1; $handmessages[$loop][1] != 0; $loop++) {
105             $numtests++ if (($handmessages[$loop][1] & $handtype) != 0);
106         }
107
108         plan tests => $numtests;
109
110         $loop = 0;
111         foreach my $message (@{$proxy->message_list}) {
112             for (; $handmessages[$loop][1] != 0
113                    && ($handmessages[$loop][1] & $handtype) == 0; $loop++) {
114                 next;
115             }
116             ok($handmessages[$loop][1] != 0
117                && $message->mt == $handmessages[$loop][0],
118                "Message type check. Got ".$message->mt
119                .", expected ".$handmessages[$loop][0]);
120             $loop++;
121         }
122         ok($handmessages[$loop][1] == 0, "All expected messages processed");
123     }
124 }