aboutsummaryrefslogtreecommitdiffstats
path: root/conversations/src/main/java/eu/siacs/conversations/utils/CryptoHelper.java
blob: 47595c6e3a0ea48d8e951f109fd995c35dc623aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package eu.siacs.conversations.utils;

import java.math.BigInteger;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import eu.siacs.conversations.entities.Account;
import android.util.Base64;

public class CryptoHelper {
	public static final String FILETRANSFER = "?FILETRANSFERv1:";
	final protected static char[] hexArray = "0123456789abcdef".toCharArray();
	final protected static char[] vowels = "aeiou".toCharArray();
	final protected static char[] consonants = "bcdfghjklmnpqrstvwxyz"
			.toCharArray();

	public static String bytesToHex(byte[] bytes) {
		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);
	}

	public static byte[] hexToBytes(String hexString) {
		int len = hexString.length();
		byte[] array = new byte[len / 2];
		for (int i = 0; i < len; i += 2) {
			array[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
					.digit(hexString.charAt(i + 1), 16));
		}
		return array;
	}

	public static String saslPlain(String username, String password) {
		String sasl = '\u0000' + username + '\u0000' + password;
		return Base64.encodeToString(sasl.getBytes(Charset.defaultCharset()),
				Base64.NO_WRAP);
	}

	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;
	}

	public static String saslDigestMd5(Account account, String challenge,
			SecureRandom random) {
		try {
			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 + ",charset=utf-8";
			return Base64.encodeToString(
					saslString.getBytes(Charset.defaultCharset()),
					Base64.NO_WRAP);
		} catch (NoSuchAlgorithmException e) {
			return null;
		}
	}

	public static String randomMucName(SecureRandom random) {
		return randomWord(3, random) + "." + randomWord(7, random);
	}

	protected static String randomWord(int lenght, SecureRandom random) {
		StringBuilder builder = new StringBuilder(lenght);
		for (int i = 0; i < lenght; ++i) {
			if (i % 2 == 0) {
				builder.append(consonants[random.nextInt(consonants.length)]);
			} else {
				builder.append(vowels[random.nextInt(vowels.length)]);
			}
		}
		return builder.toString();
	}
}