SSL.pm

filter: Statementstmt 96.7% 151 / 156 Branchbran 77.9% 53 / 68 Conditioncond 67.8% 40 / 59 MC/DCmcdc 24.2% 8 / 33 Subroutinesub 96.5% 28 / 29 Podpod 0.0% 0 / 6 totaltotal 79.7% 280 / 351 CC 55 SCAR 18.7
CC
55
Cov
86%
SSL_upgrade
14.1
SSL_connect
11.7
_SSL_args
6.3
CRAP 6.5
↑ Summary SSLStream.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2010-2023 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::SSL 0.25;
7  
8 11 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 11 ▶use POSIX qw( EAGAIN EWOULDBLOCK );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 11 ▶use IO::Socket::SSL 2.003 qw( $SSL_ERROR SSL_WANT_READ SSL_WANT_WRITE ); # default_ca
Subroutine BEGIN: called CC=1 SCAR=0.0
16 # require >= 2.003 for bugfixes - see RT#125220
17  
18 11 ▶use Future 0.33; # ->catch_with_f
Subroutine BEGIN: called CC=1 SCAR=0.0
19 11 ▶use IO::Async::Handle 0.29;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 11 ▶use IO::Async::Loop 0.61; # new Listen API
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 =head1 NAME
23  
24 C<IO::Async::SSL> - use SSL/TLS with L<IO::Async>
25  
26 =head1 SYNOPSIS
27  
28 use IO::Async::Loop;
29 use IO::Async::SSL;
30  
31 my $loop = IO::Async::Loop->new();
32  
33 $loop->SSL_connect(
34 host => "www.example.com",
35 service => "https",
36  
37 on_stream => sub {
38 my ( $stream ) = @_;
39  
40 $stream->configure(
41 on_read => sub {
42 ...
43 },
44 );
45  
46 $loop->add( $stream );
47  
48 ...
49 },
50  
51 on_resolve_error => sub { print STDERR "Cannot resolve - $_[0]\n"; },
52 on_connect_error => sub { print STDERR "Cannot connect\n"; },
53 on_ssl_error => sub { print STDERR "Cannot negotiate SSL - $_[-1]\n"; },
54 );
55  
56 =head1 DESCRIPTION
57  
58 This module extends existing L<IO::Async> classes with extra methods to allow
59 the use of SSL or TLS-based connections using L<IO::Socket::SSL>. It does not
60 directly provide any methods or functions of its own.
61  
62 Primarily, it provides C<SSL_connect> and C<SSL_listen>, which yield
63 C<IO::Socket::SSL>-upgraded socket handles or L<IO::Async::Stream>
64 instances, and two forms of C<SSL_upgrade> to upgrade an existing TCP
65 connection to use SSL.
66  
67 As an additional convenience, if the C<SSL_verify_mode> and C<SSL_ca_*>
68 options are omitted, the module will attempt to provide them by querying the
69 result of L<IO::Socket::SSL>'s C<default_ca> function. Otherwise, the module
70 will print a warning and set C<SSL_VERIFY_NONE> instead.
71  
72 =cut
73  
74 11 my %SSL_ca_args = IO::Socket::SSL::default_ca();
75  
76 sub _SSL_args
77 33 ▶{
Subroutine _SSL_args: called CC=6 SCAR=18.4
78 33 my %args = @_;
79  
80 # SSL clients (i.e. non-server) require a verify mode
81 33 ▶ if( !$args{SSL_server} and !defined $args{SSL_verify_mode} and
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"SSL_server"} or defined $args{"SSL_verify_mode"} or defined $args{"SSL_ca_file"} or defined $args{"SSL_ca_path"}) 1 32 33
Condition88%8/9
$args{"SSL_server"} or defined $args{"SSL_verify_mode"} or defined $args{"SSL_ca_file"} or defined $args{"SSL_ca_path"}
l!l&&r!l&&!r
3201
$args{"SSL_server"} or defined $args{"SSL_verify_mode"} or defined $args{"SSL_ca_file"}
l!l&&r!l&&!r
3111
$args{"SSL_server"} or defined $args{"SSL_verify_mode"}
l!l&&r!l&&!r
16152
MC/DC75%3/4
$args{"SSL_server"} or defined $args{"SSL_verify_mode"} or defined $args{"SSL_ca_file"} or defined $args{"SSL_ca_path"}
$args{"SSL_server"} defined $args{"SSL_verify_mode"} defined $args{"SSL_ca_file"} defined $args{"SSL_ca_path"}
Truth table80%4/5
A or B or C or D
A= $args{"SSL_server"}B= defined $args{"SSL_verify_mode"}C= defined $args{"SSL_ca_file"}D= defined $args{"SSL_ca_path"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
82 !defined $args{SSL_ca_file} and !defined $args{SSL_ca_path} ) {
83 1 ▶ unless( %SSL_ca_args ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%SSL_ca_args) 0 1 1
84 0 carp "Unable to set SSL_VERIFY_PEER because IO::Socket::SSL::default_ca() gives nothing";
85 0 $SSL_ca_args{SSL_verify_mode} = IO::Socket::SSL::SSL_VERIFY_NONE();
86 }
87  
88 1 %args = ( %SSL_ca_args, %args );
89 }
90  
91 33 return %args;
92 }
93  
94 sub sslread
95 293 ▶{
Subroutine sslread: called CC=5 SCAR=16.3
Pod undocumented
96 293 my $stream = shift;
97 293 my ( $fh, undef, $len ) = @_;
98  
99 293 my $ret = $stream->_sysread( $fh, $_[1], $len );
100  
101 293 ▶ my $read_wants_write = !defined $ret &&
Condition66%6/9
!defined($ret) && ($! == 11 || $! == 11) && $IO::Async::SSL::SSL_ERROR == 108177010307280
!ll&&!rl&&r
278141
not defined $ret and $! == 11 || $! == 11
!ll&&!rl&&r
278015
$! == 11 or $! == 11
l!l&&r!l&&!r
1500
MC/DC50%2/4
!defined($ret) && ($! == 11 || $! == 11) && $IO::Async::SSL::SSL_ERROR == 108177010307280
not defined $ret $! == 11 $! == 11 $IO::Async::SSL::SSL_ERROR == 108177010307280
Truth table50%3/6
not(A) and B or C && D
A= not defined $retB= $! == 11C= $! == 11D= $IO::Async::SSL::SSL_ERROR == 108177010307280
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
102 ( $! == EAGAIN or $! == EWOULDBLOCK ) &&
103 $SSL_ERROR == SSL_WANT_WRITE;
104 293 $stream->want_writeready_for_read( $read_wants_write );
105  
106 # It's possible SSL_read took all the data out of the filehandle, thus
107 # making it not appear read-ready any more.
108 293 ▶ if( $fh->pending ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh->pending) 16 277 293
109 16 ▶ $stream->loop->later( sub { $stream->on_read_ready } );
Subroutine __ANON__: called CC=1 SCAR=0.0
110 }
111  
112 293 return $ret;
113 }
114  
115 sub sslwrite
116 263 ▶{
Subroutine sslwrite: called CC=5 SCAR=17.0
Pod undocumented
117 263 my $stream = shift;
118 263 my ( $fh, undef, $len ) = @_;
119  
120 # Placate RT98372
121 263 ▶ utf8::downgrade( $_[1] ) or
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($_[1]) or carp("Wide character in sslwrite") 263 0 263
122 carp "Wide character in sslwrite";
123  
124 263 my $ret = $stream->_syswrite( $fh, $_[1], $len );
125  
126 263 ▶ my $write_wants_read = !defined $ret &&
Condition55%5/9
!defined($ret) && ($! == 11 || $! == 11) && $IO::Async::SSL::SSL_ERROR == 108177010306944
!ll&&!rl&&r
26201
not defined $ret and $! == 11 || $! == 11
!ll&&!rl&&r
26201
$! == 11 or $! == 11
l!l&&r!l&&!r
100
MC/DC25%1/4
!defined($ret) && ($! == 11 || $! == 11) && $IO::Async::SSL::SSL_ERROR == 108177010306944
not defined $ret $! == 11 $! == 11 $IO::Async::SSL::SSL_ERROR == 108177010306944
Truth table33%2/6
not(A) and B or C && D
A= not defined $retB= $! == 11C= $! == 11D= $IO::Async::SSL::SSL_ERROR == 108177010306944
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
127 ( $! == EAGAIN or $! == EWOULDBLOCK ) &&
128 $SSL_ERROR == SSL_WANT_READ;
129 263 $stream->want_readready_for_write( $write_wants_read );
130 # If write wants read, there's no point waiting on writereadiness either
131 263 $stream->want_writeready_for_write( !$write_wants_read );
132  
133 263 return $ret;
134 }
135  
136 =head1 LOOP METHODS
137  
138 The following extra methods are added to L<IO::Async::Loop>.
139  
140 =cut
141  
142 =head2 SSL_upgrade
143  
144 ( $stream or $socket ) = $loop->SSL_upgrade( %params )->get;
145  
146 This method upgrades a given stream filehandle into an SSL-wrapped stream,
147 returning a future which will yield the given stream object or socket.
148  
149 Takes the following parameters:
150  
151 =over 8
152  
153 =item handle => IO::Async::Stream | IO
154  
155 The C<IO::Async::Stream> object containing the IO handle of an
156 already-established connection to act as the transport for SSL; or the plain
157 IO socket handle itself.
158  
159 If an C<IO::Async::Stream> is passed it will have the C<reader> and C<writer>
160 functions set on it suitable for SSL use, and will be returned as the result
161 from the future.
162  
163 If a plain socket handle is passed, that will be returned from the future
164 instead.
165  
166 =item SSL_server => BOOL
167  
168 If true, indicates this is the server side of the connection.
169  
170 =back
171  
172 In addition, any parameter whose name starts C<SSL_> will be passed to the
173 C<IO::Socket::SSL> constructor.
174  
175 The following legacy callback arguments are also supported, in case the
176 returned future is not used:
177  
178 =over 8
179  
180 =item on_upgraded => CODE
181  
182 A continuation that is invoked when the socket has been successfully upgraded
183 to SSL. It will be passed an instance of an C<IO::Socket::SSL>, which will
184 have appropriate SSL-compatible reader/writer functions attached.
185  
186 $on_upgraded->( $sslsocket )
187  
188 =item on_error => CODE
189  
190 A continuation that is invoked if C<IO::Socket::SSL> detects an error while
191 negotiating the upgrade.
192  
193 $on_error->( $! )
194  
195 =back
196  
197 =cut
198  
199 sub IO::Async::Loop::SSL_upgrade
200 22 ▶{
Subroutine SSL_upgrade: called CC=14 SCAR=26.5
Pod undocumented
201 22 my $loop = shift;
202 22 my %params = @_;
203  
204 22 my $f = $loop->new_future;
205  
206 22 ▶ $params{handle} or croak "Expected 'handle'";
Branch: 1 branch
BranchTrueFalseTotal
$params{"handle"} or croak("Expected 'handle'") 22 0 22
207  
208 22 my $stream;
209 my $socket;
210 22 ▶ if( $params{handle}->isa( "IO::Async::Stream" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'handle'}->isa('IO::Async::Stream')) { } 16 6 22
211 16 $stream = delete $params{handle};
212 16 $socket = $stream->read_handle;
213 }
214 else {
215 6 $socket = delete $params{handle};
216 }
217  
218 {
219 22 my $on_upgraded = delete $params{on_upgraded} or defined wantarray
220 22 ▶ or croak "Expected 'on_upgraded' or to return a Future";
Branch: 1 branch
BranchTrueFalseTotal
my $on_upgraded = delete $params{"on_upgraded"} or defined wantarray or croak("Expected 'on_upgraded' or to return a Future") 22 0 22
Condition66%2/3
my $on_upgraded = delete $params{"on_upgraded"} or defined wantarray
l!l&&r!l&&!r
6160
MC/DC0%0/2
my $on_upgraded = delete $params{"on_upgraded"} or defined wantarray
my $on_upgraded = delete $params{"on_upgraded"} defined wantarray
Truth table66%2/3
A or B
A= my $on_upgraded = delete $params{"on_upgraded"}B= defined wantarray
ABresult
1X1
011
000
221 my $on_error = delete $params{on_error} or defined wantarray
222 22 ▶ or croak "Expected 'on_error' or to return a Future";
Branch: 1 branch
BranchTrueFalseTotal
my $on_error = delete $params{"on_error"} or defined wantarray or croak("Expected 'on_error' or to return a Future") 22 0 22
Condition66%2/3
my $on_error = delete $params{"on_error"} or defined wantarray
l!l&&r!l&&!r
6160
MC/DC0%0/2
my $on_error = delete $params{"on_error"} or defined wantarray
my $on_error = delete $params{"on_error"} defined wantarray
Truth table66%2/3
A or B
A= my $on_error = delete $params{"on_error"}B= defined wantarray
ABresult
1X1
011
000
223  
224 22 ▶ $f->on_done( $on_upgraded ) if $on_upgraded;
Branch: 1 branch
BranchTrueFalseTotal
if $on_upgraded 6 16 22
225 22 ▶ $f->on_fail( $on_error ) if $on_error;
Branch: 1 branch
BranchTrueFalseTotal
if $on_error 6 16 22
226 }
227  
228 22 my %ssl_params = map { $_ => delete $params{$_} } grep m/^SSL_/, keys %params;
229  
230 eval {
231 $socket = IO::Socket::SSL->start_SSL( $socket, _SSL_args
232 SSL_startHandshake => 0,
233  
234 # Required to make IO::Socket::SSL not ->close before we have a chance to remove it from the loop
235 4 ▶ SSL_error_trap => sub { },
Subroutine __ANON__: called CC=1 SCAR=0.0
236  
237 22 ▶ %ssl_params,
Condition100%2/2
($socket = 'IO::Socket::SSL'->start_SSL($socket, _SSL_args('SSL_startHandshake', 0, 'SSL_error_trap', sub { } , %ssl_params))) || die('IO::Socket::SSL'->errstr)
l!l
211
MC/DC100%1/1
($socket = 'IO::Socket::SSL'->start_SSL($socket, _SSL_args('SSL_startHandshake', 0, 'SSL_error_trap', sub { } , %ssl_params))) || die('IO::Socket::SSL'->errstr)
($socket = 'IO::Socket::SSL'->start_SSL($socket, _SSL_args('SSL_startHandshake', 0, 'SSL_error_trap', sub { } , %ssl_params)))
Truth table100%2/2
A
A= ($socket = 'IO::Socket::SSL'->start_SSL($socket, _SSL_args('SSL_startHandshake', 0, 'SSL_error_trap', sub { } , %ssl_params)))
Aresult
11
00
238 ) or die IO::Socket::SSL->errstr;
239 22 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $socket = "IO::Socket::SSL"->start_SSL($socket, _SSL_args("SSL_startHandshake", 0, "SSL_error_trap", sub { } , %ssl_params)) or die "IO::Socket::SSL"->errstr } } or do { chomp(my $e = $@); return $f->fail($e, "ssl") } 21 1 22
240 1 chomp( my $e = $@ );
241 1 return $f->fail( $e, "ssl" );
242 };
243  
244 21 ▶ my $ready_method = $ssl_params{SSL_server} ? "accept_SSL" : "connect_SSL";
Branch: 1 branch
BranchTrueFalseTotal
$ssl_params{'SSL_server'} ? : 10 11 21
245  
246 48 ▶ my $ready = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
247 48 my ( $self ) = @_;
248 48 ▶ if( $socket->$ready_method ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($socket->$ready_method) 17 31 48
249 17 $loop->remove( $self );
250  
251 17 ▶ if( $stream ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stream) 15 2 17
252 15 $stream->configure(
253 handle => $socket,
254 reader => \&sslread,
255 writer => \&sslwrite,
256 );
257 }
258  
259 17 ▶ $f->done( $stream || $socket );
Condition66%2/3
$stream || $socket
l!l&&r!l&&!r
1520
MC/DC0%0/2
$stream || $socket
$stream $socket
Truth table66%2/3
A || B
A= $streamB= $socket
ABresult
1X1
011
000
260 17 return;
261 }
262  
263 31 ▶ if( $! != EAGAIN and $! != EWOULDBLOCK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! != 11 and $! != 11) 4 27 31
Condition66%2/3
$! != 11 and $! != 11
!ll&&!rl&&r
2704
MC/DC50%1/2
$! != 11 and $! != 11
$! != 11 $! != 11
Truth table66%2/3
A and B
A= $! != 11B= $! != 11
ABresult
0X0
100
111
264 4 my $errstr = IO::Socket::SSL::errstr();
265 4 $loop->remove( $self );
266 4 $f->fail( $errstr, "ssl" );
267 4 return;
268 }
269  
270 27 $self->want_readready ( $SSL_ERROR == SSL_WANT_READ );
271 27 $self->want_writeready( $SSL_ERROR == SSL_WANT_WRITE );
272 21 };
273  
274 # We're going to steal the IO handle from $stream, so we'll have to
275 # temporarily deconfigure it
276 21 ▶ $stream->configure( handle => undef ) if $stream;
Branch: 1 branch
BranchTrueFalseTotal
if $stream 16 5 21
277  
278 21 $loop->add( my $handle = IO::Async::Handle->new(
279 handle => $socket,
280 on_read_ready => $ready,
281 on_write_ready => $ready,
282 ) );
283  
284 21 $ready->( $handle );
285  
286 21 ▶ return $f if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 19 2 21
287  
288 # Caller is not going to keep hold of the Future, so we have to ensure it
289 # stays alive somehow
290 2 ▶ $f->on_ready( sub { undef $f } ); # intentional cycle
Subroutine __ANON__: called CC=1 SCAR=0.0
291 }
292  
293 =head2 SSL_connect
294  
295 $stream = $loop->SSL_connect( %params )->get;
296  
297 This method performs a non-blocking connection to a given address or set of
298 addresses, upgrades the socket to SSL, then yields a C<IO::Async::Stream>
299 object when the SSL handshake is complete.
300  
301 It takes all the same arguments as C<IO::Async::Loop::connect()>. Any argument
302 whose name starts C<SSL_> will be passed on to the L<IO::Socket::SSL>
303 constructor rather than the Loop's C<connect> method. It is not required to
304 pass the C<socktype> option, as SSL implies this will be C<stream>.
305  
306 This method can also upgrade an existing C<IO::Async::Stream> or subclass
307 instance given as the C<handle> argument, by setting the C<reader> and
308 C<writer> functions.
309  
310 =head2 SSL_connect (void)
311  
312 $loop->SSL_connect( %params,
313 on_connected => sub { ... },
314 on_stream => sub { ... },
315 );
316  
317 When not returning a future, this method also supports the C<on_connected> and
318 C<on_stream> continuations.
319  
320 In addition, the following arguments are then required:
321  
322 =over 8
323  
324 =item on_ssl_error => CODE
325  
326 A continuation that is invoked if C<IO::Socket::SSL> detects an SSL-based
327 error once the actual stream socket is connected.
328  
329 =back
330  
331 If the C<on_connected> continuation is used, the socket handle it yields will
332 be a C<IO::Socket::SSL>, which must be wrapped in C<IO::Async::SSLStream> to
333 be used by C<IO::Async>. The C<on_stream> continuation will already yield such
334 an instance.
335  
336 =cut
337  
338 sub IO::Async::Loop::SSL_connect
339 6 ▶{
Subroutine SSL_connect: called CC=11 SCAR=24.6
Pod undocumented
340 6 my $loop = shift;
341 6 my %params = @_;
342  
343 6 my %ssl_params = map { $_ => delete $params{$_} } grep m/^SSL_/, keys %params;
344  
345 6 my $on_done;
346 6 ▶ if( exists $params{on_connected} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $params{'on_connected'}) { } 1 5 6
elsif (exists $params{'on_stream'}) { } 1 4 5
347 1 my $on_connected = delete $params{on_connected};
348 0 ▶ $on_done = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
349 0 my ( $stream ) = @_;
350 0 $on_connected->( $stream->read_handle );
351 1 };
352 }
353 elsif( exists $params{on_stream} ) {
354 1 my $on_stream = delete $params{on_stream};
355 1 $on_done = $on_stream;
356 }
357 else {
358 4 ▶ croak "Expected 'on_connected' or 'on_stream' or to return a Future" unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 4 4
359 }
360  
361 my $on_ssl_error = delete $params{on_ssl_error} or defined wantarray or
362 6 ▶ croak "Expected 'on_ssl_error' or to return a Future";
Branch: 1 branch
BranchTrueFalseTotal
my $on_ssl_error = delete $params{"on_ssl_error"} or defined wantarray or croak("Expected 'on_ssl_error' or to return a Future") 6 0 6
Condition66%2/3
my $on_ssl_error = delete $params{"on_ssl_error"} or defined wantarray
l!l&&r!l&&!r
330
MC/DC0%0/2
my $on_ssl_error = delete $params{"on_ssl_error"} or defined wantarray
my $on_ssl_error = delete $params{"on_ssl_error"} defined wantarray
Truth table66%2/3
A or B
A= my $on_ssl_error = delete $params{"on_ssl_error"}B= defined wantarray
ABresult
1X1
011
000
363  
364 6 ▶ my $stream = delete $params{handle} || do {
Condition66%2/3
delete $params{'handle'} || do { require IO::Async::Stream; 'IO::Async::Stream'->new }
l!l&&r!l&&!r
330
MC/DC0%0/2
delete $params{'handle'} || do { require IO::Async::Stream; 'IO::Async::Stream'->new }
delete $params{'handle'} do { require IO::Async::Stream; 'IO::Async::Stream'->new }
Truth table66%2/3
A || B
A= delete $params{'handle'}B= do { require IO::Async::Stream; 'IO::Async::Stream'->new }
ABresult
1X1
011
000
365 3 require IO::Async::Stream;
366 3 IO::Async::Stream->new;
367 };
368  
369 6 ▶ $stream->isa( "IO::Async::Stream" ) or
Branch: 1 branch
BranchTrueFalseTotal
$stream->isa("IO::Async::Stream") or croak("Can only SSL_connect a handle instance of IO::Async::Stream") 6 0 6
370 croak "Can only SSL_connect a handle instance of IO::Async::Stream";
371  
372 # Don't ->connect with the handle yet, because we'll first have to use the
373 # socket to perform SSL_upgrade on. We don't want to confuse the loop by
374 # giving it the same fd twice.
375  
376 my $f = $loop->connect(
377 socktype => 'stream', # SSL over DGRAM or RAW makes no sense
378 %params,
379 6 ▶ )->then( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
380 6 my ( $socket ) = @_;
381  
382 6 $stream->configure( handle => $socket );
383  
384 6 $loop->SSL_upgrade(
385 _SSL_args( %ssl_params ),
386 handle => $stream,
387 )
388 6 });
389  
390 6 ▶ $f->on_done( $on_done ) if $on_done;
Branch: 1 branch
BranchTrueFalseTotal
if $on_done 2 4 6
391 1 ▶ $f->on_fail( sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
392 1 ▶ $on_ssl_error->( $_[0] ) if defined $_[1] and $_[1] eq "ssl";
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] and $_[1] eq "ssl" 1 0 1
Condition33%1/3
defined $_[1] and $_[1] eq "ssl"
!ll&&!rl&&r
001
MC/DC0%0/2
defined $_[1] and $_[1] eq "ssl"
defined $_[1] $_[1] eq "ssl"
Truth table33%1/3
A and B
A= defined $_[1]B= $_[1] eq "ssl"
ABresult
0X0
100
111
393 6 ▶ }) if $on_ssl_error;
Branch: 1 branch
BranchTrueFalseTotal
if $on_ssl_error 3 3 6
394  
395 6 ▶ return $f if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 4 2 6
396  
397 # Caller is not going to keep hold of the Future, so we have to ensure it
398 # stays alive somehow
399 2 ▶ $f->on_ready( sub { undef $f } ); # intentional cycle
Subroutine __ANON__: called CC=1 SCAR=0.0
400 }
401  
402 =head2 SSL_listen
403  
404 $loop->SSL_listen( %params )->get;
405  
406 This method sets up a listening socket using the addresses given, and will
407 invoke the callback each time a new connection is accepted on the socket and
408 the SSL handshake has been completed. This can be either the C<on_accept> or
409 C<on_stream> continuation; C<on_socket> is not supported.
410  
411 It takes all the same arguments as C<IO::Async::Loop::listen()>. Any argument
412 whose name starts C<SSL_> will be passed on to the L<IO::Socket::SSL>
413 constructor rather than the Loop's C<listen> method. It is not required to
414 pass the C<socktype> option, as SSL implies this will be C<stream>.
415  
416 In addition, the following arguments are rquired:
417  
418 =over 8
419  
420 =item on_ssl_error => CODE
421  
422 A continuation that is invoked if C<IO::Socket::SSL> detects an SSL-based
423 error once the actual stream socket is connected.
424  
425 =back
426  
427 The underlying L<IO::Socket::SSL> socket will also require the server key and
428 certificate for a server-mode socket. See its documentation for more details.
429  
430 If the C<on_accept> continuation is used, the socket handle it yields will be
431 a C<IO::Socket::SSL>, which must be wrapped in C<IO::Async::SSLStream> to be
432 used by C<IO::Async>. The C<on_stream> continuation will already yield such an
433 instance.
434  
435 =cut
436  
437 sub IO::Async::Loop::SSL_listen
438 5 ▶{
Subroutine SSL_listen: called CC=4 SCAR=14.0
Pod undocumented
439 5 my $loop = shift;
440 5 my %params = @_;
441  
442 5 my %ssl_params = map { $_ => delete $params{$_} } grep m/^SSL_/, keys %params;
443 my $on_ssl_error = delete $params{on_ssl_error} or defined wantarray
444 5 ▶ or croak "Expected 'on_ssl_error'";
Branch: 1 branch
BranchTrueFalseTotal
my $on_ssl_error = delete $params{"on_ssl_error"} or defined wantarray or croak("Expected 'on_ssl_error'") 5 0 5
Condition66%2/3
my $on_ssl_error = delete $params{"on_ssl_error"} or defined wantarray
l!l&&r!l&&!r
320
MC/DC0%0/2
my $on_ssl_error = delete $params{"on_ssl_error"} or defined wantarray
my $on_ssl_error = delete $params{"on_ssl_error"} defined wantarray
Truth table66%2/3
A or B
A= my $on_ssl_error = delete $params{"on_ssl_error"}B= defined wantarray
ABresult
1X1
011
000
445  
446 my $f = $loop->listen(
447 socktype => 'stream',
448 %params,
449 5 ▶ )->on_done( sub {
Subroutine __ANON__: called CC=1 SCAR=0.1
450 5 my $listener = shift;
451  
452 5 my $cleartext_acceptor = $listener->acceptor;
453 5 ▶ my $ssl_acceptor = sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
454 5 my $listener = shift;
455 5 my ( $listen_sock, %params ) = @_;
456 5 my $stream = $params{handle};
457 5 ▶ !defined $stream or $stream->isa( "IO::Async::Stream" ) or
Branch: 1 branch
BranchTrueFalseTotal
not defined $stream or $stream->isa("IO::Async::Stream") or croak("Can only accept SSL on IO::Async::Stream handles") 5 0 5
Condition66%2/3
not defined $stream or $stream->isa("IO::Async::Stream")
l!l&&r!l&&!r
140
MC/DC0%0/2
not defined $stream or $stream->isa("IO::Async::Stream")
not defined $stream $stream->isa("IO::Async::Stream")
Truth table66%2/3
not(A) or B
A= not defined $streamB= $stream->isa("IO::Async::Stream")
ABresult
1X1
011
000
458 croak "Can only accept SSL on IO::Async::Stream handles";
459  
460 5 ▶ $listener->$cleartext_acceptor( $listen_sock )->then( sub {
Subroutine __ANON__: called CC=4 SCAR=14.3
461 5 my ( $socket ) = @_;
462  
463 5 ▶ return Future->done() unless $socket; # EAGAIN
Branch: 1 branch
BranchTrueFalseTotal
unless $socket 0 5 5
464  
465 5 ▶ $stream->configure( handle => $socket ) if $stream;
Branch: 1 branch
BranchTrueFalseTotal
if $stream 4 1 5
466  
467 $loop->SSL_upgrade(
468 _SSL_args( SSL_server => 1, %ssl_params ),
469 handle => ( $stream || $socket ),
470 1 ▶ )->catch_with_f( ssl => sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
471 1 my ( $f, $failure ) = @_;
472 1 ▶ if( $on_ssl_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_ssl_error) 1 0 1
473 1 $on_ssl_error->( $failure );
474 1 return Future->done; # eat it
475 }
476 0 return $f;
477 5 ▶ });
Condition66%2/3
$stream || $socket
l!l&&r!l&&!r
410
MC/DC0%0/2
$stream || $socket
$stream $socket
Truth table66%2/3
A || B
A= $streamB= $socket
ABresult
1X1
011
000
478 5 });
479 5 };
480  
481 5 $listener->configure( acceptor => $ssl_acceptor );
482 5 });
483  
484 5 ▶ return $f if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 3 2 5
485  
486 # Caller is not going to keep hold of the Future, so we have to ensure it
487 # stays alive somehow
488 2 ▶ $f->on_ready( sub { undef $f } ); # intentional cycle
Subroutine __ANON__: called CC=1 SCAR=0.0
489 }
490  
491 =head1 STREAM PROTOCOL METHODS
492  
493 The following extra methods are added to L<IO::Async::Protocol::Stream>.
494  
495 =cut
496  
497 =head2 SSL_upgrade
498  
499 $protocol->SSL_upgrade( %params )->get;
500  
501 A shortcut to calling C<< $loop->SSL_upgrade >>. This method will unconfigure
502 the C<transport> of the Protocol, upgrade its underlying filehandle to SSL,
503 then reconfigure it again with SSL reader and writer functions on it. It takes
504 the same arguments as C<< $loop->SSL_upgrade >>, except that the C<handle>
505 argument is not required as it's taken from the Protocol's C<transport>.
506  
507 =cut
508  
509 sub IO::Async::Protocol::Stream::SSL_upgrade
510 2 ▶{
Subroutine SSL_upgrade: called CC=3 SCAR=11.1
Pod undocumented
511 2 my $protocol = shift;
512 2 my %params = @_;
513  
514 2 ▶ my $on_upgraded = delete $params{on_upgraded} or croak "Expected 'on_upgraded'";
Branch: 1 branch
BranchTrueFalseTotal
my $on_upgraded = delete $params{"on_upgraded"} or croak("Expected 'on_upgraded'") 2 0 2
515  
516 2 ▶ my $loop = $protocol->get_loop or croak "Expected to be a member of a Loop";
Branch: 1 branch
BranchTrueFalseTotal
my $loop = $protocol->get_loop or croak("Expected to be a member of a Loop") 2 0 2
517  
518 2 my $transport = $protocol->transport;
519  
520 2 $protocol->configure( transport => undef );
521  
522 $loop->SSL_upgrade(
523 handle => $transport,
524 2 ▶ on_upgraded => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
525 2 my ( $transport ) = @_;
526  
527 2 $protocol->configure( transport => $transport );
528  
529 2 $on_upgraded->();
530 },
531  
532 2 %params,
533 );
534 }
535  
536 =head1 AUTHOR
537  
538 Paul Evans <leonerd@leonerd.org.uk>
539  
540 =cut
541  
542 11 0x55AA;
↑ Summary SSLStream.pm »