2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.utils;
|
|
|
|
|
|
|
|
import java.security.SecureRandom;
|
2014-11-12 20:35:44 +00:00
|
|
|
import java.text.Normalizer;
|
2015-01-14 17:20:02 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
2015-03-07 14:36:31 +00:00
|
|
|
import java.util.Iterator;
|
2015-01-14 17:20:02 +00:00
|
|
|
import java.util.LinkedHashSet;
|
2015-02-02 13:33:55 +00:00
|
|
|
import java.util.List;
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2015-01-14 17:20:02 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
|
|
|
|
|
|
|
public final class CryptoHelper {
|
2014-10-22 16:38:44 +00:00
|
|
|
public static final String FILETRANSFER = "?FILETRANSFERv1:";
|
2015-01-14 17:20:02 +00:00
|
|
|
private final static char[] hexArray = "0123456789abcdef".toCharArray();
|
|
|
|
private final static char[] vowels = "aeiou".toCharArray();
|
|
|
|
private final static char[] consonants = "bcdfghjklmnpqrstvwxyz".toCharArray();
|
2014-11-12 20:35:44 +00:00
|
|
|
final public static byte[] ONE = new byte[] { 0, 0, 0, 1 };
|
2014-10-22 16:38:44 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-11-15 17:57:36 +00:00
|
|
|
public static String hexToString(final String hexString) {
|
|
|
|
return new String(hexToBytes(hexString));
|
|
|
|
}
|
|
|
|
|
2014-11-12 15:15:38 +00:00
|
|
|
public static byte[] concatenateByteArrays(byte[] a, byte[] b) {
|
2014-10-22 16:38:44 +00:00
|
|
|
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 randomMucName(SecureRandom random) {
|
|
|
|
return randomWord(3, random) + "." + randomWord(7, random);
|
|
|
|
}
|
|
|
|
|
2015-01-14 17:20:02 +00:00
|
|
|
private static String randomWord(int lenght, SecureRandom random) {
|
2014-10-22 16:38:44 +00:00
|
|
|
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();
|
|
|
|
}
|
2014-11-12 20:35:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Escapes usernames or passwords for SASL.
|
|
|
|
*/
|
|
|
|
public static String saslEscape(final String s) {
|
|
|
|
final StringBuilder sb = new StringBuilder((int) (s.length() * 1.1));
|
|
|
|
for (int i = 0; i < s.length(); i++) {
|
|
|
|
char c = s.charAt(i);
|
|
|
|
switch (c) {
|
|
|
|
case ',':
|
|
|
|
sb.append("=2C");
|
|
|
|
break;
|
|
|
|
case '=':
|
|
|
|
sb.append("=3D");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sb.append(c);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String saslPrep(final String s) {
|
2014-11-16 02:09:51 +00:00
|
|
|
return Normalizer.normalize(s, Normalizer.Form.NFKC);
|
2014-11-12 20:35:44 +00:00
|
|
|
}
|
2014-11-15 23:20:20 +00:00
|
|
|
|
|
|
|
public static String prettifyFingerprint(String fingerprint) {
|
|
|
|
StringBuilder builder = new StringBuilder(fingerprint);
|
|
|
|
builder.insert(8, " ");
|
|
|
|
builder.insert(17, " ");
|
|
|
|
builder.insert(26, " ");
|
|
|
|
builder.insert(35, " ");
|
|
|
|
return builder.toString();
|
|
|
|
}
|
2015-01-14 17:20:02 +00:00
|
|
|
|
2015-02-02 13:33:55 +00:00
|
|
|
public static String[] getOrderedCipherSuites(final String[] platformSupportedCipherSuites) {
|
|
|
|
final Collection<String> cipherSuites = new LinkedHashSet<>(Arrays.asList(Config.ENABLED_CIPHERS));
|
|
|
|
final List<String> platformCiphers = Arrays.asList(platformSupportedCipherSuites);
|
|
|
|
cipherSuites.retainAll(platformCiphers);
|
|
|
|
cipherSuites.addAll(platformCiphers);
|
2015-03-07 14:36:31 +00:00
|
|
|
filterWeakCipherSuites(cipherSuites);
|
2015-02-02 13:33:55 +00:00
|
|
|
return cipherSuites.toArray(new String[cipherSuites.size()]);
|
2015-01-14 17:20:02 +00:00
|
|
|
}
|
2015-03-07 14:36:31 +00:00
|
|
|
|
|
|
|
private static void filterWeakCipherSuites(final Collection<String> cipherSuites) {
|
|
|
|
final Iterator<String> it = cipherSuites.iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
String cipherName = it.next();
|
|
|
|
// remove all ciphers with no or very weak encryption or no authentication
|
|
|
|
for (String weakCipherPattern : Config.WEAK_CIPHER_PATTERNS) {
|
|
|
|
if (cipherName.contains(weakCipherPattern)) {
|
|
|
|
it.remove();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|