All Projects → zhaozg → Lua Openssl

zhaozg / Lua Openssl

Licence: other
Openssl binding for Lua

Programming Languages

c
50402 projects - #5 most used programming language
lua
6591 projects

Projects that are alternatives of or similar to Lua Openssl

jscrypto
Crypto library for Node/ES6/Typescript/Browser.
Stars: ✭ 20 (-90.29%)
Mutual labels:  crypto, cipher, openssl
conan-openssl
[OBSOLETE] The recipe is now in https://github.com/conan-io/conan-center-index
Stars: ✭ 25 (-87.86%)
Mutual labels:  ssl, crypto, openssl
Testssl.sh
Testing TLS/SSL encryption anywhere on any port
Stars: ✭ 5,676 (+2655.34%)
Mutual labels:  ssl, openssl, cipher
Pric
Simple zero-config tool to create Private Certificate Authority & issue locally-trusted development server certificates with any domain names you'd like. SSL certificates for development purposes.
Stars: ✭ 87 (-57.77%)
Mutual labels:  ssl, openssl
Cryptojs.swift
Cross-platform cryptographic functions in swift
Stars: ✭ 42 (-79.61%)
Mutual labels:  crypto, openssl
Luamqtt
luamqtt - Pure-lua MQTT v3.1.1 and v5.0 client
Stars: ✭ 58 (-71.84%)
Mutual labels:  luajit, ssl
Cve 2016 8610 Poc
CVE-2016-8610 (SSL Death Alert) PoC
Stars: ✭ 26 (-87.38%)
Mutual labels:  ssl, openssl
Tls Channel
A Java library that implements a ByteChannel interface over SSLEngine, enabling easy-to-use (socket-like) TLS for Java applications.
Stars: ✭ 113 (-45.15%)
Mutual labels:  ssl, openssl
Ghostunnel
A simple SSL/TLS proxy with mutual authentication for securing non-TLS services
Stars: ✭ 1,296 (+529.13%)
Mutual labels:  crypto, ssl
Certstrap
Tools to bootstrap CAs, certificate requests, and signed certificates.
Stars: ✭ 1,689 (+719.9%)
Mutual labels:  crypto, ssl
Cryptcheck
Verify some SSL/TLS website or XMPP implementation
Stars: ✭ 158 (-23.3%)
Mutual labels:  crypto, openssl
Easy Crypto
A WIP module aimed at providing a safer, easier to use and beginner friendly crypto API for Node.js
Stars: ✭ 21 (-89.81%)
Mutual labels:  crypto, openssl
Nano Nginx
Nano container with nginx preconfigured as reverse proxy
Stars: ✭ 15 (-92.72%)
Mutual labels:  luajit, openssl
Wolfssl
wolfSSL (formerly CyaSSL) is a small, fast, portable implementation of TLS/SSL for embedded devices to the cloud. wolfSSL supports up to TLS 1.3!
Stars: ✭ 1,098 (+433.01%)
Mutual labels:  ssl, openssl
Simon Speck C
example C language implementation of SIMON and SPECK lightweight block ciphers.
Stars: ✭ 9 (-95.63%)
Mutual labels:  crypto, cipher
Ssl Checker
Python script that collects SSL/TLS information from hosts
Stars: ✭ 94 (-54.37%)
Mutual labels:  ssl, openssl
Simon Speck
The SIMON and SPECK families of lightweight block ciphers. #nsacyber
Stars: ✭ 146 (-29.13%)
Mutual labels:  crypto, cipher
Search Guard Ssl
Elasticsearch SSL for free. Supports native Open SSL.
Stars: ✭ 159 (-22.82%)
Mutual labels:  ssl, openssl
Mutual Tls Ssl
🔐 Tutorial of setting up Security for your API with one way authentication with TLS/SSL and mutual mutual authentication for a java based web server and a client with both Spring Boot. Different clients are provided such as Apache HttpClient, OkHttp, Spring RestTemplate, Spring WebFlux WebClient Jetty and Netty, the old and the new JDK HttpClient, the old and the new Jersey Client, Google HttpClient, Unirest, Retrofit, Feign, Methanol, vertx, Scala client Finagle, Featherbed, Dispatch Reboot, AsyncHttpClient, Sttp, Akka, Requests Scala, Http4s Blaze, Kotlin client Fuel, http4k, Kohttp and ktor. Also other server examples are available such as jersey with grizzly. Also gRPC examples are included
Stars: ✭ 163 (-20.87%)
Mutual labels:  ssl, openssl
Certigo
A utility to examine and validate certificates in a variety of formats
Stars: ✭ 662 (+221.36%)
Mutual labels:  crypto, ssl

