The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 18 | package javax.net.ssl; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 19 | |
| 20 | import java.nio.ByteBuffer; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * The abstract implementation of secure communications using SSL, TLS, or other |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 24 | * protocols. It includes the setup, handshake, and encrypt/decrypt |
| 25 | * functionality needed to create a secure connection. |
| 26 | * |
Alex Klyubin | b4675a5 | 2013-12-11 15:04:25 -0800 | [diff] [blame] | 27 | * <h3>Default configuration</h3> |
| 28 | * <p>{@code SSLEngine} instances obtained from default {@link SSLContext} are configured as |
| 29 | * follows: |
| 30 | * |
| 31 | * <h4>Protocols</h4> |
| 32 | * <table> |
| 33 | * <thead> |
| 34 | * <tr> |
| 35 | * <th>Protocol</th> |
| 36 | * <th>Supported (API Levels)</th> |
| 37 | * <th>Enabled by default (API Levels)</th> |
| 38 | * </tr> |
| 39 | * </thead> |
| 40 | * <tbody> |
| 41 | * <tr> |
| 42 | * <td>SSLv3</td> |
| 43 | * <td>1+</td> |
| 44 | * <td>1+</td> |
| 45 | * </tr> |
| 46 | * <tr> |
| 47 | * <td>TLSv1</td> |
| 48 | * <td>1+</td> |
| 49 | * <td>1+</td> |
| 50 | * </tr> |
| 51 | * </tbody> |
| 52 | * </table> |
| 53 | * |
| 54 | * <h4>Cipher suites</h4> |
| 55 | * <table> |
| 56 | * <thead> |
| 57 | * <tr> |
| 58 | * <th>Cipher suite</th> |
| 59 | * <th>Supported (API Levels)</th> |
| 60 | * <th>Enabled by default (API Levels)</th> |
| 61 | * </tr> |
| 62 | * </thead> |
| 63 | * <tbody> |
| 64 | * <tr> |
| 65 | * <td>SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA</td> |
| 66 | * <td>9+</td> |
| 67 | * <td></td> |
| 68 | * </tr> |
| 69 | * <tr> |
| 70 | * <td>SSL_DH_anon_EXPORT_WITH_RC4_40_MD5</td> |
| 71 | * <td>9+</td> |
| 72 | * <td></td> |
| 73 | * </tr> |
| 74 | * <tr> |
| 75 | * <td>SSL_DH_anon_WITH_3DES_EDE_CBC_SHA</td> |
| 76 | * <td>9+</td> |
| 77 | * <td></td> |
| 78 | * </tr> |
| 79 | * <tr> |
| 80 | * <td>SSL_DH_anon_WITH_DES_CBC_SHA</td> |
| 81 | * <td>9+</td> |
| 82 | * <td></td> |
| 83 | * </tr> |
| 84 | * <tr> |
| 85 | * <td>SSL_DH_anon_WITH_RC4_128_MD5</td> |
| 86 | * <td>9+</td> |
| 87 | * <td></td> |
| 88 | * </tr> |
| 89 | * <tr> |
| 90 | * <td>SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA</td> |
| 91 | * <td>9+</td> |
| 92 | * <td>9-19</td> |
| 93 | * </tr> |
| 94 | * <tr> |
| 95 | * <td>SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA</td> |
| 96 | * <td>9+</td> |
| 97 | * <td>9+</td> |
| 98 | * </tr> |
| 99 | * <tr> |
| 100 | * <td>SSL_DHE_DSS_WITH_DES_CBC_SHA</td> |
| 101 | * <td>9+</td> |
| 102 | * <td>9-19</td> |
| 103 | * </tr> |
| 104 | * <tr> |
| 105 | * <td>SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA</td> |
| 106 | * <td>9+</td> |
| 107 | * <td>9-19</td> |
| 108 | * </tr> |
| 109 | * <tr> |
| 110 | * <td>SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA</td> |
| 111 | * <td>9+</td> |
| 112 | * <td>9+</td> |
| 113 | * </tr> |
| 114 | * <tr> |
| 115 | * <td>SSL_DHE_RSA_WITH_DES_CBC_SHA</td> |
| 116 | * <td>9+</td> |
| 117 | * <td>9-19</td> |
| 118 | * </tr> |
| 119 | * <tr> |
| 120 | * <td>SSL_RSA_EXPORT_WITH_DES40_CBC_SHA</td> |
| 121 | * <td>9+</td> |
| 122 | * <td>9-19</td> |
| 123 | * </tr> |
| 124 | * <tr> |
| 125 | * <td>SSL_RSA_EXPORT_WITH_RC4_40_MD5</td> |
| 126 | * <td>9+</td> |
| 127 | * <td>9-19</td> |
| 128 | * </tr> |
| 129 | * <tr> |
| 130 | * <td>SSL_RSA_WITH_3DES_EDE_CBC_SHA</td> |
| 131 | * <td>9+</td> |
| 132 | * <td>9+</td> |
| 133 | * </tr> |
| 134 | * <tr> |
| 135 | * <td>SSL_RSA_WITH_DES_CBC_SHA</td> |
| 136 | * <td>9+</td> |
| 137 | * <td>9-19</td> |
| 138 | * </tr> |
| 139 | * <tr> |
| 140 | * <td>SSL_RSA_WITH_NULL_MD5</td> |
| 141 | * <td>9+</td> |
| 142 | * <td></td> |
| 143 | * </tr> |
| 144 | * <tr> |
| 145 | * <td>SSL_RSA_WITH_NULL_SHA</td> |
| 146 | * <td>9+</td> |
| 147 | * <td></td> |
| 148 | * </tr> |
| 149 | * <tr> |
| 150 | * <td>SSL_RSA_WITH_RC4_128_MD5</td> |
| 151 | * <td>9+</td> |
| 152 | * <td>9+</td> |
| 153 | * </tr> |
| 154 | * <tr> |
| 155 | * <td>SSL_RSA_WITH_RC4_128_SHA</td> |
| 156 | * <td>9+</td> |
| 157 | * <td>9+</td> |
| 158 | * </tr> |
| 159 | * <tr> |
| 160 | * <td>TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA</td> |
| 161 | * <td>1-8</td> |
| 162 | * <td></td> |
| 163 | * </tr> |
| 164 | * <tr> |
| 165 | * <td>TLS_DH_anon_WITH_3DES_EDE_CBC_SHA</td> |
| 166 | * <td>1-8</td> |
| 167 | * <td></td> |
| 168 | * </tr> |
| 169 | * <tr> |
| 170 | * <td>TLS_DH_anon_WITH_AES_128_CBC_SHA</td> |
| 171 | * <td>9+</td> |
| 172 | * <td></td> |
| 173 | * </tr> |
| 174 | * <tr> |
| 175 | * <td>TLS_DH_anon_WITH_AES_256_CBC_SHA</td> |
| 176 | * <td>9+</td> |
| 177 | * <td></td> |
| 178 | * </tr> |
| 179 | * <tr> |
| 180 | * <td>TLS_DH_anon_WITH_DES_CBC_SHA</td> |
| 181 | * <td>1-8</td> |
| 182 | * <td></td> |
| 183 | * </tr> |
| 184 | * <tr> |
| 185 | * <td>TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA</td> |
| 186 | * <td>1-8</td> |
| 187 | * <td></td> |
| 188 | * </tr> |
| 189 | * <tr> |
| 190 | * <td>TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA</td> |
| 191 | * <td>1-8</td> |
| 192 | * <td></td> |
| 193 | * </tr> |
| 194 | * <tr> |
| 195 | * <td>TLS_DH_DSS_WITH_DES_CBC_SHA</td> |
| 196 | * <td>1-8</td> |
| 197 | * <td></td> |
| 198 | * </tr> |
| 199 | * <tr> |
| 200 | * <td>TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA</td> |
| 201 | * <td>1-8</td> |
| 202 | * <td></td> |
| 203 | * </tr> |
| 204 | * <tr> |
| 205 | * <td>TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA</td> |
| 206 | * <td>1-8</td> |
| 207 | * <td></td> |
| 208 | * </tr> |
| 209 | * <tr> |
| 210 | * <td>TLS_DH_RSA_WITH_DES_CBC_SHA</td> |
| 211 | * <td>1-8</td> |
| 212 | * <td></td> |
| 213 | * </tr> |
| 214 | * <tr> |
| 215 | * <td>TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA</td> |
| 216 | * <td>1-8</td> |
| 217 | * <td>1-8</td> |
| 218 | * </tr> |
| 219 | * <tr> |
| 220 | * <td>TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA</td> |
| 221 | * <td>1-8</td> |
| 222 | * <td>1-8</td> |
| 223 | * </tr> |
| 224 | * <tr> |
| 225 | * <td>TLS_DHE_DSS_WITH_AES_128_CBC_SHA</td> |
| 226 | * <td>9+</td> |
| 227 | * <td>9+</td> |
| 228 | * </tr> |
| 229 | * <tr> |
| 230 | * <td>TLS_DHE_DSS_WITH_AES_256_CBC_SHA</td> |
| 231 | * <td>9+</td> |
| 232 | * <td>20+</td> |
| 233 | * </tr> |
| 234 | * <tr> |
| 235 | * <td>TLS_DHE_DSS_WITH_DES_CBC_SHA</td> |
| 236 | * <td>1-8</td> |
| 237 | * <td>1-8</td> |
| 238 | * </tr> |
| 239 | * <tr> |
| 240 | * <td>TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA</td> |
| 241 | * <td>1-8</td> |
| 242 | * <td>1-8</td> |
| 243 | * </tr> |
| 244 | * <tr> |
| 245 | * <td>TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA</td> |
| 246 | * <td>1-8</td> |
| 247 | * <td>1-8</td> |
| 248 | * </tr> |
| 249 | * <tr> |
| 250 | * <td>TLS_DHE_RSA_WITH_AES_128_CBC_SHA</td> |
| 251 | * <td>9+</td> |
| 252 | * <td>9+</td> |
| 253 | * </tr> |
| 254 | * <tr> |
| 255 | * <td>TLS_DHE_RSA_WITH_AES_256_CBC_SHA</td> |
| 256 | * <td>9+</td> |
| 257 | * <td>20+</td> |
| 258 | * </tr> |
| 259 | * <tr> |
| 260 | * <td>TLS_DHE_RSA_WITH_DES_CBC_SHA</td> |
| 261 | * <td>1-8</td> |
| 262 | * <td>1-8</td> |
| 263 | * </tr> |
| 264 | * <tr> |
| 265 | * <td>TLS_NULL_WITH_NULL_NULL</td> |
| 266 | * <td>1-8</td> |
| 267 | * <td></td> |
| 268 | * </tr> |
| 269 | * <tr> |
| 270 | * <td>TLS_RSA_EXPORT_WITH_DES40_CBC_SHA</td> |
| 271 | * <td>1-8</td> |
| 272 | * <td>1-8</td> |
| 273 | * </tr> |
| 274 | * <tr> |
| 275 | * <td>TLS_RSA_WITH_3DES_EDE_CBC_SHA</td> |
| 276 | * <td>1-8</td> |
| 277 | * <td>1-8</td> |
| 278 | * </tr> |
| 279 | * <tr> |
| 280 | * <td>TLS_RSA_WITH_AES_128_CBC_SHA</td> |
| 281 | * <td>9+</td> |
| 282 | * <td>9+</td> |
| 283 | * </tr> |
| 284 | * <tr> |
| 285 | * <td>TLS_RSA_WITH_AES_256_CBC_SHA</td> |
| 286 | * <td>9+</td> |
| 287 | * <td>20+</td> |
| 288 | * </tr> |
| 289 | * <tr> |
| 290 | * <td>TLS_RSA_WITH_DES_CBC_SHA</td> |
| 291 | * <td>1-8</td> |
| 292 | * <td>1-8</td> |
| 293 | * </tr> |
| 294 | * <tr> |
| 295 | * <td>TLS_RSA_WITH_NULL_MD5</td> |
| 296 | * <td>1-8</td> |
| 297 | * <td></td> |
| 298 | * </tr> |
| 299 | * <tr> |
| 300 | * <td>TLS_RSA_WITH_NULL_SHA</td> |
| 301 | * <td>1-8</td> |
| 302 | * <td></td> |
| 303 | * </tr> |
| 304 | * </tbody> |
| 305 | * </table> |
| 306 | * |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 307 | * @since 1.5 |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 308 | */ |
| 309 | public abstract class SSLEngine { |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 310 | private final String peerHost; |
| 311 | private final int peerPort; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 312 | |
| 313 | /** |
| 314 | * Creates a new {@code SSLEngine} instance. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 315 | */ |
| 316 | protected SSLEngine() { |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 317 | peerHost = null; |
| 318 | peerPort = -1; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Creates a new {@code SSLEngine} instance with the specified host and |
| 323 | * port. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 324 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 325 | * @param host |
| 326 | * the name of the host. |
| 327 | * @param port |
| 328 | * the port of the host. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 329 | */ |
| 330 | protected SSLEngine(String host, int port) { |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 331 | this.peerHost = host; |
| 332 | this.peerPort = port; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Returns the name of the peer host. |
| 337 | * |
| 338 | * @return the name of the peer host, or {@code null} if none is available. |
| 339 | */ |
| 340 | public String getPeerHost() { |
| 341 | return peerHost; |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Returns the port number of the peer host. |
| 346 | * |
| 347 | * @return the port number of the peer host, or {@code -1} is none is |
| 348 | * available. |
| 349 | */ |
| 350 | public int getPeerPort() { |
| 351 | return peerPort; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Initiates a handshake on this engine. |
| 356 | * <p> |
| 357 | * Calling this method is not needed for the initial handshake: it will be |
| 358 | * called by {@code wrap} or {@code unwrap} if the initial handshake has not |
| 359 | * been started yet. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 360 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 361 | * @throws SSLException |
| 362 | * if starting the handshake fails. |
| 363 | * @throws IllegalStateException |
| 364 | * if the engine does not have all the needed settings (e.g. |
| 365 | * client/server mode not set). |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 366 | */ |
| 367 | public abstract void beginHandshake() throws SSLException; |
| 368 | |
| 369 | /** |
| 370 | * Notifies this engine instance that no more inbound network data will be |
| 371 | * sent to this engine. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 372 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 373 | * @throws SSLException |
| 374 | * if this engine did not receive a needed protocol specific |
| 375 | * close notification message from the peer. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 376 | */ |
| 377 | public abstract void closeInbound() throws SSLException; |
| 378 | |
| 379 | /** |
| 380 | * Notifies this engine instance that no more outbound application data will |
| 381 | * be sent to this engine. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 382 | */ |
| 383 | public abstract void closeOutbound(); |
| 384 | |
| 385 | /** |
| 386 | * Returns a delegate task for this engine instance. Some engine operations |
| 387 | * may require the results of blocking or long running operations, and the |
| 388 | * {@code SSLEngineResult} instances returned by this engine may indicate |
| 389 | * that a delegated task result is needed. In this case the |
| 390 | * {@link Runnable#run() run} method of the returned {@code Runnable} |
| 391 | * delegated task must be called. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 392 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 393 | * @return a delegate task, or {@code null} if none are available. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 394 | */ |
| 395 | public abstract Runnable getDelegatedTask(); |
| 396 | |
| 397 | /** |
| 398 | * Returns the SSL cipher suite names that are enabled in this engine |
| 399 | * instance. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 400 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 401 | * @return the SSL cipher suite names that are enabled in this engine |
| 402 | * instance. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 403 | */ |
| 404 | public abstract String[] getEnabledCipherSuites(); |
| 405 | |
| 406 | /** |
| 407 | * Returns the protocol version names that are enabled in this engine |
| 408 | * instance. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 409 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 410 | * @return the protocol version names that are enabled in this engine |
| 411 | * instance. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 412 | */ |
| 413 | public abstract String[] getEnabledProtocols(); |
| 414 | |
| 415 | /** |
| 416 | * Returns whether new SSL sessions may be established by this engine. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 417 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 418 | * @return {@code true} if new session may be established, {@code false} if |
| 419 | * existing sessions must be reused. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 420 | */ |
| 421 | public abstract boolean getEnableSessionCreation(); |
| 422 | |
| 423 | /** |
| 424 | * Returns the status of the handshake of this engine instance. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 425 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 426 | * @return the status of the handshake of this engine instance. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 427 | */ |
| 428 | public abstract SSLEngineResult.HandshakeStatus getHandshakeStatus(); |
| 429 | |
| 430 | /** |
| 431 | * Returns whether this engine instance will require client authentication. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 432 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 433 | * @return {@code true} if this engine will require client authentication, |
| 434 | * {@code false} if no client authentication is needed. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 435 | */ |
| 436 | public abstract boolean getNeedClientAuth(); |
| 437 | |
| 438 | /** |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 439 | * Returns the SSL session for this engine instance. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 440 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 441 | * @return the SSL session for this engine instance. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 442 | */ |
| 443 | public abstract SSLSession getSession(); |
| 444 | |
| 445 | /** |
| 446 | * Returns the SSL cipher suite names that are supported by this engine. |
| 447 | * These cipher suites can be enabled using |
| 448 | * {@link #setEnabledCipherSuites(String[])}. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 449 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 450 | * @return the SSL cipher suite names that are supported by this engine. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 451 | */ |
| 452 | public abstract String[] getSupportedCipherSuites(); |
| 453 | |
| 454 | /** |
| 455 | * Returns the protocol names that are supported by this engine. These |
| 456 | * protocols can be enables using {@link #setEnabledProtocols(String[])}. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 457 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 458 | * @return the protocol names that are supported by this engine. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 459 | */ |
| 460 | public abstract String[] getSupportedProtocols(); |
| 461 | |
| 462 | /** |
| 463 | * Returns whether this engine is set to act in client mode when |
| 464 | * handshaking. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 465 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 466 | * @return {@code true} if the engine is set to do handshaking in client |
| 467 | * mode. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 468 | */ |
| 469 | public abstract boolean getUseClientMode(); |
| 470 | |
| 471 | /** |
| 472 | * Returns whether this engine will request client authentication. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 473 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 474 | * @return {@code true} if client authentication will be requested, |
| 475 | * {@code false} otherwise. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 476 | */ |
| 477 | public abstract boolean getWantClientAuth(); |
| 478 | |
| 479 | /** |
| 480 | * Returns whether no more inbound data will be accepted by this engine. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 481 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 482 | * @return {@code true} if no more inbound data will be accepted by this |
| 483 | * engine, {@code false} otherwise. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 484 | */ |
| 485 | public abstract boolean isInboundDone(); |
| 486 | |
| 487 | /** |
| 488 | * Returns whether no more outbound data will be produced by this engine. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 489 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 490 | * @return {@code true} if no more outbound data will be producted by this |
| 491 | * engine, {@code otherwise} false. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 492 | */ |
| 493 | public abstract boolean isOutboundDone(); |
| 494 | |
| 495 | /** |
| 496 | * Sets the SSL cipher suite names that should be enabled in this engine |
| 497 | * instance. Only cipher suites listed by {@code getSupportedCipherSuites()} |
| 498 | * are allowed. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 499 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 500 | * @param suites |
| 501 | * the SSL cipher suite names to be enabled. |
| 502 | * @throws IllegalArgumentException |
| 503 | * if one of the specified cipher suites is not supported, or if |
| 504 | * {@code suites} is {@code null}. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 505 | */ |
| 506 | public abstract void setEnabledCipherSuites(String[] suites); |
| 507 | |
| 508 | /** |
| 509 | * Sets the protocol version names that should be enabled in this engine |
| 510 | * instance. Only protocols listed by {@code getSupportedProtocols()} are |
| 511 | * allowed. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 512 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 513 | * @param protocols |
| 514 | * the protocol version names to be enabled. |
| 515 | * @throws IllegalArgumentException |
| 516 | * if one of the protocol version names is not supported, or if |
| 517 | * {@code protocols} is {@code null}. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 518 | */ |
| 519 | public abstract void setEnabledProtocols(String[] protocols); |
| 520 | |
| 521 | /** |
| 522 | * Sets whether new SSL sessions may be established by this engine instance. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 523 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 524 | * @param flag |
| 525 | * {@code true} if new SSL sessions may be established, |
| 526 | * {@code false} if existing SSL sessions must be reused. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 527 | */ |
| 528 | public abstract void setEnableSessionCreation(boolean flag); |
| 529 | |
| 530 | /** |
| 531 | * Sets whether this engine must require client authentication. The client |
| 532 | * authentication is one of: |
| 533 | * <ul> |
| 534 | * <li>authentication required</li> |
| 535 | * <li>authentication requested</li> |
| 536 | * <li>no authentication needed</li> |
| 537 | * </ul> |
| 538 | * This method overrides the setting of {@link #setWantClientAuth(boolean)}. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 539 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 540 | * @param need |
| 541 | * {@code true} if client authentication is required, |
| 542 | * {@code false} if no authentication is needed. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 543 | */ |
| 544 | public abstract void setNeedClientAuth(boolean need); |
| 545 | |
| 546 | /** |
| 547 | * Sets whether this engine should act in client (or server) mode when |
| 548 | * handshaking. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 549 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 550 | * @param mode |
| 551 | * {@code true} if this engine should act in client mode, |
| 552 | * {@code false} if not. |
| 553 | * @throws IllegalArgumentException |
| 554 | * if this method is called after starting the initial |
| 555 | * handshake. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 556 | */ |
| 557 | public abstract void setUseClientMode(boolean mode); |
| 558 | |
| 559 | /** |
| 560 | * Sets whether this engine should request client authentication. The client |
| 561 | * authentication is one of the following: |
| 562 | * <ul> |
| 563 | * <li>authentication required</li> |
| 564 | * <li>authentication requested</li> |
| 565 | * <li>no authentication needed</li> |
| 566 | * </ul> |
| 567 | * This method overrides the setting of {@link #setNeedClientAuth(boolean)}. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 568 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 569 | * @param want |
| 570 | * {@code true} if client authentication should be requested, |
| 571 | * {@code false} if no authentication is needed. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 572 | */ |
| 573 | public abstract void setWantClientAuth(boolean want); |
| 574 | |
| 575 | /** |
| 576 | * Decodes the incoming network data buffer into application data buffers. |
| 577 | * If a handshake has not been started yet, it will automatically be |
| 578 | * started. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 579 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 580 | * @param src |
| 581 | * the buffer with incoming network data |
| 582 | * @param dsts |
| 583 | * the array of destination buffers for incoming application |
| 584 | * data. |
| 585 | * @param offset |
| 586 | * the offset in the array of destination buffers to which data |
| 587 | * is to be transferred. |
| 588 | * @param length |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 589 | * the maximum number of destination buffers to be used. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 590 | * @return the result object of this operation. |
| 591 | * @throws SSLException |
| 592 | * if a problem occurred while processing the data. |
| 593 | * @throws IndexOutOfBoundsException |
| 594 | * if {@code length} is greater than |
| 595 | * {@code dsts.length - offset}. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 596 | * @throws java.nio.ReadOnlyBufferException |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 597 | * if one of the destination buffers is read-only. |
| 598 | * @throws IllegalArgumentException |
| 599 | * if {@code src}, {@code dsts}, or one of the entries in |
| 600 | * {@code dsts} is {@code null}. |
| 601 | * @throws IllegalStateException |
| 602 | * if the engine does not have all the needed settings (e.g. |
| 603 | * client/server mode not set). |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 604 | */ |
Brian Carlstrom | 0c131a2 | 2010-05-20 15:27:31 -0700 | [diff] [blame] | 605 | public abstract SSLEngineResult unwrap(ByteBuffer src, |
| 606 | ByteBuffer[] dsts, |
| 607 | int offset, |
| 608 | int length) throws SSLException; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 609 | |
| 610 | /** |
| 611 | * Encodes the outgoing application data buffers into the network data |
| 612 | * buffer. If a handshake has not been started yet, it will automatically be |
| 613 | * started. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 614 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 615 | * @param srcs |
| 616 | * the array of source buffers of outgoing application data. |
| 617 | * @param offset |
| 618 | * the offset in the array of source buffers from which data is |
| 619 | * to be retrieved. |
| 620 | * @param length |
| 621 | * the maximum number of source buffers to be used. |
| 622 | * @param dst |
| 623 | * the destination buffer for network data. |
| 624 | * @return the result object of this operation. |
| 625 | * @throws SSLException |
| 626 | * if a problem occurred while processing the data. |
| 627 | * @throws IndexOutOfBoundsException |
| 628 | * if {@code length} is greater than |
| 629 | * {@code srcs.length - offset}. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 630 | * @throws java.nio.ReadOnlyBufferException |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 631 | * if the destination buffer is readonly. |
| 632 | * @throws IllegalArgumentException |
| 633 | * if {@code srcs}, {@code dst}, or one the entries in |
| 634 | * {@code srcs} is {@code null}. |
| 635 | * @throws IllegalStateException |
| 636 | * if the engine does not have all the needed settings (e.g. |
| 637 | * client/server mode not set). |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 638 | */ |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 639 | public abstract SSLEngineResult wrap(ByteBuffer[] srcs, int offset, int length, ByteBuffer dst) |
| 640 | throws SSLException; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 641 | |
| 642 | /** |
| 643 | * Decodes the incoming network data buffer into the application data |
| 644 | * buffer. If a handshake has not been started yet, it will automatically be |
| 645 | * started. |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 646 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 647 | * @param src |
| 648 | * the buffer with incoming network data |
| 649 | * @param dst |
| 650 | * the destination buffer for incoming application data. |
| 651 | * @return the result object of this operation. |
| 652 | * @throws SSLException |
| 653 | * if a problem occurred while processing the data. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 654 | * @throws java.nio.ReadOnlyBufferException |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 655 | * if one of the destination buffers is read-only. |
| 656 | * @throws IllegalArgumentException |
| 657 | * if {@code src} or {@code dst} is {@code null}. |
| 658 | * @throws IllegalStateException |
| 659 | * if the engine does not have all the needed settings (e.g. |
| 660 | * client/server mode not set). |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 661 | */ |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 662 | public SSLEngineResult unwrap(ByteBuffer src, ByteBuffer dst) throws SSLException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 663 | return unwrap(src, new ByteBuffer[] { dst }, 0, 1); |
| 664 | } |
| 665 | |
| 666 | /** |
| 667 | * Decodes the incoming network data buffer into the application data |
| 668 | * buffers. If a handshake has not been started yet, it will automatically |
| 669 | * be started. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 670 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 671 | * @param src |
| 672 | * the buffer with incoming network data |
| 673 | * @param dsts |
| 674 | * the array of destination buffers for incoming application |
| 675 | * data. |
| 676 | * @return the result object of this operation. |
| 677 | * @throws SSLException |
| 678 | * if a problem occurred while processing the data. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 679 | * @throws java.nio.ReadOnlyBufferException |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 680 | * if one of the destination buffers is read-only. |
| 681 | * @throws IllegalArgumentException |
| 682 | * if {@code src} or {@code dsts} is {@code null}. |
| 683 | * @throws IllegalStateException |
| 684 | * if the engine does not have all the needed settings (e.g. |
| 685 | * client/server mode not set). |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 686 | */ |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 687 | public SSLEngineResult unwrap(ByteBuffer src, ByteBuffer[] dsts) throws SSLException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 688 | if (dsts == null) { |
| 689 | throw new IllegalArgumentException("Byte buffer array dsts is null"); |
| 690 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 691 | return unwrap(src, dsts, 0, dsts.length); |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * Encodes the outgoing application data buffers into the network data |
| 696 | * buffer. If a handshake has not been started yet, it will automatically be |
| 697 | * started. |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 698 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 699 | * @param srcs |
| 700 | * the array of source buffers of outgoing application data. |
| 701 | * @param dst |
| 702 | * the destination buffer for network data. |
| 703 | * @return the result object of this operation. |
| 704 | * @throws SSLException |
| 705 | * if a problem occurred while processing the data. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 706 | * @throws java.nio.ReadOnlyBufferException |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 707 | * if the destination buffer is readonly. |
| 708 | * @throws IllegalArgumentException |
| 709 | * if {@code srcs} or {@code dst} is {@code null}. |
| 710 | * @throws IllegalStateException |
| 711 | * if the engine does not have all the needed settings (e.g. |
| 712 | * client/server mode not set). |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 713 | */ |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 714 | public SSLEngineResult wrap(ByteBuffer[] srcs, ByteBuffer dst) throws SSLException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 715 | if (srcs == null) { |
| 716 | throw new IllegalArgumentException("Byte buffer array srcs is null"); |
| 717 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 718 | return wrap(srcs, 0, srcs.length, dst); |
| 719 | } |
| 720 | |
| 721 | /** |
| 722 | * Encodes the outgoing application data buffer into the network data |
| 723 | * buffer. If a handshake has not been started yet, it will automatically be |
| 724 | * started. |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 725 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 726 | * @param src |
| 727 | * the source buffers of outgoing application data. |
| 728 | * @param dst |
| 729 | * the destination buffer for network data. |
| 730 | * @return the result object of this operation. |
| 731 | * @throws SSLException |
| 732 | * if a problem occurred while processing the data. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 733 | * @throws java.nio.ReadOnlyBufferException |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 734 | * if the destination buffer is readonly. |
| 735 | * @throws IllegalArgumentException |
| 736 | * if {@code src} or {@code dst} is {@code null}. |
| 737 | * @throws IllegalStateException |
| 738 | * if the engine does not have all the needed settings (e.g. |
| 739 | * client/server mode not set). |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 740 | */ |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 741 | public SSLEngineResult wrap(ByteBuffer src, ByteBuffer dst) throws SSLException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 742 | return wrap(new ByteBuffer[] { src }, 0, 1, dst); |
| 743 | } |
Brian Carlstrom | 0c131a2 | 2010-05-20 15:27:31 -0700 | [diff] [blame] | 744 | |
| 745 | /** |
| 746 | * Returns a new SSLParameters based on this SSLSocket's current |
| 747 | * cipher suites, protocols, and client authentication settings. |
| 748 | * |
| 749 | * @since 1.6 |
| 750 | */ |
| 751 | public SSLParameters getSSLParameters() { |
| 752 | SSLParameters p = new SSLParameters(); |
| 753 | p.setCipherSuites(getEnabledCipherSuites()); |
| 754 | p.setProtocols(getEnabledProtocols()); |
| 755 | p.setNeedClientAuth(getNeedClientAuth()); |
| 756 | p.setWantClientAuth(getWantClientAuth()); |
| 757 | return p; |
| 758 | } |
| 759 | |
| 760 | /** |
| 761 | * Sets various SSL handshake parameters based on the SSLParameter |
| 762 | * argument. Specifically, sets the SSLEngine's enabled cipher |
| 763 | * suites if the parameter's cipher suites are non-null. Similarly |
| 764 | * sets the enabled protocols. If the parameters specify the want |
| 765 | * or need for client authentication, those requirements are set |
| 766 | * on the SSLEngine, otherwise both are set to false. |
| 767 | * @since 1.6 |
| 768 | */ |
| 769 | public void setSSLParameters(SSLParameters p) { |
| 770 | String[] cipherSuites = p.getCipherSuites(); |
| 771 | if (cipherSuites != null) { |
| 772 | setEnabledCipherSuites(cipherSuites); |
| 773 | } |
| 774 | String[] protocols = p.getProtocols(); |
| 775 | if (protocols != null) { |
| 776 | setEnabledProtocols(protocols); |
| 777 | } |
| 778 | if (p.getNeedClientAuth()) { |
| 779 | setNeedClientAuth(true); |
| 780 | } else if (p.getWantClientAuth()) { |
| 781 | setWantClientAuth(true); |
| 782 | } else { |
| 783 | setWantClientAuth(false); |
| 784 | } |
| 785 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 786 | } |