Skip to content

Commit adff61f

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

File tree

1 file changed

+181
-0
lines changed

1 file changed

+181
-0
lines changed

ssl_provider_keys.t

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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 => "not yet") unless $t->has_version('1.27.4');
35+
plan(skip_all => 'no providers') unless $t->has_feature('openssl:3');
36+
37+
$t->write_file_expand('nginx.conf', <<'EOF');
38+
39+
%%TEST_GLOBALS%%
40+
41+
daemon off;
42+
43+
events {
44+
}
45+
46+
env SOFTHSM2_CONF;
47+
env OPENSSL_CONF;
48+
49+
http {
50+
%%TEST_GLOBALS_HTTP%%
51+
52+
server {
53+
listen 127.0.0.1:8081 ssl;
54+
listen 127.0.0.1:8080;
55+
server_name localhost;
56+
57+
ssl_certificate localhost.crt;
58+
ssl_certificate_key "store:pkcs11:token=NginxZero;object=nx_key_0";
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+
location / {
83+
# index index.html by default
84+
}
85+
}
86+
}
87+
88+
EOF
89+
90+
# Create a SoftHSM token with a secret key, and configure OpenSSL
91+
# to access it using the pkcs11 provider.
92+
#
93+
# Note that library paths vary on different systems,
94+
# and may need to be adjusted.
95+
96+
my $libsofthsm2_path;
97+
my @so_paths = (
98+
'/usr/lib/softhsm', # Debian-based
99+
'/usr/local/lib/softhsm', # FreeBSD
100+
'/opt/local/lib/softhsm', # MacPorts
101+
'/lib64', # RHEL-based
102+
split /:/, $ENV{TEST_NGINX_SOFTHSM} || ''
103+
);
104+
105+
for my $so_path (@so_paths) {
106+
$so_path .= '/libsofthsm2.so';
107+
if (-e $so_path) {
108+
$libsofthsm2_path = $so_path;
109+
last;
110+
}
111+
};
112+
113+
plan(skip_all => "libsofthsm2.so not found") unless $libsofthsm2_path;
114+
115+
my $d = $t->testdir();
116+
117+
$t->write_file('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-token-pin = file:$d/pin.txt
133+
# https://github.com/latchset/pkcs11-provider/commit/ab6370fd
134+
pkcs11-module-quirks = no-deinit no-operation-state
135+
activate = 1
136+
137+
[ req ]
138+
default_bits = 2048
139+
encrypt_key = no
140+
distinguished_name = req_distinguished_name
141+
[ req_distinguished_name ]
142+
EOF
143+
144+
$t->write_file('pin.txt', '1234');
145+
146+
$t->write_file('softhsm2.conf', <<EOF);
147+
directories.tokendir = $d/tokens/
148+
objectstore.backend = file
149+
EOF
150+
151+
mkdir($d . '/tokens');
152+
153+
$ENV{SOFTHSM2_CONF} = "$d/softhsm2.conf";
154+
$ENV{OPENSSL_CONF} = "$d/openssl.conf";
155+
156+
foreach my $name ('localhost') {
157+
system('softhsm2-util --init-token --slot 0 --label NginxZero '
158+
. '--pin 1234 --so-pin 1234 '
159+
. ">>$d/openssl.out 2>&1");
160+
161+
system("pkcs11-tool --module=$libsofthsm2_path "
162+
. '-p 1234 -l -k -d 0 -a nx_key_0 --key-type rsa:2048 '
163+
. ">>$d/openssl.out 2>&1");
164+
165+
system('openssl req -x509 -new '
166+
. "-subj /CN=$name/ -out $d/$name.crt -text "
167+
. "-key 'pkcs11:token=NginxZero;object=nx_key_0' "
168+
. ">>$d/openssl.out 2>&1") == 0
169+
or plan(skip_all => "missing provider");
170+
}
171+
172+
$t->run()->plan(2);
173+
174+
$t->write_file('index.html', '');
175+
176+
###############################################################################
177+
178+
like(http_get('/proxy'), qr/200 OK/, 'ssl provider keys');
179+
like(http_get('/var'), qr/200 OK/, 'ssl_certificate with variable');
180+
181+
###############################################################################

0 commit comments

Comments
 (0)