View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http.ssl;
29  
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  import org.apache.hc.core5.http.ParseException;
34  import org.apache.hc.core5.http.ProtocolVersion;
35  import org.apache.hc.core5.util.Tokenizer;
36  
37  /**
38   * Supported {@code TLS} protocol versions.
39   *
40   * @since 5.0
41   */
42  public enum TLS {
43  
44      V_1_0("TLSv1",   new ProtocolVersion("TLS", 1, 0)),
45      V_1_1("TLSv1.1", new ProtocolVersion("TLS", 1, 1)),
46      V_1_2("TLSv1.2", new ProtocolVersion("TLS", 1, 2)),
47      V_1_3("TLSv1.3", new ProtocolVersion("TLS", 1, 3));
48  
49      public final String id;
50      public final ProtocolVersion version;
51  
52      TLS(final String id, final ProtocolVersion version) {
53          this.id = id;
54          this.version = version;
55      }
56  
57      public boolean isSame(final ProtocolVersion protocolVersion) {
58          return version.equals(protocolVersion);
59      }
60  
61      public boolean isComparable(final ProtocolVersion protocolVersion) {
62          return version.isComparable(protocolVersion);
63      }
64  
65      public boolean greaterEquals(final ProtocolVersion protocolVersion) {
66          return version.greaterEquals(protocolVersion);
67      }
68  
69      public boolean lessEquals(final ProtocolVersion protocolVersion) {
70          return version.lessEquals(protocolVersion);
71      }
72  
73      public static ProtocolVersion parse(final String s) throws ParseException {
74          if (s == null) {
75              return null;
76          }
77          final Tokenizer.Cursor cursor = new Tokenizer.Cursor(0, s.length());
78          return TlsVersionParser.INSTANCE.parse(s, cursor, null);
79      }
80  
81      public static String[] excludeWeak(final String... protocols) {
82          if (protocols == null) {
83              return null;
84          }
85          final List<String> enabledProtocols = new ArrayList<>();
86          for (final String protocol: protocols) {
87              if (!protocol.startsWith("SSL") && !protocol.equals(V_1_0.id) && !protocol.equals(V_1_1.id)) {
88                  enabledProtocols.add(protocol);
89              }
90          }
91          if (enabledProtocols.isEmpty()) {
92              enabledProtocols.add(V_1_2.id);
93          }
94          return enabledProtocols.toArray(new String[0]);
95      }
96  
97  }