2015-10-15 15:08:38 +00:00
|
|
|
package eu.siacs.conversations.crypto;
|
|
|
|
|
|
|
|
import android.util.Log;
|
2015-10-15 18:05:55 +00:00
|
|
|
import android.util.Pair;
|
2015-10-15 15:08:38 +00:00
|
|
|
|
2021-05-03 06:28:03 +00:00
|
|
|
import com.google.common.collect.ImmutableList;
|
|
|
|
|
2015-10-15 15:08:38 +00:00
|
|
|
import org.bouncycastle.asn1.ASN1Primitive;
|
|
|
|
import org.bouncycastle.asn1.DERIA5String;
|
|
|
|
import org.bouncycastle.asn1.DERTaggedObject;
|
|
|
|
import org.bouncycastle.asn1.DERUTF8String;
|
|
|
|
import org.bouncycastle.asn1.DLSequence;
|
|
|
|
import org.bouncycastle.asn1.x500.RDN;
|
|
|
|
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;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2021-02-04 10:15:59 +00:00
|
|
|
import java.net.IDN;
|
2015-10-15 16:06:26 +00:00
|
|
|
import java.security.cert.Certificate;
|
2017-07-16 09:05:19 +00:00
|
|
|
import java.security.cert.CertificateEncodingException;
|
2021-05-03 06:28:03 +00:00
|
|
|
import java.security.cert.CertificateParsingException;
|
2015-10-15 15:08:38 +00:00
|
|
|
import java.security.cert.X509Certificate;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.List;
|
2019-07-20 15:51:37 +00:00
|
|
|
import java.util.Locale;
|
2015-10-15 15:08:38 +00:00
|
|
|
|
2021-05-03 06:28:03 +00:00
|
|
|
import javax.net.ssl.SSLPeerUnverifiedException;
|
2015-10-15 15:08:38 +00:00
|
|
|
import javax.net.ssl.SSLSession;
|
|
|
|
|
2021-05-03 06:28:03 +00:00
|
|
|
public class XmppDomainVerifier {
|
2015-10-15 15:08:38 +00:00
|
|
|
|
2018-10-01 15:08:23 +00:00
|
|
|
private static final String LOGTAG = "XmppDomainVerifier";
|
2015-10-15 15:08:38 +00:00
|
|
|
|
2018-10-01 15:08:23 +00:00
|
|
|
private static final String SRV_NAME = "1.3.6.1.5.5.7.8.7";
|
|
|
|
private static final String XMPP_ADDR = "1.3.6.1.5.5.7.8.5";
|
2015-10-15 17:14:41 +00:00
|
|
|
|
2018-10-01 15:08:23 +00:00
|
|
|
private static List<String> getCommonNames(X509Certificate certificate) {
|
|
|
|
List<String> domains = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
X500Name x500name = new JcaX509CertificateHolder(certificate).getSubject();
|
|
|
|
RDN[] rdns = x500name.getRDNs(BCStyle.CN);
|
|
|
|
for (int i = 0; i < rdns.length; ++i) {
|
|
|
|
domains.add(IETFUtils.valueToString(x500name.getRDNs(BCStyle.CN)[i].getFirst().getValue()));
|
|
|
|
}
|
|
|
|
return domains;
|
|
|
|
} catch (CertificateEncodingException e) {
|
|
|
|
return domains;
|
|
|
|
}
|
|
|
|
}
|
2015-10-15 15:08:38 +00:00
|
|
|
|
2018-10-01 15:08:23 +00:00
|
|
|
private static Pair<String, String> parseOtherName(byte[] otherName) {
|
|
|
|
try {
|
|
|
|
ASN1Primitive asn1Primitive = ASN1Primitive.fromByteArray(otherName);
|
|
|
|
if (asn1Primitive instanceof DERTaggedObject) {
|
|
|
|
ASN1Primitive inner = ((DERTaggedObject) asn1Primitive).getObject();
|
|
|
|
if (inner instanceof DLSequence) {
|
|
|
|
DLSequence sequence = (DLSequence) inner;
|
|
|
|
if (sequence.size() >= 2 && sequence.getObjectAt(1) instanceof DERTaggedObject) {
|
|
|
|
String oid = sequence.getObjectAt(0).toString();
|
|
|
|
ASN1Primitive value = ((DERTaggedObject) sequence.getObjectAt(1)).getObject();
|
|
|
|
if (value instanceof DERUTF8String) {
|
|
|
|
return new Pair<>(oid, ((DERUTF8String) value).getString());
|
|
|
|
} else if (value instanceof DERIA5String) {
|
|
|
|
return new Pair<>(oid, ((DERIA5String) value).getString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
} catch (IOException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2017-07-16 09:05:19 +00:00
|
|
|
|
2021-03-23 10:52:34 +00:00
|
|
|
public static boolean matchDomain(final String needle, final List<String> haystack) {
|
|
|
|
for (final String entry : haystack) {
|
2018-10-01 15:08:23 +00:00
|
|
|
if (entry.startsWith("*.")) {
|
|
|
|
int offset = 0;
|
|
|
|
while (offset < needle.length()) {
|
|
|
|
int i = needle.indexOf('.', offset);
|
|
|
|
if (i < 0) {
|
|
|
|
break;
|
|
|
|
}
|
2019-07-20 15:51:37 +00:00
|
|
|
if (needle.substring(i).equalsIgnoreCase(entry.substring(1))) {
|
2018-10-01 15:08:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
offset = i + 1;
|
|
|
|
}
|
|
|
|
} else {
|
2019-07-20 15:51:37 +00:00
|
|
|
if (entry.equalsIgnoreCase(needle)) {
|
2018-10-01 15:08:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-15 18:05:55 +00:00
|
|
|
|
2021-05-03 06:28:03 +00:00
|
|
|
public boolean verify(final String unicodeDomain, final String unicodeHostname, SSLSession sslSession) throws SSLPeerUnverifiedException {
|
2021-02-04 10:15:59 +00:00
|
|
|
final String domain = IDN.toASCII(unicodeDomain);
|
|
|
|
final String hostname = unicodeHostname == null ? null : IDN.toASCII(unicodeHostname);
|
2021-05-03 06:28:03 +00:00
|
|
|
final Certificate[] chain = sslSession.getPeerCertificates();
|
|
|
|
if (chain.length == 0 || !(chain[0] instanceof X509Certificate)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
final X509Certificate certificate = (X509Certificate) chain[0];
|
|
|
|
final List<String> commonNames = getCommonNames(certificate);
|
|
|
|
if (isSelfSigned(certificate)) {
|
|
|
|
if (commonNames.size() == 1 && matchDomain(domain, commonNames)) {
|
|
|
|
Log.d(LOGTAG, "accepted CN in self signed cert as work around for " + domain);
|
|
|
|
return true;
|
2018-10-01 15:08:23 +00:00
|
|
|
}
|
2021-05-03 06:28:03 +00:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
final ValidDomains validDomains = parseValidDomains(certificate);
|
|
|
|
Log.d(LOGTAG, "searching for " + domain + " in srvNames: " + validDomains.srvNames + " xmppAddrs: " + validDomains.xmppAddrs + " domains:" + validDomains.domains);
|
2018-10-01 15:08:23 +00:00
|
|
|
if (hostname != null) {
|
|
|
|
Log.d(LOGTAG, "also trying to verify hostname " + hostname);
|
|
|
|
}
|
2021-05-03 06:28:03 +00:00
|
|
|
return validDomains.xmppAddrs.contains(domain)
|
|
|
|
|| validDomains.srvNames.contains("_xmpp-client." + domain)
|
|
|
|
|| matchDomain(domain, validDomains.domains)
|
|
|
|
|| (hostname != null && matchDomain(hostname, validDomains.domains));
|
2021-02-04 10:15:59 +00:00
|
|
|
} catch (final Exception e) {
|
2018-10-01 15:08:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-06-17 17:54:09 +00:00
|
|
|
|
2021-05-03 06:28:03 +00:00
|
|
|
public static ValidDomains parseValidDomains(final X509Certificate certificate) throws CertificateParsingException {
|
|
|
|
final List<String> commonNames = getCommonNames(certificate);
|
|
|
|
final Collection<List<?>> alternativeNames = certificate.getSubjectAlternativeNames();
|
|
|
|
final List<String> xmppAddrs = new ArrayList<>();
|
|
|
|
final List<String> srvNames = new ArrayList<>();
|
|
|
|
final List<String> domains = new ArrayList<>();
|
|
|
|
if (alternativeNames != null) {
|
|
|
|
for (List<?> san : alternativeNames) {
|
|
|
|
final Integer type = (Integer) san.get(0);
|
|
|
|
if (type == 0) {
|
|
|
|
final Pair<String, String> otherName = parseOtherName((byte[]) san.get(1));
|
|
|
|
if (otherName != null && otherName.first != null && otherName.second != null) {
|
|
|
|
switch (otherName.first) {
|
|
|
|
case SRV_NAME:
|
|
|
|
srvNames.add(otherName.second.toLowerCase(Locale.US));
|
|
|
|
break;
|
|
|
|
case XMPP_ADDR:
|
|
|
|
xmppAddrs.add(otherName.second.toLowerCase(Locale.US));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Log.d(LOGTAG, "oid: " + otherName.first + " value: " + otherName.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (type == 2) {
|
|
|
|
final Object value = san.get(1);
|
|
|
|
if (value instanceof String) {
|
|
|
|
domains.add(((String) value).toLowerCase(Locale.US));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (srvNames.size() == 0 && xmppAddrs.size() == 0 && domains.size() == 0) {
|
|
|
|
domains.addAll(commonNames);
|
|
|
|
}
|
|
|
|
return new ValidDomains(xmppAddrs, srvNames, domains);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final class ValidDomains {
|
|
|
|
final List<String> xmppAddrs;
|
|
|
|
final List<String> srvNames;
|
|
|
|
final List<String> domains;
|
|
|
|
|
|
|
|
private ValidDomains(List<String> xmppAddrs, List<String> srvNames, List<String> domains) {
|
|
|
|
this.xmppAddrs = xmppAddrs;
|
|
|
|
this.srvNames = srvNames;
|
|
|
|
this.domains = domains;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<String> all() {
|
|
|
|
ImmutableList.Builder<String> all = new ImmutableList.Builder<>();
|
|
|
|
all.addAll(xmppAddrs);
|
|
|
|
all.addAll(srvNames);
|
|
|
|
all.addAll(domains);
|
|
|
|
return all.build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 15:08:23 +00:00
|
|
|
private boolean isSelfSigned(X509Certificate certificate) {
|
|
|
|
try {
|
|
|
|
certificate.verify(certificate.getPublicKey());
|
|
|
|
return true;
|
|
|
|
} catch (Exception e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-10-15 15:08:38 +00:00
|
|
|
}
|