Compare commits

...

31 Commits

Author SHA1 Message Date
Bruno Silvestre
4c06287052 Release LuaSec 1.3.2 2023-08-31 17:09:53 -03:00
Bruno Silvestre
5f4799d8cf reset block timeout at send or receive
Addressing #159 and #160
2023-08-31 10:51:50 -03:00
Bruno Silvestre
5787d51bb8
Merge pull request #197 from Zash/errno-bug-again
Expand workaround for zero errno to OpenSSL 3.0.x
2023-07-12 21:41:40 -03:00
Kim Alvefur
a2b211f847 Expand workaround for zero errno to OpenSSL 3.0.x
Encountered the bug in #172 after upgrading to OpenSSL 3.0.9, so it
appears to still be present.

Occurs when writing to a connection that has been closed by the remote.
2023-07-05 13:21:49 +02:00
Bruno Silvestre
769ac528e2
Merge pull request #196 from barracuda156/darwin
usocket.c: place EAI_OVERFLOW inside macro, unbreak build on <10.7
2023-05-09 10:25:21 -03:00
Sergey Fedorov
0f0e07eb41 usocket.c: place EAI_OVERFLOW inside macro, unbreak build on <10.7 2023-04-09 20:20:36 +08:00
Bruno Silvestre
fddde111f7 Release 1.3.1 2023-03-19 11:55:08 -03:00
Bruno Silvestre
6405645155 Check if PSK is available 2023-03-19 11:48:56 -03:00
Bruno Silvestre
e01c6e63cd Fix: check if PSK is available 2023-03-19 11:43:09 -03:00
Bruno Silvestre
c297c5204c Update version number 2023-03-14 10:43:47 -03:00
Bruno Silvestre
e42bc358e2 Remove Windows libraries sufix 2023-03-14 10:42:51 -03:00
Bruno Silvestre
f72457bbf9 Remove Windows libraries sufix 2023-03-14 10:37:42 -03:00
Bruno Silvestre
361813c0da Update options to OpenSSL 3.0.8 2023-03-14 09:36:06 -03:00
Bruno Silvestre
e53caaad73 Merge branch 'tls-psk' into dev 2023-02-20 09:57:04 -03:00
Bruno Silvestre
0e669f6c82 Add identity hint to PSK 2023-02-19 08:56:24 -03:00
Bruno Silvestre
c3f12b8c4d Use lua_tolstring instead luaL_checklstring 2023-02-16 22:07:55 -03:00
Bruno Silvestre
38bb3edfb4 Fix checklstring, pop(3), identity_len 2023-02-16 11:27:52 -03:00
Bruno Silvestre
50431ed511 Fix identity indexing 2023-02-16 10:53:41 -03:00
Bruno Silvestre
b321ba8fab Fix PSK samples 2023-02-16 10:52:05 -03:00
Bruno Silvestre
6708ccc381 Do not wrap the PSK callbacks 2023-02-16 10:37:59 -03:00
Bruno Silvestre
4f28db9f53 Format 2023-02-16 10:31:06 -03:00
Bruno Silvestre
dd8ba1fc92 Fix PSK client callback 2023-02-16 10:28:34 -03:00
Bruno Silvestre
9b09c93249 Return '0' from callback on size error 2023-02-16 10:13:54 -03:00
Bruno Silvestre
0f793b73c0 Format 2023-02-16 09:43:44 -03:00
Bruno Silvestre
7b60dc4794 Fix psk_len type, do not check string again 2023-02-16 09:41:35 -03:00
Bruno Silvestre
4f6aec07f6 Return the value from Lua's callback, fixes 2023-02-16 09:37:09 -03:00
Bruno Silvestre
c87fe7d5ea Do not need two PSK registry 2023-02-16 09:33:29 -03:00
unknown
842380caf6 feat: tls-psk 2023-02-16 09:52:18 +09:00
Bruno Silvestre
b47bfff382 Some minor adjusts in parameters and script 2022-12-12 18:19:37 -03:00
Bruno Silvestre
480aef1626
Merge pull request #192 from mwild1/conn-local-cert-methods
ssl: Add :getlocalchain() + :getlocalcertificate() to mirror peer methods
2022-10-06 16:48:57 -03:00
Matthew Wild
4cecbb2783 ssl: Add :getlocalchain() + :getlocalcertificate() to mirror the peer methods
These methods mirror the existing methods that fetch the peer certificate and
chain. Due to various factors (SNI, multiple key types, etc.) it is not always
trivial for an application to determine what certificate was presented to the
client. However there are various use-cases where this is needed, such as
tls-server-end-point channel binding and OCSP stapling.

Requires OpenSSL 1.0.2+ (note: SSL_get_certificate() has existed for a very
long time, but was lacking documentation until OpenSSL 3.0).
2022-09-21 18:40:10 +01:00
34 changed files with 500 additions and 74 deletions

View File

@ -1,3 +1,27 @@
--------------------------------------------------------------------------------
LuaSec 1.3.2
---------------
This version includes:
* Fix: place EAI_OVERFLOW inside macro, unbreak build on <10.7 (Sergey Fedorov)
* Fix: Expand workaround for zero errno to OpenSSL 3.0.x (Kim Alvefur)
* Fix: reset block timeout at send or receive (MartinDahlberg)
--------------------------------------------------------------------------------
LuaSec 1.3.1
---------------
This version includes:
* Fix: check if PSK is available
--------------------------------------------------------------------------------
LuaSec 1.3.0
---------------
This version includes:
* Add :getlocalchain() + :getlocalcertificate() to mirror the peer methods (@mwild1)
* Add Pre-Shared Key (PSK) support (@jclab-joseph)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
LuaSec 1.2.0 LuaSec 1.2.0
--------------- ---------------

