de39a6d77b3bd6b6eaf56645dc21a94c34479754
[prosody.git] / util / sasl.lua
1
2 local md5 = require "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
9 local string = string
10 local math = require "math"
11 local type = type
12 local error = error
13 local print = print
14
15 module "sasl"
16
17 local function new_plain(realm, password_handler)
18         local object = { mechanism = "PLAIN", realm = realm, password_handler = password_handler}
19         object.feed =   function(self, message)
20                                                 --print(message:gsub("%W", function (c) return string.format("\\%d", string.byte(c)) end));
21
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]+)")
27                                                 
28                                                 local password_encoding, correct_password = self.password_handler(authentication, self.realm, "PLAIN")
29                                                 
30                                                 local claimed_password = ""
31                                                 if password_encoding == nil then claimed_password = password
32                                                 else claimed_password = password_encoding(password) end
33                                                 
34                                                 self.username = authentication
35                                                 if claimed_password == correct_password then
36                                                         log("debug", "success")
37                                                         return "success"
38                                                 else
39                                                         log("debug", "failure")
40                                                         return "failure", "not-authorized"
41                                                 end
42                                         end
43         return object
44 end
45
46 local function new_digest_md5(realm, password_handler)
47         --TODO maybe support for authzid
48
49         local function serialize(message)
50                 local data = ""
51                 
52                 if type(message) ~= "table" then error("serialize needs an argument of type table.") end
53                 
54                 -- testing all possible values
55                 if message["nonce"] then data = data..[[nonce="]]..message.nonce..[[",]] end
56                 if message["qop"] then data = data..[[qop="]]..message.qop..[[",]] end
57                 if message["charset"] then data = data..[[charset=]]..message.charset.."," end
58                 if message["algorithm"] then data = data..[[algorithm=]]..message.algorithm.."," end
59                 if message["realm"] then data = data..[[realm="]]..message.realm..[[",]] end
60                 if message["rspauth"] then data = data..[[rspauth=]]..message.rspauth.."," end
61                 data = data:gsub(",$", "")
62                 return data
63         end
64         
65         local function parse(data)
66                 message = {}
67                 log("debug", "parse-message: "..data)
68                 for k, v in gmatch(data, [[([%w%-]+)="?([%w%-%/%.%+=]+)"?,?]]) do
69                         message[k] = v
70                 log("debug", "               "..k.." = "..v)
71                 end
72                 return message
73         end
74
75         local object = { mechanism = "DIGEST-MD5", realm = realm, password_handler = password_handler}
76         
77         --TODO: something better than math.random would be nice, maybe OpenSSL's random number generator
78         object.nonce = generate_uuid()
79         object.step = 0
80         object.nonce_count = {}
81                                                                                                 
82         function object.feed(self, message)
83                 log("debug", "SASL step: "..self.step)
84                 self.step = self.step + 1
85                 if (self.step == 1) then
86                         local challenge = serialize({   nonce = object.nonce, 
87                                                                                         qop = "auth",
88                                                                                         charset = "utf-8",
89                                                                                         algorithm = "md5-sess",
90                                                                                         realm = self.realm});
91                         log("debug", "challenge: "..challenge)
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
98                         end
99                         
100                         -- check for username, it's REQUIRED by RFC 2831
101                         if not response["username"] then
102                                 return "failure", "malformed-request"
103                         end
104                         self["username"] = response["username"] 
105                         
106                         -- check for nonce, ...
107                         if not response["nonce"] then
108                                 return "failure", "malformed-request"
109                         else
110                                 -- check if it's the right nonce
111                                 if response["nonce"] ~= tostring(self.nonce) then return "failure", "malformed-request" end
112                         end
113                         
114                         if not response["cnonce"] then return "failure", "malformed-request" end
115                         if not response["qop"] then response["qop"] = "auth" end
116                         
117                         if response["realm"] == nil then response["realm"] = "" end
118                         
119                         local domain = ""
120                         local protocol = ""
121                         if response["digest-uri"] then
122                                 protocol, domain = response["digest-uri"]:match("(%w+)/(.*)$")
123                         else
124                                 return "failure", "malformed-request", "Missing entry for digest-uri in SASL message."
125                         end
126                         
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                         local A1 = Y..":"..response["nonce"]..":"..response["cnonce"]--:authzid
131                         local A2 = "AUTHENTICATE:"..protocol.."/"..domain
132                         
133                         local HA1 = md5.sumhexa(A1)
134                         local HA2 = md5.sumhexa(A2)
135                         
136                         local KD = HA1..":"..response["nonce"]..":"..response["nc"]..":"..response["cnonce"]..":"..response["qop"]..":"..HA2
137                         local response_value = md5.sumhexa(KD)
138                         
139                         log("debug", "response_value: "..response_value);
140                         log("debug", "response:       "..response["response"]);
141                         if response_value == response["response"] then
142                                 -- calculate rspauth
143                                 A2 = ":"..protocol.."/"..domain
144                                 
145                                 HA1 = md5.sumhexa(A1)
146                                 HA2 = md5.sumhexa(A2)
147         
148                                 KD = HA1..":"..response["nonce"]..":"..response["nc"]..":"..response["cnonce"]..":"..response["qop"]..":"..HA2
149                                 local rspauth = md5.sumhexa(KD)
150                                 
151                                 return "challenge", serialize({rspauth = rspauth})
152                         else
153                                 return "failure", "not-authorized", "The response provided by the client doesn't match the one we calculated."
154                         end                                                     
155                 elseif self.step == 3 then
156                         return "success"
157                 end
158         end
159         return object
160 end
161
162 function new(mechanism, realm, password_handler)
163         local object
164         if mechanism == "PLAIN" then object = new_plain(realm, password_handler)
165         elseif mechanism == "DIGEST-MD5" then object = new_digest_md5(realm, password_handler)
166         else
167                 log("debug", "Unsupported SASL mechanism: "..tostring(mechanism));
168                 return nil
169         end
170         return object
171 end
172
173 return _M;