Reverted r1616137, r1616136, r1615900

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1616688 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2014-08-08 08:30:01 +00:00
parent d5a4d66890
commit 80b0014754
5 changed files with 26 additions and 649 deletions

View File

@ -38,15 +38,20 @@ 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}
@ -195,17 +200,26 @@ public abstract class AbstractCommonHostnameVerifier extends AbstractBaseHostnam
return null;
}
final List<String> cns = new ArrayList<String>();
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);
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) {
}
}
}
} 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

@ -1,132 +0,0 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.conn.ssl;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.annotation.Immutable;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.message.ParserCursor;
import org.apache.http.util.CharArrayBuffer;
@Immutable
final class DistinguishedNameParser {
public final static DistinguishedNameParser INSTANCE = new DistinguishedNameParser();
private static final BitSet EQUAL_OR_COMMA_OR_PLUS = TokenParser.INIT_BITSET('=', ',', '+');
private static final BitSet COMMA_OR_PLUS = TokenParser.INIT_BITSET(',', '+');
private final TokenParser tokenParser;
DistinguishedNameParser() {
this.tokenParser = new InternalTokenParser();
}
String parseToken(final CharArrayBuffer buf, final ParserCursor cursor, final BitSet delimiters) {
return tokenParser.parseToken(buf, cursor, delimiters);
}
String parseValue(final CharArrayBuffer buf, final ParserCursor cursor, final BitSet delimiters) {
return tokenParser.parseValue(buf, cursor, delimiters);
}
NameValuePair parseParameter(final CharArrayBuffer buf, final ParserCursor cursor) {
final String name = parseToken(buf, cursor, EQUAL_OR_COMMA_OR_PLUS);
if (cursor.atEnd()) {
return new BasicNameValuePair(name, null);
}
final int delim = buf.charAt(cursor.getPos());
cursor.updatePos(cursor.getPos() + 1);
if (delim == ',') {
return new BasicNameValuePair(name, null);
}
final String value = parseValue(buf, cursor, COMMA_OR_PLUS);
if (!cursor.atEnd()) {
cursor.updatePos(cursor.getPos() + 1);
}
return new BasicNameValuePair(name, value);
}
public List<NameValuePair> parse(final CharArrayBuffer buf, final ParserCursor cursor) {
final List<NameValuePair> params = new ArrayList<NameValuePair>();
tokenParser.skipWhiteSpace(buf, cursor);
while (!cursor.atEnd()) {
final NameValuePair param = parseParameter(buf, cursor);
params.add(param);
}
return params;
}
public List<NameValuePair> parse(final String s) {
if (s == null) {
return null;
}
final CharArrayBuffer buffer = new CharArrayBuffer(s.length());
buffer.append(s);
final ParserCursor cursor = new ParserCursor(0, s.length());
return parse(buffer, cursor);
}
static class InternalTokenParser extends TokenParser {
@Override
public void copyUnquotedContent(
final CharArrayBuffer buf,
final ParserCursor cursor,
final BitSet delimiters,
final StringBuilder dst) {
int pos = cursor.getPos();
final int indexFrom = cursor.getPos();
final int indexTo = cursor.getUpperBound();
boolean escaped = false;
for (int i = indexFrom; i < indexTo; i++, pos++) {
final char current = buf.charAt(i);
if (escaped) {
dst.append(current);
escaped = false;
} else {
if ((delimiters != null && delimiters.get(current))
|| TokenParser.isWhitespace(current) || current == '\"') {
break;
} else if (current == '\\') {
escaped = true;
} else {
dst.append(current);
}
}
}
cursor.updatePos(pos);
}
}
}

View File

