1package eu.siacs.conversations.utils;
2
3import java.math.BigInteger;
4import java.nio.charset.Charset;
5import java.security.MessageDigest;
6import java.security.NoSuchAlgorithmException;
7import java.security.SecureRandom;
8import java.util.Arrays;
9
10import eu.siacs.conversations.entities.Account;
11import android.util.Base64;
12
13public class CryptoHelper {
14 public static final String FILETRANSFER = "?FILETRANSFERv1:";
15 final protected static char[] hexArray = "0123456789abcdef".toCharArray();
16 final protected static char[] vowels = "aeiou".toCharArray();
17 final protected static char[] consonants = "bcdfghjklmnpqrstvwxyz"
18 .toCharArray();
19
20 public static String bytesToHex(byte[] bytes) {
21 char[] hexChars = new char[bytes.length * 2];
22 for (int j = 0; j < bytes.length; j++) {
23 int v = bytes[j] & 0xFF;
24 hexChars[j * 2] = hexArray[v >>> 4];
25 hexChars[j * 2 + 1] = hexArray[v & 0x0F];
26 }
27 return new String(hexChars);
28 }
29
30 public static byte[] hexToBytes(String hexString) {
31 byte[] array = new BigInteger(hexString, 16).toByteArray();
32 if (array[0] == 0) {
33 array = Arrays.copyOfRange(array, 1, array.length);
34 }
35 return array;
36 }
37
38 public static String saslPlain(String username, String password) {
39 String sasl = '\u0000' + username + '\u0000' + password;
40 return Base64.encodeToString(sasl.getBytes(Charset.defaultCharset()),
41 Base64.NO_WRAP);
42 }
43
44 private static byte[] concatenateByteArrays(byte[] a, byte[] b) {
45 byte[] result = new byte[a.length + b.length];
46 System.arraycopy(a, 0, result, 0, a.length);
47 System.arraycopy(b, 0, result, a.length, b.length);
48 return result;
49 }
50
51 public static String saslDigestMd5(Account account, String challenge,
52 SecureRandom random) {
53 try {
54 String[] challengeParts = new String(Base64.decode(challenge,
55 Base64.DEFAULT)).split(",");
56 String nonce = "";
57 for (int i = 0; i < challengeParts.length; ++i) {
58 String[] parts = challengeParts[i].split("=");
59 if (parts[0].equals("nonce")) {
60 nonce = parts[1].replace("\"", "");
61 } else if (parts[0].equals("rspauth")) {
62 return null;
63 }
64 }
65 String digestUri = "xmpp/" + account.getServer();
66 String nonceCount = "00000001";
67 String x = account.getUsername() + ":" + account.getServer() + ":"
68 + account.getPassword();
69 MessageDigest md = MessageDigest.getInstance("MD5");
70 byte[] y = md.digest(x.getBytes(Charset.defaultCharset()));
71 String cNonce = new BigInteger(100, random).toString(32);
72 byte[] a1 = concatenateByteArrays(y,
73 (":" + nonce + ":" + cNonce).getBytes(Charset
74 .defaultCharset()));
75 String a2 = "AUTHENTICATE:" + digestUri;
76 String ha1 = bytesToHex(md.digest(a1));
77 String ha2 = bytesToHex(md.digest(a2.getBytes(Charset
78 .defaultCharset())));
79 String kd = ha1 + ":" + nonce + ":" + nonceCount + ":" + cNonce
80 + ":auth:" + ha2;
81 String response = bytesToHex(md.digest(kd.getBytes(Charset
82 .defaultCharset())));
83 String saslString = "username=\"" + account.getUsername()
84 + "\",realm=\"" + account.getServer() + "\",nonce=\""
85 + nonce + "\",cnonce=\"" + cNonce + "\",nc=" + nonceCount
86 + ",qop=auth,digest-uri=\"" + digestUri + "\",response="
87 + response + ",charset=utf-8";
88 return Base64.encodeToString(
89 saslString.getBytes(Charset.defaultCharset()),
90 Base64.NO_WRAP);
91 } catch (NoSuchAlgorithmException e) {
92 return null;
93 }
94 }
95
96 public static String randomMucName(SecureRandom random) {
97 return randomWord(3, random) + "." + randomWord(7, random);
98 }
99
100 protected static String randomWord(int lenght, SecureRandom random) {
101 StringBuilder builder = new StringBuilder(lenght);
102 for (int i = 0; i < lenght; ++i) {
103 if (i % 2 == 0) {
104 builder.append(consonants[random.nextInt(consonants.length)]);
105 } else {
106 builder.append(vowels[random.nextInt(vowels.length)]);
107 }
108 }
109 return builder.toString();
110 }
111}