2
|
1 |
/*
|
56542
|
2 |
* Copyright (c) 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 |
|
|
28 |
import java.io.IOException;
|
56542
|
29 |
import java.nio.ByteBuffer;
|
|
30 |
import java.text.MessageFormat;
|
2
|
31 |
import java.util.*;
|
56542
|
32 |
|
|
33 |
import sun.security.ssl.SSLHandshake.HandshakeMessage;
|
|
34 |
import sun.security.util.HexDumpEncoder;
|
2
|
35 |
|
|
36 |
/**
|
56542
|
37 |
* SSL/(D)TLS extensions in a handshake message.
|
2
|
38 |
*/
|
56542
|
39 |
final class SSLExtensions {
|
|
40 |
private final HandshakeMessage handshakeMessage;
|
|
41 |
private Map<SSLExtension, byte[]> extMap = new LinkedHashMap<>();
|
2
|
42 |
private int encodedLength;
|
|
43 |
|
56542
|
44 |
// Extension map for debug logging
|
|
45 |
private final Map<Integer, byte[]> logMap =
|
|
46 |
SSLLogger.isOn ? null : new LinkedHashMap<>();
|
|
47 |
|
|
48 |
SSLExtensions(HandshakeMessage handshakeMessage) {
|
|
49 |
this.handshakeMessage = handshakeMessage;
|
|
50 |
this.encodedLength = 2; // 2: the length of the extensions.
|
2
|
51 |
}
|
|
52 |
|
56542
|
53 |
SSLExtensions(HandshakeMessage hm,
|
|
54 |
ByteBuffer m, SSLExtension[] extensions) throws IOException {
|
|
55 |
this.handshakeMessage = hm;
|
|
56 |
|
|
57 |
int len = Record.getInt16(m);
|
|
58 |
encodedLength = len + 2; // 2: the length of the extensions.
|
2
|
59 |
while (len > 0) {
|
56542
|
60 |
int extId = Record.getInt16(m);
|
|
61 |
int extLen = Record.getInt16(m);
|
|
62 |
if (extLen > m.remaining()) {
|
|
63 |
hm.handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,
|
|
64 |
"Error parsing extension (" + extId +
|
|
65 |
"): no sufficient data");
|
|
66 |
}
|
|
67 |
|
|
68 |
SSLHandshake handshakeType = hm.handshakeType();
|
|
69 |
if (SSLExtension.isConsumable(extId) &&
|
|
70 |
SSLExtension.valueOf(handshakeType, extId) == null) {
|
|
71 |
hm.handshakeContext.conContext.fatal(
|
|
72 |
Alert.UNSUPPORTED_EXTENSION,
|
|
73 |
"extension (" + extId +
|
|
74 |
") should not be presented in " + handshakeType.name);
|
|
75 |
}
|
|
76 |
|
|
77 |
boolean isSupported = false;
|
|
78 |
for (SSLExtension extension : extensions) {
|
|
79 |
if ((extension.id != extId) ||
|
|
80 |
(extension.onLoadConcumer == null)) {
|
|
81 |
continue;
|
|
82 |
}
|
|
83 |
|
|
84 |
if (extension.handshakeType != handshakeType) {
|
|
85 |
hm.handshakeContext.conContext.fatal(
|
|
86 |
Alert.UNSUPPORTED_EXTENSION,
|
|
87 |
"extension (" + extId + ") should not be " +
|
|
88 |
"presented in " + handshakeType.name);
|
|
89 |
}
|
|
90 |
|
|
91 |
byte[] extData = new byte[extLen];
|
|
92 |
m.get(extData);
|
|
93 |
extMap.put(extension, extData);
|
|
94 |
if (logMap != null) {
|
|
95 |
logMap.put(extId, extData);
|
|
96 |
}
|
|
97 |
|
|
98 |
isSupported = true;
|
|
99 |
break;
|
2
|
100 |
}
|
56542
|
101 |
|
|
102 |
if (!isSupported) {
|
|
103 |
if (logMap != null) {
|
|
104 |
// cache the extension for debug logging
|
|
105 |
byte[] extData = new byte[extLen];
|
|
106 |
m.get(extData);
|
|
107 |
logMap.put(extId, extData);
|
|
108 |
|
|
109 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
|
|
110 |
SSLLogger.fine(
|
|
111 |
"Ignore unknown or unsupported extension",
|
|
112 |
toString(extId, extData));
|
|
113 |
}
|
|
114 |
} else {
|
|
115 |
// ignore the extension
|
|
116 |
int pos = m.position() + extLen;
|
|
117 |
m.position(pos);
|
|
118 |
}
|
|
119 |
}
|
|
120 |
|
|
121 |
len -= extLen + 4;
|
2
|
122 |
}
|
56542
|
123 |
}
|
|
124 |
|
|
125 |
byte[] get(SSLExtension ext) {
|
|
126 |
return extMap.get(ext);
|
|
127 |
}
|
|
128 |
|
|
129 |
/**
|
|
130 |
* Consume the specified extensions.
|
|
131 |
*/
|
|
132 |
void consumeOnLoad(HandshakeContext context,
|
|
133 |
SSLExtension[] extensions) throws IOException {
|
|
134 |
for (SSLExtension extension : extensions) {
|
|
135 |
if (context.negotiatedProtocol != null &&
|
|
136 |
!extension.isAvailable(context.negotiatedProtocol)) {
|
|
137 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
|
|
138 |
SSLLogger.fine(
|
|
139 |
"Ignore unsupported extension: " + extension.name);
|
|
140 |
}
|
|
141 |
continue;
|
|
142 |
// context.conContext.fatal(Alert.UNSUPPORTED_EXTENSION,
|
|
143 |
// context.negotiatedProtocol + " does not support " +
|
|
144 |
// extension + " extension");
|
|
145 |
}
|
|
146 |
|
|
147 |
if (!extMap.containsKey(extension)) {
|
|
148 |
if (extension.onLoadAbsence != null) {
|
|
149 |
extension.absent(context, handshakeMessage);
|
|
150 |
} else if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
|
|
151 |
SSLLogger.fine(
|
|
152 |
"Ignore unavailable extension: " + extension.name);
|
|
153 |
}
|
|
154 |
continue;
|
|
155 |
}
|
|
156 |
|
|
157 |
|
|
158 |
if (extension.onLoadConcumer == null) {
|
|
159 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
|
|
160 |
SSLLogger.warning(
|
|
161 |
"Ignore unsupported extension: " + extension.name);
|
|
162 |
}
|
|
163 |
continue;
|
|
164 |
}
|
|
165 |
|
|
166 |
ByteBuffer m = ByteBuffer.wrap(extMap.get(extension));
|
|
167 |
extension.consumeOnLoad(context, handshakeMessage, m);
|
|
168 |
|
|
169 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
|
|
170 |
SSLLogger.fine("Consumed extension: " + extension.name);
|
|
171 |
}
|
2
|
172 |
}
|
|
173 |
}
|
|
174 |
|
56542
|
175 |
/**
|
|
176 |
* Consider impact of the specified extensions.
|
|
177 |
*/
|
|
178 |
void consumeOnTrade(HandshakeContext context,
|
|
179 |
SSLExtension[] extensions) throws IOException {
|
|
180 |
for (SSLExtension extension : extensions) {
|
|
181 |
if (!extMap.containsKey(extension)) {
|
|
182 |
// No impact could be expected, so just ignore the absence.
|
|
183 |
continue;
|
|
184 |
}
|
2
|
185 |
|
56542
|
186 |
if (extension.onTradeConsumer == null) {
|
|
187 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
|
|
188 |
SSLLogger.warning(
|
|
189 |
"Ignore impact of unsupported extension: " +
|
|
190 |
extension.name);
|
|
191 |
}
|
|
192 |
continue;
|
|
193 |
}
|
2
|
194 |
|
56542
|
195 |
extension.consumeOnTrade(context, handshakeMessage);
|
|
196 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
|
|
197 |
SSLLogger.fine("Populated with extension: " + extension.name);
|
2
|
198 |
}
|
|
199 |
}
|
|
200 |
}
|
|
201 |
|
56542
|
202 |
/**
|
|
203 |
* Produce extension values for the specified extensions.
|
|
204 |
*/
|
|
205 |
void produce(HandshakeContext context,
|
|
206 |
SSLExtension[] extensions) throws IOException {
|
|
207 |
for (SSLExtension extension : extensions) {
|
|
208 |
if (extMap.containsKey(extension)) {
|
|
209 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
|
|
210 |
SSLLogger.fine(
|
|
211 |
"Ignore, duplicated extension: " +
|
|
212 |
extension.name);
|
|
213 |
}
|
|
214 |
continue;
|
2
|
215 |
}
|
56542
|
216 |
|
|
217 |
if (extension.networkProducer == null) {
|
|
218 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
|
|
219 |
SSLLogger.warning(
|
|
220 |
"Ignore, no extension producer defined: " +
|
|
221 |
extension.name);
|
|
222 |
}
|
|
223 |
continue;
|
|
224 |
}
|
2
|
225 |
|
56542
|
226 |
byte[] encoded = extension.produce(context, handshakeMessage);
|
|
227 |
if (encoded != null) {
|
|
228 |
extMap.put(extension, encoded);
|
|
229 |
encodedLength += encoded.length + 4; // extension_type (2)
|
|
230 |
// extension_data length(2)
|
|
231 |
} else if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
|
|
232 |
// The extension is not available in the context.
|
|
233 |
SSLLogger.fine(
|
|
234 |
"Ignore, context unavailable extension: " +
|
|
235 |
extension.name);
|
|
236 |
}
|
2
|
237 |
}
|
|
238 |
}
|
|
239 |
|
56542
|
240 |
/**
|
|
241 |
* Produce extension values for the specified extensions, replacing if
|
|
242 |
* there is an existing extension value for a specified extension.
|
|
243 |
*/
|
|
244 |
void reproduce(HandshakeContext context,
|
|
245 |
SSLExtension[] extensions) throws IOException {
|
|
246 |
for (SSLExtension extension : extensions) {
|
|
247 |
if (extension.networkProducer == null) {
|
|
248 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
|
|
249 |
SSLLogger.warning(
|
|
250 |
"Ignore, no extension producer defined: " +
|
|
251 |
extension.name);
|
|
252 |
}
|
|
253 |
continue;
|
|
254 |
}
|
|
255 |
|
|
256 |
byte[] encoded = extension.produce(context, handshakeMessage);
|
|
257 |
if (encoded != null) {
|
|
258 |
if (extMap.containsKey(extension)) {
|
|
259 |
byte[] old = extMap.replace(extension, encoded);
|
|
260 |
if (old != null) {
|
|
261 |
encodedLength -= old.length + 4;
|
|
262 |
}
|
|
263 |
encodedLength += encoded.length + 4;
|
|
264 |
} else {
|
|
265 |
extMap.put(extension, encoded);
|
|
266 |
encodedLength += encoded.length + 4;
|
|
267 |
// extension_type (2)
|
|
268 |
// extension_data length(2)
|
|
269 |
}
|
|
270 |
} else if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
|
|
271 |
// The extension is not available in the context.
|
|
272 |
SSLLogger.fine(
|
|
273 |
"Ignore, context unavailable extension: " +
|
|
274 |
extension.name);
|
|
275 |
}
|
|
276 |
}
|
|
277 |
}
|
|
278 |
|
|
279 |
// Note that TLS 1.3 may use empty extensions. Please consider it while
|
|
280 |
// using this method.
|
|
281 |
int length() {
|
|
282 |
if (extMap.isEmpty()) {
|
|
283 |
return 0;
|
|
284 |
} else {
|
|
285 |
return encodedLength;
|
2
|
286 |
}
|
|
287 |
}
|
56542
|
288 |
|
|
289 |
// Note that TLS 1.3 may use empty extensions. Please consider it while
|
|
290 |
// using this method.
|
|
291 |
void send(HandshakeOutStream hos) throws IOException {
|
|
292 |
int extsLen = length();
|
|
293 |
if (extsLen == 0) {
|
|
294 |
return;
|
|
295 |
}
|
|
296 |
hos.putInt16(extsLen - 2);
|
|
297 |
// extensions must be sent in the order they appear in the enum
|
|
298 |
for (SSLExtension ext : SSLExtension.values()) {
|
|
299 |
byte[] extData = extMap.get(ext);
|
|
300 |
if (extData != null) {
|
|
301 |
hos.putInt16(ext.id);
|
|
302 |
hos.putBytes16(extData);
|
|
303 |
}
|
|
304 |
}
|
|
305 |
}
|
|
306 |
|
|
307 |
@Override
|
|
308 |
public String toString() {
|
|
309 |
if (extMap.isEmpty() && (logMap == null || logMap.isEmpty())) {
|
|
310 |
return "<no extension>";
|
|
311 |
} else {
|
|
312 |
StringBuilder builder = new StringBuilder(512);
|
|
313 |
if (logMap != null) {
|
|
314 |
for (Map.Entry<Integer, byte[]> en : logMap.entrySet()) {
|
|
315 |
SSLExtension ext = SSLExtension.valueOf(
|
|
316 |
handshakeMessage.handshakeType(), en.getKey());
|
|
317 |
if (builder.length() != 0) {
|
|
318 |
builder.append(",\n");
|
|
319 |
}
|
|
320 |
if (ext != null) {
|
|
321 |
builder.append(
|
|
322 |
ext.toString(ByteBuffer.wrap(en.getValue())));
|
|
323 |
} else {
|
|
324 |
builder.append(toString(en.getKey(), en.getValue()));
|
|
325 |
}
|
|
326 |
}
|
|
327 |
|
|
328 |
return builder.toString();
|
|
329 |
} else {
|
|
330 |
for (Map.Entry<SSLExtension, byte[]> en : extMap.entrySet()) {
|
|
331 |
if (builder.length() != 0) {
|
|
332 |
builder.append(",\n");
|
|
333 |
}
|
|
334 |
builder.append(
|
|
335 |
en.getKey().toString(ByteBuffer.wrap(en.getValue())));
|
|
336 |
}
|
|
337 |
|
|
338 |
return builder.toString();
|
|
339 |
}
|
|
340 |
}
|
|
341 |
}
|
|
342 |
|
|
343 |
private static String toString(int extId, byte[] extData) {
|
|
344 |
MessageFormat messageFormat = new MessageFormat(
|
|
345 |
"\"unknown extension ({0})\": '{'\n" +
|
|
346 |
"{1}\n" +
|
|
347 |
"'}'",
|
|
348 |
Locale.ENGLISH);
|
|
349 |
|
|
350 |
HexDumpEncoder hexEncoder = new HexDumpEncoder();
|
|
351 |
String encoded = hexEncoder.encodeBuffer(extData);
|
|
352 |
|
|
353 |
Object[] messageFields = {
|
|
354 |
extId,
|
|
355 |
Utilities.indent(encoded)
|
|
356 |
};
|
|
357 |
|
|
358 |
return messageFormat.format(messageFields);
|
|
359 |
}
|
2
|
360 |
}
|