blib/lib/POE/Component/SSLify.pm

filter: Statementstmt 79.6% 98 / 123 Branchbran 54.6% 35 / 64 Conditioncond 44.4% 16 / 36 MC/DCmcdc 15.0% 3 / 20 Subroutinesub 90.0% 18 / 20 Podpod 100% 9 / 9 totaltotal 65.8% 179 / 272 SCAR 47.8
CC
45
Cov
67%
Client_SSLify
28.5
_createSSLcontext
27.1
Server_SSLify
16.2
CRAP 119.0
↑ Summary blib/lib/POE/Component/SSLify/ServerHandle.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 14 ▶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;
11 # git description: release-1.010-4-g6795ac7
12 14 $POE::Component::SSLify::VERSION = '1.011';
13 14 our $AUTHORITY = 'cpan:APOCAL';
14  
15 # ABSTRACT: Makes using SSL in the world of POE easy!
16  
17 BEGIN {
18 # should fix netbsd smoke failures, thanks BinGOs!
19 # <BinGOs> Apocal: okay cores with a 0.9.7d I've built myself from source. Doesn't if I comment out engine lines.
20 # BinGOs did an awesome job building various versions of openssl to try and track down the problem, it seems like
21 # newer versions of openssl worked fine on netbsd, but I don't want to do crazy stuff like probing openssl versions
22 # as it's fragile - best to let the user figure it out :)
23 #
24 # see http://www.cpantesters.org/cpan/report/1a660280-6eb1-11e0-a462-e9956c33433b
25 # http://www.cpantesters.org/cpan/report/49a9f2aa-6df2-11e0-a462-e9956c33433b
26 # http://www.cpantesters.org/cpan/report/78d9a234-6df5-11e0-a462-e9956c33433b
27 # and many other reports :(
28 #
29 #(gdb) bt
30 ##0 0xbd9d3e7e in engine_table_select () from /usr/lib/libcrypto.so.2
31 ##1 0xbd9b3bed in ENGINE_get_default_RSA () from /usr/lib/libcrypto.so.2
32 ##2 0xbd9b1f6d in RSA_new_method () from /usr/lib/libcrypto.so.2
33 ##3 0xbd9b1cf6 in RSA_new () from /usr/lib/libcrypto.so.2
34 ##4 0xbd9cf8a1 in RSAPrivateKey_asn1_meth () from /usr/lib/libcrypto.so.2
35 ##5 0xbd9da64b in ASN1_item_ex_new () from /usr/lib/libcrypto.so.2
36 ##6 0xbd9da567 in ASN1_item_ex_new () from /usr/lib/libcrypto.so.2
37 ##7 0xbd9d88cc in ASN1_item_ex_d2i () from /usr/lib/libcrypto.so.2
38 ##8 0xbd9d8437 in ASN1_item_d2i () from /usr/lib/libcrypto.so.2
39 ##9 0xbd9cf8d5 in d2i_RSAPrivateKey () from /usr/lib/libcrypto.so.2
40 ##10 0xbd9ad546 in d2i_PrivateKey () from /usr/lib/libcrypto.so.2
41 ##11 0xbd995e63 in PEM_read_bio_PrivateKey () from /usr/lib/libcrypto.so.2
42 ##12 0xbd980430 in PEM_read_bio_RSAPrivateKey () from /usr/lib/libcrypto.so.2
43 ##13 0xbda2e9dc in SSL_CTX_use_RSAPrivateKey_file () from /usr/lib/libssl.so.3
44 ##14 0xbda5aabe in XS_Net__SSLeay_CTX_use_RSAPrivateKey_file (cv=0x8682c80)
45 # at SSLeay.c:1716
46 ##15 0x08115401 in Perl_pp_entersub () at pp_hot.c:2885
47 ##16 0x080e0ab7 in Perl_runops_debug () at dump.c:2049
48 ##17 0x08078624 in S_run_body (oldscope=1) at perl.c:2308
49 ##18 0x08077ef2 in perl_run (my_perl=0x823f030) at perl.c:2233
50 ##19 0x0805e321 in main (argc=3, argv=0xbfbfe6a0, env=0xbfbfe6b0)
51 # at perlmain.c:117
52 ##20 0x0805e0c6 in ___start ()
53 #(gdb)
54 14 ▶ if ( ! defined &LOAD_SSL_ENGINES ) { *LOAD_SSL_ENGINES = sub () { 0 } }
Subroutine BEGIN: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if (not defined &POE::Component::SSLify::LOAD_SSL_ENGINES) 14 0 14
55 }
56  
57 # We need Net::SSLeay or all's a failure!
58 BEGIN {
59 # We need >= 1.36 because it contains a lot of important fixes
60 14 ▶ eval "use Net::SSLeay 1.36 qw( die_now die_if_ssl_error FILETYPE_PEM )";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 # Check for errors...
63 14 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 14 14
64 # Oh boy!
65 0 die $@;
66 } else {
67 # Finally, load our subclasses :)
68 # ClientHandle isa ServerHandle so it will get loaded automatically
69 14 require POE::Component::SSLify::ClientHandle;
70  
71 # Initialize Net::SSLeay
72 # Taken from http://search.cpan.org/~flora/Net-SSLeay-1.36/lib/Net/SSLeay.pm#Low_level_API
73 14 Net::SSLeay::load_error_strings();
74 14 Net::SSLeay::SSLeay_add_ssl_algorithms();
75 14 if ( LOAD_SSL_ENGINES ) {
76 Net::SSLeay::ENGINE_load_builtin_engines();
77 Net::SSLeay::ENGINE_register_all_complete();
78 }
79 14 Net::SSLeay::randomize();
80 }
81 }
82  
83 # Do the exporting magic...
84 14 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
85 14 our @EXPORT_OK = qw(
86 Client_SSLify Server_SSLify
87 SSLify_Options SSLify_GetCTX SSLify_GetCipher SSLify_GetSocket SSLify_GetSSL SSLify_ContextCreate SSLify_GetStatus
88 );
89  
90 # Bring in some socket-related stuff
91 14 ▶use Symbol qw( gensym );
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 # we need IO 1.24 for it's win32 fixes but it includes IO::Handle 1.27_02 which is dev...
94 # unfortunately we have to jump to IO 1.25 which includes IO::Handle 1.28... argh!
95 14 ▶use IO::Handle 1.28;
Subroutine BEGIN: called CC=1 SCAR=0.0
96  
97 # Use Scalar::Util's weaken() for the connref stuff
98 14 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
99 14 ▶use Task::Weaken 1.03; # to make sure it actually works!
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101 # load POE ( just to fool dzil AutoPrereqs :)
102 14 require POE;
103  
104 # The server-side CTX stuff
105 14 my $ctx;
106  
107 # global so users of this module can override it locally
108 14 our $IGNORE_SSL_ERRORS = 0;
109  
110 #pod =func Client_SSLify
111 #pod
112 #pod This function sslifies a client-side socket. You can pass several options to it:
113 #pod
114 #pod my $socket = shift;
115 #pod $socket = Client_SSLify( $socket, $version, $options, $ctx, $callback );
116 #pod $socket is the non-ssl socket you got from somewhere ( required )
117 #pod $version is the SSL version you want to use
118 #pod $options is the SSL options you want to use
119 #pod $ctx is the custom SSL context you want to use
120 #pod $callback is the callback hook on success/failure of sslification
121 #pod
122 #pod # This is an example of the callback and you should pass it as Client_SSLify( $socket, ... , \&callback );
123 #pod sub callback {
124 #pod my( $socket, $status, $errval ) = @_;
125 #pod # $socket is the original sslified socket in case you need to play with it
126 #pod # $status is either 1 or 0; with 1 signifying success and 0 failure
127 #pod # $errval will be defined if $status == 0; it's the numeric SSL error code
128 #pod # check http://www.openssl.org/docs/ssl/SSL_get_error.html for the possible error values ( and import them from Net::SSLeay! )
129 #pod
130 #pod # The return value from the callback is discarded
131 #pod }
132 #pod
133 #pod If $ctx is defined, SSLify will ignore $version and $options. Otherwise, it will be created from the $version and
134 #pod $options parameters. If all of them are undefined, it will follow the defaults in L</SSLify_ContextCreate>.
135 #pod
136 #pod BEWARE: If you passed in a CTX, SSLify will do Net::SSLeay::CTX_free( $ctx ) when the
137 #pod socket is destroyed. This means you cannot reuse contexts!
138 #pod
139 #pod NOTE: The way to have a client socket with proper certificates set up is:
140 #pod
141 #pod my $socket = shift; # get the socket from somewhere
142 #pod my $ctx = SSLify_ContextCreate( 'server.key', 'server.crt' );
143 #pod $socket = Client_SSLify( $socket, undef, undef, $ctx );
144 #pod
145 #pod NOTE: You can pass the callback anywhere in the arguments, we'll figure it out for you! If you want to call a POE event, please look
146 #pod into the postback/callback stuff in L<POE::Session>.
147 #pod
148 #pod # we got this from POE::Wheel::SocketFactory
149 #pod sub event_SuccessEvent {
150 #pod my $socket = $_[ARG0];
151 #pod $socket = Client_SSLify( $socket, $_[SESSION]->callback( 'sslify_result' ) );
152 #pod $_[HEAP]->{client} = POE::Wheel::ReadWrite->new(
153 #pod Handle => $socket,
154 #pod ...
155 #pod );
156 #pod return;
157 #pod }
158 #pod
159 #pod # the callback event
160 #pod sub event_sslify_result {
161 #pod my ($creation_args, $called_args) = @_[ARG0, ARG1];
162 #pod my( $socket, $status, $errval ) = @$called_args;
163 #pod
164 #pod if ( $status ) {
165 #pod print "Yay, SSLification worked!";
166 #pod } else {
167 #pod print "Aw, SSLification failed with error $errval";
168 #pod }
169 #pod }
170 #pod =cut
171  
172 sub Client_SSLify {
173 # Get the socket + version + options + ctx + callback
174 28 ▶ my( $socket, $version, $options, $custom_ctx, $callback ) = @_;
Subroutine Client_SSLify: called CC=14 SCAR=33.5
Pod documented
175  
176 # Validation...
177 28 ▶ if ( ! defined $socket ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $socket) 0 28 28
178 0 die "Did not get a defined socket";
179 }
180  
181 # Mangle the callback stuff
182 28 ▶ if ( defined $version and ref $version and ref( $version ) eq 'CODE' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $version and ref $version and ref $version eq 'CODE') { } 3 25 28
elsif (defined $options and ref $options and ref $options eq 'CODE') { } 0 25 25
elsif (defined $custom_ctx and ref $custom_ctx and ref $custom_ctx eq 'CODE') { } 0 50 50
Condition66%4/6
defined $version and ref $version and ref $version eq "CODE"
!ll&&!rl&&r
2503
defined $version and ref $version
!ll&&!rl&&r
2503
MC/DC33%1/3
defined $version and ref $version and ref $version eq "CODE"
defined $version ref $version ref $version eq "CODE"
Truth table50%2/4
A and B and C
A= defined $versionB= ref $versionC= ref $version eq "CODE"
ABCresult
0XX0
10X0
1100
1111
183 3 $callback = $version;
184 3 $version = $options = $custom_ctx = undef;
185 ▶ } elsif ( defined $options and ref $options and ref( $options ) eq 'CODE' ) {
Condition33%2/6
defined $options and ref $options and ref $options eq "CODE"
!ll&&!rl&&r
2500
defined $options and ref $options
!ll&&!rl&&r
2500
MC/DC0%0/3
defined $options and ref $options and ref $options eq "CODE"
defined $options ref $options ref $options eq "CODE"
Truth table25%1/4
A and B and C
A= defined $optionsB= ref $optionsC= ref $options eq "CODE"
ABCresult
0XX0
10X0
1100
1111
186 0 $callback = $options;
187 0 $options = $custom_ctx = undef;
188 ▶ } elsif ( defined $custom_ctx and ref $custom_ctx and ref( $custom_ctx ) eq 'CODE' ) {
Condition33%2/6
defined $custom_ctx and ref $custom_ctx and ref $custom_ctx eq "CODE"
!ll&&!rl&&r
2500
defined $custom_ctx and ref $custom_ctx
!ll&&!rl&&r
2500
MC/DC0%0/3
defined $custom_ctx and ref $custom_ctx and ref $custom_ctx eq "CODE"
defined $custom_ctx ref $custom_ctx ref $custom_ctx eq "CODE"
Truth table25%1/4
A and B and C
A= defined $custom_ctxB= ref $custom_ctxC= ref $custom_ctx eq "CODE"
ABCresult
0XX0
10X0
1100
1111
189 0 $callback = $custom_ctx;
190 0 $custom_ctx = undef;
191 }
192  
193 # From IO::Handle POD
194 # If an error occurs blocking will return undef and $! will be set.
195 28 ▶ if ( ! defined $socket->blocking( 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $socket->blocking(0)) 0 28 28
196 0 die "Unable to set nonblocking mode on socket: $!";
197 }
198  
199 # Now, we create the new socket and bind it to our subclass of Net::SSLeay::Handle
200 28 my $newsock = gensym();
201 28 ▶ tie( *$newsock, 'POE::Component::SSLify::ClientHandle', $socket, $version, $options, $custom_ctx, $callback ) or die "Unable to tie to our subclass: $!";
Branch: 1 branch
BranchTrueFalseTotal
tie *$newsock, "POE::Component::SSLify::ClientHandle", $socket, $version, $options, $custom_ctx, $callback or die "Unable to tie to our subclass: $!" 0 28 28
202  
203 # argh, store the newsock in the tied class to use for callback
204 28 ▶ if ( defined $callback ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $callback) 3 25 28
205 3 tied( *$newsock )->{'orig_socket'} = $newsock;
206 3 weaken( tied( *$newsock )->{'orig_socket'} );
207 }
208  
209 # All done!
210 28 return $newsock;
211 }
212  
213 #pod =func Server_SSLify
214 #pod
215 #pod This function sslifies a server-side socket. You can pass several options to it:
216 #pod
217 #pod my $socket = shift;
218 #pod $socket = Server_SSLify( $socket, $ctx, $callback );
219 #pod $socket is the non-ssl socket you got from somewhere ( required )
220 #pod $ctx is the custom SSL context you want to use; overrides the global ctx set in SSLify_Options
221 #pod $callback is the callback hook on success/failure of sslification
222 #pod
223 #pod BEWARE: L</SSLify_Options> must be called first if you aren't passing a $ctx. If you want to set some options per-connection, do this:
224 #pod
225 #pod my $socket = shift; # get the socket from somewhere
226 #pod my $ctx = SSLify_ContextCreate();
227 #pod # set various options on $ctx as desired
228 #pod $socket = Server_SSLify( $socket, $ctx );
229 #pod
230 #pod NOTE: You can use L</SSLify_GetCTX> to modify the global, and avoid doing this on every connection if the
231 #pod options are the same...
232 #pod
233 #pod Please look at L</Client_SSLify> for more details on the callback hook.
234 #pod =cut
235  
236 sub Server_SSLify {
237 # Get the socket!
238 11 ▶ my( $socket, $custom_ctx, $callback ) = @_;
Subroutine Server_SSLify: called CC=11 SCAR=27.8
Pod documented
239  
240 # Validation...
241 11 ▶ if ( ! defined $socket ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $socket) 0 11 11
242 0 die "Did not get a defined socket";
243 }
244  
245 # If we don't have a ctx ready, we can't do anything...
246 11 ▶ if ( ! defined $ctx and ! defined $custom_ctx ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ctx or defined $custom_ctx) 8 3 11
Condition66%2/3
defined $ctx or defined $custom_ctx
l!l&&r!l&&!r
308
MC/DC50%1/2
defined $ctx or defined $custom_ctx
defined $ctx defined $custom_ctx
Truth table66%2/3
A or B
A= defined $ctxB= defined $custom_ctx
ABresult
1X1
011
000
247 8 die 'Please do SSLify_Options() first ( or pass in a $ctx object )';
248 }
249  
250 # mangle custom_ctx depending on callback
251 3 ▶ if ( defined $custom_ctx and ref $custom_ctx and ref( $custom_ctx ) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $custom_ctx and ref $custom_ctx and ref $custom_ctx eq "CODE") 3 0 3
Condition33%2/6
defined $custom_ctx and ref $custom_ctx and ref $custom_ctx eq "CODE"
!ll&&!rl&&r
003
defined $custom_ctx and ref $custom_ctx
!ll&&!rl&&r
003
MC/DC0%0/3
defined $custom_ctx and ref $custom_ctx and ref $custom_ctx eq "CODE"
defined $custom_ctx ref $custom_ctx ref $custom_ctx eq "CODE"
Truth table25%1/4
A and B and C
A= defined $custom_ctxB= ref $custom_ctxC= ref $custom_ctx eq "CODE"
ABCresult
0XX0
10X0
1100
1111
252 3 $callback = $custom_ctx;
253 3 $custom_ctx = undef;
254 }
255  
256 # From IO::Handle POD
257 # If an error occurs blocking will return undef and $! will be set.
258 3 ▶ if ( ! defined $socket->blocking( 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $socket->blocking(0)) 0 3 3
259 0 die "Unable to set nonblocking mode on socket: $!";
260 }
261  
262 # Now, we create the new socket and bind it to our subclass of Net::SSLeay::Handle
263 3 my $newsock = gensym();
264 3 ▶ tie( *$newsock, 'POE::Component::SSLify::ServerHandle', $socket, ( $custom_ctx || $ctx ), $callback ) or die "Unable to tie to our subclass: $!";
Branch: 1 branch
BranchTrueFalseTotal
tie *$newsock, "POE::Component::SSLify::ServerHandle", $socket, $custom_ctx || $ctx, $callback or die "Unable to tie to our subclass: $!" 0 3 3
Condition33%1/3
$custom_ctx || $ctx
l!l&&r!l&&!r
030
MC/DC0%0/2
$custom_ctx || $ctx
$custom_ctx $ctx
Truth table33%1/3
A || B
A= $custom_ctxB= $ctx
ABresult
1X1
011
000
265  
266 # argh, store the newsock in the tied class to use for connref
267 3 ▶ if ( defined $callback ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $callback) 3 0 3
268 3 tied( *$newsock )->{'orig_socket'} = $newsock;
269 3 weaken( tied( *$newsock )->{'orig_socket'} );
270 }
271  
272 # All done!
273 3 return $newsock;
274 }
275  
276 #pod =func SSLify_ContextCreate
277 #pod
278 #pod Accepts some options, and returns a brand-new Net::SSLeay context object ( $ctx )
279 #pod
280 #pod my $ctx = SSLify_ContextCreate( $key, $cert, $version, $options );
281 #pod $key is the certificate key file
282 #pod $cert is the certificate file
283 #pod $version is the SSL version to use
284 #pod $options is the SSL options to use
285 #pod
286 #pod You can then call various Net::SSLeay methods on the context
287 #pod
288 #pod my $mode = Net::SSLeay::CTX_get_mode( $ctx );
289 #pod
290 #pod By default we don't use the SSL key + certificate files
291 #pod
292 #pod By default we use the version: default. Known versions of the SSL connection - look at
293 #pod L<http://www.openssl.org/docs/ssl/SSL_CTX_new.html> for more info.
294 #pod
295 #pod * sslv2
296 #pod * sslv3
297 #pod * tlsv1
298 #pod * sslv23
299 #pod * default ( sslv23 )
300 #pod
301 #pod By default we don't set any options - look at L<http://www.openssl.org/docs/ssl/SSL_CTX_set_options.html> for more info.
302 #pod =cut
303  
304 sub SSLify_ContextCreate {
305 # Get the key + cert + version + options
306 25 ▶ my( $key, $cert, $version, $options ) = @_;
Subroutine SSLify_ContextCreate: called CC=1 SCAR=0.0
Pod documented
307  
308 25 return _createSSLcontext( $key, $cert, $version, $options );
309 }
310  
311 #pod =func SSLify_Options
312 #pod
313 #pod Call this function to initialize the global server-side context object. This will be the default context whenever you call
314 #pod L</Server_SSLify> without passing a custom context to it.
315 #pod
316 #pod SSLify_Options( $key, $cert, $version, $options );
317 #pod $key is the certificate key file ( required )
318 #pod $cert is the certificate file ( required )
319 #pod $version is the SSL version to use
320 #pod $options is the SSL options to use
321 #pod
322 #pod By default we use the version: default
323 #pod
324 #pod By default we use the options: Net::SSLeay::OP_ALL
325 #pod
326 #pod Please look at L</SSLify_ContextCreate> for more info on the available versions/options.
327 #pod =cut
328  
329 sub SSLify_Options {
330 # Get the key + cert + version + options
331 19 ▶ my( $key, $cert, $version, $options ) = @_;
Subroutine SSLify_Options: called CC=5 SCAR=19.3
Pod documented
332  
333 # sanity
334 19 ▶ if ( ! defined $key or ! defined $cert ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key and defined $cert) 0 19 19
Condition33%1/3
defined $key and defined $cert
!ll&&!rl&&r
0019
MC/DC0%0/2
defined $key and defined $cert
defined $key defined $cert
Truth table33%1/3
A and B
A= defined $keyB= defined $cert
ABresult
0X0
100
111
335 0 die 'no key/cert specified';
336 }
337  
338 # Set the default
339 19 ▶ if ( ! defined $options ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options) 19 0 19
340 19 $options = Net::SSLeay::OP_ALL();
341 }
342  
343 # set the context, possibly overwriting the previous one
344 19 ▶ if ( defined $ctx ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ctx) 0 19 19
345 0 Net::SSLeay::CTX_free( $ctx );
346 0 undef $ctx;
347 }
348 19 $ctx = _createSSLcontext( $key, $cert, $version, $options );
349  
350 # all done!
351 3 return 1;
352 }
353  
354 sub _createSSLcontext {
355 72 ▶ my( $key, $cert, $version, $options ) = @_;
Subroutine _createSSLcontext: called CC=15 SCAR=33.0
356  
357 72 my $context;
358 72 ▶ if ( defined $version and ! ref $version ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $version and not ref $version) { } 41 31 72
Condition66%2/3
defined $version and not ref $version
!ll&&!rl&&r
31041
MC/DC50%1/2
defined $version and not ref $version
defined $version not ref $version
Truth table66%2/3
A and not(B)
A= defined $versionB= not ref $version
ABresult
0X0
100
111
359 41 ▶ if ( $version eq 'sslv2' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($version eq 'sslv2') { } 0 41 41
elsif ($version eq 'sslv3') { } 41 0 41
elsif ($version eq 'tlsv1') { } 0 0 0
elsif ($version eq 'sslv23') { } 0 0 0
elsif ($version eq 'default') { } 0 0 0
360 0 $context = Net::SSLeay::CTX_v2_new();
361 } elsif ( $version eq 'sslv3' ) {
362 41 $context = Net::SSLeay::CTX_v3_new();
363 } elsif ( $version eq 'tlsv1' ) {
364 0 $context = Net::SSLeay::CTX_tlsv1_new();
365 } elsif ( $version eq 'sslv23' ) {
366 0 $context = Net::SSLeay::CTX_v23_new();
367 } elsif ( $version eq 'default' ) {
368 0 $context = Net::SSLeay::CTX_new();
369 } else {
370 0 die "unknown SSL version: $version";
371 }
372 } else {
373 31 $context = Net::SSLeay::CTX_new();
374 }
375 31 ▶ if ( ! defined $context ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $context) 0 31 31
376 0 die_now( "Failed to create SSL_CTX $!" );
377 0 return;
378 }
379  
380 # do we need to set options?
381 31 ▶ if ( defined $options ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options) 3 28 31
382 3 Net::SSLeay::CTX_set_options( $context, $options );
383 3 ▶ die_if_ssl_error( 'ssl ctx set options' ) if ! $IGNORE_SSL_ERRORS;
Branch: 1 branch
BranchTrueFalseTotal
unless $POE::Component::SSLify::IGNORE_SSL_ERRORS 3 0 3
384 }
385  
386 # do we need to set key/etc?
387 31 ▶ if ( defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 3 28 31
388 # Following will ask password unless private key is not encrypted
389 3 Net::SSLeay::CTX_use_RSAPrivateKey_file( $context, $key, FILETYPE_PEM );
390 3 ▶ die_if_ssl_error( 'private key' ) if ! $IGNORE_SSL_ERRORS;
Branch: 1 branch
BranchTrueFalseTotal
unless $POE::Component::SSLify::IGNORE_SSL_ERRORS 3 0 3
391 }
392  
393 # Set the cert file
394 31 ▶ if ( defined $cert ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cert) 3 28 31
395 3 Net::SSLeay::CTX_use_certificate_chain_file( $context, $cert );
396 3 ▶ die_if_ssl_error( 'certificate' ) if ! $IGNORE_SSL_ERRORS;
Branch: 1 branch
BranchTrueFalseTotal
unless $POE::Component::SSLify::IGNORE_SSL_ERRORS 3 0 3
397 }
398  
399 # All done!
400 31 return $context;
401 }
402  
403 #pod =func SSLify_GetCTX
404 #pod
405 #pod Returns the actual Net::SSLeay context object in case you wanted to play with it :)
406 #pod
407 #pod If passed in a socket, it will return that socket's $ctx instead of the global.
408 #pod
409 #pod my $ctx = SSLify_GetCTX(); # get the one set via SSLify_Options
410 #pod my $ctx = SSLify_GetCTX( $sslified_sock ); # get the one in the object
411 #pod =cut
412  
413 sub SSLify_GetCTX {
414 0 ▶ my $sock = shift;
Subroutine SSLify_GetCTX: not called CC=2 SCAR=17.9
Pod documented
415 0 ▶ if ( ! defined $sock ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $sock) { } 0 0 0
416 0 return $ctx;
417 } else {
418 0 return tied( *$sock )->{'ctx'};
419 }
420 }
421  
422 #pod =func SSLify_GetCipher
423 #pod
424 #pod Returns the cipher used by the SSLified socket
425 #pod
426 #pod print "SSL Cipher is: " . SSLify_GetCipher( $sslified_sock ) . "\n";
427 #pod
428 #pod NOTE: Doing this immediately after Client_SSLify or Server_SSLify will result in "(NONE)" because the SSL handshake
429 #pod is not done yet. The socket is nonblocking, so you will have to wait a little bit for it to get ready.
430 #pod
431 #pod apoc@blackhole:~/mygit/perl-poe-sslify/examples$ perl serverclient.pl
432 #pod got connection from: 127.0.0.1 - commencing Server_SSLify()
433 #pod SSLified: 127.0.0.1 cipher type: ((NONE))
434 #pod Connected to server, commencing Client_SSLify()
435 #pod SSLified the connection to the server
436 #pod Connected to SSL server
437 #pod Input: hola
438 #pod got input from: 127.0.0.1 cipher type: (AES256-SHA) input: 'hola'
439 #pod Got Reply: hola
440 #pod Input: ^C
441 #pod stopped at serverclient.pl line 126.
442 #pod =cut
443  
444 sub SSLify_GetCipher {
445 37 ▶ my $sock = shift;
Subroutine SSLify_GetCipher: called CC=1 SCAR=0.0
Pod documented
446 37 return Net::SSLeay::get_cipher( tied( *$sock )->{'ssl'} );
447 }
448  
449 #pod =func SSLify_GetSocket
450 #pod
451 #pod Returns the actual socket used by the SSLified socket, useful for stuff like getpeername()/getsockname()
452 #pod
453 #pod print "Remote IP is: " . inet_ntoa( ( unpack_sockaddr_in( getpeername( SSLify_GetSocket( $sslified_sock ) ) ) )[1] ) . "\n";
454 #pod =cut
455  
456 sub SSLify_GetSocket {
457 25 ▶ my $sock = shift;
Subroutine SSLify_GetSocket: called CC=1 SCAR=0.0
Pod documented
458 25 return tied( *$sock )->{'socket'};
459 }
460  
461 #pod =func SSLify_GetSSL
462 #pod
463 #pod Returns the actual Net::SSLeay object so you can call methods on it
464 #pod
465 #pod print Net::SSLeay::dump_peer_certificate( SSLify_GetSSL( $sslified_sock ) );
466 #pod =cut
467  
468 sub SSLify_GetSSL {
469 0 ▶ my $sock = shift;
Subroutine SSLify_GetSSL: not called CC=1 SCAR=6.9
Pod documented
470 0 return tied( *$sock )->{'ssl'};
471 }
472  
473 #pod =func SSLify_GetStatus
474 #pod
475 #pod Returns the status of the SSL negotiation/handshake/connection. See L<http://www.openssl.org/docs/ssl/SSL_connect.html#RETURN_VALUES>
476 #pod for more info.
477 #pod
478 #pod my $status = SSLify_GetStatus( $socket );
479 #pod -1 = still in negotiation stage ( or error )
480 #pod 0 = internal SSL error, connection will be dead
481 #pod 1 = negotiation successful
482 #pod =cut
483  
484 sub SSLify_GetStatus {
485 10 ▶ my $sock = shift;
Subroutine SSLify_GetStatus: called CC=1 SCAR=0.0
Pod documented
486 10 return tied( *$sock )->{'status'};
487 }
488  
489 14 1;
490  
491 __END__
↑ Summary blib/lib/POE/Component/SSLify/ServerHandle.pm »