Comparison

util/sasl/scram.lua @ 5828:24de22c01f8d

Adding some code for channel binding advertising.
author Tobias Markmann <tm@ayena.de>
date Wed, 12 Jan 2011 21:29:37 +0100
parent 3981:2b0b8fe68df2
child 5829:40c16475194e
comparison
equal deleted inserted replaced
3985:277b5bf9a200 5828:24de22c01f8d
12 -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 12 -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13 13
14 local s_match = string.match; 14 local s_match = string.match;
15 local type = type 15 local type = type
16 local string = string 16 local string = string
17 local tostring = tostring;
17 local base64 = require "util.encodings".base64; 18 local base64 = require "util.encodings".base64;
18 local hmac_sha1 = require "util.hmac".sha1; 19 local hmac_sha1 = require "util.hmac".sha1;
19 local sha1 = require "util.hashes".sha1; 20 local sha1 = require "util.hashes".sha1;
20 local generate_uuid = require "util.uuid".generate; 21 local generate_uuid = require "util.uuid".generate;
21 local saslprep = require "util.encodings".stringprep.saslprep; 22 local saslprep = require "util.encodings".stringprep.saslprep;
108 local stored_key = sha1(hmac_sha1(salted_password, "Client Key")) 109 local stored_key = sha1(hmac_sha1(salted_password, "Client Key"))
109 local server_key = hmac_sha1(salted_password, "Server Key"); 110 local server_key = hmac_sha1(salted_password, "Server Key");
110 return true, stored_key, server_key 111 return true, stored_key, server_key
111 end 112 end
112 113
114 local support_channel_binding = true;
115
113 local function scram_gen(hash_name, H_f, HMAC_f) 116 local function scram_gen(hash_name, H_f, HMAC_f)
114 local function scram_hash(self, message) 117 local function scram_hash(self, message)
115 if not self.state then self["state"] = {} end 118 if not self.state then self["state"] = {} end
116 119
117 if type(message) ~= "string" or #message == 0 then return "failure", "malformed-request" end 120 if type(message) ~= "string" or #message == 0 then return "failure", "malformed-request" end
118 if not self.state.name then 121 if not self.state.name then
119 -- we are processing client_first_message 122 -- we are processing client_first_message
120 local client_first_message = message; 123 local client_first_message = message;
121 124 log("debug", client_first_message);
122 -- TODO: fail if authzid is provided, since we don't support them yet 125 -- TODO: fail if authzid is provided, since we don't support them yet
123 self.state["client_first_message"] = client_first_message; 126 self.state["client_first_message"] = client_first_message;
124 self.state["gs2_cbind_flag"], self.state["authzid"], self.state["name"], self.state["clientnonce"] 127 self.state["gs2_cbind_flag"], self.state["gs2_cbind_name"], self.state["authzid"], self.state["name"], self.state["clientnonce"]
125 = client_first_message:match("^(%a),(.*),n=(.*),r=([^,]*).*"); 128 = client_first_message:match("^(%a)=?([%a%-]*),(.*),n=(.*),r=([^,]*).*");
126 129
127 -- we don't do any channel binding yet 130 -- we don't do any channel binding yet
128 if self.state.gs2_cbind_flag ~= "n" and self.state.gs2_cbind_flag ~= "y" then 131 log("debug", "Decoded: cbind_flag: %s, cbind_name: %s, authzid: %s, name: %s, clientnonce: %s", tostring(self.state.gs2_cbind_flag),
129 return "failure", "malformed-request"; 132 tostring(self.state.gs2_cbind_name),
133 tostring(self.state.authzid),
134 tostring(self.state.name),
135 tostring(self.state.clientnonce));
136 if support_channel_binding then
137 if string.sub(self.state.gs2_cbind_flag, 0, 1) == "y" then
138 return "failure", "malformed-request";
139 end
140 else
141 if self.state.gs2_cbind_flag ~= "n" and self.state.gs2_cbind_flag ~= "y" then
142 return "failure", "malformed-request";
143 end
130 end 144 end
131 145
132 if not self.state.name or not self.state.clientnonce then 146 if not self.state.name or not self.state.clientnonce then
133 return "failure", "malformed-request", "Channel binding isn't support at this time."; 147 return "failure", "malformed-request", "Channel binding isn't support at this time.";
134 end 148 end
177 self.state["server_first_message"] = server_first_message; 191 self.state["server_first_message"] = server_first_message;
178 return "challenge", server_first_message 192 return "challenge", server_first_message
179 else 193 else
180 -- we are processing client_final_message 194 -- we are processing client_final_message
181 local client_final_message = message; 195 local client_final_message = message;
182 196 log("debug", "client_final_message: %s", client_final_message);
183 self.state["channelbinding"], self.state["nonce"], self.state["proof"] = client_final_message:match("^c=(.*),r=(.*),.*p=(.*)"); 197 self.state["channelbinding"], self.state["nonce"], self.state["proof"] = client_final_message:match("^c=(.*),r=(.*),.*p=(.*)");
184 198
185 if not self.state.proof or not self.state.nonce or not self.state.channelbinding then 199 if not self.state.proof or not self.state.nonce or not self.state.channelbinding then
186 return "failure", "malformed-request", "Missing an attribute(p, r or c) in SASL message."; 200 return "failure", "malformed-request", "Missing an attribute(p, r or c) in SASL message.";
187 end 201 end
211 end 225 end
212 226
213 function init(registerMechanism) 227 function init(registerMechanism)
214 local function registerSCRAMMechanism(hash_name, hash, hmac_hash) 228 local function registerSCRAMMechanism(hash_name, hash, hmac_hash)
215 registerMechanism("SCRAM-"..hash_name, {"plain", "scram_"..(hashprep(hash_name))}, scram_gen(hash_name:lower(), hash, hmac_hash)); 229 registerMechanism("SCRAM-"..hash_name, {"plain", "scram_"..(hashprep(hash_name))}, scram_gen(hash_name:lower(), hash, hmac_hash));
230
231 -- register channel binding equivalent
232 registerMechanism("SCRAM-"..hash_name.."-PLUS", {"plain", "scram_"..(hashprep(hash_name))}, scram_gen(hash_name:lower(), hash, hmac_hash));
216 end 233 end
217 234
218 registerSCRAMMechanism("SHA-1", sha1, hmac_sha1); 235 registerSCRAMMechanism("SHA-1", sha1, hmac_sha1);
219 end 236 end
220 237