0N/A/*
2362N/A * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.ssl;
0N/A
0N/Aimport javax.net.ssl.*;
0N/Aimport java.io.IOException;
0N/Aimport java.nio.ByteBuffer;
0N/Aimport java.util.LinkedList;
0N/Aimport javax.net.ssl.SSLEngineResult.HandshakeStatus;
0N/Aimport sun.misc.HexDumpEncoder;
0N/A
0N/A/**
0N/A * A class to help abstract away SSLEngine writing synchronization.
0N/A */
0N/Afinal class EngineWriter {
0N/A
0N/A /*
0N/A * Outgoing handshake Data waiting for a ride is stored here.
0N/A * Normal application data is written directly into the outbound
0N/A * buffer, but handshake data can be written out at any time,
0N/A * so we have buffer it somewhere.
0N/A *
0N/A * When wrap is called, we first check to see if there is
0N/A * any data waiting, then if we're in a data transfer state,
0N/A * we try to write app data.
0N/A *
0N/A * This will contain either ByteBuffers, or the marker
0N/A * HandshakeStatus.FINISHED to signify that a handshake just completed.
0N/A */
0N/A private LinkedList<Object> outboundList;
0N/A
0N/A private boolean outboundClosed = false;
0N/A
0N/A /* Class and subclass dynamic debugging support */
0N/A private static final Debug debug = Debug.getInstance("ssl");
0N/A
0N/A EngineWriter() {
0N/A outboundList = new LinkedList<Object>();
0N/A }
0N/A
0N/A /*
0N/A * Upper levels assured us we had room for at least one packet of data.
0N/A * As per the SSLEngine spec, we only return one SSL packets worth of
0N/A * data.
0N/A */
0N/A private HandshakeStatus getOutboundData(ByteBuffer dstBB) {
0N/A
0N/A Object msg = outboundList.removeFirst();
0N/A assert(msg instanceof ByteBuffer);
0N/A
0N/A ByteBuffer bbIn = (ByteBuffer) msg;
0N/A assert(dstBB.remaining() >= bbIn.remaining());
0N/A
0N/A dstBB.put(bbIn);
0N/A
0N/A /*
0N/A * If we have more data in the queue, it's either
0N/A * a finished message, or an indication that we need
0N/A * to call wrap again.
0N/A */
0N/A if (hasOutboundDataInternal()) {
0N/A msg = outboundList.getFirst();
0N/A if (msg == HandshakeStatus.FINISHED) {
0N/A outboundList.removeFirst(); // consume the message
0N/A return HandshakeStatus.FINISHED;
0N/A } else {
0N/A return HandshakeStatus.NEED_WRAP;
0N/A }
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Properly orders the output of the data written to the wrap call.
0N/A * This is only handshake data, application data goes through the
0N/A * other writeRecord.
0N/A */
0N/A synchronized void writeRecord(EngineOutputRecord outputRecord,
0N/A MAC writeMAC, CipherBox writeCipher) throws IOException {
0N/A
0N/A /*
0N/A * Only output if we're still open.
0N/A */
0N/A if (outboundClosed) {
0N/A throw new IOException("writer side was already closed.");
0N/A }
0N/A
0N/A outputRecord.write(writeMAC, writeCipher);
0N/A
0N/A /*
0N/A * Did our handshakers notify that we just sent the
0N/A * Finished message?
0N/A *
0N/A * Add an "I'm finished" message to the queue.
0N/A */
0N/A if (outputRecord.isFinishedMsg()) {
0N/A outboundList.addLast(HandshakeStatus.FINISHED);
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Output the packet info.
0N/A */
0N/A private void dumpPacket(EngineArgs ea, boolean hsData) {
0N/A try {
0N/A HexDumpEncoder hd = new HexDumpEncoder();
0N/A
0N/A ByteBuffer bb = ea.netData.duplicate();
0N/A
0N/A int pos = bb.position();
0N/A bb.position(pos - ea.deltaNet());
0N/A bb.limit(pos);
0N/A
0N/A System.out.println("[Raw write" +
0N/A (hsData ? "" : " (bb)") + "]: length = " +
0N/A bb.remaining());
0N/A hd.encodeBuffer(bb, System.out);
0N/A } catch (IOException e) { }
0N/A }
0N/A
0N/A /*
0N/A * Properly orders the output of the data written to the wrap call.
0N/A * Only app data goes through here, handshake data goes through
0N/A * the other writeRecord.
0N/A *
0N/A * Shouldn't expect to have an IOException here.
0N/A *
0N/A * Return any determined status.
0N/A */
0N/A synchronized HandshakeStatus writeRecord(
0N/A EngineOutputRecord outputRecord, EngineArgs ea, MAC writeMAC,
0N/A CipherBox writeCipher) throws IOException {
0N/A
0N/A /*
0N/A * If we have data ready to go, output this first before
0N/A * trying to consume app data.
0N/A */
0N/A if (hasOutboundDataInternal()) {
0N/A HandshakeStatus hss = getOutboundData(ea.netData);
0N/A
0N/A if (debug != null && Debug.isOn("packet")) {
0N/A /*
0N/A * We could have put the dump in
0N/A * OutputRecord.write(OutputStream), but let's actually
0N/A * output when it's actually output by the SSLEngine.
0N/A */
0N/A dumpPacket(ea, true);
0N/A }
0N/A
0N/A return hss;
0N/A }
0N/A
0N/A /*
0N/A * If we are closed, no more app data can be output.
0N/A * Only existing handshake data (above) can be obtained.
0N/A */
0N/A if (outboundClosed) {
0N/A throw new IOException("The write side was already closed");
0N/A }
0N/A
0N/A outputRecord.write(ea, writeMAC, writeCipher);
0N/A
0N/A if (debug != null && Debug.isOn("packet")) {
0N/A dumpPacket(ea, false);
0N/A }
0N/A
0N/A /*
0N/A * No way new outbound handshake data got here if we're
0N/A * locked properly.
0N/A *
0N/A * We don't have any status we can return.
0N/A */
0N/A return null;
0N/A }
0N/A
0N/A /*
0N/A * We already hold "this" lock, this is the callback from the
0N/A * outputRecord.write() above. We already know this
0N/A * writer can accept more data (outboundClosed == false),
0N/A * and the closure is sync'd.
0N/A */
0N/A void putOutboundData(ByteBuffer bytes) {
0N/A outboundList.addLast(bytes);
0N/A }
0N/A
0N/A /*
0N/A * This is for the really rare case that someone is writing from
0N/A * the *InputRecord* before we know what to do with it.
0N/A */
0N/A synchronized void putOutboundDataSync(ByteBuffer bytes)
0N/A throws IOException {
0N/A
0N/A if (outboundClosed) {
0N/A throw new IOException("Write side already closed");
0N/A }
0N/A
0N/A outboundList.addLast(bytes);
0N/A }
0N/A
0N/A /*
0N/A * Non-synch'd version of this method, called by internals
0N/A */
0N/A private boolean hasOutboundDataInternal() {
0N/A return (outboundList.size() != 0);
0N/A }
0N/A
0N/A synchronized boolean hasOutboundData() {
0N/A return hasOutboundDataInternal();
0N/A }
0N/A
0N/A synchronized boolean isOutboundDone() {
0N/A return outboundClosed && !hasOutboundDataInternal();
0N/A }
0N/A
0N/A synchronized void closeOutbound() {
0N/A outboundClosed = true;
0N/A }
0N/A
0N/A}