View File

@ -1,9 +1,9 @@
LuaSec 1.2.0 LuaSec 1.3.2
------------ ------------
* OpenSSL options: * OpenSSL options:
By default, this version includes options for OpenSSL 3.0.0 beta2 By default, this version includes options for OpenSSL 3.0.8
If you need to generate the options for a different version of OpenSSL: If you need to generate the options for a different version of OpenSSL:

View File

@ -1,5 +1,5 @@
LuaSec 1.2.0 license LuaSec 1.3.2 license
Copyright (C) 2006-2022 Bruno Silvestre, UFG Copyright (C) 2006-2023 Bruno Silvestre, UFG
Permission is hereby granted, free of charge, to any person obtaining Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the a copy of this software and associated documentation files (the

View File

@ -1,4 +1,4 @@
LuaSec 1.2.0 LuaSec 1.3.2
=============== ===============
LuaSec depends on OpenSSL, and integrates with LuaSocket to make it LuaSec depends on OpenSSL, and integrates with LuaSocket to make it
easy to add secure connections to any Lua applications or scripts. easy to add secure connections to any Lua applications or scripts.

View File

@ -1,8 +1,8 @@
package = "LuaSec" package = "LuaSec"
version = "1.2.0-1" version = "1.3.2-1"
source = { source = {
url = "git+https://github.com/brunoos/luasec", url = "git+https://github.com/brunoos/luasec",
tag = "v1.2.0", tag = "v1.3.2",
} }
description = { description = {
summary = "A binding for OpenSSL library to provide TLS/SSL communication over LuaSocket.", summary = "A binding for OpenSSL library to provide TLS/SSL communication over LuaSocket.",
@ -87,7 +87,7 @@ build = {
"$(OPENSSL_BINDIR)", "$(OPENSSL_BINDIR)",
}, },
libraries = { libraries = {
"libssl32MD", "libcrypto32MD", "ws2_32" "libssl", "libcrypto", "ws2_32"
}, },
incdirs = { incdirs = {
"$(OPENSSL_INCDIR)", "src/", "src/luasocket" "$(OPENSSL_INCDIR)", "src/", "src/luasocket"

View File

@ -61,7 +61,7 @@
<DebugInformationFormat>EditAndContinue</DebugInformationFormat> <DebugInformationFormat>EditAndContinue</DebugInformationFormat>
</ClCompile> </ClCompile>
<Link> <Link>
<AdditionalDependencies>ws2_32.lib;libeay32MDd.lib;ssleay32MDd.lib;lua5.1.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>ws2_32.lib;libssl.lib;libcrypto.lib;lua5.1.lib;%(AdditionalDependencies)</AdditionalDependencies>
<OutputFile>$(OutDir)ssl.dll</OutputFile> <OutputFile>$(OutDir)ssl.dll</OutputFile>
<AdditionalLibraryDirectories>C:\devel\openssl\lib\VC;C:\devel\lua-dll9;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> <AdditionalLibraryDirectories>C:\devel\openssl\lib\VC;C:\devel\lua-dll9;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
@ -85,7 +85,7 @@
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat> <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
</ClCompile> </ClCompile>
<Link> <Link>
<AdditionalDependencies>ws2_32.lib;libssl32MD.lib;libcrypto32MD.lib;lua5.1.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>ws2_32.lib;libssl.lib;libcrypto.lib;lua5.1.lib;%(AdditionalDependencies)</AdditionalDependencies>
<OutputFile>$(OutDir)$(TargetName)$(TargetExt)</OutputFile> <OutputFile>$(OutDir)$(TargetName)$(TargetExt)</OutputFile>
<AdditionalLibraryDirectories>C:\devel\openssl-1.1.0\lib\VC;C:\devel\lua-5.1\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> <AdditionalLibraryDirectories>C:\devel\openssl-1.1.0\lib\VC;C:\devel\lua-5.1\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>

View File

@ -45,6 +45,9 @@ Directories:
* oneshot * oneshot
A simple connection example. A simple connection example.
* psk
PSK(Pre Shared Key) support.
* sni * sni
Support to SNI (Server Name Indication). Support to SNI (Server Name Indication).

View File

@ -1,4 +1,4 @@
#!/usr/bin/env sh #!/bin/sh
./rootA.sh ./rootA.sh
./rootB.sh ./rootB.sh
./clientA.sh ./clientA.sh

View File

@ -1,4 +1,4 @@
#!/usr/bin/env sh #!/bin/sh
openssl req -newkey rsa:2048 -sha256 -keyout clientAkey.pem -out clientAreq.pem \ openssl req -newkey rsa:2048 -sha256 -keyout clientAkey.pem -out clientAreq.pem \
-nodes -config ./clientA.cnf -days 365 -batch -nodes -config ./clientA.cnf -days 365 -batch

View File

@ -1,4 +1,4 @@
#!/usr/bin/env sh #!/bin/sh
openssl req -newkey rsa:2048 -sha256 -keyout clientBkey.pem -out clientBreq.pem \ openssl req -newkey rsa:2048 -sha256 -keyout clientBkey.pem -out clientBreq.pem \
-nodes -config ./clientB.cnf -days 365 -batch -nodes -config ./clientB.cnf -days 365 -batch

View File

@ -1,4 +1,4 @@
#!/usr/bin/env sh #!/bin/sh
openssl req -newkey rsa:2048 -sha256 -keyout rootAkey.pem -out rootAreq.pem -nodes -config ./rootA.cnf -days 365 -batch openssl req -newkey rsa:2048 -sha256 -keyout rootAkey.pem -out rootAreq.pem -nodes -config ./rootA.cnf -days 365 -batch

View File

@ -1,4 +1,4 @@
#!/usr/bin/env sh #!/bin/sh
openssl req -newkey rsa:2048 -sha256 -keyout rootBkey.pem -out rootBreq.pem -nodes -config ./rootB.cnf -days 365 -batch openssl req -newkey rsa:2048 -sha256 -keyout rootBkey.pem -out rootBreq.pem -nodes -config ./rootB.cnf -days 365 -batch

View File

@ -1,6 +1,6 @@
#!/usr/bin/env sh #!/bin/sh
openssl req -newkey rsa:2048 -keyout serverAkey.pem -out serverAreq.pem \ openssl req -newkey rsa:2048 -sha256 -keyout serverAkey.pem -out serverAreq.pem \
-config ./serverA.cnf -nodes -days 365 -batch -config ./serverA.cnf -nodes -days 365 -batch
openssl x509 -req -in serverAreq.pem -sha256 -extfile ./serverA.cnf \ openssl x509 -req -in serverAreq.pem -sha256 -extfile ./serverA.cnf \

View File

@ -1,6 +1,6 @@
#!/usr/bin/env sh #!/bin/sh
openssl req -newkey rsa:2048 -keyout serverBkey.pem -out serverBreq.pem \ openssl req -newkey rsa:2048 -sha256 -keyout serverBkey.pem -out serverBreq.pem \
-config ./serverB.cnf -nodes -days 365 -batch -config ./serverB.cnf -nodes -days 365 -batch
openssl x509 -req -in serverBreq.pem -sha256 -extfile ./serverB.cnf \ openssl x509 -req -in serverBreq.pem -sha256 -extfile ./serverB.cnf \

View File

@ -31,8 +31,27 @@ util.show( conn:getpeercertificate() )
print("----------------------------------------------------------------------") print("----------------------------------------------------------------------")
for k, cert in ipairs( conn:getpeerchain() ) do local expectedpeerchain = { "../certs/clientAcert.pem", "../certs/rootA.pem" }
local peerchain = conn:getpeerchain()
assert(#peerchain == #expectedpeerchain)
for k, cert in ipairs( peerchain ) do
util.show(cert) util.show(cert)
local expectedpem = assert(io.open(expectedpeerchain[k])):read("*a")
assert(cert:pem() == expectedpem, "peer chain mismatch @ "..tostring(k))
end
local expectedlocalchain = { "../certs/serverAcert.pem" }
local localchain = assert(conn:getlocalchain())
assert(#localchain == #expectedlocalchain)
for k, cert in ipairs( localchain ) do
util.show(cert)
local expectedpem = assert(io.open(expectedlocalchain[k])):read("*a")
assert(cert:pem() == expectedpem, "local chain mismatch @ "..tostring(k))
if k == 1 then
assert(cert:pem() == conn:getlocalcertificate():pem())
end
end end
local f = io.open(params.certificate) local f = io.open(params.certificate)

41
samples/psk/client.lua Normal file
View File

@ -0,0 +1,41 @@
--
-- Public domain
--
local socket = require("socket")
local ssl = require("ssl")
if not ssl.config.capabilities.psk then
print("[ERRO] PSK not available")
os.exit(1)
end
-- @param hint (nil | string)
-- @param max_identity_len (number)
-- @param max_psk_len (number)
-- @return identity (string)
-- @return PSK (string)
local function pskcb(hint, max_identity_len, max_psk_len)
print(string.format("PSK Callback: hint=%q, max_identity_len=%d, max_psk_len=%d", hint, max_identity_len, max_psk_len))
return "abcd", "1234"
end
local params = {
mode = "client",
protocol = "tlsv1_2",
psk = pskcb,
}
local peer = socket.tcp()
peer:connect("127.0.0.1", 8888)
peer = assert( ssl.wrap(peer, params) )
assert(peer:dohandshake())
print("--- INFO ---")
local info = peer:info()
for k, v in pairs(info) do
print(k, v)
end
print("---")
peer:close()

60
samples/psk/server.lua Normal file
View File

@ -0,0 +1,60 @@
--
-- Public domain
--
local socket = require("socket")
local ssl = require("ssl")
if not ssl.config.capabilities.psk then
print("[ERRO] PSK not available")
os.exit(1)
end
-- @param identity (string)
-- @param max_psk_len (number)
-- @return psk (string)
local function pskcb(identity, max_psk_len)
print(string.format("PSK Callback: identity=%q, max_psk_len=%d", identity, max_psk_len))
if identity == "abcd" then
return "1234"
end
return nil
end
local params = {
mode = "server",
protocol = "any",
options = "all",
-- PSK with just a callback
psk = pskcb,
-- PSK with identity hint
-- psk = {
-- hint = "hintpsksample",
-- callback = pskcb,
-- },
}
-- [[ SSL context
local ctx = assert(ssl.newcontext(params))
--]]
local server = socket.tcp()
server:setoption('reuseaddr', true)
assert( server:bind("127.0.0.1", 8888) )
server:listen()
local peer = server:accept()
peer = assert( ssl.wrap(peer, ctx) )
assert( peer:dohandshake() )
print("--- INFO ---")
local info = peer:info()
for k, v in pairs(info) do
print(k, v)
end
print("---")
peer:close()
server:close()

View File

@ -1,7 +1,7 @@
/*-------------------------------------------------------------------------- /*--------------------------------------------------------------------------
* LuaSec 1.2.0 * LuaSec 1.3.2
* *
* Copyright (C) 2006-2022 Bruno Silvestre * Copyright (C) 2006-2023 Bruno Silvestre
* *
*--------------------------------------------------------------------------*/ *--------------------------------------------------------------------------*/
@ -48,8 +48,14 @@
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
#if !defined(LIBRESSL_VERSION_NUMBER) && ((OPENSSL_VERSION_NUMBER & 0xFFFFF000L) == 0x10101000L) #if !defined(LIBRESSL_VERSION_NUMBER) && ((OPENSSL_VERSION_NUMBER & 0xFFFFF000L) == 0x10101000L || (OPENSSL_VERSION_NUMBER & 0xFFFFF000L) == 0x30000000L)
#define LSEC_OPENSSL_1_1_1 #define LSEC_OPENSSL_ERRNO_BUG
#endif
//------------------------------------------------------------------------------
#if !defined(LIBRESSL_VERSION_NUMBER) && !defined(OPENSSL_NO_PSK)
#define LSEC_ENABLE_PSK
#endif #endif
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------

View File

@ -1,7 +1,7 @@
/*-------------------------------------------------------------------------- /*--------------------------------------------------------------------------
* LuaSec 1.2.0 * LuaSec 1.3.2
* *
* Copyright (C) 2006-2022 Bruno Silvestre. * Copyright (C) 2006-2023 Bruno Silvestre
* *
*--------------------------------------------------------------------------*/ *--------------------------------------------------------------------------*/
@ -74,6 +74,12 @@ LSEC_API int luaopen_ssl_config(lua_State *L)
lua_pushboolean(L, 1); lua_pushboolean(L, 1);
lua_rawset(L, -3); lua_rawset(L, -3);
#ifdef LSEC_ENABLE_PSK
lua_pushstring(L, "psk");
lua_pushboolean(L, 1);
lua_rawset(L, -3);
#endif
#ifdef LSEC_ENABLE_DANE #ifdef LSEC_ENABLE_DANE
// DANE // DANE
lua_pushstring(L, "dane"); lua_pushstring(L, "dane");

View File

@ -1,9 +1,8 @@
/*-------------------------------------------------------------------------- /*--------------------------------------------------------------------------
* LuaSec 1.2.0 * LuaSec 1.3.2
* *
* Copyright (C) 2014-2022 Kim Alvefur, Paul Aurich, Tobias Markmann, * Copyright (C) 2014-2023 Kim Alvefur, Paul Aurich, Tobias Markmann, Matthew Wild
* Matthew Wild. * Copyright (C) 2006-2023 Bruno Silvestre
* Copyright (C) 2006-2022 Bruno Silvestre.
* *
*--------------------------------------------------------------------------*/ *--------------------------------------------------------------------------*/
@ -708,6 +707,143 @@ static int set_alpn_cb(lua_State *L)
return 1; return 1;
} }
#if defined(LSEC_ENABLE_PSK)
/**
* Callback to select the PSK.
*/
static unsigned int server_psk_cb(SSL *ssl, const char *identity, unsigned char *psk,
unsigned int max_psk_len)
{
size_t psk_len;
const char *ret_psk;
SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
p_context pctx = (p_context)SSL_CTX_get_app_data(ctx);
lua_State *L = pctx->L;
luaL_getmetatable(L, "SSL:PSK:Registry");
lua_pushlightuserdata(L, (void*)pctx->context);
lua_gettable(L, -2);
lua_pushstring(L, identity);
lua_pushinteger(L, max_psk_len);
lua_call(L, 2, 1);
if (!lua_isstring(L, -1)) {
lua_pop(L, 2);
return 0;
}
ret_psk = lua_tolstring(L, -1, &psk_len);
if (psk_len == 0 || psk_len > max_psk_len)
psk_len = 0;
else
memcpy(psk, ret_psk, psk_len);
lua_pop(L, 2);
return psk_len;
}
/**
* Set a PSK callback for server.
*/
static int set_server_psk_cb(lua_State *L)
{
p_context ctx = checkctx(L, 1);
luaL_getmetatable(L, "SSL:PSK:Registry");
lua_pushlightuserdata(L, (void*)ctx->context);
lua_pushvalue(L, 2);
lua_settable(L, -3);
SSL_CTX_set_psk_server_callback(ctx->context, server_psk_cb);
lua_pushboolean(L, 1);
return 1;
}
/*
* Set the PSK indentity hint.
*/
static int set_psk_identity_hint(lua_State *L)
{
p_context ctx = checkctx(L, 1);
const char *hint = luaL_checkstring(L, 2);
int ret = SSL_CTX_use_psk_identity_hint(ctx->context, hint);
lua_pushboolean(L, ret);
return 1;
}
/*
* Client callback to PSK.
*/
static unsigned int client_psk_cb(SSL *ssl, const char *hint, char *identity,
unsigned int max_identity_len, unsigned char *psk, unsigned int max_psk_len)
{
size_t psk_len;
size_t identity_len;
const char *ret_psk;
const char *ret_identity;
SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
p_context pctx = (p_context)SSL_CTX_get_app_data(ctx);
lua_State *L = pctx->L;
luaL_getmetatable(L, "SSL:PSK:Registry");
lua_pushlightuserdata(L, (void*)pctx->context);
lua_gettable(L, -2);
if (hint)
lua_pushstring(L, hint);
else
lua_pushnil(L);
// Leave space to '\0'
lua_pushinteger(L, max_identity_len-1);
lua_pushinteger(L, max_psk_len);
lua_call(L, 3, 2);
if (!lua_isstring(L, -1) || !lua_isstring(L, -2)) {
lua_pop(L, 3);
return 0;
}
ret_identity = lua_tolstring(L, -2, &identity_len);
ret_psk = lua_tolstring(L, -1, &psk_len);
if (identity_len >= max_identity_len || psk_len > max_psk_len)
psk_len = 0;
else {
memcpy(identity, ret_identity, identity_len);
identity[identity_len] = 0;
memcpy(psk, ret_psk, psk_len);
}
lua_pop(L, 3);
return psk_len;
}
/**
* Set a PSK callback for client.
*/
static int set_client_psk_cb(lua_State *L) {
p_context ctx = checkctx(L, 1);
luaL_getmetatable(L, "SSL:PSK:Registry");
lua_pushlightuserdata(L, (void*)ctx->context);
lua_pushvalue(L, 2);
lua_settable(L, -3);
SSL_CTX_set_psk_client_callback(ctx->context, client_psk_cb);
lua_pushboolean(L, 1);
return 1;
}
#endif
#if defined(LSEC_ENABLE_DANE) #if defined(LSEC_ENABLE_DANE)
/* /*
* DANE * DANE
@ -759,6 +895,11 @@ static luaL_Reg funcs[] = {
{"setdhparam", set_dhparam}, {"setdhparam", set_dhparam},
{"setverify", set_verify}, {"setverify", set_verify},
{"setoptions", set_options}, {"setoptions", set_options},
#if defined(LSEC_ENABLE_PSK)
{"setpskhint", set_psk_identity_hint},
{"setserverpskcb", set_server_psk_cb},
{"setclientpskcb", set_client_psk_cb},
#endif
{"setmode", set_mode}, {"setmode", set_mode},
#if !defined(OPENSSL_NO_EC) #if !defined(OPENSSL_NO_EC)
{"setcurve", set_curve}, {"setcurve", set_curve},
@ -792,6 +933,10 @@ static int meth_destroy(lua_State *L)
lua_pushlightuserdata(L, (void*)ctx->context); lua_pushlightuserdata(L, (void*)ctx->context);
lua_pushnil(L); lua_pushnil(L);
lua_settable(L, -3); lua_settable(L, -3);
luaL_getmetatable(L, "SSL:PSK:Registry");
lua_pushlightuserdata(L, (void*)ctx->context);
lua_pushnil(L);
lua_settable(L, -3);
SSL_CTX_free(ctx->context); SSL_CTX_free(ctx->context);
ctx->context = NULL; ctx->context = NULL;
@ -936,6 +1081,7 @@ LSEC_API int luaopen_ssl_context(lua_State *L)
{ {
luaL_newmetatable(L, "SSL:DH:Registry"); /* Keep all DH callbacks */ luaL_newmetatable(L, "SSL:DH:Registry"); /* Keep all DH callbacks */
luaL_newmetatable(L, "SSL:ALPN:Registry"); /* Keep all ALPN callbacks */ luaL_newmetatable(L, "SSL:ALPN:Registry"); /* Keep all ALPN callbacks */
luaL_newmetatable(L, "SSL:PSK:Registry"); /* Keep all PSK callbacks */
luaL_newmetatable(L, "SSL:Verify:Registry"); /* Keep all verify flags */ luaL_newmetatable(L, "SSL:Verify:Registry"); /* Keep all verify flags */
luaL_newmetatable(L, "SSL:Context"); luaL_newmetatable(L, "SSL:Context");
setfuncs(L, meta); setfuncs(L, meta);

View File

@ -2,9 +2,9 @@
#define LSEC_CONTEXT_H #define LSEC_CONTEXT_H
/*-------------------------------------------------------------------------- /*--------------------------------------------------------------------------
* LuaSec 1.2.0 * LuaSec 1.3.2
* *
* Copyright (C) 2006-2022 Bruno Silvestre * Copyright (C) 2006-2023 Bruno Silvestre
* *
*--------------------------------------------------------------------------*/ *--------------------------------------------------------------------------*/

View File

@ -1,3 +1,10 @@
/*--------------------------------------------------------------------------
* LuaSec 1.3.2
*
* Copyright (C) 2006-2023 Bruno Silvestre
*
*--------------------------------------------------------------------------*/
#include <openssl/objects.h> #include <openssl/objects.h>
#include "ec.h" #include "ec.h"

View File

@ -1,7 +1,7 @@
/*-------------------------------------------------------------------------- /*--------------------------------------------------------------------------
* LuaSec 1.2.0 * LuaSec 1.3.2
* *
* Copyright (C) 2006-2022 Bruno Silvestre * Copyright (C) 2006-2023 Bruno Silvestre
* *
*--------------------------------------------------------------------------*/ *--------------------------------------------------------------------------*/

View File

@ -1,6 +1,7 @@
---------------------------------------------------------------------------- ----------------------------------------------------------------------------
-- LuaSec 1.2.0 -- LuaSec 1.3.2
-- Copyright (C) 2009-2022 PUC-Rio --
-- Copyright (C) 2009-2023 PUC-Rio
-- --
-- Author: Pablo Musa -- Author: Pablo Musa
-- Author: Tomas Guisasola -- Author: Tomas Guisasola
@ -18,8 +19,8 @@ local try = socket.try
-- Module -- Module
-- --
local _M = { local _M = {
_VERSION = "1.2.0", _VERSION = "1.3.2",
_COPYRIGHT = "LuaSec 1.2.0 - Copyright (C) 2009-2022 PUC-Rio", _COPYRIGHT = "LuaSec 1.3.2 - Copyright (C) 2009-2023 PUC-Rio",
PORT = 443, PORT = 443,
TIMEOUT = 60 TIMEOUT = 60
} }

View File

@ -78,9 +78,7 @@ int buffer_meth_send(lua_State *L, p_buffer buf) {
const char *data = luaL_checklstring(L, 2, &size); const char *data = luaL_checklstring(L, 2, &size);
long start = (long) luaL_optnumber(L, 3, 1); long start = (long) luaL_optnumber(L, 3, 1);
long end = (long) luaL_optnumber(L, 4, -1); long end = (long) luaL_optnumber(L, 4, -1);
#ifdef LUASOCKET_DEBUG timeout_markstart(buf->tm);
p_timeout tm = timeout_markstart(buf->tm);
#endif
if (start < 0) start = (long) (size+start+1); if (start < 0) start = (long) (size+start+1);
if (end < 0) end = (long) (size+end+1); if (end < 0) end = (long) (size+end+1);
if (start < 1) start = (long) 1; if (start < 1) start = (long) 1;
@ -98,7 +96,7 @@ int buffer_meth_send(lua_State *L, p_buffer buf) {
} }
#ifdef LUASOCKET_DEBUG #ifdef LUASOCKET_DEBUG
/* push time elapsed during operation as the last return value */ /* push time elapsed during operation as the last return value */
lua_pushnumber(L, timeout_gettime() - timeout_getstart(tm)); lua_pushnumber(L, timeout_gettime() - timeout_getstart(buf->tm));
#endif #endif
return lua_gettop(L) - top; return lua_gettop(L) - top;
} }
@ -117,9 +115,7 @@ int buffer_meth_receive(lua_State *L, p_buffer buf) {
top = 3; top = 3;
} }
part = luaL_optlstring(L, 3, "", &size); part = luaL_optlstring(L, 3, "", &size);
#ifdef LUASOCKET_DEBUG timeout_markstart(buf->tm);
p_timeout tm = timeout_markstart(buf->tm);
#endif
/* initialize buffer with optional extra prefix /* initialize buffer with optional extra prefix
* (useful for concatenating previous partial results) */ * (useful for concatenating previous partial results) */
luaL_buffinit(L, &b); luaL_buffinit(L, &b);
@ -155,7 +151,7 @@ int buffer_meth_receive(lua_State *L, p_buffer buf) {
} }
#ifdef LUASOCKET_DEBUG #ifdef LUASOCKET_DEBUG
/* push time elapsed during operation as the last return value */ /* push time elapsed during operation as the last return value */
lua_pushnumber(L, timeout_gettime() - timeout_getstart(tm)); lua_pushnumber(L, timeout_gettime() - timeout_getstart(buf->tm));
#endif #endif
return lua_gettop(L) - top; return lua_gettop(L) - top;
} }

