Merge tls branch
[prosody.git] / util / openssl.lua
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;
4
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]
7
8 local idna_to_ascii = require "util.encodings".idna.to_ascii;
9
10 local _M = {};
11 local config = {};
12 _M.config = config;
13
14 local ssl_config = {};
15 local ssl_config_mt = {__index=ssl_config};
16
17 function config.new()
18         return setmetatable({
19                 req = {
20                         distinguished_name = "distinguished_name",
21                         req_extensions = "v3_extensions",
22                         x509_extensions = "v3_extensions",
23                         prompt = "no",
24                 },
25                 distinguished_name = {
26                         countryName = "GB",
27                         -- stateOrProvinceName = "",
28                         localityName = "The Internet",
29                         organizationName = "Your Organisation",
30                         organizationalUnitName = "XMPP Department",
31                         commonName = "example.com",
32                         emailAddress = "xmpp@example.com",
33                 },
34                 v3_extensions = {
35                         basicConstraints = "CA:FALSE",
36                         keyUsage = "digitalSignature,keyEncipherment",
37                         extendedKeyUsage = "serverAuth,clientAuth",
38                         subjectAltName = "@subject_alternative_name",
39                 },
40                 subject_alternative_name = {
41                         DNS = {},
42                         otherName = {},
43                 },
44         }, ssl_config_mt);
45 end
46
47 local DN_order = {
48         "countryName";
49         "stateOrProvinceName";
50         "localityName";
51         "streetAddress";
52         "organizationName";
53         "organizationalUnitName";
54         "commonName";
55         "emailAddress";
56 }
57 _M._DN_order = DN_order;
58 function ssl_config:serialize()
59         local s = "";
60         for k, t in pairs(self) do
61                 s = s .. ("[%s]\n"):format(k);
62                 if k == "subject_alternative_name" then
63                         for san, n in pairs(t) do
64                                 for i = 1,#n do
65                                         s = s .. s_format("%s.%d = %s\n", san, i -1, n[i]);
66                                 end
67                         end
68                 elseif k == "distinguished_name" then
69                         for i=1,#DN_order do
70                                 local k = DN_order[i]
71                                 local v = t[k];
72                                 if v then
73                                         s = s .. ("%s = %s\n"):format(k, v);
74                                 end
75                         end
76                 else
77                         for k, v in pairs(t) do
78                                 s = s .. ("%s = %s\n"):format(k, v);
79                         end
80                 end
81                 s = s .. "\n";
82         end
83         return s;
84 end
85
86 local function utf8string(s)
87         -- This is how we tell openssl not to encode UTF-8 strings as fake Latin1
88         return s_format("FORMAT:UTF8,UTF8:%s", s);
89 end
90
91 local function ia5string(s)
92         return s_format("IA5STRING:%s", s);
93 end
94
95 _M.util = {
96         utf8string = utf8string,
97         ia5string = ia5string,
98 };
99
100 function ssl_config:add_dNSName(host)
101         t_insert(self.subject_alternative_name.DNS, idna_to_ascii(host));
102 end
103
104 function ssl_config:add_sRVName(host, service)
105         t_insert(self.subject_alternative_name.otherName,
106                 s_format("%s;%s", oid_dnssrv, ia5string("_" .. service .."." .. idna_to_ascii(host))));
107 end
108
109 function ssl_config:add_xmppAddr(host)
110         t_insert(self.subject_alternative_name.otherName,
111                 s_format("%s;%s", oid_xmppaddr, utf8string(host)));
112 end
113
114 function ssl_config:from_prosody(hosts, config, certhosts)
115         -- TODO Decide if this should go elsewhere
116         local found_matching_hosts = false;
117         for i = 1,#certhosts do
118                 local certhost = certhosts[i];
119                 for name in pairs(hosts) do
120                         if name == certhost or name:sub(-1-#certhost) == "."..certhost then
121                                 found_matching_hosts = true;
122                                 self:add_dNSName(name);
123                                 --print(name .. "#component_module: " .. (config.get(name, "component_module") or "nil"));
124                                 if config.get(name, "component_module") == nil then
125                                         self:add_sRVName(name, "xmpp-client");
126                                 end
127                                 --print(name .. "#anonymous_login: " .. tostring(config.get(name, "anonymous_login")));
128                                 if not (config.get(name, "anonymous_login") or
129                                                 config.get(name, "authentication") == "anonymous") then
130                                         self:add_sRVName(name, "xmpp-server");
131                                 end
132                                 self:add_xmppAddr(name);
133                         end
134                 end
135         end
136         if not found_matching_hosts then
137                 return nil, "no-matching-hosts";
138         end
139 end
140
141 do -- Lua to shell calls.
142         local function shell_escape(s)
143                 return s:gsub("'",[['\'']]);
144         end
145
146         local function serialize(f,o)
147                 local r = {"openssl", f};
148                 for k,v in pairs(o) do
149                         if type(k) == "string" then
150                                 t_insert(r, ("-%s"):format(k));
151                                 if v ~= true then
152                                         t_insert(r, ("'%s'"):format(shell_escape(tostring(v))));
153                                 end
154                         end
155                 end
156                 for _,v in ipairs(o) do
157                         t_insert(r, ("'%s'"):format(shell_escape(tostring(v))));
158                 end
159                 return t_concat(r, " ");
160         end
161
162         local os_execute = os.execute;
163         setmetatable(_M, {
164                 __index=function(_,f)
165                         return function(opts)
166                                 return 0 == os_execute(serialize(f, type(opts) == "table" and opts or {}));
167                         end;
168                 end;
169         });
170 end
171
172 return _M;