jdk/src/share/classes/sun/security/ssl/EngineArgs.java
author xuelei
Fri, 08 Apr 2011 02:00:09 -0700
changeset 9246 c459f79af46b
parent 5506 202f599c92aa
child 11684 c4018f1df09b
permissions -rw-r--r--
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled Summary: Reorg the SSLContext implementation Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2004, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A multi-purpose class which handles all of the SSLEngine arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * It validates arguments, checks for RO conditions, does space
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * calculations, performs scatter/gather, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author Brad R. Wetmore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
class EngineArgs {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * Keep track of the input parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    ByteBuffer netData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    ByteBuffer [] appData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private int offset;         // offset/len for the appData array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * The initial pos/limit conditions.  This is useful because we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * quickly calculate the amount consumed/produced in successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * operations, or easily return the buffers to their pre-error
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * conditions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private int netPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private int netLim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private int [] appPoss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private int [] appLims;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Sum total of the space remaining in all of the appData buffers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private int appRemaining = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private boolean wrapMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Called by the SSLEngine.wrap() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    EngineArgs(ByteBuffer [] appData, int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            ByteBuffer netData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        this.wrapMethod = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        init(netData, appData, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Called by the SSLEngine.unwrap() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    EngineArgs(ByteBuffer netData, ByteBuffer [] appData, int offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        this.wrapMethod = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        init(netData, appData, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * The main initialization method for the arguments.  Most
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * of them are pretty obvious as to what they do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Since we're already iterating over appData array for validity
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * checking, we also keep track of how much remainging space is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * available.  Info is used in both unwrap (to see if there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * enough space available in the destination), and in wrap (to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * determine how much more we can copy into the outgoing data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private void init(ByteBuffer netData, ByteBuffer [] appData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if ((netData == null) || (appData == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw new IllegalArgumentException("src/dst is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if ((offset < 0) || (len < 0) || (offset > appData.length - len)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (wrapMethod && netData.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new ReadOnlyBufferException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        netPos = netData.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        netLim = netData.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        appPoss = new int [appData.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        appLims = new int [appData.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        for (int i = offset; i < offset + len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (appData[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    "appData[" + i + "] == null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
             * If we're unwrapping, then check to make sure our
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
             * destination bufffers are writable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if (!wrapMethod && appData[i].isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                throw new ReadOnlyBufferException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            appRemaining += appData[i].remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            appPoss[i] = appData[i].position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            appLims[i] = appData[i].limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
         * Ok, looks like we have a good set of args, let's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
         * store the rest of this stuff.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        this.netData = netData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        this.appData = appData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        this.len = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Given spaceLeft bytes to transfer, gather up that much data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * from the appData buffers (starting at offset in the array),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * and transfer it into the netData buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * The user has already ensured there is enough room.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    void gather(int spaceLeft) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        for (int i = offset; (i < (offset + len)) && (spaceLeft > 0); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            int amount = Math.min(appData[i].remaining(), spaceLeft);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            appData[i].limit(appData[i].position() + amount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            netData.put(appData[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            spaceLeft -= amount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Using the supplied buffer, scatter the data into the appData buffers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * (starting at offset in the array).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * The user has already ensured there is enough room.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    void scatter(ByteBuffer readyData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        int amountLeft = readyData.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        for (int i = offset; (i < (offset + len)) && (amountLeft > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            int amount = Math.min(appData[i].remaining(), amountLeft);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            readyData.limit(readyData.position() + amount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            appData[i].put(readyData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            amountLeft -= amount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        assert(readyData.remaining() == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    int getAppRemaining() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return appRemaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Calculate the bytesConsumed/byteProduced.  Aren't you glad
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * we saved this off earlier?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    int deltaNet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return (netData.position() - netPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Calculate the bytesConsumed/byteProduced.  Aren't you glad
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * we saved this off earlier?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    int deltaApp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        int sum = 0;    // Only calculating 2^14 here, don't need a long.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        for (int i = offset; i < offset + len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            sum += appData[i].position() - appPoss[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * In the case of Exception, we want to reset the positions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * to appear as though no data has been consumed or produced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    void resetPos() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        netData.position(netPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        for (int i = offset; i < offset + len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            appData[i].position(appPoss[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * We are doing lots of ByteBuffer manipulations, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * we need to make sure that the limits get set back correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * This is one of the last things to get done before returning to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    void resetLim() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        netData.limit(netLim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        for (int i = offset; i < offset + len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            appData[i].limit(appLims[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
}