PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/README.md

https://gitlab.com/kidaa/lua-openssl
Markdown | 323 lines | 242 code | 81 blank | 0 comment | 0 complexity | e740b3ce9bf9d4e2e1f75ef6c82502d4 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. lua-openssl toolkit - A free, MIT-licensed OpenSSL binding for Lua.
  2. [![Build Status](https://travis-ci.org/zhaozg/lua-openssl.svg)](https://travis-ci.org/zhaozg/lua-openssl)
  3. [![Build status](https://ci.appveyor.com/api/projects/status/f8xchhlj035yqq88/branch/master?svg=true)](https://ci.appveyor.com/project/zhaozg/lua-openssl/branch/master)
  4. #Index
  5. 1. [Introduction](#introduction)
  6. 2. [Documentation](#documentation)
  7. 2. [Howto](#a---howto)
  8. 3. [Examples](#b--example-usage)
  9. # Introduction
  10. I needed a full OpenSSL binding for Lua, after googled, I couldn't find a version to fit my needs.
  11. I found the PHP openssl binding is a good implementation, and it inspired me.
  12. So I decided to write this OpenSSL toolkit for Lua.
  13. The goal is to fully support the listed items.Below you can find the development progress of lua-openssl.
  14. * Symmetrical encrypt/decrypt. (Finished)
  15. * Message digest. (Finished)
  16. * Asymmetrical encrypt/decrypt/sign/verify/seal/open. (Finished)
  17. * X509 certificate. (Finished)
  18. * PKCS7/CMS. (Developing)
  19. * SSL/TLS. (Finished)
  20. Most of the lua-openssl functions require a key or certificate as argument; to make things easy to use OpenSSL,
  21. This rule allow you to specify certificates or keys in the following ways:
  22. 1. As an openssl.x509 object returned from openssl.x509.read
  23. 2. As an openssl.evp_pkey object return from openssl.pkey.read or openssl.pkey.new
  24. Similarly, you can also specify a public key as a key object returned from x509:get_public().
  25. ## lua-openssl modules
  26. digest,cipher, x509, cms and so on, be write as modules.
  27. ```lua
  28. local digest = require'openssl'.digest
  29. local cipher = require'openssl'.cipher
  30. local crypto = require'crypto'
  31. local ssl = require'ssl'
  32. ```
  33. digest() equals with digest.digest(), same cipher() equals with cipher.cipher().
  34. ## documentation
  35. Document please see [here](http://zhaozg.github.io/lua-openssl/)
  36. ## compat with others
  37. **crypto** is a compat module with [LuaCrypto](https://github.com/mkottman/luacrypto),
  38. document should to [reference](http://mkottman.github.io/luacrypto/manual.html#reference)
  39. **ssl** is a compat module with [luasec](https://github.com/brunoos/luasec),
  40. document should to [refrence](https://github.com/brunoos/luasec/wiki/LuaSec-0.5).
  41. NYI list: conn:settimeout,...
  42. ## lua-openssl Objects
  43. The following are some important lua-openssl object types:
  44. ```
  45. openssl.bio,
  46. openssl.x509,
  47. openssl.stack_of_x509,
  48. openssl.x509_req,
  49. openssl.evp_pkey,
  50. openssl.evp_digest,
  51. openssl.evp_cipher,
  52. openssl.engine,
  53. openssl.pkcs7,
  54. openssl.cms,
  55. openssl.evp_cipher_ctx,
  56. openssl.evp_digest_ctx
  57. ...
  58. ```
  59. They are shortened as bio, x509, sk_x509, csr, pkey, digest, cipher,
  60. engine, cipher_ctx, and digest_ctx.
  61. ## openssl.bn
  62. * ***openssl.bn*** come from [http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/](http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/),thanks.
  63. openssl.bn is a big-number library for Lua 5.1. It handles only integers and is
  64. suitable for number-theoretical and cryptographic applications. It is based
  65. on the bn subsystem of OpenSSL cryptographic library:
  66. http://www.openssl.org/docs/crypto/bn.html
  67. If you're running Unix, you probably already have OpenSSL installed.
  68. To try the library, just edit Makefile to reflect your installation of Lua and
  69. then run make. This will build the library and run a simple test. For detailed
  70. installation instructions, see
  71. http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/install.html
  72. There is no manual but the library is simple and intuitive; see the summary
  73. below.
  74. bn library:
  75. ```
  76. __add(x,y) compare(x,y) pow(x,y)
  77. __div(x,y) div(x,y) powmod(x,y,m)
  78. __eq(x,y) divmod(x,y) random(bits)
  79. __lt(x,y) gcd(x,y) rmod(x,y)
  80. __mod(x,y) invmod(x) sqr(x)
  81. __mul(x,y) isneg(x) sqrmod(x)
  82. __pow(x,y) isodd(x) sqrtmod(x)
  83. __sub(x,y) isone(x) sub(x,y)
  84. __tostring(x) isprime(x,[checks]) submod(x,y,m)
  85. __unm(x) iszero(x) text(t)
  86. abs(x) mod(x,y) tohex(x)
  87. add(x,y) mul(x,y) tonumber(x)
  88. addmod(x,y,m) mulmod(x,y,m) tostring(x)
  89. aprime(bits) neg(x) totext(x)
  90. bits(x) number(x) version
  91. ```
  92. ## Version
  93. This lua-openssl toolkit works with Lua 5.1 or 5.2, and OpenSSL (0.9.8 or above 1.0.0).
  94. It is recommended to use the most up-to-date OpenSSL version because of the recent security fixes.
  95. If you want to get the lua-openssl and OpenSSL versions from a Lua script, here is how:
  96. ```lua
  97. openssl = require "openssl"
  98. lua_openssl_version, lua_version, openssl_version = openssl.version()
  99. ```
  100. ## Bugs
  101. Lua-Openssl is heavily updated, if you find bug, please report to [here](https://github.com/zhaozg/lua-openssl/issues/)
  102. #A. Howto
  103. ### Howto 1: Build on Linux/Unix System.
  104. make
  105. make install
  106. make clean
  107. ### Howto 2: Build on Windows with MSVC.
  108. Before building, please change the setting in the config.win file.
  109. Works with Lua5.1 (should support Lua5.2 by updating the config.win file).
  110. nmake -f makefile.win
  111. nmake -f makefile.win install
  112. nmake -f makefile.win clean
  113. ### Howto 3: Build on Windows with mingw.
  114. make
  115. make install
  116. make clean
  117. ### Howto 4: Install using luarocks.
  118. luarocks install openssl --server=https://rocks.moonscript.org/dev
  119. ### Howto 5: Handle fail or error
  120. Most lua-openssl function or methods return nil or false when error or
  121. failed, followed by string type error _reason_ and number type error _code_,
  122. _code_ can pass to openssl.error() to get more error information.
  123. All SSL object IO operation methods return nil or false when fail or error.
  124. When nil returned, it followed by 'ssl' or 'syscall', means SSL layer or
  125. system layer error. When false returned, it followed by number 0, 'want_read',
  126. 'want_write','want_x509_lookup','want_connect','want_accept'. Numnber 0 means
  127. SSL connection closed, others means you should do some SSL operation.
  128. Please remeber that when lua-openssl function or methods failed without
  129. error code, you can get last error by openssl.error(), and repeat call
  130. openssl.error() will walk through error stacks of current threads.
  131. openssl.error(true) will also clear error stacks after get last error code,
  132. this is very useful to free memory when lua-openssl repeat calls or run long times.
  133. #B. Example usage
  134. ### Example 1: short encrypt/decrypt
  135. ```lua
  136. local evp_cipher = openssl.cipher.get('des')
  137. m = 'abcdefghick'
  138. key = m
  139. cdata = evp_cipher:encrypt(m,key)
  140. m1 = evp_cipher:decrypt(cdata,key)
  141. assert(m==m1)
  142. ```
  143. ### Example 2: quick evp_digest
  144. ```lua
  145. md = openssl.digest.get('md5')
  146. m = 'abcd'
  147. aa = md:digest(m)
  148. mdc=md:new()
  149. mdc:update(m)
  150. bb = mdc:final()
  151. assert(openssl.hex(aa,true)==bb)
  152. ```
  153. ### Example 3: Iterate a openssl.stack_of_x509(sk_x509) object
  154. ```lua
  155. n = #sk
  156. for i=1, n do
  157. x = sk:get(i)
  158. end
  159. ```
  160. ### Example 4: read and parse certificate
  161. ```lua
  162. local openssl = require('openssl')
  163. function dump(t,i)
  164. for k,v in pairs(t) do
  165. if(type(v)=='table') then
  166. print( string.rep('\t',i),k..'={')
  167. dump(v,i+1)
  168. print( string.rep('\t',i),k..'=}')
  169. else
  170. print( string.rep('\t',i),k..'='..tostring(v))
  171. end
  172. end
  173. end
  174. function test_x509()
  175. local x = openssl.x509.read(certasstring)
  176. print(x)
  177. t = x:parse()
  178. dump(t,0)
  179. print(t)
  180. end
  181. test_x509()
  182. ```
  183. ###Example 5: bio network handle(TCP)
  184. * server
  185. ```lua
  186. local openssl = require'openssl'
  187. local bio = openssl.bio
  188. host = host or "127.0.0.1"; --only ip
  189. port = port or "8383";
  190. local srv = assert(bio.accept(host..':'..port))
  191. print('listen at:'..port)
  192. local cli = assert(srv:accept())
  193. while 1 do
  194. cli = assert(srv:accept())
  195. print('CLI:',cli)
  196. while cli do
  197. local s = assert(cli:read())
  198. print(s)
  199. assert(cli:write(s))
  200. end
  201. print(openssl.error(true))
  202. end
  203. ```
  204. * client
  205. ```lua
  206. local openssl = require'openssl'
  207. local bio = openssl.bio
  208. io.read()
  209. host = host or "127.0.0.1"; --only ip
  210. port = port or "8383";
  211. local cli = assert(bio.connect(host..':'..port,true))
  212. while cli do
  213. s = io.read()
  214. if(#s>0) then
  215. print(cli:write(s))
  216. ss = cli:read()
  217. assert(#s==#ss)
  218. end
  219. end
  220. print(openssl.error(true))
  221. ```
  222. For more examples, please see test lua script file.
  223. --------------------------------------------------------------------
  224. ***lua-openssl License***
  225. Copyright (c) 2011 - 2014 zhaozg, zhaozg(at)gmail.com
  226. Permission is hereby granted, free of charge, to any person obtaining a copy
  227. of this software and associated documentation files (the "Software"), to
  228. deal in the Software without restriction, including without limitation the
  229. rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  230. sell copies of the Software, and to permit persons to whom the Software is
  231. furnished to do so, subject to the following conditions:
  232. The above copyright notice and this permission notice shall be included in
  233. all copies or substantial portions of the Software.
  234. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  235. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  236. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  237. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  238. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  239. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  240. THE SOFTWARE.
  241. --------------------------------------------------------------------
  242. This product includes PHP software, freely available from <http://www.php.net/software/>