Home | History | Annotate | Download | only in ssl
      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 
     18 package javax.net.ssl;
     19 
     20 /**
     21  * The result object describing the state of the {@code SSLEngine} produced
     22  * by the {@code wrap()} and {@code unwrap()} operations.
     23  */
     24 public class SSLEngineResult {
     25 
     26     /**
     27      * The {@code enum} describing the state of the current handshake.
     28      */
     29     public enum HandshakeStatus {
     30         /**
     31          * No handshake in progress.
     32          */
     33         NOT_HANDSHAKING,
     34         /**
     35          * The handshake is finished.
     36          */
     37         FINISHED,
     38         /**
     39          * The results of one (or more) delegated tasks are needed to continue
     40          * the handshake.
     41          */
     42         NEED_TASK,
     43         /**
     44          * The engine must send data to the remote side to continue the
     45          * handshake.
     46          */
     47         NEED_WRAP,
     48         /**
     49          * The engine needs to receive data from the remote side to continue the
     50          * handshake.
     51          */
     52         NEED_UNWRAP
     53     }
     54 
     55     /**
     56      * The {@code enum} describing the result of the {@code SSLEngine}
     57      * operation.
     58      */
     59     public static enum Status {
     60         /**
     61          * The size of the destination buffer is too small to hold the result of
     62          * the current operation.
     63          */
     64         BUFFER_OVERFLOW,
     65         /**
     66          * There were not enough bytes available in the source buffer to
     67          * complete the current operation.
     68          */
     69         BUFFER_UNDERFLOW,
     70         /**
     71          * The operation closed this side of the communication or was already
     72          * closed.
     73          */
     74         CLOSED,
     75         /**
     76          * The operation completed successfully.
     77          */
     78         OK
     79     }
     80 
     81     // Store Status object
     82     private final SSLEngineResult.Status status;
     83 
     84     // Store HandshakeStatus object
     85     private final SSLEngineResult.HandshakeStatus handshakeStatus;
     86 
     87     // Store bytesConsumed
     88     private final int bytesConsumed;
     89 
     90     // Store bytesProduced
     91     private final int bytesProduced;
     92 
     93     /**
     94      * Creates a new {@code SSLEngineResult} instance with the specified state
     95      * values.
     96      *
     97      * @param status
     98      *            the return value of the {@code SSLEngine} operation.
     99      * @param handshakeStatus
    100      *            the status of the current handshake
    101      * @param bytesConsumed
    102      *            the number of bytes retrieved from the source buffer(s).
    103      * @param bytesProduced
    104      *            the number of bytes transferred to the destination buffer(s).
    105      * @throws IllegalArgumentException
    106      *             if {@code status} or {@code handshakeStatus} is {@code null},
    107      *             or if {@code bytesConsumed} or {@code bytesProduces} are
    108      *             negative.
    109      */
    110     public SSLEngineResult(SSLEngineResult.Status status,
    111             SSLEngineResult.HandshakeStatus handshakeStatus, int bytesConsumed, int bytesProduced) {
    112         if (status == null) {
    113             throw new IllegalArgumentException("status is null");
    114         }
    115         if (handshakeStatus == null) {
    116             throw new IllegalArgumentException("handshakeStatus is null");
    117         }
    118         if (bytesConsumed < 0) {
    119             throw new IllegalArgumentException("bytesConsumed is negative");
    120         }
    121         if (bytesProduced < 0) {
    122             throw new IllegalArgumentException("bytesProduced is negative");
    123         }
    124         this.status = status;
    125         this.handshakeStatus = handshakeStatus;
    126         this.bytesConsumed = bytesConsumed;
    127         this.bytesProduced = bytesProduced;
    128     }
    129 
    130     /**
    131      * Returns the return value of the {@code SSLEngine} operation.
    132      *
    133      * @return the return value of the {@code SSLEngine} operation.
    134      */
    135     public final Status getStatus() {
    136         return status;
    137     }
    138 
    139     /**
    140      * Returns the status of the current handshake.
    141      *
    142      * @return the status of the current handshake.
    143      */
    144     public final HandshakeStatus getHandshakeStatus() {
    145         return handshakeStatus;
    146     }
    147 
    148     /**
    149      * Returns the number of bytes retrieved from the source buffer(s).
    150      *
    151      * @return the number of bytes retrieved from the source buffer(s).
    152      */
    153     public final int bytesConsumed() {
    154         return bytesConsumed;
    155     }
    156 
    157     /**
    158      * Returns the number of bytes transferred to the destination buffer(s).
    159      *
    160      * @return the number of bytes transferred to the destination buffer(s).
    161      */
    162     public final int bytesProduced() {
    163         return bytesProduced;
    164     }
    165 
    166     @Override
    167     public String toString() {
    168         return "SSLEngineReport: Status = " + status + "  HandshakeStatus = " + handshakeStatus
    169                 + "\n                 bytesConsumed = " + bytesConsumed + " bytesProduced = "
    170                 + bytesProduced;
    171     }
    172 }
    173