lua-openssl toolkit - A free, MIT-licensed OpenSSL binding for Lua.

Build Status Build status Total alerts Language grade: C/C++ Coverage Status

Index

  1. Introduction
  2. Documentation
  3. Howto
  4. Examples

Introduction

I needed a full OpenSSL binding for Lua, after googled, I couldn't find a version to fit my needs. I found the PHP openssl binding is a good implementation, and it inspired me. So I decided to write this OpenSSL toolkit for Lua.

The goal is to fully support openssl, include:

  • ASN1 Process.
  • Symmetrical encrypt/decrypt.
  • Message digest.
  • Asymmetrical encrypt/decrypt/sign/verify/seal/open.
  • X509 certificate.
  • PKCS7/CMS.
  • SSL/TLS.

Support backend include OpenSSL and LibreSSL.

Most of the lua-openssl functions require a key or certificate as argument, to make things easy to use OpenSSL.

This rule allows you to specify certificates or keys in the following ways:

  1. As an openssl.x509 object returned from openssl.x509.read
  2. As an openssl.evp_pkey object return from openssl.pkey.read or openssl.pkey.new

Similarly, you can also specify a public key as a key object returned from x509:get_public().

lua-openssl modules

digest, cipher, x509, pkcs7, cms and so on, be write as modules.

   local digest = require'openssl'.digest
   local cipher = require'openssl'.cipher
   local crypto = require'crypto'
   local ssl    = require'ssl'

digest() equals with digest.digest(), same cipher() equals with cipher.cipher().

documentation

Document please see here, that are generate by LDoc.

Notice: Document quality is low and stale, feel free to make a PR to improve it.

lua-openssl Objects

The following are some important lua-openssl object types:

	openssl.bio,
	openssl.x509,
	openssl.stack_of_x509,
	openssl.x509_req,
	openssl.evp_pkey,
	openssl.evp_digest,
	openssl.evp_cipher,
	openssl.engine,
	openssl.pkcs7,
	openssl.cms,
	openssl.evp_cipher_ctx,
	openssl.evp_digest_ctx
	...

They are shortened as bio, x509, sk_x509, csr, pkey, digest, cipher, engine, cipher_ctx, and digest_ctx.

openssl.bn

  • openssl.bn come from lbn, and thanks.

openssl.bn is a big-number library for Lua 5.1. It handles only integers and is suitable for number-theoretical and cryptographic applications. It is based on the bn subsystem of OpenSSL cryptographic library: https://github.com/openssl/openssl/blob/OpenSSL_1_0_2-stable/crypto/bn/bn.h If you're running Unix, you probably already have OpenSSL installed.

To try the library, just edit Makefile to reflect your installation of Lua and then run make. This will build the library and run a simple test. For detailed installation instructions, see http://webserver2.tecgraf.puc-rio.br/~lhf/ftp/lua/index.html#lbn

There is no manual but the library is simple and intuitive; see the summary below.

bn library:

 __add(x,y)        compare(x,y)          pow(x,y)
 __div(x,y)        div(x,y)              powmod(x,y,m)
 __eq(x,y)         divmod(x,y)           random(bits)
 __lt(x,y)         gcd(x,y)              rmod(x,y)
 __mod(x,y)        invmod(x)             sqr(x)
 __mul(x,y)        isneg(x)              sqrmod(x)
 __pow(x,y)        isodd(x)              sqrtmod(x)
 __sub(x,y)        isone(x)              sub(x,y)
 __tostring(x)     isprime(x,[checks])   submod(x,y,m)
 __unm(x)          iszero(x)             text(t)
 abs(x)            mod(x,y)              tohex(x)
 add(x,y)          mul(x,y)              tonumber(x)
 addmod(x,y,m)     mulmod(x,y,m)         tostring(x)
 aprime(bits)      neg(x)                totext(x)
 bits(x)           number(x)             version

Version

This lua-openssl toolkit works with Lua 5.1/5.2/5.3/5.4 or LuaJIT 2.0/2.1, and OpenSSL(0.9.8 or above 1.0.0) or LibreSSL(v2.8.x).

It is recommended to use the most up-to-date OpenSSL version because of the recent security fixes.

You can get version of lua-openssl, lua and OpenSSL from a Lua script.

openssl = require "openssl"
-- get version string format
lua_openssl_version, lua_version, openssl_version = openssl.version()
-- get version number format
lua_openssl_version, lua_version, openssl_version = openssl.version(true)

Style

Source code of lua-openssl tidy with astyle --style=allman --indent=spaces=2

