2 local md5 = require "util.hashes".md5;
3 local log = require "util.logger".init("sasl");
4 local tostring = tostring;
5 local st = require "util.stanza";
6 local generate_uuid = require "util.uuid".generate;
7 local s_match = string.match;
8 local gmatch = string.gmatch
10 local math = require "math"
14 local idna_ascii = require "util.encodings".idna.to_ascii
18 local function new_plain(realm, password_handler)
19 local object = { mechanism = "PLAIN", realm = realm, password_handler = password_handler}
20 function object.feed(self, message)
22 if message == "" or message == nil then return "failure", "malformed-request" end
23 local response = message
24 local authorization = s_match(response, "([^&%z]+)")
25 local authentication = s_match(response, "%z([^&%z]+)%z")
26 local password = s_match(response, "%z[^&%z]+%z([^&%z]+)")
28 if authentication == nil or password == nil then return "failure", "malformed-request" end
30 local password_encoding, correct_password = self.password_handler(authentication, self.realm, "PLAIN")
32 if correct_password == nil then return "failure", "not-authorized"
33 elseif correct_password == false then return "failure", "account-disabled" end
35 local claimed_password = ""
36 if password_encoding == nil then claimed_password = password
37 else claimed_password = password_encoding(password) end
39 self.username = authentication
40 if claimed_password == correct_password then
43 return "failure", "not-authorized"
49 local function new_digest_md5(realm, password_handler)
50 --TODO maybe support for authzid
52 local function serialize(message)
55 if type(message) ~= "table" then error("serialize needs an argument of type table.") end
57 -- testing all possible values
58 if message["nonce"] then data = data..[[nonce="]]..message.nonce..[[",]] end
59 if message["qop"] then data = data..[[qop="]]..message.qop..[[",]] end
60 if message["charset"] then data = data..[[charset=]]..message.charset.."," end
61 if message["algorithm"] then data = data..[[algorithm=]]..message.algorithm.."," end
62 if message["realm"] then data = data..[[realm="]]..message.realm..[[",]] end
63 if message["rspauth"] then data = data..[[rspauth=]]..message.rspauth.."," end
64 data = data:gsub(",$", "")
68 local function parse(data)
70 for k, v in gmatch(data, [[([%w%-]+)="?([^",]*)"?,?]]) do -- FIXME The hacky regex makes me shudder
76 local object = { mechanism = "DIGEST-MD5", realm = realm, password_handler = password_handler}
78 --TODO: something better than math.random would be nice, maybe OpenSSL's random number generator
79 object.nonce = generate_uuid()
81 object.nonce_count = {}
83 function object.feed(self, message)
84 self.step = self.step + 1
85 if (self.step == 1) then
86 local challenge = serialize({ nonce = object.nonce,
89 algorithm = "md5-sess",
91 return "challenge", challenge
92 elseif (self.step == 2) then
93 local response = parse(message)
94 -- check for replay attack
95 if response["nc"] then
96 if self.nonce_count[response["nc"]] then return "failure", "not-authorized" end
99 -- check for username, it's REQUIRED by RFC 2831
100 if not response["username"] then
101 return "failure", "malformed-request"
103 self["username"] = response["username"]
105 -- check for nonce, ...
106 if not response["nonce"] then
107 return "failure", "malformed-request"
109 -- check if it's the right nonce
110 if response["nonce"] ~= tostring(self.nonce) then return "failure", "malformed-request" end
113 if not response["cnonce"] then return "failure", "malformed-request", "Missing entry for cnonce in SASL message." end
114 if not response["qop"] then response["qop"] = "auth" end
116 if response["realm"] == nil then response["realm"] = "" end
120 if response["digest-uri"] then
121 protocol, domain = response["digest-uri"]:match("(%w+)/(.*)$")
122 if protocol == nil or domain == nil then return "failure", "malformed-request" end
124 return "failure", "malformed-request", "Missing entry for digest-uri in SASL message."
127 --TODO maybe realm support
128 self.username = response["username"]
129 local password_encoding, Y = self.password_handler(response["username"], response["realm"], "DIGEST-MD5")
130 if Y == nil then return "failure", "not-authorized"
131 elseif Y == false then return "failure", "account-disabled" end
133 local A1 = Y..":"..response["nonce"]..":"..response["cnonce"]--:authzid
134 local A2 = "AUTHENTICATE:"..protocol.."/"..idna_ascii(domain)
136 local HA1 = md5(A1, true)
137 local HA2 = md5(A2, true)
139 local KD = HA1..":"..response["nonce"]..":"..response["nc"]..":"..response["cnonce"]..":"..response["qop"]..":"..HA2
140 local response_value = md5(KD, true)
142 if response_value == response["response"] then
144 A2 = ":"..protocol.."/"..idna_ascii(domain)
149 KD = HA1..":"..response["nonce"]..":"..response["nc"]..":"..response["cnonce"]..":"..response["qop"]..":"..HA2
150 local rspauth = md5(KD, true)
151 self.authenticated = true
152 return "challenge", serialize({rspauth = rspauth})
154 return "failure", "not-authorized", "The response provided by the client doesn't match the one we calculated."
156 elseif self.step == 3 then
157 if self.authenticated ~= nil then return "success"
158 else return "failure", "malformed-request" end
164 function new(mechanism, realm, password_handler)
166 if mechanism == "PLAIN" then object = new_plain(realm, password_handler)
167 elseif mechanism == "DIGEST-MD5" then object = new_digest_md5(realm, password_handler)
169 log("debug", "Unsupported SASL mechanism: "..tostring(mechanism));