|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package sun.security.ssl; |
|
|
|
import java.math.BigInteger; |
|
import java.util.*; |
|
import java.util.regex.Pattern; |
|
import javax.net.ssl.*; |
|
import sun.net.util.IPAddressUtil; |
|
import sun.security.action.GetPropertyAction; |
|
|
|
|
|
|
|
*/ |
|
final class Utilities { |
|
private static final String indent = " "; |
|
private static final Pattern lineBreakPatern = |
|
Pattern.compile("\\r\\n|\\n|\\r"); |
|
private static final HexFormat HEX_FORMATTER = |
|
HexFormat.of().withUpperCase(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
static List<SNIServerName> addToSNIServerNameList( |
|
List<SNIServerName> serverNames, String hostname) { |
|
|
|
SNIHostName sniHostName = rawToSNIHostName(hostname); |
|
if (sniHostName == null) { |
|
return serverNames; |
|
} |
|
|
|
int size = serverNames.size(); |
|
List<SNIServerName> sniList = (size != 0) ? |
|
new ArrayList<>(serverNames) : |
|
new ArrayList<>(1); |
|
|
|
boolean reset = false; |
|
for (int i = 0; i < size; i++) { |
|
SNIServerName serverName = sniList.get(i); |
|
if (serverName.getType() == StandardConstants.SNI_HOST_NAME) { |
|
sniList.set(i, sniHostName); |
|
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
SSLLogger.fine( |
|
"the previous server name in SNI (" + serverName + |
|
") was replaced with (" + sniHostName + ")"); |
|
} |
|
reset = true; |
|
break; |
|
} |
|
} |
|
|
|
if (!reset) { |
|
sniList.add(sniHostName); |
|
} |
|
|
|
return Collections.<SNIServerName>unmodifiableList(sniList); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private static SNIHostName rawToSNIHostName(String hostname) { |
|
SNIHostName sniHostName = null; |
|
if (hostname != null && hostname.indexOf('.') > 0 && |
|
!hostname.endsWith(".") && |
|
!IPAddressUtil.isIPv4LiteralAddress(hostname) && |
|
!IPAddressUtil.isIPv6LiteralAddress(hostname)) { |
|
|
|
try { |
|
sniHostName = new SNIHostName(hostname); |
|
} catch (IllegalArgumentException iae) { |
|
|
|
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
SSLLogger.fine(hostname + "\" " + |
|
"is not a legal HostName for server name indication"); |
|
} |
|
} |
|
} |
|
|
|
return sniHostName; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
static boolean getBooleanProperty(String propName, boolean defaultValue) { |
|
|
|
String b = GetPropertyAction.privilegedGetProperty(propName); |
|
if (b == null) { |
|
return defaultValue; |
|
} else if (b.equalsIgnoreCase("false")) { |
|
return false; |
|
} else if (b.equalsIgnoreCase("true")) { |
|
return true; |
|
} else { |
|
throw new RuntimeException("Value of " + propName |
|
+ " must either be 'true' or 'false'"); |
|
} |
|
} |
|
|
|
static String indent(String source) { |
|
return Utilities.indent(source, indent); |
|
} |
|
|
|
static String indent(String source, String prefix) { |
|
StringBuilder builder = new StringBuilder(); |
|
if (source == null) { |
|
builder.append("\n").append(prefix).append("<blank message>"); |
|
} else { |
|
String[] lines = lineBreakPatern.split(source); |
|
boolean isFirst = true; |
|
for (String line : lines) { |
|
if (isFirst) { |
|
isFirst = false; |
|
} else { |
|
builder.append("\n"); |
|
} |
|
builder.append(prefix).append(line); |
|
} |
|
} |
|
|
|
return builder.toString(); |
|
} |
|
|
|
static String byte16HexString(int id) { |
|
return "0x" + HEX_FORMATTER.toHexDigits((short)id); |
|
} |
|
|
|
static String toHexString(byte[] bytes) { |
|
if (bytes == null || bytes.length == 0) { |
|
return ""; |
|
} |
|
|
|
return HEX_FORMATTER.formatHex(bytes); |
|
} |
|
|
|
static String toHexString(long lv) { |
|
StringBuilder builder = new StringBuilder(128); |
|
|
|
boolean isFirst = true; |
|
do { |
|
if (isFirst) { |
|
isFirst = false; |
|
} else { |
|
builder.append(' '); |
|
} |
|
|
|
HEX_FORMATTER.toHexDigits(builder, (byte)lv); |
|
lv >>>= 8; |
|
} while (lv != 0); |
|
builder.reverse(); |
|
|
|
return builder.toString(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
static byte[] toByteArray(BigInteger bi) { |
|
byte[] b = bi.toByteArray(); |
|
if ((b.length > 1) && (b[0] == 0)) { |
|
int n = b.length - 1; |
|
byte[] newarray = new byte[n]; |
|
System.arraycopy(b, 1, newarray, 0, n); |
|
b = newarray; |
|
} |
|
return b; |
|
} |
|
|
|
static void reverseBytes(byte[] arr) { |
|
int i = 0; |
|
int j = arr.length - 1; |
|
|
|
while (i < j) { |
|
swap(arr, i, j); |
|
i++; |
|
j--; |
|
} |
|
} |
|
|
|
private static void swap(byte[] arr, int i, int j) { |
|
byte tmp = arr[i]; |
|
arr[i] = arr[j]; |
|
arr[j] = tmp; |
|
} |
|
} |