|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local core = require("ssl.core") |
|
|
local context = require("ssl.context") |
|
|
local x509 = require("ssl.x509") |
|
|
local config = require("ssl.config") |
|
|
|
|
|
local unpack = table.unpack or unpack |
|
|
|
|
|
|
|
|
|
|
|
local registry = setmetatable({}, {__mode="k"}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function optexec(func, param, ctx) |
|
|
if param then |
|
|
if type(param) == "table" then |
|
|
return func(ctx, unpack(param)) |
|
|
else |
|
|
return func(ctx, param) |
|
|
end |
|
|
end |
|
|
return true |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function newcontext(cfg) |
|
|
local succ, msg, ctx |
|
|
|
|
|
ctx, msg = context.create(cfg.protocol) |
|
|
if not ctx then return nil, msg end |
|
|
|
|
|
succ, msg = context.setmode(ctx, cfg.mode) |
|
|
if not succ then return nil, msg end |
|
|
|
|
|
if cfg.key then |
|
|
if cfg.password and |
|
|
type(cfg.password) ~= "function" and |
|
|
type(cfg.password) ~= "string" |
|
|
then |
|
|
return nil, "invalid password type" |
|
|
end |
|
|
succ, msg = context.loadkey(ctx, cfg.key, cfg.password) |
|
|
if not succ then return nil, msg end |
|
|
end |
|
|
|
|
|
if cfg.certificate then |
|
|
succ, msg = context.loadcert(ctx, cfg.certificate) |
|
|
if not succ then return nil, msg end |
|
|
if cfg.key and context.checkkey then |
|
|
succ = context.checkkey(ctx) |
|
|
if not succ then return nil, "private key does not match public key" end |
|
|
end |
|
|
end |
|
|
|
|
|
if cfg.cafile or cfg.capath then |
|
|
succ, msg = context.locations(ctx, cfg.cafile, cfg.capath) |
|
|
if not succ then return nil, msg end |
|
|
end |
|
|
|
|
|
if cfg.ciphers then |
|
|
succ, msg = context.setcipher(ctx, cfg.ciphers) |
|
|
if not succ then return nil, msg end |
|
|
end |
|
|
|
|
|
succ, msg = optexec(context.setverify, cfg.verify, ctx) |
|
|
if not succ then return nil, msg end |
|
|
|
|
|
succ, msg = optexec(context.setoptions, cfg.options, ctx) |
|
|
if not succ then return nil, msg end |
|
|
|
|
|
if cfg.depth then |
|
|
succ, msg = context.setdepth(ctx, cfg.depth) |
|
|
if not succ then return nil, msg end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if cfg.dhparam then |
|
|
if type(cfg.dhparam) ~= "function" then |
|
|
return nil, "invalid DH parameter type" |
|
|
end |
|
|
context.setdhparam(ctx, cfg.dhparam) |
|
|
end |
|
|
|
|
|
|
|
|
if (not config.algorithms.ec) and (cfg.curve or cfg.curveslist) then |
|
|
return false, "elliptic curves not supported" |
|
|
end |
|
|
if config.capabilities.curves_list and cfg.curveslist then |
|
|
succ, msg = context.setcurveslist(ctx, cfg.curveslist) |
|
|
if not succ then return nil, msg end |
|
|
elseif cfg.curve then |
|
|
succ, msg = context.setcurve(ctx, cfg.curve) |
|
|
if not succ then return nil, msg end |
|
|
end |
|
|
|
|
|
|
|
|
if cfg.verifyext and ctx.setverifyext then |
|
|
succ, msg = optexec(ctx.setverifyext, cfg.verifyext, ctx) |
|
|
if not succ then return nil, msg end |
|
|
end |
|
|
|
|
|
return ctx |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function wrap(sock, cfg) |
|
|
local ctx, msg |
|
|
if type(cfg) == "table" then |
|
|
ctx, msg = newcontext(cfg) |
|
|
if not ctx then return nil, msg end |
|
|
else |
|
|
ctx = cfg |
|
|
end |
|
|
local s, msg = core.create(ctx) |
|
|
if s then |
|
|
core.setfd(s, sock:getfd()) |
|
|
sock:setfd(core.SOCKET_INVALID) |
|
|
registry[s] = ctx |
|
|
return s |
|
|
end |
|
|
return nil, msg |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function info(ssl, field) |
|
|
local str, comp, err, protocol |
|
|
comp, err = core.compression(ssl) |
|
|
if err then |
|
|
return comp, err |
|
|
end |
|
|
|
|
|
if field == "compression" then |
|
|
return comp |
|
|
end |
|
|
local info = {compression = comp} |
|
|
str, info.bits, info.algbits, protocol = core.info(ssl) |
|
|
if str then |
|
|
info.cipher, info.protocol, info.key, |
|
|
info.authentication, info.encryption, info.mac = |
|
|
string.match(str, |
|
|
"^(%S+)%s+(%S+)%s+Kx=(%S+)%s+Au=(%S+)%s+Enc=(%S+)%s+Mac=(%S+)") |
|
|
info.export = (string.match(str, "%sexport%s*$") ~= nil) |
|
|
end |
|
|
if protocol then |
|
|
info.protocol = protocol |
|
|
end |
|
|
if field then |
|
|
return info[field] |
|
|
end |
|
|
|
|
|
return ( (next(info)) and info ) |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
core.setmethod("info", info) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local _M = { |
|
|
_VERSION = "0.7", |
|
|
_COPYRIGHT = core.copyright(), |
|
|
loadcertificate = x509.load, |
|
|
newcontext = newcontext, |
|
|
wrap = wrap, |
|
|
} |
|
|
|
|
|
return _M |
|
|
|