Added temporary fix for srv on windows: using opendns nameservers
[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         function object.feed(self, message)
20         
21                 if message == "" or message == nil then return "failure", "malformed-request" end
22                 local response = message
23                 local authorization = s_match(response, "([^&%z]+)")
24                 local authentication = s_match(response, "%z([^&%z]+)%z")
25                 local password = s_match(response, "%z[^&%z]+%z([^&%z]+)")
26                 
27                 local password_encoding, correct_password = self.password_handler(authentication, self.realm, "PLAIN")
28                 
29                 local claimed_password = ""
30                 if password_encoding == nil then claimed_password = password
31                 else claimed_password = password_encoding(password) end
32                 
33                 self.username = authentication
34                 if claimed_password == correct_password then
35                         log("debug", "success")
36                         return "success"
37                 else
38                         log("debug", "failure")
39                         return "failure", "not-authorized"
40                 end
41         end
42         return object
43 end
44
45 local function new_digest_md5(realm, password_handler)
46         --TODO maybe support for authzid
47
48         local function serialize(message)
49                 local data = ""
50                 
51                 if type(message) ~= "table" then error("serialize needs an argument of type table.") end
52                 
53                 -- testing all possible values
54                 if message["nonce"] then data = data..[[nonce="]]..message.nonce..[[",]] end
55                 if message["qop"] then data = data..[[qop="]]..message.qop..[[",]] end
56                 if message["charset"] then data = data..[[charset=]]..message.charset.."," end
57                 if message["algorithm"] then data = data..[[algorithm=]]..message.algorithm.."," end
58                 if message["realm"] then data = data..[[realm="]]..message.realm..[[",]] end
59                 if message["rspauth"] then data = data..[[rspauth=]]..message.rspauth.."," end
60                 data = data:gsub(",$", "")
61                 return data
62         end
63         
64         local function parse(data)
65                 message = {}
66                 log("debug", "parse-message: "..data)
67                 for k, v in gmatch(data, [[([%w%-]+)="?([%w%-%/%.%+=]+)"?,?]]) do
68                         message[k] = v
69                 log("debug", "               "..k.." = "..v)
70                 end
71                 return message
72         end
73
74         local object = { mechanism = "DIGEST-MD5", realm = realm, password_handler = password_handler}
75         
76         --TODO: something better than math.random would be nice, maybe OpenSSL's random number generator
77         object.nonce = generate_uuid()
78         object.step = 0
79         object.nonce_count = {}
80                                                                                                 
81         function object.feed(self, message)
82                 log("debug", "SASL step: "..self.step)
83                 self.step = self.step + 1
84                 if (self.step == 1) then
85                         local challenge = serialize({   nonce = object.nonce, 
86                                                                                         qop = "auth",
87                                                                                         charset = "utf-8",
88                                                                                         algorithm = "md5-sess",
89                                                                                         realm = self.realm});
90                         log("debug", "challenge: "..challenge)
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
97                         end
98                         
99                         -- check for username, it's REQUIRED by RFC 2831
100                         if not response["username"] then
101                                 return "failure", "malformed-request"
102                         end
103                         self["username"] = response["username"] 
104                         
105                         -- check for nonce, ...
106                         if not response["nonce"] then
107                                 return "failure", "malformed-request"
108                         else
109                                 -- check if it's the right nonce
110                                 if response["nonce"] ~= tostring(self.nonce) then return "failure", "malformed-request" end
111                         end
112                         
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
115                         
116                         if response["realm"] == nil then response["realm"] = "" end
117                         
118                         local domain = ""
119                         local protocol = ""
120                         if response["digest-uri"] then
121                                 protocol, domain = response["digest-uri"]:match("(%w+)/(.*)$")
122                         else
123                                 return "failure", "malformed-request", "Missing entry for digest-uri in SASL message."
124                         end
125                         
126                         --TODO maybe realm support
127                         self.username = response["username"]
128                         local password_encoding, Y = self.password_handler(response["username"], response["realm"], "DIGEST-MD5")
129                         local A1 = Y..":"..response["nonce"]..":"..response["cnonce"]--:authzid
130                         local A2 = "AUTHENTICATE:"..protocol.."/"..domain
131                         
132                         local HA1 = md5.sumhexa(A1)
133                         local HA2 = md5.sumhexa(A2)
134                         
135                         local KD = HA1..":"..response["nonce"]..":"..response["nc"]..":"..response["cnonce"]..":"..response["qop"]..":"..HA2
136                         local response_value = md5.sumhexa(KD)
137                         
138                         log("debug", "response_value: "..response_value);
139                         log("debug", "response:       "..response["response"]);
140                         if response_value == response["response"] then
141                                 -- calculate rspauth
142                                 A2 = ":"..protocol.."/"..domain
143                                 
144                                 HA1 = md5.sumhexa(A1)
145                                 HA2 = md5.sumhexa(A2)
146         
147                                 KD = HA1..":"..response["nonce"]..":"..response["nc"]..":"..response["cnonce"]..":"..response["qop"]..":"..HA2
148                                 local rspauth = md5.sumhexa(KD)
149                                 self.authenticated = true
150                                 return "challenge", serialize({rspauth = rspauth})
151                         else
152                                 return "failure", "not-authorized", "The response provided by the client doesn't match the one we calculated."
153                         end                                                     
154                 elseif self.step == 3 then
155                         if self.authenticated ~= nil then return "success"
156                         else return "failure", "malformed-request" end
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;