< prev index next >

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

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -21,49 +21,74 @@
  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
 
-
 package sun.security.ssl;
 
 import java.io.*;
+import java.nio.ByteBuffer;
 import java.security.SecureRandom;
+import java.util.Arrays;
 
 /*
  * RandomCookie ... SSL hands standard format random cookies (nonces)
  * around.  These know how to encode/decode themselves on SSL streams,
  * and can be created and printed.
  *
  * @author David Brownell
  */
 final class RandomCookie {
+    final byte[] randomBytes = new byte[32];   // exactly 32 bytes
+
+    private static final byte[] hrrRandomBytes = new byte[] {
+            (byte)0xCF, (byte)0x21, (byte)0xAD, (byte)0x74,
+            (byte)0xE5, (byte)0x9A, (byte)0x61, (byte)0x11,
+            (byte)0xBE, (byte)0x1D, (byte)0x8C, (byte)0x02,
+            (byte)0x1E, (byte)0x65, (byte)0xB8, (byte)0x91,
+            (byte)0xC2, (byte)0xA2, (byte)0x11, (byte)0x16,
+            (byte)0x7A, (byte)0xBB, (byte)0x8C, (byte)0x5E,
+            (byte)0x07, (byte)0x9E, (byte)0x09, (byte)0xE2,
+            (byte)0xC8, (byte)0xA8, (byte)0x33, (byte)0x9C
+        };
+
+    private static final byte[] t12Protection = new byte[] {
+            (byte)0x44, (byte)0x4F, (byte)0x57, (byte)0x4E,
+            (byte)0x47, (byte)0x52, (byte)0x44, (byte)0x01
+        };
+
+    private static final byte[] t11Protection = new byte[] {
+            (byte)0x44, (byte)0x4F, (byte)0x57, (byte)0x4E,
+            (byte)0x47, (byte)0x52, (byte)0x44, (byte)0x01
+        };
 
-    byte[] random_bytes;  // exactly 32 bytes
+    static final RandomCookie hrrRandom = new RandomCookie(hrrRandomBytes);
 
     RandomCookie(SecureRandom generator) {
-        random_bytes = new byte[32];
-        generator.nextBytes(random_bytes);
+        generator.nextBytes(randomBytes);
+    }
+
+    RandomCookie(ByteBuffer m) throws IOException {
+        m.get(randomBytes);
     }
 
-    RandomCookie(HandshakeInStream m) throws IOException {
-        random_bytes = new byte[32];
-        m.read(random_bytes, 0, 32);
+    private RandomCookie(byte[] randomBytes) {
+        System.arraycopy(randomBytes, 0, this.randomBytes, 0, 32);
     }
 
-    void send(HandshakeOutStream out) throws IOException {
-        out.write(random_bytes, 0, 32);
+    @Override
+    public String toString() {
+        return "random_bytes = {" + Utilities.toHexString(randomBytes) + "}";
     }
 
-    void print(PrintStream s) {
-        s.print("random_bytes = {");
-        for (int i = 0; i < 32; i++) {
-            int k = random_bytes[i] & 0xFF;
-            if (i != 0) {
-                s.print(' ');
+    boolean isHelloRetryRequest() {
+        return Arrays.equals(hrrRandomBytes, randomBytes);
             }
-            s.print(Utilities.hexDigits[k >>> 4]);
-            s.print(Utilities.hexDigits[k & 0xf]);
+
+    boolean isT12Downgrade() {
+        return Arrays.equals(hrrRandomBytes, 24, 31, t12Protection, 0, 7);
         }
-        s.println("}");
+
+    boolean isT11Downgrade() {
+        return Arrays.equals(hrrRandomBytes, 24, 31, t11Protection, 0, 7);
     }
 }
< prev index next >