51fee42aafe6f4cf06c6ef450673d007bf1f2793
[openssl.git] / test / recipes / 70-test_sslsigalgs.t
1 #! /usr/bin/env perl
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 use strict;
10 use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
11 use OpenSSL::Test::Utils;
12 use TLSProxy::Proxy;
13
14 my $test_name = "test_sslsigalgs";
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 TLS1.2 or TLS1.3 enabled"
27     if disabled("tls1_2") && disabled("tls1_3");
28
29 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
30 my $proxy = TLSProxy::Proxy->new(
31     undef,
32     cmdstr(app(["openssl"]), display => 1),
33     srctop_file("apps", "server.pem"),
34     (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
35 );
36
37 use constant {
38     NO_SIG_ALGS_EXT => 0,
39     EMPTY_SIG_ALGS_EXT => 1,
40     NO_KNOWN_SIG_ALGS => 2,
41     NO_PSS_SIG_ALGS => 3,
42     PSS_ONLY_SIG_ALGS => 4
43 };
44
45 #Note: Throughout this test we override the default ciphersuites where TLSv1.2
46 #      is expected to ensure that a ServerKeyExchange message is sent that uses
47 #      the sigalgs
48
49 #Test 1: Default sig algs should succeed
50 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
51 plan tests => 15;
52 ok(TLSProxy::Message->success, "Default sigalgs");
53 my $testtype;
54
55 SKIP: {
56     skip "TLSv1.3 disabled", 5 if disabled("tls1_3");
57
58     $proxy->filter(\&sigalgs_filter);
59
60     #Test 2: Sending no sig algs extension in TLSv1.3 should fail
61     $proxy->clear();
62     $testtype = NO_SIG_ALGS_EXT;
63     $proxy->start();
64     ok(TLSProxy::Message->fail, "No TLSv1.3 sigalgs");
65
66     #Test 3: Sending an empty sig algs extension in TLSv1.3 should fail
67     $proxy->clear();
68     $testtype = EMPTY_SIG_ALGS_EXT;
69     $proxy->start();
70     ok(TLSProxy::Message->fail, "Empty TLSv1.3 sigalgs");
71
72     #Test 4: Sending a list with no recognised sig algs in TLSv1.3 should fail
73     $proxy->clear();
74     $testtype = NO_KNOWN_SIG_ALGS;
75     $proxy->start();
76     ok(TLSProxy::Message->fail, "No known TLSv1.3 sigalgs");
77
78     #Test 5: Sending a sig algs list without pss for an RSA cert in TLSv1.3
79     #        should fail
80     $proxy->clear();
81     $testtype = NO_PSS_SIG_ALGS;
82     $proxy->start();
83     ok(TLSProxy::Message->fail, "No PSS TLSv1.3 sigalgs");
84
85     #Test 6: Sending only TLSv1.3 PSS sig algs in TLSv1.3 should succeed
86     #TODO(TLS1.3): Do we need to verify the cert to make sure its a PSS only
87     #cert in this case?
88     $proxy->clear();
89     $testtype = PSS_ONLY_SIG_ALGS;
90     $proxy->start();
91     ok(TLSProxy::Message->success, "PSS only sigalgs in TLSv1.3");
92 }
93
94 SKIP: {
95     skip "TLSv1.3 or TLSv1.2 disabled", 2
96         if disabled("tls1_2") || disabled("tls1_3");
97
98     #Test 7: Sending a valid sig algs list but not including a sig type that
99     #        matches the certificate should fail in TLSv1.3. We need TLSv1.2
100     #        enabled for this test - otherwise the client will not attempt to
101     #        connect due to no TLSv1.3 ciphers being available.
102     #        TODO(TLS1.3): When proper TLSv1.3 certificate selection is working
103     #        we can move this test into the section above
104     $proxy->clear();
105     $proxy->clientflags("-sigalgs ECDSA+SHA256");
106     $proxy->filter(undef);
107     $proxy->start();
108     ok(TLSProxy::Message->fail, "No matching TLSv1.3 sigalgs");
109
110     #Test 8: Sending a full list of TLSv1.3 sig algs but negotiating TLSv1.2
111     #        should succeed
112     $proxy->clear();
113     $proxy->serverflags("-no_tls1_3");
114     $proxy->ciphers("ECDHE-RSA-AES128-SHA:TLS13-AES-128-GCM-SHA256");
115     $proxy->filter(undef);
116     $proxy->start();
117     ok(TLSProxy::Message->success, "TLSv1.3 client TLSv1.2 server");
118 }
119
120 SKIP: {
121     skip "TLSv1.2 disabled", 7 if disabled("tls1_2");
122
123     $proxy->filter(\&sigalgs_filter);
124
125     #Test 9: Sending no sig algs extension in TLSv1.2 should succeed
126     $proxy->clear();
127     $testtype = NO_SIG_ALGS_EXT;
128     $proxy->clientflags("-no_tls1_3");
129     $proxy->ciphers("ECDHE-RSA-AES128-SHA:TLS13-AES-128-GCM-SHA256");
130     $proxy->start();
131     ok(TLSProxy::Message->success, "No TLSv1.2 sigalgs");
132
133     #Test 10: Sending an empty sig algs extension in TLSv1.2 should fail
134     $proxy->clear();
135     $testtype = EMPTY_SIG_ALGS_EXT;
136     $proxy->clientflags("-no_tls1_3");
137     $proxy->ciphers("ECDHE-RSA-AES128-SHA:TLS13-AES-128-GCM-SHA256");
138     $proxy->start();
139     ok(TLSProxy::Message->fail, "Empty TLSv1.2 sigalgs");
140
141     #Test 11: Sending a list with no recognised sig algs in TLSv1.2 should fail
142     $proxy->clear();
143     $testtype = NO_KNOWN_SIG_ALGS;
144     $proxy->clientflags("-no_tls1_3");
145     $proxy->ciphers("ECDHE-RSA-AES128-SHA:TLS13-AES-128-GCM-SHA256");
146     $proxy->start();
147     ok(TLSProxy::Message->fail, "No known TLSv1.3 sigalgs");
148
149     #Test 12: Sending a sig algs list without pss for an RSA cert in TLSv1.2
150     #         should succeed
151     $proxy->clear();
152     $testtype = NO_PSS_SIG_ALGS;
153     $proxy->clientflags("-no_tls1_3");
154     $proxy->ciphers("ECDHE-RSA-AES128-SHA:TLS13-AES-128-GCM-SHA256");
155     $proxy->start();
156     ok(TLSProxy::Message->success, "No PSS TLSv1.2 sigalgs");
157
158     #Test 13: Sending only TLSv1.3 PSS sig algs in TLSv1.2 should succeed
159     $proxy->clear();
160     $testtype = PSS_ONLY_SIG_ALGS;
161     $proxy->serverflags("-no_tls1_3");
162     $proxy->ciphers("ECDHE-RSA-AES128-SHA:TLS13-AES-128-GCM-SHA256");
163     $proxy->start();
164     ok(TLSProxy::Message->success, "PSS only sigalgs in TLSv1.2");
165
166     #Test 14: Responding with a sig alg we did not send in TLSv1.2 should fail
167     #         We send rsa_pkcs1_sha256 and respond with rsa_pss_sha256
168     #         TODO(TLS1.3): Add a similar test to the TLSv1.3 section above
169     #         when we have an API capable of configuring the TLSv1.3 sig algs
170     $proxy->clear();
171     $testtype = PSS_ONLY_SIG_ALGS;
172     $proxy->clientflags("-no_tls1_3 -sigalgs RSA+SHA256");
173     $proxy->ciphers("ECDHE-RSA-AES128-SHA:TLS13-AES-128-GCM-SHA256");
174     $proxy->start();
175     ok(TLSProxy::Message->fail, "Sigalg we did not send in TLSv1.2");
176
177     #Test 15: Sending a valid sig algs list but not including a sig type that
178     #         matches the certificate should fail in TLSv1.2
179     $proxy->clear();
180     $proxy->clientflags("-no_tls1_3 -sigalgs ECDSA+SHA256");
181     $proxy->ciphers("ECDHE-RSA-AES128-SHA:TLS13-AES-128-GCM-SHA256");
182     $proxy->filter(undef);
183     $proxy->start();
184     ok(TLSProxy::Message->fail, "No matching TLSv1.2 sigalgs");
185     $proxy->filter(\&sigalgs_filter);
186 }
187
188
189
190 sub sigalgs_filter
191 {
192     my $proxy = shift;
193
194     # We're only interested in the initial ClientHello
195     if ($proxy->flight != 0) {
196         return;
197     }
198
199     foreach my $message (@{$proxy->message_list}) {
200         if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
201             if ($testtype == NO_SIG_ALGS_EXT) {
202                 $message->delete_extension(TLSProxy::Message::EXT_SIG_ALGS);
203             } else {
204                 my $sigalg;
205                 if ($testtype == EMPTY_SIG_ALGS_EXT) {
206                     $sigalg = pack "C2", 0x00, 0x00;
207                 } elsif ($testtype == NO_KNOWN_SIG_ALGS) {
208                     $sigalg = pack "C4", 0x00, 0x02, 0xff, 0xff;
209                 } elsif ($testtype == NO_PSS_SIG_ALGS) {
210                     #No PSS sig algs - just send rsa_pkcs1_sha256
211                     $sigalg = pack "C4", 0x00, 0x02, 0x04, 0x01;
212                 } else {
213                     #PSS sig algs only - just send rsa_pss_sha256
214                     $sigalg = pack "C4", 0x00, 0x02, 0x08, 0x04;
215                 }
216                 $message->set_extension(TLSProxy::Message::EXT_SIG_ALGS, $sigalg);
217             }
218
219             $message->repack();
220         }
221     }
222 }