blib/lib/POE/Component/SSLify/ServerHandle.pm

filter: Statementstmt 81.6% 71 / 87 Branchbran 60.0% 30 / 50 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 84.6% 11 / 13 Podpod n/a totaltotal 74.1% 115 / 155 SCAR 37.0
CC
27
Cov
74%
WRITE
13.8
READ
10.0
_check_status
8.2
CRAP 40.5
« blib/lib/POE/Component/SSLify.pm ↑ Summary blib/lib/POE/Component/SSLify/ClientHandle.pm »
1 #
2 # This file is part of POE-Component-SSLify
3 #
4 # This software is copyright (c) 2014 by Apocalypse.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 16 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
10 package POE::Component::SSLify::ServerHandle;
11 16 $POE::Component::SSLify::ServerHandle::VERSION = '1.011';
12 16 our $AUTHORITY = 'cpan:APOCAL';
13  
14 # ABSTRACT: Server-side handle for SSLify
15  
16 # Import the SSL death routines
17 16 ▶use Net::SSLeay 1.36 qw( die_now die_if_ssl_error ERROR_WANT_READ ERROR_WANT_WRITE );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # Ties the socket
20 sub TIEHANDLE {
21 3 ▶ my ( $class, $socket, $ctx, $connref ) = @_;
Subroutine TIEHANDLE: called CC=3 SCAR=11.2
22  
23 3 ▶ my $ssl = Net::SSLeay::new( $ctx ) or die_now( "Failed to create SSL $!" );
Branch: 1 branch
BranchTrueFalseTotal
my $ssl = &Net::SSLeay::new($ctx) or die_now("Failed to create SSL $!") 0 3 3
24  
25 3 my $fileno = fileno( $socket );
26  
27 3 Net::SSLeay::set_fd( $ssl, $fileno );
28  
29 # Socket is in non-blocking mode, so accept() will return immediately.
30 # die_if_ssl_error won't die on non-blocking errors. We don't need to call accept()
31 # again, because OpenSSL I/O functions (read, write, ...) can handle that entirely
32 # by self (it's needed to accept() once to determine connection type).
33 3 ▶ my $res = Net::SSLeay::accept( $ssl ) and die_if_ssl_error( 'ssl accept' );
Branch: 1 branch
BranchTrueFalseTotal
my $res = &Net::SSLeay::accept($ssl) and die_if_ssl_error("ssl accept") 3 0 3
34  
35 3 my $self = bless {
36 'ssl' => $ssl,
37 'ctx' => $ctx,
38 'socket' => $socket,
39 'fileno' => $fileno,
40 'status' => $res,
41 'on_connect' => $connref,
42 'ssl_started' => 0,
43 }, $class;
44  
45 3 return $self;
46 }
47  
48 # TODO should we make a convenience function to convert retval to string equivalents for easier debugging?
49 # From OpenSSL 1.0.0d
50 #define SSL_ERROR_NONE 0
51 #define SSL_ERROR_SSL 1
52 #define SSL_ERROR_WANT_READ 2
53 #define SSL_ERROR_WANT_WRITE 3
54 #define SSL_ERROR_WANT_X509_LOOKUP 4
55 #define SSL_ERROR_SYSCALL 5 /* look at error stack/return value/errno */
56 #define SSL_ERROR_ZERO_RETURN 6
57 #define SSL_ERROR_WANT_CONNECT 7
58 #define SSL_ERROR_WANT_ACCEPT 8
59  
60 sub _check_status {
61 8 ▶ my $self = shift;
Subroutine _check_status: called CC=8 SCAR=21.0
62  
63 # Okay, is negotiation done?
64 # http://www.openssl.org/docs/ssl/SSL_connect.html#RETURN_VALUES
65 8 ▶ if ( exists $self->{'client'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'client'}) { } 3 5 8
66 3 $self->{'status'} = Net::SSLeay::connect( $self->{'ssl'} );
67 } else {
68 5 $self->{'status'} = Net::SSLeay::accept( $self->{'ssl'} );
69 }
70  
71 8 ▶ if ( $self->{'status'} <= 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'status'} <= 0) { } 4 4 8
elsif ($self->{'status'} == 1) { } 4 0 4
72 # http://www.openssl.org/docs/ssl/SSL_get_error.html
73 4 my $errval = Net::SSLeay::get_error( $self->{'ssl'}, $self->{'status'} );
74  
75 # Handle the case of ERROR_WANT_READ and ERROR_WANT_WRITE
76 # TODO should we skip ERROR_WANT_ACCEPT and ERROR_WANT_CONNECT ?
77 # also, ERROR_WANT_ACCEPT isn't exported by Net::SSLeay, huh?
78 4 ▶ if ( $errval == ERROR_WANT_READ or $errval == ERROR_WANT_WRITE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errval == ERROR_WANT_READ() or $errval == ERROR_WANT_WRITE()) { } 2 2 4
Condition66%2/3
$errval == ERROR_WANT_READ() or $errval == ERROR_WANT_WRITE()
l!l&&r!l&&!r
202
MC/DC50%1/2
$errval == ERROR_WANT_READ() or $errval == ERROR_WANT_WRITE()
$errval == ERROR_WANT_READ() $errval == ERROR_WANT_WRITE()
Truth table66%2/3
A or B
A= $errval == ERROR_WANT_READ()B= $errval == ERROR_WANT_WRITE()
ABresult
1X1
011
000
79 # continue reading/writing from the socket until we connect or not...
80 2 return 1;
81 } else {
82 # call the hook function for error connect
83 2 ▶ if ( defined $self->{'on_connect'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"on_connect"}) 2 0 2
84 2 $self->{'on_connect'}->( $self->{'orig_socket'}, 0, $errval );
85 }
86  
87 # don't try to read/write from the socket anymore!
88 2 return 0;
89 }
90 } elsif ( $self->{'status'} == 1 ) {
91 # SSL handshake is done!
92 4 $self->{'ssl_started'} = 1;
93  
94 # call the hook function for successful connect
95 4 ▶ if ( defined $self->{'on_connect'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"on_connect"}) 4 0 4
96 4 $self->{'on_connect'}->( $self->{'orig_socket'}, 1 );
97 }
98  
99 # we can now read/write from the socket!
100 4 return 1;
101 }
102 }
103  
104 # Read something from the socket
105 sub READ {
106 # Get ourself!
107 10 ▶ my $self = shift;
Subroutine READ: called CC=7 SCAR=23.0
108  
109 # Get the pointers to buffer, length, and the offset
110 10 my( $buf, $len, $offset ) = \( @_ );
111  
112 # Check the status of the SSL handshake
113 10 ▶ if ( ! $self->{'ssl_started'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ssl_started"}) 7 3 10
114 7 ▶ return if $self->_check_status == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_check_status == 0 2 5 7
115 }
116  
117 # If we have no offset, replace the buffer with some input
118 8 ▶ if ( ! defined $$offset ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$offset) 8 0 8
119 8 $$buf = Net::SSLeay::read( $self->{'ssl'}, $$len );
120  
121 # Are we done?
122 8 ▶ if ( defined $$buf ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$buf) { } 2 6 8
123 # TODO do we need the same "flush is success" logic in WRITE?
124  
125 2 return length( $$buf );
126 } else {
127 # Nah, clear the buffer too...
128 6 $$buf = "";
129 6 return;
130 }
131 }
132  
133 # Now, actually read the data
134 0 ▶ defined( my $read = Net::SSLeay::read( $self->{'ssl'}, $$len ) ) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $read = &Net::SSLeay::read($self->{"ssl"}, $$len)) or return 0 0 0
135  
136 # TODO do we need the same "flush is success" logic in WRITE?
137  
138 # Figure out the buffer and offset
139 0 my $buf_len = length( $$buf );
140  
141 # If our offset is bigger, pad the buffer
142 0 ▶ if ( $$offset > $buf_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$offset > $buf_len) 0 0 0
143 0 $$buf .= chr( 0 ) x ( $$offset - $buf_len );
144 }
145  
146 # Insert what we just read into the buffer
147 0 substr( $$buf, $$offset, 1, $read );
148  
149 # All done!
150 0 return length( $read );
151 }
152  
153 # Write some stuff to the socket
154 sub WRITE {
155 # Get ourself + buffer + length + offset to write
156 2 ▶ my( $self, $buf, $len, $offset ) = @_;
Subroutine WRITE: called CC=8 SCAR=26.2
157  
158 # Check the status of the SSL handshake
159 2 ▶ if ( ! $self->{'ssl_started'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ssl_started"}) 1 1 2
160 # The normal syswrite() POE uses expects 0 here.
161 1 ▶ return 0 if $self->_check_status == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_check_status == 0 0 1 1
162 }
163  
164 # If we have nothing to offset, then start from the beginning
165 2 ▶ if ( ! defined $offset ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $offset) 0 2 2
166 0 $offset = 0;
167 }
168  
169 # Thanks to RT#95071 and RT#58243 we need to clamp the length to the TLS 16K limit
170 # seems like the same thing happened to https://www.mail-archive.com/openssl-users@openssl.org/msg28151.html
171 2 ▶ $len = 16_384 if $len > 16_384;
Branch: 1 branch
BranchTrueFalseTotal
if $len > 16384 0 2 2
172  
173 # We count the number of characters written to the socket
174 2 my $wrote_len = Net::SSLeay::write( $self->{'ssl'}, substr( $buf, $offset, $len ) );
175  
176 # Did we get an error or number of bytes written?
177 # Net::SSLeay::write() returns the number of bytes written, or 0 on unsuccessful
178 # operation (probably connection closed), or -1 on error.
179 2 ▶ if ( $wrote_len < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wrote_len < 0) { } 0 2 2
180 # The normal syswrite() POE uses expects 0 here.
181 0 return 0;
182 } else {
183 # We flushed some data, which means we finished the handshake!
184 # This is IMPORTANT, as MIRE found out!
185 # Otherwise openssl will zonk out and give us SSL_ERROR_SSL and things randomly break :(
186 # this is because we tried to connect() or accept() and the handshake was done... or something like that hah
187 2 ▶ if ( ! $self->{'ssl_started'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ssl_started"}) 0 2 2
188 0 $self->{'ssl_started'} = 1;
189 0 $self->{'status'} = 1;
190  
191 # call the hook function for successful connect
192 0 ▶ if ( defined $self->{'on_connect'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"on_connect"}) 0 0 0
193 0 $self->{'on_connect'}->( $self->{'orig_socket'}, 1 );
194 }
195 }
196  
197 # All done!
198 2 return $wrote_len;
199 }
200 }
201  
202 # Sets binmode on the socket
203 # Thanks to RT #27117
204 sub BINMODE {
205 20 ▶ my $self = shift;
Subroutine BINMODE: called CC=2 SCAR=7.9
206 20 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 20 20
207 0 my $mode = shift;
208 0 binmode $self->{'socket'}, $mode;
209 } else {
210 20 binmode $self->{'socket'};
211 }
212  
213 20 return;
214 }
215  
216 # Closes the socket
217 sub CLOSE {
218 31 ▶ my $self = shift;
Subroutine CLOSE: called CC=3 SCAR=11.0
219 31 ▶ if ( defined $self->{'socket'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"socket"}) 31 0 31
220 31 Net::SSLeay::free( $self->{'ssl'} );
221  
222 # TODO we ignore any close errors because there's no way to sanely propagate it up the stack...
223 31 close( $self->{'socket'} ); ## no critic ( InputOutput::RequireCheckedClose )
224 31 undef $self->{'socket'};
225  
226 # do we need to do CTX_free?
227 31 ▶ if ( exists $self->{'client'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"client"}) 28 3 31
228 28 Net::SSLeay::CTX_free( $self->{'ctx'} );
229 }
230 }
231  
232 31 return 1;
233 }
234  
235 # Add DESTROY handler
236 sub DESTROY {
237 31 ▶ my $self = shift;
Subroutine DESTROY: called CC=2 SCAR=7.0
238  
239 # Did we already CLOSE?
240 31 ▶ if ( defined $self->{'socket'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"socket"}) 31 0 31
241 # Guess not...
242 31 $self->CLOSE();
243 }
244  
245 31 return;
246 }
247  
248 sub FILENO {
249 343 ▶ my $self = shift;
Subroutine FILENO: called CC=1 SCAR=0.0
250 343 return $self->{'fileno'};
251 }
252  
253 # Not implemented TIE's
254 sub READLINE {
255 0 ▶ die 'Not Implemented';
Subroutine READLINE: not called CC=1 SCAR=6.9
256 }
257  
258 sub PRINT {
259 0 ▶ die 'Not Implemented';
Subroutine PRINT: not called CC=1 SCAR=6.9
260 }
261  
262 16 1;
263  
264 __END__
« blib/lib/POE/Component/SSLify.pm ↑ Summary blib/lib/POE/Component/SSLify/ClientHandle.pm »