summaryrefslogtreecommitdiffhomepage
path: root/libs/luci-lib-nixio/axTLS/samples/lua/axssl.lua
blob: 6ea26b69dda6323ffe6287bd336963c05c989d21 (plain)
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#!/usr/local/bin/lua

--
-- Copyright (c) 2007, Cameron Rich
--
-- All rights reserved.
--
-- Redistribution and use in source and binary forms, with or without
-- modification, are permitted provided that the following conditions are met:
--
-- * Redistributions of source code must retain the above copyright notice,
--   this list of conditions and the following disclaimer.
-- * Redistributions in binary form must reproduce the above copyright
--   notice, this list of conditions and the following disclaimer in the
--   documentation and/or other materials provided with the distribution.
-- * Neither the name of the axTLS project nor the names of its
--   contributors may be used to endorse or promote products derived
--   from this software without specific prior written permission.
--
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
-- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
-- TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
-- OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
-- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--

--
-- Demonstrate the use of the axTLS library in Lua with a set of 
-- command-line parameters similar to openssl. In fact, openssl clients 
-- should be able to communicate with axTLS servers and visa-versa.
--
-- This code has various bits enabled depending on the configuration. To enable
-- the most interesting version, compile with the 'full mode' enabled.
--
-- To see what options you have, run the following:
-- > [lua] axssl s_server -?
-- > [lua] axssl s_client -?
--
-- The axtls/axtlsl shared libraries must be in the same directory or be found 
-- by the OS.
--
--
require "bit"
require("axtlsl")
local socket = require("socket")

-- print version?
if #arg == 1 and arg[1] == "version" then
    print("axssl.lua "..axtlsl.ssl_version())
    os.exit(1)
end

--
-- We've had some sort of command-line error. Print out the basic options.
--
function print_options(option)
    print("axssl: Error: '"..option.."' is an invalid command.")
    print("usage: axssl [s_server|s_client|version] [args ...]")
    os.exit(1)
end

--
-- We've had some sort of command-line error. Print out the server options.
--
function print_server_options(build_mode, option)
    local cert_size = axtlsl.ssl_get_config(axtlsl.SSL_MAX_CERT_CFG_OFFSET)
    local ca_cert_size = axtlsl.ssl_get_config(
            axtlsl.SSL_MAX_CA_CERT_CFG_OFFSET)

    print("unknown option "..option)
    print("usage: s_server [args ...]")
    print(" -accept\t- port to accept on (default is 4433)")
    print(" -quiet\t\t- No server output")

    if build_mode >= axtlsl.SSL_BUILD_SERVER_ONLY then
        print(" -cert arg\t- certificate file to add (in addition to "..
                "default) to chain -")
        print("\t\t  Can repeat up to "..cert_size.." times")
        print(" -key arg\t- Private key file to use - default DER format")
        print(" -pass\t\t- private key file pass phrase source")
    end

    if build_mode >= axtlsl.SSL_BUILD_ENABLE_VERIFICATION then
        print(" -verify\t- turn on peer certificate verification")
        print(" -CAfile arg\t- Certificate authority - default DER format")
        print("\t\t  Can repeat up to "..ca_cert_size.." times")
    end

    if build_mode == axtlsl.SSL_BUILD_FULL_MODE then
        print(" -debug\t\t- Print more output")
        print(" -state\t\t- Show state messages")
        print(" -show-rsa\t- Show RSA state")
    end

    os.exit(1)
end

--
-- We've had some sort of command-line error. Print out the client options.
--
function print_client_options(build_mode, option)
    local cert_size = axtlsl.ssl_get_config(axtlsl.SSL_MAX_CERT_CFG_OFFSET)
    local ca_cert_size = axtlsl.ssl_get_config(
            axtlsl.SSL_MAX_CA_CERT_CFG_OFFSET)

    print("unknown option "..option)

    if build_mode >= axtlsl.SSL_BUILD_ENABLE_CLIENT then
        print("usage: s_client [args ...]")
        print(" -connect host:port - who to connect to (default "..
                "is localhost:4433)")
        print(" -verify\t- turn on peer certificate verification")
        print(" -cert arg\t- certificate file to use - default DER format")
        print(" -key arg\t- Private key file to use - default DER format")
        print("\t\t  Can repeat up to "..cert_size.." times")
        print(" -CAfile arg\t- Certificate authority - default DER format")
        print("\t\t  Can repeat up to "..ca_cert_size.."times")
        print(" -quiet\t\t- No client output")
        print(" -pass\t\t- private key file pass phrase source")
        print(" -reconnect\t- Drop and re-make the connection "..
                "with the same Session-ID")

        if build_mode == axtlsl.SSL_BUILD_FULL_MODE then
            print(" -debug\t\t- Print more output")
            print(" -state\t\t- Show state messages")
            print(" -show-rsa\t- Show RSA state")
        end
    else
        print("Change configuration to allow this feature")
    end

    os.exit(1)
