cb5700a269fecab1bbbff17027380b447fb8f134
[oweals/openssl.git] / test / recipes / 70-test_sslmessages.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 TLS enabled"
27     if alldisabled(available_protocols("tls"));
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     RENEG_HANDSHAKE => 16,
37
38     ALL_HANDSHAKES => 31
39 };
40
41 my @handmessages = (
42     [TLSProxy::Message::MT_CLIENT_HELLO, ALL_HANDSHAKES],
43     [TLSProxy::Message::MT_SERVER_HELLO, ALL_HANDSHAKES],
44     [TLSProxy::Message::MT_CERTIFICATE, ALL_HANDSHAKES & ~RESUME_HANDSHAKE],
45     [TLSProxy::Message::MT_CERTIFICATE_STATUS, OCSP_HANDSHAKE],
46     #ServerKeyExchange handshakes not currently supported by TLSProxy
47     [TLSProxy::Message::MT_CERTIFICATE_REQUEST, CLIENT_AUTH_HANDSHAKE],
48     [TLSProxy::Message::MT_SERVER_HELLO_DONE, ALL_HANDSHAKES & ~RESUME_HANDSHAKE],
49     [TLSProxy::Message::MT_CERTIFICATE, CLIENT_AUTH_HANDSHAKE],
50     [TLSProxy::Message::MT_CLIENT_KEY_EXCHANGE, ALL_HANDSHAKES & ~RESUME_HANDSHAKE],
51     [TLSProxy::Message::MT_CERTIFICATE_VERIFY, CLIENT_AUTH_HANDSHAKE],
52     [TLSProxy::Message::MT_FINISHED, ALL_HANDSHAKES],
53     [TLSProxy::Message::MT_NEW_SESSION_TICKET, ALL_HANDSHAKES & ~RESUME_HANDSHAKE],
54     [TLSProxy::Message::MT_FINISHED, ALL_HANDSHAKES],
55     [TLSProxy::Message::MT_CLIENT_HELLO, RENEG_HANDSHAKE],
56     [TLSProxy::Message::MT_SERVER_HELLO, RENEG_HANDSHAKE],
57     [TLSProxy::Message::MT_CERTIFICATE, RENEG_HANDSHAKE],
58     [TLSProxy::Message::MT_SERVER_HELLO_DONE, RENEG_HANDSHAKE],
59     [TLSProxy::Message::MT_CLIENT_KEY_EXCHANGE, RENEG_HANDSHAKE],
60     [TLSProxy::Message::MT_FINISHED, RENEG_HANDSHAKE],
61     [TLSProxy::Message::MT_NEW_SESSION_TICKET, RENEG_HANDSHAKE],
62     [TLSProxy::Message::MT_FINISHED, RENEG_HANDSHAKE],
63     [0, 0]
64 );
65
66 my $proxy = TLSProxy::Proxy->new(
67     undef,
68     cmdstr(app(["openssl"]), display => 1),
69     srctop_file("apps", "server.pem"),
70     (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
71 );
72
73 sub checkmessages($$);
74
75 #Test 1: Check we get all the right messages for a default handshake
76 (undef, my $session) = tempfile();
77 $proxy->serverconnects(2);
78 $proxy->clientflags("-no_tls1_3 -sess_out ".$session);
79 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
80 plan tests => 5;
81 checkmessages(DEFAULT_HANDSHAKE, "Default handshake test");
82
83 #Test 2: Resumption handshake
84 $proxy->clearClient();
85 $proxy->clientflags("-no_tls1_3 -sess_in ".$session);
86 $proxy->clientstart();
87 checkmessages(RESUME_HANDSHAKE, "Resumption handshake test");
88 unlink $session;
89
90 #Test 3: A default handshake, but with a CertificateStatus message
91 #
92 #TODO: PR #1932 adds the "-status_file" option below. Once added we can enable
93 #this test
94 #
95 #$proxy->clear();
96 #$proxy->clientflags("-no_tls1_3 -status");
97 #$proxy->serverflags("-no_ticket -status_file "
98 #                    .srctop_file("test", "recipes", "ocsp-response.der"));
99 #$proxy->start();
100 #checkmessages(OCSP_HANDSHAKE, "OCSP handshake test");
101
102 #Test 4: A client auth handshake
103 $proxy->clear();
104 $proxy->clientflags("-no_tls1_3 -cert ".srctop_file("apps", "server.pem"));
105 $proxy->serverflags("-Verify 5");
106 $proxy->start();
107 checkmessages(CLIENT_AUTH_HANDSHAKE, "Client auth handshake test");
108
109 #Test 5: A handshake with a renegotiation
110 $proxy->clear();
111 $proxy->clientflags("-no_tls1_3");
112 $proxy->reneg(1);
113 $proxy->start();
114 checkmessages(RENEG_HANDSHAKE, "Rengotiation handshake test");
115
116 #Test 6: A handshake with a renegotiation and client auth
117 $proxy->clear();
118 $proxy->clientflags("-no_tls1_3 -cert ".srctop_file("apps", "server.pem"));
119 $proxy->serverflags("-Verify 5");
120 $proxy->reneg(1);
121 $proxy->start();
122 checkmessages(RENEG_HANDSHAKE | CLIENT_AUTH_HANDSHAKE,
123               "Renogitation and client auth handshake test");
124
125 sub checkmessages($$)
126 {
127     my ($handtype, $testname) = @_;
128
129     subtest $testname => sub {
130         my $loop = 0;
131         my $numtests;
132
133         #First count the number of tests
134         for ($numtests = 0; $handmessages[$loop][1] != 0; $loop++) {
135             $numtests++ if (($handmessages[$loop][1] & $handtype) != 0);
136         }
137
138         plan tests => $numtests;
139
140         my $nextmess = 0;
141         my $message = undef;
142         for ($loop = 0; $handmessages[$loop][1] != 0; $loop++) {
143             next if (($handmessages[$loop][1] & $handtype) == 0);
144             if (scalar @{$proxy->message_list} > $nextmess) {
145                 $message = ${$proxy->message_list}[$nextmess];
146                 $nextmess++;
147             } else {
148                 $message = undef;
149             }
150             if (!defined $message) {
151                 fail("Message type check. Got nothing, expected "
152                      .$handmessages[$loop][0]);
153             } else {
154                 ok($message->mt == $handmessages[$loop][0],
155                    "Message type check. Got ".$message->mt
156                    .", expected ".$handmessages[$loop][0]);
157             }
158         }
159     }
160 }