moved other name parsing into seperate method

This commit is contained in:
Daniel Gultsch 2015-10-15 20:05:55 +02:00
parent fc96dcaa4d
commit c1716a35e3

View File

@ -1,6 +1,7 @@
package eu.siacs.conversations.crypto;
import android.util.Log;
import android.util.Pair;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.DERIA5String;
@ -25,7 +26,7 @@ import javax.net.ssl.SSLSession;
public class XmppDomainVerifier implements HostnameVerifier {
private final String LOGTAG = "XmppDomainVerifier";
private static final String LOGTAG = "XmppDomainVerifier";
private final String SRVName = "1.3.6.1.5.5.7.8.7";
private final String xmppAddr = "1.3.6.1.5.5.7.8.5";
@ -46,39 +47,19 @@ public class XmppDomainVerifier implements HostnameVerifier {
for (List<?> san : alternativeNames) {
Integer type = (Integer) san.get(0);
if (type == 0) {
try {
ASN1Primitive asn1Primitive = ASN1Primitive.fromByteArray((byte[]) san.get(1));
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();
switch (oid) {
case xmppAddr:
if (value instanceof DERUTF8String) {
xmppAddrs.add(((DERUTF8String) value).getString());
} else if (value instanceof DERIA5String) {
xmppAddrs.add(((DERIA5String) value).getString());
}
break;
Pair<String, String> otherName = parseOtherName((byte[]) san.get(1));
if (otherName != null) {
switch (otherName.first) {
case SRVName:
if (value instanceof DERUTF8String) {
srvNames.add(((DERUTF8String) value).getString());
} else if (value instanceof DERIA5String) {
srvNames.add(((DERIA5String) value).getString());
}
srvNames.add(otherName.second);
break;
case xmppAddr:
xmppAddrs.add(otherName.second);
break;
default:
Log.d(LOGTAG,"value was of type:"+value.getClass().getName()+ " oid was:"+oid);
Log.d(LOGTAG, "oid: " + otherName.first + " value: " + otherName.second);
}
}
}
}
} catch (IOException e) {
//ignored
}
} else if (type == 2) {
Object value = san.get(1);
if (value instanceof String) {
@ -101,7 +82,31 @@ public class XmppDomainVerifier implements HostnameVerifier {
}
}
private boolean matchDomain(String needle, List<String> haystack) {
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;
}
}
private static boolean matchDomain(String needle, List<String> haystack) {
for (String entry : haystack) {
if (entry.startsWith("*.")) {
int i = needle.indexOf('.');