Merge 0.10->trunk
[prosody.git] / util / sasl / scram.lua
index a18f025e951e693a80981880d8fc8b46a1d845a1..a1b5117a7f2833d09d1427a5956b0c7f9f2efb9e 100644 (file)
@@ -25,7 +25,7 @@ local t_concat = table.concat;
 local char = string.char;
 local byte = string.byte;
 
-module "sasl.scram"
+local _ENV = nil;
 
 --=========================
 --SASL SCRAM-SHA-1 according to RFC 5802
@@ -87,7 +87,7 @@ local function hashprep(hashname)
        return hashname:lower():gsub("-", "_");
 end
 
-function getAuthenticationDatabaseSHA1(password, salt, iteration_count)
+local function getAuthenticationDatabaseSHA1(password, salt, iteration_count)
        if type(password) ~= "string" or type(salt) ~= "string" or type(iteration_count) ~= "number" then
                return false, "inappropriate argument types"
        end
@@ -101,6 +101,7 @@ function getAuthenticationDatabaseSHA1(password, salt, iteration_count)
 end
 
 local function scram_gen(hash_name, H_f, HMAC_f)
+       local profile_name = "scram_" .. hashprep(hash_name);
        local function scram_hash(self, message)
                local support_channel_binding = false;
                if self.profile.cb then support_channel_binding = true; end
@@ -112,8 +113,8 @@ local function scram_gen(hash_name, H_f, HMAC_f)
                        local client_first_message = message;
 
                        -- TODO: fail if authzid is provided, since we don't support them yet
-                       local gs2_header, gs2_cbind_flag, gs2_cbind_name, authzid, name, clientnonce
-                               = client_first_message:match("^(([ynp])=?([%a%-]*),(.*),)n=(.*),r=([^,]*).*");
+                       local gs2_header, gs2_cbind_flag, gs2_cbind_name, authzid, client_first_message_bare, username, clientnonce
+                               = s_match(client_first_message, "^(([pny])=?([^,]*),([^,]*),)(m?=?[^,]*,?n=([^,]*),r=([^,]*),?.*)$");
 
                        if not gs2_cbind_flag then
                                return "failure", "malformed-request";
@@ -140,8 +141,8 @@ local function scram_gen(hash_name, H_f, HMAC_f)
                                gs2_cbind_name = nil;
                        end
 
-                       name = validate_username(name, self.profile.nodeprep);
-                       if not name then
+                       username = validate_username(username, self.profile.nodeprep);
+                       if not username then
                                log("debug", "Username violates either SASLprep or contains forbidden character sequences.")
                                return "failure", "malformed-request", "Invalid username.";
                        end
@@ -149,7 +150,7 @@ local function scram_gen(hash_name, H_f, HMAC_f)
                        -- retreive credentials
                        local stored_key, server_key, salt, iteration_count;
                        if self.profile.plain then
-                               local password, state = self.profile.plain(self, name, self.realm)
+                               local password, state = self.profile.plain(self, username, self.realm)
                                if state == nil then return "failure", "not-authorized"
                                elseif state == false then return "failure", "account-disabled" end
 
@@ -168,9 +169,9 @@ local function scram_gen(hash_name, H_f, HMAC_f)
                                        log("error", "Generating authentication database failed. Reason: %s", stored_key);
                                        return "failure", "temporary-auth-failure";
                                end
-                       elseif self.profile["scram_"..hashprep(hash_name)] then
+                       elseif self.profile[profile_name] then
                                local state;
-                               stored_key, server_key, iteration_count, salt, state = self.profile["scram_"..hashprep(hash_name)](self, name, self.realm);
+                               stored_key, server_key, iteration_count, salt, state = self.profile[profile_name](self, username, self.realm);
                                if state == nil then return "failure", "not-authorized"
                                elseif state == false then return "failure", "account-disabled" end
                        end
@@ -180,12 +181,12 @@ local function scram_gen(hash_name, H_f, HMAC_f)
                        self.state = {
                                gs2_header = gs2_header;
                                gs2_cbind_name = gs2_cbind_name;
-                               name = name;
+                               username = username;
                                nonce = nonce;
 
                                server_key = server_key;
                                stored_key = stored_key;
-                               client_first_message = client_first_message;
+                               client_first_message_bare = client_first_message_bare;
                                server_first_message = server_first_message;
                        }
                        return "challenge", server_first_message
@@ -193,7 +194,8 @@ local function scram_gen(hash_name, H_f, HMAC_f)
                        -- we are processing client_final_message
                        local client_final_message = message;
 
-                       local channelbinding, nonce, proof = client_final_message:match("^c=(.*),r=(.*),.*p=(.*)");
+                       local client_final_message_without_proof, channelbinding, nonce, proof
+                               = s_match(client_final_message, "(c=([^,]*),r=([^,]*),?.-),p=(.*)$");
 
                        if not proof or not nonce or not channelbinding then
                                return "failure", "malformed-request", "Missing an attribute(p, r or c) in SASL message.";
@@ -216,14 +218,14 @@ local function scram_gen(hash_name, H_f, HMAC_f)
                        local ServerKey = state.server_key;
                        local StoredKey = state.stored_key;
 
-                       local AuthMessage = "n=" .. s_match(state.client_first_message,"n=(.+)") .. "," .. state.server_first_message .. "," .. s_match(client_final_message, "(.+),p=.+")
+                       local AuthMessage = state.client_first_message_bare .. "," .. state.server_first_message .. "," .. client_final_message_without_proof
                        local ClientSignature = HMAC_f(StoredKey, AuthMessage)
                        local ClientKey = binaryXOR(ClientSignature, base64.decode(proof))
                        local ServerSignature = HMAC_f(ServerKey, AuthMessage)
 
                        if StoredKey == H_f(ClientKey) then
                                local server_final_message = "v="..base64.encode(ServerSignature);
-                               self["username"] = state.name;
+                               self["username"] = state.username;
                                return "success", server_final_message;
                        else
                                return "failure", "not-authorized", "The response provided by the client doesn't match the one we calculated.";
@@ -233,7 +235,7 @@ local function scram_gen(hash_name, H_f, HMAC_f)
        return scram_hash;
 end
 
-function init(registerMechanism)
+local function init(registerMechanism)
        local function registerSCRAMMechanism(hash_name, hash, hmac_hash)
                registerMechanism("SCRAM-"..hash_name, {"plain", "scram_"..(hashprep(hash_name))}, scram_gen(hash_name:lower(), hash, hmac_hash));
 
@@ -244,4 +246,7 @@ function init(registerMechanism)
        registerSCRAMMechanism("SHA-1", sha1, hmac_sha1);
 end
 
-return _M;
+return {
+       getAuthenticationDatabaseSHA1 = getAuthenticationDatabaseSHA1;
+       init = init;
+}