comparison uwsgi_ssl_certificate.t @ 1688:31ea330ac360

Tests: more uwsgi tests with SSL. This covers tests for client certificate (including encrypted) to SSL backend and backend certificate verification.
author Sergey Kandaurov <pluknet@nginx.com>
date Mon, 24 May 2021 18:28:17 +0300
parents
children b5036a0f9ae0
comparison
equal deleted inserted replaced
1687:41b213d611f5 1688:31ea330ac360
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Tests for http uwsgi module with client certificate to ssl backend.
7 # The uwsgi_ssl_certificate and uwsgi_ssl_password_file directives.
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 my $t = Test::Nginx->new()->has(qw/http http_ssl uwsgi/)
27 ->has_daemon('openssl')->plan(5);
28
29 $t->write_file_expand('nginx.conf', <<'EOF');
30
31 %%TEST_GLOBALS%%
32
33 daemon off;
34
35 events {
36 }
37
38 http {
39 %%TEST_GLOBALS_HTTP%%
40
41 server {
42 listen 127.0.0.1:8080;
43 server_name localhost;
44
45 uwsgi_ssl_session_reuse off;
46
47 location /verify {
48 uwsgi_pass suwsgi://127.0.0.1:8081;
49 uwsgi_ssl_certificate 1.example.com.crt;
50 uwsgi_ssl_certificate_key 1.example.com.key;
51 }
52
53 location /fail {
54 uwsgi_pass suwsgi://127.0.0.1:8081;
55 uwsgi_ssl_certificate 2.example.com.crt;
56 uwsgi_ssl_certificate_key 2.example.com.key;
57 }
58
59 location /encrypted {
60 uwsgi_pass suwsgi://127.0.0.1:8082;
61 uwsgi_ssl_certificate 3.example.com.crt;
62 uwsgi_ssl_certificate_key 3.example.com.key;
63 uwsgi_ssl_password_file password;
64 }
65 }
66
67 # stub to implement SSL logic for tests
68
69 server {
70 listen 127.0.0.1:8081 ssl;
71 server_name localhost;
72
73 ssl_certificate 2.example.com.crt;
74 ssl_certificate_key 2.example.com.key;
75
76 ssl_verify_client optional_no_ca;
77 ssl_trusted_certificate 1.example.com.crt;
78
79 add_header X-Verify $ssl_client_verify always;
80 add_header X-Name $ssl_client_s_dn always;
81 }
82
83 server {
84 listen 127.0.0.1:8082 ssl;
85 server_name localhost;
86
87 ssl_certificate 1.example.com.crt;
88 ssl_certificate_key 1.example.com.key;
89
90 ssl_verify_client optional_no_ca;
91 ssl_trusted_certificate 3.example.com.crt;
92
93 add_header X-Verify $ssl_client_verify always;
94 }
95 }
96
97 EOF
98
99 $t->write_file('openssl.conf', <<EOF);
100 [ req ]
101 default_bits = 2048
102 encrypt_key = no
103 distinguished_name = req_distinguished_name
104 [ req_distinguished_name ]
105 EOF
106
107 my $d = $t->testdir();
108
109 foreach my $name ('1.example.com', '2.example.com') {
110 system('openssl req -x509 -new '
111 . "-config $d/openssl.conf -subj /CN=$name/ "
112 . "-out $d/$name.crt -keyout $d/$name.key "
113 . ">>$d/openssl.out 2>&1") == 0
114 or die "Can't create certificate for $name: $!\n";
115 }
116
117 foreach my $name ('3.example.com') {
118 system("openssl genrsa -out $d/$name.key -passout pass:$name "
119 . "-aes128 2048 >>$d/openssl.out 2>&1") == 0
120 or die "Can't create private key: $!\n";
121 system('openssl req -x509 -new '
122 . "-config $d/openssl.conf -subj /CN=$name/ "
123 . "-out $d/$name.crt "
124 . "-key $d/$name.key -passin pass:$name"
125 . ">>$d/openssl.out 2>&1") == 0
126 or die "Can't create certificate for $name: $!\n";
127 }
128
129 sleep 1 if $^O eq 'MSWin32';
130
131 $t->write_file('password', '3.example.com');
132 $t->write_file('index.html', '');
133
134 $t->run();
135
136 ###############################################################################
137
138 like(http_get('/verify'), qr/X-Verify: SUCCESS/ms, 'verify certificate');
139 like(http_get('/fail'), qr/X-Verify: FAILED/ms, 'fail certificate');
140 like(http_get('/encrypted'), qr/X-Verify: SUCCESS/ms, 'with encrypted key');
141
142 like(http_get('/verify'), qr!X-Name: /?CN=1.example!, 'valid certificate');
143 unlike(http_get('/fail'), qr!X-Name: /?CN=1.example!, 'invalid certificate');
144
145 ###############################################################################