Debian Codegraph @

cassandane/Cassandane/Cyrus/StartTLS.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2011-2024 FastMail Pty Ltd. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in
14 # the documentation and/or other materials provided with the
15 # distribution.
16 #
17 # 3. The name "Fastmail Pty Ltd" must not be used to
18 # endorse or promote products derived from this software without
19 # prior written permission. For permission or any legal
20 # details, please contact
21 # FastMail Pty Ltd
22 # PO Box 234
23 # Collins St West 8007
24 # Victoria
25 # Australia
26 #
27 # 4. Redistributions of any form whatsoever must retain the following
28 # acknowledgment:
29 # "This product includes software developed by Fastmail Pty. Ltd."
30 #
31 # FASTMAIL PTY LTD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
32 # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
33 # EVENT SHALL OPERA SOFTWARE AUSTRALIA BE LIABLE FOR ANY SPECIAL, INDIRECT
34 # OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
35 # USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
36 # TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
37 # OF THIS SOFTWARE.
38 #
39
40 package Cassandane::Cyrus::StartTLS;
41 use strict;
42 use warnings;
43 use JSON::XS;
44 use Convert::Base64;
45 use Mail::JMAPTalk 0.13;
46 use Cwd qw(abs_path);
47 use Data::Dumper;
48
49 use lib '.';
50 use base qw(Cassandane::Cyrus::TestCase);
51 use Cassandane::Util::Log;
52 use Cassandane::Util::Socket;
53
54 sub new
55 {
56 my ($class, @args) = @_;
57
58 my $config = Cassandane::Config->default()->clone();
59 $config->set(conversations => 'yes',
60 httpmodules => 'jmap',
61 http_allowplaintext => 'off',
62 httpallowcompress => 'no');
63
64 my $self = $class->SUPER::new({
65 config => $config,
66 jmap => 1,
67 httpmurder => 1,
68 adminstore => 1,
69 services => [ 'imap', 'http' ]
70 }, @args);
71
72 $self->needs('component', 'jmap');
73 $self->needs('component', 'murder');
74 return $self;
75 }
76
77 sub set_up
78 {
79 my ($self) = @_;
80 $self->SUPER::set_up();
81 }
82
83 sub tear_down
84 {
85 my ($self) = @_;
86 $self->SUPER::tear_down();
87 }
88
89 sub test_imap_disabled
90 :TLS :needs_dependency_openssl
91 {
92 my ($self) = @_;
93
94 # get a pristine connection
95 $self->{store}->disconnect();
96 my $talk = $self->{store}->get_client(NoLogin => 1);
97
98 # STARTTLS should NOT be advertised
99 my $res = $talk->capability();
100 $self->assert_null($res->{starttls});
101
102 # STARTTLS should be unrecognized command
103 $talk->_imap_cmd('starttls', 0, 'starttls');
104 $self->assert_str_equals('bad', $talk->get_last_completion_response());
105 }
106
107 sub test_imap_enabled
108 :TLS :needs_dependency_openssl :NoStartInstances
109 {
110 my ($self) = @_;
111
112 $self->config_set(allowstarttls => 'on');
113
114 $self->_start_instances();
115
116 # get a pristine connection
117 $self->{store}->disconnect();
118 my $talk = $self->{store}->get_client(NoLogin => 1);
119
120 # STARTTLS should be advertised
121 my $res = $talk->capability();
122 $self->assert_not_null($res->{starttls});
123
124 # STARTTLS should succeed
125 $talk->_imap_cmd('starttls', 0, 'starttls');
126 $self->assert_str_equals('ok', $talk->get_last_completion_response());
127 my $ca_file = abs_path("data/certs/cacert.pem");
128 IO::Socket::SSL->start_SSL($talk->{Socket},
129 SSL_ca_file => $ca_file,
130 SSL_verifycn_scheme => 'none',
131 );
132 $self->assert_str_equals('IO::Socket::SSL', ref $talk->{Socket});
133 }
134
135 sub test_http_disabled
136 :TLS :needs_dependency_openssl
137 {
138 my ($self) = @_;
139
140 my $ca_file = abs_path("data/certs/cacert.pem");
141 my $http = HTTP::Tiny->new(
142 max_redirect => 0,
143 SSL_options => {
144 SSL_ca_file => $ca_file,
145 SSL_verifycn_scheme => 'none'
146 }
147 );
148
149 # frontend should NOT offer TLS upgrade and should redirect to https://
150 my $frontend_svc = $self->{frontend}->get_service("http");
151 my $frontend_host = $frontend_svc->host();
152 my $frontend_port = $frontend_svc->port();
153
154 my $scheme = "http";
155 my $host = "$frontend_host:$frontend_port";
156 my $hier_part = "//$host/jmap/";
157 my $url = "$scheme:$hier_part";
158
159 my $req = {
160 method => 'GET',
161 uri => $url,
162 headers => {
163 'Host' => $host,
164 'Connection' => 'Upgrade',
165 'Upgrade' => 'TLS/1.2'
166 },
167 content => '',
168 };
169
170 my $res = $http->request('GET', $url);
171 $self->assert_str_equals('301', $res->{status});
172 $self->assert_matches(qr/https:$hier_part/, $res->{headers}->{location});
173
174 # backend should NOT offer TLS upgrade and should redirect to https://
175 my $backend_svc = $self->{instance}->get_service("http");
176 my $backend_host = $backend_svc->host();
177 my $backend_port = $backend_svc->port();
178
179 $host = "$backend_host:$backend_port";
180 $hier_part = "//$host/jmap/";
181 $url = "$scheme:$hier_part";
182
183 $req = {
184 method => 'GET',
185 uri => $url,
186 headers => {
187 'Host' => $host,
188 'Connection' => 'Upgrade',
189 'Upgrade' => 'TLS/1.2'
190 },
191 content => '',
192 };
193
194 $res = $http->request('GET', $url);
195 $self->assert_str_equals('301', $res->{status});
196 $self->assert_matches(qr/https:$hier_part/, $res->{headers}->{location});
197 }
198
199 sub test_http_enabled
200 :TLS :needs_dependency_openssl :NoStartInstances
201 {
202 my ($self) = @_;
203
204 $self->config_set(allowstarttls => 'on');
205
206 $self->_start_instances();
207 $self->_setup_http_service_objects();
208
209 my $ca_file = abs_path("data/certs/cacert.pem");
210 my $http = HTTP::Tiny->new(
211 max_redirect => 0,
212 SSL_options => {
213 SSL_ca_file => $ca_file,
214 SSL_verifycn_scheme => 'none'
215 }
216 );
217
218 # frontend should NOT offer TLS upgrade and should redirect to https://
219 my $frontend_svc = $self->{frontend}->get_service("http");
220 my $frontend_host = $frontend_svc->host();
221 my $frontend_port = $frontend_svc->port();
222
223 my $scheme = "http";
224 my $host = "$frontend_host:$frontend_port";
225 my $hier_part = "//$host/jmap/";
226 my $url = "$scheme:$hier_part";
227
228 my $req = {
229 method => 'GET',
230 uri => $url,
231 headers => {
232 'Host' => $host,
233 'Connection' => 'Upgrade',
234 'Upgrade' => 'TLS/1.2'
235 },
236 content => '',
237 };
238
239 my $res = $http->request('GET', $url);
240 $self->assert_str_equals('301', $res->{status});
241 $self->assert_matches(qr/https:$hier_part/, $res->{headers}->{location});
242
243 # backend should offer TLS upgrade
244 my $backend_svc = $self->{instance}->get_service("http");
245 my $backend_host = $backend_svc->host();
246 my $backend_port = $backend_svc->port();
247
248 $host = "$backend_host:$backend_port";
249 $hier_part = "//$host/jmap/";
250 $url = "$scheme:$hier_part";
251
252 $req = {
253 method => 'GET',
254 uri => $url,
255 headers => {
256 'Host' => $host,
257 'Connection' => 'Upgrade',
258 'Upgrade' => 'TLS/1.2'
259 },
260 content => '',
261 };
262
263 $res = $http->request('GET', $url);
264 $self->assert_str_equals('426', $res->{status});
265
266 # TLS upgrade should succeed (and request authentication)
267 $http->{handle}->write_request($req);
268 $res = $http->{handle}->read_response_header;
269 $self->assert_str_equals('101', $res->{status});
270
271 $http->{handle}->start_ssl( $backend_host );
272 $res = $http->{handle}->read_response_header;
273 $self->assert_str_equals('401', $res->{status});
274 }
275
276 1;