Path: blob/master/src/java.base/share/classes/sun/security/ssl/CertificateRequest.java
67767 views
/*1* Copyright (c) 2015, 2021, 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.Arrays;34import java.util.Collection;35import java.util.Collections;36import java.util.HashSet;37import java.util.LinkedList;38import java.util.List;39import java.util.Locale;40import javax.net.ssl.SSLEngine;41import javax.net.ssl.SSLSocket;42import javax.net.ssl.X509ExtendedKeyManager;43import javax.security.auth.x500.X500Principal;44import sun.security.ssl.CipherSuite.KeyExchange;45import sun.security.ssl.SSLHandshake.HandshakeMessage;46import sun.security.ssl.X509Authentication.X509Possession;47import sun.security.ssl.X509Authentication.X509PossessionGenerator;4849/**50* Pack of the CertificateRequest handshake message.51*/52final class CertificateRequest {53static final SSLConsumer t10HandshakeConsumer =54new T10CertificateRequestConsumer();55static final HandshakeProducer t10HandshakeProducer =56new T10CertificateRequestProducer();5758static final SSLConsumer t12HandshakeConsumer =59new T12CertificateRequestConsumer();60static final HandshakeProducer t12HandshakeProducer =61new T12CertificateRequestProducer();6263static final SSLConsumer t13HandshakeConsumer =64new T13CertificateRequestConsumer();65static final HandshakeProducer t13HandshakeProducer =66new T13CertificateRequestProducer();6768// TLS 1.2 and prior versions69private static enum ClientCertificateType {70// RFC 224671RSA_SIGN ((byte)0x01, "rsa_sign", List.of("RSA"), true),72DSS_SIGN ((byte)0x02, "dss_sign", List.of("DSA"), true),73RSA_FIXED_DH ((byte)0x03, "rsa_fixed_dh"),74DSS_FIXED_DH ((byte)0x04, "dss_fixed_dh"),7576// RFC 434677RSA_EPHEMERAL_DH ((byte)0x05, "rsa_ephemeral_dh"),78DSS_EPHEMERAL_DH ((byte)0x06, "dss_ephemeral_dh"),79FORTEZZA_DMS ((byte)0x14, "fortezza_dms"),8081// RFC 4492 and 844282ECDSA_SIGN ((byte)0x40, "ecdsa_sign",83List.of("EC", "EdDSA"),84JsseJce.isEcAvailable()),85RSA_FIXED_ECDH ((byte)0x41, "rsa_fixed_ecdh"),86ECDSA_FIXED_ECDH ((byte)0x42, "ecdsa_fixed_ecdh");8788private static final byte[] CERT_TYPES =89JsseJce.isEcAvailable() ? new byte[] {90ECDSA_SIGN.id,91RSA_SIGN.id,92DSS_SIGN.id93} : new byte[] {94RSA_SIGN.id,95DSS_SIGN.id96};9798final byte id;99final String name;100final List<String> keyAlgorithm;101final boolean isAvailable;102103private ClientCertificateType(byte id, String name) {104this(id, name, null, false);105}106107private ClientCertificateType(byte id, String name,108List<String> keyAlgorithm, boolean isAvailable) {109this.id = id;110this.name = name;111this.keyAlgorithm = keyAlgorithm;112this.isAvailable = isAvailable;113}114115private static String nameOf(byte id) {116for (ClientCertificateType cct : ClientCertificateType.values()) {117if (cct.id == id) {118return cct.name;119}120}121return "UNDEFINED-CLIENT-CERTIFICATE-TYPE(" + (int)id + ")";122}123124private static ClientCertificateType valueOf(byte id) {125for (ClientCertificateType cct : ClientCertificateType.values()) {126if (cct.id == id) {127return cct;128}129}130131return null;132}133134private static String[] getKeyTypes(byte[] ids) {135ArrayList<String> keyTypes = new ArrayList<>(3);136for (byte id : ids) {137ClientCertificateType cct = ClientCertificateType.valueOf(id);138if (cct.isAvailable) {139cct.keyAlgorithm.forEach(key -> {140if (!keyTypes.contains(key)) {141keyTypes.add(key);142}143});144}145}146147return keyTypes.toArray(new String[0]);148}149}150151/**152* The "CertificateRequest" handshake message for SSL 3.0 and TLS 1.0/1.1.153*/154static final class T10CertificateRequestMessage extends HandshakeMessage {155final byte[] types; // certificate types156final List<byte[]> authorities; // certificate authorities157158T10CertificateRequestMessage(HandshakeContext handshakeContext,159X509Certificate[] trustedCerts, KeyExchange keyExchange) {160super(handshakeContext);161162this.authorities = new ArrayList<>(trustedCerts.length);163for (X509Certificate cert : trustedCerts) {164X500Principal x500Principal = cert.getSubjectX500Principal();165authorities.add(x500Principal.getEncoded());166}167168this.types = ClientCertificateType.CERT_TYPES;169}170171T10CertificateRequestMessage(HandshakeContext handshakeContext,172ByteBuffer m) throws IOException {173super(handshakeContext);174175// struct {176// ClientCertificateType certificate_types<1..2^8-1>;177// DistinguishedName certificate_authorities<0..2^16-1>;178// } CertificateRequest;179if (m.remaining() < 4) {180throw handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,181"Incorrect CertificateRequest message: no sufficient data");182}183this.types = Record.getBytes8(m);184185int listLen = Record.getInt16(m);186if (listLen > m.remaining()) {187throw handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,188"Incorrect CertificateRequest message:no sufficient data");189}190191if (listLen > 0) {192this.authorities = new LinkedList<>();193while (listLen > 0) {194// opaque DistinguishedName<1..2^16-1>;195byte[] encoded = Record.getBytes16(m);196listLen -= (2 + encoded.length);197authorities.add(encoded);198}199} else {200this.authorities = Collections.emptyList();201}202}203204String[] getKeyTypes() {205return ClientCertificateType.getKeyTypes(types);206}207208X500Principal[] getAuthorities() {209X500Principal[] principals = new X500Principal[authorities.size()];210int i = 0;211for (byte[] encoded : authorities) {212principals[i++] = new X500Principal(encoded);213}214215return principals;216}217218@Override219public SSLHandshake handshakeType() {220return SSLHandshake.CERTIFICATE_REQUEST;221}222223@Override224public int messageLength() {225int len = 1 + types.length + 2;226for (byte[] encoded : authorities) {227len += encoded.length + 2;228}229return len;230}231232@Override233public void send(HandshakeOutStream hos) throws IOException {234hos.putBytes8(types);235236int listLen = 0;237for (byte[] encoded : authorities) {238listLen += encoded.length + 2;239}240241hos.putInt16(listLen);242for (byte[] encoded : authorities) {243hos.putBytes16(encoded);244}245}246247@Override248public String toString() {249MessageFormat messageFormat = new MessageFormat(250"\"CertificateRequest\": '{'\n" +251" \"certificate types\": {0}\n" +252" \"certificate authorities\": {1}\n" +253"'}'",254Locale.ENGLISH);255256List<String> typeNames = new ArrayList<>(types.length);257for (byte type : types) {258typeNames.add(ClientCertificateType.nameOf(type));259}260261List<String> authorityNames = new ArrayList<>(authorities.size());262for (byte[] encoded : authorities) {263X500Principal principal = new X500Principal(encoded);264authorityNames.add(principal.toString());265}266Object[] messageFields = {267typeNames,268authorityNames269};270271return messageFormat.format(messageFields);272}273}274275/**276* The "CertificateRequest" handshake message producer for SSL 3.0 and277* TLS 1.0/1.1.278*/279private static final280class T10CertificateRequestProducer implements HandshakeProducer {281// Prevent instantiation of this class.282private T10CertificateRequestProducer() {283// blank284}285286@Override287public byte[] produce(ConnectionContext context,288HandshakeMessage message) throws IOException {289// The producing happens in server side only.290ServerHandshakeContext shc = (ServerHandshakeContext)context;291292X509Certificate[] caCerts =293shc.sslContext.getX509TrustManager().getAcceptedIssuers();294T10CertificateRequestMessage crm = new T10CertificateRequestMessage(295shc, caCerts, shc.negotiatedCipherSuite.keyExchange);296if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {297SSLLogger.fine(298"Produced CertificateRequest handshake message", crm);299}300301// Output the handshake message.302crm.write(shc.handshakeOutput);303shc.handshakeOutput.flush();304305//306// update307//308shc.handshakeConsumers.put(SSLHandshake.CERTIFICATE.id,309SSLHandshake.CERTIFICATE);310shc.handshakeConsumers.put(SSLHandshake.CERTIFICATE_VERIFY.id,311SSLHandshake.CERTIFICATE_VERIFY);312313// The handshake message has been delivered.314return null;315}316}317318/**319* The "CertificateRequest" handshake message consumer for SSL 3.0 and320* TLS 1.0/1.1.321*/322private static final323class T10CertificateRequestConsumer implements SSLConsumer {324// Prevent instantiation of this class.325private T10CertificateRequestConsumer() {326// blank327}328329@Override330public void consume(ConnectionContext context,331ByteBuffer message) throws IOException {332// The consuming happens in client side only.333ClientHandshakeContext chc = (ClientHandshakeContext)context;334335// clean up this consumer336chc.handshakeConsumers.remove(SSLHandshake.CERTIFICATE_REQUEST.id);337chc.receivedCertReq = true;338339// If we're processing this message and the server's certificate340// message consumer has not already run then this is a state341// machine violation.342if (chc.handshakeConsumers.containsKey(343SSLHandshake.CERTIFICATE.id)) {344throw chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE,345"Unexpected CertificateRequest handshake message");346}347348SSLConsumer certStatCons = chc.handshakeConsumers.remove(349SSLHandshake.CERTIFICATE_STATUS.id);350if (certStatCons != null) {351// Stapling was active but no certificate status message352// was sent. We need to run the absence handler which will353// check the certificate chain.354CertificateStatus.handshakeAbsence.absent(context, null);355}356357T10CertificateRequestMessage crm =358new T10CertificateRequestMessage(chc, message);359if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {360SSLLogger.fine(361"Consuming CertificateRequest handshake message", crm);362}363364//365// validate366//367// blank368369//370// update371//372373// An empty client Certificate handshake message may be allow.374chc.handshakeProducers.put(SSLHandshake.CERTIFICATE.id,375SSLHandshake.CERTIFICATE);376377X509ExtendedKeyManager km = chc.sslContext.getX509KeyManager();378String clientAlias = null;379if (chc.conContext.transport instanceof SSLSocketImpl) {380clientAlias = km.chooseClientAlias(crm.getKeyTypes(),381crm.getAuthorities(), (SSLSocket)chc.conContext.transport);382} else if (chc.conContext.transport instanceof SSLEngineImpl) {383clientAlias = km.chooseEngineClientAlias(crm.getKeyTypes(),384crm.getAuthorities(), (SSLEngine)chc.conContext.transport);385}386387388if (clientAlias == null) {389if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {390SSLLogger.warning("No available client authentication");391}392return;393}394395PrivateKey clientPrivateKey = km.getPrivateKey(clientAlias);396if (clientPrivateKey == null) {397if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {398SSLLogger.warning("No available client private key");399}400return;401}402403X509Certificate[] clientCerts = km.getCertificateChain(clientAlias);404if ((clientCerts == null) || (clientCerts.length == 0)) {405if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {406SSLLogger.warning("No available client certificate");407}408return;409}410411chc.handshakePossessions.add(412new X509Possession(clientPrivateKey, clientCerts));413chc.handshakeProducers.put(SSLHandshake.CERTIFICATE_VERIFY.id,414SSLHandshake.CERTIFICATE_VERIFY);415}416}417418/**419* The CertificateRequest handshake message for TLS 1.2.420*/421static final class T12CertificateRequestMessage extends HandshakeMessage {422final byte[] types; // certificate types423final int[] algorithmIds; // supported signature algorithms424final List<byte[]> authorities; // certificate authorities425426T12CertificateRequestMessage(HandshakeContext handshakeContext,427X509Certificate[] trustedCerts, KeyExchange keyExchange,428List<SignatureScheme> signatureSchemes) throws IOException {429super(handshakeContext);430431this.types = ClientCertificateType.CERT_TYPES;432433if (signatureSchemes == null || signatureSchemes.isEmpty()) {434throw handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,435"No signature algorithms specified for " +436"CertificateRequest hanshake message");437}438this.algorithmIds = new int[signatureSchemes.size()];439int i = 0;440for (SignatureScheme scheme : signatureSchemes) {441algorithmIds[i++] = scheme.id;442}443444this.authorities = new ArrayList<>(trustedCerts.length);445for (X509Certificate cert : trustedCerts) {446X500Principal x500Principal = cert.getSubjectX500Principal();447authorities.add(x500Principal.getEncoded());448}449}450451T12CertificateRequestMessage(HandshakeContext handshakeContext,452ByteBuffer m) throws IOException {453super(handshakeContext);454455// struct {456// ClientCertificateType certificate_types<1..2^8-1>;457// SignatureAndHashAlgorithm458// supported_signature_algorithms<2..2^16-2>;459// DistinguishedName certificate_authorities<0..2^16-1>;460// } CertificateRequest;461462// certificate_authorities463if (m.remaining() < 8) {464throw handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,465"Invalid CertificateRequest handshake message: " +466"no sufficient data");467}468this.types = Record.getBytes8(m);469470// supported_signature_algorithms471if (m.remaining() < 6) {472throw handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,473"Invalid CertificateRequest handshake message: " +474"no sufficient data");475}476477byte[] algs = Record.getBytes16(m);478if (algs == null || algs.length == 0 || (algs.length & 0x01) != 0) {479throw handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,480"Invalid CertificateRequest handshake message: " +481"incomplete signature algorithms");482}483484this.algorithmIds = new int[(algs.length >> 1)];485for (int i = 0, j = 0; i < algs.length;) {486byte hash = algs[i++];487byte sign = algs[i++];488algorithmIds[j++] = ((hash & 0xFF) << 8) | (sign & 0xFF);489}490491// certificate_authorities492if (m.remaining() < 2) {493throw handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,494"Invalid CertificateRequest handshake message: " +495"no sufficient data");496}497498int listLen = Record.getInt16(m);499if (listLen > m.remaining()) {500throw handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,501"Invalid CertificateRequest message: no sufficient data");502}503504if (listLen > 0) {505this.authorities = new LinkedList<>();506while (listLen > 0) {507// opaque DistinguishedName<1..2^16-1>;508byte[] encoded = Record.getBytes16(m);509listLen -= (2 + encoded.length);510authorities.add(encoded);511}512} else {513this.authorities = Collections.emptyList();514}515}516517String[] getKeyTypes() {518return ClientCertificateType.getKeyTypes(types);519}520521X500Principal[] getAuthorities() {522X500Principal[] principals = new X500Principal[authorities.size()];523int i = 0;524for (byte[] encoded : authorities) {525principals[i++] = new X500Principal(encoded);526}527528return principals;529}530531@Override532public SSLHandshake handshakeType() {533return SSLHandshake.CERTIFICATE_REQUEST;534}535536@Override537public int messageLength() {538int len = 1 + types.length + 2 + (algorithmIds.length << 1) + 2;539for (byte[] encoded : authorities) {540len += encoded.length + 2;541}542return len;543}544545@Override546public void send(HandshakeOutStream hos) throws IOException {547hos.putBytes8(types);548549int listLen = 0;550for (byte[] encoded : authorities) {551listLen += encoded.length + 2;552}553554hos.putInt16(algorithmIds.length << 1);555for (int algorithmId : algorithmIds) {556hos.putInt16(algorithmId);557}558559hos.putInt16(listLen);560for (byte[] encoded : authorities) {561hos.putBytes16(encoded);562}563}564565@Override566public String toString() {567MessageFormat messageFormat = new MessageFormat(568"\"CertificateRequest\": '{'\n" +569" \"certificate types\": {0}\n" +570" \"supported signature algorithms\": {1}\n" +571" \"certificate authorities\": {2}\n" +572"'}'",573Locale.ENGLISH);574575List<String> typeNames = new ArrayList<>(types.length);576for (byte type : types) {577typeNames.add(ClientCertificateType.nameOf(type));578}579580List<String> algorithmNames = new ArrayList<>(algorithmIds.length);581for (int algorithmId : algorithmIds) {582algorithmNames.add(SignatureScheme.nameOf(algorithmId));583}584585List<String> authorityNames = new ArrayList<>(authorities.size());586for (byte[] encoded : authorities) {587X500Principal principal = new X500Principal(encoded);588authorityNames.add(principal.toString());589}590Object[] messageFields = {591typeNames,592algorithmNames,593authorityNames594};595596return messageFormat.format(messageFields);597}598}599600/**601* The "CertificateRequest" handshake message producer for TLS 1.2.602*/603private static final604class T12CertificateRequestProducer implements HandshakeProducer {605// Prevent instantiation of this class.606private T12CertificateRequestProducer() {607// blank608}609610@Override611public byte[] produce(ConnectionContext context,612HandshakeMessage message) throws IOException {613// The producing happens in server side only.614ServerHandshakeContext shc = (ServerHandshakeContext)context;615if (shc.localSupportedSignAlgs == null) {616shc.localSupportedSignAlgs =617SignatureScheme.getSupportedAlgorithms(618shc.sslConfig,619shc.algorithmConstraints, shc.activeProtocols);620}621622if (shc.localSupportedSignAlgs == null ||623shc.localSupportedSignAlgs.isEmpty()) {624throw shc.conContext.fatal(Alert.HANDSHAKE_FAILURE,625"No supported signature algorithm");626}627628X509Certificate[] caCerts =629shc.sslContext.getX509TrustManager().getAcceptedIssuers();630T12CertificateRequestMessage crm = new T12CertificateRequestMessage(631shc, caCerts, shc.negotiatedCipherSuite.keyExchange,632shc.localSupportedSignAlgs);633if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {634SSLLogger.fine(635"Produced CertificateRequest handshake message", crm);636}637638// Output the handshake message.639crm.write(shc.handshakeOutput);640shc.handshakeOutput.flush();641642//643// update644//645shc.handshakeConsumers.put(SSLHandshake.CERTIFICATE.id,646SSLHandshake.CERTIFICATE);647shc.handshakeConsumers.put(SSLHandshake.CERTIFICATE_VERIFY.id,648SSLHandshake.CERTIFICATE_VERIFY);649650// The handshake message has been delivered.651return null;652}653}654655/**656* The "CertificateRequest" handshake message consumer for TLS 1.2.657*/658private static final659class T12CertificateRequestConsumer implements SSLConsumer {660// Prevent instantiation of this class.661private T12CertificateRequestConsumer() {662// blank663}664665@Override666public void consume(ConnectionContext context,667ByteBuffer message) throws IOException {668// The consuming happens in client side only.669ClientHandshakeContext chc = (ClientHandshakeContext)context;670671// clean up this consumer672chc.handshakeConsumers.remove(SSLHandshake.CERTIFICATE_REQUEST.id);673chc.receivedCertReq = true;674675// If we're processing this message and the server's certificate676// message consumer has not already run then this is a state677// machine violation.678if (chc.handshakeConsumers.containsKey(679SSLHandshake.CERTIFICATE.id)) {680throw chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE,681"Unexpected CertificateRequest handshake message");682}683684SSLConsumer certStatCons = chc.handshakeConsumers.remove(685SSLHandshake.CERTIFICATE_STATUS.id);686if (certStatCons != null) {687// Stapling was active but no certificate status message688// was sent. We need to run the absence handler which will689// check the certificate chain.690CertificateStatus.handshakeAbsence.absent(context, null);691}692693T12CertificateRequestMessage crm =694new T12CertificateRequestMessage(chc, message);695if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {696SSLLogger.fine(697"Consuming CertificateRequest handshake message", crm);698}699700//701// validate702//703// blank704705//706// update707//708709// An empty client Certificate handshake message may be allow.710chc.handshakeProducers.put(SSLHandshake.CERTIFICATE.id,711SSLHandshake.CERTIFICATE);712713List<SignatureScheme> sss =714SignatureScheme.getSupportedAlgorithms(715chc.sslConfig,716chc.algorithmConstraints, chc.negotiatedProtocol,717crm.algorithmIds);718if (sss == null || sss.isEmpty()) {719throw chc.conContext.fatal(Alert.HANDSHAKE_FAILURE,720"No supported signature algorithm");721}722723chc.peerRequestedSignatureSchemes = sss;724chc.peerRequestedCertSignSchemes = sss; // use the same schemes725chc.handshakeSession.setPeerSupportedSignatureAlgorithms(sss);726chc.peerSupportedAuthorities = crm.getAuthorities();727728// For TLS 1.2, we need to use a combination of the CR message's729// allowed key types and the signature algorithms in order to730// find a certificate chain that has the right key and all certs731// using one or more of the allowed cert signature schemes.732SSLPossession pos = choosePossession(chc, crm);733if (pos == null) {734return;735}736737chc.handshakePossessions.add(pos);738chc.handshakeProducers.put(SSLHandshake.CERTIFICATE_VERIFY.id,739SSLHandshake.CERTIFICATE_VERIFY);740}741742private static SSLPossession choosePossession(HandshakeContext hc,743T12CertificateRequestMessage crm) throws IOException {744if (hc.peerRequestedCertSignSchemes == null ||745hc.peerRequestedCertSignSchemes.isEmpty()) {746if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {747SSLLogger.warning("No signature and hash algorithms " +748"in CertificateRequest");749}750return null;751}752753// Put the CR key type into a more friendly format for searching754List<String> crKeyTypes = new ArrayList<>(755Arrays.asList(crm.getKeyTypes()));756// For TLS 1.2 only if RSA is a requested key type then we757// should also allow RSASSA-PSS.758if (crKeyTypes.contains("RSA")) {759crKeyTypes.add("RSASSA-PSS");760}761762Collection<String> checkedKeyTypes = new HashSet<>();763for (SignatureScheme ss : hc.peerRequestedCertSignSchemes) {764if (checkedKeyTypes.contains(ss.keyAlgorithm)) {765if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {766SSLLogger.warning(767"Unsupported authentication scheme: " + ss.name);768}769continue;770}771772// Don't select a signature scheme unless we will be able to773// produce a CertificateVerify message later774if (SignatureScheme.getPreferableAlgorithm(775hc.algorithmConstraints,776hc.peerRequestedSignatureSchemes,777ss, hc.negotiatedProtocol) == null) {778779if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {780SSLLogger.warning(781"Unable to produce CertificateVerify for " +782"signature scheme: " + ss.name);783}784checkedKeyTypes.add(ss.keyAlgorithm);785continue;786}787788X509Authentication ka = X509Authentication.valueOf(ss);789if (ka == null) {790if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {791SSLLogger.warning(792"Unsupported authentication scheme: " + ss.name);793}794checkedKeyTypes.add(ss.keyAlgorithm);795continue;796} else {797// Any auth object will have a possession generator and798// we need to make sure the key types for that generator799// share at least one common algorithm with the CR's800// allowed key types.801if (ka.possessionGenerator instanceof802X509PossessionGenerator xpg) {803if (Collections.disjoint(crKeyTypes,804Arrays.asList(xpg.keyTypes))) {805if (SSLLogger.isOn &&806SSLLogger.isOn("ssl,handshake")) {807SSLLogger.warning(808"Unsupported authentication scheme: " +809ss.name);810}811checkedKeyTypes.add(ss.keyAlgorithm);812continue;813}814}815}816817SSLPossession pos = ka.createPossession(hc);818if (pos == null) {819if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {820SSLLogger.warning(821"Unavailable authentication scheme: " + ss.name);822}823continue;824}825826return pos;827}828829if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {830SSLLogger.warning("No available authentication scheme");831}832return null;833}834}835836/**837* The CertificateRequest handshake message for TLS 1.3.838*/839static final class T13CertificateRequestMessage extends HandshakeMessage {840private final byte[] requestContext;841private final SSLExtensions extensions;842843T13CertificateRequestMessage(844HandshakeContext handshakeContext) throws IOException {845super(handshakeContext);846847this.requestContext = new byte[0];848this.extensions = new SSLExtensions(this);849}850851T13CertificateRequestMessage(HandshakeContext handshakeContext,852ByteBuffer m) throws IOException {853super(handshakeContext);854855// struct {856// opaque certificate_request_context<0..2^8-1>;857// Extension extensions<2..2^16-1>;858// } CertificateRequest;859if (m.remaining() < 5) {860throw handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,861"Invalid CertificateRequest handshake message: " +862"no sufficient data");863}864this.requestContext = Record.getBytes8(m);865866if (m.remaining() < 4) {867throw handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,868"Invalid CertificateRequest handshake message: " +869"no sufficient extensions data");870}871SSLExtension[] enabledExtensions =872handshakeContext.sslConfig.getEnabledExtensions(873SSLHandshake.CERTIFICATE_REQUEST);874this.extensions = new SSLExtensions(this, m, enabledExtensions);875}876877@Override878SSLHandshake handshakeType() {879return SSLHandshake.CERTIFICATE_REQUEST;880}881882@Override883int messageLength() {884// In TLS 1.3, use of certain extensions is mandatory.885return 1 + requestContext.length + extensions.length();886}887888@Override889void send(HandshakeOutStream hos) throws IOException {890hos.putBytes8(requestContext);891892// In TLS 1.3, use of certain extensions is mandatory.893extensions.send(hos);894}895896@Override897public String toString() {898MessageFormat messageFormat = new MessageFormat(899"\"CertificateRequest\": '{'\n" +900" \"certificate_request_context\": \"{0}\",\n" +901" \"extensions\": [\n" +902"{1}\n" +903" ]\n" +904"'}'",905Locale.ENGLISH);906Object[] messageFields = {907Utilities.toHexString(requestContext),908Utilities.indent(Utilities.indent(extensions.toString()))909};910911return messageFormat.format(messageFields);912}913}914915/**916* The "CertificateRequest" handshake message producer for TLS 1.3.917*/918private static final919class T13CertificateRequestProducer implements HandshakeProducer {920// Prevent instantiation of this class.921private T13CertificateRequestProducer() {922// blank923}924925@Override926public byte[] produce(ConnectionContext context,927HandshakeMessage message) throws IOException {928// The producing happens in server side only.929ServerHandshakeContext shc = (ServerHandshakeContext)context;930931T13CertificateRequestMessage crm =932new T13CertificateRequestMessage(shc);933// Produce extensions for CertificateRequest handshake message.934SSLExtension[] extTypes = shc.sslConfig.getEnabledExtensions(935SSLHandshake.CERTIFICATE_REQUEST, shc.negotiatedProtocol);936crm.extensions.produce(shc, extTypes);937if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {938SSLLogger.fine("Produced CertificateRequest message", crm);939}940941// Output the handshake message.942crm.write(shc.handshakeOutput);943shc.handshakeOutput.flush();944945//946// update947//948shc.certRequestContext = crm.requestContext.clone();949shc.handshakeConsumers.put(SSLHandshake.CERTIFICATE.id,950SSLHandshake.CERTIFICATE);951shc.handshakeConsumers.put(SSLHandshake.CERTIFICATE_VERIFY.id,952SSLHandshake.CERTIFICATE_VERIFY);953954// The handshake message has been delivered.955return null;956}957}958959/**960* The "CertificateRequest" handshake message consumer for TLS 1.3.961*/962private static final963class T13CertificateRequestConsumer implements SSLConsumer {964// Prevent instantiation of this class.965private T13CertificateRequestConsumer() {966// blank967}968969@Override970public void consume(ConnectionContext context,971ByteBuffer message) throws IOException {972// The consuming happens in client side only.973ClientHandshakeContext chc = (ClientHandshakeContext)context;974975// clean up this consumer976chc.handshakeConsumers.remove(SSLHandshake.CERTIFICATE_REQUEST.id);977chc.receivedCertReq = true;978979// Ensure that the CertificateRequest has not been sent prior980// to EncryptedExtensions981if (chc.handshakeConsumers.containsKey(982SSLHandshake.ENCRYPTED_EXTENSIONS.id)) {983throw chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE,984"Unexpected CertificateRequest handshake message");985}986987T13CertificateRequestMessage crm =988new T13CertificateRequestMessage(chc, message);989if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {990SSLLogger.fine(991"Consuming CertificateRequest handshake message", crm);992}993994//995// validate996//997SSLExtension[] extTypes = chc.sslConfig.getEnabledExtensions(998SSLHandshake.CERTIFICATE_REQUEST);999crm.extensions.consumeOnLoad(chc, extTypes);10001001//1002// update1003//1004crm.extensions.consumeOnTrade(chc, extTypes);10051006//1007// produce1008//1009chc.certRequestContext = crm.requestContext.clone();1010chc.handshakeProducers.put(SSLHandshake.CERTIFICATE.id,1011SSLHandshake.CERTIFICATE);1012chc.handshakeProducers.put(SSLHandshake.CERTIFICATE_VERIFY.id,1013SSLHandshake.CERTIFICATE_VERIFY);1014}1015}1016}101710181019