Path: blob/master/src/java.base/share/classes/sun/security/ssl/ClientKeyExchange.java
41159 views
/*1* Copyright (c) 2003, 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.util.Map;30import sun.security.ssl.SSLHandshake.HandshakeMessage;3132/**33* Pack of the "ClientKeyExchange" handshake message.34*/35final class ClientKeyExchange {36static final SSLConsumer handshakeConsumer =37new ClientKeyExchangeConsumer();38static final HandshakeProducer handshakeProducer =39new ClientKeyExchangeProducer();404142/**43* The "ClientKeyExchange" handshake message producer.44*/45private static final46class ClientKeyExchangeProducer implements HandshakeProducer {47// Prevent instantiation of this class.48private ClientKeyExchangeProducer() {49// blank50}5152@Override53public byte[] produce(ConnectionContext context,54HandshakeMessage message) throws IOException {55// The producing happens in client side only.56ClientHandshakeContext chc = (ClientHandshakeContext)context;57SSLKeyExchange ke = SSLKeyExchange.valueOf(58chc.negotiatedCipherSuite.keyExchange,59chc.negotiatedProtocol);60if (ke != null) {61for (Map.Entry<Byte, HandshakeProducer> hp :62ke.getHandshakeProducers(chc)) {63if (hp.getKey() == SSLHandshake.CLIENT_KEY_EXCHANGE.id) {64return hp.getValue().produce(context, message);65}66}67}6869// not consumer defined.70throw chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE,71"Unexpected ClientKeyExchange handshake message.");72}73}7475/**76* The "ClientKeyExchange" handshake message consumer.77*/78private static final79class ClientKeyExchangeConsumer implements SSLConsumer {80// Prevent instantiation of this class.81private ClientKeyExchangeConsumer() {82// blank83}8485@Override86public void consume(ConnectionContext context,87ByteBuffer message) throws IOException {88// The consuming happens in server side only.89ServerHandshakeContext shc = (ServerHandshakeContext)context;90// clean up this consumer91shc.handshakeConsumers.remove(SSLHandshake.CLIENT_KEY_EXCHANGE.id);9293// Check for an unprocessed client Certificate message. If that94// handshake consumer is still present then that expected message95// was not sent.96if (shc.handshakeConsumers.containsKey(97SSLHandshake.CERTIFICATE.id)) {98throw shc.conContext.fatal(Alert.UNEXPECTED_MESSAGE,99"Unexpected ClientKeyExchange handshake message.");100}101102SSLKeyExchange ke = SSLKeyExchange.valueOf(103shc.negotiatedCipherSuite.keyExchange,104shc.negotiatedProtocol);105if (ke != null) {106for (Map.Entry<Byte, SSLConsumer> hc :107ke.getHandshakeConsumers(shc)) {108if (hc.getKey() == SSLHandshake.CLIENT_KEY_EXCHANGE.id) {109hc.getValue().consume(context, message);110return;111}112}113}114115// not consumer defined.116throw shc.conContext.fatal(Alert.UNEXPECTED_MESSAGE,117"Unexpected ClientKeyExchange handshake message.");118}119}120}121122123124