comparison ssl_engine_keys.t @ 1747:7f09d144d15c

Tests: updated ssl_engine_keys.t test to use SoftHSM v2. Notably, this implies not using slots to identify keys, since "softhsm2-util --init-token" automatically reassigns initialized token to a new slot. Additionally, the "-config" option of is no longer used when generating certificates, as in OpenSSL 1.1.0 and later it conflicts with the configuration file provided via OPENSSL_CONF and results in "conflicting engine id" errors.
author Maxim Dounin <mdounin@mdounin.ru>
date Tue, 23 Nov 2021 03:58:07 +0300
parents 144c6ce732e4
children
comparison
equal deleted inserted replaced
1746:be8d5213d6b9 1747:7f09d144d15c
26 26
27 plan(skip_all => 'may not work, leaves coredump') 27 plan(skip_all => 'may not work, leaves coredump')
28 unless $ENV{TEST_NGINX_UNSAFE}; 28 unless $ENV{TEST_NGINX_UNSAFE};
29 29
30 my $t = Test::Nginx->new()->has(qw/http proxy http_ssl/)->has_daemon('openssl') 30 my $t = Test::Nginx->new()->has(qw/http proxy http_ssl/)->has_daemon('openssl')
31 ->has_daemon('softhsm')->has_daemon('pkcs11-tool')->plan(2); 31 ->has_daemon('softhsm2-util')->has_daemon('pkcs11-tool')->plan(2);
32 32
33 $t->write_file_expand('nginx.conf', <<'EOF'); 33 $t->write_file_expand('nginx.conf', <<'EOF');
34 34
35 %%TEST_GLOBALS%% 35 %%TEST_GLOBALS%%
36 36
46 listen 127.0.0.1:8081 ssl; 46 listen 127.0.0.1:8081 ssl;
47 listen 127.0.0.1:8080; 47 listen 127.0.0.1:8080;
48 server_name localhost; 48 server_name localhost;
49 49
50 ssl_certificate localhost.crt; 50 ssl_certificate localhost.crt;
51 ssl_certificate_key engine:pkcs11:slot_0-id_00; 51 ssl_certificate_key engine:pkcs11:id_00;
52 52
53 location / { 53 location / {
54 # index index.html by default 54 # index index.html by default
55 } 55 }
56 56
68 server { 68 server {
69 listen 127.0.0.1:8082 ssl; 69 listen 127.0.0.1:8082 ssl;
70 server_name localhost; 70 server_name localhost;
71 71
72 ssl_certificate $ssl_server_name.crt; 72 ssl_certificate $ssl_server_name.crt;
73 ssl_certificate_key engine:pkcs11:slot_0-id_00; 73 ssl_certificate_key engine:pkcs11:id_00;
74 74
75 location / { 75 location / {
76 # index index.html by default 76 # index index.html by default
77 } 77 }
78 } 78 }
99 pkcs11 = pkcs11_section 99 pkcs11 = pkcs11_section
100 100
101 [pkcs11_section] 101 [pkcs11_section]
102 engine_id = pkcs11 102 engine_id = pkcs11
103 dynamic_path = /usr/local/lib/engines/pkcs11.so 103 dynamic_path = /usr/local/lib/engines/pkcs11.so
104 MODULE_PATH = /usr/local/lib/softhsm/libsofthsm.so 104 MODULE_PATH = /usr/local/lib/softhsm/libsofthsm2.so
105 init = 1 105 init = 1
106 PIN = 1234 106 PIN = 1234
107 107
108 [ req ] 108 [ req ]
109 default_bits = 2048 109 default_bits = 2048
112 [ req_distinguished_name ] 112 [ req_distinguished_name ]
113 EOF 113 EOF
114 114
115 my $d = $t->testdir(); 115 my $d = $t->testdir();
116 116
117 $t->write_file('softhsm.conf', <<EOF); 117 $t->write_file('softhsm2.conf', <<EOF);
118 0:$d/slot0.db 118 directories.tokendir = $d/tokens/
119 objectstore.backend = file
119 EOF 120 EOF
120 121
121 $ENV{SOFTHSM_CONF} = "$d/softhsm.conf"; 122 mkdir($d . '/tokens');
123
124 $ENV{SOFTHSM2_CONF} = "$d/softhsm2.conf";
122 $ENV{OPENSSL_CONF} = "$d/openssl.conf"; 125 $ENV{OPENSSL_CONF} = "$d/openssl.conf";
123 126
124 foreach my $name ('localhost') { 127 foreach my $name ('localhost') {
125 system('softhsm --init-token --slot 0 --label "NginxZero" ' 128 system('softhsm2-util --init-token --slot 0 --label NginxZero '
126 . '--pin 1234 --so-pin 1234 ' 129 . '--pin 1234 --so-pin 1234 '
127 . ">>$d/openssl.out 2>&1"); 130 . ">>$d/openssl.out 2>&1");
128 131
129 system('pkcs11-tool --module=/usr/local/lib/softhsm/libsofthsm.so ' 132 system('pkcs11-tool --module=/usr/local/lib/softhsm/libsofthsm2.so '
130 . '-p 1234 -l -k -d 0 -a nx_key_0 --key-type rsa:2048 ' 133 . '-p 1234 -l -k -d 0 -a nx_key_0 --key-type rsa:2048 '
131 . ">>$d/openssl.out 2>&1"); 134 . ">>$d/openssl.out 2>&1");
132 135
133 system('openssl req -x509 -new -engine pkcs11 ' 136 system('openssl req -x509 -new '
134 . "-config $d/openssl.conf -subj /CN=$name/ " 137 . "-subj /CN=$name/ -out $d/$name.crt -text "
135 . "-out $d/$name.crt -keyform engine -text -key id_00 " 138 . "-engine pkcs11 -keyform engine -key id_00 "
136 . ">>$d/openssl.out 2>&1") == 0 139 . ">>$d/openssl.out 2>&1") == 0
137 or die "Can't create certificate for $name: $!\n"; 140 or die "Can't create certificate for $name: $!\n";
138 } 141 }
139 142
140 $t->run(); 143 $t->run();