Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/ssl/CertificateRequest.java
38830 views
/*1* Copyright (c) 2015, 2019, 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.security.PrivateKey;30import java.security.cert.X509Certificate;31import java.text.MessageFormat;32import java.util.ArrayList;33import java.util.Collection;34import java.util.Collections;35import java.util.HashSet;36import java.util.LinkedList;37import java.util.List;38import java.util.Locale;39import javax.net.ssl.SSLEngine;40import javax.net.ssl.SSLSocket;41import javax.net.ssl.X509ExtendedKeyManager;42import javax.security.auth.x500.X500Principal;43import sun.security.ssl.CipherSuite.KeyExchange;44import sun.security.ssl.SSLHandshake.HandshakeMessage;45import sun.security.ssl.X509Authentication.X509Possession;4647/**48* Pack of the CertificateRequest handshake message.49*/50final class CertificateRequest {51static final SSLConsumer t10HandshakeConsumer =52new T10CertificateRequestConsumer();53static final HandshakeProducer t10HandshakeProducer =54new T10CertificateRequestProducer();5556static final SSLConsumer t12HandshakeConsumer =57new T12CertificateRequestConsumer();58static final HandshakeProducer t12HandshakeProducer =59new T12CertificateRequestProducer();6061static final SSLConsumer t13HandshakeConsumer =62new T13CertificateRequestConsumer();63static final HandshakeProducer t13HandshakeProducer =64new T13CertificateRequestProducer();6566// TLS 1.2 and prior versions67private static enum ClientCertificateType {68// RFC 224669RSA_SIGN ((byte)0x01, "rsa_sign", "RSA", true),70DSS_SIGN ((byte)0x02, "dss_sign", "DSA", true),71RSA_FIXED_DH ((byte)0x03, "rsa_fixed_dh"),72DSS_FIXED_DH ((byte)0x04, "dss_fixed_dh"),7374// RFC 434675RSA_EPHEMERAL_DH ((byte)0x05, "rsa_ephemeral_dh"),76DSS_EPHEMERAL_DH ((byte)0x06, "dss_ephemeral_dh"),77FORTEZZA_DMS ((byte)0x14, "fortezza_dms"),7879// RFC 449280ECDSA_SIGN ((byte)0x40, "ecdsa_sign",81"EC", JsseJce.isEcAvailable()),82RSA_FIXED_ECDH ((byte)0x41, "rsa_fixed_ecdh"),83ECDSA_FIXED_ECDH ((byte)0x42, "ecdsa_fixed_ecdh");8485private static final byte[] CERT_TYPES =86JsseJce.isEcAvailable() ? new byte[] {87ECDSA_SIGN.id,88RSA_SIGN.id,89DSS_SIGN.id90} : new byte[] {91RSA_SIGN.id,92DSS_SIGN.id93};9495final byte id;96final String name;97final String keyAlgorithm;98final boolean isAvailable;99100private ClientCertificateType(byte id, String name) {101this(id, name, null, false);102}103104private ClientCertificateType(byte id, String name,105String keyAlgorithm, boolean isAvailable) {106this.id = id;107this.name = name;108this.keyAlgorithm = keyAlgorithm;109this.isAvailable = isAvailable;110}111112private static String nameOf(byte id) {113for (ClientCertificateType cct : ClientCertificateType.values()) {114if (cct.id == id) {115return cct.name;116}117}118return "UNDEFINED-CLIENT-CERTIFICATE-TYPE(" + (int)id + ")";119}120121private static ClientCertificateType valueOf(byte id) {122for (ClientCertificateType cct : ClientCertificateType.values()) {123if (cct.id == id) {124return cct;125}126}127128return null;129}130131private static String[] getKeyTypes(byte[] ids) {132ArrayList<String> keyTypes = new ArrayList<>(3);133for (byte id : ids) {134ClientCertificateType cct = ClientCertificateType.valueOf(id);135if (cct.isAvailable) {136keyTypes.add(cct.keyAlgorithm);137}138}139140return keyTypes.toArray(new String[0]);141}142}143144/**145* The "CertificateRequest" handshake message for SSL 3.0 and TLS 1.0/1.1.146*/147static final class T10CertificateRequestMessage extends HandshakeMessage {148final byte[] types; // certificate types149final List<byte[]> authorities; // certificate authorities150151T10CertificateRequestMessage(HandshakeContext handshakeContext,152X509Certificate[] trustedCerts, KeyExchange keyExchange) {153super(handshakeContext);154155this.authorities = new ArrayList<>(trustedCerts.length);156for (X509Certificate cert : trustedCerts) {157X500Principal x500Principal = cert.getSubjectX500Principal();158authorities.add(x500Principal.getEncoded());159}160161this.types = ClientCertificateType.CERT_TYPES;162}163164T10CertificateRequestMessage(HandshakeContext handshakeContext,165ByteBuffer m) throws IOException {166super(handshakeContext);167168// struct {169// ClientCertificateType certificate_types<1..2^8-1>;170// DistinguishedName certificate_authorities<0..2^16-1>;171// } CertificateRequest;172if (m.remaining() < 4) {173throw handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,174"Incorrect CertificateRequest message: no sufficient data");175}176this.types = Record.getBytes8(m);177178int listLen = Record.getInt16(m);179if (listLen > m.remaining()) {180throw handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,181"Incorrect CertificateRequest message:no sufficient data");182}183184if (listLen > 0) {185this.authorities = new LinkedList<>();186while (listLen > 0) {187// opaque DistinguishedName<1..2^16-1>;188byte[] encoded = Record.getBytes16(m);189listLen -= (2 + encoded.length);190authorities.add(encoded);191}192} else {193this.authorities = Collections.emptyList();194}195}196197String[] getKeyTypes() {198return ClientCertificateType.getKeyTypes(types);199}200201X500Principal[] getAuthorities() {202List<X500Principal> principals =203new ArrayList<>(authorities.size());204for (byte[] encoded : authorities) {205X500Principal principal = new X500Principal(encoded);206principals.add(principal);207}208209return principals.toArray(new X500Principal[0]);210}211212@Override213public SSLHandshake handshakeType() {214return SSLHandshake.CERTIFICATE_REQUEST;215}216217@Override218public int messageLength() {219int len = 1 + types.length + 2;220for (byte[] encoded : authorities) {221len += encoded.length + 2;222}223return len;224}225226@Override227public void send(HandshakeOutStream hos) throws IOException {228hos.putBytes8(types);229230int listLen = 0;231for (byte[] encoded : authorities) {232listLen += encoded.length + 2;233}234235hos.putInt16(listLen);236for (byte[] encoded : authorities) {237hos.putBytes16(encoded);238}239}240241@Override242public String toString() {243MessageFormat messageFormat = new MessageFormat(244"\"CertificateRequest\": '{'\n" +245" \"certificate types\": {0}\n" +246" \"certificate authorities\": {1}\n" +247"'}'",248Locale.ENGLISH);249250List<String> typeNames = new ArrayList<>(types.length);251for (byte type : types) {252typeNames.add(ClientCertificateType.nameOf(type));253}254255List<String> authorityNames = new ArrayList<>(authorities.size());256for (byte[] encoded : authorities) {257X500Principal principal = new X500Principal(encoded);258authorityNames.add(principal.toString());259}260Object[] messageFields = {261typeNames,262authorityNames263};264265return messageFormat.format(messageFields);266}267}268269/**270* The "CertificateRequest" handshake message producer for SSL 3.0 and271* TLS 1.0/1.1.272*/273private static final274class T10CertificateRequestProducer implements HandshakeProducer {275// Prevent instantiation of this class.276private T10CertificateRequestProducer() {277// blank278}279280@Override281public byte[] produce(ConnectionContext context,282HandshakeMessage message) throws IOException {283// The producing happens in server side only.284ServerHandshakeContext shc = (ServerHandshakeContext)context;285286X509Certificate[] caCerts =287shc.sslContext.getX509TrustManager().getAcceptedIssuers();288T10CertificateRequestMessage crm = new T10CertificateRequestMessage(289shc, caCerts, shc.negotiatedCipherSuite.keyExchange);290if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {291SSLLogger.fine(292"Produced CertificateRequest handshake message", crm);293}294295// Output the handshake message.296crm.write(shc.handshakeOutput);297shc.handshakeOutput.flush();298299//300// update301//302shc.handshakeConsumers.put(SSLHandshake.CERTIFICATE.id,303SSLHandshake.CERTIFICATE);304shc.handshakeConsumers.put(SSLHandshake.CERTIFICATE_VERIFY.id,305SSLHandshake.CERTIFICATE_VERIFY);306307// The handshake message has been delivered.308return null;309}310}311312/**313* The "CertificateRequest" handshake message consumer for SSL 3.0 and314* TLS 1.0/1.1.315*/316private static final317class T10CertificateRequestConsumer implements SSLConsumer {318// Prevent instantiation of this class.319private T10CertificateRequestConsumer() {320// blank321}322323@Override324public void consume(ConnectionContext context,325ByteBuffer message) throws IOException {326// The consuming happens in client side only.327ClientHandshakeContext chc = (ClientHandshakeContext)context;328329// clean up this consumer330chc.handshakeConsumers.remove(SSLHandshake.CERTIFICATE_REQUEST.id);331332SSLConsumer certStatCons = chc.handshakeConsumers.remove(333SSLHandshake.CERTIFICATE_STATUS.id);334if (certStatCons != null) {335// Stapling was active but no certificate status message336// was sent. We need to run the absence handler which will337// check the certificate chain.338CertificateStatus.handshakeAbsence.absent(context, null);339}340341T10CertificateRequestMessage crm =342new T10CertificateRequestMessage(chc, message);343if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {344SSLLogger.fine(345"Consuming CertificateRequest handshake message", crm);346}347348//349// validate350//351// blank352353//354// update355//356357// An empty client Certificate handshake message may be allow.358chc.handshakeProducers.put(SSLHandshake.CERTIFICATE.id,359SSLHandshake.CERTIFICATE);360361X509ExtendedKeyManager km = chc.sslContext.getX509KeyManager();362String clientAlias = null;363if (chc.conContext.transport instanceof SSLSocketImpl) {364clientAlias = km.chooseClientAlias(crm.getKeyTypes(),365crm.getAuthorities(), (SSLSocket)chc.conContext.transport);366} else if (chc.conContext.transport instanceof SSLEngineImpl) {367clientAlias = km.chooseEngineClientAlias(crm.getKeyTypes(),368crm.getAuthorities(), (SSLEngine)chc.conContext.transport);369}370371372if (clientAlias == null) {373if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {374SSLLogger.warning("No available client authentication");375}376return;377}378379PrivateKey clientPrivateKey = km.getPrivateKey(clientAlias);380if (clientPrivateKey == null) {381if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {382SSLLogger.warning("No available client private key");383}384return;385}386387X509Certificate[] clientCerts = km.getCertificateChain(clientAlias);388if ((clientCerts == null) || (clientCerts.length == 0)) {389if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {390SSLLogger.warning("No available client certificate");391}392return;393}394395chc.handshakePossessions.add(396new X509Possession(clientPrivateKey, clientCerts));397chc.handshakeProducers.put(SSLHandshake.CERTIFICATE_VERIFY.id,398SSLHandshake.CERTIFICATE_VERIFY);399}400}401402/**403* The CertificateRequest handshake message for TLS 1.2.404*/405static final class T12CertificateRequestMessage extends HandshakeMessage {406final byte[] types; // certificate types407final int[] algorithmIds; // supported signature algorithms408final List<byte[]> authorities; // certificate authorities409410T12CertificateRequestMessage(HandshakeContext handshakeContext,411X509Certificate[] trustedCerts, KeyExchange keyExchange,412List<SignatureScheme> signatureSchemes) throws IOException {413super(handshakeContext);414415this.types = ClientCertificateType.CERT_TYPES;416417if (signatureSchemes == null || signatureSchemes.isEmpty()) {418throw handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,419"No signature algorithms specified for " +420"CertificateRequest hanshake message");421}422this.algorithmIds = new int[signatureSchemes.size()];423int i = 0;424for (SignatureScheme scheme : signatureSchemes) {425algorithmIds[i++] = scheme.id;426}427428this.authorities = new ArrayList<>(trustedCerts.length);429for (X509Certificate cert : trustedCerts) {430X500Principal x500Principal = cert.getSubjectX500Principal();431authorities.add(x500Principal.getEncoded());432}433}434435T12CertificateRequestMessage(HandshakeContext handshakeContext,436ByteBuffer m) throws IOException {437super(handshakeContext);438439// struct {440// ClientCertificateType certificate_types<1..2^8-1>;441// SignatureAndHashAlgorithm442// supported_signature_algorithms<2..2^16-2>;443// DistinguishedName certificate_authorities<0..2^16-1>;444// } CertificateRequest;445446// certificate_authorities447if (m.remaining() < 8) {448throw handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,449"Invalid CertificateRequest handshake message: " +450"no sufficient data");451}452this.types = Record.getBytes8(m);453454// supported_signature_algorithms455if (m.remaining() < 6) {456throw handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,457"Invalid CertificateRequest handshake message: " +458"no sufficient data");459}460461byte[] algs = Record.getBytes16(m);462if (algs == null || algs.length == 0 || (algs.length & 0x01) != 0) {463throw handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,464"Invalid CertificateRequest handshake message: " +465"incomplete signature algorithms");466}467468this.algorithmIds = new int[(algs.length >> 1)];469for (int i = 0, j = 0; i < algs.length;) {470byte hash = algs[i++];471byte sign = algs[i++];472algorithmIds[j++] = ((hash & 0xFF) << 8) | (sign & 0xFF);473}474475// certificate_authorities476if (m.remaining() < 2) {477throw handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,478"Invalid CertificateRequest handshake message: " +479"no sufficient data");480}481482int listLen = Record.getInt16(m);483if (listLen > m.remaining()) {484throw handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,485"Invalid CertificateRequest message: no sufficient data");486}487488if (listLen > 0) {489this.authorities = new LinkedList<>();490while (listLen > 0) {491// opaque DistinguishedName<1..2^16-1>;492byte[] encoded = Record.getBytes16(m);493listLen -= (2 + encoded.length);494authorities.add(encoded);495}496} else {497this.authorities = Collections.emptyList();498}499}500501String[] getKeyTypes() {502return ClientCertificateType.getKeyTypes(types);503}504505X500Principal[] getAuthorities() {506List<X500Principal> principals =507new ArrayList<>(authorities.size());508for (byte[] encoded : authorities) {509X500Principal principal = new X500Principal(encoded);510principals.add(principal);511}512513return principals.toArray(new X500Principal[0]);514}515516@Override517public SSLHandshake handshakeType() {518return SSLHandshake.CERTIFICATE_REQUEST;519}520521@Override522public int messageLength() {523int len = 1 + types.length + 2 + (algorithmIds.length << 1) + 2;524for (byte[] encoded : authorities) {525len += encoded.length + 2;526}527return len;528}529530@Override531public void send(HandshakeOutStream hos) throws IOException {532hos.putBytes8(types);533534int listLen = 0;535for (byte[] encoded : authorities) {536listLen += encoded.length + 2;537}538539hos.putInt16(algorithmIds.length << 1);540for (int algorithmId : algorithmIds) {541hos.putInt16(algorithmId);542}543544hos.putInt16(listLen);545for (byte[] encoded : authorities) {546hos.putBytes16(encoded);547}548}549550@Override551public String toString() {552MessageFormat messageFormat = new MessageFormat(553"\"CertificateRequest\": '{'\n" +554" \"certificate types\": {0}\n" +555" \"supported signature algorithms\": {1}\n" +556" \"certificate authorities\": {2}\n" +557"'}'",558Locale.ENGLISH);559560List<String> typeNames = new ArrayList<>(types.length);561for (byte type : types) {562typeNames.add(ClientCertificateType.nameOf(type));563}564565List<String> algorithmNames = new ArrayList<>(algorithmIds.length);566for (int algorithmId : algorithmIds) {567algorithmNames.add(SignatureScheme.nameOf(algorithmId));568}569570List<String> authorityNames = new ArrayList<>(authorities.size());571for (byte[] encoded : authorities) {572X500Principal principal = new X500Principal(encoded);573authorityNames.add(principal.toString());574}575Object[] messageFields = {576typeNames,577algorithmNames,578authorityNames579};580581return messageFormat.format(messageFields);582}583}584585/**586* The "CertificateRequest" handshake message producer for TLS 1.2.587*/588private static final589class T12CertificateRequestProducer implements HandshakeProducer {590// Prevent instantiation of this class.591private T12CertificateRequestProducer() {592// blank593}594595@Override596public byte[] produce(ConnectionContext context,597HandshakeMessage message) throws IOException {598// The producing happens in server side only.599ServerHandshakeContext shc = (ServerHandshakeContext)context;600if (shc.localSupportedSignAlgs == null) {601shc.localSupportedSignAlgs =602SignatureScheme.getSupportedAlgorithms(603shc.sslConfig,604shc.algorithmConstraints, shc.activeProtocols);605}606607if (shc.localSupportedSignAlgs == null ||608shc.localSupportedSignAlgs.isEmpty()) {609throw shc.conContext.fatal(Alert.HANDSHAKE_FAILURE,610"No supported signature algorithm");611}612613X509Certificate[] caCerts =614shc.sslContext.getX509TrustManager().getAcceptedIssuers();615T12CertificateRequestMessage crm = new T12CertificateRequestMessage(616shc, caCerts, shc.negotiatedCipherSuite.keyExchange,617shc.localSupportedSignAlgs);618if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {619SSLLogger.fine(620"Produced CertificateRequest handshake message", crm);621}622623// Output the handshake message.624crm.write(shc.handshakeOutput);625shc.handshakeOutput.flush();626627//628// update629//630shc.handshakeConsumers.put(SSLHandshake.CERTIFICATE.id,631SSLHandshake.CERTIFICATE);632shc.handshakeConsumers.put(SSLHandshake.CERTIFICATE_VERIFY.id,633SSLHandshake.CERTIFICATE_VERIFY);634635// The handshake message has been delivered.636return null;637}638}639640/**641* The "CertificateRequest" handshake message consumer for TLS 1.2.642*/643private static final644class T12CertificateRequestConsumer implements SSLConsumer {645// Prevent instantiation of this class.646private T12CertificateRequestConsumer() {647// blank648}649650@Override651public void consume(ConnectionContext context,652ByteBuffer message) throws IOException {653// The consuming happens in client side only.654ClientHandshakeContext chc = (ClientHandshakeContext)context;655656// clean up this consumer657chc.handshakeConsumers.remove(SSLHandshake.CERTIFICATE_REQUEST.id);658659SSLConsumer certStatCons = chc.handshakeConsumers.remove(660SSLHandshake.CERTIFICATE_STATUS.id);661if (certStatCons != null) {662// Stapling was active but no certificate status message663// was sent. We need to run the absence handler which will664// check the certificate chain.665CertificateStatus.handshakeAbsence.absent(context, null);666}667668T12CertificateRequestMessage crm =669new T12CertificateRequestMessage(chc, message);670if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {671SSLLogger.fine(672"Consuming CertificateRequest handshake message", crm);673}674675//676// validate677//678// blank679680//681// update682//683684// An empty client Certificate handshake message may be allow.685chc.handshakeProducers.put(SSLHandshake.CERTIFICATE.id,686SSLHandshake.CERTIFICATE);687688List<SignatureScheme> sss = new LinkedList<>();689for (int id : crm.algorithmIds) {690SignatureScheme ss = SignatureScheme.valueOf(id);691if (ss != null) {692sss.add(ss);693}694}695chc.peerRequestedSignatureSchemes = sss;696chc.peerRequestedCertSignSchemes = sss; // use the same schemes697chc.handshakeSession.setPeerSupportedSignatureAlgorithms(sss);698chc.peerSupportedAuthorities = crm.getAuthorities();699700// For TLS 1.2, we no longer use the certificate_types field701// from the CertificateRequest message to directly determine702// the SSLPossession. Instead, the choosePossession method703// will use the accepted signature schemes in the message to704// determine the set of acceptable certificate types to select from.705SSLPossession pos = choosePossession(chc);706if (pos == null) {707return;708}709710chc.handshakePossessions.add(pos);711chc.handshakeProducers.put(SSLHandshake.CERTIFICATE_VERIFY.id,712SSLHandshake.CERTIFICATE_VERIFY);713}714715private static SSLPossession choosePossession(HandshakeContext hc)716throws IOException {717if (hc.peerRequestedCertSignSchemes == null ||718hc.peerRequestedCertSignSchemes.isEmpty()) {719if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {720SSLLogger.warning("No signature and hash algorithms " +721"in CertificateRequest");722}723return null;724}725726Collection<String> checkedKeyTypes = new HashSet<>();727for (SignatureScheme ss : hc.peerRequestedCertSignSchemes) {728if (checkedKeyTypes.contains(ss.keyAlgorithm)) {729if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {730SSLLogger.warning(731"Unsupported authentication scheme: " + ss.name);732}733continue;734}735736// Don't select a signature scheme unless we will be able to737// produce a CertificateVerify message later738if (SignatureScheme.getPreferableAlgorithm(739hc.peerRequestedSignatureSchemes,740ss, hc.negotiatedProtocol) == null) {741742if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {743SSLLogger.warning(744"Unable to produce CertificateVerify for " +745"signature scheme: " + ss.name);746}747checkedKeyTypes.add(ss.keyAlgorithm);748continue;749}750751SSLAuthentication ka = X509Authentication.valueOf(ss);752if (ka == null) {753if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {754SSLLogger.warning(755"Unsupported authentication scheme: " + ss.name);756}757checkedKeyTypes.add(ss.keyAlgorithm);758continue;759}760761SSLPossession pos = ka.createPossession(hc);762if (pos == null) {763if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {764SSLLogger.warning(765"Unavailable authentication scheme: " + ss.name);766}767continue;768}769770return pos;771}772773if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {774SSLLogger.warning("No available authentication scheme");775}776return null;777}778}779780/**781* The CertificateRequest handshake message for TLS 1.3.782*/783static final class T13CertificateRequestMessage extends HandshakeMessage {784private final byte[] requestContext;785private final SSLExtensions extensions;786787T13CertificateRequestMessage(788HandshakeContext handshakeContext) throws IOException {789super(handshakeContext);790791this.requestContext = new byte[0];792this.extensions = new SSLExtensions(this);793}794795T13CertificateRequestMessage(HandshakeContext handshakeContext,796ByteBuffer m) throws IOException {797super(handshakeContext);798799// struct {800// opaque certificate_request_context<0..2^8-1>;801// Extension extensions<2..2^16-1>;802// } CertificateRequest;803if (m.remaining() < 5) {804throw handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,805"Invalid CertificateRequest handshake message: " +806"no sufficient data");807}808this.requestContext = Record.getBytes8(m);809810if (m.remaining() < 4) {811throw handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,812"Invalid CertificateRequest handshake message: " +813"no sufficient extensions data");814}815SSLExtension[] enabledExtensions =816handshakeContext.sslConfig.getEnabledExtensions(817SSLHandshake.CERTIFICATE_REQUEST);818this.extensions = new SSLExtensions(this, m, enabledExtensions);819}820821@Override822SSLHandshake handshakeType() {823return SSLHandshake.CERTIFICATE_REQUEST;824}825826@Override827int messageLength() {828// In TLS 1.3, use of certain extensions is mandatory.829return 1 + requestContext.length + extensions.length();830}831832@Override833void send(HandshakeOutStream hos) throws IOException {834hos.putBytes8(requestContext);835836// In TLS 1.3, use of certain extensions is mandatory.837extensions.send(hos);838}839840@Override841public String toString() {842MessageFormat messageFormat = new MessageFormat(843"\"CertificateRequest\": '{'\n" +844" \"certificate_request_context\": \"{0}\",\n" +845" \"extensions\": [\n" +846"{1}\n" +847" ]\n" +848"'}'",849Locale.ENGLISH);850Object[] messageFields = {851Utilities.toHexString(requestContext),852Utilities.indent(Utilities.indent(extensions.toString()))853};854855return messageFormat.format(messageFields);856}857}858859/**860* The "CertificateRequest" handshake message producer for TLS 1.3.861*/862private static final863class T13CertificateRequestProducer implements HandshakeProducer {864// Prevent instantiation of this class.865private T13CertificateRequestProducer() {866// blank867}868869@Override870public byte[] produce(ConnectionContext context,871HandshakeMessage message) throws IOException {872// The producing happens in server side only.873ServerHandshakeContext shc = (ServerHandshakeContext)context;874875T13CertificateRequestMessage crm =876new T13CertificateRequestMessage(shc);877// Produce extensions for CertificateRequest handshake message.878SSLExtension[] extTypes = shc.sslConfig.getEnabledExtensions(879SSLHandshake.CERTIFICATE_REQUEST, shc.negotiatedProtocol);880crm.extensions.produce(shc, extTypes);881if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {882SSLLogger.fine("Produced CertificateRequest message", crm);883}884885// Output the handshake message.886crm.write(shc.handshakeOutput);887shc.handshakeOutput.flush();888889//890// update891//892shc.certRequestContext = crm.requestContext.clone();893shc.handshakeConsumers.put(SSLHandshake.CERTIFICATE.id,894SSLHandshake.CERTIFICATE);895shc.handshakeConsumers.put(SSLHandshake.CERTIFICATE_VERIFY.id,896SSLHandshake.CERTIFICATE_VERIFY);897898// The handshake message has been delivered.899return null;900}901}902903/**904* The "CertificateRequest" handshake message consumer for TLS 1.3.905*/906private static final907class T13CertificateRequestConsumer implements SSLConsumer {908// Prevent instantiation of this class.909private T13CertificateRequestConsumer() {910// blank911}912913@Override914public void consume(ConnectionContext context,915ByteBuffer message) throws IOException {916// The consuming happens in client side only.917ClientHandshakeContext chc = (ClientHandshakeContext)context;918919// clean up this consumer920chc.handshakeConsumers.remove(SSLHandshake.CERTIFICATE_REQUEST.id);921922T13CertificateRequestMessage crm =923new T13CertificateRequestMessage(chc, message);924if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {925SSLLogger.fine(926"Consuming CertificateRequest handshake message", crm);927}928929//930// validate931//932SSLExtension[] extTypes = chc.sslConfig.getEnabledExtensions(933SSLHandshake.CERTIFICATE_REQUEST);934crm.extensions.consumeOnLoad(chc, extTypes);935936//937// update938//939crm.extensions.consumeOnTrade(chc, extTypes);940941//942// produce943//944chc.certRequestContext = crm.requestContext.clone();945chc.handshakeProducers.put(SSLHandshake.CERTIFICATE.id,946SSLHandshake.CERTIFICATE);947chc.handshakeProducers.put(SSLHandshake.CERTIFICATE_VERIFY.id,948SSLHandshake.CERTIFICATE_VERIFY);949}950}951}952953954