/* * Copyright (c) 2020, 2022, Oracle and/or its affiliates. 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.
*/
// SunJSSE does not support dynamic system properties, no way to re-use // system properties in samevm/agentvm mode.
/** * A SSLEngine usage example which simplifies the presentation * by removing the I/O and multi-threading concerns. * * The test creates two SSLEngines, simulating a client and server. * The "transport" layer consists two byte buffers: think of them * as directly connected pipes. * * Note, this is a *very* simple example: real code will be much more * involved. For example, different threading and I/O models could be * used, transport mechanisms could close unexpectedly, and so on. * * When this application runs, notice that several messages * (wrap/unwrap) pass before any application data is consumed or * produced.
*/ publicclass AlpnGreaseTest implements SSLContextTemplate {
privatefinal SSLEngine clientEngine; // client Engine privatefinal ByteBuffer clientOut; // write side of clientEngine privatefinal ByteBuffer clientIn; // read side of clientEngine
privatefinal SSLEngine serverEngine; // server Engine privatefinal ByteBuffer serverOut; // write side of serverEngine privatefinal ByteBuffer serverIn; // read side of serverEngine
// For data transport, this example uses local ByteBuffers. This // isn't really useful, but the purpose of this example is to show // SSLEngine concepts, not how to do network transport. privatefinal ByteBuffer cTOs; // "reliable" transport client->server privatefinal ByteBuffer sTOc; // "reliable" transport server->client
// These are the various 8-bit char values that could be sent as GREASE // values. We'll just make one big String here to make it easy to check // that the right values are being output. privatestaticfinalbyte[] greaseBytes = newbyte[] {
(byte) 0x0A, (byte) 0x1A, (byte) 0x2A, (byte) 0x3A,
(byte) 0x4A, (byte) 0x5A, (byte) 0x6A, (byte) 0x7A,
(byte) 0x8A, (byte) 0x9A, (byte) 0xAA, (byte) 0xBA,
(byte) 0xCA, (byte) 0xDA, (byte) 0xEA, (byte) 0xFA
};
privatestaticfinal String greaseString = new String(greaseBytes, StandardCharsets.ISO_8859_1);
privatestaticvoid findGreaseInClientHello(byte[] bytes) throws Exception { for (int i = 0; i < bytes.length - greaseBytes.length + 1; i++) { if (Arrays.equals(bytes, i, i + greaseBytes.length,
greaseBytes, 0, greaseBytes.length)) {
System.out.println("Found greaseBytes in ClientHello at: " + i); return;
}
} thrownew Exception("Couldn't find greaseBytes");
}
// We'll assume the buffer sizes are the same // between client and server.
SSLSession session = clientEngine.getSession(); int appBufferMax = session.getApplicationBufferSize(); int netBufferMax = session.getPacketBufferSize();
// We'll make the input buffers a bit bigger than the max needed // size, so that unwrap()s following a successful data transfer // won't generate BUFFER_OVERFLOWS. // // We'll use a mix of direct and indirect ByteBuffers for // tutorial purposes only. In reality, only use direct // ByteBuffers when they give a clear performance enhancement.
clientIn = ByteBuffer.allocate(appBufferMax + 50);
serverIn = ByteBuffer.allocate(appBufferMax + 50);
/* * Configure the server side engine.
*/ protected SSLEngine configureServerEngine(SSLEngine serverEngine) {
serverEngine.setUseClientMode(false);
serverEngine.setNeedClientAuth(true);
// After we've transferred all application data between the client // and server, we close the clientEngine's outbound stream. // This generates a close_notify handshake message, which the // server engine receives and responds by closing itself. if (!dataDone && (clientOut.limit() == serverIn.position()) &&
(serverOut.limit() == clientIn.position())) {
// If the result indicates that we have outstanding tasks to do, // go ahead and run them in this thread. privatestaticvoid runDelegatedTasks(SSLEngine engine) throws Exception { if (engine.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
Runnable runnable; while ((runnable = engine.getDelegatedTask()) != null) {
log(" running delegated task...");
runnable.run();
}
HandshakeStatus hsStatus = engine.getHandshakeStatus(); if (hsStatus == HandshakeStatus.NEED_TASK) { thrownew Exception( "handshake shouldn't need additional tasks");
}
logEngineStatus(engine);
}
}
// Simple check to make sure everything came across as expected. privatestaticvoid checkTransfer(ByteBuffer a, ByteBuffer b) throws Exception {
a.flip();
b.flip();
if (!a.equals(b)) { thrownew Exception("Data didn't transfer cleanly");
} else {
log("\tData transferred cleanly");
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.