The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

Name

SPVM::Net::SSLeay - SPVM bindings for OpenSSL

Description

The Net::SSLeay class of SPVM has methods to call OpenSSL functions.

Usage

use Net::SSLeay;

Fields

operation_error

has operation_error : ro int;

error

has error : ro long;

Class Methods

new

static method new : Net::SSLeay ($ssl_ctx : Net::SSLeay::SSL_CTX);

Instance Methods

DESTROY

method DESTROY : void ();

set_fd

method set_fd : int ($fd : int);

connect

method connect : int ();

accept

method accept : int ();

shutdown

method shutdown : int ();

set_tlsext_host_name

method set_tlsext_host_name : int ($name : string);

read

method read : int ($buf : mutable string, $num : int = -1, $offset : int = 0);

peek

method peek : int ($buf : mutable string, $num : int = -1, $offset : int = 0);

write

method write : int ($buf : string, $num : int = -1, $offset : int = 0);

Examples

HTTPS Client:

# Socket
my $host = "www.google.com";
my $port = 443;
my $socket = IO::Socket::INET->new({PeerAddr => $host, PeerPort => $port});

my $ssl_method = Net::SSLeay::SSL_METHOD->SSLv23_client_method;

my $ssl_ctx = Net::SSLeay::SSL_CTX->new($ssl_method);

$ssl_ctx->set_verify(SSL->SSL_VERIFY_PEER);

$ssl_ctx->set_default_verify_paths;

my $verify_param = $ssl_ctx->get0_param;

$verify_param->set_hostflags(SSL->X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);

$verify_param->set1_host($host);

my $ssl = Net::SSLeay->new($ssl_ctx);

my $socket_fileno = $socket->fileno;

$ssl->set_fd($socket_fileno);

$ssl->set_tlsext_host_name($host);

$ssl->connect;

my $send_buffer = "GET / HTTP/1.0\r\n\r\n";
$ssl->write($send_buffer);

my $buffer = StringBuffer->new;

my $recv_buffer = (mutable string)new_string_len 100;
while (1) {
  my $recv_length = $ssl->read($recv_buffer);
  
  if ($recv_length > 0) {
    $buffer->push($recv_buffer, 0, $recv_length);
    # print $recv_buffer;
  }
  
  if ($recv_length < 0) {
    die "Read error";
  }
  
  if ($recv_length < length $recv_buffer) {
    last;
  }
}

my $shutdown_ret = $ssl->shutdown;

if ($shutdown_ret == 0) {
  while (1) {
    my $recv_buffer = (mutable string)new_string_len 100;
    my $read_length = $ssl->read($recv_buffer);
    if ($read_length <= 0) {
      last;
    }
  }
}

$socket->close;

Modules

Repository

SPVM::Net::SSLeay - Github

Author

Yuki Kimoto<kimoto.yuki@gmail.com>

Copyright & License

Copyright (c) 2023 Yuki Kimoto

MIT License