Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/ssl/CertSignAlgsExtension.java
38830 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.util.Arrays;30import java.util.Collections;31import java.util.List;32import sun.security.ssl.SSLExtension.ExtensionConsumer;33import sun.security.ssl.SSLHandshake.HandshakeMessage;34import sun.security.ssl.SignatureAlgorithmsExtension.SignatureSchemesSpec;3536/**37* Pack of the "signature_algorithms_cert" extensions.38*/39final class CertSignAlgsExtension {40static final HandshakeProducer chNetworkProducer =41new CHCertSignatureSchemesProducer();42static final ExtensionConsumer chOnLoadConsumer =43new CHCertSignatureSchemesConsumer();44static final HandshakeConsumer chOnTradeConsumer =45new CHCertSignatureSchemesUpdate();4647static final HandshakeProducer crNetworkProducer =48new CRCertSignatureSchemesProducer();49static final ExtensionConsumer crOnLoadConsumer =50new CRCertSignatureSchemesConsumer();51static final HandshakeConsumer crOnTradeConsumer =52new CRCertSignatureSchemesUpdate();5354static final SSLStringizer ssStringizer =55new CertSignatureSchemesStringizer();5657private static final58class CertSignatureSchemesStringizer implements SSLStringizer {59@Override60public String toString(ByteBuffer buffer) {61try {62return (new SignatureSchemesSpec(buffer)).toString();63} catch (IOException ioe) {64// For debug logging only, so please swallow exceptions.65return ioe.getMessage();66}67}68}6970/**71* Network data producer of a "signature_algorithms_cert" extension in72* the ClientHello handshake message.73*/74private static final75class CHCertSignatureSchemesProducer implements HandshakeProducer {76// Prevent instantiation of this class.77private CHCertSignatureSchemesProducer() {78// blank79}8081@Override82public byte[] produce(ConnectionContext context,83HandshakeMessage message) throws IOException {84// The producing happens in client side only.85ClientHandshakeContext chc = (ClientHandshakeContext)context;8687// Is it a supported and enabled extension?88if (!chc.sslConfig.isAvailable(89SSLExtension.CH_SIGNATURE_ALGORITHMS_CERT)) {90if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {91SSLLogger.fine(92"Ignore unavailable " +93"signature_algorithms_cert extension");94}9596return null; // ignore the extension97}9899// Produce the extension.100if (chc.localSupportedSignAlgs == null) {101chc.localSupportedSignAlgs =102SignatureScheme.getSupportedAlgorithms(103chc.sslConfig,104chc.algorithmConstraints, chc.activeProtocols);105}106107int vectorLen = SignatureScheme.sizeInRecord() *108chc.localSupportedSignAlgs.size();109byte[] extData = new byte[vectorLen + 2];110ByteBuffer m = ByteBuffer.wrap(extData);111Record.putInt16(m, vectorLen);112for (SignatureScheme ss : chc.localSupportedSignAlgs) {113Record.putInt16(m, ss.id);114}115116// Update the context.117chc.handshakeExtensions.put(118SSLExtension.CH_SIGNATURE_ALGORITHMS_CERT,119new SignatureSchemesSpec(chc.localSupportedSignAlgs));120121return extData;122}123}124125/**126* Network data consumer of a "signature_algorithms_cert" extension in127* the ClientHello handshake message.128*/129private static final130class CHCertSignatureSchemesConsumer implements ExtensionConsumer {131// Prevent instantiation of this class.132private CHCertSignatureSchemesConsumer() {133// blank134}135136@Override137public void consume(ConnectionContext context,138HandshakeMessage message, ByteBuffer buffer) throws IOException {139// The consuming happens in server side only.140ServerHandshakeContext shc = (ServerHandshakeContext)context;141142// Is it a supported and enabled extension?143if (!shc.sslConfig.isAvailable(144SSLExtension.CH_SIGNATURE_ALGORITHMS_CERT)) {145if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {146SSLLogger.fine(147"Ignore unavailable " +148"signature_algorithms_cert extension");149}150return; // ignore the extension151}152153// Parse the extension.154SignatureSchemesSpec spec;155try {156spec = new SignatureSchemesSpec(buffer);157} catch (IOException ioe) {158throw shc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, ioe);159}160161// Update the context.162shc.handshakeExtensions.put(163SSLExtension.CH_SIGNATURE_ALGORITHMS_CERT, spec);164165// No impact on session resumption.166}167}168169/**170* After session creation consuming of a "signature_algorithms_cert"171* extension in the ClientHello handshake message.172*/173private static final class CHCertSignatureSchemesUpdate174implements HandshakeConsumer {175// Prevent instantiation of this class.176private CHCertSignatureSchemesUpdate() {177// blank178}179180@Override181public void consume(ConnectionContext context,182HandshakeMessage message) throws IOException {183// The consuming happens in server side only.184ServerHandshakeContext shc = (ServerHandshakeContext)context;185186SignatureSchemesSpec spec = (SignatureSchemesSpec)187shc.handshakeExtensions.get(188SSLExtension.CH_SIGNATURE_ALGORITHMS_CERT);189if (spec == null) {190// Ignore, no signature_algorithms_cert extension requested.191return;192}193194// update the context195List<SignatureScheme> schemes =196SignatureScheme.getSupportedAlgorithms(197shc.sslConfig,198shc.algorithmConstraints, shc.negotiatedProtocol,199spec.signatureSchemes);200shc.peerRequestedCertSignSchemes = schemes;201shc.handshakeSession.setPeerSupportedSignatureAlgorithms(schemes);202203if (!shc.isResumption && shc.negotiatedProtocol.useTLS13PlusSpec()) {204if (shc.sslConfig.clientAuthType !=205ClientAuthType.CLIENT_AUTH_NONE) {206shc.handshakeProducers.putIfAbsent(207SSLHandshake.CERTIFICATE_REQUEST.id,208SSLHandshake.CERTIFICATE_REQUEST);209}210shc.handshakeProducers.put(SSLHandshake.CERTIFICATE.id,211SSLHandshake.CERTIFICATE);212shc.handshakeProducers.putIfAbsent(213SSLHandshake.CERTIFICATE_VERIFY.id,214SSLHandshake.CERTIFICATE_VERIFY);215}216}217}218219/**220* Network data producer of a "signature_algorithms_cert" extension in221* the CertificateRequest handshake message.222*/223private static final224class CRCertSignatureSchemesProducer implements HandshakeProducer {225// Prevent instantiation of this class.226private CRCertSignatureSchemesProducer() {227// blank228}229230@Override231public byte[] produce(ConnectionContext context,232HandshakeMessage message) throws IOException {233// The producing happens in server side only.234ServerHandshakeContext shc = (ServerHandshakeContext)context;235236// Is it a supported and enabled extension?237if (!shc.sslConfig.isAvailable(238SSLExtension.CH_SIGNATURE_ALGORITHMS_CERT)) {239if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {240SSLLogger.fine(241"Ignore unavailable " +242"signature_algorithms_cert extension");243}244return null; // ignore the extension245}246247// Produce the extension.248List<ProtocolVersion> protocols = Arrays.asList(shc.negotiatedProtocol);249protocols = Collections.unmodifiableList(protocols);250List<SignatureScheme> sigAlgs =251SignatureScheme.getSupportedAlgorithms(252shc.sslConfig,253shc.algorithmConstraints,254protocols);255256int vectorLen = SignatureScheme.sizeInRecord() * sigAlgs.size();257byte[] extData = new byte[vectorLen + 2];258ByteBuffer m = ByteBuffer.wrap(extData);259Record.putInt16(m, vectorLen);260for (SignatureScheme ss : sigAlgs) {261Record.putInt16(m, ss.id);262}263264// Update the context.265shc.handshakeExtensions.put(266SSLExtension.CR_SIGNATURE_ALGORITHMS_CERT,267new SignatureSchemesSpec(shc.localSupportedSignAlgs));268269return extData;270}271}272273/**274* Network data consumer of a "signature_algorithms_cert" extension in275* the CertificateRequest handshake message.276*/277private static final278class CRCertSignatureSchemesConsumer implements ExtensionConsumer {279// Prevent instantiation of this class.280private CRCertSignatureSchemesConsumer() {281// blank282}283@Override284public void consume(ConnectionContext context,285HandshakeMessage message, ByteBuffer buffer) throws IOException {286// The consuming happens in client side only.287ClientHandshakeContext chc = (ClientHandshakeContext)context;288289// Is it a supported and enabled extension?290if (!chc.sslConfig.isAvailable(291SSLExtension.CH_SIGNATURE_ALGORITHMS_CERT)) {292if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {293SSLLogger.fine(294"Ignore unavailable " +295"signature_algorithms_cert extension");296}297return; // ignore the extension298}299300// Parse the extension.301SignatureSchemesSpec spec;302try {303spec = new SignatureSchemesSpec(buffer);304} catch (IOException ioe) {305throw chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, ioe);306}307308// Update the context.309chc.handshakeExtensions.put(310SSLExtension.CR_SIGNATURE_ALGORITHMS_CERT, spec);311312// No impact on session resumption.313}314}315316/**317* After session creation consuming of a "signature_algorithms_cert"318* extension in the CertificateRequest handshake message.319*/320private static final class CRCertSignatureSchemesUpdate321implements HandshakeConsumer {322// Prevent instantiation of this class.323private CRCertSignatureSchemesUpdate() {324// blank325}326327@Override328public void consume(ConnectionContext context,329HandshakeMessage message) throws IOException {330// The consuming happens in client side only.331ClientHandshakeContext chc = (ClientHandshakeContext)context;332333SignatureSchemesSpec spec = (SignatureSchemesSpec)334chc.handshakeExtensions.get(335SSLExtension.CR_SIGNATURE_ALGORITHMS_CERT);336if (spec == null) {337// Ignore, no "signature_algorithms_cert" extension requested.338return;339}340341// update the context342List<SignatureScheme> schemes =343SignatureScheme.getSupportedAlgorithms(344chc.sslConfig,345chc.algorithmConstraints, chc.negotiatedProtocol,346spec.signatureSchemes);347chc.peerRequestedCertSignSchemes = schemes;348chc.handshakeSession.setPeerSupportedSignatureAlgorithms(schemes);349}350}351}352353354