| 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 */  | 
 | 
 | 
 | 
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 { | 
 | 
    static final char[] hexDigits = "0123456789ABCDEF".toCharArray();  | 
 | 
    private static final String indent = "  ";  | 
 | 
    private static final Pattern lineBreakPatern =  | 
 | 
                Pattern.compile("\\r\\n|\\n|\\r"); | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
     */  | 
 | 
    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<SNIServerName>(serverNames) :  | 
 | 
                new ArrayList<SNIServerName>(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" + prefix + "<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 toHexString(byte b) { | 
 | 
        return String.valueOf(hexDigits[(b >> 4) & 0x0F]) +  | 
 | 
                String.valueOf(hexDigits[b & 0x0F]);  | 
 | 
    }  | 
 | 
 | 
 | 
    static String byte16HexString(int id) { | 
 | 
        return "0x" +  | 
 | 
                hexDigits[(id >> 12) & 0x0F] + hexDigits[(id >> 8) & 0x0F] +  | 
 | 
                hexDigits[(id >> 4) & 0x0F] + hexDigits[id & 0x0F];  | 
 | 
    }  | 
 | 
 | 
 | 
    static String toHexString(byte[] bytes) { | 
 | 
        if (bytes == null || bytes.length == 0) { | 
 | 
            return "";  | 
 | 
        }  | 
 | 
 | 
 | 
        StringBuilder builder = new StringBuilder(bytes.length * 3);  | 
 | 
        boolean isFirst = true;  | 
 | 
        for (byte b : bytes) { | 
 | 
            if (isFirst) { | 
 | 
                isFirst = false;  | 
 | 
            } else { | 
 | 
                builder.append(' '); | 
 | 
            }  | 
 | 
 | 
 | 
            builder.append(hexDigits[(b >> 4) & 0x0F]);  | 
 | 
            builder.append(hexDigits[b & 0x0F]);  | 
 | 
        }  | 
 | 
        return builder.toString();  | 
 | 
    }  | 
 | 
 | 
 | 
    static String toHexString(long lv) { | 
 | 
        StringBuilder builder = new StringBuilder(128);  | 
 | 
 | 
 | 
        boolean isFirst = true;  | 
 | 
        do { | 
 | 
            if (isFirst) { | 
 | 
                isFirst = false;  | 
 | 
            } else { | 
 | 
                builder.append(' '); | 
 | 
            }  | 
 | 
 | 
 | 
            builder.append(hexDigits[(int)(lv & 0x0F)]);  | 
 | 
            lv >>>= 4;  | 
 | 
            builder.append(hexDigits[(int)(lv & 0x0F)]);  | 
 | 
            lv >>>= 4;  | 
 | 
        } 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;  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
 | 
 | 
     */  | 
 | 
    private static void rangeCheck(int arrayLength, int fromIndex, int toIndex) { | 
 | 
        if (fromIndex > toIndex) { | 
 | 
            throw new IllegalArgumentException(  | 
 | 
                    "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")"); | 
 | 
        }  | 
 | 
        if (fromIndex < 0) { | 
 | 
            throw new ArrayIndexOutOfBoundsException(fromIndex);  | 
 | 
        }  | 
 | 
        if (toIndex > arrayLength) { | 
 | 
            throw new ArrayIndexOutOfBoundsException(toIndex);  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
 | 
 | 
     */  | 
 | 
    static boolean equals(byte[] arr1, int st1, int end1, byte[] arr2, int st2, int end2) { | 
 | 
        rangeCheck(arr1.length, st1, end1);  | 
 | 
        rangeCheck(arr2.length, st2, end2);  | 
 | 
 | 
 | 
        int aLength = end1 - st1;  | 
 | 
        int bLength = end2 - st2;  | 
 | 
        if (aLength != bLength)  | 
 | 
            return false;  | 
 | 
 | 
 | 
        for(int i=0; i<aLength; i++)  | 
 | 
            if(arr1[i + st1] != arr2[i + st2])  | 
 | 
                return false;  | 
 | 
        return true;  | 
 | 
    }  | 
 | 
 | 
 | 
}  |