src/java.base/share/classes/sun/security/ssl/RandomCookie.java
author xuelei
Wed, 06 Jun 2018 09:39:14 -0700
branchJDK-8145252-TLS13-branch
changeset 56683 cf2370de8673
parent 56614 1fc6a8df1958
permissions -rw-r--r--
Arrays::equals is exclusive, fix RandomCookie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
     2
 * Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    29
import java.nio.ByteBuffer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.SecureRandom;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    31
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * RandomCookie ... SSL hands standard format random cookies (nonces)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * around.  These know how to encode/decode themselves on SSL streams,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * and can be created and printed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
final class RandomCookie {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    41
    final byte[] randomBytes = new byte[32];   // exactly 32 bytes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    43
    private static final byte[] hrrRandomBytes = new byte[] {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    44
            (byte)0xCF, (byte)0x21, (byte)0xAD, (byte)0x74,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    45
            (byte)0xE5, (byte)0x9A, (byte)0x61, (byte)0x11,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    46
            (byte)0xBE, (byte)0x1D, (byte)0x8C, (byte)0x02,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    47
            (byte)0x1E, (byte)0x65, (byte)0xB8, (byte)0x91,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    48
            (byte)0xC2, (byte)0xA2, (byte)0x11, (byte)0x16,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    49
            (byte)0x7A, (byte)0xBB, (byte)0x8C, (byte)0x5E,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    50
            (byte)0x07, (byte)0x9E, (byte)0x09, (byte)0xE2,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    51
            (byte)0xC8, (byte)0xA8, (byte)0x33, (byte)0x9C
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    52
        };
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    53
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    54
    private static final byte[] t12Protection = new byte[] {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    55
            (byte)0x44, (byte)0x4F, (byte)0x57, (byte)0x4E,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    56
            (byte)0x47, (byte)0x52, (byte)0x44, (byte)0x01
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    57
        };
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    58
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    59
    private static final byte[] t11Protection = new byte[] {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    60
            (byte)0x44, (byte)0x4F, (byte)0x57, (byte)0x4E,
56614
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    61
            (byte)0x47, (byte)0x52, (byte)0x44, (byte)0x00
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    62
        };
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    63
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    64
    static final RandomCookie hrrRandom = new RandomCookie(hrrRandomBytes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    RandomCookie(SecureRandom generator) {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    67
        generator.nextBytes(randomBytes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
56614
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    70
    // Used for server random generation with version downgrade protection.
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    71
    RandomCookie(HandshakeContext context) {
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    72
        SecureRandom generator = context.sslContext.getSecureRandom();
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    73
        generator.nextBytes(randomBytes);
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    74
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    75
        // TLS 1.3 has a downgrade protection mechanism embedded in the
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    76
        // server's random value.  TLS 1.3 servers which negotiate TLS 1.2
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    77
        // or below in response to a ClientHello MUST set the last eight
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    78
        // bytes of their Random value specially.
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    79
        byte[] protection = null;
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    80
        if (context.maximumActiveProtocol.useTLS13PlusSpec()) {
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    81
            if (!context.negotiatedProtocol.useTLS13PlusSpec()) {
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    82
                if (context.negotiatedProtocol.useTLS12PlusSpec()) {
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    83
                    protection = t12Protection;
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    84
                } else {
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    85
                    protection = t11Protection;
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    86
                }
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    87
            }
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    88
        } else if (context.maximumActiveProtocol.useTLS12PlusSpec()) {
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    89
            if (!context.negotiatedProtocol.useTLS12PlusSpec()) {
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    90
                protection = t11Protection;
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    91
            }
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    92
        }
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    93
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    94
        if (protection != null) {
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    95
            System.arraycopy(protection, 0, randomBytes,
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    96
                    randomBytes.length - protection.length, protection.length);
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    97
        }
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    98
    }
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
    99
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   100
    RandomCookie(ByteBuffer m) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   101
        m.get(randomBytes);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   102
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   103
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   104
    private RandomCookie(byte[] randomBytes) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   105
        System.arraycopy(randomBytes, 0, this.randomBytes, 0, 32);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   106
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   107
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   108
    @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   109
    public String toString() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   110
        return "random_bytes = {" + Utilities.toHexString(randomBytes) + "}";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   113
    boolean isHelloRetryRequest() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   114
        return Arrays.equals(hrrRandomBytes, randomBytes);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   115
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   116
56614
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
   117
    // Used for client random validation of version downgrade protection.
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
   118
    boolean isVersionDowngrade(HandshakeContext context) {
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
   119
        if (context.maximumActiveProtocol.useTLS13PlusSpec()) {
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
   120
            if (!context.negotiatedProtocol.useTLS13PlusSpec()) {
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
   121
                return isT12Downgrade() || isT11Downgrade();
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
   122
            }
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
   123
        } else if (context.maximumActiveProtocol.useTLS12PlusSpec()) {
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
   124
            if (!context.negotiatedProtocol.useTLS12PlusSpec()) {
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
   125
                return isT11Downgrade();
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
   126
            }
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
   127
        }
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
   128
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
   129
        return false;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   130
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   131
56614
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
   132
    private boolean isT12Downgrade() {
56683
cf2370de8673 Arrays::equals is exclusive, fix RandomCookie
xuelei
parents: 56614
diff changeset
   133
        return Arrays.equals(randomBytes, 24, 32, t12Protection, 0, 8);
56614
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
   134
    }
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
   135
1fc6a8df1958 implement version downgrad protection
xuelei
parents: 56542
diff changeset
   136
    private boolean isT11Downgrade() {
56683
cf2370de8673 Arrays::equals is exclusive, fix RandomCookie
xuelei
parents: 56614
diff changeset
   137
        return Arrays.equals(randomBytes, 24, 32, t11Protection, 0, 8);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
}