changeset 16524:2253b5ac1726

8046294: Generate the 4-byte timestamp randomly Reviewed-by: jnimeh, weijun
author xuelei
date Mon, 23 May 2016 10:51:21 +0000
parents 9f8e6c4bc12f
children 0735c35f15b3
files src/java.base/share/classes/sun/security/ssl/RandomCookie.java src/java.base/share/classes/sun/security/ssl/Utilities.java
diffstat 2 files changed, 15 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/sun/security/ssl/RandomCookie.java	Mon May 23 18:18:20 2016 +0800
+++ b/src/java.base/share/classes/sun/security/ssl/RandomCookie.java	Mon May 23 10:51:21 2016 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2016, 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
@@ -41,21 +41,8 @@
     byte[] random_bytes;  // exactly 32 bytes
 
     RandomCookie(SecureRandom generator) {
-        long temp = System.currentTimeMillis() / 1000;
-        int gmt_unix_time;
-        if (temp < Integer.MAX_VALUE) {
-            gmt_unix_time = (int) temp;
-        } else {
-            gmt_unix_time = Integer.MAX_VALUE;          // Whoops!
-        }
-
         random_bytes = new byte[32];
         generator.nextBytes(random_bytes);
-
-        random_bytes[0] = (byte)(gmt_unix_time >> 24);
-        random_bytes[1] = (byte)(gmt_unix_time >> 16);
-        random_bytes[2] = (byte)(gmt_unix_time >>  8);
-        random_bytes[3] = (byte)gmt_unix_time;
     }
 
     RandomCookie(HandshakeInStream m) throws IOException {
@@ -68,22 +55,15 @@
     }
 
     void print(PrintStream s) {
-        int i, gmt_unix_time;
-
-        gmt_unix_time = ((random_bytes[0] & 0xFF) << 24) |
-                        ((random_bytes[1] & 0xFF) << 16) |
-                        ((random_bytes[2] & 0xFF) << 8) |
-                         (random_bytes[3] & 0xFF);
-
-        s.print("GMT: " + gmt_unix_time + " ");
-        s.print("bytes = { ");
-
-        for (i = 4; i < 32; i++) {
-            if (i != 4) {
-                s.print(", ");
+        s.print("random_bytes = {");
+        for (int i = 0; i < 32; i++) {
+            int k = random_bytes[i] & 0xFF;
+            if (i != 0) {
+                s.print(' ');
             }
-            s.print(random_bytes[i] & 0x0ff);
+            s.print(Utilities.hexDigits[k >>> 4]);
+            s.print(Utilities.hexDigits[k & 0xf]);
         }
-        s.println(" }");
+        s.println("}");
     }
 }
--- a/src/java.base/share/classes/sun/security/ssl/Utilities.java	Mon May 23 18:18:20 2016 +0800
+++ b/src/java.base/share/classes/sun/security/ssl/Utilities.java	Mon May 23 10:51:21 2016 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2016, 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
@@ -34,6 +34,11 @@
  */
 final class Utilities {
     /**
+     * hex digits
+     */
+    static final char[] hexDigits = "0123456789ABCDEF".toCharArray();
+
+    /**
      * Puts {@code hostname} into the {@code serverNames} list.
      * <P>
      * If the {@code serverNames} does not look like a legal FQDN, it will