| 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::ALPN; |
| 41 |
use strict; |
| 42 |
use warnings; |
| 43 |
use Cwd qw(abs_path); |
| 44 |
use Data::Dumper; |
| 45 |
use HTTP::Tiny; |
| 46 |
use IO::Socket::SSL; |
| 47 |
use XML::Spice; |
| 48 |
|
| 49 |
use lib '.'; |
| 50 |
use base qw(Cassandane::Cyrus::TestCase); |
| 51 |
use Cassandane::Util::Log; |
| 52 |
|
| 53 |
# When ALPN negotiation fails, depending on the openssl version, we might get |
| 54 |
# a sensible error message, or an opaque reference to "1120". |
| 55 |
# https://github.com/openssl/openssl/issues/24300 |
| 56 |
my $alpn_fail_pattern = qr{(?: tlsv1\salert\sno\sapplication\sprotocol |
| 57 |
| ssl3_read_bytes:reason\(1120\) |
| 58 |
| SSL\sroutines::reason\(1120\) |
| 59 |
)}x; |
| 60 |
|
| 61 |
sub new |
| 62 |
{ |
| 63 |
my $class = shift; |
| 64 |
|
| 65 |
my $config = Cassandane::Config->default()->clone(); |
| 66 |
$config->set(tls_server_cert => '@basedir@/conf/certs/cert.pem', |
| 67 |
tls_server_key => '@basedir@/conf/certs/key.pem', |
| 68 |
allowstarttls => 'on', |
| 69 |
httpmodules => 'caldav'); |
| 70 |
|
| 71 |
my $self = $class->SUPER::new({ |
| 72 |
config => $config, |
| 73 |
install_certificates => 1, |
| 74 |
services => [ 'imap', 'imaps' ], |
| 75 |
}, @_); |
| 76 |
|
| 77 |
$self->needs('dependency', 'openssl'); |
| 78 |
$self->needs('dependency', 'openssl_alpn'); |
| 79 |
|
| 80 |
return $self; |
| 81 |
} |
| 82 |
|
| 83 |
sub set_up |
| 84 |
{ |
| 85 |
my ($self) = @_; |
| 86 |
$self->SUPER::set_up(); |
| 87 |
} |
| 88 |
|
| 89 |
sub tear_down |
| 90 |
{ |
| 91 |
my ($self) = @_; |
| 92 |
$self->SUPER::tear_down(); |
| 93 |
} |
| 94 |
|
| 95 |
sub do_imap_starttls |
| 96 |
{ |
| 97 |
my ($self, $talk, $alpn_map) = @_; |
| 98 |
my $ca_file = abs_path("data/certs/cacert.pem"); |
| 99 |
|
| 100 |
$talk->_imap_cmd('starttls', 0, 'starttls'); |
| 101 |
|
| 102 |
die $talk->get_last_error() |
| 103 |
if $talk->get_last_completion_response() ne 'ok'; |
| 104 |
|
| 105 |
IO::Socket::SSL->start_SSL($talk->{Socket}, |
| 106 |
SSL_ca_file => $ca_file, |
| 107 |
SSL_verifycn_scheme => 'none', |
| 108 |
SSL_alpn_protocols => $alpn_map, |
| 109 |
); |
| 110 |
|
| 111 |
if (ref $talk->{Socket} ne 'IO::Socket::SSL') { |
| 112 |
# TLS negotiation failed! |
| 113 |
eval { |
| 114 |
# connection will be dropped by server; make sure $talk notices |
| 115 |
local $SIG{__DIE__}; |
| 116 |
$talk->logout() if $talk->state() > Mail::IMAPTalk::Unconnected; |
| 117 |
}; |
| 118 |
die "TLS negotiation failed: $SSL_ERROR"; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
# check if we selected the expected ALPN protocol. |
| 123 |
# if the underlying IO::Socket::SSL object isn't accessible, pass undef |
| 124 |
# and this check will examine logs instead |
| 125 |
sub assert_alpn_protocol |
| 126 |
{ |
| 127 |
my ($self, $socket, $protocol) = @_; |
| 128 |
|
| 129 |
if ($socket) { |
| 130 |
if ($protocol) { |
| 131 |
$self->assert_str_equals($protocol, $socket->alpn_selected()); |
| 132 |
} |
| 133 |
else { |
| 134 |
$self->assert_null($socket->alpn_selected()); |
| 135 |
} |
| 136 |
} |
| 137 |
else { |
| 138 |
return if !$self->{instance}->{have_syslog_replacement}; |
| 139 |
|
| 140 |
my @lines = $self->{instance}->getsyslog(qr{starttls: \S+ with cipher}); |
| 141 |
$self->assert_num_equals(1, scalar @lines); |
| 142 |
|
| 143 |
if ($protocol) { |
| 144 |
$self->assert_matches(qr{; application protocol = $protocol}, |
| 145 |
$lines[0]); |
| 146 |
} |
| 147 |
else { |
| 148 |
$self->assert_does_not_match(qr{; application protocol =}, |
| 149 |
$lines[0]); |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
sub test_imap_none |
| 155 |
{ |
| 156 |
my ($self) = @_; |
| 157 |
|
| 158 |
# get a pristine connection |
| 159 |
$self->{store}->disconnect(); |
| 160 |
my $talk = $self->{store}->get_client(NoLogin => 1); |
| 161 |
|
| 162 |
$self->do_imap_starttls($talk, undef); |
| 163 |
|
| 164 |
$talk->login('cassandane', 'secret'); |
| 165 |
$self->assert_str_equals('ok', $talk->get_last_completion_response()); |
| 166 |
|
| 167 |
$talk->select("INBOX"); |
| 168 |
$self->assert_str_equals('ok', $talk->get_last_completion_response()); |
| 169 |
|
| 170 |
$self->assert_alpn_protocol($talk->{Socket}, undef); |
| 171 |
} |
| 172 |
|
| 173 |
sub test_imap_good |
| 174 |
{ |
| 175 |
my ($self) = @_; |
| 176 |
|
| 177 |
# get a pristine connection |
| 178 |
$self->{store}->disconnect(); |
| 179 |
my $talk = $self->{store}->get_client(NoLogin => 1); |
| 180 |
|
| 181 |
$self->do_imap_starttls($talk, [ 'imap' ]); |
| 182 |
|
| 183 |
$talk->login('cassandane', 'secret'); |
| 184 |
$self->assert_str_equals('ok', $talk->get_last_completion_response()); |
| 185 |
|
| 186 |
$talk->select("INBOX"); |
| 187 |
$self->assert_str_equals('ok', $talk->get_last_completion_response()); |
| 188 |
|
| 189 |
$self->assert_alpn_protocol($talk->{Socket}, 'imap'); |
| 190 |
} |
| 191 |
|
| 192 |
sub test_imap_bad |
| 193 |
{ |
| 194 |
my ($self) = @_; |
| 195 |
|
| 196 |
# get a pristine connection |
| 197 |
$self->{store}->disconnect(); |
| 198 |
my $talk = $self->{store}->get_client(NoLogin => 1); |
| 199 |
|
| 200 |
eval { |
| 201 |
$self->do_imap_starttls($talk, [ 'bogus' ]); |
| 202 |
}; |
| 203 |
|
| 204 |
my $e = $@; |
| 205 |
$self->assert_not_null($e); |
| 206 |
$self->assert_matches($alpn_fail_pattern, $e); |
| 207 |
$self->assert_num_equals(Mail::IMAPTalk::Unconnected, $talk->state()); |
| 208 |
} |
| 209 |
|
| 210 |
sub test_imaps_none |
| 211 |
{ |
| 212 |
my ($self) = @_; |
| 213 |
|
| 214 |
my $imaps = $self->{instance}->get_service('imaps'); |
| 215 |
my $store = $imaps->create_store(username => 'cassandane'); |
| 216 |
my $talk = $store->get_client(OverrideALPN => undef); |
| 217 |
|
| 218 |
$talk->select("INBOX"); |
| 219 |
$self->assert_str_equals('ok', $talk->get_last_completion_response()); |
| 220 |
|
| 221 |
$self->assert_alpn_protocol($talk->{Socket}, undef); |
| 222 |
} |
| 223 |
|
| 224 |
sub test_imaps_good |
| 225 |
{ |
| 226 |
my ($self) = @_; |
| 227 |
|
| 228 |
my $imaps = $self->{instance}->get_service('imaps'); |
| 229 |
my $store = $imaps->create_store(username => 'cassandane'); |
| 230 |
my $talk = $store->get_client(); # correct ALPN map is the default |
| 231 |
|
| 232 |
$talk->select("INBOX"); |
| 233 |
$self->assert_str_equals('ok', $talk->get_last_completion_response()); |
| 234 |
|
| 235 |
$self->assert_alpn_protocol($talk->{Socket}, 'imap'); |
| 236 |
} |
| 237 |
|
| 238 |
sub test_imaps_bad |
| 239 |
{ |
| 240 |
my ($self) = @_; |
| 241 |
|
| 242 |
my $imaps = $self->{instance}->get_service('imaps'); |
| 243 |
my $store = $imaps->create_store(username => 'cassandane'); |
| 244 |
my $talk; |
| 245 |
|
| 246 |
eval { |
| 247 |
$talk = $store->get_client(OverrideALPN => [ 'bogus' ]); |
| 248 |
}; |
| 249 |
my $e = $@; |
| 250 |
|
| 251 |
$self->assert_not_null($e); |
| 252 |
$self->assert_matches($alpn_fail_pattern, $e); |
| 253 |
} |
| 254 |
|
| 255 |
sub do_https_request |
| 256 |
{ |
| 257 |
my ($self, $service, $alpn_map) = @_; |
| 258 |
|
| 259 |
my $ca_file = abs_path("data/certs/cacert.pem"); |
| 260 |
|
| 261 |
my $client = HTTP::Tiny->new(verify_SSL => 1, |
| 262 |
SSL_options => { |
| 263 |
SSL_ca_file => $ca_file, |
| 264 |
SSL_verifycn_scheme => 'none', |
| 265 |
SSL_alpn_protocols => $alpn_map, |
| 266 |
}); |
| 267 |
|
| 268 |
my $url = sprintf("https://%s:%s@%s:%s/dav/calendars", |
| 269 |
'cassandane', 'secret', |
| 270 |
$service->host(), $service->port()); |
| 271 |
|
| 272 |
my $content = x('D:propfind', { 'xmlns:D' => 'DAV:' }, |
| 273 |
x('D:prop', |
| 274 |
x('D:current-user-principal'))); |
| 275 |
|
| 276 |
my $response = $client->request('PROPFIND', $url, { |
| 277 |
headers => { |
| 278 |
'content-type' => 'application/xml; charset=utf8', |
| 279 |
}, |
| 280 |
content => "$content", # stringify the XML::Spice::Chunk |
| 281 |
}); |
| 282 |
|
| 283 |
return $response; |
| 284 |
} |
| 285 |
|
| 286 |
sub test_https_none |
| 287 |
:want_service_https :needs_component_httpd |
| 288 |
{ |
| 289 |
my ($self) = @_; |
| 290 |
|
| 291 |
# skip past setup logs |
| 292 |
$self->{instance}->getsyslog(); |
| 293 |
|
| 294 |
my $https = $self->{instance}->get_service('https'); |
| 295 |
my $alpn_map = undef; |
| 296 |
|
| 297 |
my $response = $self->do_https_request($https, $alpn_map); |
| 298 |
|
| 299 |
$self->assert_num_equals(1, $response->{success}); |
| 300 |
$self->assert_alpn_protocol(undef, undef); |
| 301 |
} |
| 302 |
|
| 303 |
sub test_https_http10 |
| 304 |
:want_service_https :needs_component_httpd |
| 305 |
{ |
| 306 |
my ($self) = @_; |
| 307 |
|
| 308 |
# skip past setup logs |
| 309 |
$self->{instance}->getsyslog(); |
| 310 |
|
| 311 |
my $https = $self->{instance}->get_service('https'); |
| 312 |
my $alpn_map = [ 'http/1.0' ]; |
| 313 |
|
| 314 |
my $response = $self->do_https_request($https, $alpn_map); |
| 315 |
|
| 316 |
$self->assert_num_equals(1, $response->{success}); |
| 317 |
$self->assert_alpn_protocol(undef, $alpn_map->[0]); |
| 318 |
} |
| 319 |
|
| 320 |
sub test_https_http11 |
| 321 |
:want_service_https :needs_component_httpd |
| 322 |
{ |
| 323 |
my ($self) = @_; |
| 324 |
|
| 325 |
# skip past setup logs |
| 326 |
$self->{instance}->getsyslog(); |
| 327 |
|
| 328 |
my $https = $self->{instance}->get_service('https'); |
| 329 |
my $alpn_map = [ 'http/1.1' ]; |
| 330 |
|
| 331 |
my $response = $self->do_https_request($https, $alpn_map); |
| 332 |
|
| 333 |
$self->assert_num_equals(1, $response->{success}); |
| 334 |
$self->assert_alpn_protocol(undef, $alpn_map->[0]); |
| 335 |
} |
| 336 |
|
| 337 |
sub test_https_multi |
| 338 |
:want_service_https :needs_component_httpd |
| 339 |
{ |
| 340 |
my ($self) = @_; |
| 341 |
|
| 342 |
# skip past setup logs |
| 343 |
$self->{instance}->getsyslog(); |
| 344 |
|
| 345 |
my $https = $self->{instance}->get_service('https'); |
| 346 |
my $alpn_map = [ 'http/1.1', 'http/1.0' ]; |
| 347 |
|
| 348 |
my $response = $self->do_https_request($https, $alpn_map); |
| 349 |
|
| 350 |
$self->assert_num_equals(1, $response->{success}); |
| 351 |
$self->assert_alpn_protocol(undef, $alpn_map->[0]); |
| 352 |
} |
| 353 |
|
| 354 |
sub test_https_h2 |
| 355 |
:want_service_https :needs_component_httpd :needs_dependency_nghttp2 |
| 356 |
{ |
| 357 |
my ($self) = @_; |
| 358 |
|
| 359 |
# skip past setup logs |
| 360 |
$self->{instance}->getsyslog(); |
| 361 |
|
| 362 |
my $https = $self->{instance}->get_service('https'); |
| 363 |
my $alpn_map = [ 'h2' ]; |
| 364 |
|
| 365 |
my $response = $self->do_https_request($https, $alpn_map); |
| 366 |
|
| 367 |
# HTTP::Tiny can't speak h2, so the request itself will fail |
| 368 |
$self->assert_num_equals(0, $response->{success}); |
| 369 |
$self->assert_str_equals('Internal Exception', $response->{reason}); |
| 370 |
|
| 371 |
# but we can still examine the log to check the ALPN result |
| 372 |
$self->assert_alpn_protocol(undef, $alpn_map->[0]); |
| 373 |
} |
| 374 |
|
| 375 |
sub test_https_multi2 |
| 376 |
:want_service_https :needs_component_httpd :needs_dependency_nghttp2 |
| 377 |
{ |
| 378 |
my ($self) = @_; |
| 379 |
|
| 380 |
# skip past setup logs |
| 381 |
$self->{instance}->getsyslog(); |
| 382 |
|
| 383 |
my $https = $self->{instance}->get_service('https'); |
| 384 |
my $alpn_map = [ 'h2', 'http/1.1', 'http/1.0' ]; |
| 385 |
|
| 386 |
my $response = $self->do_https_request($https, $alpn_map); |
| 387 |
|
| 388 |
# HTTP::Tiny can't speak h2, so the request itself will fail |
| 389 |
$self->assert_num_equals(0, $response->{success}); |
| 390 |
$self->assert_str_equals('Internal Exception', $response->{reason}); |
| 391 |
|
| 392 |
# but we can still examine the log to check the ALPN result |
| 393 |
$self->assert_alpn_protocol(undef, $alpn_map->[0]); |
| 394 |
} |
| 395 |
|
| 396 |
sub test_https_bad |
| 397 |
:want_service_https :needs_component_httpd |
| 398 |
{ |
| 399 |
my ($self) = @_; |
| 400 |
|
| 401 |
# skip past setup logs |
| 402 |
$self->{instance}->getsyslog(); |
| 403 |
|
| 404 |
my $https = $self->{instance}->get_service('https'); |
| 405 |
my $alpn_map = [ 'bogus' ]; |
| 406 |
|
| 407 |
my $response = $self->do_https_request($https, $alpn_map); |
| 408 |
|
| 409 |
$self->assert_num_equals(0, $response->{success}); |
| 410 |
$self->assert_str_equals('Internal Exception', $response->{reason}); |
| 411 |
$self->assert_matches($alpn_fail_pattern, $response->{content}); |
| 412 |
} |
| 413 |
|
| 414 |
1; |