File: 162-socket-tls-handshake.t

package info (click to toggle)
libnginx-mod-http-lua 1%3A0.10.28-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,456 kB
  • sloc: ansic: 42,658; perl: 480; sh: 303; python: 23; makefile: 11
file content (373 lines) | stat: -rw-r--r-- 8,658 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# vim:set ft= ts=4 sw=4 et fdm=marker:

use Test::Nginx::Socket::Lua;

repeat_each(2);

plan tests => repeat_each() * 43;

$ENV{TEST_NGINX_RESOLVER} ||= '8.8.8.8';

log_level 'debug';

no_long_string();
#no_diff();

sub read_file {
    my $infile = shift;
    open my $in, $infile
        or die "cannot open $infile for reading: $!";
    my $cert = do { local $/; <$in> };
    close $in;
    $cert;
}

our $MTLSCA = read_file("t/cert/mtls_ca.crt");
our $MTLSClient = read_file("t/cert/mtls_client.crt");
our $MTLSClientKey = read_file("t/cert/mtls_client.key");
our $MTLSServer = read_file("t/cert/mtls_server.crt");
our $MTLSServerKey = read_file("t/cert/mtls_server.key");

our $HtmlDir = html_dir;

our $mtls_http_config = <<"_EOC_";
server {
    listen unix:$::HtmlDir/mtls.sock ssl;

    ssl_certificate        $::HtmlDir/mtls_server.crt;
    ssl_certificate_key    $::HtmlDir/mtls_server.key;
    ssl_client_certificate $::HtmlDir/mtls_ca.crt;
    ssl_verify_client      on;
    server_tokens          off;

    location / {
        return 200 "hello, \$ssl_client_s_dn";
    }
}
_EOC_

our $mtls_user_files = <<"_EOC_";
>>> mtls_server.key
$::MTLSServerKey
>>> mtls_server.crt
$::MTLSServer
>>> mtls_ca.crt
$::MTLSCA
>>> mtls_client.key
$::MTLSClientKey
>>> mtls_client.crt
$::MTLSClient
_EOC_

run_tests();

__DATA__

=== TEST 1: sanity: www.google.com
--- config
    server_tokens off;
    resolver $TEST_NGINX_RESOLVER ipv6=off;

    location /t {
        content_by_lua_block {
            -- avoid flushing bing in "check leak" testing mode:
            local counter = package.loaded.counter
            if not counter then
                counter = 1
            elseif counter >= 2 then
                return ngx.exit(503)
            else
                counter = counter + 1
            end

            package.loaded.counter = counter

            do
                local sock = ngx.socket.tcp()
                sock:settimeout(2000)

                local ok, err = sock:connect("www.google.com", 443)
                if not ok then
                    ngx.say("failed to connect: ", err)
                    return
                end

                ngx.say("connected: ", ok)

                local sess, err = sock:sslhandshake()
                if not sess then
                    ngx.say("failed to do SSL handshake: ", err)
                    return
                end

                ngx.say("ssl handshake: ", type(sess))

                local req = "GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n"
                local bytes, err = sock:send(req)
                if not bytes then
                    ngx.say("failed to send http request: ", err)
                    return
                end

                ngx.say("sent http request: ", bytes, " bytes.")

                local line, err = sock:receive()
                if not line then
                    ngx.say("failed to receive response status line: ", err)
                    return
                end

                ngx.say("received: ", line)

                local ok, err = sock:close()
                ngx.say("close: ", ok, " ", err)
            end  -- do

            collectgarbage()
        }
    }
--- request
GET /t
--- response_body_like chop
\Aconnected: 1
ssl handshake: cdata
sent http request: 59 bytes.
received: HTTP/1.1 (?:200 OK|302 Found)
close: 1 nil
\z
--- grep_error_log eval: qr/lua ssl (?:set|save|free) session: [0-9A-F]+/
--- grep_error_log_out eval
qr/^lua ssl save session: ([0-9A-F]+)
lua ssl free session: ([0-9A-F]+)
$/
--- no_error_log
lua ssl server name:
SSL reused session
[error]
[alert]
--- timeout: 5



