author | wetmore |
Tue, 21 Aug 2018 11:30:48 -0700 | |
changeset 51574 | ed52ea83f830 |
parent 50768 | 68fa3d4026ea |
child 52151 | af6fb2cb82ae |
permissions | -rw-r--r-- |
50768 | 1 |
/* |
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
|
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 |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
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 |
* |
|
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. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.security.ssl; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.nio.ByteBuffer; |
|
30 |
import java.util.List; |
|
31 |
import sun.security.ssl.SSLExtension.ExtensionConsumer; |
|
32 |
import sun.security.ssl.SSLHandshake.HandshakeMessage; |
|
33 |
import sun.security.ssl.SignatureAlgorithmsExtension.SignatureSchemesSpec; |
|
34 |
||
35 |
/** |
|
36 |
* Pack of the "signature_algorithms_cert" extensions. |
|
37 |
*/ |
|
38 |
final class CertSignAlgsExtension { |
|
39 |
static final HandshakeProducer chNetworkProducer = |
|
40 |
new CHCertSignatureSchemesProducer(); |
|
41 |
static final ExtensionConsumer chOnLoadConsumer = |
|
42 |
new CHCertSignatureSchemesConsumer(); |
|
43 |
static final HandshakeConsumer chOnTradeConsumer = |
|
44 |
new CHCertSignatureSchemesUpdate(); |
|
45 |
||
46 |
static final HandshakeProducer crNetworkProducer = |
|
47 |
new CRCertSignatureSchemesProducer(); |
|
48 |
static final ExtensionConsumer crOnLoadConsumer = |
|
49 |
new CRCertSignatureSchemesConsumer(); |
|
50 |
static final HandshakeConsumer crOnTradeConsumer = |
|
51 |
new CRCertSignatureSchemesUpdate(); |
|
52 |
||
53 |
static final SSLStringizer ssStringizer = |
|
54 |
new CertSignatureSchemesStringizer(); |
|
55 |
||
56 |
private static final |
|
57 |
class CertSignatureSchemesStringizer implements SSLStringizer { |
|
58 |
@Override |
|
59 |
public String toString(ByteBuffer buffer) { |
|
60 |
try { |
|
61 |
return (new SignatureSchemesSpec(buffer)).toString(); |
|
62 |
} catch (IOException ioe) { |
|
63 |
// For debug logging only, so please swallow exceptions. |
|
64 |
return ioe.getMessage(); |
|
65 |
} |
|
66 |
} |
|
67 |
} |
|
68 |
||
69 |
/** |
|
70 |
* Network data producer of a "signature_algorithms_cert" extension in |
|
71 |
* the ClientHello handshake message. |
|
72 |
*/ |
|
73 |
private static final |
|
74 |
class CHCertSignatureSchemesProducer implements HandshakeProducer { |
|
75 |
// Prevent instantiation of this class. |
|
76 |
private CHCertSignatureSchemesProducer() { |
|
77 |
// blank |
|
78 |
} |
|
79 |
||
80 |
@Override |
|
81 |
public byte[] produce(ConnectionContext context, |
|
82 |
HandshakeMessage message) throws IOException { |
|
83 |
// The producing happens in client side only. |
|
84 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
85 |
||
86 |
// Is it a supported and enabled extension? |
|
87 |
if (!chc.sslConfig.isAvailable( |
|
88 |
SSLExtension.CH_SIGNATURE_ALGORITHMS_CERT)) { |
|
89 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
90 |
SSLLogger.fine( |
|
91 |
"Ignore unavailable " + |
|
92 |
"signature_algorithms_cert extension"); |
|
93 |
} |
|
94 |
||
95 |
return null; // ignore the extension |
|
96 |
} |
|
97 |
||
98 |
// Produce the extension. |
|
99 |
if (chc.localSupportedSignAlgs == null) { |
|
100 |
chc.localSupportedSignAlgs = |
|
101 |
SignatureScheme.getSupportedAlgorithms( |
|
102 |
chc.algorithmConstraints, chc.activeProtocols); |
|
103 |
} |
|
104 |
||
105 |
int vectorLen = SignatureScheme.sizeInRecord() * |
|
106 |
chc.localSupportedSignAlgs.size(); |
|
107 |
byte[] extData = new byte[vectorLen + 2]; |
|
108 |
ByteBuffer m = ByteBuffer.wrap(extData); |
|
109 |
Record.putInt16(m, vectorLen); |
|
110 |
for (SignatureScheme ss : chc.localSupportedSignAlgs) { |
|
111 |
Record.putInt16(m, ss.id); |
|
112 |
} |
|
113 |
||
114 |
// Update the context. |
|
115 |
chc.handshakeExtensions.put( |
|
116 |
SSLExtension.CH_SIGNATURE_ALGORITHMS_CERT, |
|
117 |
new SignatureSchemesSpec(chc.localSupportedSignAlgs)); |
|
118 |
||
119 |
return extData; |
|
120 |
} |
|
121 |
} |
|
122 |
||
123 |
/** |
|
124 |
* Network data consumer of a "signature_algorithms_cert" extension in |
|
125 |
* the ClientHello handshake message. |
|
126 |
*/ |
|
127 |
private static final |
|
128 |
class CHCertSignatureSchemesConsumer implements ExtensionConsumer { |
|
129 |
// Prevent instantiation of this class. |
|
130 |
private CHCertSignatureSchemesConsumer() { |
|
131 |
// blank |
|
132 |
} |
|
133 |
||
134 |
@Override |
|
135 |
public void consume(ConnectionContext context, |
|
136 |
HandshakeMessage message, ByteBuffer buffer) throws IOException { |
|
137 |
// The consuming happens in server side only. |
|
138 |
ServerHandshakeContext shc = (ServerHandshakeContext)context; |
|
139 |
||
140 |
// Is it a supported and enabled extension? |
|
141 |
if (!shc.sslConfig.isAvailable( |
|
142 |
SSLExtension.CH_SIGNATURE_ALGORITHMS_CERT)) { |
|
143 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
144 |
SSLLogger.fine( |
|
145 |
"Ignore unavailable " + |
|
146 |
"signature_algorithms_cert extension"); |
|
147 |
} |
|
148 |
return; // ignore the extension |
|
149 |
} |
|
150 |
||
151 |
// Parse the extension. |
|
152 |
SignatureSchemesSpec spec; |
|
153 |
try { |
|
154 |
spec = new SignatureSchemesSpec(buffer); |
|
155 |
} catch (IOException ioe) { |
|
156 |
shc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, ioe); |
|
157 |
return; // fatal() always throws, make the compiler happy. |
|
158 |
} |
|
159 |
||
160 |
// Update the context. |
|
161 |
shc.handshakeExtensions.put( |
|
162 |
SSLExtension.CH_SIGNATURE_ALGORITHMS_CERT, spec); |
|
163 |
||
164 |
// No impact on session resumption. |
|
165 |
} |
|
166 |
} |
|
167 |
||
168 |
/** |
|
169 |
* After session creation consuming of a "signature_algorithms_cert" |
|
170 |
* extension in the ClientHello handshake message. |
|
171 |
*/ |
|
172 |
private static final class CHCertSignatureSchemesUpdate |
|
173 |
implements HandshakeConsumer { |
|
174 |
// Prevent instantiation of this class. |
|
175 |
private CHCertSignatureSchemesUpdate() { |
|
176 |
// blank |
|
177 |
} |
|
178 |
||
179 |
@Override |
|
180 |
public void consume(ConnectionContext context, |
|
181 |
HandshakeMessage message) throws IOException { |
|
182 |
// The consuming happens in server side only. |
|
183 |
ServerHandshakeContext shc = (ServerHandshakeContext)context; |
|
184 |
||
185 |
SignatureSchemesSpec spec = (SignatureSchemesSpec) |
|
186 |
shc.handshakeExtensions.get( |
|
187 |
SSLExtension.CH_SIGNATURE_ALGORITHMS_CERT); |
|
188 |
if (spec == null) { |
|
189 |
// Ignore, no signature_algorithms_cert extension requested. |
|
190 |
return; |
|
191 |
} |
|
192 |
||
193 |
// update the context |
|
51574
ed52ea83f830
8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents:
50768
diff
changeset
|
194 |
List<SignatureScheme> schemes = |
50768 | 195 |
SignatureScheme.getSupportedAlgorithms( |
196 |
shc.algorithmConstraints, shc.negotiatedProtocol, |
|
197 |
spec.signatureSchemes); |
|
51574
ed52ea83f830
8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents:
50768
diff
changeset
|
198 |
shc.peerRequestedCertSignSchemes = schemes; |
ed52ea83f830
8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents:
50768
diff
changeset
|
199 |
shc.handshakeSession.setPeerSupportedSignatureAlgorithms(schemes); |
50768 | 200 |
|
201 |
if (!shc.isResumption && shc.negotiatedProtocol.useTLS13PlusSpec()) { |
|
202 |
if (shc.sslConfig.clientAuthType != |
|
203 |
ClientAuthType.CLIENT_AUTH_NONE) { |
|
204 |
shc.handshakeProducers.putIfAbsent( |
|
205 |
SSLHandshake.CERTIFICATE_REQUEST.id, |
|
206 |
SSLHandshake.CERTIFICATE_REQUEST); |
|
207 |
} |
|
208 |
shc.handshakeProducers.put(SSLHandshake.CERTIFICATE.id, |
|
209 |
SSLHandshake.CERTIFICATE); |
|
210 |
shc.handshakeProducers.putIfAbsent( |
|
211 |
SSLHandshake.CERTIFICATE_VERIFY.id, |
|
212 |
SSLHandshake.CERTIFICATE_VERIFY); |
|
213 |
} |
|
214 |
} |
|
215 |
} |
|
216 |
||
217 |
/** |
|
218 |
* Network data producer of a "signature_algorithms_cert" extension in |
|
219 |
* the CertificateRequest handshake message. |
|
220 |
*/ |
|
221 |
private static final |
|
222 |
class CRCertSignatureSchemesProducer implements HandshakeProducer { |
|
223 |
// Prevent instantiation of this class. |
|
224 |
private CRCertSignatureSchemesProducer() { |
|
225 |
// blank |
|
226 |
} |
|
227 |
||
228 |
@Override |
|
229 |
public byte[] produce(ConnectionContext context, |
|
230 |
HandshakeMessage message) throws IOException { |
|
231 |
// The producing happens in server side only. |
|
232 |
ServerHandshakeContext shc = (ServerHandshakeContext)context; |
|
233 |
||
234 |
// Is it a supported and enabled extension? |
|
235 |
if (!shc.sslConfig.isAvailable( |
|
236 |
SSLExtension.CH_SIGNATURE_ALGORITHMS_CERT)) { |
|
237 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
238 |
SSLLogger.fine( |
|
239 |
"Ignore unavailable " + |
|
240 |
"signature_algorithms_cert extension"); |
|
241 |
} |
|
242 |
return null; // ignore the extension |
|
243 |
} |
|
244 |
||
245 |
// Produce the extension. |
|
246 |
if (shc.localSupportedSignAlgs == null) { |
|
247 |
shc.localSupportedSignAlgs = |
|
248 |
SignatureScheme.getSupportedAlgorithms( |
|
249 |
shc.algorithmConstraints, shc.activeProtocols); |
|
250 |
} |
|
251 |
||
252 |
int vectorLen = SignatureScheme.sizeInRecord() * |
|
253 |
shc.localSupportedSignAlgs.size(); |
|
254 |
byte[] extData = new byte[vectorLen + 2]; |
|
255 |
ByteBuffer m = ByteBuffer.wrap(extData); |
|
256 |
Record.putInt16(m, vectorLen); |
|
257 |
for (SignatureScheme ss : shc.localSupportedSignAlgs) { |
|
258 |
Record.putInt16(m, ss.id); |
|
259 |
} |
|
260 |
||
261 |
// Update the context. |
|
262 |
shc.handshakeExtensions.put( |
|
263 |
SSLExtension.CR_SIGNATURE_ALGORITHMS_CERT, |
|
264 |
new SignatureSchemesSpec(shc.localSupportedSignAlgs)); |
|
265 |
||
266 |
return extData; |
|
267 |
} |
|
268 |
} |
|
269 |
||
270 |
/** |
|
271 |
* Network data consumer of a "signature_algorithms_cert" extension in |
|
272 |
* the CertificateRequest handshake message. |
|
273 |
*/ |
|
274 |
private static final |
|
275 |
class CRCertSignatureSchemesConsumer implements ExtensionConsumer { |
|
276 |
// Prevent instantiation of this class. |
|
277 |
private CRCertSignatureSchemesConsumer() { |
|
278 |
// blank |
|
279 |
} |
|
280 |
@Override |
|
281 |
public void consume(ConnectionContext context, |
|
282 |
HandshakeMessage message, ByteBuffer buffer) throws IOException { |
|
283 |
// The consuming happens in client side only. |
|
284 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
285 |
||
286 |
// Is it a supported and enabled extension? |
|
287 |
if (!chc.sslConfig.isAvailable( |
|
288 |
SSLExtension.CH_SIGNATURE_ALGORITHMS_CERT)) { |
|
289 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
290 |
SSLLogger.fine( |
|
291 |
"Ignore unavailable " + |
|
292 |
"signature_algorithms_cert extension"); |
|
293 |
} |
|
294 |
return; // ignore the extension |
|
295 |
} |
|
296 |
||
297 |
// Parse the extension. |
|
298 |
SignatureSchemesSpec spec; |
|
299 |
try { |
|
300 |
spec = new SignatureSchemesSpec(buffer); |
|
301 |
} catch (IOException ioe) { |
|
302 |
chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, ioe); |
|
303 |
return; // fatal() always throws, make the compiler happy. |
|
304 |
} |
|
305 |
||
306 |
// Update the context. |
|
307 |
chc.handshakeExtensions.put( |
|
308 |
SSLExtension.CR_SIGNATURE_ALGORITHMS_CERT, spec); |
|
309 |
||
310 |
// No impact on session resumption. |
|
311 |
} |
|
312 |
} |
|
313 |
||
314 |
/** |
|
315 |
* After session creation consuming of a "signature_algorithms_cert" |
|
316 |
* extension in the CertificateRequest handshake message. |
|
317 |
*/ |
|
318 |
private static final class CRCertSignatureSchemesUpdate |
|
319 |
implements HandshakeConsumer { |
|
320 |
// Prevent instantiation of this class. |
|
321 |
private CRCertSignatureSchemesUpdate() { |
|
322 |
// blank |
|
323 |
} |
|
324 |
||
325 |
@Override |
|
326 |
public void consume(ConnectionContext context, |
|
327 |
HandshakeMessage message) throws IOException { |
|
328 |
// The consuming happens in client side only. |
|
329 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
330 |
||
331 |
SignatureSchemesSpec spec = (SignatureSchemesSpec) |
|
332 |
chc.handshakeExtensions.get( |
|
333 |
SSLExtension.CR_SIGNATURE_ALGORITHMS_CERT); |
|
334 |
if (spec == null) { |
|
335 |
// Ignore, no "signature_algorithms_cert" extension requested. |
|
336 |
return; |
|
337 |
} |
|
338 |
||
339 |
// update the context |
|
51574
ed52ea83f830
8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents:
50768
diff
changeset
|
340 |
List<SignatureScheme> schemes = |
50768 | 341 |
SignatureScheme.getSupportedAlgorithms( |
342 |
chc.algorithmConstraints, chc.negotiatedProtocol, |
|
343 |
spec.signatureSchemes); |
|
51574
ed52ea83f830
8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents:
50768
diff
changeset
|
344 |
chc.peerRequestedCertSignSchemes = schemes; |
ed52ea83f830
8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents:
50768
diff
changeset
|
345 |
chc.handshakeSession.setPeerSupportedSignatureAlgorithms(schemes); |
50768 | 346 |
} |
347 |
} |
|
348 |
} |