522272
From 6b05dc28e94e90ab4852c9977d7fbe66fec6cd48 Mon Sep 17 00:00:00 2001
522272
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
522272
Date: Fri, 8 Feb 2019 14:50:32 +0100
522272
Subject: [PATCH] Test client performs Post-Handshake-Authentication
522272
MIME-Version: 1.0
522272
Content-Type: text/plain; charset=UTF-8
522272
Content-Transfer-Encoding: 8bit
522272
522272
This test uses openssl tool because PHA is not yet supported by
522272
IO::Socket::SSL's server implementation. The openssl tool uses a fixed
522272
port. So the test can fail.
522272
522272
Signed-off-by: Petr Písař <ppisar@redhat.com>
522272
---
522272
 MANIFEST       |  1 +
522272
 t/pha_client.t | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++
522272
 2 files changed, 91 insertions(+)
522272
 create mode 100755 t/pha_client.t
522272
522272
diff --git a/MANIFEST b/MANIFEST
522272
index 20cddb6..2b8328d 100644
522272
--- a/MANIFEST
522272
+++ b/MANIFEST
522272
@@ -57,6 +57,7 @@ t/mitm.t
522272
 t/multiple-cert-rsa-ecc.t
522272
 t/nonblock.t
522272
 t/npn.t
522272
+t/pha_client.t
522272
 t/plain_upgrade_downgrade.t
522272
 t/protocol_version.t
522272
 t/public_suffix_lib_encode_idn.t
522272
diff --git a/t/pha_client.t b/t/pha_client.t
522272
new file mode 100755
522272
index 0000000..2413588
522272
--- /dev/null
522272
+++ b/t/pha_client.t
522272
@@ -0,0 +1,90 @@
522272
+#!/usr/bin/perl
522272
+use strict;
522272
+use warnings;
522272
+use Test::More;
522272
+use IPC::Run ();
522272
+use IO::Socket::SSL ();
522272
+use Net::SSLeay ();
522272
+use IO::Select ();
522272
+
522272
+if (system('openssl', 'version')) {
522272
+    plan skip_all => 'openssl tool is not available';
522272
+} elsif (!defined &Net::SSLeay::CTX_set_post_handshake_auth) {
522272
+    plan skip_all => 'Net::SSLeay does not expose PHA';
522272
+} else {
522272
+    plan tests => 5;
522272
+}
522272
+
522272
+my $port = 2000;
522272
+my $ca_cert = 'certs/test-ca.pem';
522272
+
522272
+diag 'Starting a server';
522272
+my ($server, $input, $stdout, $stderr);
522272
+eval {
522272
+    $server = IPC::Run::start(['openssl', 's_server', '-port', $port,
522272
+            '-Verify', '1',
522272
+            '-cert', 'certs/server-wildcard.pem',
522272
+            '-key', 'certs/server-wildcard.pem', '-CAfile', $ca_cert],
522272
+        \$input, \$stdout, \$stderr);
522272
+    # subsequent \undef does not work
522272
+    # <https://github.com/toddr/IPC-Run/issues/124>
522272
+};
522272
+if (!$server or $@) {
522272
+    BAIL_OUT("Could not start a server: $@");
522272
+}
522272
+# openssl s_server does not return a non-zero exit code in case of bind(2) failure.
522272
+while ($server->pumpable && $stdout !~ /\nACCEPT\n/) { $server->pump; }
522272
+if ($stderr =~ /unable to bind socket/) {
522272
+    $server->kill_kill;
522272
+    BAIL_OUT("Could not start a server: $stderr");
522272
+}
522272
+ok($server, 'Server started');
522272
+
522272
+my $client = IO::Socket::SSL->new(
522272
+    PeerHost => 'localhost',
522272
+    PeerPort => $port,
522272
+    SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_PEER,
522272
+    SSL_verifycn_scheme => 'www',
522272
+    SSL_verifycn_name => 'www.server.local',
522272
+    SSL_ca_file => $ca_cert,
522272
+    SSL_key_file => 'certs/client-key.pem',
522272
+    SSL_cert_file => 'certs/client-cert.pem'
522272
+);
522272
+ok($client, 'Client connected');
522272
+
522272
+SKIP: {
522272
+    skip "Connection failed: errno=$!, SSL errror=$IO::Socket::SSL::SSL_ERROR", 2
522272
+        unless $client;
522272
+    $client->blocking(0);
522272
+
522272
+    SKIP: {
522272
+        # Ask openssl s_server for PHA request and wait for the result.
522272
+        $input .= "c\n";
522272
+        while ($server->pumpable &&
522272
+            $stderr !~ /SSL_verify_client_post_handshake/ &&
522272
+            $stdout !~ /SSL_do_handshake -> 1/
522272
+        ) {
522272
+            # Push the PHA command to the server and read outputs.
522272
+            $server->pump;
522272
+
522272
+            # Client also must perform I/O to process the PHA request.
522272
+            my $select = IO::Select->new($client);
522272
+            while ($select->can_read(1)) {  # 1 second time-out because of
522272
+                                            # blocking IPC::Run
522272
+                my $retval = $client->read(my $buf, 1);
522272
+                if (defined $buf and $buf eq 'c') {
522272
+                    skip 'openssl tool does not support PHA command', 1;
522272
+                }
522272
+            }
522272
+        }
522272
+        ok($stdout =~ /SSL_do_handshake -> 1/, 'Client performed PHA');
522272
+    }
522272
+
522272
+    ok($client->close, 'Client disconnected');
522272
+}
522272
+
522272
+eval {
522272
+    $server->kill_kill;
522272
+};
522272
+ok(!$@, 'Server terminated');
522272
+
522272
-- 
522272
2.20.1
522272