2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.utils;
|
|
|
|
|
2015-12-23 18:18:53 +00:00
|
|
|
import android.os.Bundle;
|
2017-07-30 09:39:47 +00:00
|
|
|
import android.util.Base64;
|
2015-10-11 13:48:58 +00:00
|
|
|
import android.util.Pair;
|
|
|
|
|
|
|
|
import org.bouncycastle.asn1.x500.X500Name;
|
|
|
|
import org.bouncycastle.asn1.x500.style.BCStyle;
|
|
|
|
import org.bouncycastle.asn1.x500.style.IETFUtils;
|
|
|
|
import org.bouncycastle.cert.jcajce.JcaX509CertificateHolder;
|
|
|
|
|
2017-04-05 16:46:56 +00:00
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
2015-12-23 18:18:53 +00:00
|
|
|
import java.security.MessageDigest;
|
2016-05-28 09:04:18 +00:00
|
|
|
import java.security.NoSuchAlgorithmException;
|
2017-07-30 09:39:47 +00:00
|
|
|
import java.security.SecureRandom;
|
2015-10-11 13:48:58 +00:00
|
|
|
import java.security.cert.CertificateEncodingException;
|
2015-10-16 21:48:42 +00:00
|
|
|
import java.security.cert.CertificateParsingException;
|
2015-10-11 13:48:58 +00:00
|
|
|
import java.security.cert.X509Certificate;
|
2014-11-12 20:35:44 +00:00
|
|
|
import java.text.Normalizer;
|
2015-10-16 21:48:42 +00:00
|
|
|
import java.util.ArrayList;
|
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;
|
2015-12-27 17:37:12 +00:00
|
|
|
import java.util.Locale;
|
2016-07-28 20:58:37 +00:00
|
|
|
import java.util.regex.Pattern;
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2015-01-14 17:20:02 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2015-10-28 21:40:09 +00:00
|
|
|
import eu.siacs.conversations.R;
|
2016-10-06 16:09:55 +00:00
|
|
|
import eu.siacs.conversations.entities.Account;
|
2015-10-28 21:40:09 +00:00
|
|
|
import eu.siacs.conversations.entities.Message;
|
2017-04-16 06:44:21 +00:00
|
|
|
import eu.siacs.conversations.http.AesGcmURLStreamHandler;
|
2018-03-05 17:30:40 +00:00
|
|
|
import rocks.xmpp.addr.Jid;
|
2015-01-14 17:20:02 +00:00
|
|
|
|
|
|
|
public final class CryptoHelper {
|
|
|
|
private final static char[] hexArray = "0123456789abcdef".toCharArray();
|
2016-07-28 20:58:37 +00:00
|
|
|
|
|
|
|
public static final Pattern UUID_PATTERN = Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}");
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-07-30 09:39:47 +00:00
|
|
|
public static String random(int length, SecureRandom random) {
|
|
|
|
final byte[] bytes = new byte[length];
|
|
|
|
random.nextBytes(bytes);
|
2018-02-24 19:47:02 +00:00
|
|
|
return Base64.encodeToString(bytes,Base64.NO_PADDING|Base64.NO_WRAP|Base64.URL_SAFE);
|
2017-07-30 09:39:47 +00:00
|
|
|
}
|
|
|
|
|
2014-11-15 23:20:20 +00:00
|
|
|
public static String prettifyFingerprint(String fingerprint) {
|
2015-07-11 19:23:58 +00:00
|
|
|
if (fingerprint==null) {
|
|
|
|
return "";
|
|
|
|
} else if (fingerprint.length() < 40) {
|
2015-04-25 12:42:32 +00:00
|
|
|
return fingerprint;
|
|
|
|
}
|
2017-06-18 14:35:30 +00:00
|
|
|
StringBuilder builder = new StringBuilder(fingerprint);
|
2015-07-07 17:28:35 +00:00
|
|
|
for(int i=8;i<builder.length();i+=9) {
|
|
|
|
builder.insert(i, ' ');
|
|
|
|
}
|
2014-11-15 23:20:20 +00:00
|
|
|
return builder.toString();
|
|
|
|
}
|
2015-01-14 17:20:02 +00:00
|
|
|
|
2015-12-23 18:18:53 +00:00
|
|
|
public static String prettifyFingerprintCert(String fingerprint) {
|
|
|
|
StringBuilder builder = new StringBuilder(fingerprint);
|
|
|
|
for(int i=2;i < builder.length(); i+=3) {
|
|
|
|
builder.insert(i,':');
|
|
|
|
}
|
|
|
|
return builder.toString();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-11 13:48:58 +00:00
|
|
|
|
2018-03-05 17:30:40 +00:00
|
|
|
public static Pair<Jid,String> extractJidAndName(X509Certificate certificate) throws CertificateEncodingException, IllegalArgumentException, CertificateParsingException {
|
2015-10-16 21:48:42 +00:00
|
|
|
Collection<List<?>> alternativeNames = certificate.getSubjectAlternativeNames();
|
|
|
|
List<String> emails = new ArrayList<>();
|
|
|
|
if (alternativeNames != null) {
|
|
|
|
for(List<?> san : alternativeNames) {
|
|
|
|
Integer type = (Integer) san.get(0);
|
|
|
|
if (type == 1) {
|
|
|
|
emails.add((String) san.get(1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-11 13:48:58 +00:00
|
|
|
X500Name x500name = new JcaX509CertificateHolder(certificate).getSubject();
|
2017-08-07 14:02:48 +00:00
|
|
|
if (emails.size() == 0 && x500name.getRDNs(BCStyle.EmailAddress).length > 0) {
|
2015-10-16 21:48:42 +00:00
|
|
|
emails.add(IETFUtils.valueToString(x500name.getRDNs(BCStyle.EmailAddress)[0].getFirst().getValue()));
|
|
|
|
}
|
2017-08-07 14:02:48 +00:00
|
|
|
String name = x500name.getRDNs(BCStyle.CN).length > 0 ? IETFUtils.valueToString(x500name.getRDNs(BCStyle.CN)[0].getFirst().getValue()) : null;
|
2015-10-16 21:48:42 +00:00
|
|
|
if (emails.size() >= 1) {
|
2018-03-05 17:30:40 +00:00
|
|
|
return new Pair<>(Jid.of(emails.get(0)), name);
|
2017-08-07 14:02:48 +00:00
|
|
|
} else if (name != null){
|
|
|
|
try {
|
2018-03-05 17:30:40 +00:00
|
|
|
Jid jid = Jid.of(name);
|
|
|
|
if (jid.isBareJid() && jid.getLocal() != null) {
|
2017-08-07 14:02:48 +00:00
|
|
|
return new Pair<>(jid,null);
|
|
|
|
}
|
2018-03-05 17:30:40 +00:00
|
|
|
} catch (IllegalArgumentException e) {
|
2017-08-07 14:02:48 +00:00
|
|
|
return null;
|
|
|
|
}
|
2015-10-16 21:48:42 +00:00
|
|
|
}
|
2017-08-07 14:02:48 +00:00
|
|
|
return null;
|
2015-10-11 13:48:58 +00:00
|
|
|
}
|
2015-10-28 21:40:09 +00:00
|
|
|
|
2015-12-23 18:18:53 +00:00
|
|
|
public static Bundle extractCertificateInformation(X509Certificate certificate) {
|
|
|
|
Bundle information = new Bundle();
|
|
|
|
try {
|
|
|
|
JcaX509CertificateHolder holder = new JcaX509CertificateHolder(certificate);
|
|
|
|
X500Name subject = holder.getSubject();
|
|
|
|
try {
|
|
|
|
information.putString("subject_cn", subject.getRDNs(BCStyle.CN)[0].getFirst().getValue().toString());
|
|
|
|
} catch (Exception e) {
|
|
|
|
//ignored
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
information.putString("subject_o",subject.getRDNs(BCStyle.O)[0].getFirst().getValue().toString());
|
|
|
|
} catch (Exception e) {
|
|
|
|
//ignored
|
|
|
|
}
|
|
|
|
|
|
|
|
X500Name issuer = holder.getIssuer();
|
|
|
|
try {
|
|
|
|
information.putString("issuer_cn", issuer.getRDNs(BCStyle.CN)[0].getFirst().getValue().toString());
|
|
|
|
} catch (Exception e) {
|
|
|
|
//ignored
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
information.putString("issuer_o", issuer.getRDNs(BCStyle.O)[0].getFirst().getValue().toString());
|
|
|
|
} catch (Exception e) {
|
|
|
|
//ignored
|
|
|
|
}
|
|
|
|
try {
|
2016-05-28 09:04:18 +00:00
|
|
|
information.putString("sha1", getFingerprintCert(certificate.getEncoded()));
|
2015-12-23 18:18:53 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
return information;
|
|
|
|
} catch (CertificateEncodingException e) {
|
|
|
|
return information;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-28 09:04:18 +00:00
|
|
|
public static String getFingerprintCert(byte[] input) throws NoSuchAlgorithmException {
|
|
|
|
MessageDigest md = MessageDigest.getInstance("SHA-1");
|
|
|
|
byte[] fingerprint = md.digest(input);
|
|
|
|
return prettifyFingerprintCert(bytesToHex(fingerprint));
|
|
|
|
}
|
|
|
|
|
2018-05-12 15:23:37 +00:00
|
|
|
public static String getAccountFingerprint(Account account, String androidId) {
|
|
|
|
return getFingerprint(account.getJid().asBareJid().toEscapedString()+"\00"+androidId);
|
2016-11-22 21:31:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String getFingerprint(String value) {
|
2016-10-06 16:09:55 +00:00
|
|
|
try {
|
2018-05-12 15:23:37 +00:00
|
|
|
MessageDigest md = MessageDigest.getInstance("SHA-1");
|
2016-11-22 21:31:46 +00:00
|
|
|
return bytesToHex(md.digest(value.getBytes("UTF-8")));
|
2016-10-06 16:09:55 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-28 21:40:09 +00:00
|
|
|
public static int encryptionTypeToText(int encryption) {
|
|
|
|
switch (encryption) {
|
|
|
|
case Message.ENCRYPTION_OTR:
|
|
|
|
return R.string.encryption_choice_otr;
|
|
|
|
case Message.ENCRYPTION_AXOLOTL:
|
2018-04-04 16:21:22 +00:00
|
|
|
case Message.ENCRYPTION_AXOLOTL_NOT_FOR_THIS_DEVICE:
|
2015-10-28 21:40:09 +00:00
|
|
|
return R.string.encryption_choice_omemo;
|
|
|
|
case Message.ENCRYPTION_NONE:
|
|
|
|
return R.string.encryption_choice_unencrypted;
|
|
|
|
default:
|
|
|
|
return R.string.encryption_choice_pgp;
|
|
|
|
}
|
|
|
|
}
|
2017-04-05 16:46:56 +00:00
|
|
|
|
|
|
|
public static URL toAesGcmUrl(URL url) {
|
|
|
|
if (!url.getProtocol().equalsIgnoreCase("https")) {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
try {
|
2017-04-16 06:44:21 +00:00
|
|
|
return new URL(AesGcmURLStreamHandler.PROTOCOL_NAME+url.toString().substring(url.getProtocol().length()));
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static URL toHttpsUrl(URL url) {
|
|
|
|
if (!url.getProtocol().equalsIgnoreCase(AesGcmURLStreamHandler.PROTOCOL_NAME)) {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return new URL("https"+url.toString().substring(url.getProtocol().length()));
|
2017-04-05 16:46:56 +00:00
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
}
|
2017-04-05 19:01:29 +00:00
|
|
|
|
|
|
|
public static boolean isPgpEncryptedUrl(String url) {
|
|
|
|
if (url == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
final String u = url.toLowerCase();
|
|
|
|
return !u.contains(" ") && (u.startsWith("https://") || u.startsWith("http://")) && u.endsWith(".pgp");
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|