ltls.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5. #include <openssl/err.h>
  6. #include <openssl/dh.h>
  7. #include <openssl/ssl.h>
  8. #include <openssl/conf.h>
  9. #include <openssl/engine.h>
  10. #include <lua.h>
  11. #include <lauxlib.h>
  12. static bool TLS_IS_INIT = false;
  13. struct tls_context {
  14. SSL* ssl;
  15. BIO* in_bio;
  16. BIO* out_bio;
  17. bool is_server;
  18. bool is_close;
  19. };
  20. struct ssl_ctx {
  21. SSL_CTX* ctx;
  22. };
  23. // static int
  24. // _ssl_verify_peer(int ok, X509_STORE_CTX* ctx) {
  25. // return 1;
  26. // }
  27. static void
  28. _init_bio(lua_State* L, struct tls_context* tls_p, struct ssl_ctx* ctx_p) {
  29. tls_p->ssl = SSL_new(ctx_p->ctx);
  30. if(!tls_p->ssl) {
  31. luaL_error(L, "SSL_new faild");
  32. }
  33. tls_p->in_bio = BIO_new(BIO_s_mem());
  34. if(!tls_p->in_bio) {
  35. luaL_error(L, "new in bio faild");
  36. }
  37. BIO_set_mem_eof_return(tls_p->in_bio, -1); /* see: https://www.openssl.org/docs/crypto/BIO_s_mem.html */
  38. tls_p->out_bio = BIO_new(BIO_s_mem());
  39. if(!tls_p->out_bio) {
  40. luaL_error(L, "new out bio faild");
  41. }
  42. BIO_set_mem_eof_return(tls_p->out_bio, -1); /* see: https://www.openssl.org/docs/crypto/BIO_s_mem.html */
  43. SSL_set_bio(tls_p->ssl, tls_p->in_bio, tls_p->out_bio);
  44. }
  45. static void
  46. _init_client_context(lua_State* L, struct tls_context* tls_p, struct ssl_ctx* ctx_p) {
  47. tls_p->is_server = false;
  48. _init_bio(L, tls_p, ctx_p);
  49. SSL_set_connect_state(tls_p->ssl);
  50. }
  51. static void
  52. _init_server_context(lua_State* L, struct tls_context* tls_p, struct ssl_ctx* ctx_p) {
  53. tls_p->is_server = true;
  54. _init_bio(L, tls_p, ctx_p);
  55. SSL_set_accept_state(tls_p->ssl);
  56. }
  57. static struct tls_context *
  58. _check_context(lua_State* L, int idx) {
  59. struct tls_context* tls_p = (struct tls_context*)lua_touserdata(L, idx);
  60. if(!tls_p) {
  61. luaL_error(L, "need tls context");
  62. }
  63. if(tls_p->is_close) {
  64. luaL_error(L, "context is closed");
  65. }
  66. return tls_p;
  67. }
  68. static struct ssl_ctx *
  69. _check_sslctx(lua_State* L, int idx) {
  70. struct ssl_ctx* ctx_p = (struct ssl_ctx*)lua_touserdata(L, idx);
  71. if(!ctx_p) {
  72. luaL_error(L, "need sslctx");
  73. }
  74. return ctx_p;
  75. }
  76. static int
  77. _ltls_context_finished(lua_State* L) {
  78. struct tls_context* tls_p = _check_context(L, 1);
  79. int b = SSL_is_init_finished(tls_p->ssl);
  80. lua_pushboolean(L, b);
  81. return 1;
  82. }
  83. static int
  84. _ltls_context_close(lua_State* L) {
  85. struct tls_context* tls_p = lua_touserdata(L, 1);
  86. if(!tls_p->is_close) {
  87. SSL_free(tls_p->ssl);
  88. tls_p->ssl = NULL;
  89. tls_p->in_bio = NULL; //in_bio and out_bio will be free when SSL_free is called
  90. tls_p->out_bio = NULL;
  91. tls_p->is_close = true;
  92. }
  93. return 0;
  94. }
  95. static int
  96. _bio_read(lua_State* L, struct tls_context* tls_p) {
  97. char outbuff[4096];
  98. int all_read = 0;
  99. int read = 0;
  100. int pending = BIO_ctrl_pending(tls_p->out_bio);
  101. if(pending >0) {
  102. luaL_Buffer b;
  103. luaL_buffinit(L, &b);
  104. while(pending > 0) {
  105. read = BIO_read(tls_p->out_bio, outbuff, sizeof(outbuff));
  106. // printf("BIO_read read:%d pending:%d\n", read, pending);
  107. if(read <= 0) {
  108. luaL_error(L, "BIO_read error:%d", read);
  109. }else if(read <= sizeof(outbuff)) {
  110. all_read += read;
  111. luaL_addlstring(&b, (const char*)outbuff, read);
  112. }else {
  113. luaL_error(L, "invalid BIO_read:%d", read);
  114. }
  115. pending = BIO_ctrl_pending(tls_p->out_bio);
  116. }
  117. if(all_read>0) {
  118. luaL_pushresult(&b);
  119. }
  120. }
  121. return all_read;
  122. }
  123. static void
  124. _bio_write(lua_State* L, struct tls_context* tls_p, const char* s, size_t len) {
  125. char* p = (char*)s;
  126. size_t sz = len;
  127. while(sz > 0) {
  128. int written = BIO_write(tls_p->in_bio, p, sz);
  129. // printf("BIO_write written:%d sz:%zu\n", written, sz);
  130. if(written <= 0) {
  131. luaL_error(L, "BIO_write error:%d", written);
  132. }else if (written <= sz) {
  133. p += written;
  134. sz -= written;
  135. }else {
  136. luaL_error(L, "invalid BIO_write:%d", written);
  137. }
  138. }
  139. }
  140. static int
  141. _ltls_context_handshake(lua_State* L) {
  142. struct tls_context* tls_p = _check_context(L, 1);
  143. size_t slen = 0;
  144. const char* exchange = lua_tolstring(L, 2, &slen);
  145. // check handshake is finished
  146. if(SSL_is_init_finished(tls_p->ssl)) {
  147. luaL_error(L, "handshake is finished");
  148. }
  149. // handshake exchange
  150. if(slen > 0 && exchange != NULL) {
  151. _bio_write(L, tls_p, exchange, slen);
  152. }
  153. // first handshake; initiated by client
  154. if(!SSL_is_init_finished(tls_p->ssl)) {
  155. int ret = SSL_do_handshake(tls_p->ssl);
  156. if(ret == 1) {
  157. return 0;
  158. } else if (ret < 0) {
  159. int err = SSL_get_error(tls_p->ssl, ret);
  160. ERR_clear_error();
  161. if(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
  162. int all_read = _bio_read(L, tls_p);
  163. if(all_read>0) {
  164. return 1;
  165. }
  166. } else {
  167. luaL_error(L, "SSL_do_handshake error:%d ret:%d", err, ret);
  168. }
  169. } else {
  170. int err = SSL_get_error(tls_p->ssl, ret);
  171. ERR_clear_error();
  172. luaL_error(L, "SSL_do_handshake error:%d ret:%d", err, ret);
  173. }
  174. }
  175. return 0;
  176. }
  177. static int
  178. _ltls_context_read(lua_State* L) {
  179. struct tls_context* tls_p = _check_context(L, 1);
  180. size_t slen = 0;
  181. const char* encrypted_data = lua_tolstring(L, 2, &slen);
  182. // write encrypted data
  183. if(slen>0 && encrypted_data) {
  184. _bio_write(L, tls_p, encrypted_data, slen);
  185. }
  186. char outbuff[4096];
  187. int read = 0;
  188. luaL_Buffer b;
  189. luaL_buffinit(L, &b);
  190. do {
  191. read = SSL_read(tls_p->ssl, outbuff, sizeof(outbuff));
  192. if(read < 0) {
  193. int err = SSL_get_error(tls_p->ssl, read);
  194. ERR_clear_error();
  195. if(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
  196. break;
  197. }
  198. luaL_error(L, "SSL_read error:%d", err);
  199. }else if(read == 0){
  200. break;
  201. }
  202. else if(read <= sizeof(outbuff)) {
  203. luaL_addlstring(&b, outbuff, read);
  204. }else {
  205. luaL_error(L, "invalid SSL_read:%d", read);
  206. }
  207. }while(true);
  208. luaL_pushresult(&b);
  209. return 1;
  210. }
  211. static int
  212. _ltls_context_write(lua_State* L) {
  213. struct tls_context* tls_p = _check_context(L, 1);
  214. size_t slen = 0;
  215. char* unencrypted_data = (char*)lua_tolstring(L, 2, &slen);
  216. while(slen > 0) {
  217. int written = SSL_write(tls_p->ssl, unencrypted_data, slen);
  218. if(written <= 0) {
  219. int err = SSL_get_error(tls_p->ssl, written);
  220. ERR_clear_error();
  221. luaL_error(L, "SSL_write error:%d", err);
  222. }else if(written <= slen) {
  223. unencrypted_data += written;
  224. slen -= written;
  225. }else {
  226. luaL_error(L, "invalid SSL_write:%d", written);
  227. }
  228. }
  229. int all_read = _bio_read(L, tls_p);
  230. if(all_read <= 0) {
  231. lua_pushstring(L, "");
  232. }
  233. return 1;
  234. }
  235. static int
  236. _lctx_gc(lua_State* L) {
  237. struct ssl_ctx* ctx_p = _check_sslctx(L, 1);
  238. if(ctx_p->ctx) {
  239. SSL_CTX_free(ctx_p->ctx);
  240. ctx_p->ctx = NULL;
  241. }
  242. return 0;
  243. }
  244. static int
  245. _lctx_cert(lua_State* L) {
  246. struct ssl_ctx* ctx_p = _check_sslctx(L, 1);
  247. const char* certfile = lua_tostring(L, 2);
  248. const char* key = lua_tostring(L, 3);
  249. if(!certfile) {
  250. luaL_error(L, "need certfile");
  251. }
  252. if(!key) {
  253. luaL_error(L, "need private key");
  254. }
  255. int ret = SSL_CTX_use_certificate_chain_file(ctx_p->ctx, certfile);
  256. if(ret != 1) {
  257. luaL_error(L, "SSL_CTX_use_certificate_chain_file error:%d", ret);
  258. }
  259. ret = SSL_CTX_use_PrivateKey_file(ctx_p->ctx, key, SSL_FILETYPE_PEM);
  260. if(ret != 1) {
  261. luaL_error(L, "SSL_CTX_use_PrivateKey_file error:%d", ret);
  262. }
  263. ret = SSL_CTX_check_private_key(ctx_p->ctx);
  264. if(ret != 1) {
  265. luaL_error(L, "SSL_CTX_check_private_key error:%d", ret);
  266. }
  267. return 0;
  268. }
  269. static int
  270. _lctx_ciphers(lua_State* L) {
  271. struct ssl_ctx* ctx_p = _check_sslctx(L, 1);
  272. const char* s = lua_tostring(L, 2);
  273. if(!s) {
  274. luaL_error(L, "need cipher list");
  275. }
  276. int ret = SSL_CTX_set_tlsext_use_srtp(ctx_p->ctx, s);
  277. if(ret != 0) {
  278. luaL_error(L, "SSL_CTX_set_tlsext_use_srtp error:%d", ret);
  279. }
  280. return 0;
  281. }
  282. static int
  283. lnew_ctx(lua_State* L) {
  284. struct ssl_ctx* ctx_p = (struct ssl_ctx*)lua_newuserdatauv(L, sizeof(*ctx_p), 0);
  285. ctx_p->ctx = SSL_CTX_new(SSLv23_method());
  286. if(!ctx_p->ctx) {
  287. unsigned int err = ERR_get_error();
  288. char buf[256];
  289. ERR_error_string_n(err, buf, sizeof(buf));
  290. luaL_error(L, "SSL_CTX_new client faild. %s\n", buf);
  291. }
  292. if(luaL_newmetatable(L, "_TLS_SSLCTX_METATABLE_")) {
  293. luaL_Reg l[] = {
  294. {"set_ciphers", _lctx_ciphers},
  295. {"set_cert", _lctx_cert},
  296. {NULL, NULL},
  297. };
  298. luaL_newlib(L, l);
  299. lua_setfield(L, -2, "__index");
  300. lua_pushcfunction(L, _lctx_gc);
  301. lua_setfield(L, -2, "__gc");
  302. }
  303. lua_setmetatable(L, -2);
  304. return 1;
  305. }
  306. static int
  307. lnew_tls(lua_State* L) {
  308. struct tls_context* tls_p = (struct tls_context*)lua_newuserdatauv(L, sizeof(*tls_p), 1);
  309. tls_p->is_close = false;
  310. const char* method = luaL_optstring(L, 1, "nil");
  311. struct ssl_ctx* ctx_p = _check_sslctx(L, 2);
  312. lua_pushvalue(L, 2);
  313. lua_setiuservalue(L, -2, 1); // set ssl_ctx associated to tls_context
  314. if(strcmp(method, "client") == 0) {
  315. _init_client_context(L, tls_p, ctx_p);
  316. if (!lua_isnoneornil(L, 3)) {
  317. const char* hostname = luaL_checkstring(L, 3);
  318. SSL_set_tlsext_host_name(tls_p->ssl, hostname);
  319. }
  320. }else if(strcmp(method, "server") == 0) {
  321. _init_server_context(L, tls_p, ctx_p);
  322. } else {
  323. luaL_error(L, "invalid method:%s e.g[server, client]", method);
  324. }
  325. if(luaL_newmetatable(L, "_TLS_CONTEXT_METATABLE_")) {
  326. luaL_Reg l[] = {
  327. {"close", _ltls_context_close},
  328. {"finished", _ltls_context_finished},
  329. {"handshake", _ltls_context_handshake},
  330. {"read", _ltls_context_read},
  331. {"write", _ltls_context_write},
  332. {NULL, NULL},
  333. };
  334. luaL_newlib(L, l);
  335. lua_setfield(L, -2, "__index");
  336. lua_pushcfunction(L, _ltls_context_close);
  337. lua_setfield(L, -2, "__gc");
  338. }
  339. lua_setmetatable(L, -2);
  340. return 1;
  341. }
  342. int
  343. luaopen_ltls_c(lua_State* L) {
  344. if(!TLS_IS_INIT) {
  345. luaL_error(L, "ltls need init, Put enablessl = true in you config file.");
  346. }
  347. luaL_Reg l[] = {
  348. {"newctx", lnew_ctx},
  349. {"newtls", lnew_tls},
  350. {NULL, NULL},
  351. };
  352. luaL_checkversion(L);
  353. luaL_newlib(L, l);
  354. return 1;
  355. }
  356. // for ltls init
  357. static int
  358. ltls_init_constructor(lua_State* L) {
  359. #ifndef OPENSSL_EXTERNAL_INITIALIZATION
  360. if(!TLS_IS_INIT) {
  361. SSL_library_init();
  362. SSL_load_error_strings();
  363. #if OPENSSL_VERSION_NUMBER < 0x30000000L
  364. ERR_load_BIO_strings();
  365. #endif
  366. OpenSSL_add_all_algorithms();
  367. }
  368. #endif
  369. TLS_IS_INIT = true;
  370. return 0;
  371. }
  372. static int
  373. ltls_init_destructor(lua_State* L) {
  374. #ifndef OPENSSL_EXTERNAL_INITIALIZATION
  375. if(TLS_IS_INIT) {
  376. ENGINE_cleanup();
  377. CONF_modules_unload(1);
  378. ERR_free_strings();
  379. EVP_cleanup();
  380. sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
  381. CRYPTO_cleanup_all_ex_data();
  382. }
  383. #endif
  384. TLS_IS_INIT = false;
  385. return 0;
  386. }
  387. int
  388. luaopen_ltls_init_c(lua_State* L) {
  389. luaL_Reg l[] = {
  390. {"constructor", ltls_init_constructor},
  391. {"destructor", ltls_init_destructor},
  392. {NULL, NULL},
  393. };
  394. luaL_checkversion(L);
  395. luaL_newlib(L, l);
  396. return 1;
  397. }