You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2018/10/21 14:54:20 UTC
[2/3] httpcomponents-client git commit: Added enum for supported TLS
versions & TLS version parser
Added enum for supported TLS versions & TLS version parser
Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/commit/1eab9876
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/tree/1eab9876
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/diff/1eab9876
Branch: refs/heads/master
Commit: 1eab98760b9951abe1d7c084ee5edc3d35960ffd
Parents: e72d745
Author: Oleg Kalnichevski <ol...@apache.org>
Authored: Sat Oct 20 14:34:22 2018 +0200
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Sun Oct 21 16:52:40 2018 +0200
----------------------------------------------------------------------
.../org/apache/hc/client5/http/ssl/TLS.java | 78 ++++++++++++++
.../hc/client5/http/ssl/TlsVersionParser.java | 105 +++++++++++++++++++
.../client5/http/ssl/TestTlsVersionParser.java | 93 ++++++++++++++++
3 files changed, 276 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/1eab9876/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/TLS.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/TLS.java b/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/TLS.java
new file mode 100644
index 0000000..a369d6f
--- /dev/null
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/TLS.java
@@ -0,0 +1,78 @@
+/*
+ * ====================================================================
+ * 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.hc.client5.http.ssl;
+
+import org.apache.hc.core5.http.ParseException;
+import org.apache.hc.core5.http.ProtocolVersion;
+import org.apache.hc.core5.http.message.ParserCursor;
+
+/**
+ * Supported {@code TLS} protocol versions.
+ *
+ * @since 5.0
+ */
+public enum TLS {
+
+ V_1_0("TLSv1", new ProtocolVersion("TLS", 1, 0)),
+ V_1_1("TLSv1.1", new ProtocolVersion("TLS", 1, 1)),
+ V_1_2("TLSv1.2", new ProtocolVersion("TLS", 1, 2)),
+ V_1_3("TLSv1.3", new ProtocolVersion("TLS", 1, 3));
+
+ public final String ident;
+ public final ProtocolVersion version;
+
+ TLS(final String ident, final ProtocolVersion version) {
+ this.ident = ident;
+ this.version = version;
+ }
+
+ public boolean isSame(final ProtocolVersion protocolVersion) {
+ return version.equals(protocolVersion);
+ }
+
+ public boolean isComparable(final ProtocolVersion protocolVersion) {
+ return version.isComparable(protocolVersion);
+ }
+
+ public boolean greaterEquals(final ProtocolVersion protocolVersion) {
+ return version.greaterEquals(protocolVersion);
+ }
+
+ public boolean lessEquals(final ProtocolVersion protocolVersion) {
+ return version.lessEquals(protocolVersion);
+ }
+
+ public static ProtocolVersion parse(final String s) throws ParseException {
+ if (s == null) {
+ return null;
+ }
+ final ParserCursor cursor = new ParserCursor(0, s.length());
+ return TlsVersionParser.INSTANCE.parse(s, cursor, null);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/1eab9876/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/TlsVersionParser.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/TlsVersionParser.java b/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/TlsVersionParser.java
new file mode 100644
index 0000000..606bdea
--- /dev/null
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/TlsVersionParser.java
@@ -0,0 +1,105 @@
+/*
+ * ====================================================================
+ * 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.hc.client5.http.ssl;
+
+import java.util.BitSet;
+
+import org.apache.hc.core5.http.ParseException;
+import org.apache.hc.core5.http.ProtocolVersion;
+import org.apache.hc.core5.http.message.ParserCursor;
+import org.apache.hc.core5.http.message.TokenParser;
+
+final class TlsVersionParser {
+
+ public final static TlsVersionParser INSTANCE = new TlsVersionParser();
+
+ private final TokenParser tokenParser;
+
+ TlsVersionParser() {
+ this.tokenParser = TokenParser.INSTANCE;
+ }
+
+ ProtocolVersion parse(
+ final CharSequence buffer,
+ final ParserCursor cursor,
+ final BitSet delimiters) throws ParseException {
+ final int lowerBound = cursor.getLowerBound();
+ final int upperBound = cursor.getUpperBound();
+
+ int pos = cursor.getPos();
+ if (pos + 4 > cursor.getUpperBound()) {
+ throw new ParseException("Invalid TLS protocol version", buffer, lowerBound, upperBound, pos);
+ }
+ if (buffer.charAt(pos) != 'T' || buffer.charAt(pos + 1) != 'L' || buffer.charAt(pos + 2) != 'S'
+ || buffer.charAt(pos + 3) != 'v') {
+ throw new ParseException("Invalid TLS protocol version", buffer, lowerBound, upperBound, pos);
+ }
+ pos = pos + 4;
+ cursor.updatePos(pos);
+ if (cursor.atEnd()) {
+ throw new ParseException("Invalid TLS version", buffer, lowerBound, upperBound, pos);
+ }
+ final String s = this.tokenParser.parseToken(buffer, cursor, delimiters);
+ final int idx = s.indexOf('.');
+ if (idx == -1) {
+ final int major;
+ try {
+ major = Integer.parseInt(s);
+ } catch (final NumberFormatException e) {
+ throw new ParseException("Invalid TLS major version", buffer, lowerBound, upperBound, pos);
+ }
+ return new ProtocolVersion("TLS", major, 0);
+ } else {
+ final String s1 = s.substring(0, idx);
+ final int major;
+ try {
+ major = Integer.parseInt(s1);
+ } catch (final NumberFormatException e) {
+ throw new ParseException("Invalid TLS major version", buffer, lowerBound, upperBound, pos);
+ }
+ final String s2 = s.substring(idx + 1);
+ final int minor;
+ try {
+ minor = Integer.parseInt(s2);
+ } catch (final NumberFormatException e) {
+ throw new ParseException("Invalid TLS minor version", buffer, lowerBound, upperBound, pos);
+ }
+ return new ProtocolVersion("TLS", major, minor);
+ }
+ }
+
+ ProtocolVersion parse(final String s) throws ParseException {
+ if (s == null) {
+ return null;
+ }
+ final ParserCursor cursor = new ParserCursor(0, s.length());
+ return parse(s, cursor, null);
+ }
+
+}
+
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/1eab9876/httpclient5/src/test/java/org/apache/hc/client5/http/ssl/TestTlsVersionParser.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/ssl/TestTlsVersionParser.java b/httpclient5/src/test/java/org/apache/hc/client5/http/ssl/TestTlsVersionParser.java
new file mode 100644
index 0000000..30654b6
--- /dev/null
+++ b/httpclient5/src/test/java/org/apache/hc/client5/http/ssl/TestTlsVersionParser.java
@@ -0,0 +1,93 @@
+/*
+ * ====================================================================
+ * 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.hc.client5.http.ssl;
+
+import org.apache.hc.core5.http.ParseException;
+import org.apache.hc.core5.http.ProtocolVersion;
+import org.apache.hc.core5.http.message.ParserCursor;
+import org.apache.hc.core5.http.message.TokenParser;
+import org.hamcrest.CoreMatchers;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Unit tests for {@link TlsVersionParser}.
+ */
+public class TestTlsVersionParser {
+
+ private TlsVersionParser impl;
+
+ @Before
+ public void setup() {
+ impl = new TlsVersionParser();
+ }
+
+ @Test
+ public void testParseBasic() throws Exception {
+ Assert.assertThat(impl.parse("TLSv1"), CoreMatchers.equalTo(TLS.V_1_0.version));
+ Assert.assertThat(impl.parse("TLSv1.1"), CoreMatchers.equalTo(TLS.V_1_1.version));
+ Assert.assertThat(impl.parse("TLSv1.2"), CoreMatchers.equalTo(TLS.V_1_2.version));
+ Assert.assertThat(impl.parse("TLSv1.3"), CoreMatchers.equalTo(TLS.V_1_3.version));
+ Assert.assertThat(impl.parse("TLSv22.356"), CoreMatchers.equalTo(new ProtocolVersion("TLS", 22, 356)));
+ }
+
+ @Test
+ public void testParseBuffer() throws Exception {
+ final ParserCursor cursor = new ParserCursor(1, 13);
+ Assert.assertThat(impl.parse(" TLSv1.2,0000", cursor, TokenParser.INIT_BITSET(',')),
+ CoreMatchers.equalTo(TLS.V_1_2.version));
+ Assert.assertThat(cursor.getPos(), CoreMatchers.equalTo(8));
+ }
+
+ @Test(expected = ParseException.class)
+ public void testParseFailure1() throws Exception {
+ impl.parse("Tlsv1");
+ }
+
+ @Test(expected = ParseException.class)
+ public void testParseFailure2() throws Exception {
+ impl.parse("TLSV1");
+ }
+
+ @Test(expected = ParseException.class)
+ public void testParseFailure3() throws Exception {
+ impl.parse("TLSv");
+ }
+
+ @Test(expected = ParseException.class)
+ public void testParseFailure4() throws Exception {
+ impl.parse("TLSv1A");
+ }
+
+ @Test(expected = ParseException.class)
+ public void testParseFailure5() throws Exception {
+ impl.parse("TLSv1.A");
+ }
+
+}