|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package sun.security.ssl; |
|
|
|
import java.security.CryptoPrimitive; |
|
import java.util.ArrayList; |
|
import java.util.Collections; |
|
import java.util.EnumSet; |
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public enum ProtocolVersion { |
|
TLS13 (0x0304, "TLSv1.3"), |
|
TLS12 (0x0303, "TLSv1.2"), |
|
TLS11 (0x0302, "TLSv1.1"), |
|
TLS10 (0x0301, "TLSv1"), |
|
SSL30 (0x0300, "SSLv3"), |
|
SSL20Hello (0x0002, "SSLv2Hello"), |
|
|
|
|
|
NONE (-1, "NONE"); |
|
|
|
|
|
final int id; |
|
final String name; |
|
final byte major; |
|
final byte minor; |
|
final boolean isAvailable; |
|
|
|
|
|
static final int LIMIT_MAX_VALUE = 0xFFFF; |
|
|
|
|
|
static final int LIMIT_MIN_VALUE = 0x0000; |
|
|
|
|
|
static final ProtocolVersion[] PROTOCOLS_TO_10 = new ProtocolVersion[] { |
|
TLS10, SSL30 |
|
}; |
|
|
|
|
|
static final ProtocolVersion[] PROTOCOLS_TO_11 = new ProtocolVersion[] { |
|
TLS11, TLS10, SSL30 |
|
}; |
|
|
|
|
|
static final ProtocolVersion[] PROTOCOLS_TO_12 = new ProtocolVersion[] { |
|
TLS12, TLS11, TLS10, SSL30 |
|
}; |
|
|
|
|
|
static final ProtocolVersion[] PROTOCOLS_TO_13 = new ProtocolVersion[] { |
|
TLS13, TLS12, TLS11, TLS10, SSL30 |
|
}; |
|
|
|
|
|
static final ProtocolVersion[] PROTOCOLS_OF_NONE = new ProtocolVersion[] { |
|
NONE |
|
}; |
|
|
|
|
|
static final ProtocolVersion[] PROTOCOLS_OF_30 = new ProtocolVersion[] { |
|
SSL30 |
|
}; |
|
|
|
|
|
static final ProtocolVersion[] PROTOCOLS_OF_11 = new ProtocolVersion[] { |
|
TLS11 |
|
}; |
|
|
|
|
|
static final ProtocolVersion[] PROTOCOLS_OF_12 = new ProtocolVersion[] { |
|
TLS12 |
|
}; |
|
|
|
|
|
static final ProtocolVersion[] PROTOCOLS_OF_13 = new ProtocolVersion[] { |
|
TLS13 |
|
}; |
|
|
|
|
|
static final ProtocolVersion[] PROTOCOLS_10_11 = new ProtocolVersion[] { |
|
TLS11, TLS10 |
|
}; |
|
|
|
|
|
static final ProtocolVersion[] PROTOCOLS_11_12 = new ProtocolVersion[] { |
|
TLS12, TLS11 |
|
}; |
|
|
|
|
|
static final ProtocolVersion[] PROTOCOLS_12_13 = new ProtocolVersion[] { |
|
TLS13, TLS12 |
|
}; |
|
|
|
|
|
static final ProtocolVersion[] PROTOCOLS_10_12 = new ProtocolVersion[] { |
|
TLS12, TLS11, TLS10 |
|
}; |
|
|
|
|
|
static final ProtocolVersion[] PROTOCOLS_TO_TLS12 = new ProtocolVersion[] { |
|
TLS12, TLS11, TLS10, SSL30 |
|
}; |
|
|
|
|
|
static final ProtocolVersion[] PROTOCOLS_TO_TLS11 = new ProtocolVersion[] { |
|
TLS11, TLS10, SSL30 |
|
}; |
|
|
|
|
|
static final ProtocolVersion[] PROTOCOLS_TO_TLS10 = new ProtocolVersion[] { |
|
TLS10, SSL30 |
|
}; |
|
|
|
|
|
static final ProtocolVersion[] PROTOCOLS_EMPTY = new ProtocolVersion[0]; |
|
|
|
private ProtocolVersion(int id, String name) { |
|
this.id = id; |
|
this.name = name; |
|
this.major = (byte)((id >>> 8) & 0xFF); |
|
this.minor = (byte)(id & 0xFF); |
|
|
|
this.isAvailable = SSLAlgorithmConstraints.DEFAULT_SSL_ONLY.permits( |
|
EnumSet.<CryptoPrimitive>of(CryptoPrimitive.KEY_AGREEMENT), |
|
name, null); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
static ProtocolVersion valueOf(byte major, byte minor) { |
|
for (ProtocolVersion pv : ProtocolVersion.values()) { |
|
if ((pv.major == major) && (pv.minor == minor)) { |
|
return pv; |
|
} |
|
} |
|
|
|
return null; |
|
} |
|
|
|
|
|
|
|
*/ |
|
static ProtocolVersion valueOf(int id) { |
|
for (ProtocolVersion pv : ProtocolVersion.values()) { |
|
if (pv.id == id) { |
|
return pv; |
|
} |
|
} |
|
|
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
static String nameOf(byte major, byte minor) { |
|
for (ProtocolVersion pv : ProtocolVersion.values()) { |
|
if ((pv.major == major) && (pv.minor == minor)) { |
|
return pv.name; |
|
} |
|
} |
|
|
|
return "TLS-" + major + "." + minor; |
|
} |
|
|
|
|
|
|
|
*/ |
|
static String nameOf(int id) { |
|
return nameOf((byte)((id >>> 8) & 0xFF), (byte)(id & 0xFF)); |
|
} |
|
|
|
|
|
|
|
*/ |
|
static ProtocolVersion nameOf(String name) { |
|
for (ProtocolVersion pv : ProtocolVersion.values()) { |
|
if (pv.name.equals(name)) { |
|
return pv; |
|
} |
|
} |
|
|
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
static boolean isNegotiable( |
|
byte major, byte minor, boolean allowSSL20Hello) { |
|
int v = ((major & 0xFF) << 8) | (minor & 0xFF); |
|
if (v < SSL30.id) { |
|
if (!allowSSL20Hello || (v != SSL20Hello.id)) { |
|
return false; |
|
} |
|
} |
|
return true; |
|
} |
|
|
|
|
|
|
|
*/ |
|
static String[] toStringArray(List<ProtocolVersion> protocolVersions) { |
|
if ((protocolVersions != null) && !protocolVersions.isEmpty()) { |
|
String[] protocolNames = new String[protocolVersions.size()]; |
|
int i = 0; |
|
for (ProtocolVersion pv : protocolVersions) { |
|
protocolNames[i++] = pv.name; |
|
} |
|
|
|
return protocolNames; |
|
} |
|
|
|
return new String[0]; |
|
} |
|
|
|
|
|
|
|
*/ |
|
static String[] toStringArray(int[] protocolVersions) { |
|
if ((protocolVersions != null) && protocolVersions.length != 0) { |
|
String[] protocolNames = new String[protocolVersions.length]; |
|
int i = 0; |
|
for (int pv : protocolVersions) { |
|
protocolNames[i++] = ProtocolVersion.nameOf(pv); |
|
} |
|
|
|
return protocolNames; |
|
} |
|
|
|
return new String[0]; |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
static List<ProtocolVersion> namesOf(String[] protocolNames) { |
|
if (protocolNames == null || protocolNames.length == 0) { |
|
return Collections.<ProtocolVersion>emptyList(); |
|
} |
|
|
|
List<ProtocolVersion> pvs = new ArrayList<>(protocolNames.length); |
|
for (String pn : protocolNames) { |
|
ProtocolVersion pv = ProtocolVersion.nameOf(pn); |
|
if (pv == null) { |
|
throw new IllegalArgumentException( |
|
"Unsupported protocol" + pn); |
|
} |
|
|
|
pvs.add(pv); |
|
} |
|
|
|
return Collections.unmodifiableList(pvs); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
static boolean useTLS12PlusSpec(String name) { |
|
ProtocolVersion pv = ProtocolVersion.nameOf(name); |
|
if (pv != null && pv != NONE) { |
|
return pv.id >= TLS12.id; |
|
} |
|
|
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
int compare(ProtocolVersion that) { |
|
if (this == that) { |
|
return 0; |
|
} |
|
|
|
if (this == ProtocolVersion.NONE) { |
|
return -1; |
|
} else if (that == ProtocolVersion.NONE) { |
|
return 1; |
|
} |
|
|
|
return this.id - that.id; |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
boolean useTLS13PlusSpec() { |
|
return this.id >= TLS13.id; |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
boolean useTLS12PlusSpec() { |
|
return this.id >= TLS12.id; |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
boolean useTLS11PlusSpec() { |
|
return this.id >= TLS11.id; |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
boolean useTLS10PlusSpec() { |
|
return this.id >= TLS10.id; |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
static boolean useTLS10PlusSpec(int id) { |
|
return id >= TLS10.id; |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
static boolean useTLS13PlusSpec(int id) { |
|
return id >= TLS13.id; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
static ProtocolVersion selectedFrom( |
|
List<ProtocolVersion> listedVersions, int suggestedVersion) { |
|
ProtocolVersion selectedVersion = ProtocolVersion.NONE; |
|
for (ProtocolVersion pv : listedVersions) { |
|
if (pv.id == suggestedVersion) { |
|
return pv; |
|
} else { |
|
if (pv.id < suggestedVersion && pv.id > selectedVersion.id) { |
|
selectedVersion = pv; |
|
} |
|
} |
|
} |
|
|
|
return selectedVersion; |
|
} |
|
} |