util.sslconfig: Rename variable to avoid name clash [luacheck]
[prosody.git] / util / sslconfig.lua
1
2 local handlers = { };
3 local finalisers = { };
4 local id = function (v) return v end
5
6 function handlers.options(a, k, b)
7         local o = a[k] or { };
8         if type(b) ~= "table" then b = { b } end
9         for key, value in pairs(b) do
10                 if value == true or value == false then
11                         o[key] = value;
12                 else
13                         o[value] = true;
14                 end
15         end
16         a[k] = o;
17 end
18
19 handlers.verify = handlers.options;
20 handlers.verifyext = handlers.options;
21
22 function finalisers.options(a)
23         local o = {};
24         for opt, enable in pairs(a) do
25                 if enable then
26                         o[#o+1] = opt;
27                 end
28         end
29         return o;
30 end
31
32 finalisers.verify = finalisers.options;
33 finalisers.verifyext = finalisers.options;
34
35 function finalisers.ciphers(a)
36         if type(a) == "table" then
37                 return table.concat(a, ":");
38         end
39         return a;
40 end
41
42 local protocols = { "sslv2", "sslv3", "tlsv1", "tlsv1_1", "tlsv1_2" };
43 for i = 1, #protocols do protocols[protocols[i] .. "+"] = i - 1; end
44
45 local function protocol(a)
46         local min_protocol = protocols[a.protocol];
47         if min_protocol then
48                 a.protocol = "sslv23";
49                 for i = 1, min_protocol do
50                         table.insert(a.options, "no_"..protocols[i]);
51                 end
52         end
53 end
54
55 local function apply(a, b)
56         if type(b) == "table" then
57                 for k,v in pairs(b) do
58                         (handlers[k] or rawset)(a, k, v);
59                 end
60         end
61 end
62
63 local function final(a)
64         local f = { };
65         for k,v in pairs(a) do
66                 f[k] = (finalisers[k] or id)(v);
67         end
68         protocol(f);
69         return f;
70 end
71
72 local sslopts_mt = {
73         __index = {
74                 apply = apply;
75                 final = final;
76         };
77 };
78
79 local function new()
80         return setmetatable({options={}}, sslopts_mt);
81 end
82
83 return {
84         apply = apply;
85         final = final;
86         new = new;
87 };