You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lua_rsa.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * @file lua_rsa.c
  18. * This module exports routines to load rsa keys, check inline or external
  19. * rsa signatures. It assumes sha256 based signatures.
  20. */
  21. #include "lua_common.h"
  22. #include "unix-std.h"
  23. #include <openssl/err.h>
  24. #include <openssl/pem.h>
  25. #include <openssl/sha.h>
  26. #include <openssl/rsa.h>
  27. LUA_FUNCTION_DEF (rsa_pubkey, load);
  28. LUA_FUNCTION_DEF (rsa_pubkey, create);
  29. LUA_FUNCTION_DEF (rsa_pubkey, gc);
  30. LUA_FUNCTION_DEF (rsa_privkey, load_file);
  31. LUA_FUNCTION_DEF (rsa_privkey, load_pem);
  32. LUA_FUNCTION_DEF (rsa_privkey, load_raw);
  33. LUA_FUNCTION_DEF (rsa_privkey, load_base64);
  34. LUA_FUNCTION_DEF (rsa_privkey, create);
  35. LUA_FUNCTION_DEF (rsa_privkey, gc);
  36. LUA_FUNCTION_DEF (rsa_signature, create);
  37. LUA_FUNCTION_DEF (rsa_signature, load);
  38. LUA_FUNCTION_DEF (rsa_signature, save);
  39. LUA_FUNCTION_DEF (rsa_signature, base64);
  40. LUA_FUNCTION_DEF (rsa_signature, gc);
  41. LUA_FUNCTION_DEF (rsa, verify_memory);
  42. LUA_FUNCTION_DEF (rsa, sign_memory);
  43. static const struct luaL_reg rsalib_f[] = {
  44. LUA_INTERFACE_DEF (rsa, verify_memory),
  45. LUA_INTERFACE_DEF (rsa, sign_memory),
  46. {NULL, NULL}
  47. };
  48. static const struct luaL_reg rsapubkeylib_f[] = {
  49. LUA_INTERFACE_DEF (rsa_pubkey, load),
  50. LUA_INTERFACE_DEF (rsa_pubkey, create),
  51. {NULL, NULL}
  52. };
  53. static const struct luaL_reg rsapubkeylib_m[] = {
  54. {"__tostring", rspamd_lua_class_tostring},
  55. {"__gc", lua_rsa_pubkey_gc},
  56. {NULL, NULL}
  57. };
  58. static const struct luaL_reg rsaprivkeylib_f[] = {
  59. LUA_INTERFACE_DEF (rsa_privkey, load_file),
  60. LUA_INTERFACE_DEF (rsa_privkey, load_pem),
  61. LUA_INTERFACE_DEF (rsa_privkey, load_raw),
  62. LUA_INTERFACE_DEF (rsa_privkey, load_base64),
  63. LUA_INTERFACE_DEF (rsa_privkey, create),
  64. {NULL, NULL}
  65. };
  66. static const struct luaL_reg rsaprivkeylib_m[] = {
  67. {"__tostring", rspamd_lua_class_tostring},
  68. {"__gc", lua_rsa_privkey_gc},
  69. {NULL, NULL}
  70. };
  71. static const struct luaL_reg rsasignlib_f[] = {
  72. LUA_INTERFACE_DEF (rsa_signature, load),
  73. LUA_INTERFACE_DEF (rsa_signature, create),
  74. {NULL, NULL}
  75. };
  76. static const struct luaL_reg rsasignlib_m[] = {
  77. LUA_INTERFACE_DEF (rsa_signature, save),
  78. LUA_INTERFACE_DEF (rsa_signature, base64),
  79. {"__tostring", rspamd_lua_class_tostring},
  80. {"__gc", lua_rsa_signature_gc},
  81. {NULL, NULL}
  82. };
  83. static RSA *
  84. lua_check_rsa_pubkey (lua_State * L, int pos)
  85. {
  86. void *ud = rspamd_lua_check_udata (L, pos, "rspamd{rsa_pubkey}");
  87. luaL_argcheck (L, ud != NULL, 1, "'rsa_pubkey' expected");
  88. return ud ? *((RSA **)ud) : NULL;
  89. }
  90. static RSA *
  91. lua_check_rsa_privkey (lua_State * L, int pos)
  92. {
  93. void *ud = rspamd_lua_check_udata (L, pos, "rspamd{rsa_privkey}");
  94. luaL_argcheck (L, ud != NULL, 1, "'rsa_privkey' expected");
  95. return ud ? *((RSA **)ud) : NULL;
  96. }
  97. static rspamd_fstring_t *
  98. lua_check_rsa_sign (lua_State * L, int pos)
  99. {
  100. void *ud = rspamd_lua_check_udata (L, pos, "rspamd{rsa_signature}");
  101. luaL_argcheck (L, ud != NULL, 1, "'rsa_signature' expected");
  102. return ud ? *((rspamd_fstring_t **)ud) : NULL;
  103. }
  104. static gint
  105. lua_rsa_pubkey_load (lua_State *L)
  106. {
  107. RSA *rsa = NULL, **prsa;
  108. const gchar *filename;
  109. FILE *f;
  110. filename = luaL_checkstring (L, 1);
  111. if (filename != NULL) {
  112. f = fopen (filename, "r");
  113. if (f == NULL) {
  114. msg_err ("cannot open pubkey from file: %s, %s",
  115. filename,
  116. strerror (errno));
  117. lua_pushnil (L);
  118. }
  119. else {
  120. if (!PEM_read_RSA_PUBKEY (f, &rsa, NULL, NULL)) {
  121. msg_err ("cannot open pubkey from file: %s, %s", filename,
  122. ERR_error_string (ERR_get_error (), NULL));
  123. lua_pushnil (L);
  124. }
  125. else {
  126. prsa = lua_newuserdata (L, sizeof (RSA *));
  127. rspamd_lua_setclass (L, "rspamd{rsa_pubkey}", -1);
  128. *prsa = rsa;
  129. }
  130. fclose (f);
  131. }
  132. }
  133. else {
  134. lua_pushnil (L);
  135. }
  136. return 1;
  137. }
  138. static gint
  139. lua_rsa_pubkey_create (lua_State *L)
  140. {
  141. RSA *rsa = NULL, **prsa;
  142. const gchar *buf;
  143. BIO *bp;
  144. buf = luaL_checkstring (L, 1);
  145. if (buf != NULL) {
  146. bp = BIO_new_mem_buf ((void *)buf, -1);
  147. if (!PEM_read_bio_RSA_PUBKEY (bp, &rsa, NULL, NULL)) {
  148. msg_err ("cannot parse pubkey: %s",
  149. ERR_error_string (ERR_get_error (), NULL));
  150. lua_pushnil (L);
  151. }
  152. else {
  153. prsa = lua_newuserdata (L, sizeof (RSA *));
  154. rspamd_lua_setclass (L, "rspamd{rsa_pubkey}", -1);
  155. *prsa = rsa;
  156. }
  157. BIO_free (bp);
  158. }
  159. else {
  160. lua_pushnil (L);
  161. }
  162. return 1;
  163. }
  164. static gint
  165. lua_rsa_pubkey_gc (lua_State *L)
  166. {
  167. RSA *rsa = lua_check_rsa_pubkey (L, 1);
  168. if (rsa != NULL) {
  169. RSA_free (rsa);
  170. }
  171. return 0;
  172. }
  173. static gint
  174. lua_rsa_privkey_load_file (lua_State *L)
  175. {
  176. RSA *rsa = NULL, **prsa;
  177. const gchar *filename;
  178. FILE *f;
  179. filename = luaL_checkstring (L, 1);
  180. if (filename != NULL) {
  181. f = fopen (filename, "r");
  182. if (f == NULL) {
  183. msg_err ("cannot open private key from file: %s, %s",
  184. filename,
  185. strerror (errno));
  186. lua_pushnil (L);
  187. }
  188. else {
  189. if (!PEM_read_RSAPrivateKey (f, &rsa, NULL, NULL)) {
  190. msg_err ("cannot open private key from file: %s, %s", filename,
  191. ERR_error_string (ERR_get_error (), NULL));
  192. lua_pushnil (L);
  193. }
  194. else {
  195. prsa = lua_newuserdata (L, sizeof (RSA *));
  196. rspamd_lua_setclass (L, "rspamd{rsa_privkey}", -1);
  197. *prsa = rsa;
  198. }
  199. fclose (f);
  200. }
  201. }
  202. else {
  203. lua_pushnil (L);
  204. }
  205. return 1;
  206. }
  207. static gint
  208. lua_rsa_privkey_load_pem (lua_State *L)
  209. {
  210. RSA *rsa = NULL, **prsa;
  211. BIO *b;
  212. struct rspamd_lua_text *t;
  213. const gchar *data;
  214. gsize len;
  215. if (lua_isuserdata (L, 1)) {
  216. t = lua_check_text (L, 1);
  217. if (!t) {
  218. return luaL_error (L, "invalid arguments");
  219. }
  220. data = t->start;
  221. len = t->len;
  222. }
  223. else {
  224. data = luaL_checklstring (L, 1, &len);
  225. }
  226. if (data != NULL) {
  227. b = BIO_new_mem_buf (data, len);
  228. if (!PEM_read_bio_RSAPrivateKey (b, &rsa, NULL, NULL)) {
  229. msg_err ("cannot open private key from data, %s",
  230. ERR_error_string (ERR_get_error (), NULL));
  231. lua_pushnil (L);
  232. }
  233. else {
  234. prsa = lua_newuserdata (L, sizeof (RSA *));
  235. rspamd_lua_setclass (L, "rspamd{rsa_privkey}", -1);
  236. *prsa = rsa;
  237. }
  238. BIO_free (b);
  239. }
  240. else {
  241. return luaL_error (L, "invalid arguments");
  242. }
  243. return 1;
  244. }
  245. static gint
  246. lua_rsa_privkey_load_raw (lua_State *L)
  247. {
  248. RSA *rsa = NULL, **prsa;
  249. BIO *b;
  250. struct rspamd_lua_text *t;
  251. const gchar *data;
  252. gsize len;
  253. if (lua_isuserdata (L, 1)) {
  254. t = lua_check_text (L, 1);
  255. if (!t) {
  256. return luaL_error (L, "invalid arguments");
  257. }
  258. data = t->start;
  259. len = t->len;
  260. }
  261. else {
  262. data = luaL_checklstring (L, 1, &len);
  263. }
  264. if (data != NULL) {
  265. b = BIO_new_mem_buf (data, len);
  266. rsa = d2i_RSAPrivateKey_bio (b, NULL);
  267. if (rsa == NULL) {
  268. msg_err ("cannot open private key from data, %s",
  269. ERR_error_string (ERR_get_error (), NULL));
  270. lua_pushnil (L);
  271. }
  272. else {
  273. prsa = lua_newuserdata (L, sizeof (RSA *));
  274. rspamd_lua_setclass (L, "rspamd{rsa_privkey}", -1);
  275. *prsa = rsa;
  276. }
  277. BIO_free (b);
  278. }
  279. else {
  280. return luaL_error (L, "invalid arguments");
  281. }
  282. return 1;
  283. }
  284. static gint
  285. lua_rsa_privkey_load_base64 (lua_State *L)
  286. {
  287. RSA *rsa = NULL, **prsa;
  288. BIO *b;
  289. EVP_PKEY *evp = NULL;
  290. struct rspamd_lua_text *t;
  291. const gchar *data;
  292. guchar *decoded;
  293. gsize len, dec_len;
  294. if (lua_isuserdata (L, 1)) {
  295. t = lua_check_text (L, 1);
  296. if (!t) {
  297. return luaL_error (L, "invalid arguments");
  298. }
  299. data = t->start;
  300. len = t->len;
  301. }
  302. else {
  303. data = luaL_checklstring (L, 1, &len);
  304. }
  305. if (data != NULL) {
  306. decoded = g_malloc (len);
  307. if (!rspamd_cryptobox_base64_decode (data, len, decoded, &dec_len)) {
  308. g_free (decoded);
  309. return luaL_error (L, "invalid base64 encoding");
  310. }
  311. b = BIO_new_mem_buf (decoded, dec_len);
  312. if (d2i_PrivateKey_bio (b, &evp) != NULL) {
  313. rsa = EVP_PKEY_get1_RSA (evp);
  314. if (rsa == NULL) {
  315. msg_err ("cannot open RSA private key from data, %s",
  316. ERR_error_string (ERR_get_error (), NULL));
  317. lua_pushnil (L);
  318. }
  319. else {
  320. prsa = lua_newuserdata (L, sizeof (RSA *));
  321. rspamd_lua_setclass (L, "rspamd{rsa_privkey}", -1);
  322. *prsa = rsa;
  323. }
  324. EVP_PKEY_free (evp);
  325. }
  326. else {
  327. msg_err ("cannot open EVP private key from data, %s",
  328. ERR_error_string (ERR_get_error (), NULL));
  329. lua_pushnil (L);
  330. }
  331. BIO_free (b);
  332. g_free (decoded);
  333. }
  334. else {
  335. return luaL_error (L, "invalid arguments");
  336. }
  337. return 1;
  338. }
  339. static gint
  340. lua_rsa_privkey_create (lua_State *L)
  341. {
  342. RSA *rsa = NULL, **prsa;
  343. const gchar *buf;
  344. BIO *bp;
  345. buf = luaL_checkstring (L, 1);
  346. if (buf != NULL) {
  347. bp = BIO_new_mem_buf ((void *)buf, -1);
  348. if (!PEM_read_bio_RSAPrivateKey (bp, &rsa, NULL, NULL)) {
  349. msg_err ("cannot parse private key: %s",
  350. ERR_error_string (ERR_get_error (), NULL));
  351. lua_pushnil (L);
  352. }
  353. else {
  354. prsa = lua_newuserdata (L, sizeof (RSA *));
  355. rspamd_lua_setclass (L, "rspamd{rsa_privkey}", -1);
  356. *prsa = rsa;
  357. }
  358. BIO_free (bp);
  359. }
  360. else {
  361. lua_pushnil (L);
  362. }
  363. return 1;
  364. }
  365. static gint
  366. lua_rsa_privkey_gc (lua_State *L)
  367. {
  368. RSA *rsa = lua_check_rsa_privkey (L, 1);
  369. if (rsa != NULL) {
  370. RSA_free (rsa);
  371. }
  372. return 0;
  373. }
  374. static gint
  375. lua_rsa_signature_load (lua_State *L)
  376. {
  377. rspamd_fstring_t *sig, **psig;
  378. const gchar *filename;
  379. gpointer data;
  380. int fd;
  381. struct stat st;
  382. filename = luaL_checkstring (L, 1);
  383. if (filename != NULL) {
  384. fd = open (filename, O_RDONLY);
  385. if (fd == -1) {
  386. msg_err ("cannot open signature file: %s, %s", filename,
  387. strerror (errno));
  388. lua_pushnil (L);
  389. }
  390. else {
  391. sig = g_malloc (sizeof (rspamd_fstring_t));
  392. if (fstat (fd, &st) == -1 ||
  393. (data =
  394. mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0))
  395. == MAP_FAILED) {
  396. msg_err ("cannot mmap file %s: %s", filename, strerror (errno));
  397. lua_pushnil (L);
  398. }
  399. else {
  400. sig = rspamd_fstring_new_init (data, st.st_size);
  401. psig = lua_newuserdata (L, sizeof (rspamd_fstring_t *));
  402. rspamd_lua_setclass (L, "rspamd{rsa_signature}", -1);
  403. *psig = sig;
  404. munmap (data, st.st_size);
  405. }
  406. close (fd);
  407. }
  408. }
  409. else {
  410. lua_pushnil (L);
  411. }
  412. return 1;
  413. }
  414. static gint
  415. lua_rsa_signature_save (lua_State *L)
  416. {
  417. rspamd_fstring_t *sig;
  418. gint fd, flags;
  419. const gchar *filename;
  420. gboolean forced = FALSE, res = TRUE;
  421. sig = lua_check_rsa_sign (L, 1);
  422. filename = luaL_checkstring (L, 2);
  423. if (lua_gettop (L) > 2) {
  424. forced = lua_toboolean (L, 3);
  425. }
  426. if (sig != NULL && filename != NULL) {
  427. flags = O_WRONLY | O_CREAT;
  428. if (forced) {
  429. flags |= O_TRUNC;
  430. }
  431. else {
  432. flags |= O_EXCL;
  433. }
  434. fd = open (filename, flags, 00644);
  435. if (fd == -1) {
  436. msg_err ("cannot create a signature file: %s, %s",
  437. filename,
  438. strerror (errno));
  439. lua_pushboolean (L, FALSE);
  440. }
  441. else {
  442. while (write (fd, sig->str, sig->len) == -1) {
  443. if (errno == EINTR) {
  444. continue;
  445. }
  446. msg_err ("cannot write to a signature file: %s, %s",
  447. filename,
  448. strerror (errno));
  449. res = FALSE;
  450. break;
  451. }
  452. lua_pushboolean (L, res);
  453. close (fd);
  454. }
  455. }
  456. else {
  457. lua_pushboolean (L, FALSE);
  458. }
  459. return 1;
  460. }
  461. static gint
  462. lua_rsa_signature_create (lua_State *L)
  463. {
  464. rspamd_fstring_t *sig, **psig;
  465. const gchar *data;
  466. gsize dlen;
  467. data = luaL_checklstring (L, 1, &dlen);
  468. if (data != NULL) {
  469. sig = rspamd_fstring_new_init (data, dlen);
  470. psig = lua_newuserdata (L, sizeof (rspamd_fstring_t *));
  471. rspamd_lua_setclass (L, "rspamd{rsa_signature}", -1);
  472. *psig = sig;
  473. }
  474. return 1;
  475. }
  476. static gint
  477. lua_rsa_signature_gc (lua_State *L)
  478. {
  479. rspamd_fstring_t *sig = lua_check_rsa_sign (L, 1);
  480. rspamd_fstring_free (sig);
  481. return 0;
  482. }
  483. static gint
  484. lua_rsa_signature_base64 (lua_State *L)
  485. {
  486. rspamd_fstring_t *sig = lua_check_rsa_sign (L, 1);
  487. guint boundary = 0;
  488. gchar *b64;
  489. gsize outlen;
  490. enum rspamd_newlines_type how = RSPAMD_TASK_NEWLINES_CRLF;
  491. if (lua_isnumber (L, 2)) {
  492. boundary = lua_tonumber (L, 2);
  493. }
  494. if (lua_isstring (L, 3)) {
  495. const gchar *how_str = lua_tostring (L, 3);
  496. if (strcmp (how_str, "cr") == 0) {
  497. how = RSPAMD_TASK_NEWLINES_CR;
  498. }
  499. else if (strcmp (how_str, "lf") == 0) {
  500. how = RSPAMD_TASK_NEWLINES_LF;
  501. }
  502. else {
  503. how = RSPAMD_TASK_NEWLINES_CRLF;
  504. }
  505. }
  506. b64 = rspamd_encode_base64_fold (sig->str, sig->len, boundary, &outlen, how);
  507. if (b64) {
  508. lua_pushlstring (L, b64, outlen);
  509. g_free (b64);
  510. }
  511. else {
  512. lua_pushnil (L);
  513. }
  514. return 1;
  515. }
  516. /**
  517. * Check memory using specified rsa key and signature
  518. *
  519. * arguments:
  520. * (rsa_pubkey, rsa_signature, string)
  521. *
  522. * returns:
  523. * true - if string match rsa signature
  524. * false - otherwise
  525. */
  526. static gint
  527. lua_rsa_verify_memory (lua_State *L)
  528. {
  529. RSA *rsa;
  530. rspamd_fstring_t *signature;
  531. const gchar *data;
  532. gsize sz;
  533. gint ret;
  534. rsa = lua_check_rsa_pubkey (L, 1);
  535. signature = lua_check_rsa_sign (L, 2);
  536. data = luaL_checklstring (L, 3, &sz);
  537. if (rsa != NULL && signature != NULL && data != NULL) {
  538. ret = RSA_verify (NID_sha256, data, sz,
  539. signature->str, signature->len, rsa);
  540. if (ret == 0) {
  541. msg_info ("cannot check rsa signature for data: %s",
  542. ERR_error_string (ERR_get_error (), NULL));
  543. lua_pushboolean (L, FALSE);
  544. }
  545. else {
  546. lua_pushboolean (L, TRUE);
  547. }
  548. }
  549. else {
  550. lua_pushnil (L);
  551. }
  552. return 1;
  553. }
  554. /**
  555. * Sign memory using specified rsa key and signature
  556. *
  557. * arguments:
  558. * (rsa_privkey, string)
  559. *
  560. * returns:
  561. * rspamd_signature object
  562. * nil - otherwise
  563. */
  564. static gint
  565. lua_rsa_sign_memory (lua_State *L)
  566. {
  567. RSA *rsa;
  568. rspamd_fstring_t *signature, **psig;
  569. const gchar *data;
  570. gsize sz;
  571. gint ret;
  572. rsa = lua_check_rsa_privkey (L, 1);
  573. data = luaL_checklstring (L, 2, &sz);
  574. if (rsa != NULL && data != NULL) {
  575. signature = rspamd_fstring_sized_new (RSA_size (rsa));
  576. ret = RSA_sign (NID_sha256, data, sz,
  577. signature->str, (guint *)&signature->len, rsa);
  578. if (ret != 1) {
  579. return luaL_error (L, "cannot sign: %s",
  580. ERR_error_string (ERR_get_error (), NULL));
  581. }
  582. else {
  583. psig = lua_newuserdata (L, sizeof (rspamd_fstring_t *));
  584. rspamd_lua_setclass (L, "rspamd{rsa_signature}", -1);
  585. *psig = signature;
  586. }
  587. }
  588. else {
  589. return luaL_error (L, "invalid arguments");
  590. }
  591. return 1;
  592. }
  593. static gint
  594. lua_load_pubkey (lua_State * L)
  595. {
  596. lua_newtable (L);
  597. luaL_register (L, NULL, rsapubkeylib_f);
  598. return 1;
  599. }
  600. static gint
  601. lua_load_privkey (lua_State * L)
  602. {
  603. lua_newtable (L);
  604. luaL_register (L, NULL, rsaprivkeylib_f);
  605. return 1;
  606. }
  607. static gint
  608. lua_load_signature (lua_State * L)
  609. {
  610. lua_newtable (L);
  611. luaL_register (L, NULL, rsasignlib_f);
  612. return 1;
  613. }
  614. static gint
  615. lua_load_rsa (lua_State * L)
  616. {
  617. lua_newtable (L);
  618. luaL_register (L, NULL, rsalib_f);
  619. return 1;
  620. }
  621. void
  622. luaopen_rsa (lua_State * L)
  623. {
  624. luaL_newmetatable (L, "rspamd{rsa_pubkey}");
  625. lua_pushstring (L, "__index");
  626. lua_pushvalue (L, -2);
  627. lua_settable (L, -3);
  628. lua_pushstring (L, "class");
  629. lua_pushstring (L, "rspamd{rsa_pubkey}");
  630. lua_rawset (L, -3);
  631. luaL_register (L, NULL, rsapubkeylib_m);
  632. rspamd_lua_add_preload (L, "rspamd_rsa_pubkey", lua_load_pubkey);
  633. luaL_newmetatable (L, "rspamd{rsa_privkey}");
  634. lua_pushstring (L, "__index");
  635. lua_pushvalue (L, -2);
  636. lua_settable (L, -3);
  637. lua_pushstring (L, "class");
  638. lua_pushstring (L, "rspamd{rsa_privkey}");
  639. lua_rawset (L, -3);
  640. luaL_register (L, NULL, rsaprivkeylib_m);
  641. rspamd_lua_add_preload (L, "rspamd_rsa_privkey", lua_load_privkey);
  642. luaL_newmetatable (L, "rspamd{rsa_signature}");
  643. lua_pushstring (L, "__index");
  644. lua_pushvalue (L, -2);
  645. lua_settable (L, -3);
  646. lua_pushstring (L, "class");
  647. lua_pushstring (L, "rspamd{rsa_signature}");
  648. lua_rawset (L, -3);
  649. luaL_register (L, NULL, rsasignlib_m);
  650. rspamd_lua_add_preload (L, "rspamd_rsa_signature", lua_load_signature);
  651. rspamd_lua_add_preload (L, "rspamd_rsa", lua_load_rsa);
  652. lua_settop (L, 0);
  653. }