Path: blob/master/src/java.base/share/classes/sun/security/ssl/HelloRequest.java
41159 views
/*1* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.ssl;2627import java.io.IOException;28import java.nio.ByteBuffer;29import sun.security.ssl.SSLHandshake.HandshakeMessage;3031/**32* Pack of the HelloRequest handshake message.33*/34final class HelloRequest {35static final SSLProducer kickstartProducer =36new HelloRequestKickstartProducer();3738static final SSLConsumer handshakeConsumer =39new HelloRequestConsumer();40static final HandshakeProducer handshakeProducer =41new HelloRequestProducer();4243/**44* The HelloRequest handshake message.45*46* [RFC 5246] The HelloRequest message MAY be sent by the server at any47* time. HelloRequest is a simple notification that the client should48* begin the negotiation process anew.49*50* struct { } HelloRequest;51*/52static final class HelloRequestMessage extends HandshakeMessage {53HelloRequestMessage(HandshakeContext handshakeContext) {54super(handshakeContext);55}5657HelloRequestMessage(HandshakeContext handshakeContext,58ByteBuffer m) throws IOException {59super(handshakeContext);60if (m.hasRemaining()) {61throw handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,62"Error parsing HelloRequest message: not empty");63}64}6566@Override67public SSLHandshake handshakeType() {68return SSLHandshake.HELLO_REQUEST;69}7071@Override72public int messageLength() {73return 0;74}7576@Override77public void send(HandshakeOutStream s) throws IOException {78// empty, nothing to send79}8081@Override82public String toString() {83return "<empty>";84}85}8687/**88* The "HelloRequest" handshake message kick start producer.89*/90private static final91class HelloRequestKickstartProducer implements SSLProducer {92// Prevent instantiation of this class.93private HelloRequestKickstartProducer() {94// blank95}9697@Override98public byte[] produce(ConnectionContext context) throws IOException {99// The producing happens in server side only.100ServerHandshakeContext shc = (ServerHandshakeContext)context;101102HelloRequestMessage hrm = new HelloRequestMessage(shc);103if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {104SSLLogger.fine("Produced HelloRequest handshake message", hrm);105}106107// Output the handshake message.108hrm.write(shc.handshakeOutput);109shc.handshakeOutput.flush();110111// update the context112113// What's the expected response?114shc.handshakeConsumers.put(115SSLHandshake.CLIENT_HELLO.id, SSLHandshake.CLIENT_HELLO);116117// The handshake message has been delivered.118return null;119}120}121122/**123* The "HelloRequest" handshake message producer.124*/125private static final class HelloRequestProducer126implements HandshakeProducer {127// Prevent instantiation of this class.128private HelloRequestProducer() {129// blank130}131132@Override133public byte[] produce(ConnectionContext context,134HandshakeMessage message) throws IOException {135// The producing happens in server side only.136ServerHandshakeContext shc = (ServerHandshakeContext)context;137138HelloRequestMessage hrm = new HelloRequestMessage(shc);139if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {140SSLLogger.fine("Produced HelloRequest handshake message", hrm);141}142143// Output the handshake message.144hrm.write(shc.handshakeOutput);145shc.handshakeOutput.flush();146147// update the context148149// What's the expected response?150shc.handshakeConsumers.put(151SSLHandshake.CLIENT_HELLO.id, SSLHandshake.CLIENT_HELLO);152153// The handshake message has been delivered.154return null;155}156}157158/**159* The "HelloRequest" handshake message consumer.160*/161private static final class HelloRequestConsumer162implements SSLConsumer {163164// Prevent instantiation of this class.165private HelloRequestConsumer() {166// blank167}168169@Override170public void consume(ConnectionContext context,171ByteBuffer message) throws IOException {172// The consuming happens in client side only.173ClientHandshakeContext chc = (ClientHandshakeContext)context;174175// For TLS 1.2 and prior versions, the HelloRequest message MAY176// be sent by the server at any time. Please don't clean up this177// handshake consumer.178HelloRequestMessage hrm = new HelloRequestMessage(chc, message);179if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {180SSLLogger.fine(181"Consuming HelloRequest handshake message", hrm);182}183184if (!chc.kickstartMessageDelivered) {185if (!chc.conContext.secureRenegotiation &&186!HandshakeContext.allowUnsafeRenegotiation) {187throw chc.conContext.fatal(Alert.HANDSHAKE_FAILURE,188"Unsafe renegotiation is not allowed");189}190191if (!chc.conContext.secureRenegotiation) {192if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {193SSLLogger.warning(194"Continue with insecure renegotiation");195}196}197198// update the responders199chc.handshakeProducers.put(200SSLHandshake.CLIENT_HELLO.id,201SSLHandshake.CLIENT_HELLO);202203//204// produce response handshake message205//206SSLHandshake.CLIENT_HELLO.produce(context, hrm);207} else {208if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {209SSLLogger.fine(210"Ingore HelloRequest, handshaking is in progress");211}212}213}214}215}216217218219