src/java.base/share/classes/sun/security/ssl/PostHandshakeContext.java
author xuelei
Mon, 25 Jun 2018 13:41:39 -0700
changeset 50768 68fa3d4026ea
child 51134 a0de9a3a6766
permissions -rw-r--r--
8196584: TLS 1.3 Implementation Reviewed-by: ascarpino, coffeys, dfuchs, jjiang, jnimeh, mullan, rhalade, ssahoo, valeriep, weijun, wetmore, xuelei Contributed-by: Adam Petcher <adam.petcher@oracle.com>, Amanda Jiang <amanda.jiang@oracle.com>, Anthony Scarpino <anthony.scarpino@oracle.com>, Bradford Wetmore <bradford.wetmore@oracle.com>, Jamil Nimeh <jamil.j.nimeh@oracle.com>, John Jiang <sha.jiang@oracle.com>, Rajan Halade <rajan.halade@oracle.com>, Sibabrata Sahoo <sibabrata.sahoo@oracle.com>, Valerie Peng <valerie.peng@oracle.com>, Weijun Wang <weijun.wang@oracle.com>, Xuelei Fan <xuelei.fan@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
     1
/*
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
     4
 *
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    10
 *
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    15
 * accompanied this code).
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    16
 *
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    20
 *
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    23
 * questions.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    24
 */
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    25
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    26
package sun.security.ssl;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    27
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    28
import java.io.IOException;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    29
import java.nio.ByteBuffer;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    30
import java.util.LinkedHashMap;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    31
import java.util.Map;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    32
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    33
/**
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    34
 * A compact implementation of HandshakeContext for post-handshake messages
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    35
 */
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    36
final class PostHandshakeContext extends HandshakeContext {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    37
    private final static Map<Byte, SSLConsumer> consumers = Map.of(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    38
        SSLHandshake.KEY_UPDATE.id, SSLHandshake.KEY_UPDATE,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    39
        SSLHandshake.NEW_SESSION_TICKET.id, SSLHandshake.NEW_SESSION_TICKET);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    40
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    41
    PostHandshakeContext(TransportContext context) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    42
        super(context);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    43
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    44
        if (!negotiatedProtocol.useTLS13PlusSpec()) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    45
            conContext.fatal(Alert.UNEXPECTED_MESSAGE,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    46
                "Post-handshake not supported in " + negotiatedProtocol.name);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    47
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    48
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    49
        handshakeConsumers = new LinkedHashMap<>(consumers);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    50
        handshakeFinished = true;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    51
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    52
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    53
    @Override
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    54
    void kickstart() throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    55
        SSLHandshake.kickstart(this);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    56
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    57
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    58
    @Override
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    59
    void dispatch(byte handshakeType, ByteBuffer fragment) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    60
        SSLConsumer consumer = handshakeConsumers.get(handshakeType);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    61
        if (consumer == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    62
            conContext.fatal(Alert.UNEXPECTED_MESSAGE,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    63
                    "Unexpected post-handshake message: " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    64
                            SSLHandshake.nameOf(handshakeType));
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    65
            return;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    66
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    67
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    68
        try {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    69
            consumer.consume(this, fragment);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    70
        } catch (UnsupportedOperationException unsoe) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    71
            conContext.fatal(Alert.UNEXPECTED_MESSAGE,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    72
                    "Unsupported post-handshake message: " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    73
                            SSLHandshake.nameOf(handshakeType), unsoe);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    74
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    75
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    76
}