Wednesday 21 December 2011

Issue creating SSL socket with Perl -- IO::Socket::SSL

Issue creating SSL socket with Perl -- IO::Socket::SSL

Hello,

I am new to Perl and I'm attempting to interface with Virtuozzo's XML API and seem to be encountering issues establishing a SSL socket -- the SSL handshake is failing.

Error:

Code:

[root@srv perl]# perl wind0ze_api.pl
Connecting to Agent...

DEBUG: .../IO/Socket/SSL.pm:1545: new ctx 41090720
DEBUG: .../IO/Socket/SSL.pm:334: socket not yet connected
DEBUG: .../IO/Socket/SSL.pm:336: socket connected
DEBUG: .../IO/Socket/SSL.pm:349: ssl handshake not started
DEBUG: .../IO/Socket/SSL.pm:392: Net::SSLeay::connect -> 0
DEBUG: .../IO/Socket/SSL.pm:440: connection failed - connect returned 0
DEBUG: .../IO/Socket/SSL.pm:1276: SSL connect attempt failed because of handshake problemserror:00000000:lib(0):func(0):reason(0)

DEBUG: .../IO/Socket/SSL.pm:1276: IO::Socket::INET configuration failederror:00000000:lib(0):func(0):reason(0)

DEBUG: .../IO/Socket/SSL.pm:1582: free ctx 41090720 open=41090720
DEBUG: .../IO/Socket/SSL.pm:1590: OK free ctx 41090720
Error establishing SSL socket at wind0ze_api.pl line 21.
[root@srv perl]#

When I attempt to establish a non-SSL socket it works without an issue.

I have also tested the connection via openssl CLI and it works, so I'm certain it's not related to any packet filtering.

Code:

openssl s_client -debug -connect $IP:4434 -cipher ADH-AES256-SHA
^ WORKS

However, if I omit the '-cipher' option then openssl also will yield a handshake error:

Code:

[root@srv perl]# openssl s_client -debug -connect $IP:4434
CONNECTED(00000003)
write to 0xbcea350 [0xbcead90] (121 bytes => 121 (0x79))
0000 - 80 77 01 03 01 00 4e 00-00 00 20 00 00 39 00 00  .w....N... ..9..
0010 - 38 00 00 35 00 00 16 00-00 13 00 00 0a 07 00 c0  8..5............
0020 - 00 00 33 00 00 32 00 00-2f 03 00 80 00 00 05 00  ..3..2../.......
0030 - 00 04 01 00 80 00 00 15-00 00 12 00 00 09 06 00  ................
0040 - 40 00 00 14 00 00 11 00-00 08 00 00 06 04 00 80  @...............
0050 - 00 00 03 02 00 80 00 00-ff 0b ca 9e 3e e5 69 5f  ............>.i_
0060 - 38 f5 ad 54 98 29 45 21-78 40 07 20 34 d8 39 fc  8..T.)E!x@. 4.9.
0070 - b0 85 99 a3 d6 b4 64 42-1d                        ......dB.
read from 0xbcea350 [0xbcf02f0] (7 bytes => 0 (0x0))
26080:error:140790E5:SSL routines:SSL23_WRITE:ssl handshake failure:s23_lib.c:188:
[root@srv perl]#

Code:

#!/usr/bin/perl -w
use strict;
use IO::Socket::INET;
use IO::Socket::SSL qw(debug3);
use Net::SSLeay;
use Socket;

use constant CONF_CONNECTION => {
      ip => '$IP',
      port => '4434',
      class => 'IO::Socket::SSL'
};

# Null-terminating character (packet separator).

use constant MSG_TERMINATOR => "\0";
local $/ = &MSG_TERMINATOR;


print "Connecting to Agent...\n\n";
our $socket = new IO::Socket::SSL(
PeerAddr => &CONF_CONNECTION->{ip},
PeerPort=> &CONF_CONNECTION->{port},
Proto => 'tcp',
Reuse => 1) || die "Error establishing SSL socket";

my $ssl_sock = IO::Socket::SSL->start_SSL(
SSL_verify_mode => 0,
$socket);

my $hello = $ssl_sock->getline;
chomp($hello);
print $hello;

P.S. numeric value of $IP omitted for security reasons. :)

I have also read the IO::Socket:: CPAN pages but it's a lot of information to take in and isn't written in the most user-friendly format. I would appreciate any pointers.

No comments:

Post a Comment