0N/A/*
4613N/A * Copyright (c) 2004, 2012, 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 java.nio.*;
0N/A
0N/A/*
0N/A * A multi-purpose class which handles all of the SSLEngine arguments.
0N/A * It validates arguments, checks for RO conditions, does space
0N/A * calculations, performs scatter/gather, etc.
0N/A *
0N/A * @author Brad R. Wetmore
0N/A */
0N/Aclass EngineArgs {
0N/A
0N/A /*
0N/A * Keep track of the input parameters.
0N/A */
0N/A ByteBuffer netData;
0N/A ByteBuffer [] appData;
0N/A
0N/A private int offset; // offset/len for the appData array.
0N/A private int len;
0N/A
0N/A /*
0N/A * The initial pos/limit conditions. This is useful because we can
0N/A * quickly calculate the amount consumed/produced in successful
0N/A * operations, or easily return the buffers to their pre-error
0N/A * conditions.
0N/A */
0N/A private int netPos;
0N/A private int netLim;
0N/A
0N/A private int [] appPoss;
0N/A private int [] appLims;
0N/A
0N/A /*
0N/A * Sum total of the space remaining in all of the appData buffers
0N/A */
0N/A private int appRemaining = 0;
0N/A
0N/A private boolean wrapMethod;
0N/A
0N/A /*
0N/A * Called by the SSLEngine.wrap() method.
0N/A */
0N/A EngineArgs(ByteBuffer [] appData, int offset, int len,
0N/A ByteBuffer netData) {
0N/A this.wrapMethod = true;
0N/A init(netData, appData, offset, len);
0N/A }
0N/A
0N/A /*
0N/A * Called by the SSLEngine.unwrap() method.
0N/A */
0N/A EngineArgs(ByteBuffer netData, ByteBuffer [] appData, int offset,
0N/A int len) {
0N/A this.wrapMethod = false;
0N/A init(netData, appData, offset, len);
0N/A }
0N/A
0N/A /*
0N/A * The main initialization method for the arguments. Most
0N/A * of them are pretty obvious as to what they do.
0N/A *
0N/A * Since we're already iterating over appData array for validity
0N/A * checking, we also keep track of how much remainging space is
0N/A * available. Info is used in both unwrap (to see if there is
0N/A * enough space available in the destination), and in wrap (to
0N/A * determine how much more we can copy into the outgoing data
0N/A * buffer.
0N/A */
0N/A private void init(ByteBuffer netData, ByteBuffer [] appData,
0N/A int offset, int len) {
0N/A
0N/A if ((netData == null) || (appData == null)) {
0N/A throw new IllegalArgumentException("src/dst is null");
0N/A }
0N/A
0N/A if ((offset < 0) || (len < 0) || (offset > appData.length - len)) {
0N/A throw new IndexOutOfBoundsException();
0N/A }
0N/A
0N/A if (wrapMethod && netData.isReadOnly()) {
0N/A throw new ReadOnlyBufferException();
0N/A }
0N/A
0N/A netPos = netData.position();
0N/A netLim = netData.limit();
0N/A
0N/A appPoss = new int [appData.length];
0N/A appLims = new int [appData.length];
0N/A
0N/A for (int i = offset; i < offset + len; i++) {
0N/A if (appData[i] == null) {
0N/A throw new IllegalArgumentException(
0N/A "appData[" + i + "] == null");
0N/A }
0N/A
0N/A /*
0N/A * If we're unwrapping, then check to make sure our
0N/A * destination bufffers are writable.
0N/A */
0N/A if (!wrapMethod && appData[i].isReadOnly()) {
0N/A throw new ReadOnlyBufferException();
0N/A }
0N/A
0N/A appRemaining += appData[i].remaining();
0N/A
0N/A appPoss[i] = appData[i].position();
0N/A appLims[i] = appData[i].limit();
0N/A }
0N/A
0N/A /*
0N/A * Ok, looks like we have a good set of args, let's
0N/A * store the rest of this stuff.
0N/A */
0N/A this.netData = netData;
0N/A this.appData = appData;
0N/A this.offset = offset;
0N/A this.len = len;
0N/A }
0N/A
0N/A /*
0N/A * Given spaceLeft bytes to transfer, gather up that much data
0N/A * from the appData buffers (starting at offset in the array),
0N/A * and transfer it into the netData buffer.
0N/A *
0N/A * The user has already ensured there is enough room.
0N/A */
0N/A void gather(int spaceLeft) {
0N/A for (int i = offset; (i < (offset + len)) && (spaceLeft > 0); i++) {
0N/A int amount = Math.min(appData[i].remaining(), spaceLeft);
0N/A appData[i].limit(appData[i].position() + amount);
0N/A netData.put(appData[i]);
4613N/A appRemaining -= amount;
0N/A spaceLeft -= amount;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Using the supplied buffer, scatter the data into the appData buffers
0N/A * (starting at offset in the array).
0N/A *
0N/A * The user has already ensured there is enough room.
0N/A */
0N/A void scatter(ByteBuffer readyData) {
0N/A int amountLeft = readyData.remaining();
0N/A
0N/A for (int i = offset; (i < (offset + len)) && (amountLeft > 0);
0N/A i++) {
0N/A int amount = Math.min(appData[i].remaining(), amountLeft);
0N/A readyData.limit(readyData.position() + amount);
0N/A appData[i].put(readyData);
0N/A amountLeft -= amount;
0N/A }
0N/A assert(readyData.remaining() == 0);
0N/A }
0N/A
0N/A int getAppRemaining() {
0N/A return appRemaining;
0N/A }
0N/A
0N/A /*
0N/A * Calculate the bytesConsumed/byteProduced. Aren't you glad
0N/A * we saved this off earlier?
0N/A */
0N/A int deltaNet() {
0N/A return (netData.position() - netPos);
0N/A }
0N/A
0N/A /*
0N/A * Calculate the bytesConsumed/byteProduced. Aren't you glad
0N/A * we saved this off earlier?
0N/A */
0N/A int deltaApp() {
0N/A int sum = 0; // Only calculating 2^14 here, don't need a long.
0N/A
0N/A for (int i = offset; i < offset + len; i++) {
0N/A sum += appData[i].position() - appPoss[i];
0N/A }
0N/A
0N/A return sum;
0N/A }
0N/A
0N/A /*
0N/A * In the case of Exception, we want to reset the positions
0N/A * to appear as though no data has been consumed or produced.
4613N/A *
4613N/A * Currently, this method is only called as we are preparing to
4613N/A * fail out, and thus we don't need to actually recalculate
4613N/A * appRemaining. If that assumption changes, that variable should
4613N/A * be updated here.
0N/A */
0N/A void resetPos() {
0N/A netData.position(netPos);
0N/A for (int i = offset; i < offset + len; i++) {
4613N/A // See comment above about recalculating appRemaining.
0N/A appData[i].position(appPoss[i]);
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * We are doing lots of ByteBuffer manipulations, in which case
0N/A * we need to make sure that the limits get set back correctly.
0N/A * This is one of the last things to get done before returning to
0N/A * the user.
0N/A */
0N/A void resetLim() {
0N/A netData.limit(netLim);
0N/A for (int i = offset; i < offset + len; i++) {
0N/A appData[i].limit(appLims[i]);
0N/A }
0N/A }
0N/A}