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
15 local idna_unicode = require "util.encodings".idna.to_unicode
19 local function new_plain(realm, password_handler)
20 local object = { mechanism = "PLAIN", realm = realm, password_handler = password_handler}
21 function object.feed(self, message)
23 if message == "" or message == nil then return "failure", "malformed-request" end
24 local response = message
25 local authorization = s_match(response, "([^&%z]+)")
26 local authentication = s_match(response, "%z([^&%z]+)%z")
27 local password = s_match(response, "%z[^&%z]+%z([^&%z]+)")
29 if authentication == nil or password == nil then return "failure", "malformed-request" end
31 local password_encoding, correct_password = self.password_handler(authentication, self.realm, "PLAIN")
33 if correct_password == nil then return "failure", "not-authorized"
34 elseif correct_password == false then return "failure", "account-disabled" end
36 local claimed_password = ""
37 if password_encoding == nil then claimed_password = password
38 else claimed_password = password_encoding(password) end
40 self.username = authentication
41 if claimed_password == correct_password then
44 return "failure", "not-authorized"
50 local function new_digest_md5(realm, password_handler)
51 --TODO maybe support for authzid
53 local function serialize(message)
56 if type(message) ~= "table" then error("serialize needs an argument of type table.") end
58 -- testing all possible values
59 if message["nonce"] then data = data..[[nonce="]]..message.nonce..[[",]] end
60 if message["qop"] then data = data..[[qop="]]..message.qop..[[",]] end
61 if message["charset"] then data = data..[[charset=]]..message.charset.."," end
62 if message["algorithm"] then data = data..[[algorithm=]]..message.algorithm.."," end
63 if message["realm"] then data = data..[[realm="]]..message.realm..[[",]] end
64 if message["rspauth"] then data = data..[[rspauth=]]..message.rspauth.."," end
65 data = data:gsub(",$", "")
69 local function parse(data)
71 for k, v in gmatch(data, [[([%w%-]+)="?([^",]*)"?,?]]) do -- FIXME The hacky regex makes me shudder
77 local object = { mechanism = "DIGEST-MD5", realm = realm, password_handler = password_handler}
79 --TODO: something better than math.random would be nice, maybe OpenSSL's random number generator
80 object.nonce = generate_uuid()
82 object.nonce_count = {}
84 function object.feed(self, message)
85 self.step = self.step + 1
86 if (self.step == 1) then
87 local challenge = serialize({ nonce = object.nonce,
90 algorithm = "md5-sess",
91 realm = idna_ascii(self.realm)});
92 return "challenge", challenge
93 elseif (self.step == 2) then
94 local response = parse(message)
95 -- check for replay attack
96 if response["nc"] then
97 if self.nonce_count[response["nc"]] then return "failure", "not-authorized" end
100 -- check for username, it's REQUIRED by RFC 2831
101 if not response["username"] then
102 return "failure", "malformed-request"
104 self["username"] = response["username"]
106 -- check for nonce, ...
107 if not response["nonce"] then
108 return "failure", "malformed-request"
110 -- check if it's the right nonce
111 if response["nonce"] ~= tostring(self.nonce) then return "failure", "malformed-request" end
114 if not response["cnonce"] then return "failure", "malformed-request", "Missing entry for cnonce in SASL message." end
115 if not response["qop"] then response["qop"] = "auth" end
117 if response["realm"] == nil then response["realm"] = "" end
121 if response["digest-uri"] then
122 protocol, domain = response["digest-uri"]:match("(%w+)/(.*)$")
123 if protocol == nil or domain == nil then return "failure", "malformed-request" end
125 return "failure", "malformed-request", "Missing entry for digest-uri in SASL message."
128 --TODO maybe realm support
129 self.username = response["username"]
130 local password_encoding, Y = self.password_handler(response["username"], idna_unicode(response["realm"]), "DIGEST-MD5")
131 if Y == nil then return "failure", "not-authorized"
132 elseif Y == false then return "failure", "account-disabled" end
134 local A1 = Y..":"..response["nonce"]..":"..response["cnonce"]--:authzid
135 local A2 = "AUTHENTICATE:"..protocol.."/"..idna_ascii(domain)
137 local HA1 = md5(A1, true)
138 local HA2 = md5(A2, true)
140 local KD = HA1..":"..response["nonce"]..":"..response["nc"]..":"..response["cnonce"]..":"..response["qop"]..":"..HA2
141 local response_value = md5(KD, true)
143 if response_value == response["response"] then
145 A2 = ":"..protocol.."/"..idna_ascii(domain)
150 KD = HA1..":"..response["nonce"]..":"..response["nc"]..":"..response["cnonce"]..":"..response["qop"]..":"..HA2
151 local rspauth = md5(KD, true)
152 self.authenticated = true
153 return "challenge", serialize({rspauth = rspauth})
155 return "failure", "not-authorized", "The response provided by the client doesn't match the one we calculated."
157 elseif self.step == 3 then
158 if self.authenticated ~= nil then return "success"
159 else return "failure", "malformed-request" end
165 function new(mechanism, realm, password_handler)
167 if mechanism == "PLAIN" then object = new_plain(realm, password_handler)
168 elseif mechanism == "DIGEST-MD5" then object = new_digest_md5(realm, password_handler)
170 log("debug", "Unsupported SASL mechanism: "..tostring(mechanism));