author | chegar |
Sun, 17 Aug 2014 15:54:13 +0100 | |
changeset 25859 | 3317bb8137f4 |
parent 23010 | jdk/src/share/classes/sun/security/ssl/HandshakeOutStream.java@6dadb192ad81 |
child 30904 | ec0224270f90 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
21278
diff
changeset
|
2 |
* Copyright (c) 1996, 2013, 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.OutputStream; |
|
30 |
import java.io.IOException; |
|
31 |
||
32 |
/** |
|
33 |
* Output stream for handshake data. This is used only internally |
|
34 |
* to the SSL classes. |
|
35 |
* |
|
36 |
* MT note: one thread at a time is presumed be writing handshake |
|
37 |
* messages, but (after initial connection setup) it's possible to |
|
38 |
* have other threads reading/writing application data. It's the |
|
39 |
* SSLSocketImpl class that synchronizes record writes. |
|
40 |
* |
|
41 |
* @author David Brownell |
|
42 |
*/ |
|
4236 | 43 |
public class HandshakeOutStream extends OutputStream { |
2 | 44 |
|
45 |
private SSLSocketImpl socket; |
|
46 |
private SSLEngineImpl engine; |
|
47 |
||
48 |
OutputRecord r; |
|
49 |
||
50 |
HandshakeOutStream(ProtocolVersion protocolVersion, |
|
51 |
ProtocolVersion helloVersion, HandshakeHash handshakeHash, |
|
52 |
SSLSocketImpl socket) { |
|
53 |
this.socket = socket; |
|
54 |
r = new OutputRecord(Record.ct_handshake); |
|
55 |
init(protocolVersion, helloVersion, handshakeHash); |
|
56 |
} |
|
57 |
||
58 |
HandshakeOutStream(ProtocolVersion protocolVersion, |
|
59 |
ProtocolVersion helloVersion, HandshakeHash handshakeHash, |
|
60 |
SSLEngineImpl engine) { |
|
61 |
this.engine = engine; |
|
62 |
r = new EngineOutputRecord(Record.ct_handshake, engine); |
|
63 |
init(protocolVersion, helloVersion, handshakeHash); |
|
64 |
} |
|
65 |
||
66 |
private void init(ProtocolVersion protocolVersion, |
|
67 |
ProtocolVersion helloVersion, HandshakeHash handshakeHash) { |
|
68 |
r.setVersion(protocolVersion); |
|
69 |
r.setHelloVersion(helloVersion); |
|
70 |
r.setHandshakeHash(handshakeHash); |
|
71 |
} |
|
72 |
||
73 |
||
74 |
/* |
|
75 |
* Update the handshake data hashes ... mostly for use after a |
|
76 |
* client cert has been sent, so the cert verify message can be |
|
77 |
* constructed correctly yet without forcing extra I/O. In all |
|
78 |
* other cases, automatic hash calculation suffices. |
|
79 |
*/ |
|
80 |
void doHashes() { |
|
81 |
r.doHashes(); |
|
82 |
} |
|
83 |
||
84 |
/* |
|
85 |
* Write some data out onto the stream ... buffers as much as possible. |
|
86 |
* Hashes are updated automatically if something gets flushed to the |
|
87 |
* network (e.g. a big cert message etc). |
|
88 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14004
diff
changeset
|
89 |
@Override |
2 | 90 |
public void write(byte buf[], int off, int len) throws IOException { |
91 |
while (len > 0) { |
|
92 |
int howmuch = Math.min(len, r.availableDataBytes()); |
|
93 |
||
94 |
if (howmuch == 0) { |
|
95 |
flush(); |
|
96 |
} else { |
|
97 |
r.write(buf, off, howmuch); |
|
98 |
off += howmuch; |
|
99 |
len -= howmuch; |
|
100 |
} |
|
101 |
} |
|
102 |
} |
|
103 |
||
104 |
/* |
|
105 |
* write-a-byte |
|
106 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14004
diff
changeset
|
107 |
@Override |
2 | 108 |
public void write(int i) throws IOException { |
109 |
if (r.availableDataBytes() < 1) { |
|
110 |
flush(); |
|
111 |
} |
|
112 |
r.write(i); |
|
113 |
} |
|
114 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14004
diff
changeset
|
115 |
@Override |
2 | 116 |
public void flush() throws IOException { |
117 |
if (socket != null) { |
|
118 |
try { |
|
119 |
socket.writeRecord(r); |
|
120 |
} catch (IOException e) { |
|
121 |
// Had problems writing; check if there was an |
|
122 |
// alert from peer. If alert received, waitForClose |
|
123 |
// will throw an exception for the alert |
|
124 |
socket.waitForClose(true); |
|
125 |
||
126 |
// No alert was received, just rethrow exception |
|
127 |
throw e; |
|
128 |
} |
|
129 |
} else { // engine != null |
|
130 |
/* |
|
131 |
* Even if record might be empty, flush anyway in case |
|
132 |
* there is a finished handshake message that we need |
|
133 |
* to queue. |
|
134 |
*/ |
|
135 |
engine.writeRecord((EngineOutputRecord)r); |
|
136 |
} |
|
137 |
} |
|
138 |
||
139 |
/* |
|
140 |
* Tell the OutputRecord that a finished message was |
|
141 |
* contained either in this record or the one immeiately |
|
21278 | 142 |
* preceding it. We need to reliably pass back notifications |
143 |
* that a finish message occurred. |
|
2 | 144 |
*/ |
145 |
void setFinishedMsg() { |
|
146 |
assert(socket == null); |
|
147 |
||
148 |
((EngineOutputRecord)r).setFinishedMsg(); |
|
149 |
} |
|
150 |
||
151 |
/* |
|
152 |
* Put integers encoded in standard 8, 16, 24, and 32 bit |
|
153 |
* big endian formats. Note that OutputStream.write(int) only |
|
154 |
* writes the least significant 8 bits and ignores the rest. |
|
155 |
*/ |
|
156 |
||
157 |
void putInt8(int i) throws IOException { |
|
14004
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
158 |
checkOverflow(i, Record.OVERFLOW_OF_INT08); |
2 | 159 |
r.write(i); |
160 |
} |
|
161 |
||
162 |
void putInt16(int i) throws IOException { |
|
14004
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
163 |
checkOverflow(i, Record.OVERFLOW_OF_INT16); |
2 | 164 |
if (r.availableDataBytes() < 2) { |
165 |
flush(); |
|
166 |
} |
|
167 |
r.write(i >> 8); |
|
168 |
r.write(i); |
|
169 |
} |
|
170 |
||
171 |
void putInt24(int i) throws IOException { |
|
14004
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
172 |
checkOverflow(i, Record.OVERFLOW_OF_INT24); |
2 | 173 |
if (r.availableDataBytes() < 3) { |
174 |
flush(); |
|
175 |
} |
|
176 |
r.write(i >> 16); |
|
177 |
r.write(i >> 8); |
|
178 |
r.write(i); |
|
179 |
} |
|
180 |
||
181 |
void putInt32(int i) throws IOException { |
|
182 |
if (r.availableDataBytes() < 4) { |
|
183 |
flush(); |
|
184 |
} |
|
185 |
r.write(i >> 24); |
|
186 |
r.write(i >> 16); |
|
187 |
r.write(i >> 8); |
|
188 |
r.write(i); |
|
189 |
} |
|
190 |
||
191 |
/* |
|
192 |
* Put byte arrays with length encoded as 8, 16, 24 bit |
|
193 |
* integers in big-endian format. |
|
194 |
*/ |
|
195 |
void putBytes8(byte b[]) throws IOException { |
|
196 |
if (b == null) { |
|
197 |
putInt8(0); |
|
198 |
return; |
|
14004
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
199 |
} else { |
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
200 |
checkOverflow(b.length, Record.OVERFLOW_OF_INT08); |
2 | 201 |
} |
202 |
putInt8(b.length); |
|
203 |
write(b, 0, b.length); |
|
204 |
} |
|
205 |
||
4236 | 206 |
public void putBytes16(byte b[]) throws IOException { |
2 | 207 |
if (b == null) { |
208 |
putInt16(0); |
|
209 |
return; |
|
14004
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
210 |
} else { |
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
211 |
checkOverflow(b.length, Record.OVERFLOW_OF_INT16); |
2 | 212 |
} |
213 |
putInt16(b.length); |
|
214 |
write(b, 0, b.length); |
|
215 |
} |
|
216 |
||
217 |
void putBytes24(byte b[]) throws IOException { |
|
218 |
if (b == null) { |
|
219 |
putInt24(0); |
|
220 |
return; |
|
14004
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
221 |
} else { |
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
222 |
checkOverflow(b.length, Record.OVERFLOW_OF_INT24); |
2 | 223 |
} |
224 |
putInt24(b.length); |
|
225 |
write(b, 0, b.length); |
|
226 |
} |
|
14004
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
227 |
|
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
228 |
private void checkOverflow(int length, int overflow) { |
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
229 |
if (length >= overflow) { |
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
230 |
// internal_error alert will be triggered |
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
231 |
throw new RuntimeException( |
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
232 |
"Field length overflow, the field length (" + |
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
233 |
length + ") should be less than " + overflow); |
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
234 |
} |
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
235 |
} |
2 | 236 |
} |