fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

BkacjiosApplication update checker7e0ab3b

v2.1.5
8.0 KiB286 linesraw
1------------------------------------------------------------------------------
2-- LuaSec 1.2.0
3--
4-- Copyright (C) 2006-2022 Bruno Silvestre
5--
6------------------------------------------------------------------------------
7
8local core    = require("ssl.core")
9local context = require("ssl.context")
10local x509    = require("ssl.x509")
11local config  = require("ssl.config")
12
13local unpack  = table.unpack or unpack
14
15-- We must prevent the contexts to be collected before the connections,
16-- otherwise the C registry will be cleared.
17local registry = setmetatable({}, {__mode="k"})
18
19--
20--
21--
22local function optexec(func, param, ctx)
23  if param then
24    if type(param) == "table" then
25      return func(ctx, unpack(param))
26    else
27      return func(ctx, param)
28    end
29  end
30  return true
31end
32
33--
34-- Convert an array of strings to wire-format
35--
36local function array2wireformat(array)
37   local str = ""
38   for k, v in ipairs(array) do
39      if type(v) ~= "string" then return nil end
40      local len = #v
41      if len == 0 then
42        return nil, "invalid ALPN name (empty string)"
43      elseif len > 255 then
44        return nil, "invalid ALPN name (length > 255)"
45      end
46      str = str .. string.char(len) .. v
47   end
48   if str == "" then return nil, "invalid ALPN list (empty)" end
49   return str
50end
51
52--
53-- Convert wire-string format to array
54--
55local function wireformat2array(str)
56   local i = 1
57   local array = {}
58   while i < #str do
59      local len = str:byte(i)
60      array[#array + 1] = str:sub(i + 1, i + len)
61      i = i + len + 1
62   end
63   return array
64end
65
66--
67--
68--
69local function newcontext(cfg)
70   local succ, msg, ctx
71   -- Create the context
72   ctx, msg = context.create(cfg.protocol)
73   if not ctx then return nil, msg end
74   -- Mode
75   succ, msg = context.setmode(ctx, cfg.mode)
76   if not succ then return nil, msg end
77   local certificates = cfg.certificates
78   if not certificates then
79      certificates = {
80         { certificate = cfg.certificate, key = cfg.key, password = cfg.password }
81      }
82   end
83   for _, certificate in ipairs(certificates) do
84      -- Load the key
85      if certificate.key then
86         if certificate.password and
87            type(certificate.password) ~= "function" and
88            type(certificate.password) ~= "string"
89         then
90            return nil, "invalid password type"
91         end
92         succ, msg = context.loadkey(ctx, certificate.key, certificate.password)
93         if not succ then return nil, msg end
94      end
95      -- Load the certificate(s)
96      if certificate.certificate then
97        succ, msg = context.loadcert(ctx, certificate.certificate)
98        if not succ then return nil, msg end
99        if certificate.key and context.checkkey then
100          succ = context.checkkey(ctx)
101          if not succ then return nil, "private key does not match public key" end
102        end
103      end
104   end
105   -- Load the CA certificates
106   if cfg.cafile or cfg.capath then
107      succ, msg = context.locations(ctx, cfg.cafile, cfg.capath)
108      if not succ then return nil, msg end
109   end
110   -- Set SSL ciphers
111   if cfg.ciphers then
112      succ, msg = context.setcipher(ctx, cfg.ciphers)
113      if not succ then return nil, msg end
114   end
115   -- Set SSL cipher suites
116   if cfg.ciphersuites then
117      succ, msg = context.setciphersuites(ctx, cfg.ciphersuites)
118      if not succ then return nil, msg end
119   end
120    -- Set the verification options
121   succ, msg = optexec(context.setverify, cfg.verify, ctx)
122   if not succ then return nil, msg end
123   -- Set SSL options
124   succ, msg = optexec(context.setoptions, cfg.options, ctx)
125   if not succ then return nil, msg end
126   -- Set the depth for certificate verification
127   if cfg.depth then
128      succ, msg = context.setdepth(ctx, cfg.depth)
129      if not succ then return nil, msg end
130   end
131
132   -- NOTE: Setting DH parameters and elliptic curves needs to come after
133   -- setoptions(), in case the user has specified the single_{dh,ecdh}_use
134   -- options.
135
136   -- Set DH parameters
137   if cfg.dhparam then
138      if type(cfg.dhparam) ~= "function" then
139         return nil, "invalid DH parameter type"
140      end
141      context.setdhparam(ctx, cfg.dhparam)
142   end
143   
144   -- Set elliptic curves
145   if (not config.algorithms.ec) and (cfg.curve or cfg.curveslist) then
146     return false, "elliptic curves not supported"
147   end
148   if config.capabilities.curves_list and cfg.curveslist then
149     succ, msg = context.setcurveslist(ctx, cfg.curveslist)
150     if not succ then return nil, msg end
151   elseif cfg.curve then
152     succ, msg = context.setcurve(ctx, cfg.curve)
153     if not succ then return nil, msg end
154   end
155
156   -- Set extra verification options
157   if cfg.verifyext and ctx.setverifyext then
158      succ, msg = optexec(ctx.setverifyext, cfg.verifyext, ctx)
159      if not succ then return nil, msg end
160   end
161
162   -- ALPN
163   if cfg.mode == "server" and cfg.alpn then
164      if type(cfg.alpn) == "function" then
165         local alpncb = cfg.alpn
166         -- This callback function has to return one value only
167         succ, msg = context.setalpncb(ctx, function(str)
168            local protocols = alpncb(wireformat2array(str))
169            if type(protocols) == "string" then
170               protocols = { protocols }
171            elseif type(protocols) ~= "table" then
172               return nil
173            end
174            return (array2wireformat(protocols))    -- use "()" to drop error message
175         end)
176         if not succ then return nil, msg end
177      elseif type(cfg.alpn) == "table" then
178         local protocols = cfg.alpn
179         -- check if array is valid before use it
180         succ, msg = array2wireformat(protocols)
181         if not succ then return nil, msg end
182         -- This callback function has to return one value only
183         succ, msg = context.setalpncb(ctx, function()
184            return (array2wireformat(protocols))    -- use "()" to drop error message
185         end)
186         if not succ then return nil, msg end
187      else
188         return nil, "invalid ALPN parameter"
189      end
190   elseif cfg.mode == "client" and cfg.alpn then
191      local alpn
192      if type(cfg.alpn) == "string" then
193         alpn, msg = array2wireformat({ cfg.alpn })
194      elseif type(cfg.alpn) == "table" then
195         alpn, msg = array2wireformat(cfg.alpn)
196      else
197         return nil, "invalid ALPN parameter"
198      end
199      if not alpn then return nil, msg end
200      succ, msg = context.setalpn(ctx, alpn)
201      if not succ then return nil, msg end
202   end
203
204   if config.capabilities.dane and cfg.dane then
205      if type(cfg.dane) == "table" then
206         context.setdane(ctx, unpack(cfg.dane))
207      else
208         context.setdane(ctx)
209      end
210   end
211
212   return ctx
213end
214
215--
216--
217--
218local function wrap(sock, cfg)
219   local ctx, msg
220   if type(cfg) == "table" then
221      ctx, msg = newcontext(cfg)
222      if not ctx then return nil, msg end
223   else
224      ctx = cfg
225   end
226   local s, msg = core.create(ctx)
227   if s then
228      core.setfd(s, sock:getfd())
229      sock:setfd(core.SOCKET_INVALID)
230      registry[s] = ctx
231      return s
232   end
233   return nil, msg 
234end
235
236--
237-- Extract connection information.
238--
239local function info(ssl, field)
240  local str, comp, err, protocol
241  comp, err = core.compression(ssl)
242  if err then
243    return comp, err
244  end
245  -- Avoid parser
246  if field == "compression" then
247    return comp
248  end
249  local info = {compression = comp}
250  str, info.bits, info.algbits, protocol = core.info(ssl)
251  if str then
252    info.cipher, info.protocol, info.key,
253    info.authentication, info.encryption, info.mac =
254        string.match(str, 
255          "^(%S+)%s+(%S+)%s+Kx=(%S+)%s+Au=(%S+)%s+Enc=(%S+)%s+Mac=(%S+)")
256    info.export = (string.match(str, "%sexport%s*$") ~= nil)
257  end
258  if protocol then
259    info.protocol = protocol
260  end
261  if field then
262    return info[field]
263  end
264  -- Empty?
265  return ( (next(info)) and info )
266end
267
268--
269-- Set method for SSL connections.
270--
271core.setmethod("info", info)
272
273--------------------------------------------------------------------------------
274-- Export module
275--
276
277local _M = {
278  _VERSION        = "1.2.0",
279  _COPYRIGHT      = core.copyright(),
280  config          = config,
281  loadcertificate = x509.load,
282  newcontext      = newcontext,
283  wrap            = wrap,
284}
285
286return _M