end

-- Implement the SSL server logic. 
function do_server(build_mode)
    local i = 2
    local v
    local port = 4433
    local options = axtlsl.SSL_DISPLAY_CERTS
    local quiet = false
    local password = ""
    local private_key_file = nil
    local cert_size = axtlsl.ssl_get_config(axtlsl.SSL_MAX_CERT_CFG_OFFSET)
    local ca_cert_size = axtlsl.
                            ssl_get_config(axtlsl.SSL_MAX_CA_CERT_CFG_OFFSET)
    local cert = {}
    local ca_cert = {}

    while i <= #arg do
        if arg[i] ==  "-accept" then
            if i >= #arg then
                print_server_options(build_mode, arg[i])
            end

            i = i + 1
            port = arg[i]
        elseif arg[i] == "-quiet" then
            quiet = true
            options = bit.band(options, bit.bnot(axtlsl.SSL_DISPLAY_CERTS))
        elseif build_mode >= axtlsl.SSL_BUILD_SERVER_ONLY then
            if arg[i] == "-cert" then
                if i >= #arg or #cert >= cert_size then
                    print_server_options(build_mode, arg[i]) 
                end

                i = i + 1
                table.insert(cert, arg[i])
            elseif arg[i] == "-key" then
                if i >= #arg then
                    print_server_options(build_mode, arg[i]) 
                end

                i = i + 1
                private_key_file = arg[i]
                options = bit.bor(options, axtlsl.SSL_NO_DEFAULT_KEY)
            elseif arg[i] == "-pass" then
                if i >= #arg then
                    print_server_options(build_mode, arg[i]) 
                end

                i = i + 1
                password = arg[i]
            elseif build_mode >= axtlsl.SSL_BUILD_ENABLE_VERIFICATION then
                if arg[i] == "-verify" then
                    options = bit.bor(options, axtlsl.SSL_CLIENT_AUTHENTICATION)
                elseif arg[i] == "-CAfile" then
                    if i >= #arg or #ca_cert >= ca_cert_size then
                        print_server_options(build_mode, arg[i])  
                    end

                    i = i + 1
                    table.insert(ca_cert, arg[i])
                elseif build_mode == axtlsl.SSL_BUILD_FULL_MODE then
                    if arg[i] == "-debug" then
                        options = bit.bor(options, axtlsl.SSL_DISPLAY_BYTES)
                    elseif arg[i] == "-state" then
                        options = bit.bor(options, axtlsl.SSL_DISPLAY_STATES)
                    elseif arg[i] == "-show-rsa" then
                        options = bit.bor(options, axtlsl.SSL_DISPLAY_RSA)
                    else
                        print_server_options(build_mode, arg[i])
                    end
                else
                    print_server_options(build_mode, arg[i])
                end
            else 
                print_server_options(build_mode, arg[i])
            end
        else 
            print_server_options(build_mode, arg[i])
        end

        i = i + 1
    end

    -- Create socket for incoming connections
    local server_sock = socket.try(socket.bind("*", port))

    ---------------------------------------------------------------------------
    -- This is where the interesting stuff happens. Up until now we've
    -- just been setting up sockets etc. Now we do the SSL handshake.
    ---------------------------------------------------------------------------
    local ssl_ctx = axtlsl.ssl_ctx_new(options, axtlsl.SSL_DEFAULT_SVR_SESS)
    if ssl_ctx == nil then error("Error: Server context is invalid") end

    if private_key_file ~= nil then
        local obj_type = axtlsl.SSL_OBJ_RSA_KEY

        if string.find(private_key_file, ".p8") then 
            obj_type = axtlsl.SSL_OBJ_PKCS8 
        end

        if string.find(private_key_file, ".p12") then 
            obj_type = axtlsl.SSL_OBJ_PKCS12 
        end

        if axtlsl.ssl_obj_load(ssl_ctx, obj_type, private_key_file, 
                                        password) ~= axtlsl.SSL_OK then
            error("Private key '" .. private_key_file .. "' is undefined.")
        end
    end

    for _, v in ipairs(cert) do
        if axtlsl.ssl_obj_load(ssl_ctx, axtlsl.SSL_OBJ_X509_CERT, v, "") ~= 
                                        axtlsl.SSL_OK then
            error("Certificate '"..v .. "' is undefined.")
        end
    end

    for _, v in ipairs(ca_cert) do
        if axtlsl.ssl_obj_load(ssl_ctx, axtlsl.SSL_OBJ_X509_CACERT, v, "") ~= 
                                        axtlsl.SSL_OK then
            error("Certificate '"..v .."' is undefined.")
        end
    end

    while true do
        if not quiet then print("ACCEPT") end
        local client_sock = server_sock:accept();
        local ssl = axtlsl.ssl_server_new(ssl_ctx, client_sock:getfd())

        -- do the actual SSL handshake
        local connected = false
        local res
        local buf

        while true do
            socket.select({client_sock}, nil)
            res, buf = axtlsl.ssl_read(ssl)

            if res == axtlsl.SSL_OK then -- connection established and ok
                if axtlsl.ssl_handshake_status(ssl) == axtlsl.SSL_OK then
                    if not quiet and not connected then
                        display_session_id(ssl)
                        display_cipher(ssl)
                    end
                    connected = true
                end
            end

            if res > axtlsl.SSL_OK then
                for _, v in ipairs(buf) do
                    io.write(string.format("%c", v))
                end
            elseif res < axtlsl.SSL_OK then 
                if not quiet then
                    axtlsl.ssl_display_error(res)
                end
                break
            end
        end

        -- client was disconnected or the handshake failed.
        print("CONNECTION CLOSED")
        axtlsl.ssl_free(ssl)
        client_sock:close()
    end

    axtlsl.ssl_ctx_free(ssl_ctx)