@ -1,266 +0,0 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.conn.ssl;
import java.util.BitSet;
import org.apache.http.message.ParserCursor;
import org.apache.http.util.CharArrayBuffer;
/**
* Low level parser for header field elements. The parsing routines of this class are designed
* to produce near zero intermediate garbage and make no intermediate copies of input data.
* <p>
* This class is immutable and thread safe.
*
* Temporary package-private copy of org.apache.http.message.TokenParser
*/
class TokenParser {
public static BitSet INIT_BITSET(final int ... b) {
final BitSet bitset = new BitSet();
for (final int aB : b) {
bitset.set(aB);
}
return bitset;
}
/** US-ASCII CR, carriage return (13) */
public static final char CR = '\r';
/** US-ASCII LF, line feed (10) */
public static final char LF = '\n';
/** US-ASCII SP, space (32) */
public static final char SP = ' ';
/** US-ASCII HT, horizontal-tab (9) */
public static final char HT = '\t';
/** Double quote */
public static final char DQUOTE = '\"';
/** Backward slash / escape character */
public static final char ESCAPE = '\\';
public static boolean isWhitespace(final char ch) {
return ch == SP || ch == HT || ch == CR || ch == LF;
}
public static final TokenParser INSTANCE = new TokenParser();
/**
* Extracts from the sequence of chars a token terminated with any of the given delimiters
* discarding semantically insignificant whitespace characters.
*
* @param buf buffer with the sequence of chars to be parsed
* @param cursor defines the bounds and current position of the buffer
* @param delimiters set of delimiting characters. Can be <code>null</code> if the token
* is not delimited by any character.
*/
public String parseToken(final CharArrayBuffer buf, final ParserCursor cursor, final BitSet delimiters) {
final StringBuilder dst = new StringBuilder();
boolean whitespace = false;
while (!cursor.atEnd()) {
final char current = buf.charAt(cursor.getPos());
if (delimiters != null && delimiters.get(current)) {
break;
} else if (isWhitespace(current)) {
skipWhiteSpace(buf, cursor);
whitespace = true;
} else {
if (whitespace && dst.length() > 0) {
dst.append(' ');
}
copyContent(buf, cursor, delimiters, dst);
whitespace = false;
}
}
return dst.toString();
}
/**
* Extracts from the sequence of chars a value which can be enclosed in quote marks and
* terminated with any of the given delimiters discarding semantically insignificant
* whitespace characters.
*
* @param buf buffer with the sequence of chars to be parsed
* @param cursor defines the bounds and current position of the buffer
* @param delimiters set of delimiting characters. Can be <code>null</code> if the value
* is not delimited by any character.
*/
public String parseValue(final CharArrayBuffer buf, final ParserCursor cursor, final BitSet delimiters) {
final StringBuilder dst = new StringBuilder();
boolean whitespace = false;
while (!cursor.atEnd()) {
final char current = buf.charAt(cursor.getPos());
if (delimiters != null && delimiters.get(current)) {
break;
} else if (isWhitespace(current)) {
skipWhiteSpace(buf, cursor);
whitespace = true;
} else if (current == DQUOTE) {
if (whitespace && dst.length() > 0) {
dst.append(' ');
}
copyQuotedContent(buf, cursor, dst);
whitespace = false;
} else {
if (whitespace && dst.length() > 0) {
dst.append(' ');
}
copyUnquotedContent(buf, cursor, delimiters, dst);
whitespace = false;
}
}
return dst.toString();
}
/**
* Skips semantically insignificant whitespace characters and moves the cursor to the closest
* non-whitespace character.
*
* @param buf buffer with the sequence of chars to be parsed
* @param cursor defines the bounds and current position of the buffer
*/
public void skipWhiteSpace(final CharArrayBuffer buf, final ParserCursor cursor) {
int pos = cursor.getPos();
final int indexFrom = cursor.getPos();
final int indexTo = cursor.getUpperBound();
for (int i = indexFrom; i < indexTo; i++) {
final char current = buf.charAt(i);
if (!isWhitespace(current)) {
break;
} else {
pos++;
}
}
cursor.updatePos(pos);
}
/**
* Transfers content into the destination buffer until a whitespace character or any of
* the given delimiters is encountered.
*
* @param buf buffer with the sequence of chars to be parsed
* @param cursor defines the bounds and current position of the buffer
* @param delimiters set of delimiting characters. Can be <code>null</code> if the value
* is delimited by a whitespace only.
* @param dst destination buffer
*/
public void copyContent(final CharArrayBuffer buf, final ParserCursor cursor, final BitSet delimiters,
final StringBuilder dst) {
int pos = cursor.getPos();
final int indexFrom = cursor.getPos();
final int indexTo = cursor.getUpperBound();
for (int i = indexFrom; i < indexTo; i++) {
final char current = buf.charAt(i);
if ((delimiters != null && delimiters.get(current)) || isWhitespace(current)) {
break;
} else {
pos++;
dst.append(current);
}
}
cursor.updatePos(pos);
}
/**
* Transfers content into the destination buffer until a whitespace character, a quote,
* or any of the given delimiters is encountered.
*
* @param buf buffer with the sequence of chars to be parsed
* @param cursor defines the bounds and current position of the buffer
* @param delimiters set of delimiting characters. Can be <code>null</code> if the value
* is delimited by a whitespace or a quote only.
* @param dst destination buffer
*/
public void copyUnquotedContent(final CharArrayBuffer buf, final ParserCursor cursor,
final BitSet delimiters, final StringBuilder dst) {
int pos = cursor.getPos();
final int indexFrom = cursor.getPos();
final int indexTo = cursor.getUpperBound();
for (int i = indexFrom; i < indexTo; i++) {
final char current = buf.charAt(i);
if ((delimiters != null && delimiters.get(current))
|| isWhitespace(current) || current == DQUOTE) {
break;
} else {
pos++;
dst.append(current);
}
}
cursor.updatePos(pos);
}
/**
* Transfers content enclosed with quote marks into the destination buffer.
*
* @param buf buffer with the sequence of chars to be parsed
* @param cursor defines the bounds and current position of the buffer
* @param dst destination buffer
*/
public void copyQuotedContent(final CharArrayBuffer buf, final ParserCursor cursor,
final StringBuilder dst) {
if (cursor.atEnd()) {
return;
}
int pos = cursor.getPos();
int indexFrom = cursor.getPos();
final int indexTo = cursor.getUpperBound();
char current = buf.charAt(pos);
if (current != DQUOTE) {
return;
}
pos++;
indexFrom++;
boolean escaped = false;
for (int i = indexFrom; i < indexTo; i++, pos++) {
current = buf.charAt(i);
if (escaped) {
if (current != DQUOTE && current != ESCAPE) {
dst.append(ESCAPE);
}
dst.append(current);
escaped = false;
} else {
if (current == DQUOTE) {
pos++;
break;
}
if (current == ESCAPE) {
escaped = true;
} else if (current != CR && current != LF) {
dst.append(current);
}
}
}
cursor.updatePos(pos);
}
}

