util/sasl/scram.lua

changeset 2190
41d42d253a1d
child 2192
614c839c30c5
equal deleted inserted replaced
2189:8fbbdb11a520 2190:41d42d253a1d
1 -- sasl.lua v0.4
2 -- Copyright (C) 2008-2009 Tobias Markmann
3 --
4 -- All rights reserved.
5 --
6 -- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 --
8 -- * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
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.
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.
13
14 local s_match = string.match;
15
16 local base64 = require "util.encodings".base64;
17 local xor = require "bit".bxor
18 local hmac_sha1 = require "util.hmac".sha1;
19 local sha1 = require "util.hashes".sha1;
20
21 module "plain"
22
23 --=========================
24 --SASL SCRAM-SHA-1 according to draft-ietf-sasl-scram-10
25 local default_i = 4096
26
27 local function bp( b )
28 local result = ""
29 for i=1, b:len() do
30 result = result.."\\"..b:byte(i)
31 end
32 return result
33 end
34
35 local function binaryXOR( a, b )
36 if string.len(a) > string.len(b) then
37 b = string.rep("\0", a:len() - b:len())..b
38 elseif string.len(a) < string.len(b) then
39 a = string.rep("\0", b:len() - a:len())..a
40 end
41 local result = ""
42 for i=1, a:len() do
43 result = result..string.char(xor(a:byte(i), b:byte(i)))
44 end
45 return result
46 end
47
48 -- hash algorithm independent Hi(PBKDF2) implementation
49 local function Hi(hmac, str, salt, i)
50 local Ust = hmac(str, salt.."\0\0\0\1");
51 local res = Ust;
52 for n=1,i-1 do
53 Und = hmac(str, Ust)
54 res = binaryXOR(res, Und)
55 Ust = Und
56 end
57 return res
58 end
59
60 local function validate_username(username)
61 -- check for forbidden char sequences
62
63 -- replace =2D with , and =3D with =
64
65 -- apply SASLprep
66 return username;
67 end
68
69 local function scram_sha_1(self, message)
70 if not self.state then self["state"] = {} end
71
72 if not self.state.name then
73 -- we are processing client_first_message
74 self.state["name"] = string.match(client_first_message, "n=(.+),r=")
75 self.state["clientnonce"] = string.match(client_first_message, "r=([^,]+)")
76
77 self.state.name = validate_username(self.state.name);
78 if not self.state.name then
79 return "failure", "malformed-request";
80 end
81 else
82 -- we are processing client_final_message
83 end
84 end
85
86 function init(registerMechanism)
87 registerMechanism("SCRAM-SHA-1", {"plain"}, scram_sha_1);
88 end
89
90 return _M;

mercurial