< prev index next >

src/java.base/share/classes/sun/security/ssl/Utilities.java

Print this page


   1 /*
   2  * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  44     /**
  45      * Puts {@code hostname} into the {@code serverNames} list.
  46      * <P>
  47      * If the {@code serverNames} does not look like a legal FQDN, it will
  48      * not be put into the returned list.
  49      * <P>
  50      * Note that the returned list does not allow duplicated name type.
  51      *
  52      * @return a list of {@link SNIServerName}
  53      */
  54     static List<SNIServerName> addToSNIServerNameList(
  55             List<SNIServerName> serverNames, String hostname) {
  56 
  57         SNIHostName sniHostName = rawToSNIHostName(hostname);
  58         if (sniHostName == null) {
  59             return serverNames;
  60         }
  61 
  62         int size = serverNames.size();
  63         List<SNIServerName> sniList = (size != 0) ?
  64                 new ArrayList<>(serverNames) :
  65                 new ArrayList<>(1);
  66 
  67         boolean reset = false;
  68         for (int i = 0; i < size; i++) {
  69             SNIServerName serverName = sniList.get(i);
  70             if (serverName.getType() == StandardConstants.SNI_HOST_NAME) {
  71                 sniList.set(i, sniHostName);
  72                 if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
  73                      SSLLogger.fine(
  74                         "the previous server name in SNI (" + serverName +
  75                         ") was replaced with (" + sniHostName + ")");
  76                 }
  77                 reset = true;
  78                 break;
  79             }
  80         }
  81 
  82         if (!reset) {
  83             sniList.add(sniHostName);
  84         }
  85 


 130         String b = GetPropertyAction.privilegedGetProperty(propName);
 131         if (b == null) {
 132             return defaultValue;
 133         } else if (b.equalsIgnoreCase("false")) {
 134             return false;
 135         } else if (b.equalsIgnoreCase("true")) {
 136             return true;
 137         } else {
 138             throw new RuntimeException("Value of " + propName
 139                 + " must either be 'true' or 'false'");
 140         }
 141     }
 142 
 143     static String indent(String source) {
 144         return Utilities.indent(source, indent);
 145     }
 146 
 147     static String indent(String source, String prefix) {
 148         StringBuilder builder = new StringBuilder();
 149         if (source == null) {
 150              builder.append("\n").append(prefix).append("<blank message>");
 151         } else {
 152             String[] lines = lineBreakPatern.split(source);
 153             boolean isFirst = true;
 154             for (String line : lines) {
 155                 if (isFirst) {
 156                     isFirst = false;
 157                 } else {
 158                     builder.append("\n");
 159                 }
 160                 builder.append(prefix).append(line);
 161             }
 162         }
 163 
 164         return builder.toString();
 165     }
 166 
 167     static String toHexString(byte b) {
 168         return String.valueOf(hexDigits[(b >> 4) & 0x0F]) +
 169                 String.valueOf(hexDigits[b & 0x0F]);
 170     }


 214         builder.reverse();
 215 
 216         return builder.toString();
 217     }
 218 
 219     /**
 220      * Utility method to convert a BigInteger to a byte array in unsigned
 221      * format as needed in the handshake messages. BigInteger uses
 222      * 2's complement format, i.e. it prepends an extra zero if the MSB
 223      * is set. We remove that.
 224      */
 225     static byte[] toByteArray(BigInteger bi) {
 226         byte[] b = bi.toByteArray();
 227         if ((b.length > 1) && (b[0] == 0)) {
 228             int n = b.length - 1;
 229             byte[] newarray = new byte[n];
 230             System.arraycopy(b, 1, newarray, 0, n);
 231             b = newarray;
 232         }
 233         return b;
 234     }
 235 
 236     static void reverseBytes(byte[] arr) {
 237         int i = 0;
 238         int j = arr.length - 1;
 239 
 240         while (i < j) {
 241             swap(arr, i, j);
 242             i++;
 243             j--;
 244         }
 245     }
 246 
 247     private static void swap(byte[] arr, int i, int j) {
 248         byte tmp = arr[i];
 249         arr[i] = arr[j];
 250         arr[j] = tmp;
 251     }
 252 }
   1 /*
   2  * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  44     /**
  45      * Puts {@code hostname} into the {@code serverNames} list.
  46      * <P>
  47      * If the {@code serverNames} does not look like a legal FQDN, it will
  48      * not be put into the returned list.
  49      * <P>
  50      * Note that the returned list does not allow duplicated name type.
  51      *
  52      * @return a list of {@link SNIServerName}
  53      */
  54     static List<SNIServerName> addToSNIServerNameList(
  55             List<SNIServerName> serverNames, String hostname) {
  56 
  57         SNIHostName sniHostName = rawToSNIHostName(hostname);
  58         if (sniHostName == null) {
  59             return serverNames;
  60         }
  61 
  62         int size = serverNames.size();
  63         List<SNIServerName> sniList = (size != 0) ?
  64                 new ArrayList<SNIServerName>(serverNames) :
  65                 new ArrayList<SNIServerName>(1);
  66 
  67         boolean reset = false;
  68         for (int i = 0; i < size; i++) {
  69             SNIServerName serverName = sniList.get(i);
  70             if (serverName.getType() == StandardConstants.SNI_HOST_NAME) {
  71                 sniList.set(i, sniHostName);
  72                 if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
  73                      SSLLogger.fine(
  74                         "the previous server name in SNI (" + serverName +
  75                         ") was replaced with (" + sniHostName + ")");
  76                 }
  77                 reset = true;
  78                 break;
  79             }
  80         }
  81 
  82         if (!reset) {
  83             sniList.add(sniHostName);
  84         }
  85 


 130         String b = GetPropertyAction.privilegedGetProperty(propName);
 131         if (b == null) {
 132             return defaultValue;
 133         } else if (b.equalsIgnoreCase("false")) {
 134             return false;
 135         } else if (b.equalsIgnoreCase("true")) {
 136             return true;
 137         } else {
 138             throw new RuntimeException("Value of " + propName
 139                 + " must either be 'true' or 'false'");
 140         }
 141     }
 142 
 143     static String indent(String source) {
 144         return Utilities.indent(source, indent);
 145     }
 146 
 147     static String indent(String source, String prefix) {
 148         StringBuilder builder = new StringBuilder();
 149         if (source == null) {
 150              builder.append("\n" + prefix + "<blank message>");
 151         } else {
 152             String[] lines = lineBreakPatern.split(source);
 153             boolean isFirst = true;
 154             for (String line : lines) {
 155                 if (isFirst) {
 156                     isFirst = false;
 157                 } else {
 158                     builder.append("\n");
 159                 }
 160                 builder.append(prefix).append(line);
 161             }
 162         }
 163 
 164         return builder.toString();
 165     }
 166 
 167     static String toHexString(byte b) {
 168         return String.valueOf(hexDigits[(b >> 4) & 0x0F]) +
 169                 String.valueOf(hexDigits[b & 0x0F]);
 170     }


 214         builder.reverse();
 215 
 216         return builder.toString();
 217     }
 218 
 219     /**
 220      * Utility method to convert a BigInteger to a byte array in unsigned
 221      * format as needed in the handshake messages. BigInteger uses
 222      * 2's complement format, i.e. it prepends an extra zero if the MSB
 223      * is set. We remove that.
 224      */
 225     static byte[] toByteArray(BigInteger bi) {
 226         byte[] b = bi.toByteArray();
 227         if ((b.length > 1) && (b[0] == 0)) {
 228             int n = b.length - 1;
 229             byte[] newarray = new byte[n];
 230             System.arraycopy(b, 1, newarray, 0, n);
 231             b = newarray;
 232         }
 233         return b;

















 234     }
 235 }
< prev index next >