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