File Coverage

blib/lib/IO/Stream/MatrixSSL/Server.pm
Criterion Covered Total %
statement 48 48 100.0
branch 8 12 66.6
condition 2 5 40.0
subroutine 14 14 100.0
pod 1 2 50.0
total 73 81 90.1


line stmt bran cond sub pod time code
1             package IO::Stream::MatrixSSL::Server;
2 9     9   129 use 5.010001;
  9         27  
3 9     9   37 use warnings;
  9         12  
  9         176  
4 9     9   33 use strict;
  9         17  
  9         130  
5 9     9   32 use utf8;
  9         13  
  9         34  
6 9     9   163 use Carp;
  9         28  
  9         589  
7              
8             our $VERSION = 'v2.0.2';
9              
10 9     9   58 use IO::Stream::const;
  9         27  
  9         56  
11 9     9   1259 use IO::Stream::MatrixSSL::const;
  9         14  
  9         68  
12 9     9   44 use Crypt::MatrixSSL3 qw( :all );
  9         17  
  9         45  
13 9     9   7641 use Scalar::Util qw( weaken );
  9         16  
  9         485  
14              
15 9     9   53 use parent qw( -norequire IO::Stream::MatrixSSL );
  9         39  
  9         48  
16              
17              
18             sub new {
19 4     4 1 3970 my ($class, $opt) = @_;
20             croak '{crt} and {key} required'
21 4 50 33     37 if !defined $opt->{crt} || !defined $opt->{key};
22             my $self = bless {
23             crt => undef, # filename(s) with server's certificate(s)
24             key => undef, # filename with server's private key
25             pass => undef, # password to decrypt private key
26             trusted_CA => undef, # filename(s) with trusted root CA cert(s)
27             cb => undef, # callback for validating certificate
28 4   50     13 %{$opt // {}},
  4         58  
29             out_buf => q{}, # modified on: OUT
30             out_pos => undef, # modified on: OUT
31             out_bytes => 0, # modified on: OUT
32             in_buf => q{}, # modified on: IN
33             in_bytes => 0, # modified on: IN
34             ip => undef, # modified on: RESOLVED
35             is_eof => undef, # modified on: EOF
36             _ssl => undef, # MatrixSSL 'session' object
37             _ssl_keys => undef, # MatrixSSL 'keys' object
38             _handshaked => 0, # flag, will be true after handshake
39             _want_write => 0, # flag, will be true if write() was called before handshake
40             _want_close => 0, # flag, will be true after generating MATRIXSSL_REQUEST_CLOSE
41             _closed => 0, # flag, will be true after sending MATRIXSSL_REQUEST_CLOSE
42             _t => undef,
43             _cb_t => undef,
44             }, $class;
45 4         19 weaken(my $this = $self);
46 4 50   2   37 $self->{_cb_t} = sub { $this && $this->T() };
  2         99228  
47             my $cb = !$self->{cb} ? undef : sub {
48 1 50   1   9 $this ? $this->{cb}->($this, @_) : CERTVALIDATOR_INTERNAL_ERROR
49 4 100       20 };
50             # Initialize SSL.
51             # TODO OPTIMIZATION Cache {_ssl_keys}.
52 4         27 $self->{_ssl_keys} = Crypt::MatrixSSL3::Keys->new();
53             my $rc = $self->{_ssl_keys}->load_rsa(
54             $self->{crt}, $self->{key}, $self->{pass}, $self->{trusted_CA}
55 4         1400 );
56 4 50       23 croak 'ssl error: '.get_ssl_error($rc) if $rc != PS_SUCCESS;
57 4         55 $self->{_ssl} = Crypt::MatrixSSL3::Server->new($self->{_ssl_keys}, $cb);
58 4         150 return $self;
59             }
60              
61             sub PREPARE {
62 4     4 0 350 my ($self, $fh, $host, $port) = @_;
63 4 100       14 if (!defined $host) { # ... else timer will be set on CONNECTED
64 3         22 $self->{_t} = EV::timer(TOHANDSHAKE, 0, $self->{_cb_t});
65             }
66 4         17 $self->{_slave}->PREPARE($fh, $host, $port);
67 4         49 return;
68             }
69              
70              
71             1;