src/java.base/share/classes/sun/security/ssl/KeyUpdate.java
author xuelei
Sat, 02 Jun 2018 21:23:02 -0700
branchJDK-8145252-TLS13-branch
changeset 56660 66c803c3ce32
parent 56544 ad120e0dfcfb
child 56686 07dc566630ee
permissions -rw-r--r--
key and iv implementation update, and two tests correction
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     1
/*
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     4
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    10
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    15
 * accompanied this code).
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    16
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    20
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    23
 * questions.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    24
 */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    25
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    26
package sun.security.ssl;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    27
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    28
import java.io.IOException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    29
import java.nio.ByteBuffer;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    30
import java.security.GeneralSecurityException;
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
    31
import java.text.MessageFormat;
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
    32
import java.util.Locale;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    33
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    34
import sun.security.ssl.SSLHandshake.HandshakeMessage;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    35
import sun.security.ssl.SSLCipher.SSLReadCipher;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    36
import sun.security.ssl.SSLCipher.SSLWriteCipher;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    37
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    38
import javax.crypto.SecretKey;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    39
import javax.crypto.spec.IvParameterSpec;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    40
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    41
/**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    42
 * Pack of the KeyUpdate handshake message.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    43
 */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    44
final class KeyUpdate {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    45
    static final SSLProducer kickstartProducer =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    46
        new KeyUpdateKickstartProducer();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    47
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    48
    static final SSLConsumer handshakeConsumer =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    49
        new KeyUpdateConsumer();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    50
    static final HandshakeProducer handshakeProducer =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    51
        new KeyUpdateProducer();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    52
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    53
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    54
     * The KeyUpdate handshake message.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    55
     *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    56
     * The KeyUpdate handshake message is used to indicate that the sender is
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    57
     * updating its sending cryptographic keys.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    58
     *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    59
     *       enum {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    60
     *           update_not_requested(0), update_requested(1), (255)
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    61
     *       } KeyUpdateRequest;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    62
     *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    63
     *       struct {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    64
     *           KeyUpdateRequest request_update;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    65
     *       } KeyUpdate;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    66
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    67
    static final class KeyUpdateMessage extends HandshakeMessage {
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
    68
        private final KeyUpdateRequest status;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    69
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
    70
        KeyUpdateMessage(PostHandshakeContext context,
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
    71
                KeyUpdateRequest status) {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    72
            super(context);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    73
            this.status = status;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    74
        }
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
    75
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
    76
        KeyUpdateMessage(PostHandshakeContext context,
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
    77
                ByteBuffer m) throws IOException {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    78
            super(context);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    79
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    80
            if (m.remaining() != 1) {
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
    81
                context.conContext.fatal(Alert.ILLEGAL_PARAMETER,
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
    82
                        "KeyUpdate has an unexpected length of "+
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    83
                        m.remaining());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    84
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    85
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
    86
            byte request = m.get();
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
    87
            this.status = KeyUpdateRequest.valueOf(request);
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
    88
            if (status == null) {
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
    89
                context.conContext.fatal(Alert.ILLEGAL_PARAMETER,
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
    90
                        "Invalid KeyUpdate message value: " +
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
    91
                        KeyUpdateRequest.nameOf(request));
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    92
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    93
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    94
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    95
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    96
        public SSLHandshake handshakeType() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    97
            return SSLHandshake.KEY_UPDATE;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    98
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    99
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   100
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   101
        public int messageLength() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   102
            // one byte enum
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   103
            return 1;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   104
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   105
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   106
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   107
        public void send(HandshakeOutStream s) throws IOException {
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   108
            s.putInt8(status.id);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   109
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   110
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   111
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   112
        public String toString() {
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   113
            MessageFormat messageFormat = new MessageFormat(
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   114
                    "\"KeyUpdate\": '{'\n" +
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   115
                    "  \"request_update\": {0}\n" +
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   116
                    "'}'",
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   117
                    Locale.ENGLISH);
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   118
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   119
            Object[] messageFields = {
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   120
                status.name
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   121
            };
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   122
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   123
            return messageFormat.format(messageFields);
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   124
        }
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   125
    }
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   126
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   127
    enum KeyUpdateRequest {
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   128
        NOTREQUSTED         ((byte)0, "update_not_requested"),
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   129
        REQUSTED            ((byte)1, "update_requested");
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   130
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   131
        final byte id;
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   132
        final String name;
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   133
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   134
        private KeyUpdateRequest(byte id, String name) {
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   135
            this.id = id;
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   136
            this.name = name;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   137
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   138
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   139
        static KeyUpdateRequest valueOf(byte id) {
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   140
            for (KeyUpdateRequest kur : KeyUpdateRequest.values()) {
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   141
                if (kur.id == id) {
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   142
                    return kur;
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   143
                }
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   144
            }
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   145
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   146
            return null;
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   147
        }
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   148
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   149
        static String nameOf(byte id) {
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   150
            for (KeyUpdateRequest kur : KeyUpdateRequest.values()) {
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   151
                if (kur.id == id) {
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   152
                    return kur.name;
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   153
                }
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   154
            }
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   155
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   156
            return "<UNKNOWN KeyUpdateRequest TYPE: " + (id & 0x0FF) + ">";
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   157
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   158
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   159
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   160
    private static final
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   161
            class KeyUpdateKickstartProducer implements SSLProducer {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   162
        // Prevent instantiation of this class.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   163
        private KeyUpdateKickstartProducer() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   164
            // blank
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   165
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   166
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   167
        // Produce kickstart handshake message.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   168
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   169
        public byte[] produce(ConnectionContext context) throws IOException {
56544
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   170
            PostHandshakeContext hc = (PostHandshakeContext)context;
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   171
            return handshakeProducer.produce(context,
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   172
                    new KeyUpdateMessage(hc, KeyUpdateRequest.REQUSTED));
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   173
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   174
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   175
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   176
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   177
     * The "KeyUpdate" handshake message consumer.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   178
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   179
    private static final class KeyUpdateConsumer implements SSLConsumer {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   180
        // Prevent instantiation of this class.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   181
        private KeyUpdateConsumer() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   182
            // blank
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   183
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   184
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   185
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   186
        public void consume(ConnectionContext context,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   187
                ByteBuffer message) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   188
            // The consuming happens in client side only.
56544
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   189
            PostHandshakeContext hc = (PostHandshakeContext)context;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   190
            KeyUpdateMessage km = new KeyUpdateMessage(hc, message);
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   191
            if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   192
                SSLLogger.fine(
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   193
                        "Consuming KeyUpdate post-handshake message", km);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   194
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   195
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   196
            // Update read key and IV.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   197
            SSLTrafficKeyDerivation kdg =
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   198
                SSLTrafficKeyDerivation.valueOf(hc.conContext.protocolVersion);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   199
            if (kdg == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   200
                // unlikely
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   201
                hc.conContext.fatal(Alert.INTERNAL_ERROR,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   202
                        "Not supported key derivation: " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   203
                                hc.conContext.protocolVersion);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   204
                return;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   205
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   206
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   207
            SSLKeyDerivation skd = kdg.createKeyDerivation(hc,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   208
                    hc.conContext.inputRecord.readCipher.baseSecret);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   209
            if (skd == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   210
                // unlikely
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   211
                hc.conContext.fatal(Alert.INTERNAL_ERROR, "no key derivation");
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   212
                return;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   213
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   214
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   215
            SecretKey nplus1 = skd.deriveKey("TlsUpdateNplus1", null);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   216
            SSLKeyDerivation kd = kdg.createKeyDerivation(hc, nplus1);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   217
            SecretKey key = kd.deriveKey("TlsKey", null);
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   218
            IvParameterSpec ivSpec = new IvParameterSpec(
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   219
                    kd.deriveKey("TlsIv", null).getEncoded());
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   220
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   221
                SSLReadCipher rc =
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   222
                    hc.negotiatedCipherSuite.bulkCipher.createReadCipher(
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   223
                        Authenticator.valueOf(hc.conContext.protocolVersion),
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   224
                        hc.conContext.protocolVersion, key, ivSpec,
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   225
                        hc.sslContext.getSecureRandom());
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   226
                hc.conContext.inputRecord.changeReadCiphers(rc);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   227
                hc.conContext.inputRecord.readCipher.baseSecret = nplus1;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   228
                if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   229
                    SSLLogger.fine("KeyUpdate: read key updated");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   230
                }
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   231
            } catch (GeneralSecurityException gse) {
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   232
                hc.conContext.fatal(Alert.INTERNAL_ERROR,
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   233
                        "Failure to derive read secrets", gse);
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   234
                return;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   235
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   236
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   237
            if (km.status == KeyUpdateRequest.REQUSTED) {
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   238
                // Update the write key and IV.
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   239
                handshakeProducer.produce(hc,
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   240
                    new KeyUpdateMessage(hc, KeyUpdateRequest.NOTREQUSTED));
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   241
            }
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   242
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   243
            // clean handshake context
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   244
            hc.conContext.finishPostHandshake();
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   245
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   246
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   247
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   248
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   249
     * The "KeyUpdate" handshake message producer.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   250
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   251
    private static final class KeyUpdateProducer implements HandshakeProducer {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   252
        // Prevent instantiation of this class.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   253
        private KeyUpdateProducer() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   254
            // blank
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   255
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   256
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   257
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   258
        public byte[] produce(ConnectionContext context,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   259
                HandshakeMessage message) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   260
            // The producing happens in server side only.
56544
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   261
            PostHandshakeContext hc = (PostHandshakeContext)context;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   262
            KeyUpdateMessage km = (KeyUpdateMessage)message;
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   263
            if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   264
                SSLLogger.fine(
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   265
                        "Produced KeyUpdate post-handshake message", km);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   266
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   267
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   268
            // Update the write key and IV.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   269
            SSLTrafficKeyDerivation kdg =
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   270
                SSLTrafficKeyDerivation.valueOf(hc.conContext.protocolVersion);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   271
            if (kdg == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   272
                // unlikely
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   273
                hc.conContext.fatal(Alert.INTERNAL_ERROR,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   274
                        "Not supported key derivation: " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   275
                                hc.conContext.protocolVersion);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   276
                return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   277
            }
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   278
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   279
            SSLKeyDerivation skd = kdg.createKeyDerivation(hc,
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   280
                    hc.conContext.outputRecord.writeCipher.baseSecret);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   281
            if (skd == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   282
                // unlikely
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   283
                hc.conContext.fatal(Alert.INTERNAL_ERROR, "no key derivation");
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   284
                return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   285
            }
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   286
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   287
            SecretKey nplus1 = skd.deriveKey("TlsUpdateNplus1", null);
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   288
            SSLKeyDerivation kd = kdg.createKeyDerivation(hc, nplus1);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   289
            SecretKey key = kd.deriveKey("TlsKey", null);
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   290
            IvParameterSpec ivSpec = new IvParameterSpec(
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   291
                    kd.deriveKey("TlsIv", null).getEncoded());
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   292
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   293
            SSLWriteCipher wc;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   294
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   295
                wc = hc.negotiatedCipherSuite.bulkCipher.createWriteCipher(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   296
                        Authenticator.valueOf(hc.conContext.protocolVersion),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   297
                        hc.conContext.protocolVersion, key, ivSpec,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   298
                        hc.sslContext.getSecureRandom());
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   299
            } catch (GeneralSecurityException gse) {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   300
                hc.conContext.fatal(Alert.INTERNAL_ERROR,
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   301
                        "Failure to derive write secrets", gse);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   302
                return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   303
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   304
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   305
            // Output the handshake message.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   306
            km.write(hc.handshakeOutput);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   307
            hc.handshakeOutput.flush();
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   308
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   309
            // change write cipher
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   310
            hc.conContext.outputRecord.changeWriteCiphers(wc, false);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   311
            hc.conContext.outputRecord.writeCipher.baseSecret = nplus1;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   312
            if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   313
                SSLLogger.fine("KeyUpdate: write key updated");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   314
            }
56660
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   315
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   316
            // clean handshake context
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   317
            hc.conContext.finishPostHandshake();
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   318
66c803c3ce32 key and iv implementation update, and two tests correction
xuelei
parents: 56544
diff changeset
   319
            // The handshake message has been delivered.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   320
            return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   321
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   322
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   323
}