Skip to content

Commit 8c3db0b

Browse files
committed
Tests: test for loading "store:..." keys.
1 parent fe05bad commit 8c3db0b

File tree

1 file changed

+180
-0
lines changed

1 file changed

+180
-0
lines changed

ssl_provider_keys.t

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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.
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, leaves coredump')
29+
unless $ENV{TEST_NGINX_UNSAFE};
30+
31+
my $t = Test::Nginx->new()->has(qw/http proxy http_ssl/)->has_daemon('openssl')
32+
->has_daemon('softhsm2-util')->has_daemon('pkcs11-tool');
33+
34+
plan(skip_all => 'no providers') unless $t->has_feature('openssl:3');
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+
env OPENSSL_CONF;
47+
48+
http {
49+
%%TEST_GLOBALS_HTTP%%
50+
51+
server {
52+
listen 127.0.0.1:8081 ssl;
53+
listen 127.0.0.1:8080;
54+
server_name localhost;
55+
56+
ssl_certificate localhost.crt;
57+
ssl_certificate_key "store:pkcs11:token=NginxZero;object=nx_key_0";
58+
59+
location / {
60+
# index index.html by default
61+
}
62+
63+
location /proxy {
64+
proxy_pass https://127.0.0.1:8081/;
65+
}
66+
67+
location /var {
68+
proxy_pass https://127.0.0.1:8082/;
69+
proxy_ssl_name localhost;
70+
proxy_ssl_server_name on;
71+
}
72+
}
73+
74+
server {
75+
listen 127.0.0.1:8082 ssl;
76+
server_name localhost;
77+
78+
ssl_certificate $ssl_server_name.crt;
79+
ssl_certificate_key "store:pkcs11:token=NginxZero;object=nx_key_0";
80+
81+
location / {
82+
# index index.html by default
83+
}
84+
}
85+
}
86+
87+
EOF
88+
89+
# Create a SoftHSM token with a secret key, and configure OpenSSL
90+
# to access it using the pkcs11 provider.
91+
#
92+
# Note that library paths vary on different systems,
93+
# and may need to be adjusted.
94+
95+
my $libsofthsm2_path;
96+
my @so_paths = (
97+
'/usr/lib/softhsm', # Debian-based
98+
'/usr/local/lib/softhsm', # FreeBSD
99+
'/opt/local/lib/softhsm', # MacPorts
100+
'/lib64', # RHEL-based
101+
split /:/, $ENV{TEST_NGINX_SOFTHSM} || ''
102+
);
103+
104+
for my $so_path (@so_paths) {
105+
$so_path .= '/libsofthsm2.so';
106+
if (-e $so_path) {
107+
$libsofthsm2_path = $so_path;
108+
last;
109+
}
110+
};
111+
112+
plan(skip_all => "libsofthsm2.so not found") unless $libsofthsm2_path;
113+
114+
my $d = $t->testdir();
115+
116+
$t->write_file('openssl.conf', <<EOF);
117+
openssl_conf = openssl_def
118+
119+
[openssl_def]
120+
providers = provider_sect
121+
122+
[provider_sect]
123+
default = default_sect
124+
pkcs11 = pkcs11_sect
125+
126+
[default_sect]
127+
activate = 1
128+
129+
[pkcs11_sect]
130+
pkcs11-module-path = $libsofthsm2_path
131+
pkcs11-module-token-pin = file:$d/pin.txt
132+
# https://github.com/latchset/pkcs11-provider/commit/ab6370fd
133+
pkcs11-module-quirks = no-deinit no-operation-state
134+
activate = 1
135+
136+
[ req ]
137+
default_bits = 2048
138+
encrypt_key = no
139+
distinguished_name = req_distinguished_name
140+
[ req_distinguished_name ]
141+
EOF
142+
143+
$t->write_file('pin.txt', '1234');
144+
145+
$t->write_file('softhsm2.conf', <<EOF);
146+
directories.tokendir = $d/tokens/
147+
objectstore.backend = file
148+
EOF
149+
150+
mkdir($d . '/tokens');
151+
152+
$ENV{SOFTHSM2_CONF} = "$d/softhsm2.conf";
153+
$ENV{OPENSSL_CONF} = "$d/openssl.conf";
154+
155+
foreach my $name ('localhost') {
156+
system('softhsm2-util --init-token --slot 0 --label NginxZero '
157+
. '--pin 1234 --so-pin 1234 '
158+
. ">>$d/openssl.out 2>&1");
159+
160+
system("pkcs11-tool --module=$libsofthsm2_path "
161+
. '-p 1234 -l -k -d 0 -a nx_key_0 --key-type rsa:2048 '
162+
. ">>$d/openssl.out 2>&1");
163+
164+
system('openssl req -x509 -new '
165+
. "-subj /CN=$name/ -out $d/$name.crt -text "
166+
. "-key 'pkcs11:token=NginxZero;object=nx_key_0' "
167+
. ">>$d/openssl.out 2>&1") == 0
168+
or plan(skip_all => "missing provider");
169+
}
170+
171+
$t->run()->plan(2);
172+
173+
$t->write_file('index.html', '');
174+
175+
###############################################################################
176+
177+
like(http_get('/proxy'), qr/200 OK/, 'ssl provider keys');
178+
like(http_get('/var'), qr/200 OK/, 'ssl_certificate with variable');
179+
180+
###############################################################################

0 commit comments

Comments
 (0)