1 module hunt.net.ssl.SSLEngineResult;
2 
3 import hunt.Exceptions;
4 
5 import std.conv;
6 
7 /**
8  * An <code>SSLEngineResult</code> enum describing the current
9  * handshaking state of this <code>SSLEngine</code>.
10  *
11  * @author Brad R. Wetmore
12  */
13 enum HandshakeStatus {
14 
15     /**
16      * The <code>SSLEngine</code> is not currently handshaking.
17      */
18     NOT_HANDSHAKING,
19 
20     /**
21      * The <code>SSLEngine</code> has just finished handshaking.
22      * <P>
23      * This value is only generated by a call to
24      * <code>SSLEngine.wrap()/unwrap()</code> when that call
25      * finishes a handshake.  It is never generated by
26      * <code>SSLEngine.getHandshakeStatus()</code>.
27      *
28      * @see SSLEngine#wrap(ByteBuffer, ByteBuffer)
29      * @see SSLEngine#unwrap(ByteBuffer, ByteBuffer)
30      * @see SSLEngine#getHandshakeStatus()
31      */
32     FINISHED,
33 
34     /**
35      * The <code>SSLEngine</code> needs the results of one (or more)
36      * delegated tasks before handshaking can continue.
37      *
38      * @see SSLEngine#getDelegatedTask()
39      */
40     NEED_TASK,
41 
42     /**
43      * The <code>SSLEngine</code> must send data to the remote side
44      * before handshaking can continue, so <code>SSLEngine.wrap()</code>
45      * should be called.
46      *
47      * @see SSLEngine#wrap(ByteBuffer, ByteBuffer)
48      */
49     NEED_WRAP,
50 
51     /**
52      * The <code>SSLEngine</code> needs to receive data from the
53      * remote side before handshaking can continue.
54      */
55     NEED_UNWRAP
56 }
57 
58 /**
59  * An encapsulation of the result state produced by
60  * <code>SSLEngine</code> I/O calls.
61  *
62  * <p> A <code>SSLEngine</code> provides a means for establishing
63  * secure communication sessions between two peers.  <code>SSLEngine</code>
64  * operations typically consume bytes from an input buffer and produce
65  * bytes in an output buffer.  This class provides operational result
66  * values describing the state of the <code>SSLEngine</code>, including
67  * indications of what operations are needed to finish an
68  * ongoing handshake.  Lastly, it reports the number of bytes consumed
69  * and produced as a result of this operation.
70  *
71  * @see SSLEngine
72  * @see SSLEngine#wrap(ByteBuffer, ByteBuffer)
73  * @see SSLEngine#unwrap(ByteBuffer, ByteBuffer)
74  *
75  * @author Brad R. Wetmore
76  */
77 
78 class SSLEngineResult {
79 
80     /**
81      * An <code>SSLEngineResult</code> enum describing the overall result
82      * of the <code>SSLEngine</code> operation.
83      *
84      * The <code>Status</code> value does not reflect the
85      * state of a <code>SSLEngine</code> handshake currently
86      * in progress.  The <code>SSLEngineResult's HandshakeStatus</code>
87      * should be consulted for that information.
88      *
89      * @author Brad R. Wetmore
90      */
91     static enum Status {
92 
93         /**
94          * The <code>SSLEngine</code> was not able to unwrap the
95          * incoming data because there were not enough source bytes
96          * available to make a complete packet.
97          *
98          * <P>
99          * Repeat the call once more bytes are available.
100          */
101         BUFFER_UNDERFLOW,
102 
103         /**
104          * The <code>SSLEngine</code> was not able to process the
105          * operation because there are not enough bytes available in the
106          * destination buffer to hold the result.
107          * <P>
108          * Repeat the call once more bytes are available.
109          *
110          * @see SSLSession#getPacketBufferSize()
111          * @see SSLSession#getApplicationBufferSize()
112          */
113         BUFFER_OVERFLOW,
114 
115         /**
116          * The <code>SSLEngine</code> completed the operation, and
117          * is available to process similar calls.
118          */
119         OK,
120 
121         /**
122          * The operation just closed this side of the
123          * <code>SSLEngine</code>, or the operation
124          * could not be completed because it was already closed.
125          */
126         CLOSED
127     }
128 
129     
130 
131 
132     private Status status;
133     private HandshakeStatus handshakeStatus;
134     private int _bytesConsumed;
135     private int _bytesProduced;
136 
137     /**
138      * Initializes a new instance of this class.
139      *
140      * @param   status
141      *          the return value of the operation.
142      *
143      * @param   handshakeStatus
144      *          the current handshaking status.
145      *
146      * @param   bytesConsumed
147      *          the number of bytes consumed from the source ByteBuffer
148      *
149      * @param   bytesProduced
150      *          the number of bytes placed into the destination ByteBuffer
151      *
152      * @throws  IllegalArgumentException
153      *          if the <code>status</code> or <code>handshakeStatus</code>
154      *          arguments are null, or if <code>bytesConsumed</code> or
155      *          <code>bytesProduced</code> is negative.
156      */
157     this(Status status, HandshakeStatus handshakeStatus,
158             int bytesConsumed, int bytesProduced) {
159 
160         if ((bytesConsumed < 0) || (bytesProduced < 0)) {
161             throw new IllegalArgumentException("Invalid Parameter(s)");
162         }
163 
164         this.status = status;
165         this.handshakeStatus = handshakeStatus;
166         this._bytesConsumed = bytesConsumed;
167         this._bytesProduced = bytesProduced;
168     }
169 
170     /**
171      * Gets the return value of this <code>SSLEngine</code> operation.
172      *
173      * @return  the return value
174      */
175     Status getStatus() {
176         return status;
177     }
178 
179     /**
180      * Gets the handshake status of this <code>SSLEngine</code>
181      * operation.
182      *
183      * @return  the handshake status
184      */
185     HandshakeStatus getHandshakeStatus() {
186         return handshakeStatus;
187     }
188 
189     /**
190      * Returns the number of bytes consumed from the input buffer.
191      *
192      * @return  the number of bytes consumed.
193      */
194     int bytesConsumed() {
195         return _bytesConsumed;
196     }
197 
198     /**
199      * Returns the number of bytes written to the output buffer.
200      *
201      * @return  the number of bytes produced
202      */
203     int bytesProduced() {
204         return _bytesProduced;
205     }
206 
207     /**
208      * Returns a string representation of this object.
209      */
210     override string toString() {
211         return "{Status = " ~ status.to!string() ~
212             ", HandshakeStatus = " ~ handshakeStatus.to!string() ~
213             ", bytesConsumed = " ~ _bytesConsumed.to!string() ~
214             ", bytesProduced = " ~ _bytesProduced.to!string() ~
215             "}";
216     }
217 }