1 -- util to easily merge multiple sets of LuaSec context options
6 local t_concat
= table.concat
;
7 local t_insert
= table.insert
;
8 local setmetatable
= setmetatable
;
14 local finalisers
= { };
15 local id
= function (v
) return v
end
17 -- All "handlers" behave like extended rawset(table, key, value) with extra
18 -- processing usually merging the new value with the old in some reasonable
20 -- If a field does not have a defined handler then a new value simply
24 -- Convert either a list or a set into a special type of set where each
25 -- item is either positive or negative in order for a later set of options
26 -- to be able to remove options from this set by filtering out the negative ones
27 function handlers
.options(config
, field
, new
)
28 local options
= config
[field
] or { };
29 if type(new
) ~= "table" then new
= { new
} end
30 for key
, value
in pairs(new
) do
31 if value
== true or value
== false then
34 options
[value
] = true;
37 config
[field
] = options
;
40 handlers
.verifyext
= handlers
.options
;
42 -- finalisers take something produced by handlers and return what luasec
45 -- Produce a list of "positive" options from the set
46 function finalisers
.options(options
)
48 for opt
, enable
in pairs(options
) do
50 output
[#output
+1] = opt
;
56 finalisers
.verifyext
= finalisers
.options
;
58 -- We allow ciphers to be a list
60 function finalisers
.ciphers(cipherlist
)
61 if type(cipherlist
) == "table" then
62 return t_concat(cipherlist
, ":");
68 finalisers
.curveslist
= finalisers
.ciphers
;
70 -- protocol = "x" should enable only that protocol
71 -- protocol = "x+" should enable x and later versions
73 local protocols
= { "sslv2", "sslv3", "tlsv1", "tlsv1_1", "tlsv1_2", "tlsv1_3" };
74 for i
= 1, #protocols
do protocols
[protocols
[i
] .. "+"] = i
- 1; end
76 -- this interacts with ssl.options as well to add no_x
77 local function protocol(config
)
78 local min_protocol
= protocols
[config
.protocol
];
80 config
.protocol
= "sslv23";
81 for i
= 1, min_protocol
do
82 t_insert(config
.options
, "no_"..protocols
[i
]);
87 -- Merge options from 'new' config into 'config'
88 local function apply(config
, new
)
89 if type(new
) == "table" then
90 for field
, value
in pairs(new
) do
91 (handlers
[field
] or rawset)(config
, field
, value
);
96 -- Finalize the config into the form LuaSec expects
97 local function final(config
)
99 for field
, value
in pairs(config
) do
100 output
[field
] = (finalisers
[field
] or id
)(value
);
102 -- Need to handle protocols last because it adds to the options list
115 return setmetatable({options
={}}, sslopts_mt
);