forked from zhaozg/lua-openssl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssl.c
More file actions
2888 lines (2664 loc) · 79 KB
/
ssl.c
File metadata and controls
2888 lines (2664 loc) · 79 KB
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***
ssl modules to create SSL/TLS server or client, send and recv data over SSL channels.
@module ssl
@usage
ssl = require('openssl').ssl
*/
#include <openssl/dh.h>
#include <openssl/ec.h>
#include <openssl/rsa.h>
#include <openssl/ssl.h>
#include "openssl.h"
#include "private.h"
#include "ssl_options.h"
#include <stdint.h>
#if OPENSSL_VERSION_NUMBER > 0x30000000
#ifndef SSL_get_peer_certificate
#define SSL_get_peer_certificate SSL_get1_peer_certificate
#endif
#ifndef SSL_DEFAULT_CIPHER_LIST
#define SSL_DEFAULT_CIPHER_LIST OSSL_default_cipher_list()
#endif
#endif
/***
create ssl_ctx object, which mapping to SSL_CTX in openssl.
@function ctx_new
@tparam string protocol support 'SSLv3', 'SSLv23', 'SSLv2', 'TSLv1', 'TSLv1_1','TSLv1_2','TLS',
'DTLSv1','DTLSv1_2', and can be follow by '_server' or '_client', in general you should use 'TLS' to
negotiate highest available SSL/TLS version
@tparam[opt] string support_ciphers, if not given, default of openssl will be used
@treturn openssl.ssl_ctx
*/
#if OPENSSL_VERSION_NUMBER > 0x10100000L
#define TLS_PROTOCOL_TIPS \
"only support TLS, DTLS to negotiate highest available SSL/TLS or DTLS " \
"version above openssl v1.1.0\n" \
"optional followed by _client or _server\n" \
"default is TLS\n"
#define DEFAULT_PROTOCOL "TLS"
#else
#define TLS_PROTOCOL_TIPS \
"SSLv23, TLSv1_2, TLSv1_1, TLSv1, DTLSv1_2 or DTLSv1, optional followed by _client or _server\n" \
"optional followed by _client or _server\n" \
"default is SSLv23 to negotiate highest available SSL/TLS\n"
#define DEFAULT_PROTOCOL "SSLv23"
#endif
typedef enum
{
SSL_CTX_SESSION_ADD = 0,
SSL_CTX_SESSION_GET,
SSL_CTX_SESSION_DEL,
#if OPENSSL_VERSION_NUMBER < 0x10100000L
SSL_CTX_TEMP_DH,
SSL_CTX_TEMP_RSA,
SSL_CTX_TEMP_ECDH,
#endif
SSL_CTX_MAX_IDX
} SSL_CTX_INDEX;
/***
create a new SSL context object
@function new
@tparam[opt="TLS"] string method SSL/TLS protocol method ("TLS", "SSLv23", "TLSv1", "TLSv1_1", "TLSv1_2", etc.)
@treturn openssl.ssl_ctx SSL context object
*/
static int
openssl_ssl_ctx_new(lua_State *L)
{
const char *meth = luaL_optstring(L, 1, DEFAULT_PROTOCOL);
#if OPENSSL_VERSION_NUMBER >= 0x01000000L
const
#endif
SSL_METHOD *method
= NULL;
const char *ciphers;
SSL_CTX *ctx;
if (strcmp(meth, "SSLv23") == 0)
method = SSLv23_method();
else if (strcmp(meth, "SSLv23_server") == 0)
method = SSLv23_server_method();
else if (strcmp(meth, "SSLv23_client") == 0)
method = SSLv23_client_method();
#if OPENSSL_VERSION_NUMBER > 0x10100000L
else if (strcmp(meth, "TLS") == 0)
method = TLS_method();
else if (strcmp(meth, "TLS_server") == 0)
method = TLS_server_method();
else if (strcmp(meth, "TLS_client") == 0)
method = TLS_client_method();
else if (strcmp(meth, "DTLS") == 0)
method = DTLS_method();
else if (strcmp(meth, "DTLS_server") == 0)
method = DTLS_server_method();
else if (strcmp(meth, "DTLS_client") == 0)
method = DTLS_client_method();
#endif
#if !defined(OPENSSL_NO_DEPRECATED) && !defined(OPENSSL_NO_DTLS1_2_METHOD) && \
OPENSSL_VERSION_NUMBER < 0x10100000L
else if (strcmp(meth, "DTLSv1_2") == 0)
method = DTLSv1_2_method();
else if (strcmp(meth, "DTLSv1_2_server") == 0)
method = DTLSv1_2_server_method();
else if (strcmp(meth, "DTLSv1_2_client") == 0)
method = DTLSv1_2_client_method();
#endif
#if !defined(OPENSSL_NO_DEPRECATED) && !defined(OPENSSL_NO_DTLS1_METHOD) && \
OPENSSL_VERSION_NUMBER < 0x10100000L
else if (strcmp(meth, "DTLSv1") == 0)
method = DTLSv1_method();
else if (strcmp(meth, "DTLSv1_server") == 0)
method = DTLSv1_server_method();
else if (strcmp(meth, "DTLSv1_client") == 0)
method = DTLSv1_client_method();
#endif
#if !defined(OPENSSL_NO_DEPRECATED) && !defined(OPENSSL_NO_TLS1_2_METHOD) && \
OPENSSL_VERSION_NUMBER < 0x10100000L
else if (strcmp(meth, "TLSv1_2") == 0)
method = TLSv1_2_method();
else if (strcmp(meth, "TLSv1_2_server") == 0)
method = TLSv1_2_server_method();
else if (strcmp(meth, "TLSv1_2_client") == 0)
method = TLSv1_2_client_method();
#endif
#if !defined(OPENSSL_NO_DEPRECATED) && !defined(OPENSSL_NO_TLS1_1_METHOD) && \
OPENSSL_VERSION_NUMBER < 0x10100000L
else if (strcmp(meth, "TLSv1_1") == 0)
method = TLSv1_1_method();
else if (strcmp(meth, "TLSv1_1_server") == 0)
method = TLSv1_1_server_method();
else if (strcmp(meth, "TLSv1_1_client") == 0)
method = TLSv1_1_client_method();
#endif
#if !defined(OPENSSL_NO_DEPRECATED) && !defined(OPENSSL_NO_TLS1_METHOD) && \
OPENSSL_VERSION_NUMBER < 0x10100000L
else if (strcmp(meth, "TLSv1") == 0)
method = TLSv1_method();
else if (strcmp(meth, "TLSv1_server") == 0)
method = TLSv1_server_method();
else if (strcmp(meth, "TLSv1_client") == 0)
method = TLSv1_client_method();
#endif
#ifndef OPENSSL_NO_SSL3_METHOD
else if (strcmp(meth, "SSLv3") == 0)
method = SSLv3_method();
else if (strcmp(meth, "SSLv3_server") == 0)
method = SSLv3_server_method();
else if (strcmp(meth, "SSLv3_client") == 0)
method = SSLv3_client_method();
#endif
#ifdef LOAD_SSL_CUSTOM
LOAD_SSL_CUSTOM
#endif
else luaL_argerror(L, 1, TLS_PROTOCOL_TIPS);
ctx = SSL_CTX_new(method);
if (!ctx) luaL_argerror(L, 1, TLS_PROTOCOL_TIPS);
ciphers = luaL_optstring(L, 2, SSL_DEFAULT_CIPHER_LIST);
#if OPENSSL_VERSION_NUMBER > 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
if (!SSL_CTX_set_ciphersuites(ctx, ciphers) && !SSL_CTX_set_cipher_list(ctx, ciphers))
#else
if (!SSL_CTX_set_cipher_list(ctx, ciphers))
#endif
luaL_argerror(L, 2, "Error to set cipher list");
PUSH_OBJECT(ctx, "openssl.ssl_ctx");
SSL_CTX_set_app_data(ctx, openssl_mainthread(L));
openssl_newvalue(L, ctx);
return 1;
}
/***
get alert_type for ssl state
@function alert_type
@tparam number alert
@tparam[opt=false] boolean long
@treturn string alert type
*/
static int
openssl_ssl_alert_type(lua_State *L)
{
int v = luaL_checkint(L, 1);
int _long = lua_isnone(L, 2) ? 0 : auxiliar_checkboolean(L, 2);
const char *val;
if (_long)
val = SSL_alert_type_string_long(v << 8);
else
val = SSL_alert_type_string(v << 8);
lua_pushstring(L, val);
return 1;
}
/***
get alert_desc for ssl state
@function alert_desc
@tparam number alert
@tparam[opt=false] boolean long
@treturn string alert type
@treturn string desc string, if long set true will return long info
*/
static int
openssl_ssl_alert_desc(lua_State *L)
{
int v = luaL_checkint(L, 1);
int _long = lua_isnone(L, 2) ? 0 : auxiliar_checkboolean(L, 2);
const char *val;
if (_long)
val = SSL_alert_desc_string_long(v);
else
val = SSL_alert_desc_string(v);
lua_pushstring(L, val);
return 1;
}
/***
create new SSL session object
@function session_new
@treturn ssl_session new SSL session object
*/
static int
openssl_ssl_session_new(lua_State *L)
{
SSL_SESSION *ss = SSL_SESSION_new();
PUSH_OBJECT(ss, "openssl.ssl_session");
return 1;
}
/***
read SSL session from BIO or string data
@function session_read
@tparam bio|string input BIO object or string containing session data (PEM or DER format)
@treturn ssl_session|nil SSL session object or nil on error
*/
static int
openssl_ssl_session_read(lua_State *L)
{
BIO *in = load_bio_object(L, 1);
SSL_SESSION *ss = PEM_read_bio_SSL_SESSION(in, NULL, NULL, NULL);
if (!ss) {
(void)BIO_reset(in);
ss = d2i_SSL_SESSION_bio(in, NULL);
}
BIO_free(in);
if (ss) {
PUSH_OBJECT(ss, "openssl.ssl_session");
return 1;
}
return openssl_pushresult(L, 0);
}
static luaL_Reg R[] = {
{ "ctx_new", openssl_ssl_ctx_new },
{ "alert_type", openssl_ssl_alert_type },
{ "alert_desc", openssl_ssl_alert_desc },
{ "session_new", openssl_ssl_session_new },
{ "session_read", openssl_ssl_session_read },
{ NULL, NULL }
};
/* SSL CTX object */
/***
openssl.ssl_ctx object
@type ssl_ctx
*/
/***
tell ssl_ctx use private key and certificate, and check private key
@function use
@tparam openssl.evp_pkey pkey
@tparam openssl.x509 cert
@treturn boolean result return true for ok, or nil followed by errmsg and errval
*/
static int
openssl_ssl_ctx_use(lua_State *L)
{
int ret;
SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
EVP_PKEY *pkey = CHECK_OBJECT(2, EVP_PKEY, "openssl.evp_pkey");
if (lua_isstring(L, 3)) {
ret = SSL_CTX_use_certificate_chain_file(ctx, luaL_checkstring(L, 3));
} else {
X509 *cert = CHECK_OBJECT(3, X509, "openssl.x509");
ret = SSL_CTX_use_certificate(ctx, cert);
}
if (ret == 1) {
ret = SSL_CTX_use_PrivateKey(ctx, pkey);
if (ret == 1) {
ret = SSL_CTX_check_private_key(ctx);
}
}
return openssl_pushresult(L, ret);
}
/***
add client ca cert and option extra chain cert
@function add
@tparam openssl.x509 clientca
@tparam[opt] table extra_chain_cert_array
@treturn boolean result
*/
static int
openssl_ssl_ctx_add(lua_State *L)
{
SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
X509 *x = CHECK_OBJECT(2, X509, "openssl.x509");
int ret = SSL_CTX_add_client_CA(ctx, x);
if (ret == 1 && !lua_isnone(L, 3)) {
size_t i;
luaL_checktable(L, 3);
for (i = 1; ret == 1 && i <= lua_rawlen(L, 3); i++) {
lua_rawgeti(L, 3, i);
x = CHECK_OBJECT(2, X509, "openssl.x509");
lua_pop(L, 1);
X509_up_ref(x);
ret = SSL_CTX_add_extra_chain_cert(ctx, x);
}
}
return openssl_pushresult(L, ret);
}
static int
openssl_ssl_ctx_gc(lua_State *L)
{
SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
SSL_CTX_free(ctx);
openssl_freevalue(L, ctx);
return 0;
}
/***
get timeout
@function timeout
@return number
*/
/***
set timeout
@function timeout
@tparam number timeout
@treturn number previous timeout
*/
static int
openssl_ssl_ctx_timeout(lua_State *L)
{
SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
long t;
if (!lua_isnone(L, 2)) {
t = SSL_CTX_set_timeout(ctx, luaL_checkint(L, 2));
lua_pushinteger(L, t);
return 1;
}
t = SSL_CTX_get_timeout(ctx);
lua_pushinteger(L, t);
return 1;
}
static const int iMode_options[] = { SSL_MODE_ENABLE_PARTIAL_WRITE,
SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER,
SSL_MODE_AUTO_RETRY,
SSL_MODE_NO_AUTO_CHAIN,
#ifdef SSL_MODE_RELEASE_BUFFERS
SSL_MODE_RELEASE_BUFFERS,
#endif
0 };
static const char *sMode_options[] = { "enable_partial_write",
"accept_moving_write_buffer",
"auto_retry",
"no_auto_chain",
#ifdef SSL_MODE_RELEASE_BUFFERS
"release_buffers",
#endif
NULL };
/***
clean given mode
mode support
'enable_partial_write','accept_moving_write_buffer','auto_retry','no_auto_chain','release_buffers'
@function mode
@tparam boolean clear must be true
@tparam string mode
@param[opt] ...
@treturn string
@treturn ...
@usage
modes = { ssl_ctx:mode('enable_partial_write','accept_moving_write_buffer','auto_retry') },
for i, v in ipairs(modes)
print(v)
end
--output 'enable_partial_write','accept_moving_write_buffer','auto_retry'
*/
static int
openssl_ssl_ctx_mode(lua_State *L)
{
SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
int mode = 0;
int ret;
int i;
if (!lua_isnoneornil(L, 2)) {
int clear = 0;
if (lua_isboolean(L, 2)) {
clear = lua_toboolean(L, 2);
i = 3;
} else
i = 2;
while (i <= lua_gettop(L)) {
mode = mode | auxiliar_checkoption(L, i++, NULL, sMode_options, iMode_options);
}
if (clear != 0)
mode = SSL_CTX_set_mode(ctx, mode);
else
mode = SSL_CTX_clear_mode(ctx, mode);
} else
mode = SSL_CTX_get_mode(ctx);
ret = 0;
for (i = 0; i < sizeof(iMode_options) / sizeof(int); i++) {
if (mode & iMode_options[i]) {
lua_pushstring(L, sMode_options[i]);
ret++;
}
}
return ret;
};
/***
get options
@function options
@treturn table string list of current options
*/
/***
set options
@function options
@tparam string option, support "microsoft_sess_id_bug", "netscape_challenge_bug",
"netscape_reuse_cipher_change_bug", "sslref2_reuse_cert_type_bug", "microsoft_big_sslv3_buffer",
"msie_sslv3_rsa_padding","ssleay_080_client_dh_bug",
"tls_d5_bug","tls_block_padding_bug","dont_insert_empty_fragments","all", please to see
ssl_options.h
@treturn table string list of current options after set new option
*/
/***
clear options
@function options
@tparam boolean clear set true to clear options
@tparam string option, support "microsoft_sess_id_bug", "netscape_challenge_bug",
"netscape_reuse_cipher_change_bug", "sslref2_reuse_cert_type_bug", "microsoft_big_sslv3_buffer",
"msie_sslv3_rsa_padding","ssleay_080_client_dh_bug",
"tls_d5_bug","tls_block_padding_bug","dont_insert_empty_fragments","all", please to see
ssl_options.h
@treturn table string list of current options after clear some option
*/
static int
openssl_ssl_ctx_options(lua_State *L)
{
SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
long options = 0;
int ret;
int i;
if (!lua_isnone(L, 2)) {
int top = lua_gettop(L);
int clear = 0;
if (lua_isboolean(L, 2)) {
clear = lua_toboolean(L, 2);
i = 3;
} else
i = 2;
for (; i <= top; i++) {
if (lua_isnumber(L, i))
options |= (long)luaL_checkinteger(L, i);
else {
const char *s = luaL_checkstring(L, i);
int j;
for (j = 0; ssl_options[j].name; j++) {
LuaL_Enumeration e = ssl_options[j];
if (strcasecmp(s, e.name) == 0) {
options |= e.val;
break;
}
}
}
}
if (clear != 0)
options = SSL_CTX_clear_options(ctx, options);
else
options = SSL_CTX_set_options(ctx, options);
} else
options = SSL_CTX_get_options(ctx);
lua_newtable(L);
ret = 0;
for (i = 0; ssl_options[i].name; i++) {
LuaL_Enumeration e = ssl_options[i];
if (options & e.val) {
lua_pushstring(L, e.name);
ret++;
lua_rawseti(L, -2, ret);
}
}
return 1;
}
/***
get min_proto_version and max_proto_version
@function version
@treturn[1] integer min_proto_version
@treturn[2] integer man_proto_version
*/
/***
set min_proto_version and max_proto_version
@function options
@tparam integer min
@tparam integer max
@treturn boolean result or fail
*/
#if OPENSSL_VERSION_NUMBER > 0x10100000L
static int
openssl_ssl_ctx_version(lua_State *L)
{
SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
int ret;
int minv = SSL_CTX_get_min_proto_version(ctx);
int maxv = SSL_CTX_get_max_proto_version(ctx);
if (lua_isnone(L, 2)) {
lua_pushinteger(L, minv);
lua_pushinteger(L, maxv);
return 2;
}
minv = luaL_optinteger(L, 2, minv);
maxv = luaL_optinteger(L, 3, maxv);
luaL_argcheck(L, minv <= maxv, 3, "max version can't less than min");
ret = SSL_CTX_set_min_proto_version(ctx, minv);
if (ret == 1) ret = SSL_CTX_set_min_proto_version(ctx, maxv);
if (ret == 1) {
lua_pushvalue(L, 1);
return 1;
}
return openssl_pushresult(L, ret);
}
#endif
/***
get quit_shutdown is set or not
Normally when a SSL connection is finished, the parties must send out
"close notify" alert messages using ***SSL:shutdown"*** for a clean shutdown.
@function quiet_shutdown
@treturn boolean result
*/
/***
set quiet_shutdown
@function quiet_shutdown
@tparam boolean quiet
When setting the "quiet shutdown" flag to 1, ***SSL:shutdown*** will set the internal flags
to SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN. ***SSL:shutdown*** then behaves like
***SSL:set_shutdown*** called with SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN.
The session is thus considered to be shutdown, but no "close notify" alert
is sent to the peer. This behaviour violates the TLS standard.
The default is normal shutdown behaviour as described by the TLS standard.
@treturn boolean result
*/
static int
openssl_ssl_ctx_quiet_shutdown(lua_State *L)
{
SSL_CTX *s = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
if (lua_isnone(L, 2)) {
int m = SSL_CTX_get_quiet_shutdown(s);
lua_pushinteger(L, m);
return 1;
} else {
int m = luaL_checkint(L, 2);
SSL_CTX_set_quiet_shutdown(s, m);
return 0;
}
};
/***
set verify locations with cafile and capath
ssl_ctx:verify_locations specifies the locations for *ctx*, at
which CA certificates for verification purposes are located. The certificates
available via *CAfile* and *CApath* are trusted.
@function verify_locations
@tparam string cafile
@tparam string capath
@treturn boolean result
*/
static int
openssl_ssl_ctx_load_verify_locations(lua_State *L)
{
SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
const char *CAfile = luaL_checkstring(L, 2);
const char *CApath = luaL_optstring(L, 3, NULL);
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
int ret = !(CAfile == NULL && CApath == NULL);
if (CAfile != NULL) ret = SSL_CTX_load_verify_file(ctx, CAfile);
if (ret == 1 && CApath != NULL) ret = SSL_CTX_load_verify_dir(ctx, CApath);
#else
int ret = SSL_CTX_load_verify_locations(ctx, CAfile, CApath);
#endif
return openssl_pushresult(L, ret);
}
/***
get certificate verification store of ssl_ctx
@function cert_store
@treturn x509_store store
*/
/***
set or replaces then certificate verification store of ssl_ctx
@function cert_store
@tparam x509_store store
@treturn x509_store store
*/
static int
openssl_ssl_ctx_cert_store(lua_State *L)
{
#if OPENSSL_VERSION_NUMBER > 0x10002000L
SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
X509_STORE *store = NULL;
if (lua_isnone(L, 2)) {
store = SSL_CTX_get_cert_store(ctx);
X509_STORE_up_ref(store);
PUSH_OBJECT(store, "openssl.x509_store");
return 1;
} else {
store = CHECK_OBJECT(2, X509_STORE, "openssl.x509_store");
X509_STORE_up_ref(store);
SSL_CTX_set_cert_store(ctx, store);
X509_STORE_set_trust(store, 1);
return 0;
}
#else
luaL_error(L, "NYI, openssl below 1.0.2 not fully support this feature");
return 0;
#endif
}
#ifndef OPENSSL_NO_ENGINE
/***
set client certificate engine for SSL context
@function set_engine
@tparam openssl.engine eng engine object to use for client certificates
@treturn boolean result true for success
*/
static int
openssl_ssl_ctx_set_engine(lua_State *L)
{
SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
ENGINE *eng = CHECK_OBJECT(2, ENGINE, "openssl.engine");
int ret = SSL_CTX_set_client_cert_engine(ctx, eng);
return openssl_pushresult(L, ret);
}
#endif
/* ssl functions */
/***
create SSL object from SSL context
This function creates a new SSL object from an SSL context. It supports two modes:
1. Using a file descriptor (fd)
2. Using BIO objects for input/output
@function ssl
@tparam openssl.ssl_ctx ctx SSL context object
@tparam number|openssl.bio fd_or_input File descriptor or input BIO
@tparam[opt=false] boolean|openssl.bio server_or_output Server mode flag or output BIO
@treturn[1] openssl.ssl SSL object on success
@treturn[2] nil on error
@treturn[2] string error message
-- @see OpenSSL function: SSL_new
-- @see OpenSSL function: SSL_set_fd
-- @see OpenSSL function: SSL_set_bio
@usage
-- Create SSL object from file descriptor
local ssl_ctx = require('openssl').ssl.ctx_new('TLS')
local fd = 5 -- Assume fd 5 is a connected socket
local ssl = ssl_ctx:ssl(fd)
-- Create SSL server object from BIO
local bio_in = require('openssl').bio.new('mem')
local bio_out = require('openssl').bio.new('mem')
local ssl = ssl_ctx:ssl(bio_in, bio_out, true) -- true for server mode
*/
static int
openssl_ssl_ctx_new_ssl(lua_State *L)
{
SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
int server = 0;
int mode_idx = 2;
SSL *ssl = SSL_new(ctx);
int ret = 1;
if (auxiliar_getclassudata(L, "openssl.bio", 2)) {
BIO *bi = CHECK_OBJECT(2, BIO, "openssl.bio");
BIO *bo = bi;
/* avoid bi be gc */
BIO_up_ref(bi);
if (auxiliar_getclassudata(L, "openssl.bio", 3)) {
bo = CHECK_OBJECT(3, BIO, "openssl.bio");
mode_idx = 4;
} else
mode_idx = 3;
/* avoid bo be gc */
BIO_up_ref(bo);
#if OPENSSL_VERSION_NUMBER > 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
SSL_set0_rbio(ssl, bi);
SSL_set0_wbio(ssl, bo);
#else
SSL_set_bio(ssl, bi, bo);
#endif
ret = 1;
} else if (lua_isnumber(L, 2)) {
ret = SSL_set_fd(ssl, luaL_checkint(L, 2));
mode_idx = 3;
}
if (ret == 1 && !lua_isnone(L, mode_idx)) {
server = lua_isnil(L, mode_idx) ? 0 : auxiliar_checkboolean(L, mode_idx);
}
if (ret == 1) {
if (server)
SSL_set_accept_state(ssl);
else
SSL_set_connect_state(ssl);
PUSH_OBJECT(ssl, "openssl.ssl");
openssl_newvalue(L, ssl);
/* ref to ctx */
lua_pushvalue(L, 1);
openssl_valueset(L, ssl, "ctx");
} else {
SSL_free(ssl);
openssl_freevalue(L, ssl);
return openssl_pushresult(L, ret);
}
return 1;
}
/***
create bio object
@function bio
@tparam string host_addr format like 'host:port'
@tparam[opt=false] boolean server, true listen at host_addr,false connect to host_addr
@tparam[opt=true] boolean autoretry ssl operation autoretry mode
@treturn openssl.bio bio object
*/
static int
openssl_ssl_ctx_new_bio(lua_State *L)
{
SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
const char *host_addr = luaL_checkstring(L, 2);
int server = lua_isnone(L, 3) ? 0 : auxiliar_checkboolean(L, 3);
int autoretry = lua_isnone(L, 4) ? 1 : auxiliar_checkboolean(L, 4);
BIO *bio = server ? BIO_new_ssl(ctx, 0) : BIO_new_ssl_connect(ctx);
if (bio) {
int ret = 0;
if (autoretry) {
SSL *ssl = NULL;
ret = BIO_get_ssl(bio, &ssl);
if (ret == 1) SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
}
if (server) {
BIO *acpt = BIO_new_accept((char *)host_addr);
BIO_set_accept_bios(acpt, bio);
bio = acpt;
} else {
ret = BIO_set_conn_hostname(bio, host_addr);
}
if (ret == 1) {
PUSH_OBJECT(bio, "openssl.bio");
return 1;
} else
return openssl_pushresult(L, ret);
} else {
BIO_free(bio);
bio = NULL;
return 0;
}
}
/***
get verify depth when cert chain veirition
@function verify_depth
@treturn number depth
*/
/***
set verify depth when cert chain veirition
@function verify_depth
@tparam number depth
@treturn number depth
*/
static int
openssl_ssl_ctx_verify_depth(lua_State *L)
{
SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
int depth;
if (!lua_isnone(L, 2)) {
depth = luaL_checkint(L, 2);
SSL_CTX_set_verify_depth(ctx, depth);
}
depth = SSL_CTX_get_verify_depth(ctx);
lua_pushinteger(L, depth);
return 1;
}
static const int iVerifyMode_Options[] = { SSL_VERIFY_NONE,
SSL_VERIFY_PEER,
SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
SSL_VERIFY_CLIENT_ONCE,
0 };
static const char *sVerifyMode_Options[] = { "none",
"peer",
"fail", /* fail_if_no_peer_cert */
"once",
NULL };
/***
get verify_mode, return number mode and all string modes list
@function verify_mode
@treturn number mode_code
@return ...
none: not verify client cert
peer: verify client cert
fail: if client not have cert, will failure
once: verify client only once.
@usage
mode = {ctx:verify_mode()}
print('integer mode',mode[1])
for i=2, #mode then
print('string mode:'..mode[i])
end
*/
/***
set ssl verify mode and callback
@function verify_mode
@tparam number mode, mode set to ctx, must be ssl.none or ssl.peer, and ssl.peer support combine
with ssl.fail or ssl.once
@tparam[opt=nil] function ssl verify callback in lua function, not give will use default openssl
callback, when mode is 'none', will be ignore this verify_cb must be boolean function(verifyarg)
prototype, return true to continue or false to end ssl handshake verifyarg has field 'error',
'error_string','error_depth','current_cert', and 'preverify_ok'
@treturn boolean result
*/
static int
openssl_ssl_ctx_verify_mode(lua_State *L)
{
SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
if (lua_gettop(L) > 1) {
int mode = luaL_checkint(L, 2);
luaL_argcheck(
L,
mode == SSL_VERIFY_NONE
|| (mode & ~(SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT | SSL_VERIFY_CLIENT_ONCE))
== 0,
2,
"must be none or peer(combined with fail, once or none");
luaL_argcheck(L, lua_isnone(L, 3) || lua_isfunction(L, 3), 3, "must be callback function");
if (lua_isfunction(L, 3)) {
lua_pushvalue(L, 3);
openssl_valueset(L, ctx, "verify_cb");
SSL_CTX_set_verify(ctx, mode, openssl_verify_cb);
} else {
lua_pushnil(L);
openssl_valueset(L, ctx, "verify_cb");
SSL_CTX_set_verify(ctx, mode, openssl_verify_cb);
}
return 0;
} else {
int i = 0;
int mode = SSL_CTX_get_verify_mode(ctx);
lua_pushinteger(L, mode);
i += 1;
if (mode == SSL_VERIFY_NONE) {
lua_pushstring(L, "none");
i += 1;
} else {
if (mode & SSL_VERIFY_PEER) {
lua_pushstring(L, "peer");
i += 1;
if (mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
lua_pushstring(L, "fail");
i += 1;
}
if (mode & SSL_VERIFY_CLIENT_ONCE) {
lua_pushstring(L, "once");
i += 1;
}
}
}
return i;
}
}
/***
set certificate verify callback function
@function set_cert_verify
@tparam[opt] function cert_verify_cb with boolean function(verifyargs) prototype, if nil or none
will use openssl default callback verifyargs has field 'error',
'error_string','error_depth','current_cert'
@treturn various return value
*/
/***
set certificate verify options
@function set_cert_verify
@tparam table verify_cb_flag support field always_continue with boolean value and verify_depth with
number value.
@treturn boolean result true for success
*/
static int
openssl_ssl_ctx_set_cert_verify(lua_State *L)
{
SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
luaL_argcheck(L,
lua_isnone(L, 2) || lua_isfunction(L, 2) || lua_istable(L, 2),
2,
"need function or table contains flags");
if (lua_istable(L, 2)) {
lua_pushvalue(L, 2);
openssl_valueset(L, ctx, "verify_cb_flags");
SSL_CTX_set_cert_verify_callback(ctx, openssl_cert_verify_cb, openssl_mainthread(L));
} else if (lua_isfunction(L, 2)) {
lua_pushvalue(L, 2);
openssl_valueset(L, ctx, "cert_verify_cb");
SSL_CTX_set_cert_verify_callback(ctx, openssl_cert_verify_cb, openssl_mainthread(L));
} else
SSL_CTX_set_cert_verify_callback(ctx, NULL, NULL);
return 0;
}
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
/***
set the list of client ALPN protocols available to be negotiated by the server
@function set_alpn_protos
@tparam table protos the protocol list
@treturn various return value
*/
static int
openssl_ssl_ctx_set_alpn_protos(lua_State *L)
{
SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
if (lua_istable(L, 2)) {
size_t proto_list_len = 0;
size_t proto_list_size = 256;
unsigned char *proto_list = malloc(proto_list_size);
size_t proto_len;
const char *proto;
int i;
int l = lua_rawlen(L, 2);
char *err = NULL;
for (i = 1; i <= l; i++) {
lua_rawgeti(L, 2, i);
proto = lua_tolstring(L, -1, &proto_len);