1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
/*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test 8263188
* @summary If TLS the server and client has no common signature algorithms,
* the connection should fail fast with "No supported signature algorithm".
* This test only covers TLS 1.2.
*
* @library /test/lib
* /javax/net/ssl/templates
*
* @run main/othervm
* -Djdk.tls.server.SignatureSchemes=ecdsa_secp384r1_sha384
* -Djdk.tls.client.SignatureSchemes=ecdsa_secp256r1_sha256,ecdsa_secp384r1_sha384
* -Dtest.clientAuth=false
* -Dtest.expectFail=false
* SigAlgosExtTestWithTLS12
* @run main/othervm
* -Djdk.tls.server.SignatureSchemes=ecdsa_secp384r1_sha384
* -Djdk.tls.client.SignatureSchemes=ecdsa_secp256r1_sha256
* -Dtest.clientAuth=false
* -Dtest.expectFail=true
* SigAlgosExtTestWithTLS12
* @run main/othervm
* -Djdk.tls.server.SignatureSchemes=ecdsa_secp256r1_sha256
* -Djdk.tls.client.SignatureSchemes=ecdsa_secp256r1_sha256
* -Dtest.clientAuth=true
* -Dtest.expectFail=true
* SigAlgosExtTestWithTLS12
*/
import javax.net.ssl.*;
import java.nio.ByteBuffer;
import java.util.*;
public class SigAlgosExtTestWithTLS12 extends SSLEngineTemplate {
private static final boolean CLIENT_AUTH
= Boolean.getBoolean("test.clientAuth");
private static final boolean EXPECT_FAIL
= Boolean.getBoolean("test.expectFail");
private static final int TLS_HS_CERT_REQ = 13;
public SigAlgosExtTestWithTLS12() throws Exception {
super();
}
/*
* Create an instance of KeyManager for client use.
*/
@Override
protected KeyManager createClientKeyManager() throws Exception {
return createKeyManager(
new Cert[]{Cert.EE_ECDSA_SECP256R1, Cert.EE_ECDSA_SECP384R1},
getClientContextParameters());
}
@Override
public TrustManager createClientTrustManager() throws Exception {
return createTrustManager(
new Cert[]{Cert.CA_ECDSA_SECP256R1, Cert.CA_ECDSA_SECP384R1},
getServerContextParameters());
}
@Override
public KeyManager createServerKeyManager() throws Exception {
return createKeyManager(
new Cert[]{Cert.EE_ECDSA_SECP256R1, Cert.EE_ECDSA_SECP384R1},
getServerContextParameters());
}
@Override
public TrustManager createServerTrustManager() throws Exception {
return createTrustManager(
new Cert[]{Cert.CA_ECDSA_SECP256R1, Cert.CA_ECDSA_SECP384R1},
getServerContextParameters());
}
@Override
protected SSLEngine configureServerEngine(SSLEngine serverEngine) {
serverEngine.setUseClientMode(false);
serverEngine.setNeedClientAuth(CLIENT_AUTH);
return serverEngine;
}
@Override
protected SSLEngine configureClientEngine(SSLEngine clientEngine) {
clientEngine.setUseClientMode(true);
clientEngine.setEnabledProtocols(new String[] { "TLSv1.2" });
return clientEngine;
}
public static void main(String[] args) throws Exception {
System.setProperty("javax.net.debug", "ssl:handshake");
try {
new SigAlgosExtTestWithTLS12().run();
if (EXPECT_FAIL) {
throw new RuntimeException(
"Expected SSLHandshakeException wasn't thrown");
}
} catch (SSLHandshakeException e) {
if (EXPECT_FAIL && e.getMessage().equals(
"No supported signature algorithm")) {
System.out.println("Expected SSLHandshakeException");
} else {
throw e;
}
}
}
private void run() throws Exception {
boolean dataDone = false;
while (isOpen(clientEngine) || isOpen(serverEngine)) {
clientEngine.wrap(clientOut, cTOs);
cTOs.flip();
// Consume the ClientHello and get the server flight of handshake
// messages. We expect that it will be one TLS record containing
// multiple handshake messages, one of which is a CertificateRequest
// when the client authentication is required.
serverEngine.unwrap(cTOs, serverIn);
runDelegatedTasks(serverEngine);
// Wrap the server flight
serverEngine.wrap(serverOut, sTOc);
sTOc.flip();
if (CLIENT_AUTH && EXPECT_FAIL) {
twistCertReqMsg(sTOc);
}
clientEngine.unwrap(sTOc, clientIn);
runDelegatedTasks(clientEngine);
serverEngine.unwrap(cTOs, serverIn);
runDelegatedTasks(serverEngine);
cTOs.compact();
sTOc.compact();
if (!dataDone && (clientOut.limit() == serverIn.position()) &&
(serverOut.limit() == clientIn.position())) {
checkTransfer(serverOut, clientIn);
checkTransfer(clientOut, serverIn);
clientEngine.closeOutbound();
dataDone = true;
serverEngine.closeOutbound();
}
}
}
/**
* Twists signature schemes in CertificateRequest message for negative
* client authentication cases.
*
* @param tlsRecord a ByteBuffer containing a TLS record. It is assumed
* that the position of the ByteBuffer is on the first byte of the TLS
* record header.
*
* @throws SSLException if the incoming ByteBuffer does not contain a
* well-formed TLS message.
*/
private static void twistCertReqMsg(
ByteBuffer tlsRecord) throws SSLException {
Objects.requireNonNull(tlsRecord);
tlsRecord.mark();
// Process the TLS record header
int type = Byte.toUnsignedInt(tlsRecord.get());
int ver_major = Byte.toUnsignedInt(tlsRecord.get());
int ver_minor = Byte.toUnsignedInt(tlsRecord.get());
int recLen = Short.toUnsignedInt(tlsRecord.getShort());
// Simple sanity checks
if (type != 22) {
throw new SSLException("Not a handshake: Type = " + type);
} else if (recLen > tlsRecord.remaining()) {
throw new SSLException("Incomplete record in buffer: " +
"Record length = " + recLen + ", Remaining = " +
tlsRecord.remaining());
}
while (tlsRecord.hasRemaining()) {
// Grab the handshake message header.
int msgHdr = tlsRecord.getInt();
int msgType = (msgHdr >> 24) & 0x000000FF;
int msgLen = msgHdr & 0x00FFFFFF;
if (msgType == TLS_HS_CERT_REQ) {
// Slice the buffer such that it contains the entire
// handshake message (less the handshake header).
int bufPos = tlsRecord.position();
ByteBuffer buf = tlsRecord.slice(bufPos, msgLen);
// Replace the signature scheme with an unknown value
twistSigSchemesCertReq(buf, (short) 0x0000);
byte[] bufBytes = new byte[buf.limit()];
buf.get(bufBytes);
tlsRecord.put(bufPos, bufBytes);
break;
} else {
// Skip to the next handshake message, if there is one
tlsRecord.position(tlsRecord.position() + msgLen);
}
}
tlsRecord.reset();
}
/**
* Replace the signature schemes in CertificateRequest message with an
* alternative value. It is assumed that the provided ByteBuffer has its
* position set at the first byte of the CertificateRequest message body
* (AFTER the handshake header) and contains the entire CR message. Upon
* successful completion of this method the ByteBuffer will have its
* position reset to the initial offset in the buffer.
* If an exception is thrown the position at the time of the exception
* will be preserved.
*
* @param data the ByteBuffer containing the CertificateRequest bytes
* @param altSigScheme an alternative signature scheme
*/
private static void twistSigSchemesCertReq(ByteBuffer data,
Short altSigScheme) {
Objects.requireNonNull(data);
data.mark();
// Jump past the certificate types
int certTypeLen = Byte.toUnsignedInt(data.get());
if (certTypeLen != 0) {
data.position(data.position() + certTypeLen);
}
int sigSchemeLen = Short.toUnsignedInt(data.getShort());
for (int ssOff = 0; ssOff < sigSchemeLen; ssOff += 2) {
System.err.println(
"Use alternative signature scheme: " + altSigScheme);
data.putShort(data.position(), altSigScheme);
}
data.reset();
}
}
|