View File

@ -426,7 +426,9 @@ const char *socket_gaistrerror(int err) {
case EAI_MEMORY: return "memory allocation failure"; case EAI_MEMORY: return "memory allocation failure";
case EAI_NONAME: case EAI_NONAME:
return "host or service not provided, or not known"; return "host or service not provided, or not known";
#ifdef EAI_OVERFLOW
case EAI_OVERFLOW: return "argument buffer overflow"; case EAI_OVERFLOW: return "argument buffer overflow";
#endif
#ifdef EAI_PROTOCOL #ifdef EAI_PROTOCOL
case EAI_PROTOCOL: return "resolved protocol is unknown"; case EAI_PROTOCOL: return "resolved protocol is unknown";
#endif #endif

View File

@ -1,7 +1,7 @@
/*-------------------------------------------------------------------------- /*--------------------------------------------------------------------------
* LuaSec 1.2.0 * LuaSec 1.3.2
* *
* Copyright (C) 2006-2022 Bruno Silvestre * Copyright (C) 2006-2023 Bruno Silvestre
* *
*--------------------------------------------------------------------------*/ *--------------------------------------------------------------------------*/
@ -13,7 +13,7 @@
/* /*
OpenSSL version: OpenSSL 3.0.0-beta2 OpenSSL version: OpenSSL 3.0.8
*/ */
static lsec_ssl_option_t ssl_options[] = { static lsec_ssl_option_t ssl_options[] = {

View File

@ -2,9 +2,9 @@
#define LSEC_OPTIONS_H #define LSEC_OPTIONS_H
/*-------------------------------------------------------------------------- /*--------------------------------------------------------------------------
* LuaSec 1.2.0 * LuaSec 1.3.2
* *
* Copyright (C) 2006-2022 Bruno Silvestre * Copyright (C) 2006-2023 Bruno Silvestre
* *
*--------------------------------------------------------------------------*/ *--------------------------------------------------------------------------*/

View File

@ -18,9 +18,9 @@ end
local function generate(options, version) local function generate(options, version)
print([[ print([[
/*-------------------------------------------------------------------------- /*--------------------------------------------------------------------------
* LuaSec 1.2.0 * LuaSec 1.3.2
* *
* Copyright (C) 2006-2022 Bruno Silvestre * Copyright (C) 2006-2023 Bruno Silvestre
* *
*--------------------------------------------------------------------------*/ *--------------------------------------------------------------------------*/

102
src/ssl.c
View File

@ -1,9 +1,8 @@
/*-------------------------------------------------------------------------- /*--------------------------------------------------------------------------
* LuaSec 1.2.0 * LuaSec 1.3.2
* *
* Copyright (C) 2014-2022 Kim Alvefur, Paul Aurich, Tobias Markmann, * Copyright (C) 2014-2023 Kim Alvefur, Paul Aurich, Tobias Markmann, Matthew Wild
* Matthew Wild. * Copyright (C) 2006-2023 Bruno Silvestre
* Copyright (C) 2006-2022 Bruno Silvestre.
* *
*--------------------------------------------------------------------------*/ *--------------------------------------------------------------------------*/
@ -48,8 +47,8 @@ static int lsec_socket_error()
#if defined(WIN32) #if defined(WIN32)
return WSAGetLastError(); return WSAGetLastError();
#else #else
#if defined(LSEC_OPENSSL_1_1_1) #if defined(LSEC_OPENSSL_ERRNO_BUG)
// Bug in OpenSSL 1.1.1 // Bug in OpenSSL
if (errno == 0) if (errno == 0)
return LSEC_IO_SSL; return LSEC_IO_SSL;
#endif #endif
@ -530,6 +529,58 @@ static int meth_getpeercertificate(lua_State *L)
return 1; return 1;
} }
/**
* Return the nth certificate of the chain sent to our peer.
*/
static int meth_getlocalcertificate(lua_State *L)
{
int n;
X509 *cert;
STACK_OF(X509) *certs;
p_ssl ssl = (p_ssl)luaL_checkudata(L, 1, "SSL:Connection");
if (ssl->state != LSEC_STATE_CONNECTED) {
lua_pushnil(L);
lua_pushstring(L, "closed");
return 2;
}
/* Default to the first cert */
n = (int)luaL_optinteger(L, 2, 1);
/* This function is 1-based, but OpenSSL is 0-based */
--n;
if (n < 0) {
lua_pushnil(L);
lua_pushliteral(L, "invalid certificate index");
return 2;
}
if (n == 0) {
cert = SSL_get_certificate(ssl->ssl);
if (cert)
lsec_pushx509(L, cert);
else
lua_pushnil(L);
return 1;
}
/* In a server-context, the stack doesn't contain the peer cert,
* so adjust accordingly.
*/
if (SSL_is_server(ssl->ssl))
--n;
if(SSL_get0_chain_certs(ssl->ssl, &certs) != 1) {
lua_pushnil(L);
} else {
if (n >= sk_X509_num(certs)) {
lua_pushnil(L);
return 1;
}
cert = sk_X509_value(certs, n);
/* Increment the reference counting of the object. */
/* See SSL_get_peer_certificate() source code. */
X509_up_ref(cert);
lsec_pushx509(L, cert);
}
return 1;
}
/** /**
* Return the chain of certificate of the peer. * Return the chain of certificate of the peer.
*/ */
@ -564,6 +615,41 @@ static int meth_getpeerchain(lua_State *L)
return 1; return 1;
} }
/**
* Return the chain of certificates sent to the peer.
*/
static int meth_getlocalchain(lua_State *L)
{
int i;
int idx = 1;
int n_certs;
X509 *cert;
STACK_OF(X509) *certs;
p_ssl ssl = (p_ssl)luaL_checkudata(L, 1, "SSL:Connection");
if (ssl->state != LSEC_STATE_CONNECTED) {
lua_pushnil(L);
lua_pushstring(L, "closed");
return 2;
}
lua_newtable(L);
if (SSL_is_server(ssl->ssl)) {
lsec_pushx509(L, SSL_get_certificate(ssl->ssl));
lua_rawseti(L, -2, idx++);
}
if(SSL_get0_chain_certs(ssl->ssl, &certs)) {
n_certs = sk_X509_num(certs);
for (i = 0; i < n_certs; i++) {
cert = sk_X509_value(certs, i);
/* Increment the reference counting of the object. */
/* See SSL_get_peer_certificate() source code. */
X509_up_ref(cert);
lsec_pushx509(L, cert);
lua_rawseti(L, -2, idx++);
}
}
return 1;
}
/** /**
* Copy the table src to the table dst. * Copy the table src to the table dst.
*/ */
@ -861,7 +947,7 @@ static int meth_getalpn(lua_State *L)
static int meth_copyright(lua_State *L) static int meth_copyright(lua_State *L)
{ {
lua_pushstring(L, "LuaSec 1.2.0 - Copyright (C) 2006-2022 Bruno Silvestre, UFG" lua_pushstring(L, "LuaSec 1.3.2 - Copyright (C) 2006-2023 Bruno Silvestre, UFG"
#if defined(WITH_LUASOCKET) #if defined(WITH_LUASOCKET)
"\nLuaSocket 3.0-RC1 - Copyright (C) 2004-2013 Diego Nehab" "\nLuaSocket 3.0-RC1 - Copyright (C) 2004-2013 Diego Nehab"
#endif #endif
@ -908,7 +994,9 @@ static luaL_Reg methods[] = {
{"getfd", meth_getfd}, {"getfd", meth_getfd},
{"getfinished", meth_getfinished}, {"getfinished", meth_getfinished},
{"getpeercertificate", meth_getpeercertificate}, {"getpeercertificate", meth_getpeercertificate},
{"getlocalcertificate", meth_getlocalcertificate},
{"getpeerchain", meth_getpeerchain}, {"getpeerchain", meth_getpeerchain},
{"getlocalchain", meth_getlocalchain},
{"getpeerverification", meth_getpeerverification}, {"getpeerverification", meth_getpeerverification},
{"getpeerfinished", meth_getpeerfinished}, {"getpeerfinished", meth_getpeerfinished},
{"exportkeyingmaterial",meth_exportkeyingmaterial}, {"exportkeyingmaterial",meth_exportkeyingmaterial},

View File

@ -2,9 +2,9 @@
#define LSEC_SSL_H #define LSEC_SSL_H
/*-------------------------------------------------------------------------- /*--------------------------------------------------------------------------
* LuaSec 1.2.0 * LuaSec 1.3.2
* *
* Copyright (C) 2006-2022 Bruno Silvestre * Copyright (C) 2006-2023 Bruno Silvestre
* *
*--------------------------------------------------------------------------*/ *--------------------------------------------------------------------------*/

View File

@ -1,7 +1,7 @@
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
-- LuaSec 1.2.0 -- LuaSec 1.3.2
-- --
-- Copyright (C) 2006-2022 Bruno Silvestre -- Copyright (C) 2006-2023 Bruno Silvestre
-- --
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
@ -201,6 +201,33 @@ local function newcontext(cfg)
if not succ then return nil, msg end if not succ then return nil, msg end
end end
-- PSK
if config.capabilities.psk and cfg.psk then
if cfg.mode == "client" then
if type(cfg.psk) ~= "function" then
return nil, "invalid PSK configuration"
end
succ = context.setclientpskcb(ctx, cfg.psk)
if not succ then return nil, msg end
elseif cfg.mode == "server" then
if type(cfg.psk) == "function" then
succ, msg = context.setserverpskcb(ctx, cfg.psk)
if not succ then return nil, msg end
elseif type(cfg.psk) == "table" then
if type(cfg.psk.hint) == "string" and type(cfg.psk.callback) == "function" then
succ, msg = context.setpskhint(ctx, cfg.psk.hint)
if not succ then return succ, msg end
succ = context.setserverpskcb(ctx, cfg.psk.callback)
if not succ then return succ, msg end
else
return nil, "invalid PSK configuration"
end
else
return nil, "invalid PSK configuration"
end
end
end
if config.capabilities.dane and cfg.dane then if config.capabilities.dane and cfg.dane then
if type(cfg.dane) == "table" then if type(cfg.dane) == "table" then
context.setdane(ctx, unpack(cfg.dane)) context.setdane(ctx, unpack(cfg.dane))
@ -275,7 +302,7 @@ core.setmethod("info", info)
-- --
local _M = { local _M = {
_VERSION = "1.2.0", _VERSION = "1.3.2",
_COPYRIGHT = core.copyright(), _COPYRIGHT = core.copyright(),
config = config, config = config,
loadcertificate = x509.load, loadcertificate = x509.load,

View File

@ -1,8 +1,8 @@
/*-------------------------------------------------------------------------- /*--------------------------------------------------------------------------
* LuaSec 1.2.0 * LuaSec 1.3.2
* *
* Copyright (C) 2014-2022 Kim Alvefur, Paul Aurich, Tobias Markmann * Copyright (C) 2014-2023 Kim Alvefur, Paul Aurich, Tobias Markmann, Matthew Wild
* Matthew Wild, Bruno Silvestre. * Copyright (C) 2014-2023 Bruno Silvestre
* *
*--------------------------------------------------------------------------*/ *--------------------------------------------------------------------------*/

View File

@ -1,8 +1,8 @@
/*-------------------------------------------------------------------------- /*--------------------------------------------------------------------------
* LuaSec 1.2.0 * LuaSec 1.3.2
* *
* Copyright (C) 2014-2022 Kim Alvefur, Paul Aurich, Tobias Markmann * Copyright (C) 2014-2023 Kim Alvefur, Paul Aurich, Tobias Markmann, Matthew Wild
* Matthew Wild, Bruno Silvestre. * Copyright (C) 2013-2023 Bruno Silvestre
* *
*--------------------------------------------------------------------------*/ *--------------------------------------------------------------------------*/