2014-03-07 14:24:33 +01:00
|
|
|
package eu.siacs.conversations.utils;
|
|
|
|
|
2014-03-31 15:22:27 +02:00
|
|
|
import java.math.BigInteger;
|
2014-03-28 14:48:11 +01:00
|
|
|
import java.nio.charset.Charset;
|
2014-03-31 15:22:27 +02:00
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2014-03-15 15:13:35 +01:00
|
|
|
import java.security.SecureRandom;
|
|
|
|
import java.util.Random;
|
|
|
|
|
2014-03-31 15:22:27 +02:00
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
|
2014-03-09 12:58:29 +01:00
|
|
|
import android.util.Base64;
|
2014-03-31 15:22:27 +02:00
|
|
|
import android.util.Log;
|
2014-03-09 12:58:29 +01:00
|
|
|
|
2014-03-07 14:24:33 +01:00
|
|
|
public class CryptoHelper {
|
|
|
|
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
|
2014-03-15 15:13:35 +01:00
|
|
|
final protected static char[] vowels = "aeiou".toCharArray();
|
2014-03-31 15:22:27 +02:00
|
|
|
final protected static char[] consonants = "bcdfghjklmnpqrstvwxyz"
|
|
|
|
.toCharArray();
|
|
|
|
|
2014-03-07 14:24:33 +01:00
|
|
|
public static String bytesToHex(byte[] bytes) {
|
2014-03-31 15:22:27 +02:00
|
|
|
char[] hexChars = new char[bytes.length * 2];
|
|
|
|
for (int j = 0; j < bytes.length; j++) {
|
|
|
|
int v = bytes[j] & 0xFF;
|
|
|
|
hexChars[j * 2] = hexArray[v >>> 4];
|
|
|
|
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
|
|
|
|
}
|
|
|
|
return new String(hexChars).toLowerCase();
|
2014-03-07 14:24:33 +01:00
|
|
|
}
|
2014-03-31 15:22:27 +02:00
|
|
|
|
2014-03-09 12:58:29 +01:00
|
|
|
public static String saslPlain(String username, String password) {
|
2014-03-31 15:22:27 +02:00
|
|
|
String sasl = '\u0000' + username + '\u0000' + password;
|
|
|
|
return Base64.encodeToString(sasl.getBytes(Charset.defaultCharset()),
|
|
|
|
Base64.NO_WRAP);
|
2014-03-09 12:58:29 +01:00
|
|
|
}
|
2014-03-31 15:22:27 +02:00
|
|
|
|
|
|
|
private static byte[] concatenateByteArrays(byte[] a, byte[] b) {
|
|
|
|
byte[] result = new byte[a.length + b.length];
|
|
|
|
System.arraycopy(a, 0, result, 0, a.length);
|
|
|
|
System.arraycopy(b, 0, result, a.length, b.length);
|
|
|
|
return result;
|
|
|
|
}
|
2014-03-15 15:13:35 +01:00
|
|
|
|
2014-03-31 15:22:27 +02:00
|
|
|
public static String saslDigestMd5(Account account, String challenge) {
|
|
|
|
try {
|
|
|
|
Random random = new SecureRandom();
|
|
|
|
String[] challengeParts = new String(Base64.decode(challenge,
|
|
|
|
Base64.DEFAULT)).split(",");
|
|
|
|
String nonce = "";
|
|
|
|
for (int i = 0; i < challengeParts.length; ++i) {
|
|
|
|
String[] parts = challengeParts[i].split("=");
|
|
|
|
if (parts[0].equals("nonce")) {
|
|
|
|
nonce = parts[1].replace("\"", "");
|
|
|
|
} else if (parts[0].equals("rspauth")) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
String digestUri = "xmpp/"+account.getServer();
|
|
|
|
String nonceCount = "00000001";
|
|
|
|
String x = account.getUsername() + ":" + account.getServer() + ":"
|
|
|
|
+ account.getPassword();
|
|
|
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
|
|
|
byte[] y = md
|
|
|
|
.digest(x.getBytes(Charset.defaultCharset()));
|
|
|
|
String cNonce = new BigInteger(100, random).toString(32);
|
|
|
|
byte[] a1 = concatenateByteArrays(y,(":"+nonce+":"+cNonce).getBytes(Charset.defaultCharset()));
|
|
|
|
String a2 = "AUTHENTICATE:"+digestUri;
|
|
|
|
String ha1 = bytesToHex(md.digest(a1));
|
|
|
|
String ha2 = bytesToHex(md.digest(a2.getBytes(Charset
|
|
|
|
.defaultCharset())));
|
|
|
|
String kd = ha1 + ":" + nonce + ":"+nonceCount+":" + cNonce + ":auth:"
|
|
|
|
+ ha2;
|
|
|
|
String response = bytesToHex(md.digest(kd.getBytes(Charset
|
|
|
|
.defaultCharset())));
|
|
|
|
String saslString = "username=\"" + account.getUsername()
|
|
|
|
+ "\",realm=\"" + account.getServer() + "\",nonce=\""
|
|
|
|
+ nonce + "\",cnonce=\"" + cNonce
|
|
|
|
+ "\",nc="+nonceCount+",qop=auth,digest-uri=\""+digestUri+"\",response=" + response
|
2014-04-01 11:38:34 +02:00
|
|
|
+ ",charset=utf-8";
|
2014-03-31 15:22:27 +02:00
|
|
|
return Base64.encodeToString(
|
|
|
|
saslString.getBytes(Charset.defaultCharset()),
|
|
|
|
Base64.NO_WRAP);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-15 15:13:35 +01:00
|
|
|
public static String randomMucName() {
|
|
|
|
Random random = new SecureRandom();
|
2014-03-31 15:22:27 +02:00
|
|
|
return randomWord(3, random) + "." + randomWord(7, random);
|
2014-03-15 15:13:35 +01:00
|
|
|
}
|
2014-03-31 15:22:27 +02:00
|
|
|
|
|
|
|
protected static String randomWord(int lenght, Random random) {
|
2014-03-15 15:13:35 +01:00
|
|
|
StringBuilder builder = new StringBuilder(lenght);
|
2014-03-31 15:22:27 +02:00
|
|
|
for (int i = 0; i < lenght; ++i) {
|
2014-03-15 15:13:35 +01:00
|
|
|
if (i % 2 == 0) {
|
|
|
|
builder.append(consonants[random.nextInt(consonants.length)]);
|
|
|
|
} else {
|
|
|
|
builder.append(vowels[random.nextInt(vowels.length)]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return builder.toString();
|
|
|
|
}
|
2014-03-07 14:24:33 +01:00
|
|
|
}
|