| 1 | package IO::Stream::MatrixSSL::Server; | ||
| 2 | |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | ▶ | use version; our $VERSION = qv('1.1.0'); # update POD & Changes & README |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | # update DEPENDENCIES in POD & Makefile.PL & README | ||
| 10 | 1 | ▶ | use IO::Stream::const; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use IO::Stream::MatrixSSL::const; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use Crypt::MatrixSSL 1.83; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | use Scalar::Util qw( weaken ); | ||
| 14 | |||
| 15 | use base qw( IO::Stream::MatrixSSL ); | ||
| 16 | |||
| 17 | sub new { | ||
| 18 | my ($class, $opt) = @_; | ||
| 19 | croak '{crt} and {key} required' | ||
| 20 | if !defined $opt->{crt} || !defined $opt->{key}; | ||
| 21 | my $self = bless { | ||
| 22 | crt => undef, # filename(s) with server certificate(s) | ||
| 23 | key => undef, # filename with server private key | ||
| 24 | pass => undef, # password to decrypt private key | ||
| 25 | trusted_CA => undef, # filename(s) with trusted root CA cert(s) | ||
| 26 | cb => undef, # callback for validating certificate | ||
| 27 | %{$opt}, | ||
| 28 | out_buf => q{}, # modified on: OUT | ||
| 29 | out_pos => undef, # modified on: OUT | ||
| 30 | out_bytes => 0, # modified on: OUT | ||
| 31 | in_buf => q{}, # modified on: IN | ||
| 32 | in_bytes => 0, # modified on: IN | ||
| 33 | ip => undef, # modified on: RESOLVED | ||
| 34 | is_eof => undef, # modified on: EOF | ||
| 35 | _param => [], # param for cb | ||
| 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 => undef, | ||
| 40 | _t => undef, | ||
| 41 | _cb_t => undef, | ||
| 42 | }, $class; | ||
| 43 | my $this = $self; | ||
| 44 | weaken($this); | ||
| 45 | $self->{_cb_t} = sub { $this->T() }; | ||
| 46 | # Initialize SSL. | ||
| 47 | # TODO OPTIMIZATION Cache {_ssl_keys}. | ||
| 48 | matrixSslReadKeys($self->{_ssl_keys}, $self->{crt}, $self->{key}, | ||
| 49 | $self->{pass}, $self->{trusted_CA}) | ||
| 50 | == 0 or croak 'matrixSslReadKeys: wrong {crt}, {key}, {pass} or {trusted_CA}?'; | ||
| 51 | my $flags = $SSL_FLAGS_SERVER; | ||
| 52 | if (defined $self->{trusted_CA}) { | ||
| 53 | $flags |= $SSL_FLAGS_CLIENT_AUTH; | ||
| 54 | } | ||
| 55 | matrixSslNewSession($self->{_ssl}, $self->{_ssl_keys}, | ||
| 56 | undef, $flags) | ||
| 57 | == 0 or croak 'matrixSslNewSession'; | ||
| 58 | # Prepare first param for cb. | ||
| 59 | $self->{_param}[0] = $self; | ||
| 60 | weaken $self->{_param}[0]; | ||
| 61 | if (defined $self->{cb}) { | ||
| 62 | matrixSslSetCertValidator($self->{_ssl}, $self->{cb}, $self->{_param}); | ||
| 63 | } | ||
| 64 | return $self; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub PREPARE { | ||
| 68 | my ($self, $fh, $host, $port) = @_; | ||
| 69 | if (!defined $host) { # ... else timer will be set on CONNECTED | ||
| 70 | $self->{_t} = EV::timer(TOHANDSHAKE, 0, $self->{_cb_t}); | ||
| 71 | } | ||
| 72 | # Prepare second param for cb. | ||
| 73 | my $io = $self; | ||
| 74 | while ($io->{_master}) { | ||
| 75 | $io = $io->{_master}; | ||
| 76 | } | ||
| 77 | $self->{_param}[1] = $io; | ||
| 78 | weaken $self->{_param}[1]; | ||
| 79 | $self->{_slave}->PREPARE($fh, $host, $port); | ||
| 80 | return; | ||
| 81 | } | ||
| 82 | |||
| 83 | |||
| 84 | 1; | ||