Comparison

util/sasl/digest-md5.lua @ 2188:1fd38975addd sasl

Add support for plain profile in digest-md5 implementation.
author Tobias Markmann <tm@ayena.de>
date Fri, 13 Nov 2009 10:54:17 +0100
parent 2186:1112871916eb
child 2189:1182e7ae2964
comparison
equal deleted inserted replaced
2187:f0a85d11823e 2188:1fd38975addd
9 -- * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 9 -- * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10 -- * Neither the name of Tobias Markmann nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 10 -- * Neither the name of Tobias Markmann nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11 -- 11 --
12 -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 12 -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13 13
14 local tostring = tostring;
15 local type = type;
16
17 local s_gmatch = string.gmatch;
18 local s_match = string.match;
19 local t_concat = table.concat;
20 local t_insert = table.insert;
21 local to_byte, to_char = string.byte, string.char;
22
23 local md5 = require "util.hashes".md5;
24 local log = require "util.logger".init("sasl");
25 local generate_uuid = require "util.uuid".generate;
26
27 module "plain"
14 28
15 --========================= 29 --=========================
16 --SASL DIGEST-MD5 according to RFC 2831 30 --SASL DIGEST-MD5 according to RFC 2831
17 local function sasl_mechanism_digest_md5(self, message) 31 local function digest_response()
32
33 return response, A1, A2
34 end
35
36 local function digest(self, message)
18 --TODO complete support for authzid 37 --TODO complete support for authzid
19 38
20 local function serialize(message) 39 local function serialize(message)
21 local data = "" 40 local data = ""
22 41
66 end 85 end
67 return t_concat(p); 86 return t_concat(p);
68 end 87 end
69 local function latin1toutf8(str) 88 local function latin1toutf8(str)
70 local p = {}; 89 local p = {};
71 for ch in gmatch(str, ".") do 90 for ch in s_gmatch(str, ".") do
72 ch = to_byte(ch); 91 ch = to_byte(ch);
73 if (ch < 0x80) then 92 if (ch < 0x80) then
74 t_insert(p, to_char(ch)); 93 t_insert(p, to_char(ch));
75 elseif (ch < 0xC0) then 94 elseif (ch < 0xC0) then
76 t_insert(p, to_char(0xC2, ch)); 95 t_insert(p, to_char(0xC2, ch));
80 end 99 end
81 return t_concat(p); 100 return t_concat(p);
82 end 101 end
83 local function parse(data) 102 local function parse(data)
84 local message = {} 103 local message = {}
85 for k, v in gmatch(data, [[([%w%-]+)="?([^",]*)"?,?]]) do -- FIXME The hacky regex makes me shudder 104 for k, v in s_gmatch(data, [[([%w%-]+)="?([^",]*)"?,?]]) do -- FIXME The hacky regex makes me shudder
86 message[k] = v; 105 message[k] = v;
87 end 106 end
88 return message; 107 return message;
89 end 108 end
90 109
94 self.nonce_count = {}; 113 self.nonce_count = {};
95 end 114 end
96 115
97 self.step = self.step + 1; 116 self.step = self.step + 1;
98 if (self.step == 1) then 117 if (self.step == 1) then
99 local challenge = serialize({ nonce = object.nonce, 118 local challenge = serialize({ nonce = self.nonce,
100 qop = "auth", 119 qop = "auth",
101 charset = "utf-8", 120 charset = "utf-8",
102 algorithm = "md5-sess", 121 algorithm = "md5-sess",
103 realm = self.realm}); 122 realm = self.realm});
104 return "challenge", challenge; 123 return "challenge", challenge;
148 return "failure", "malformed-request", "Missing entry for digest-uri in SASL message." 167 return "failure", "malformed-request", "Missing entry for digest-uri in SASL message."
149 end 168 end
150 169
151 --TODO maybe realm support 170 --TODO maybe realm support
152 self.username = response["username"]; 171 self.username = response["username"];
153 local password_encoding, Y = self.credentials_handler("DIGEST-MD5", response["username"], self.realm, response["realm"], decoder); 172 if self.profile.plain then
154 if Y == nil then return "failure", "not-authorized" 173 local password, state = self.profile.plain(response["username"], self.realm)
155 elseif Y == false then return "failure", "account-disabled" end 174 if state == nil then return "failure", "not-authorized"
175 elseif state == false then return "failure", "account-disabled" end
176 Y = md5(response["username"]..":"..self.realm..":"..password);
177 elseif self.profile["digest-md5"] then
178 --local Y, state = self.profile["digest-md5"](response["username"], self.realm, response["charset"])
179 elseif self.profile["digest-md5-test"] then
180
181 end
182 --local password_encoding, Y = self.credentials_handler("DIGEST-MD5", response["username"], self.realm, response["realm"], decoder);
183 --if Y == nil then return "failure", "not-authorized"
184 --elseif Y == false then return "failure", "account-disabled" end
156 local A1 = ""; 185 local A1 = "";
157 if response.authzid then 186 if response.authzid then
158 if response.authzid == self.username.."@"..self.realm then 187 if response.authzid == self.username.."@"..self.realm then
159 -- COMPAT 188 -- COMPAT
160 log("warn", "Client is violating XMPP RFC. See section 6.1 of RFC 3920."); 189 log("warn", "Client is violating XMPP RFC. See section 6.1 of RFC 3920.");
190 elseif self.step == 3 then 219 elseif self.step == 3 then
191 if self.authenticated ~= nil then return "success" 220 if self.authenticated ~= nil then return "success"
192 else return "failure", "malformed-request" end 221 else return "failure", "malformed-request" end
193 end 222 end
194 end 223 end
224
225 function init(registerMechanism)
226 registerMechanism("DIGEST-MD5", {"plain"}, digest);
227 end
228
229 return _M;