=== TEST 2: mutual TLS handshake, upstream is not accessible without client certs
--- http_config eval: $::mtls_http_config
--- config eval
"
    location /t {
        content_by_lua_block {
            local sock = ngx.socket.tcp()
            local ok, err = sock:connect('unix:$::HtmlDir/mtls.sock')
            if not ok then
                ngx.say('failed to connect: ', err)
            end

            assert(sock:sslhandshake())

            ngx.say('connected: ', ok)

            local req = 'GET /\\r\\n'

            local bytes, err = sock:send(req)
            if not bytes then
                ngx.say('failed to send request: ', err)
                return
            end

            ngx.say('request sent: ', bytes)

            ngx.say(sock:receive('*a'))

            assert(sock:close())
        }
    }
"
--- user_files eval: $::mtls_user_files
--- request
GET /t
--- response_body_like: 400 No required SSL certificate was sent
--- no_error_log
[alert]
[error]
[crit]
[emerg]



=== TEST 3: mutual TLS handshake, upstream is accessible with client certs
--- http_config eval: $::mtls_http_config
--- config eval
"
    location /t {
        content_by_lua_block {
            local sock = ngx.socket.tcp()
            local ok, err = sock:connect('unix:$::HtmlDir/mtls.sock')
            if not ok then
                ngx.say('failed to connect: ', err)
            end

            local f = assert(io.open('$::HtmlDir/mtls_client.crt'))
            local cert_data = f:read('*a')
            f:close()

            f = assert(io.open('$::HtmlDir/mtls_client.key'))
            local key_data = f:read('*a')
            f:close()

            local ssl = require('ngx.ssl')

            local chain = assert(ssl.parse_pem_cert(cert_data))
            local priv = assert(ssl.parse_pem_priv_key(key_data))

            sock:setclientcert(chain, priv)

            assert(sock:sslhandshake())

            ngx.say('connected: ', ok)

            local req = 'GET /\\r\\n'

            local bytes, err = sock:send(req)
            if not bytes then
                ngx.say('failed to send request: ', err)
                return
            end

            ngx.say('request sent: ', bytes)

            ngx.say(sock:receive('*a'))

            assert(sock:close())
        }
    }
"
--- user_files eval: $::mtls_user_files
--- request
GET /t
--- response_body
connected: 1
request sent: 7
hello, CN=foo@example.com,O=OpenResty,ST=California,C=US
--- no_error_log
[alert]
[error]
[crit]
[emerg]



=== TEST 4: incorrect type of client cert
--- config
    location /t {
        content_by_lua_block {
            local sock = ngx.socket.tcp()

            local ok, err = sock:setclientcert("doesnt", "work")
            if not ok then
                ngx.say('failed to setclientcert: ', err)
                return
            end

            assert(sock:close())
        }
    }
--- request
GET /t
--- response_body
failed to setclientcert: bad cert arg: cdata expected, got string
--- no_error_log
[alert]
[error]
[crit]
[emerg]



=== TEST 5: incorrect type of client key
--- config eval
"
    location /t {
        content_by_lua_block {
            local sock = ngx.socket.tcp()

            local f = assert(io.open('$::HtmlDir/mtls_client.crt'))
            local cert_data = f:read('*a')
            f:close()

            local ssl = require('ngx.ssl')

            local chain = assert(ssl.parse_pem_cert(cert_data))

            local ok, err = sock:setclientcert(chain, 'work')
            if not ok then
                ngx.say('failed to setclientcert: ', err)
                return
            end

            assert(sock:close())
        }
    }
"
--- user_files eval: $::mtls_user_files
--- request
GET /t
--- response_body
failed to setclientcert: bad pkey arg: cdata expected, got string
--- no_error_log
[alert]
[error]
[crit]
[emerg]



=== TEST 6: missing client cert
--- config
    location /t {
        content_by_lua_block {
            local sock = ngx.socket.tcp()

            local ok, err = sock:setclientcert(nil, "work")
            if not ok then
                ngx.say('failed to setclientcert: ', err)
                return
            end

            assert(sock:close())
        }
    }
--- request
GET /t
--- response_body
failed to setclientcert: client certificate must be supplied with corresponding private key
--- no_error_log
[alert]
[error]
[crit]
[emerg]



=== TEST 7: missing private key
--- config
    location /t {
        content_by_lua_block {
            local sock = ngx.socket.tcp()

            local ok, err = sock:setclientcert('doesnt', nil)
            if not ok then
                ngx.say('failed to setclientcert: ', err)
                return
            end

            assert(sock:close())
        }
    }
--- request
GET /t
--- response_body
failed to setclientcert: client certificate must be supplied with corresponding private key
--- no_error_log
[alert]
[error]
[crit]
[emerg]