Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
100 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package Mojo::IOLoop::TLS; | ||
use Mojo::Base 'Mojo::EventEmitter'; | ||
|
||
use Mojo::File 'path'; | ||
use Mojo::IOLoop; | ||
use Scalar::Util 'weaken'; | ||
|
||
# TLS support requires IO::Socket::SSL | ||
use constant TLS => $ENV{MOJO_NO_TLS} | ||
? 0 | ||
: eval 'use IO::Socket::SSL 1.94 (); 1'; | ||
use constant TLS_READ => TLS ? IO::Socket::SSL::SSL_WANT_READ() : 0; | ||
use constant TLS_WRITE => TLS ? IO::Socket::SSL::SSL_WANT_WRITE() : 0; | ||
|
||
has reactor => sub { Mojo::IOLoop->singleton->reactor }; | ||
|
||
# To regenerate the certificate run this command (18.04.2012) | ||
# openssl req -new -x509 -keyout server.key -out server.crt -nodes -days 7300 | ||
my $CERT = path(__FILE__)->dirname->child('resources', 'server.crt')->to_string; | ||
my $KEY = path(__FILE__)->dirname->child('resources', 'server.key')->to_string; | ||
|
||
sub DESTROY { | ||
my $self = shift; | ||
return unless my $reactor = $self->reactor; | ||
$reactor->remove($self->{handle}) if $self->{handle}; | ||
} | ||
|
||
sub has_tls {TLS} | ||
|
||
sub negotiate { | ||
my ($self, $args) = (shift, ref $_[0] ? $_[0] : {@_}); | ||
|
||
return $self->emit(error => 'IO::Socket::SSL 1.94+ required for TLS support') | ||
unless TLS; | ||
|
||
weaken $self; | ||
my $tls = { | ||
SSL_cert_file => $args->{tls_cert} || $CERT, | ||
SSL_error_trap => sub { $self->emit(error => $_[1]) }, | ||
SSL_honor_cipher_order => 1, | ||
SSL_key_file => $args->{tls_key} || $KEY, | ||
SSL_startHandshake => 0, | ||
SSL_verify_mode => $args->{tls_verify} // ($args->{tls_ca} ? 0x03 : 0x00) | ||
}; | ||
$tls->{SSL_ca_file} = $args->{tls_ca} | ||
if $args->{tls_ca} && -T $args->{tls_ca}; | ||
$tls->{SSL_cipher_list} = $args->{tls_ciphers} if $args->{tls_ciphers}; | ||
$tls->{SSL_version} = $args->{tls_version} if $args->{tls_version}; | ||
|
||
my $handle = $args->{handle}; | ||
return $self->emit(error => "TLS upgrade failed: $IO::Socket::SSL::SSL_ERROR") | ||
unless IO::Socket::SSL->start_SSL($handle, %$tls, SSL_server => 1); | ||
$self->reactor->io($self->{handle} = $handle => sub { $self->_tls($handle) }); | ||
} | ||
|
||
sub _tls { | ||
my ($self, $handle) = @_; | ||
|
||
return $self->emit(negotiated => delete $self->{handle}) | ||
if $handle->accept_SSL; | ||
|
||
# Switch between reading and writing | ||
my $err = $IO::Socket::SSL::SSL_ERROR; | ||
if ($err == TLS_READ) { $self->reactor->watch($handle, 1, 0) } | ||
elsif ($err == TLS_WRITE) { $self->reactor->watch($handle, 1, 1) } | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters