|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# (C) Sergey Kandaurov |
| 4 | +# (C) Nginx, Inc. |
| 5 | + |
| 6 | +# Tests for http uwsgi module with variables in ssl certificates. |
| 7 | + |
| 8 | +############################################################################### |
| 9 | + |
| 10 | +use warnings; |
| 11 | +use strict; |
| 12 | + |
| 13 | +use Test::More; |
| 14 | + |
| 15 | +BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 16 | + |
| 17 | +use lib 'lib'; |
| 18 | +use Test::Nginx; |
| 19 | + |
| 20 | +############################################################################### |
| 21 | + |
| 22 | +select STDERR; $| = 1; |
| 23 | +select STDOUT; $| = 1; |
| 24 | + |
| 25 | +my $t = Test::Nginx->new()->has(qw/http http_ssl uwsgi/) |
| 26 | + ->has_daemon('openssl'); |
| 27 | + |
| 28 | +$t->write_file_expand('nginx.conf', <<'EOF'); |
| 29 | +
|
| 30 | +%%TEST_GLOBALS%% |
| 31 | +
|
| 32 | +daemon off; |
| 33 | +
|
| 34 | +events { |
| 35 | +} |
| 36 | +
|
| 37 | +http { |
| 38 | + %%TEST_GLOBALS_HTTP%% |
| 39 | +
|
| 40 | + server { |
| 41 | + listen 127.0.0.1:8080; |
| 42 | + server_name localhost; |
| 43 | +
|
| 44 | + uwsgi_ssl_session_reuse off; |
| 45 | +
|
| 46 | + location / { |
| 47 | + uwsgi_pass suwsgi://127.0.0.1:8081; |
| 48 | + uwsgi_ssl_certificate $arg_cert.example.com.crt; |
| 49 | + uwsgi_ssl_certificate_key $arg_cert.example.com.key; |
| 50 | + } |
| 51 | +
|
| 52 | + location /encrypted { |
| 53 | + uwsgi_pass suwsgi://127.0.0.1:8082; |
| 54 | + uwsgi_ssl_certificate $arg_cert.example.com.crt; |
| 55 | + uwsgi_ssl_certificate_key $arg_cert.example.com.key; |
| 56 | + uwsgi_ssl_password_file password; |
| 57 | + } |
| 58 | +
|
| 59 | + location /none { |
| 60 | + uwsgi_pass suwsgi://127.0.0.1:8082; |
| 61 | + uwsgi_ssl_certificate $arg_cert; |
| 62 | + uwsgi_ssl_certificate_key $arg_cert; |
| 63 | + } |
| 64 | + } |
| 65 | +
|
| 66 | + # stub to implement SSL logic for tests |
| 67 | +
|
| 68 | + server { |
| 69 | + listen 127.0.0.1:8081 ssl; |
| 70 | + server_name localhost; |
| 71 | +
|
| 72 | + ssl_certificate 2.example.com.crt; |
| 73 | + ssl_certificate_key 2.example.com.key; |
| 74 | +
|
| 75 | + ssl_verify_client optional_no_ca; |
| 76 | + ssl_trusted_certificate 1.example.com.crt; |
| 77 | +
|
| 78 | + add_header X-Verify $ssl_client_verify always; |
| 79 | + add_header X-Name $ssl_client_s_dn always; |
| 80 | + } |
| 81 | +
|
| 82 | + server { |
| 83 | + listen 127.0.0.1:8082 ssl; |
| 84 | + server_name localhost; |
| 85 | +
|
| 86 | + ssl_certificate 1.example.com.crt; |
| 87 | + ssl_certificate_key 1.example.com.key; |
| 88 | +
|
| 89 | + ssl_verify_client optional_no_ca; |
| 90 | + ssl_trusted_certificate 3.example.com.crt; |
| 91 | +
|
| 92 | + add_header X-Verify $ssl_client_verify always; |
| 93 | + } |
| 94 | +} |
| 95 | +
|
| 96 | +EOF |
| 97 | + |
| 98 | +$t->write_file('openssl.conf', <<EOF); |
| 99 | +[ req ] |
| 100 | +default_bits = 2048 |
| 101 | +encrypt_key = no |
| 102 | +distinguished_name = req_distinguished_name |
| 103 | +[ req_distinguished_name ] |
| 104 | +EOF |
| 105 | + |
| 106 | +my $d = $t->testdir(); |
| 107 | + |
| 108 | +foreach my $name ('1.example.com', '2.example.com') { |
| 109 | + system('openssl req -x509 -new ' |
| 110 | + . "-config $d/openssl.conf -subj /CN=$name/ " |
| 111 | + . "-out $d/$name.crt -keyout $d/$name.key " |
| 112 | + . ">>$d/openssl.out 2>&1") == 0 |
| 113 | + or die "Can't create certificate for $name: $!\n"; |
| 114 | +} |
| 115 | + |
| 116 | +foreach my $name ('3.example.com') { |
| 117 | + system("openssl genrsa -out $d/$name.key -passout pass:$name " |
| 118 | + . "-aes128 2048 >>$d/openssl.out 2>&1") == 0 |
| 119 | + or die "Can't create private key: $!\n"; |
| 120 | + system('openssl req -x509 -new ' |
| 121 | + . "-config $d/openssl.conf -subj /CN=$name/ " |
| 122 | + . "-out $d/$name.crt " |
| 123 | + . "-key $d/$name.key -passin pass:$name" |
| 124 | + . ">>$d/openssl.out 2>&1") == 0 |
| 125 | + or die "Can't create certificate for $name: $!\n"; |
| 126 | +} |
| 127 | + |
| 128 | +sleep 1 if $^O eq 'MSWin32'; |
| 129 | + |
| 130 | +$t->write_file('password', '3.example.com'); |
| 131 | +$t->write_file('index.html', ''); |
| 132 | + |
| 133 | +$t->try_run('no upstream ssl_certificate variables')->plan(4); |
| 134 | + |
| 135 | +############################################################################### |
| 136 | + |
| 137 | +like(http_get('/?cert=1'), |
| 138 | + qr/X-Verify: SUCCESS/ms, 'variable - verify certificate'); |
| 139 | +like(http_get('/?cert=2'), |
| 140 | + qr/X-Verify: FAILED/ms, 'variable - fail certificate'); |
| 141 | +like(http_get('/encrypted?cert=3'), |
| 142 | + qr/X-Verify: SUCCESS/ms, 'variable - with encrypted key'); |
| 143 | +like(http_get('/none'), |
| 144 | + qr/X-Verify: NONE/ms, 'variable - no certificate'); |
| 145 | + |
| 146 | +############################################################################### |
0 commit comments