1 local type, tostring, pairs, ipairs = type, tostring, pairs, ipairs;
2 local t_insert, t_concat = table.insert, table.concat;
3 local s_format = string.format;
5 local oid_xmppaddr = "1.3.6.1.5.5.7.8.5"; -- [XMPP-CORE]
6 local oid_dnssrv = "1.3.6.1.5.5.7.8.7"; -- [SRV-ID]
8 local idna_to_ascii = require "util.encodings".idna.to_ascii;
14 local ssl_config = {};
15 local ssl_config_mt = {__index=ssl_config};
20 distinguished_name = "distinguished_name",
21 req_extensions = "v3_extensions",
22 x509_extensions = "v3_extensions",
25 distinguished_name = {
26 commonName = "example.com",
28 localityName = "The Internet",
29 organizationName = "Your Organisation",
30 organizationalUnitName = "XMPP Department",
31 emailAddress = "xmpp@example.com",
34 basicConstraints = "CA:FALSE",
35 keyUsage = "digitalSignature,keyEncipherment",
36 extendedKeyUsage = "serverAuth,clientAuth",
37 subjectAltName = "@subject_alternative_name",
39 subject_alternative_name = {
46 function ssl_config:serialize()
48 for k, t in pairs(self) do
49 s = s .. ("[%s]\n"):format(k);
50 if k == "subject_alternative_name" then
51 for san, n in pairs(t) do
53 s = s .. s_format("%s.%d = %s\n", san, i -1, n[i]);
57 for k, v in pairs(t) do
58 s = s .. ("%s = %s\n"):format(k, v);
66 local function utf8string(s)
67 -- This is how we tell openssl not to encode UTF-8 strings as fake Latin1
68 return s_format("FORMAT:UTF8,UTF8:%s", s);
71 local function ia5string(s)
72 return s_format("IA5STRING:%s", s);
76 utf8string = utf8string,
77 ia5string = ia5string,
80 function ssl_config:add_dNSName(host)
81 t_insert(self.subject_alternative_name.DNS, idna_to_ascii(host));
84 function ssl_config:add_sRVName(host, service)
85 t_insert(self.subject_alternative_name.otherName,
86 s_format("%s;%s", oid_dnssrv, ia5string("_" .. service .."." .. idna_to_ascii(host))));
89 function ssl_config:add_xmppAddr(host)
90 t_insert(self.subject_alternative_name.otherName,
91 s_format("%s;%s", oid_xmppaddr, utf8string(host)));
94 function ssl_config:from_prosody(hosts, config, certhosts)
95 -- TODO Decide if this should go elsewhere
96 local found_matching_hosts = false;
97 for i = 1,#certhosts do
98 local certhost = certhosts[i];
99 for name in pairs(hosts) do
100 if name == certhost or name:sub(-1-#certhost) == "."..certhost then
101 found_matching_hosts = true;
102 self:add_dNSName(name);
103 --print(name .. "#component_module: " .. (config.get(name, "component_module") or "nil"));
104 if config.get(name, "component_module") == nil then
105 self:add_sRVName(name, "xmpp-client");
107 --print(name .. "#anonymous_login: " .. tostring(config.get(name, "anonymous_login")));
108 if not (config.get(name, "anonymous_login") or
109 config.get(name, "authentication") == "anonymous") then
110 self:add_sRVName(name, "xmpp-server");
112 self:add_xmppAddr(name);
116 if not found_matching_hosts then
117 return nil, "no-matching-hosts";
121 do -- Lua to shell calls.
122 local function shell_escape(s)
123 return s:gsub("'",[['\'']]);
126 local function serialize(f,o)
127 local r = {"openssl", f};
128 for k,v in pairs(o) do
129 if type(k) == "string" then
130 t_insert(r, ("-%s"):format(k));
132 t_insert(r, ("'%s'"):format(shell_escape(tostring(v))));
136 for _,v in ipairs(o) do
137 t_insert(r, ("'%s'"):format(shell_escape(tostring(v))));
139 return t_concat(r, " ");
142 local os_execute = os.execute;
144 __index=function(_,f)
145 return function(opts)
146 return 0 == os_execute(serialize(f, type(opts) == "table" and opts or {}));