1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
39
40
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 }