Skip to content

Commit 95a4109

Browse files
committed
cleanup
1 parent d6406af commit 95a4109

File tree

9 files changed

+236
-281
lines changed

9 files changed

+236
-281
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

.luacheckrc

Lines changed: 0 additions & 18 deletions
This file was deleted.

examples/tcp_client_blocking_tls.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
local socket = require("ljsocket")
22
local host = "www.google.com"
33
local socket = assert(socket.create("inet", "stream", "tcp"))
4-
local ssl = require("ssl")
4+
local ssl = require("ljtls")
55
do
66
local tls = ssl.tls_client()
77

examples/tcp_client_nonblocking_tls.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
local socket = require("ljsocket")
22
local host = "google.com"
33
local socket = assert(socket.create("inet", "stream", "tcp"))
4-
local ssl = require("ssl")
4+
local ssl = require("ljtls")
55
do
66
local tls = ssl.tls_client()
77

libtls.dylib

-58.9 KB
Binary file not shown.

libtls.so

-371 KB
Binary file not shown.

ssl.lua renamed to ljtls.lua

Lines changed: 232 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
-- these have mostly all been thrown up by ai and likely need fixing
2-
32
local ffi = require("ffi")
43
local socket = require("ljsocket")
54
local ssl = {}
@@ -436,7 +435,238 @@ local loaders = {
436435
}
437436
end,
438437
function()
439-
local lib = require("tls")
438+
local CLIB
439+
440+
if ffi.os == "OSX" then
441+
CLIB = assert(ffi.load("./libtls.dylib"))
442+
elseif ffi.os == "Windows" then
443+
CLIB = assert(ffi.load("./tls.dll"))
444+
else
445+
CLIB = assert(ffi.load("./libtls.so"))
446+
end
447+
448+
ffi.cdef([[struct tls {};
449+
struct tls_config {};
450+
const char*(tls_peer_ocsp_url)(struct tls*);
451+
int(tls_config_set_dheparams)(struct tls_config*,const char*);
452+
int(tls_config_set_keypair_file)(struct tls_config*,const char*,const char*);
453+
const char*(tls_conn_version)(struct tls*);
454+
int(tls_conn_session_resumed)(struct tls*);
455+
int(tls_config_set_ca_file)(struct tls_config*,const char*);
456+
int(tls_config_set_ciphers)(struct tls_config*,const char*);
457+
int(tls_ocsp_process_response)(struct tls*,const unsigned char*,unsigned long);
458+
int(tls_peer_ocsp_cert_status)(struct tls*);
459+
void(tls_config_insecure_noverifytime)(struct tls_config*);
460+
int(tls_config_add_keypair_mem)(struct tls_config*,const unsigned char*,unsigned long,const unsigned char*,unsigned long);
461+
int(tls_config_set_cert_mem)(struct tls_config*,const unsigned char*,unsigned long);
462+
const char*(tls_config_error)(struct tls_config*);
463+
int(tls_config_set_ocsp_staple_file)(struct tls_config*,const char*);
464+
const char*(tls_peer_ocsp_result)(struct tls*);
465+
void(tls_config_verify_client)(struct tls_config*);
466+
int(tls_config_add_keypair_ocsp_mem)(struct tls_config*,const unsigned char*,unsigned long,const unsigned char*,unsigned long,const unsigned char*,unsigned long);
467+
int(tls_connect_cbs)(struct tls*,long(*_read_cb)(struct tls*,void*,unsigned long,void*),long(*_write_cb)(struct tls*,const void*,unsigned long,void*),void*,const char*);
468+
struct tls_config*(tls_config_new)();
469+
void(tls_config_insecure_noverifycert)(struct tls_config*);
470+
int(tls_config_set_key_file)(struct tls_config*,const char*);
471+
long(tls_peer_ocsp_next_update)(struct tls*);
472+
int(tls_config_set_cert_file)(struct tls_config*,const char*);
473+
int(tls_handshake)(struct tls*);
474+
struct tls*(tls_server)();
475+
int(tls_config_set_crl_mem)(struct tls_config*,const unsigned char*,unsigned long);
476+
void(tls_config_ocsp_require_stapling)(struct tls_config*);
477+
int(tls_config_parse_protocols)(unsigned int*,const char*);
478+
void(tls_config_verify_client_optional)(struct tls_config*);
479+
void(tls_config_verify)(struct tls_config*);
480+
int(tls_config_set_alpn)(struct tls_config*,const char*);
481+
int(tls_connect_fds)(struct tls*,int,int,const char*);
482+
void(tls_config_free)(struct tls_config*);
483+
int(tls_config_set_ocsp_staple_mem)(struct tls_config*,const unsigned char*,unsigned long);
484+
void(tls_free)(struct tls*);
485+
int(tls_config_set_verify_depth)(struct tls_config*,int);
486+
int(tls_config_set_ecdhecurve)(struct tls_config*,const char*);
487+
long(tls_peer_ocsp_this_update)(struct tls*);
488+
long(tls_peer_ocsp_revocation_time)(struct tls*);
489+
const char*(tls_conn_cipher)(struct tls*);
490+
int(tls_peer_ocsp_response_status)(struct tls*);
491+
void(tls_unload_file)(unsigned char*,unsigned long);
492+
int(tls_connect)(struct tls*,const char*,const char*);
493+
int(tls_peer_ocsp_crl_reason)(struct tls*);
494+
unsigned char*(tls_load_file)(const char*,unsigned long*,char*);
495+
const char*(tls_default_ca_cert_file)();
496+
const char*(tls_conn_alpn_selected)(struct tls*);
497+
const char*(tls_peer_cert_hash)(struct tls*);
498+
int(tls_config_add_ticket_key)(struct tls_config*,unsigned int,unsigned char*,unsigned long);
499+
int(tls_config_set_ecdhecurves)(struct tls_config*,const char*);
500+
void(tls_config_prefer_ciphers_client)(struct tls_config*);
501+
int(tls_accept_fds)(struct tls*,struct tls**,int,int);
502+
long(tls_peer_cert_notafter)(struct tls*);
503+
long(tls_peer_cert_notbefore)(struct tls*);
504+
int(tls_peer_cert_provided)(struct tls*);
505+
int(tls_accept_cbs)(struct tls*,struct tls**,long(*_read_cb)(struct tls*,void*,unsigned long,void*),long(*_write_cb)(struct tls*,const void*,unsigned long,void*),void*);
506+
const char*(tls_peer_cert_subject)(struct tls*);
507+
int(tls_config_add_keypair_ocsp_file)(struct tls_config*,const char*,const char*,const char*);
508+
int(tls_accept_socket)(struct tls*,struct tls**,int);
509+
const char*(tls_peer_cert_issuer)(struct tls*);
510+
int(tls_init)();
511+
int(tls_peer_cert_contains_name)(struct tls*,const char*);
512+
int(tls_connect_servername)(struct tls*,const char*,const char*,const char*);
513+
const char*(tls_error)(struct tls*);
514+
int(tls_close)(struct tls*);
515+
long(tls_write)(struct tls*,const void*,unsigned long);
516+
long(tls_read)(struct tls*,void*,unsigned long);
517+
int(tls_connect_socket)(struct tls*,int,const char*);
518+
int(tls_config_set_crl_file)(struct tls_config*,const char*);
519+
struct tls*(tls_client)();
520+
int(tls_configure)(struct tls*,struct tls_config*);
521+
int(tls_config_set_keypair_mem)(struct tls_config*,const unsigned char*,unsigned long,const unsigned char*,unsigned long);
522+
int(tls_config_set_ca_path)(struct tls_config*,const char*);
523+
void(tls_config_insecure_noverifyname)(struct tls_config*);
524+
const char*(tls_conn_servername)(struct tls*);
525+
int(tls_config_set_keypair_ocsp_mem)(struct tls_config*,const unsigned char*,unsigned long,const unsigned char*,unsigned long,const unsigned char*,unsigned long);
526+
int(tls_config_add_keypair_file)(struct tls_config*,const char*,const char*);
527+
int(tls_config_set_protocols)(struct tls_config*,unsigned int);
528+
void(tls_reset)(struct tls*);
529+
int(tls_config_set_key_mem)(struct tls_config*,const unsigned char*,unsigned long);
530+
const unsigned char*(tls_peer_cert_chain_pem)(struct tls*,unsigned long*);
531+
int(tls_config_set_session_lifetime)(struct tls_config*,int);
532+
int(tls_config_set_keypair_ocsp_file)(struct tls_config*,const char*,const char*,const char*);
533+
void(tls_config_prefer_ciphers_server)(struct tls_config*);
534+
int(tls_config_set_ca_mem)(struct tls_config*,const unsigned char*,unsigned long);
535+
int(tls_config_set_session_id)(struct tls_config*,const unsigned char*,unsigned long);
536+
int(tls_config_set_session_fd)(struct tls_config*,int);
537+
void(tls_config_clear_keys)(struct tls_config*);
538+
]])
539+
local library = {}
540+
library = {
541+
tls_peer_ocsp_url = CLIB.tls_peer_ocsp_url,
542+
tls_config_set_dheparams = CLIB.tls_config_set_dheparams,
543+
tls_config_set_keypair_file = CLIB.tls_config_set_keypair_file,
544+
tls_conn_version = CLIB.tls_conn_version,
545+
tls_conn_session_resumed = CLIB.tls_conn_session_resumed,
546+
tls_config_set_ca_file = CLIB.tls_config_set_ca_file,
547+
tls_config_set_ciphers = CLIB.tls_config_set_ciphers,
548+
tls_ocsp_process_response = CLIB.tls_ocsp_process_response,
549+
tls_peer_ocsp_cert_status = CLIB.tls_peer_ocsp_cert_status,
550+
tls_config_insecure_noverifytime = CLIB.tls_config_insecure_noverifytime,
551+
tls_config_add_keypair_mem = CLIB.tls_config_add_keypair_mem,
552+
tls_config_set_cert_mem = CLIB.tls_config_set_cert_mem,
553+
tls_config_error = CLIB.tls_config_error,
554+
tls_config_set_ocsp_staple_file = CLIB.tls_config_set_ocsp_staple_file,
555+
tls_peer_ocsp_result = CLIB.tls_peer_ocsp_result,
556+
__debugbreak = CLIB.__debugbreak,
557+
tls_config_verify_client = CLIB.tls_config_verify_client,
558+
tls_config_add_keypair_ocsp_mem = CLIB.tls_config_add_keypair_ocsp_mem,
559+
tls_connect_cbs = CLIB.tls_connect_cbs,
560+
tls_config_new = CLIB.tls_config_new,
561+
tls_config_insecure_noverifycert = CLIB.tls_config_insecure_noverifycert,
562+
tls_config_set_key_file = CLIB.tls_config_set_key_file,
563+
tls_peer_ocsp_next_update = CLIB.tls_peer_ocsp_next_update,
564+
tls_config_set_cert_file = CLIB.tls_config_set_cert_file,
565+
tls_handshake = CLIB.tls_handshake,
566+
tls_server = CLIB.tls_server,
567+
tls_config_set_crl_mem = CLIB.tls_config_set_crl_mem,
568+
tls_config_ocsp_require_stapling = CLIB.tls_config_ocsp_require_stapling,
569+
tls_config_parse_protocols = CLIB.tls_config_parse_protocols,
570+
tls_config_verify_client_optional = CLIB.tls_config_verify_client_optional,
571+
tls_config_verify = CLIB.tls_config_verify,
572+
tls_config_set_alpn = CLIB.tls_config_set_alpn,
573+
tls_connect_fds = CLIB.tls_connect_fds,
574+
tls_config_free = CLIB.tls_config_free,
575+
tls_config_set_ocsp_staple_mem = CLIB.tls_config_set_ocsp_staple_mem,
576+
tls_free = CLIB.tls_free,
577+
tls_config_set_verify_depth = CLIB.tls_config_set_verify_depth,
578+
tls_config_set_ecdhecurve = CLIB.tls_config_set_ecdhecurve,
579+
_errno = CLIB._errno,
580+
tls_peer_ocsp_this_update = CLIB.tls_peer_ocsp_this_update,
581+
tls_peer_ocsp_revocation_time = CLIB.tls_peer_ocsp_revocation_time,
582+
tls_conn_cipher = CLIB.tls_conn_cipher,
583+
tls_peer_ocsp_response_status = CLIB.tls_peer_ocsp_response_status,
584+
tls_unload_file = CLIB.tls_unload_file,
585+
tls_connect = CLIB.tls_connect,
586+
tls_peer_ocsp_crl_reason = CLIB.tls_peer_ocsp_crl_reason,
587+
tls_load_file = CLIB.tls_load_file,
588+
tls_default_ca_cert_file = CLIB.tls_default_ca_cert_file,
589+
__threadhandle = CLIB.__threadhandle,
590+
tls_conn_alpn_selected = CLIB.tls_conn_alpn_selected,
591+
tls_peer_cert_hash = CLIB.tls_peer_cert_hash,
592+
tls_config_add_ticket_key = CLIB.tls_config_add_ticket_key,
593+
tls_config_set_ecdhecurves = CLIB.tls_config_set_ecdhecurves,
594+
tls_config_prefer_ciphers_client = CLIB.tls_config_prefer_ciphers_client,
595+
tls_accept_fds = CLIB.tls_accept_fds,
596+
tls_peer_cert_notafter = CLIB.tls_peer_cert_notafter,
597+
tls_peer_cert_notbefore = CLIB.tls_peer_cert_notbefore,
598+
tls_peer_cert_provided = CLIB.tls_peer_cert_provided,
599+
tls_accept_cbs = CLIB.tls_accept_cbs,
600+
tls_peer_cert_subject = CLIB.tls_peer_cert_subject,
601+
tls_config_add_keypair_ocsp_file = CLIB.tls_config_add_keypair_ocsp_file,
602+
tls_accept_socket = CLIB.tls_accept_socket,
603+
tls_peer_cert_issuer = CLIB.tls_peer_cert_issuer,
604+
tls_init = CLIB.tls_init,
605+
tls_peer_cert_contains_name = CLIB.tls_peer_cert_contains_name,
606+
tls_connect_servername = CLIB.tls_connect_servername,
607+
tls_error = CLIB.tls_error,
608+
tls_close = CLIB.tls_close,
609+
tls_write = CLIB.tls_write,
610+
tls_read = CLIB.tls_read,
611+
tls_connect_socket = CLIB.tls_connect_socket,
612+
tls_config_set_crl_file = CLIB.tls_config_set_crl_file,
613+
tls_client = CLIB.tls_client,
614+
tls_configure = CLIB.tls_configure,
615+
_set_errno = CLIB._set_errno,
616+
tls_config_set_keypair_mem = CLIB.tls_config_set_keypair_mem,
617+
_get_errno = CLIB._get_errno,
618+
tls_config_set_ca_path = CLIB.tls_config_set_ca_path,
619+
tls_config_insecure_noverifyname = CLIB.tls_config_insecure_noverifyname,
620+
tls_conn_servername = CLIB.tls_conn_servername,
621+
tls_config_set_keypair_ocsp_mem = CLIB.tls_config_set_keypair_ocsp_mem,
622+
tls_config_add_keypair_file = CLIB.tls_config_add_keypair_file,
623+
tls_config_set_protocols = CLIB.tls_config_set_protocols,
624+
tls_reset = CLIB.tls_reset,
625+
tls_config_set_key_mem = CLIB.tls_config_set_key_mem,
626+
tls_peer_cert_chain_pem = CLIB.tls_peer_cert_chain_pem,
627+
tls_config_set_session_lifetime = CLIB.tls_config_set_session_lifetime,
628+
tls_config_set_keypair_ocsp_file = CLIB.tls_config_set_keypair_ocsp_file,
629+
__mingw_get_crt_info = CLIB.__mingw_get_crt_info,
630+
tls_config_prefer_ciphers_server = CLIB.tls_config_prefer_ciphers_server,
631+
__threadid = CLIB.__threadid,
632+
tls_config_set_ca_mem = CLIB.tls_config_set_ca_mem,
633+
tls_config_set_session_id = CLIB.tls_config_set_session_id,
634+
tls_config_set_session_fd = CLIB.tls_config_set_session_fd,
635+
tls_config_clear_keys = CLIB.tls_config_clear_keys,
636+
}
637+
library.e = {
638+
HEADER_TLS_H = 1,
639+
TLS_API = 20180210,
640+
TLS_PROTOCOL_TLSv1_0 = 2,
641+
TLS_PROTOCOL_TLSv1_1 = 4,
642+
TLS_PROTOCOL_TLSv1_2 = 8,
643+
TLS_PROTOCOLS_DEFAULT = 8,
644+
TLS_WANT_POLLIN = -2,
645+
TLS_WANT_POLLOUT = -3,
646+
TLS_OCSP_RESPONSE_SUCCESSFUL = 0,
647+
TLS_OCSP_RESPONSE_MALFORMED = 1,
648+
TLS_OCSP_RESPONSE_INTERNALERROR = 2,
649+
TLS_OCSP_RESPONSE_TRYLATER = 3,
650+
TLS_OCSP_RESPONSE_SIGREQUIRED = 4,
651+
TLS_OCSP_RESPONSE_UNAUTHORIZED = 5,
652+
TLS_OCSP_CERT_GOOD = 0,
653+
TLS_OCSP_CERT_REVOKED = 1,
654+
TLS_OCSP_CERT_UNKNOWN = 2,
655+
TLS_CRL_REASON_UNSPECIFIED = 0,
656+
TLS_CRL_REASON_KEY_COMPROMISE = 1,
657+
TLS_CRL_REASON_CA_COMPROMISE = 2,
658+
TLS_CRL_REASON_AFFILIATION_CHANGED = 3,
659+
TLS_CRL_REASON_SUPERSEDED = 4,
660+
TLS_CRL_REASON_CESSATION_OF_OPERATION = 5,
661+
TLS_CRL_REASON_CERTIFICATE_HOLD = 6,
662+
TLS_CRL_REASON_REMOVE_FROM_CRL = 8,
663+
TLS_CRL_REASON_PRIVILEGE_WITHDRAWN = 9,
664+
TLS_CRL_REASON_AA_COMPROMISE = 10,
665+
TLS_MAX_SESSION_ID_LENGTH = 32,
666+
TLS_TICKET_KEY_SIZE = 48,
667+
}
668+
library.clib = CLIB
669+
local lib = library
440670

441671
if not lib.initialized then
442672
lib.tls_init()

readme.md

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
Slightly resembles luasocket's core module, but it's a bit more low level and tries to follow the unix socket api.
1+
This is a pure luajit binding for sockets on linux, macos and windows with no binary dependencies. It slightly resembles luasocket's core module, but it's a bit more low level and tries to follow the bsd socket api. It aims to just be a single file that's easy to copy paste. :)
22

3-
Assuming you have luajit installed, you can test the library by run the examples like this:
4-
`luajit examples/tcp_client_blocking_tls.lua`
5-
6-
The TLS client examples uses libtls (LibreSSL) which in turn depends on libssl and libcrypto. `tls.lua` has been auto generated based on libtls' headers.
7-
8-
It seems to be working, but I haven't explored paths other than TCP and UDP. TCP is the one I've used this the most with. My intention is to keep this close to how it works on the OS level.
9-
10-
High level abstractions are out of scope in this library. It also aims to just be a single file that's easy to copy paste. :)
3+
There is an additional ljtls.lua binding for various OS specific TLS implementations. While it seems to be working on the surface, the code is 80% AI slop. It also only implements client use. It serves as a working starting point at least for something more serious.

0 commit comments

Comments
 (0)