NAME

Protocol::TLS::Client - pure Perl TLS Client

SYNOPSIS

use Protocol::TLS::Client;

# Create client object
my $client = Protocol::TLS::Client->new();

# You must create tcp connection yourself
my $cv = AE::cv;
tcp_connect 'example.com', 443, sub {
    my $fh = shift or do {
        warn "error: $!\n";
        $cv->send;
        return;
    };
    
    # socket handling
    my $h;
    $h = AnyEvent::Handle->new(
        fh       => $fh,
        on_error => sub {
            $_[0]->destroy;
            print "connection error\n";
            $cv->send;
        },
        on_eof => sub {
            $h->destroy;
            print "that's all folks\n";
            $cv->send;
        },
    );


    # Create new TLS-connection object
    my $con = $client->new_connection(

        # SERVER NAME (FQDN)
        'example.com',

        # Callback executed when TLS-handshake finished
        on_handshake_finish => sub {
            my ($tls) = @_;

            # Send some application data
            $tls->send("hi there\n");
        },
        
        # Callback executed when application data received
        on_data => sub {
            my ( $tls, $data ) = @_;
            print $data;
            
            # send close notify and close application level connection
            $tls->close;
        }
    );

    # Handshake start
    # Send TLS records to socket
    while ( my $record = $con->next_record ) {
        $h->push_write($record);
    }

    # low level socket operations (read/write)
    $h->on_read(
        sub {
            my $handle = shift;
            
            # read TLS records from socket and put them to $con object
            $con->feed( $handle->{rbuf} );
            $handle->{rbuf} = '';

            # write TLS records to socket
            while ( my $record = $con->next_record ) {
                $handle->push_write($record);
            }

            # Terminate connection if all done
            $handle->push_shutdown if $con->shutdown;
            ();
        }
    );
    ();
};

# finish
$cv->recv;

DESCRIPTION

Protocol::TLS::Client is TLS client library. It's intended to make TLS-client implementations on top of your favorite event loop.

METHODS

new

Initialize new client object

my $client = Procotol::TLS::Client->new( %options );

Availiable options:

cert_file => /path/to/cert.crt

Path to client certificate to perform client to server authentication

key_file => /path/to/cert.key

Path to private key for client certificate

new_connection

Create new TLS-connection object

my $con = $client->new_connection( 'F.Q.D.N', %options );

'F.Q.D.N' - fully qualified domain name

%options - options hash

Availiable options:

on_handshake_finish => sub { ... }

Callback invoked when TLS handshake completed

on_handshake_finish => sub {
    my ($tls) = @_;

    # Send some application data
    $tls->send("hi there\n");
},
on_data => sub { ... }

Callback executed when application data received

on_data => sub {
    my ( $tls, $data ) = @_;
    print $data;

    # send close notify and close application level connection
    $tls->close;
}

LICENSE

Copyright (C) Vladimir Lettiev.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

Vladimir Lettiev <thecrux@gmail.com>