author | xuelei |
Mon, 25 Jun 2018 13:41:39 -0700 | |
changeset 50768 | 68fa3d4026ea |
parent 47216 | 71c04702a3d5 |
child 51407 | 910f7b56592f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
50768 | 2 |
* Copyright (c) 1996, 2018, 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 |
package sun.security.ssl; |
|
27 |
||
30904 | 28 |
import java.io.ByteArrayOutputStream; |
2 | 29 |
import java.io.IOException; |
30 |
||
31 |
/** |
|
32 |
* Output stream for handshake data. This is used only internally |
|
33 |
* to the SSL classes. |
|
34 |
* |
|
35 |
* MT note: one thread at a time is presumed be writing handshake |
|
36 |
* messages, but (after initial connection setup) it's possible to |
|
37 |
* have other threads reading/writing application data. It's the |
|
38 |
* SSLSocketImpl class that synchronizes record writes. |
|
39 |
* |
|
40 |
* @author David Brownell |
|
41 |
*/ |
|
30904 | 42 |
public class HandshakeOutStream extends ByteArrayOutputStream { |
2 | 43 |
|
30904 | 44 |
OutputRecord outputRecord; // May be null if not actually used to |
45 |
// output handshake message records. |
|
2 | 46 |
|
30904 | 47 |
HandshakeOutStream(OutputRecord outputRecord) { |
48 |
super(); |
|
49 |
this.outputRecord = outputRecord; |
|
2 | 50 |
} |
51 |
||
30904 | 52 |
// Complete a handshakin message writing. Called by HandshakeMessage. |
53 |
void complete() throws IOException { |
|
54 |
if (size() < 4) { // 4: handshake message header size |
|
55 |
// internal_error alert will be triggered |
|
56 |
throw new RuntimeException("handshake message is not available"); |
|
57 |
} |
|
2 | 58 |
|
50768 | 59 |
if (outputRecord != null) { |
60 |
outputRecord.encodeHandshake(buf, 0, count); |
|
30904 | 61 |
|
50768 | 62 |
// reset the byte array output stream |
63 |
reset(); |
|
64 |
} // otherwise, the handshake outstream is temporarily used only. |
|
2 | 65 |
} |
66 |
||
30904 | 67 |
// |
68 |
// overridden ByteArrayOutputStream methods |
|
69 |
// |
|
2 | 70 |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14004
diff
changeset
|
71 |
@Override |
30904 | 72 |
public void write(byte[] b, int off, int len) { |
73 |
// The maximum fragment size is 24 bytes. |
|
74 |
checkOverflow(len, Record.OVERFLOW_OF_INT24); |
|
75 |
super.write(b, off, len); |
|
2 | 76 |
} |
77 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14004
diff
changeset
|
78 |
@Override |
2 | 79 |
public void flush() throws IOException { |
50768 | 80 |
if (outputRecord != null) { |
81 |
outputRecord.flush(); |
|
82 |
} |
|
2 | 83 |
} |
84 |
||
30904 | 85 |
// |
86 |
// handshake output stream management functions |
|
87 |
// |
|
2 | 88 |
|
89 |
/* |
|
90 |
* Put integers encoded in standard 8, 16, 24, and 32 bit |
|
91 |
* big endian formats. Note that OutputStream.write(int) only |
|
92 |
* writes the least significant 8 bits and ignores the rest. |
|
93 |
*/ |
|
94 |
void putInt8(int i) throws IOException { |
|
14004
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
95 |
checkOverflow(i, Record.OVERFLOW_OF_INT08); |
30904 | 96 |
super.write(i); |
2 | 97 |
} |
98 |
||
99 |
void putInt16(int i) throws IOException { |
|
14004
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
100 |
checkOverflow(i, Record.OVERFLOW_OF_INT16); |
30904 | 101 |
super.write(i >> 8); |
102 |
super.write(i); |
|
2 | 103 |
} |
104 |
||
105 |
void putInt24(int i) throws IOException { |
|
14004
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
106 |
checkOverflow(i, Record.OVERFLOW_OF_INT24); |
30904 | 107 |
super.write(i >> 16); |
108 |
super.write(i >> 8); |
|
109 |
super.write(i); |
|
2 | 110 |
} |
111 |
||
50768 | 112 |
void putInt32(int i) throws IOException { |
113 |
super.write(i >> 24); |
|
114 |
super.write(i >> 16); |
|
115 |
super.write(i >> 8); |
|
116 |
super.write(i); |
|
117 |
} |
|
118 |
||
2 | 119 |
/* |
120 |
* Put byte arrays with length encoded as 8, 16, 24 bit |
|
121 |
* integers in big-endian format. |
|
122 |
*/ |
|
30904 | 123 |
void putBytes8(byte[] b) throws IOException { |
2 | 124 |
if (b == null) { |
125 |
putInt8(0); |
|
14004
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
126 |
} else { |
30904 | 127 |
putInt8(b.length); |
128 |
super.write(b, 0, b.length); |
|
2 | 129 |
} |
130 |
} |
|
131 |
||
31538
0981099a3e54
8130022: Use Java-style array declarations consistently
igerasim
parents:
30904
diff
changeset
|
132 |
public void putBytes16(byte[] b) throws IOException { |
2 | 133 |
if (b == null) { |
134 |
putInt16(0); |
|
14004
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
135 |
} else { |
30904 | 136 |
putInt16(b.length); |
137 |
super.write(b, 0, b.length); |
|
2 | 138 |
} |
139 |
} |
|
140 |
||
31538
0981099a3e54
8130022: Use Java-style array declarations consistently
igerasim
parents:
30904
diff
changeset
|
141 |
void putBytes24(byte[] b) throws IOException { |
2 | 142 |
if (b == null) { |
143 |
putInt24(0); |
|
14004
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
144 |
} else { |
30904 | 145 |
putInt24(b.length); |
146 |
super.write(b, 0, b.length); |
|
2 | 147 |
} |
148 |
} |
|
14004
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
149 |
|
30904 | 150 |
/* |
151 |
* Does the specified length overflow the limitation? |
|
152 |
*/ |
|
153 |
private static void checkOverflow(int length, int limit) { |
|
154 |
if (length >= limit) { |
|
14004
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
155 |
// internal_error alert will be triggered |
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
156 |
throw new RuntimeException( |
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
157 |
"Field length overflow, the field length (" + |
30904 | 158 |
length + ") should be less than " + limit); |
14004
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
159 |
} |
611031f93e76
7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents:
5506
diff
changeset
|
160 |
} |
2 | 161 |
} |