Comparison

util/sasl/scram.lua @ 2294:90e4941ea8b6

Merge with Tobias
author Matthew Wild <mwild1@gmail.com>
date Wed, 02 Dec 2009 20:33:09 +0000
parent 2290:ef7027a0f0c9
child 2314:c2e1bde4d84d
comparison
equal deleted inserted replaced
2278:8c10f13c0c20 2294:90e4941ea8b6
19 local hmac_sha1 = require "util.hmac".sha1; 19 local hmac_sha1 = require "util.hmac".sha1;
20 local sha1 = require "util.hashes".sha1; 20 local sha1 = require "util.hashes".sha1;
21 local generate_uuid = require "util.uuid".generate; 21 local generate_uuid = require "util.uuid".generate;
22 local saslprep = require "util.encodings".stringprep.saslprep; 22 local saslprep = require "util.encodings".stringprep.saslprep;
23 local log = require "util.logger".init("sasl"); 23 local log = require "util.logger".init("sasl");
24 local t_concat = table.concat;
25 local char = string.char;
26 local byte = string.byte;
27 24
28 module "scram" 25 module "scram"
29 26
30 --========================= 27 --=========================
31 --SASL SCRAM-SHA-1 according to draft-ietf-sasl-scram-10 28 --SASL SCRAM-SHA-1 according to draft-ietf-sasl-scram-10
37 result = result.."\\"..b:byte(i) 34 result = result.."\\"..b:byte(i)
38 end 35 end
39 return result 36 return result
40 end 37 end
41 38
42 local xor_map = {0;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;1;0;3;2;5;4;7;6;9;8;11;10;13;12;15;14;2;3;0;1;6;7;4;5;10;11;8;9;14;15;12;13;3;2;1;0;7;6;5;4;11;10;9;8;15;14;13;12;4;5;6;7;0;1;2;3;12;13;14;15;8;9;10;11;5;4;7;6;1;0;3;2;13;12;15;14;9;8;11;10;6;7;4;5;2;3;0;1;14;15;12;13;10;11;8;9;7;6;5;4;3;2;1;0;15;14;13;12;11;10;9;8;8;9;10;11;12;13;14;15;0;1;2;3;4;5;6;7;9;8;11;10;13;12;15;14;1;0;3;2;5;4;7;6;10;11;8;9;14;15;12;13;2;3;0;1;6;7;4;5;11;10;9;8;15;14;13;12;3;2;1;0;7;6;5;4;12;13;14;15;8;9;10;11;4;5;6;7;0;1;2;3;13;12;15;14;9;8;11;10;5;4;7;6;1;0;3;2;14;15;12;13;10;11;8;9;6;7;4;5;2;3;0;1;15;14;13;12;11;10;9;8;7;6;5;4;3;2;1;0;};
43
44 local result = {};
45 local function binaryXOR( a, b ) 39 local function binaryXOR( a, b )
46 for i=1, #a do 40 if a:len() > b:len() then
47 local x, y = byte(a, i), byte(b, i); 41 b = string.rep("\0", a:len() - b:len())..b
48 local lowx, lowy = x % 16, y % 16; 42 elseif string.len(a) < string.len(b) then
49 local hix, hiy = (x - lowx) / 16, (y - lowy) / 16; 43 a = string.rep("\0", b:len() - a:len())..a
50 local lowr, hir = xor_map[lowx * 16 + lowy + 1], xor_map[hix * 16 + hiy + 1];
51 local r = hir * 16 + lowr;
52 result[i] = char(r)
53 end 44 end
54 return t_concat(result); 45 local result = ""
46 for i=1, a:len() do
47 result = result..string.char(xor(a:byte(i), b:byte(i)))
48 end
49 return result
55 end 50 end
56 51
57 -- hash algorithm independent Hi(PBKDF2) implementation 52 -- hash algorithm independent Hi(PBKDF2) implementation
58 local function Hi(hmac, str, salt, i) 53 local function Hi(hmac, str, salt, i)
59 local Ust = hmac(str, salt.."\0\0\0\1"); 54 local Ust = hmac(str, salt.."\0\0\0\1");
119 self.state["channelbinding"] = client_final_message:match("c=(.+),r="); 114 self.state["channelbinding"] = client_final_message:match("c=(.+),r=");
120 if not self.state.proof or not self.state.nonce or not self.state.channelbinding then 115 if not self.state.proof or not self.state.nonce or not self.state.channelbinding then
121 return "failure", "malformed-request", "Missing an attribute(p, r or c) in SASL message."; 116 return "failure", "malformed-request", "Missing an attribute(p, r or c) in SASL message.";
122 end 117 end
123 118
124 local password, state; 119 local password;
125 if self.profile.plain then 120 if self.profile.plain then
126 password, state = self.profile.plain(self.state.name, self.realm) 121 local password, state = self.profile.plain(self.state.name, self.realm)
127 if state == nil then return "failure", "not-authorized" 122 if state == nil then return "failure", "not-authorized"
128 elseif state == false then return "failure", "account-disabled" end 123 elseif state == false then return "failure", "account-disabled" end
129 password = saslprep(password); 124 password = saslprep(password);
130 if not password then 125 if not password then
131 log("debug", "Password violates SASLprep."); 126 log("debug", "Password violates SASLprep.");