Replaced LdapName with custom DN parser

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1616137 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2014-08-06 09:29:58 +00:00
parent 95953f857e
commit 0bc123c992
2 changed files with 19 additions and 29 deletions

View File

@ -38,20 +38,15 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.NoSuchElementException;
import javax.naming.InvalidNameException;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.ldap.LdapName;
import javax.naming.ldap.Rdn;
import javax.net.ssl.SSLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.NameValuePair;
import org.apache.http.annotation.Immutable;
import org.apache.http.conn.util.InetAddressUtils;
import org.apache.http.util.TextUtils;
/**
* Abstract base class for all standard {@link org.apache.http.conn.ssl.X509HostnameVerifier}
@ -200,26 +195,17 @@ public abstract class AbstractCommonHostnameVerifier extends AbstractBaseHostnam
return null;
}
final List<String> cns = new ArrayList<String>();
try {
final LdapName subjectDN = new LdapName(subjectPrincipal);
final List<Rdn> rdns = subjectDN.getRdns();
for (int i = rdns.size() - 1; i >= 0; i--) {
final Rdn rds = rdns.get(i);
final Attributes attributes = rds.toAttributes();
final Attribute cn = attributes.get("cn");
if (cn != null) {
try {
final Object value = cn.get();
if (value != null) {
cns.add(value.toString());
}
} catch (NoSuchElementException ignore) {
} catch (NamingException ignore) {
}
}
final List<NameValuePair> nvps = DistinguishedNameParser.INSTANCE.parse(subjectPrincipal);
for (int i = 0; i < nvps.size(); i++) {
final NameValuePair nvp = nvps.get(i);
final String attribName = nvp.getName();
final String attribValue = nvp.getValue();
if (TextUtils.isBlank(attribValue)) {
throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name");
}
if (attribName.equalsIgnoreCase("cn")) {
cns.add(attribValue);
}
} catch (InvalidNameException e) {
throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name");
}
return cns.isEmpty() ? null : cns.toArray(new String[ cns.size() ]);
}

View File

@ -357,16 +357,20 @@ public class TestHostnameVerifier {
Assert.assertArrayEquals(new String[] {"blah, blah"}, AbstractCommonHostnameVerifier.extractCNs("cn=\"blah, blah\", ou=blah, o=blah"));
Assert.assertArrayEquals(new String[] {"blah, blah"}, AbstractCommonHostnameVerifier.extractCNs("cn=blah\\, blah, ou=blah, o=blah"));
Assert.assertArrayEquals(new String[] {"blah"}, AbstractCommonHostnameVerifier.extractCNs("c = cn=uuh, cn=blah, ou=blah, o=blah"));
Assert.assertArrayEquals(new String[] {""}, AbstractCommonHostnameVerifier.extractCNs("cn= , ou=blah, o=blah"));
}
@Test(expected = SSLException.class)
public void testExtractCNInvalid1() throws Exception {
public void testExtractCNEmpty() throws Exception {
AbstractCommonHostnameVerifier.extractCNs("cn= , ou=blah, o=blah");
}
@Test(expected = SSLException.class)
public void testExtractCNMissing() throws Exception {
AbstractCommonHostnameVerifier.extractCNs("blah,blah");
}
@Test(expected = SSLException.class)
public void testExtractCNInvalid2() throws Exception {
public void testExtractCNNull() throws Exception {
AbstractCommonHostnameVerifier.extractCNs("cn,o=blah");
}