Future-IO-TLS
view release on metacpan or search on metacpan
lib/Future/IO/TLS.pm view on Meta::CPAN
my ($inner, $pipe) = Crypt::OpenSSL3::BIO->new_bio_pair(8192, 8192);
$ssl->set_rbio($inner);
$ssl->set_wbio($inner);
$ssl->set_mode(Crypt::OpenSSL3::SSL::MODE_ENABLE_PARTIAL_WRITE | Crypt::OpenSSL3::SSL::MODE_ACCEPT_MOVING_WRITE_BUFFER);
if (my $hostname = $options{hostname}) {
$ssl->set_verify(Crypt::OpenSSL3::SSL::VERIFY_PEER);
$ssl->set_tlsext_host_name($hostname);
$ssl->set_host($hostname);
}
$ssl->use_PrivateKey_file($options{private_key_file}, Crypt::OpenSSL3::SSL::FILETYPE_PEM) if $options{private_key_file};
$ssl->use_certificate_chain_file($options{certificate_chain_file}) if $options{certificate_chain_file};
my $set_state_method = $options{server} ? 'set_accept_state' : 'set_connect_state';
$ssl->$set_state_method;
while (1) {
my $ret = $ssl->do_handshake;
last if $ret >= 0;
if (my $pending = $pipe->pending) {
lib/Future/IO/TLS.pm view on Meta::CPAN
If true the connection will take the accepting role in the handshake, otherwise it will take the connecting role.
=item * context
An L<TLS Context|Crypt::OpenSSL3::SSL::Context> used to base connections on.
=item * hostname
The hostname of the other side of the connection. Typically used for client connections.
=item * private_key_file
The location of the private key file. Typically used for server connections.
=item * certificate_chain_file
The location of the certificate chain file. Typically used for server connections.
=back
=head2 connect
my $tls = Future::IO::TLS->connect($fh, $sockaddr, %options);
This combines C<< Future::IO->connect >> with C<< Future::IO::TLS->start_TLS >>. You probably want to pass this a C<hostname> parameter, otherwise the peer's identity can't be verified.
=head2 accept
my $tls = Future::IO::TLS->accept($fh, $sockaddr, %options);
This combines C<< Future::IO->accept >> with C<< Future::IO::TLS->start_TLS >>. You probably want to pass this the C<private_key_file> and C<certificate_chain_file> arguments.
=head2 read
my $data = await $io->read($fh, $size);
Read C<$size> bytes from C<$fh> using TLS.
=head2 write
my $written = await $io->write($fh, $data);
t/10-basic.t view on Meta::CPAN
use IO::Socket::UNIX;
my $context = Crypt::OpenSSL3::SSL::Context->new;
$context->load_verify_file('t/server.crt');
my ($left, $right) = IO::Socket::UNIX->socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC) or die "$!";
$_->blocking(0) for $left, $right;
async sub main() {
my $connecting = Future::IO::TLS->start_TLS($left, context => $context, hostname => 'server');
my $accepting = Future::IO::TLS->start_TLS($right, server => 1, private_key_file => 't/server.key', certificate_chain_file => 't/server.crt');
my $io = await $connecting;
ok await $io->write($left, "Hello, world!");
my $io2 = await $accepting;
my $received = await $io2->read($right, 1024);
is $received, 'Hello, world!';
}
alarm 3;
main()->get;
( run in 0.626 second using v1.01-cache-2.11-cpan-63428c044ed )