Bugs

Lua-Openssl is heavily updated, if you find a bug, please report to here

I try to use luaunit to write unit test, and welcome PR to improve it.

Howto

Howto 1: Build on Linux/Unix System.

git clone --recurse https://github.com/zhaozg/lua-openssl.git lua-openssl
cd lua-openssl
make
make install
make clean

Howto 2: Build on Windows with MSVC.

Before building, please change the setting in the config.win file. Works with Lua5.1 (should support Lua5.2 by updating the config.win file).

git clone --recurse https://github.com/zhaozg/lua-openssl.git lua-openssl
cd lua-openssl
nmake -f makefile.win
nmake -f makefile.win install
nmake -f makefile.win clean

Howto 3: Build on Windows with mingw.

git clone --recurse https://github.com/zhaozg/lua-openssl.git lua-openssl
cd lua-openssl
make
make install
make clean

Howto 4: Install using luarocks.

luarocks install openssl

Howto 5: Build with CMake

Build shared lua-openssl.

cmake -Bbuild -H. -DOPENSSL_ROOT_DIR=... && cd build && make

Build static lua-openssl

cmake -Bbuild -H. -DOPENSSL_ROOT_DIR=... -DBUILD_SHARED_LUA_OPENSSL=OFF && cd build && make

Howto 5: Handle fail or error

Most lua-openssl function or methods return nil or false when error or failed, followed by string type error reason and number type error code, code can pass to openssl.error() to get more error information.

All SSL object IO operation methods return nil or false when fail or error. When nil returned, it followed by 'ssl' or 'syscall', means SSL layer or system layer error. When false returned, it is followed by number 0, 'want_read', 'want_write','want_x509_lookup','want_connect','want_accept'. Number 0 means SSL connection closed, other numbers means you should do some SSL operation.

Please remember that when lua-openssl function or methods fail without an error code, you can get the last error by openssl.error(), and repeat call openssl.error() will walk through error stacks of current threads. openssl.error(true) will also clear error stacks after get last error code, this is very useful to free memory when lua-openssl repeat calls or run long times.

Example usage

Example 1: short encrypt/decrypt

local evp_cipher = openssl.cipher.get('des')
m = 'abcdefghick'
key = m
cdata = evp_cipher:encrypt(m,key)
m1  = evp_cipher:decrypt(cdata,key)
assert(m==m1)

Example 2: quick evp_digest

md = openssl.digest.get('md5')
m = 'abcd'
aa = md:digest(m)

mdc=md:new()
mdc:update(m)
bb = mdc:final()
assert(openssl.hex(aa,true)==bb)

Example 3: Quick HMAC hash

local hmac = require "openssl".hmac

alg = 'sha256'
key = '0123456789'
msg = 'example message'

hmac.hmac(alg, msg, key, true) -- binary/"raw" output
hmac.hmac(alg, msg, key, false) -- hex output

Example 4: Iterate a openssl.stack_of_x509(sk_x509) object

n = #sk
for i=1, n do
	x = sk:get(i)
end

Example 5: read and parse certificate

local openssl = require('openssl')

function dump(t,i)
	for k,v in pairs(t) do
		if(type(v)=='table') then
			print( string.rep('\t',i),k..'={')
			dump(v,i+1)
			print( string.rep('\t',i),k..'=}')
		else
			print( string.rep('\t',i),k..'='..tostring(v))
		end
	end
end

function test_x509()
	local x = openssl.x509.read(certasstring)
	print(x)
	t = x:parse()
	dump(t,0)
	print(t)
end

test_x509()

Example 5: bio network handle(TCP)

  • server
local openssl = require'openssl'
local bio = openssl.bio

host = host or "127.0.0.1"; --only ip
port = port or "8383";

local srv = assert(bio.accept(host..':'..port))
print('listen at:'..port)
local cli = assert(srv:accept())
while 1 do
    cli = assert(srv:accept())
    print('CLI:',cli)
    while cli do
        local s = assert(cli:read())
        print(s)
        assert(cli:write(s))
    end
    print(openssl.error(true))
end
  • client
local openssl = require'openssl'
local bio = openssl.bio
io.read()

host = host or "127.0.0.1"; --only ip
port = port or "8383";

local cli = assert(bio.connect(host..':'..port,true))

    while cli do
        s = io.read()
        if(#s>0) then
            print(cli:write(s))
            ss = cli:read()
            assert(#s==#ss)
        end
    end
    print(openssl.error(true))

For more examples, please see test lua script file.


lua-openssl License

Copyright (c) 2011 - 2014 zhaozg, zhaozg(at)gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].