Path: blob/master/src/java.base/share/classes/sun/security/ssl/EncryptedExtensions.java
41159 views
/*1* Copyright (c) 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 java.text.MessageFormat;30import java.util.Locale;31import sun.security.ssl.SSLHandshake.HandshakeMessage;3233/**34* Pack of the EncryptedExtensions handshake message.35*/36final class EncryptedExtensions {37static final HandshakeProducer handshakeProducer =38new EncryptedExtensionsProducer();39static final SSLConsumer handshakeConsumer =40new EncryptedExtensionsConsumer();4142/**43* The EncryptedExtensions handshake message.44*/45static final class EncryptedExtensionsMessage extends HandshakeMessage {46private final SSLExtensions extensions;4748EncryptedExtensionsMessage(49HandshakeContext handshakeContext) throws IOException {50super(handshakeContext);51this.extensions = new SSLExtensions(this);52}5354EncryptedExtensionsMessage(HandshakeContext handshakeContext,55ByteBuffer m) throws IOException {56super(handshakeContext);5758// struct {59// Extension extensions<0..2^16-1>;60// } EncryptedExtensions;61if (m.remaining() < 2) {62throw handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,63"Invalid EncryptedExtensions handshake message: " +64"no sufficient data");65}6667SSLExtension[] encryptedExtensions =68handshakeContext.sslConfig.getEnabledExtensions(69SSLHandshake.ENCRYPTED_EXTENSIONS);70this.extensions = new SSLExtensions(this, m, encryptedExtensions);71}7273@Override74SSLHandshake handshakeType() {75return SSLHandshake.ENCRYPTED_EXTENSIONS;76}7778@Override79int messageLength() {80int extLen = extensions.length();81if (extLen == 0) {82extLen = 2; // empty extensions83}84return extLen;85}8687@Override88void send(HandshakeOutStream hos) throws IOException {89// Is it an empty extensions?90if (extensions.length() == 0) {91hos.putInt16(0);92} else {93extensions.send(hos);94}95}9697@Override98public String toString() {99MessageFormat messageFormat = new MessageFormat(100"\"EncryptedExtensions\": [\n" +101"{0}\n" +102"]",103Locale.ENGLISH);104Object[] messageFields = {105Utilities.indent(extensions.toString())106};107108return messageFormat.format(messageFields);109}110}111112/**113* The EncryptedExtensions handshake message consumer.114*/115private static final class EncryptedExtensionsProducer116implements HandshakeProducer {117// Prevent instantiation of this class.118private EncryptedExtensionsProducer() {119// blank120}121122@Override123public byte[] produce(ConnectionContext context,124HandshakeMessage message) throws IOException {125// The producing happens in server side only.126ServerHandshakeContext shc = (ServerHandshakeContext)context;127128EncryptedExtensionsMessage eem =129new EncryptedExtensionsMessage(shc);130SSLExtension[] extTypes =131shc.sslConfig.getEnabledExtensions(132SSLHandshake.ENCRYPTED_EXTENSIONS,133shc.negotiatedProtocol);134eem.extensions.produce(shc, extTypes);135if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {136SSLLogger.fine("Produced EncryptedExtensions message", eem);137}138139// Output the handshake message.140eem.write(shc.handshakeOutput);141shc.handshakeOutput.flush();142143// The handshake message has been delivered.144return null;145}146}147148/**149* The EncryptedExtensions handshake message consumer.150*/151private static final class EncryptedExtensionsConsumer152implements SSLConsumer {153// Prevent instantiation of this class.154private EncryptedExtensionsConsumer() {155// blank156}157158@Override159public void consume(ConnectionContext context,160ByteBuffer message) throws IOException {161// The consuming happens in client side only.162ClientHandshakeContext chc = (ClientHandshakeContext)context;163164// clean up this consumer165chc.handshakeConsumers.remove(SSLHandshake.ENCRYPTED_EXTENSIONS.id);166167EncryptedExtensionsMessage eem =168new EncryptedExtensionsMessage(chc, message);169if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {170SSLLogger.fine(171"Consuming EncryptedExtensions handshake message", eem);172}173174//175// validate176//177SSLExtension[] extTypes = chc.sslConfig.getEnabledExtensions(178SSLHandshake.ENCRYPTED_EXTENSIONS);179eem.extensions.consumeOnLoad(chc, extTypes);180181//182// update183//184eem.extensions.consumeOnTrade(chc, extTypes);185186//187// produce188//189// Need no new handshake message producers here.190}191}192}193194195