Checking some variables for nil so no errors occur that'll break the server.
[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                 if authentication == nil or password == nil then return "failure", "malformed-request" end
28                 
29                 local password_encoding, correct_password = self.password_handler(authentication, self.realm, "PLAIN")
30                 
31                 if correct_password == nil then return "failure", "malformed-request" end
32                 
33                 local claimed_password = ""
34                 if password_encoding == nil then claimed_password = password
35                 else claimed_password = password_encoding(password) end
36                 
37                 self.username = authentication
38                 if claimed_password == correct_password then
39                         return "success"
40                 else
41                         return "failure", "not-authorized"
42                 end
43         end
44         return object
45 end
46
47 local function new_digest_md5(realm, password_handler)
48         --TODO maybe support for authzid
49
50         local function serialize(message)
51                 local data = ""
52                 
53                 if type(message) ~= "table" then error("serialize needs an argument of type table.") end
54                 
55                 -- testing all possible values
56                 if message["nonce"] then data = data..[[nonce="]]..message.nonce..[[",]] end
57                 if message["qop"] then data = data..[[qop="]]..message.qop..[[",]] end
58                 if message["charset"] then data = data..[[charset=]]..message.charset.."," end
59                 if message["algorithm"] then data = data..[[algorithm=]]..message.algorithm.."," end
60                 if message["realm"] then data = data..[[realm="]]..message.realm..[[",]] end
61                 if message["rspauth"] then data = data..[[rspauth=]]..message.rspauth.."," end
62                 data = data:gsub(",$", "")
63                 return data
64         end
65         
66         local function parse(data)
67                 message = {}
68                 for k, v in gmatch(data, [[([%w%-]+)="?([%w%-%/%.%+=]+)"?,?]]) do
69                         message[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                 self.step = self.step + 1
83                 if (self.step == 1) then
84                         local challenge = serialize({   nonce = object.nonce, 
85                                                                                         qop = "auth",
86                                                                                         charset = "utf-8",
87                                                                                         algorithm = "md5-sess",
88                                                                                         realm = self.realm});
89                         return "challenge", challenge
90                 elseif (self.step == 2) then
91                         local response = parse(message)
92                         -- check for replay attack
93                         if response["nc"] then
94                                 if self.nonce_count[response["nc"]] then return "failure", "not-authorized" end
95                         end
96                         
97                         -- check for username, it's REQUIRED by RFC 2831
98                         if not response["username"] then
99                                 return "failure", "malformed-request"
100                         end
101                         self["username"] = response["username"] 
102                         
103                         -- check for nonce, ...
104                         if not response["nonce"] then
105                                 return "failure", "malformed-request"
106                         else
107                                 -- check if it's the right nonce
108                                 if response["nonce"] ~= tostring(self.nonce) then return "failure", "malformed-request" end
109                         end
110                         
111                         if not response["cnonce"] then return "failure", "malformed-request", "Missing entry for cnonce in SASL message." end
112                         if not response["qop"] then response["qop"] = "auth" end
113                         
114                         if response["realm"] == nil then response["realm"] = "" end
115                         
116                         local domain = ""
117                         local protocol = ""
118                         if response["digest-uri"] then
119                                 protocol, domain = response["digest-uri"]:match("(%w+)/(.*)$")
120                                 if protocol == nil or domain == nil then return "failure", "malformed-request" end
121                         else
122                                 return "failure", "malformed-request", "Missing entry for digest-uri in SASL message."
123                         end
124                         
125                         --TODO maybe realm support
126                         self.username = response["username"]
127                         local password_encoding, Y = self.password_handler(response["username"], response["realm"], "DIGEST-MD5")
128                         if Y == nil then return "failure", "malformed-request" end
129                         
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                         if response_value == response["response"] then
140                                 -- calculate rspauth
141                                 A2 = ":"..protocol.."/"..domain
142                                 
143                                 HA1 = md5.sumhexa(A1)
144                                 HA2 = md5.sumhexa(A2)
145         
146                                 KD = HA1..":"..response["nonce"]..":"..response["nc"]..":"..response["cnonce"]..":"..response["qop"]..":"..HA2
147                                 local rspauth = md5.sumhexa(KD)
148                                 self.authenticated = true
149                                 return "challenge", serialize({rspauth = rspauth})
150                         else
151                                 return "failure", "not-authorized", "The response provided by the client doesn't match the one we calculated."
152                         end                                                     
153                 elseif self.step == 3 then
154                         if self.authenticated ~= nil then return "success"
155                         else return "failure", "malformed-request" end
156                 end
157         end
158         return object
159 end
160
161 function new(mechanism, realm, password_handler)
162         local object
163         if mechanism == "PLAIN" then object = new_plain(realm, password_handler)
164         elseif mechanism == "DIGEST-MD5" then object = new_digest_md5(realm, password_handler)
165         else
166                 log("debug", "Unsupported SASL mechanism: "..tostring(mechanism));
167                 return nil
168         end
169         return object
170 end
171
172 return _M;