Tue, 25 Aug 2015 16:07:20 +0200
util.sasl.scram: Use the new util.hashes and util.random
355 | 1 | |
2 | local base64, unbase64 = require "mime".b64, require"mime".unb64; | |
390
7f535a1d5827
util.sasl.scram: Use the new util.hashes and util.random
Kim Alvefur <zash@zash.se>
parents:
365
diff
changeset
|
3 | local hashes = require"util.hashes"; |
355 | 4 | local bit = require"bit"; |
390
7f535a1d5827
util.sasl.scram: Use the new util.hashes and util.random
Kim Alvefur <zash@zash.se>
parents:
365
diff
changeset
|
5 | local random = require"util.random"; |
355 | 6 | |
7 | local tonumber = tonumber; | |
8 | local char, byte = string.char, string.byte; | |
9 | local gsub = string.gsub; | |
10 | local xor = bit.bxor; | |
11 | ||
358
a8f6fd6a70ed
plugins.sasl: Alter mechanism loading and pass name of loaded mechanism. Fixes attempting SCRAM-PLUS when only SCRAM is offered
Kim Alvefur <zash@zash.se>
parents:
356
diff
changeset
|
12 | local function XOR(a, b) |
355 | 13 | return (gsub(a, "()(.)", function(i, c) |
14 | return char(xor(byte(c), byte(b, i))) | |
15 | end)); | |
16 | end | |
17 | ||
390
7f535a1d5827
util.sasl.scram: Use the new util.hashes and util.random
Kim Alvefur <zash@zash.se>
parents:
365
diff
changeset
|
18 | local H, HMAC = hashes.sha1, hashes.hmac_sha1; |
355 | 19 | |
358
a8f6fd6a70ed
plugins.sasl: Alter mechanism loading and pass name of loaded mechanism. Fixes attempting SCRAM-PLUS when only SCRAM is offered
Kim Alvefur <zash@zash.se>
parents:
356
diff
changeset
|
20 | local function Hi(str, salt, i) |
355 | 21 | local U = HMAC(str, salt .. "\0\0\0\1"); |
22 | local ret = U; | |
23 | for _ = 2, i do | |
24 | U = HMAC(str, U); | |
25 | ret = XOR(ret, U); | |
26 | end | |
27 | return ret; | |
28 | end | |
29 | ||
30 | local function Normalize(str) | |
31 | return str; -- TODO | |
32 | end | |
33 | ||
34 | local function value_safe(str) | |
35 | return (gsub(str, "[,=]", { [","] = "=2C", ["="] = "=3D" })); | |
36 | end | |
37 | ||
38 | local function scram(stream, name) | |
39 | local username = "n=" .. value_safe(stream.username); | |
390
7f535a1d5827
util.sasl.scram: Use the new util.hashes and util.random
Kim Alvefur <zash@zash.se>
parents:
365
diff
changeset
|
40 | local c_nonce = base64(random.bytes(15)); |
362
d8c3e94d765d
util.sasl.scram: Correctly verify that the server added its own nonce
Kim Alvefur <zash@zash.se>
parents:
359
diff
changeset
|
41 | local our_nonce = "r=" .. c_nonce; |
d8c3e94d765d
util.sasl.scram: Correctly verify that the server added its own nonce
Kim Alvefur <zash@zash.se>
parents:
359
diff
changeset
|
42 | local client_first_message_bare = username .. "," .. our_nonce; |
355 | 43 | local cbind_data = ""; |
365
48bf6993b4c4
util.sasl.scram: Only indicate channel binding support when TLS is used
Kim Alvefur <zash@zash.se>
parents:
363
diff
changeset
|
44 | local gs2_cbind_flag = stream.conn:ssl() and "y" or "n"; |
356
f95e797895ee
SCRAM: Add channel binding support (SCRAM-SHA-1-PLUS)
Kim Alvefur <zash@zash.se>
parents:
355
diff
changeset
|
45 | if name == "SCRAM-SHA-1-PLUS" then |
f95e797895ee
SCRAM: Add channel binding support (SCRAM-SHA-1-PLUS)
Kim Alvefur <zash@zash.se>
parents:
355
diff
changeset
|
46 | cbind_data = stream.conn:socket():getfinished(); |
f95e797895ee
SCRAM: Add channel binding support (SCRAM-SHA-1-PLUS)
Kim Alvefur <zash@zash.se>
parents:
355
diff
changeset
|
47 | gs2_cbind_flag = "p=tls-unique"; |
f95e797895ee
SCRAM: Add channel binding support (SCRAM-SHA-1-PLUS)
Kim Alvefur <zash@zash.se>
parents:
355
diff
changeset
|
48 | end |
355 | 49 | local gs2_header = gs2_cbind_flag .. ",,"; |
50 | local client_first_message = gs2_header .. client_first_message_bare; | |
51 | local cont, server_first_message = coroutine.yield(client_first_message); | |
52 | if cont ~= "challenge" then return false end | |
53 | ||
362
d8c3e94d765d
util.sasl.scram: Correctly verify that the server added its own nonce
Kim Alvefur <zash@zash.se>
parents:
359
diff
changeset
|
54 | local nonce, salt, iteration_count = server_first_message:match("(r=[^,]+),s=([^,]*),i=(%d+)"); |
355 | 55 | local i = tonumber(iteration_count); |
56 | salt = unbase64(salt); | |
57 | if not nonce or not salt or not i then | |
58 | return false, "Could not parse server_first_message"; | |
59 | elseif nonce:find(c_nonce, 3, true) ~= 3 then | |
60 | return false, "nonce sent by server does not match our nonce"; | |
362
d8c3e94d765d
util.sasl.scram: Correctly verify that the server added its own nonce
Kim Alvefur <zash@zash.se>
parents:
359
diff
changeset
|
61 | elseif nonce == our_nonce then |
355 | 62 | return false, "server did not append s-nonce to nonce"; |
63 | end | |
64 | ||
65 | local cbind_input = gs2_header .. cbind_data; | |
66 | local channel_binding = "c=" .. base64(cbind_input); | |
67 | local client_final_message_without_proof = channel_binding .. "," .. nonce; | |
68 | ||
69 | local SaltedPassword = Hi(Normalize(stream.password), salt, i); | |
70 | local ClientKey = HMAC(SaltedPassword, "Client Key"); | |
71 | local StoredKey = H(ClientKey); | |
72 | local AuthMessage = client_first_message_bare .. "," .. server_first_message .. "," .. client_final_message_without_proof; | |
73 | local ClientSignature = HMAC(StoredKey, AuthMessage); | |
74 | local ClientProof = XOR(ClientKey, ClientSignature); | |
75 | local ServerKey = HMAC(SaltedPassword, "Server Key"); | |
76 | local ServerSignature = HMAC(ServerKey, AuthMessage); | |
77 | ||
78 | local proof = "p=" .. base64(ClientProof); | |
79 | local client_final_message = client_final_message_without_proof .. "," .. proof; | |
80 | ||
81 | local ok, server_final_message = coroutine.yield(client_final_message); | |
82 | if ok ~= "success" then return false, "success-expected" end | |
83 | ||
84 | local verifier = server_final_message:match("v=([^,]+)"); | |
85 | if unbase64(verifier) ~= ServerSignature then | |
86 | return false, "server signature did not match"; | |
87 | end | |
88 | return true; | |
89 | end | |
90 | ||
358
a8f6fd6a70ed
plugins.sasl: Alter mechanism loading and pass name of loaded mechanism. Fixes attempting SCRAM-PLUS when only SCRAM is offered
Kim Alvefur <zash@zash.se>
parents:
356
diff
changeset
|
91 | return function (stream, name) |
355 | 92 | if stream.username and (stream.password or (stream.client_key or stream.server_key)) then |
358
a8f6fd6a70ed
plugins.sasl: Alter mechanism loading and pass name of loaded mechanism. Fixes attempting SCRAM-PLUS when only SCRAM is offered
Kim Alvefur <zash@zash.se>
parents:
356
diff
changeset
|
93 | if name == "SCRAM-SHA-1" then |
a8f6fd6a70ed
plugins.sasl: Alter mechanism loading and pass name of loaded mechanism. Fixes attempting SCRAM-PLUS when only SCRAM is offered
Kim Alvefur <zash@zash.se>
parents:
356
diff
changeset
|
94 | return scram, 99; |
359 | 95 | elseif name == "SCRAM-SHA-1-PLUS" then |
358
a8f6fd6a70ed
plugins.sasl: Alter mechanism loading and pass name of loaded mechanism. Fixes attempting SCRAM-PLUS when only SCRAM is offered
Kim Alvefur <zash@zash.se>
parents:
356
diff
changeset
|
96 | local sock = stream.conn:ssl() and stream.conn:socket(); |
a8f6fd6a70ed
plugins.sasl: Alter mechanism loading and pass name of loaded mechanism. Fixes attempting SCRAM-PLUS when only SCRAM is offered
Kim Alvefur <zash@zash.se>
parents:
356
diff
changeset
|
97 | if sock and sock.getfinished then |
a8f6fd6a70ed
plugins.sasl: Alter mechanism loading and pass name of loaded mechanism. Fixes attempting SCRAM-PLUS when only SCRAM is offered
Kim Alvefur <zash@zash.se>
parents:
356
diff
changeset
|
98 | return scram, 100; |
a8f6fd6a70ed
plugins.sasl: Alter mechanism loading and pass name of loaded mechanism. Fixes attempting SCRAM-PLUS when only SCRAM is offered
Kim Alvefur <zash@zash.se>
parents:
356
diff
changeset
|
99 | end |
356
f95e797895ee
SCRAM: Add channel binding support (SCRAM-SHA-1-PLUS)
Kim Alvefur <zash@zash.se>
parents:
355
diff
changeset
|
100 | end |
355 | 101 | end |
102 | end | |
358
a8f6fd6a70ed
plugins.sasl: Alter mechanism loading and pass name of loaded mechanism. Fixes attempting SCRAM-PLUS when only SCRAM is offered
Kim Alvefur <zash@zash.se>
parents:
356
diff
changeset
|
103 |