View File

@ -1,234 +0,0 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.conn.ssl;
import java.util.Arrays;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.message.ParserCursor;
import org.apache.http.util.CharArrayBuffer;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class TestDistinguishedNameParser {
private DistinguishedNameParser parser;
@Before
public void setUp() throws Exception {
parser = new DistinguishedNameParser();
}
private static CharArrayBuffer createBuffer(final String value) {
if (value == null) {
return null;
}
final CharArrayBuffer buffer = new CharArrayBuffer(value.length());
buffer.append(value);
return buffer;
}
@Test
public void testTokenParsingMixedValuesAndQuotedValues() throws Exception {
final String s = " stuff and \" some more \" \"stuff ;";
final CharArrayBuffer raw = createBuffer(s);
final ParserCursor cursor = new ParserCursor(0, s.length());
final String result = parser.parseValue(raw, cursor, TokenParser.INIT_BITSET(';'));
Assert.assertEquals("stuff and some more stuff ;", result);
}
@Test
public void testTokenParsingMixedValuesAndQuotedValues2() throws Exception {
final String s = "stuff\"more\"stuff;";
final CharArrayBuffer raw = createBuffer(s);
final ParserCursor cursor = new ParserCursor(0, s.length());
final String result = parser.parseValue(raw, cursor, TokenParser.INIT_BITSET(';'));
Assert.assertEquals("stuffmorestuff", result);
}
@Test
public void testTokenParsingEscapedQuotes() throws Exception {
final String s = "stuff\"\\\"more\\\"\"stuff;";
final CharArrayBuffer raw = createBuffer(s);
final ParserCursor cursor = new ParserCursor(0, s.length());
final String result = parser.parseValue(raw, cursor, TokenParser.INIT_BITSET(';'));
Assert.assertEquals("stuff\"more\"stuff", result);
}
@Test
public void testTokenParsingEscapedDelimiter() throws Exception {
final String s = "stuff\"\\\"more\\\";\"stuff;";
final CharArrayBuffer raw = createBuffer(s);
final ParserCursor cursor = new ParserCursor(0, s.length());
final String result = parser.parseValue(raw, cursor, TokenParser.INIT_BITSET(';'));
Assert.assertEquals("stuff\"more\";stuff", result);
}
@Test
public void testTokenParsingEscapedSlash() throws Exception {
final String s = "stuff\"\\\"more\\\";\\\\\"stuff;";
final CharArrayBuffer raw = createBuffer(s);
final ParserCursor cursor = new ParserCursor(0, s.length());
final String result = parser.parseValue(raw, cursor, TokenParser.INIT_BITSET(';'));
Assert.assertEquals("stuff\"more\";\\stuff", result);
}
@Test
public void testTokenParsingSlashOutsideQuotes() throws Exception {
final String s = "stuff\\; more stuff;";
final CharArrayBuffer raw = createBuffer(s);
final ParserCursor cursor = new ParserCursor(0, s.length());
final String result = parser.parseValue(raw, cursor, TokenParser.INIT_BITSET(';'));
Assert.assertEquals("stuff; more stuff", result);
}
@Test
public void testBasicParameterParsing() throws Exception {
final String s = "cn=blah,";
final CharArrayBuffer raw = createBuffer(s);
final ParserCursor cursor = new ParserCursor(0, s.length());
final NameValuePair result = parser.parseParameter(raw, cursor);
Assert.assertNotNull("cn", result.getName());
Assert.assertEquals("blah", result.getValue());
}
@Test
public void testParameterParsingBlanks() throws Exception {
final String s = " cn = blah ,stuff";
final CharArrayBuffer raw = createBuffer(s);
final ParserCursor cursor = new ParserCursor(0, s.length());
final NameValuePair result = parser.parseParameter(raw, cursor);
Assert.assertNotNull("cn", result.getName());
Assert.assertEquals("blah", result.getValue());
Assert.assertEquals('s', raw.charAt(cursor.getPos()));
}
@Test
public void testParameterParsingEmptyValue() throws Exception {
final String s = " cn = ,stuff ";
final CharArrayBuffer raw = createBuffer(s);
final ParserCursor cursor = new ParserCursor(0, s.length());
final NameValuePair result = parser.parseParameter(raw, cursor);
Assert.assertNotNull("cn", result.getName());
Assert.assertEquals("", result.getValue());
Assert.assertEquals('s', raw.charAt(cursor.getPos()));
}
@Test
public void testParameterParsingNullValue() throws Exception {
final String s = " cn ";
final CharArrayBuffer raw = createBuffer(s);
final ParserCursor cursor = new ParserCursor(0, s.length());
final NameValuePair result = parser.parseParameter(raw, cursor);
Assert.assertNotNull("cn", result.getName());
Assert.assertEquals(null, result.getValue());
Assert.assertTrue(cursor.atEnd());
}
@Test
public void testParameterParsingQuotedValue() throws Exception {
final String s = "cn = \"blah, blah\" ,stuff";
final CharArrayBuffer raw = createBuffer(s);
final ParserCursor cursor = new ParserCursor(0, s.length());
final NameValuePair result = parser.parseParameter(raw, cursor);
Assert.assertNotNull("cn", result.getName());
Assert.assertEquals("blah, blah", result.getValue());
Assert.assertEquals('s', raw.charAt(cursor.getPos()));
}
@Test
public void testParameterParsingQuotedValueWithEscapedQuotes() throws Exception {
final String s = "cn = \"blah, blah, \\\"yada, yada\\\"\" ,stuff";
final CharArrayBuffer raw = createBuffer(s);
final ParserCursor cursor = new ParserCursor(0, s.length());
final NameValuePair result = parser.parseParameter(raw, cursor);
Assert.assertNotNull("cn", result.getName());
Assert.assertEquals("blah, blah, \"yada, yada\"", result.getValue());
Assert.assertEquals('s', raw.charAt(cursor.getPos()));
}
@Test
public void testParameterParsingValueWithEscapedDelimiter() throws Exception {
final String s = "cn = blah\\, blah\\, blah ,stuff";
final CharArrayBuffer raw = createBuffer(s);
final ParserCursor cursor = new ParserCursor(0, s.length());
final NameValuePair result = parser.parseParameter(raw, cursor);
Assert.assertNotNull("cn", result.getName());
Assert.assertEquals("blah, blah, blah", result.getValue());
Assert.assertEquals('s', raw.charAt(cursor.getPos()));
}
@Test
public void testDNParsing() throws Exception {
Assert.assertEquals(Arrays.asList(
new BasicNameValuePair("cn", "blah"),
new BasicNameValuePair("cn", "yada"),
new BasicNameValuePair("cn", "booh")),
parser.parse("cn=blah, cn=yada, cn=booh"));
Assert.assertEquals(Arrays.asList(
new BasicNameValuePair("c", "pampa"),
new BasicNameValuePair("cn", "blah"),
new BasicNameValuePair("ou", "blah"),
new BasicNameValuePair("o", "blah")),
parser.parse("c = pampa , cn = blah , ou = blah , o = blah"));
Assert.assertEquals(Arrays.asList(
new BasicNameValuePair("cn", "blah"),
new BasicNameValuePair("ou", "blah"),
new BasicNameValuePair("o", "blah")),
parser.parse("cn=\"blah\", ou=blah, o=blah"));
Assert.assertEquals(Arrays.asList(
new BasicNameValuePair("cn", "blah blah"),
new BasicNameValuePair("ou", "blah"),
new BasicNameValuePair("o", "blah")),
parser.parse("cn=\"blah blah\", ou=blah, o=blah"));
Assert.assertEquals(Arrays.asList(
new BasicNameValuePair("cn", "blah, blah"),
new BasicNameValuePair("ou", "blah"),
new BasicNameValuePair("o", "blah")),
parser.parse("cn=\"blah, blah\", ou=blah, o=blah"));
Assert.assertEquals(Arrays.asList(
new BasicNameValuePair("cn", "blah, blah"),
new BasicNameValuePair("ou", "blah"),
new BasicNameValuePair("o", "blah")),
parser.parse("cn=blah\\, blah, ou=blah, o=blah"));
Assert.assertEquals(Arrays.asList(
new BasicNameValuePair("c", "cn=uuh"),
new BasicNameValuePair("cn", "blah"),
new BasicNameValuePair("ou", "blah"),
new BasicNameValuePair("o", "blah")),
parser.parse("c = cn=uuh, cn=blah, ou=blah, o=blah"));
Assert.assertEquals(Arrays.asList(
new BasicNameValuePair("cn", ""),
new BasicNameValuePair("ou", "blah"),
new BasicNameValuePair("o", "blah")),
parser.parse("cn= , ou=blah, o=blah"));
}
}

View File

@ -359,11 +359,6 @@ public class TestHostnameVerifier {
Assert.assertArrayEquals(new String[] {"blah"}, AbstractCommonHostnameVerifier.extractCNs("c = cn=uuh, cn=blah, ou=blah, o=blah"));
}
@Test(expected = SSLException.class)
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");