|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# (C) Sergey Kandaurov |
| 4 | +# (C) Aleksei Bavshin |
| 5 | +# (C) Nginx, Inc. |
| 6 | + |
| 7 | +# Tests for http ssl module, loading "store:..." keys from OpenSSL providers. |
| 8 | + |
| 9 | +############################################################################### |
| 10 | + |
| 11 | +use warnings; |
| 12 | +use strict; |
| 13 | + |
| 14 | +use Test::More; |
| 15 | + |
| 16 | +BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 17 | + |
| 18 | +use lib 'lib'; |
| 19 | +use Test::Nginx; |
| 20 | + |
| 21 | +############################################################################### |
| 22 | + |
| 23 | +select STDERR; $| = 1; |
| 24 | +select STDOUT; $| = 1; |
| 25 | + |
| 26 | +plan(skip_all => 'win32') if $^O eq 'MSWin32'; |
| 27 | + |
| 28 | +plan(skip_all => 'may not work, incompatible with sanitizers') |
| 29 | + unless $ENV{TEST_NGINX_UNSAFE}; |
| 30 | + |
| 31 | +my $t = Test::Nginx->new()->has(qw/http proxy http_ssl openssl:3/) |
| 32 | + ->has_daemon('openssl')->has_daemon('softhsm2-util'); |
| 33 | + |
| 34 | +plan(skip_all => 'not yet') unless $t->has_version('1.29.0'); |
| 35 | + |
| 36 | +$t->write_file_expand('nginx.conf', <<'EOF'); |
| 37 | +
|
| 38 | +%%TEST_GLOBALS%% |
| 39 | +
|
| 40 | +daemon off; |
| 41 | +
|
| 42 | +events { |
| 43 | +} |
| 44 | +
|
| 45 | +env SOFTHSM2_CONF; |
| 46 | +
|
| 47 | +http { |
| 48 | + %%TEST_GLOBALS_HTTP%% |
| 49 | +
|
| 50 | + server { |
| 51 | + listen 127.0.0.1:8081 ssl; |
| 52 | + listen 127.0.0.1:8080; |
| 53 | + server_name localhost; |
| 54 | +
|
| 55 | + ssl_certificate localhost.crt; |
| 56 | + ssl_certificate_key "store:pkcs11:token=NginxZero;object=nx_key_0"; |
| 57 | +
|
| 58 | + ssl_password_file pin.txt; |
| 59 | +
|
| 60 | + location / { |
| 61 | + # index index.html by default |
| 62 | + } |
| 63 | +
|
| 64 | + location /proxy { |
| 65 | + proxy_pass https://127.0.0.1:8081/; |
| 66 | + } |
| 67 | +
|
| 68 | + location /var { |
| 69 | + proxy_pass https://127.0.0.1:8082/; |
| 70 | + proxy_ssl_name localhost; |
| 71 | + proxy_ssl_server_name on; |
| 72 | + } |
| 73 | + } |
| 74 | +
|
| 75 | + server { |
| 76 | + listen 127.0.0.1:8082 ssl; |
| 77 | + server_name localhost; |
| 78 | +
|
| 79 | + ssl_certificate $ssl_server_name.crt; |
| 80 | + ssl_certificate_key "store:pkcs11:token=NginxZero;object=nx_key_0"; |
| 81 | +
|
| 82 | + ssl_password_file pin.txt; |
| 83 | +
|
| 84 | + location / { |
| 85 | + # index index.html by default |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | +
|
| 90 | +EOF |
| 91 | + |
| 92 | +# Create a SoftHSM token with a secret key, and configure OpenSSL to access it |
| 93 | +# using the pkcs11 provider (https://github.com/latchset/pkcs11-provider). |
| 94 | +# |
| 95 | +# Note that library paths vary on different systems, |
| 96 | +# and may need to be adjusted. |
| 97 | + |
| 98 | +my $libsofthsm2_path; |
| 99 | +my @so_paths = ( |
| 100 | + '/usr/lib/softhsm', # Debian-based |
| 101 | + '/usr/local/lib/softhsm', # FreeBSD |
| 102 | + '/opt/local/lib/softhsm', # MacPorts |
| 103 | + '/lib64', # RHEL-based |
| 104 | + split /:/, $ENV{TEST_NGINX_SOFTHSM} || '' |
| 105 | +); |
| 106 | + |
| 107 | +for my $so_path (@so_paths) { |
| 108 | + $so_path .= '/libsofthsm2.so'; |
| 109 | + if (-e $so_path) { |
| 110 | + $libsofthsm2_path = $so_path; |
| 111 | + last; |
| 112 | + } |
| 113 | +}; |
| 114 | + |
| 115 | +plan(skip_all => "libsofthsm2.so not found") unless $libsofthsm2_path; |
| 116 | + |
| 117 | +my $openssl_conf = <<EOF; |
| 118 | +openssl_conf = openssl_def |
| 119 | +
|
| 120 | +[openssl_def] |
| 121 | +providers = provider_sect |
| 122 | +
|
| 123 | +[provider_sect] |
| 124 | +default = default_sect |
| 125 | +pkcs11 = pkcs11_sect |
| 126 | +
|
| 127 | +[default_sect] |
| 128 | +activate = 1 |
| 129 | +
|
| 130 | +[pkcs11_sect] |
| 131 | +pkcs11-module-path = $libsofthsm2_path |
| 132 | +pkcs11-module-cache-pins = cache |
| 133 | +# https://github.com/latchset/pkcs11-provider/commit/ab6370fd |
| 134 | +pkcs11-module-quirks = no-deinit no-operation-state |
| 135 | +module = /usr/local/lib/ossl-modules/pkcs11.so |
| 136 | +activate = 1 |
| 137 | +
|
| 138 | +[ req ] |
| 139 | +default_bits = 2048 |
| 140 | +encrypt_key = no |
| 141 | +distinguished_name = req_distinguished_name |
| 142 | +[ req_distinguished_name ] |
| 143 | +EOF |
| 144 | + |
| 145 | +$openssl_conf =~ s|^(?=module)|# |m if $^O ne 'freebsd'; |
| 146 | +$t->write_file('openssl.conf', $openssl_conf); |
| 147 | + |
| 148 | +my $d = $t->testdir(); |
| 149 | + |
| 150 | +$t->write_file('softhsm2.conf', <<EOF); |
| 151 | +directories.tokendir = $d/tokens/ |
| 152 | +objectstore.backend = file |
| 153 | +EOF |
| 154 | + |
| 155 | +mkdir($d . '/tokens'); |
| 156 | + |
| 157 | +$ENV{SOFTHSM2_CONF} = "$d/softhsm2.conf"; |
| 158 | +$ENV{OPENSSL_CONF} = "$d/openssl.conf"; |
| 159 | + |
| 160 | +foreach my $name ('localhost') { |
| 161 | + system('softhsm2-util --init-token --slot 0 --label NginxZero ' |
| 162 | + . '--pin 1234 --so-pin 1234 ' |
| 163 | + . ">>$d/openssl.out 2>&1"); |
| 164 | + |
| 165 | + system("openssl genrsa -out $d/$name.key 2048 " |
| 166 | + . ">>$d/openssl.out 2>&1") == 0 |
| 167 | + or die "Can't create private key: $!\n"; |
| 168 | + |
| 169 | + system("softhsm2-util --import $d/$name.key --id 00 --label nx_key_0 " |
| 170 | + . '--token NginxZero --pin 1234 ' |
| 171 | + . ">>$d/openssl.out 2>&1") == 0 |
| 172 | + or die "Can't import private key: $!\n"; |
| 173 | + |
| 174 | + system('openssl req -x509 -new ' |
| 175 | + . "-subj /CN=$name/ -out $d/$name.crt -text -passin pass:1234 " |
| 176 | + . '-key "pkcs11:token=NginxZero;object=nx_key_0" ' |
| 177 | + . ">>$d/openssl.out 2>&1") == 0 |
| 178 | + or plan(skip_all => "missing pkcs11-provider"); |
| 179 | +} |
| 180 | + |
| 181 | +$t->write_file('pin.txt', '1234'); |
| 182 | +$t->write_file('index.html', ''); |
| 183 | + |
| 184 | +$t->run()->plan(2); |
| 185 | + |
| 186 | +############################################################################### |
| 187 | + |
| 188 | +like(http_get('/proxy'), qr/200 OK/, 'ssl provider keys'); |
| 189 | +like(http_get('/var'), qr/200 OK/, 'ssl_certificate with variable'); |
| 190 | + |
| 191 | +############################################################################### |
0 commit comments