NAME

Net::mbedTLS - mbedTLS in Perl

SYNOPSIS

my $fh = IO::Socket::INET->new("example.com:12345");

my $mbedtls = Net::mbedTLS->new();

my $client = $mbedtls->create_client($fh);

# Optional, but useful to do separately if, e.g., you want
# to report a successful handshake.
$client->shake_hands();

# Throws if the error is an “unexpected” one:
my $input = "\0" x 23;
my $got = $client->read($input) // do {

    # We get here if, e.g., the socket is non-blocking and we
    # weren’t ready to read.
};

# Similar to read(); throws on “unexpected” errors:
my $wrote = $tls->write($byte_string) // do {
    # ...
};

DESCRIPTION

OpenSSL is great but rather large.

This distribution allows use of mbedTLS, a smaller, simpler TLS library, from Perl.

BENEFITS & LIABILITIES

This library, like mbedTLS itself, minimizes memory usage at the cost of performance. After a simple TLS handshake with this library Perl’s memory usage is about 6.5 MiB lower than when using IO::Socket::SSL for the same. On the other hand, OpenSSL does the handshake (as of this writing) about 18 times faster.

AVAILABLE FUNCTIONALITY

For now this module largely just exposes the ability to do TLS. mbedTLS itself exposes a good deal more functionality like raw crypto and configurable ciphers; if you want that stuff, file a feature request. (A patch with a test is highly desirable!)

BUILDING/LINKING

This library can link to mbedTLS in several ways:

Dynamic linking allows Net::mbedTLS to use the most recent (compatible) mbedTLS but requires you to have a shared mbedTLS available, whereas static linking alleviates that dependency at the cost of always using the same library version.

mbedTLS, alas, as of this writing does not support pkg-config. (GitHub issue) If that changes then dynamic linking may become more reliable.

NB: mbedTLS MUST be built with position-independent code. If you’re building your own mbedTLS then you’ll need to configure that manually. GCC’s -fPIC flag does this; see this distribution’s CI tests for an example.

METHODS

$obj = CLASS->new( %OPTS )

Instantiates this class. %OPTS are:

$client = OBJ->create_client( $SOCKET, %OPTS )

Initializes a client session on $SOCKET. Returns a Net::mbedTLS::Client instance.

%OPTS are:

$client = OBJ->create_server( $SOCKET, %OPTS )

Initializes a server session on $SOCKET. Returns a Net::mbedTLS::Server instance.

%OPTS are:

CONSTANTS

These come from mbedTLS:

SEE ALSO

Net::SSLeay, an XS binding to OpenSSL, is Perl’s de facto standard TLS library.

IO::Socket::SSL wraps Net::SSLeay with logic to make TLS almost as easy to use as plain TCP.

#----------------------------------------------------------------------

LICENSE & COPYRIGHT

Copyright 2022 Gasper Software Consulting. All rights reserved.

This library is licensed under the same terms as Perl itself. See perlartistic.

This library was originally a research project at cPanel, L.L.C..