|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# (C) Sergey Kandaurov |
| 4 | +# (C) Nginx, Inc. |
| 5 | + |
| 6 | +# Tests for http ssl module, ssl_verify_client with ssl_trusted_certificate. |
| 7 | + |
| 8 | +############################################################################### |
| 9 | + |
| 10 | +use warnings; |
| 11 | +use strict; |
| 12 | + |
| 13 | +use Test::More; |
| 14 | + |
| 15 | +use Socket qw/ CRLF /; |
| 16 | + |
| 17 | +BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 18 | + |
| 19 | +use lib 'lib'; |
| 20 | +use Test::Nginx qw/ :DEFAULT http_end /; |
| 21 | + |
| 22 | +############################################################################### |
| 23 | + |
| 24 | +select STDERR; $| = 1; |
| 25 | +select STDOUT; $| = 1; |
| 26 | + |
| 27 | +my $t = Test::Nginx->new()->has(qw/http http_ssl sni socket_ssl_sni/) |
| 28 | + ->has_daemon('openssl'); |
| 29 | + |
| 30 | +$t->write_file_expand('nginx.conf', <<'EOF'); |
| 31 | +
|
| 32 | +%%TEST_GLOBALS%% |
| 33 | +
|
| 34 | +daemon off; |
| 35 | +
|
| 36 | +events { |
| 37 | +} |
| 38 | +
|
| 39 | +http { |
| 40 | + %%TEST_GLOBALS_HTTP%% |
| 41 | +
|
| 42 | + add_header X-Verify x$ssl_client_verify:${ssl_client_cert}x; |
| 43 | + add_header X-Protocol $ssl_protocol; |
| 44 | +
|
| 45 | + ssl_session_cache shared:SSL:1m; |
| 46 | + ssl_session_tickets off; |
| 47 | +
|
| 48 | + server { |
| 49 | + listen 127.0.0.1:8443 ssl; |
| 50 | + server_name trusted; |
| 51 | +
|
| 52 | + ssl_certificate_key 1.example.com.key; |
| 53 | + ssl_certificate 1.example.com.crt; |
| 54 | +
|
| 55 | + ssl_verify_client on; |
| 56 | + ssl_trusted_certificate 2.example.com.crt; |
| 57 | + } |
| 58 | +} |
| 59 | +
|
| 60 | +EOF |
| 61 | + |
| 62 | +$t->write_file('openssl.conf', <<EOF); |
| 63 | +[ req ] |
| 64 | +default_bits = 2048 |
| 65 | +encrypt_key = no |
| 66 | +distinguished_name = req_distinguished_name |
| 67 | +[ req_distinguished_name ] |
| 68 | +EOF |
| 69 | + |
| 70 | +my $d = $t->testdir(); |
| 71 | + |
| 72 | +foreach my $name ('1.example.com', '2.example.com', '3.example.com') { |
| 73 | + system('openssl req -x509 -new ' |
| 74 | + . "-config $d/openssl.conf -subj /CN=$name/ " |
| 75 | + . "-out $d/$name.crt -keyout $d/$name.key " |
| 76 | + . ">>$d/openssl.out 2>&1") == 0 |
| 77 | + or die "Can't create certificate for $name: $!\n"; |
| 78 | +} |
| 79 | + |
| 80 | +sleep 1 if $^O eq 'MSWin32'; |
| 81 | + |
| 82 | +$t->write_file('t', 'SEE-THIS'); |
| 83 | + |
| 84 | +$t->try_run('wants ssl_client_certificate')->plan(3); |
| 85 | + |
| 86 | +############################################################################### |
| 87 | + |
| 88 | +like(get('trusted', '2.example.com'), qr/SUCCESS/, 'good cert trusted only'); |
| 89 | +like(get('trusted', '3.example.com'), qr/400 Bad/, 'bad cert trusted only'); |
| 90 | + |
| 91 | +my $ca = join ' ', get('trusted', '2.example.com'); |
| 92 | +is($ca, '', 'no ca sent trusted only'); |
| 93 | + |
| 94 | +############################################################################### |
| 95 | + |
| 96 | +sub get { |
| 97 | + my ($sni, $cert, $host) = @_; |
| 98 | + |
| 99 | + $host = $sni if !defined $host; |
| 100 | + |
| 101 | + my $s = http( |
| 102 | + "GET /t HTTP/1.0" . CRLF . |
| 103 | + "Host: $host" . CRLF . CRLF, |
| 104 | + start => 1, |
| 105 | + SSL => 1, |
| 106 | + SSL_hostname => $sni, |
| 107 | + SSL_version => 'SSLv23', |
| 108 | + SSL_cipher_list => 'ALL:@SECLEVEL=0', |
| 109 | + $cert ? ( |
| 110 | + SSL_cert_file => "$d/$cert.crt", |
| 111 | + SSL_key_file => "$d/$cert.key" |
| 112 | + ) : () |
| 113 | + ); |
| 114 | + |
| 115 | + return http_end($s) unless wantarray(); |
| 116 | + |
| 117 | + # Note: this uses IO::Socket::SSL::_get_ssl_object() internal method. |
| 118 | + # While not exactly correct, it looks like there is no other way to |
| 119 | + # obtain CA list with IO::Socket::SSL, and this seems to be good |
| 120 | + # enough for tests. |
| 121 | + |
| 122 | + my $ssl = $s->_get_ssl_object(); |
| 123 | + my $list = Net::SSLeay::get_client_CA_list($ssl); |
| 124 | + my @names; |
| 125 | + for my $i (0 .. Net::SSLeay::sk_X509_NAME_num($list) - 1) { |
| 126 | + my $name = Net::SSLeay::sk_X509_NAME_value($list, $i); |
| 127 | + push @names, Net::SSLeay::X509_NAME_oneline($name); |
| 128 | + } |
| 129 | + return @names; |
| 130 | +} |
| 131 | + |
| 132 | +############################################################################### |
0 commit comments