end

--
-- Implement the SSL client logic.
--
function do_client(build_mode)
    local i = 2
    local v
    local port = 4433
    local options = 
            bit.bor(axtlsl.SSL_SERVER_VERIFY_LATER, axtlsl.SSL_DISPLAY_CERTS)
    local private_key_file = nil
    local reconnect = 0
    local quiet = false
    local password = ""
    local session_id = {}
    local host = "127.0.0.1"
    local cert_size = axtlsl.ssl_get_config(axtlsl.SSL_MAX_CERT_CFG_OFFSET)
    local ca_cert_size = axtlsl.
                            ssl_get_config(axtlsl.SSL_MAX_CA_CERT_CFG_OFFSET)
    local cert = {}
    local ca_cert = {}

    while i <= #arg do
        if arg[i] == "-connect" then
            if i >= #arg then
                print_client_options(build_mode, arg[i]) 
            end

            i = i + 1
            local t = string.find(arg[i], ":")
            host = string.sub(arg[i], 1, t-1)
            port = string.sub(arg[i], t+1)
        elseif arg[i] == "-cert" then
            if i >= #arg or #cert >= cert_size then
                print_client_options(build_mode, arg[i]) 
            end

            i = i + 1
            table.insert(cert, arg[i])
        elseif arg[i] == "-key" then
            if i >= #arg then
                print_client_options(build_mode, arg[i])
            end

            i = i + 1
            private_key_file = arg[i]
            options = bit.bor(options, axtlsl.SSL_NO_DEFAULT_KEY)
        elseif arg[i] == "-CAfile" then
            if i >= #arg or #ca_cert >= ca_cert_size then
                print_client_options(build_mode, arg[i]) 
            end

            i = i + 1
            table.insert(ca_cert, arg[i])
        elseif arg[i] == "-verify" then
            options = bit.band(options, 
                                bit.bnot(axtlsl.SSL_SERVER_VERIFY_LATER))
        elseif arg[i] == "-reconnect" then
            reconnect = 4
        elseif arg[i] == "-quiet" then
            quiet = true
            options = bit.band(options, bnot(axtlsl.SSL_DISPLAY_CERTS))
        elseif arg[i] == "-pass" then
            if i >= #arg then
                print_server_options(build_mode, arg[i])
            end

            i = i + 1
            password = arg[i]
        elseif build_mode == axtlsl.SSL_BUILD_FULL_MODE then
            if arg[i] == "-debug" then
                options = bit.bor(options, axtlsl.SSL_DISPLAY_BYTES)
            elseif arg[i] == "-state" then
                options = bit.bor(axtlsl.SSL_DISPLAY_STATES)
            elseif arg[i] == "-show-rsa" then
                options = bit.bor(axtlsl.SSL_DISPLAY_RSA)
            else    -- don't know what this is
                print_client_options(build_mode, arg[i])
            end
        else    -- don't know what this is
            print_client_options(build_mode, arg[i])
        end

        i = i + 1
    end

    local client_sock = socket.try(socket.connect(host, port))
    local ssl
    local res

    if not quiet then print("CONNECTED") end

    ---------------------------------------------------------------------------
    -- This is where the interesting stuff happens. Up until now we've
    -- just been setting up sockets etc. Now we do the SSL handshake.
    ---------------------------------------------------------------------------
    local ssl_ctx = axtlsl.ssl_ctx_new(options, axtlsl.SSL_DEFAULT_CLNT_SESS)

    if ssl_ctx == nil then 
        error("Error: Client context is invalid")
    end

    if private_key_file ~= nil then
        local obj_type = axtlsl.SSL_OBJ_RSA_KEY

        if string.find(private_key_file, ".p8") then 
            obj_type = axtlsl.SSL_OBJ_PKCS8 
        end

        if string.find(private_key_file, ".p12") then 
            obj_type = axtlsl.SSL_OBJ_PKCS12 
        end

        if axtlsl.ssl_obj_load(ssl_ctx, obj_type, private_key_file, 
                                        password) ~= axtlsl.SSL_OK then
            error("Private key '"..private_key_file.."' is undefined.")
        end
    end

    for _, v in ipairs(cert) do
        if axtlsl.ssl_obj_load(ssl_ctx, axtlsl.SSL_OBJ_X509_CERT, v, "") ~= 
                                        axtlsl.SSL_OK then
            error("Certificate '"..v .. "' is undefined.")
        end
    end

    for _, v in ipairs(ca_cert) do
        if axtlsl.ssl_obj_load(ssl_ctx, axtlsl.SSL_OBJ_X509_CACERT, v, "") ~= 
                                        axtlsl.SSL_OK then
            error("Certificate '"..v .."' is undefined.")
        end
    end

    -- Try session resumption?
    if reconnect ~= 0 then
        local session_id = nil
        local sess_id_size = 0

        while reconnect > 0 do
            reconnect = reconnect - 1
            ssl = axtlsl.ssl_client_new(ssl_ctx, 
                    client_sock:getfd(), session_id, sess_id_size)

            res = axtlsl.ssl_handshake_status(ssl)
            if res ~= axtlsl.SSL_OK then
                if not quiet then axtlsl.ssl_display_error(res) end
                axtlsl.ssl_free(ssl)
                os.exit(1)
            end

            display_session_id(ssl)
            session_id = axtlsl.ssl_get_session_id(ssl)
            sess_id_size = axtlsl.ssl_get_session_id_size(ssl)

            if reconnect > 0 then
                axtlsl.ssl_free(ssl)
                client_sock:close()
                client_sock = socket.try(socket.connect(host, port))
            end

        end
    else
        ssl = axtlsl.ssl_client_new(ssl_ctx, client_sock:getfd(), nil, 0)
    end

    -- check the return status
    res = axtlsl.ssl_handshake_status(ssl)
    if res ~= axtlsl.SSL_OK then
        if not quiet then axtlsl.ssl_display_error(res) end
        os.exit(1)
    end

    if not quiet then
        local common_name = axtlsl.ssl_get_cert_dn(ssl, 
                            axtlsl.SSL_X509_CERT_COMMON_NAME)

        if common_name ~= nil then 
            print("Common Name:\t\t\t"..common_name)
        end

        display_session_id(ssl)
        display_cipher(ssl)
    end

    while true do
        local line = io.read()
    if line == nil then break end
        local bytes = {}

        for i = 1, #line do
            bytes[i] = line.byte(line, i)
        end

        bytes[#line+1] = 10      -- add carriage return, null
        bytes[#line+2] = 0

        res = axtlsl.ssl_write(ssl, bytes, #bytes)
        if res < axtlsl.SSL_OK then
            if not quiet then axtlsl.ssl_display_error(res) end
            break
        end
    end

    axtlsl.ssl_ctx_free(ssl_ctx)
    client_sock:close()
end

--
-- Display what cipher we are using 
--
function display_cipher(ssl)
    io.write("CIPHER is ")
    local cipher_id = axtlsl.ssl_get_cipher_id(ssl)

    if cipher_id == axtlsl.SSL_AES128_SHA then
        print("AES128-SHA")
    elseif cipher_id == axtlsl.SSL_AES256_SHA then
        print("AES256-SHA")
    elseif axtlsl.SSL_RC4_128_SHA then
        print("RC4-SHA")
    elseif axtlsl.SSL_RC4_128_MD5 then
        print("RC4-MD5")
    else 
        print("Unknown - "..cipher_id)
    end
end

--
-- Display what session id we have.
--
function display_session_id(ssl)
    local session_id = axtlsl.ssl_get_session_id(ssl)
    local v

    if #session_id > 0 then
        print("-----BEGIN SSL SESSION PARAMETERS-----")
        for _, v in ipairs(session_id) do
            io.write(string.format("%02x", v))
        end
        print("\n-----END SSL SESSION PARAMETERS-----")
    end
end

--
-- Main entry point. Doesn't do much except works out whether we are a client
-- or a server.
--
if #arg == 0 or (arg[1] ~= "s_server" and arg[1] ~= "s_client") then
    print_options(#arg > 0 and arg[1] or "")
end

local build_mode = axtlsl.ssl_get_config(axtlsl.SSL_BUILD_MODE)
_ = arg[1] == "s_server" and do_server(build_mode) or do_client(build_mode)
os.exit(0)