author | lana |
Tue, 23 Oct 2012 11:29:53 -0700 | |
changeset 14229 | 40fbffe104bd |
parent 14194 | 971f46db533d |
parent 14212 | faa4afc89a09 |
child 14664 | e71aa0962e70 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14212
faa4afc89a09
7186286: TLS implementation to better adhere to RFC
xuelei
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
||
27 |
package sun.security.ssl; |
|
28 |
||
29 |
import java.io.InputStream; |
|
30 |
import java.io.IOException; |
|
31 |
import java.security.MessageDigest; |
|
32 |
||
33 |
import javax.net.ssl.SSLException; |
|
34 |
||
35 |
/** |
|
36 |
* InputStream for handshake data, used internally only. Contains the |
|
37 |
* handshake message buffer and methods to parse them. |
|
38 |
* |
|
39 |
* Once a new handshake record arrives, it is buffered in this class until |
|
40 |
* processed by the Handshaker. The buffer may also contain incomplete |
|
41 |
* handshake messages in case the message is split across multiple records. |
|
42 |
* Handshaker.process_record deals with all that. It may also contain |
|
43 |
* handshake messages larger than the default buffer size (e.g. large |
|
44 |
* certificate messages). The buffer is grown dynamically to handle that |
|
45 |
* (see InputRecord.queueHandshake()). |
|
46 |
* |
|
47 |
* Note that the InputRecord used as a buffer here is separate from the |
|
48 |
* AppInStream.r, which is where data from the socket is initially read |
|
49 |
* into. This is because once the initial handshake has been completed, |
|
50 |
* handshake and application data messages may be interleaved arbitrarily |
|
51 |
* and must be processed independently. |
|
52 |
* |
|
53 |
* @author David Brownell |
|
54 |
*/ |
|
4236 | 55 |
public class HandshakeInStream extends InputStream { |
2 | 56 |
|
57 |
InputRecord r; |
|
58 |
||
59 |
/* |
|
60 |
* Construct the stream; we'll be accumulating hashes of the |
|
61 |
* input records using two sets of digests. |
|
62 |
*/ |
|
63 |
HandshakeInStream(HandshakeHash handshakeHash) { |
|
64 |
r = new InputRecord(); |
|
65 |
r.setHandshakeHash(handshakeHash); |
|
66 |
} |
|
67 |
||
68 |
||
69 |
// overridden InputStream methods |
|
70 |
||
71 |
/* |
|
72 |
* Return the number of bytes available for read(). |
|
73 |
* |
|
74 |
* Note that this returns the bytes remaining in the buffer, not |
|
75 |
* the bytes remaining in the current handshake message. |
|
76 |
*/ |
|
77 |
public int available() { |
|
78 |
return r.available(); |
|
79 |
} |
|
80 |
||
81 |
/* |
|
82 |
* Get a byte of handshake data. |
|
83 |
*/ |
|
84 |
public int read() throws IOException { |
|
85 |
int n = r.read(); |
|
86 |
if (n == -1) { |
|
87 |
throw new SSLException("Unexpected end of handshake data"); |
|
88 |
} |
|
89 |
return n; |
|
90 |
} |
|
91 |
||
92 |
/* |
|
93 |
* Get a bunch of bytes of handshake data. |
|
94 |
*/ |
|
95 |
public int read(byte b [], int off, int len) throws IOException { |
|
96 |
// we read from a ByteArrayInputStream, it always returns the |
|
97 |
// data in a single read if enough is available |
|
98 |
int n = r.read(b, off, len); |
|
99 |
if (n != len) { |
|
100 |
throw new SSLException("Unexpected end of handshake data"); |
|
101 |
} |
|
102 |
return n; |
|
103 |
} |
|
104 |
||
105 |
/* |
|
106 |
* Skip some handshake data. |
|
107 |
*/ |
|
108 |
public long skip(long n) throws IOException { |
|
109 |
return r.skip(n); |
|
110 |
} |
|
111 |
||
112 |
/* |
|
113 |
* Mark/ reset code, implemented using InputRecord mark/ reset. |
|
114 |
* |
|
115 |
* Note that it currently provides only a limited mark functionality |
|
116 |
* and should be used with care (once a new handshake record has been |
|
117 |
* read, data that has already been consumed is lost even if marked). |
|
118 |
*/ |
|
119 |
||
120 |
public void mark(int readlimit) { |
|
121 |
r.mark(readlimit); |
|
122 |
} |
|
123 |
||
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
5506
diff
changeset
|
124 |
@Override |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
5506
diff
changeset
|
125 |
public void reset() throws IOException { |
2 | 126 |
r.reset(); |
127 |
} |
|
128 |
||
129 |
public boolean markSupported() { |
|
130 |
return true; |
|
131 |
} |
|
132 |
||
133 |
||
134 |
// handshake management functions |
|
135 |
||
136 |
/* |
|
137 |
* Here's an incoming record with handshake data. Queue the contents; |
|
138 |
* it might be one or more entire messages, complete a message that's |
|
139 |
* partly queued, or both. |
|
140 |
*/ |
|
141 |
void incomingRecord(InputRecord in) throws IOException { |
|
142 |
r.queueHandshake(in); |
|
143 |
} |
|
144 |
||
145 |
/* |
|
146 |
* Hash any data we've consumed but not yet hashed. Useful mostly |
|
147 |
* for processing client certificate messages (so we can check the |
|
148 |
* immediately following cert verify message) and finished messages |
|
149 |
* (so we can compute our own finished message). |
|
150 |
*/ |
|
151 |
void digestNow() { |
|
152 |
r.doHashes(); |
|
153 |
} |
|
154 |
||
155 |
/* |
|
156 |
* Do more than skip that handshake data ... totally ignore it. |
|
157 |
* The difference is that the data does not get hashed. |
|
158 |
*/ |
|
159 |
void ignore(int n) { |
|
160 |
r.ignore(n); |
|
161 |
} |
|
162 |
||
163 |
||
164 |
// Message parsing methods |
|
165 |
||
166 |
/* |
|
167 |
* Read 8, 16, 24, and 32 bit SSL integer data types, encoded |
|
168 |
* in standard big-endian form. |
|
169 |
*/ |
|
170 |
||
171 |
int getInt8() throws IOException { |
|
172 |
return read(); |
|
173 |
} |
|
174 |
||
175 |
int getInt16() throws IOException { |
|
176 |
return (getInt8() << 8) | getInt8(); |
|
177 |
} |
|
178 |
||
179 |
int getInt24() throws IOException { |
|
180 |
return (getInt8() << 16) | (getInt8() << 8) | getInt8(); |
|
181 |
} |
|
182 |
||
183 |
int getInt32() throws IOException { |
|
184 |
return (getInt8() << 24) | (getInt8() << 16) |
|
185 |
| (getInt8() << 8) | getInt8(); |
|
186 |
} |
|
187 |
||
188 |
/* |
|
189 |
* Read byte vectors with 8, 16, and 24 bit length encodings. |
|
190 |
*/ |
|
191 |
||
192 |
byte[] getBytes8() throws IOException { |
|
193 |
int len = getInt8(); |
|
14212
faa4afc89a09
7186286: TLS implementation to better adhere to RFC
xuelei
parents:
5506
diff
changeset
|
194 |
verifyLength(len); |
2 | 195 |
byte b[] = new byte[len]; |
196 |
||
197 |
read(b, 0, len); |
|
198 |
return b; |
|
199 |
} |
|
200 |
||
4236 | 201 |
public byte[] getBytes16() throws IOException { |
2 | 202 |
int len = getInt16(); |
14212
faa4afc89a09
7186286: TLS implementation to better adhere to RFC
xuelei
parents:
5506
diff
changeset
|
203 |
verifyLength(len); |
2 | 204 |
byte b[] = new byte[len]; |
205 |
||
206 |
read(b, 0, len); |
|
207 |
return b; |
|
208 |
} |
|
209 |
||
210 |
byte[] getBytes24() throws IOException { |
|
211 |
int len = getInt24(); |
|
14212
faa4afc89a09
7186286: TLS implementation to better adhere to RFC
xuelei
parents:
5506
diff
changeset
|
212 |
verifyLength(len); |
2 | 213 |
byte b[] = new byte[len]; |
214 |
||
215 |
read(b, 0, len); |
|
216 |
return b; |
|
217 |
} |
|
218 |
||
14212
faa4afc89a09
7186286: TLS implementation to better adhere to RFC
xuelei
parents:
5506
diff
changeset
|
219 |
// Is a length greater than available bytes in the record? |
faa4afc89a09
7186286: TLS implementation to better adhere to RFC
xuelei
parents:
5506
diff
changeset
|
220 |
private void verifyLength(int len) throws SSLException { |
faa4afc89a09
7186286: TLS implementation to better adhere to RFC
xuelei
parents:
5506
diff
changeset
|
221 |
if (len > available()) { |
faa4afc89a09
7186286: TLS implementation to better adhere to RFC
xuelei
parents:
5506
diff
changeset
|
222 |
throw new SSLException( |
faa4afc89a09
7186286: TLS implementation to better adhere to RFC
xuelei
parents:
5506
diff
changeset
|
223 |
"Not enough data to fill declared vector size"); |
faa4afc89a09
7186286: TLS implementation to better adhere to RFC
xuelei
parents:
5506
diff
changeset
|
224 |
} |
faa4afc89a09
7186286: TLS implementation to better adhere to RFC
xuelei
parents:
5506
diff
changeset
|
225 |
} |
faa4afc89a09
7186286: TLS implementation to better adhere to RFC
xuelei
parents:
5506
diff
changeset
|
226 |
|
2 | 227 |
} |