| 1 |
namespace Xmpp.Sasl { |
| 2 |
private const string NS_URI = "urn:ietf:params:xml:ns:xmpp-sasl"; |
| 3 |
|
| 4 |
public class Flag : XmppStreamFlag { |
| 5 |
public static FlagIdentity<Flag> IDENTITY = new FlagIdentity<Flag>(NS_URI, "sasl"); |
| 6 |
public string mechanism; |
| 7 |
public string name; |
| 8 |
public string password; |
| 9 |
public string client_nonce; |
| 10 |
public uint8[] server_signature; |
| 11 |
public bool finished = false; |
| 12 |
|
| 13 |
public override string get_ns() { return NS_URI; } |
| 14 |
public override string get_id() { return IDENTITY.id; } |
| 15 |
} |
| 16 |
|
| 17 |
namespace Mechanism { |
| 18 |
public const string PLAIN = "PLAIN"; |
| 19 |
public const string SCRAM_SHA_1 = "SCRAM-SHA-1"; |
| 20 |
public const string SCRAM_SHA_1_PLUS = "SCRAM-SHA-1-PLUS"; |
| 21 |
} |
| 22 |
|
| 23 |
public class Module : XmppStreamNegotiationModule { |
| 24 |
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "sasl"); |
| 25 |
|
| 26 |
public string name { get; set; } |
| 27 |
public string password { get; set; } |
| 28 |
public bool use_full_name = false; |
| 29 |
|
| 30 |
public signal void received_auth_failure(XmppStream stream, StanzaNode node); |
| 31 |
|
| 32 |
public Module(string name, string password) { |
| 33 |
this.name = name; |
| 34 |
this.password = password; |
| 35 |
} |
| 36 |
|
| 37 |
public override void attach(XmppStream stream) { |
| 38 |
stream.received_features_node.connect(this.received_features_node); |
| 39 |
stream.received_nonza.connect(this.received_nonza); |
| 40 |
} |
| 41 |
|
| 42 |
public override void detach(XmppStream stream) { |
| 43 |
stream.received_features_node.disconnect(this.received_features_node); |
| 44 |
stream.received_nonza.disconnect(this.received_nonza); |
| 45 |
} |
| 46 |
|
| 47 |
private static size_t SHA1_SIZE = 20; |
| 48 |
|
| 49 |
private static uint8[] sha1(uint8[] data) { |
| 50 |
Checksum checksum = new Checksum(ChecksumType.SHA1); |
| 51 |
checksum.update(data, data.length); |
| 52 |
uint8[] res = new uint8[SHA1_SIZE]; |
| 53 |
checksum.get_digest(res, ref SHA1_SIZE); |
| 54 |
return res; |
| 55 |
} |
| 56 |
|
| 57 |
private static uint8[] hmac_sha1(uint8[] key, uint8[] data) { |
| 58 |
Hmac hmac = new Hmac(ChecksumType.SHA1, key); |
| 59 |
hmac.update(data); |
| 60 |
uint8[] res = new uint8[SHA1_SIZE]; |
| 61 |
hmac.get_digest(res, ref SHA1_SIZE); |
| 62 |
return res; |
| 63 |
} |
| 64 |
|
| 65 |
private static uint8[] pbkdf2_sha1(string password, uint8[] salt, uint iterations) { |
| 66 |
uint8[] res = new uint8[SHA1_SIZE]; |
| 67 |
uint8[] last = new uint8[salt.length + 4]; |
| 68 |
for(int i = 0; i < salt.length; i++) { |
| 69 |
last[i] = salt[i]; |
| 70 |
} |
| 71 |
last[salt.length + 3] = 1; |
| 72 |
for(int i = 0; i < iterations; i++) { |
| 73 |
last = hmac_sha1((uint8[]) password.to_utf8(), last); |
| 74 |
xor_inplace(res, last); |
| 75 |
} |
| 76 |
return res; |
| 77 |
} |
| 78 |
|
| 79 |
private static void xor_inplace(uint8[] mix, uint8[] a2) { |
| 80 |
for(int i = 0; i < mix.length; i++) { |
| 81 |
mix[i] = mix[i] ^ a2[i]; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
private static uint8[] xor(uint8[] a1, uint8[] a2) { |
| 86 |
uint8[] mix = new uint8[a1.length]; |
| 87 |
for(int i = 0; i < a1.length; i++) { |
| 88 |
mix[i] = a1[i] ^ a2[i]; |
| 89 |
} |
| 90 |
return mix; |
| 91 |
} |
| 92 |
|
| 93 |
public void received_nonza(XmppStream stream, StanzaNode node) { |
| 94 |
if (node.ns_uri == NS_URI) { |
| 95 |
if (node.name == "success") { |
| 96 |
Flag flag = stream.get_flag(Flag.IDENTITY); |
| 97 |
if (flag.mechanism == Mechanism.SCRAM_SHA_1) { |
| 98 |
string confirm = (string) Base64.decode(node.get_string_content()); |
| 99 |
uint8[] server_signature = null; |
| 100 |
foreach(string c in confirm.split(",")) { |
| 101 |
string[] split = c.split("=", 2); |
| 102 |
if (split.length != 2) continue; |
| 103 |
switch(split[0]) { |
| 104 |
case "v": server_signature = Base64.decode(split[1]); break; |
| 105 |
} |
| 106 |
} |
| 107 |
if (server_signature == null) return; |
| 108 |
if (server_signature.length != flag.server_signature.length) return; |
| 109 |
for(int i = 0; i < server_signature.length; i++) { |
| 110 |
if (server_signature[i] != flag.server_signature[i]) return; |
| 111 |
} |
| 112 |
} |
| 113 |
stream.require_setup(); |
| 114 |
flag.password = null; // Remove password from memory |
| 115 |
flag.finished = true; |
| 116 |
} else if (node.name == "failure") { |
| 117 |
stream.remove_flag(stream.get_flag(Flag.IDENTITY)); |
| 118 |
received_auth_failure(stream, node); |
| 119 |
} else if (node.name == "challenge" && stream.has_flag(Flag.IDENTITY)) { |
| 120 |
Flag flag = stream.get_flag(Flag.IDENTITY); |
| 121 |
if (flag.mechanism == Mechanism.SCRAM_SHA_1) { |
| 122 |
string challenge = (string) Base64.decode(node.get_string_content()); |
| 123 |
string? server_nonce = null; |
| 124 |
uint8[] salt = null; |
| 125 |
uint iterations = 0; |
| 126 |
foreach(string c in challenge.split(",")) { |
| 127 |
string[] split = c.split("=", 2); |
| 128 |
if (split.length != 2) continue; |
| 129 |
switch(split[0]) { |
| 130 |
case "r": server_nonce = split[1]; break; |
| 131 |
case "s": salt = Base64.decode(split[1]); break; |
| 132 |
case "i": iterations = int.parse(split[1]); break; |
| 133 |
} |
| 134 |
} |
| 135 |
if (server_nonce == null || salt == null || iterations == 0) return; |
| 136 |
if (!server_nonce.has_prefix(flag.client_nonce)) return; |
| 137 |
string client_final_message_bare = @"c=biws,r=$server_nonce"; |
| 138 |
uint8[] salted_password = pbkdf2_sha1(flag.password, salt, iterations); |
| 139 |
uint8[] client_key = hmac_sha1(salted_password, (uint8[]) "Client Key".to_utf8()); |
| 140 |
uint8[] stored_key = sha1(client_key); |
| 141 |
string auth_message = @"n=$(flag.name),r=$(flag.client_nonce),$challenge,$client_final_message_bare"; |
| 142 |
uint8[] client_signature = hmac_sha1(stored_key, (uint8[]) auth_message.to_utf8()); |
| 143 |
uint8[] client_proof = xor(client_key, client_signature); |
| 144 |
uint8[] server_key = hmac_sha1(salted_password, (uint8[]) "Server Key".to_utf8()); |
| 145 |
flag.server_signature = hmac_sha1(server_key, (uint8[]) auth_message.to_utf8()); |
| 146 |
string client_final_message = @"$client_final_message_bare,p=$(Base64.encode(client_proof))"; |
| 147 |
stream.write(new StanzaNode.build("response", NS_URI).add_self_xmlns() |
| 148 |
.put_node(new StanzaNode.text(Base64.encode((uchar[]) (client_final_message).to_utf8())))); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
public void received_features_node(XmppStream stream) { |
| 155 |
if (stream.has_flag(Flag.IDENTITY)) return; |
| 156 |
if (stream.is_setup_needed()) return; |
| 157 |
|
| 158 |
var mechanisms = stream.features.get_subnode("mechanisms", NS_URI); |
| 159 |
string[] supported_mechanisms = {}; |
| 160 |
foreach (var mechanism in mechanisms.sub_nodes) { |
| 161 |
if (mechanism.name != "mechanism" || mechanism.ns_uri != NS_URI) continue; |
| 162 |
supported_mechanisms += mechanism.get_string_content(); |
| 163 |
} |
| 164 |
if (!name.contains("@")) { |
| 165 |
name = "%s@%s".printf(name, stream.remote_name.to_string()); |
| 166 |
} |
| 167 |
if (!use_full_name && name.contains("@")) { |
| 168 |
var split = name.split("@"); |
| 169 |
if (split[1] == stream.remote_name.to_string()) { |
| 170 |
name = split[0]; |
| 171 |
} else { |
| 172 |
use_full_name = true; |
| 173 |
} |
| 174 |
} |
| 175 |
string name = this.name; |
| 176 |
if (!use_full_name && name.contains("@")) { |
| 177 |
var split = name.split("@"); |
| 178 |
if (split[1] == stream.remote_name.to_string()) { |
| 179 |
name = split[0]; |
| 180 |
} |
| 181 |
} |
| 182 |
if (Mechanism.SCRAM_SHA_1 in supported_mechanisms) { |
| 183 |
string normalized_password = password.normalize(-1, NormalizeMode.NFKC); |
| 184 |
string client_nonce = Random.next_int().to_string("%.8x") + Random.next_int().to_string("%.8x") + Random.next_int().to_string("%.8x"); |
| 185 |
string initial_message = @"n=$name,r=$client_nonce"; |
| 186 |
stream.write(new StanzaNode.build("auth", NS_URI).add_self_xmlns() |
| 187 |
.put_attribute("mechanism", Mechanism.SCRAM_SHA_1) |
| 188 |
.put_node(new StanzaNode.text(Base64.encode((uchar[]) ("n,,"+initial_message).to_utf8())))); |
| 189 |
var flag = new Flag(); |
| 190 |
flag.mechanism = Mechanism.SCRAM_SHA_1; |
| 191 |
flag.name = name; |
| 192 |
flag.password = normalized_password; |
| 193 |
flag.client_nonce = client_nonce; |
| 194 |
stream.add_flag(flag); |
| 195 |
} else if (Mechanism.PLAIN in supported_mechanisms) { |
| 196 |
stream.write(new StanzaNode.build("auth", NS_URI).add_self_xmlns() |
| 197 |
.put_attribute("mechanism", Mechanism.PLAIN) |
| 198 |
.put_node(new StanzaNode.text(Base64.encode(get_plain_bytes(name, password))))); |
| 199 |
var flag = new Flag(); |
| 200 |
flag.mechanism = Mechanism.PLAIN; |
| 201 |
flag.name = name; |
| 202 |
stream.add_flag(flag); |
| 203 |
} else { |
| 204 |
stderr.printf("No supported mechanism provided by server at %s\n", stream.remote_name.to_string()); |
| 205 |
return; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
private static uchar[] get_plain_bytes(string name_s, string password_s) { |
| 210 |
var name = name_s.to_utf8(); |
| 211 |
var password = password_s.to_utf8(); |
| 212 |
uchar[] res = new uchar[name.length + password.length + 2]; |
| 213 |
res[0] = 0; |
| 214 |
res[name.length + 1] = 0; |
| 215 |
for(int i = 0; i < name.length; i++) { res[i + 1] = (uchar) name[i]; } |
| 216 |
for(int i = 0; i < password.length; i++) { res[i + name.length + 2] = (uchar) password[i]; } |
| 217 |
return res; |
| 218 |
} |
| 219 |
|
| 220 |
public override bool mandatory_outstanding(XmppStream stream) { |
| 221 |
return !stream.has_flag(Flag.IDENTITY) || !stream.get_flag(Flag.IDENTITY).finished; |
| 222 |
} |
| 223 |
|
| 224 |
public override bool negotiation_active(XmppStream stream) { |
| 225 |
return stream.has_flag(Flag.IDENTITY) && !stream.get_flag(Flag.IDENTITY).finished; |
| 226 |
} |
| 227 |
|
| 228 |
public override string get_ns() { return NS_URI; } |
| 229 |
public override string get_id() { return IDENTITY.id; } |
| 230 |
} |
| 231 |
} |