author | xuelei |
Mon, 12 Aug 2019 21:36:29 -0700 | |
changeset 57718 | a93b7b28f644 |
parent 54417 | f87041131515 |
child 58951 | d6e682e8fcc3 |
permissions | -rw-r--r-- |
50768 | 1 |
/* |
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
53064
diff
changeset
|
2 |
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. |
50768 | 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.security.*; |
|
31 |
import java.text.MessageFormat; |
|
32 |
import java.util.Arrays; |
|
33 |
import java.util.Locale; |
|
34 |
import sun.security.ssl.SSLHandshake.HandshakeMessage; |
|
35 |
import sun.security.ssl.X509Authentication.X509Credentials; |
|
36 |
import sun.security.ssl.X509Authentication.X509Possession; |
|
37 |
import sun.security.util.HexDumpEncoder; |
|
38 |
||
39 |
/** |
|
40 |
* Pack of the CertificateVerify handshake message. |
|
41 |
*/ |
|
42 |
final class CertificateVerify { |
|
43 |
static final SSLConsumer s30HandshakeConsumer = |
|
44 |
new S30CertificateVerifyConsumer(); |
|
45 |
static final HandshakeProducer s30HandshakeProducer = |
|
46 |
new S30CertificateVerifyProducer(); |
|
47 |
||
48 |
static final SSLConsumer t10HandshakeConsumer = |
|
49 |
new T10CertificateVerifyConsumer(); |
|
50 |
static final HandshakeProducer t10HandshakeProducer = |
|
51 |
new T10CertificateVerifyProducer(); |
|
52 |
||
53 |
static final SSLConsumer t12HandshakeConsumer = |
|
54 |
new T12CertificateVerifyConsumer(); |
|
55 |
static final HandshakeProducer t12HandshakeProducer = |
|
56 |
new T12CertificateVerifyProducer(); |
|
57 |
||
58 |
static final SSLConsumer t13HandshakeConsumer = |
|
59 |
new T13CertificateVerifyConsumer(); |
|
60 |
static final HandshakeProducer t13HandshakeProducer = |
|
61 |
new T13CertificateVerifyProducer(); |
|
62 |
||
63 |
/** |
|
64 |
* The CertificateVerify handshake message (SSL 3.0). |
|
65 |
*/ |
|
66 |
static final class S30CertificateVerifyMessage extends HandshakeMessage { |
|
67 |
// signature bytes |
|
68 |
private final byte[] signature; |
|
69 |
||
70 |
S30CertificateVerifyMessage(HandshakeContext context, |
|
71 |
X509Possession x509Possession) throws IOException { |
|
72 |
super(context); |
|
73 |
||
74 |
// This happens in client side only. |
|
75 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
76 |
byte[] temproary = null; |
|
77 |
String algorithm = x509Possession.popPrivateKey.getAlgorithm(); |
|
78 |
try { |
|
79 |
Signature signer = |
|
80 |
getSignature(algorithm, x509Possession.popPrivateKey); |
|
81 |
byte[] hashes = chc.handshakeHash.digest(algorithm, |
|
82 |
chc.handshakeSession.getMasterSecret()); |
|
83 |
signer.update(hashes); |
|
84 |
temproary = signer.sign(); |
|
85 |
} catch (NoSuchAlgorithmException nsae) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
86 |
throw chc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 87 |
"Unsupported signature algorithm (" + algorithm + |
88 |
") used in CertificateVerify handshake message", nsae); |
|
89 |
} catch (GeneralSecurityException gse) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
90 |
throw chc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 91 |
"Cannot produce CertificateVerify signature", gse); |
92 |
} |
|
93 |
||
94 |
this.signature = temproary; |
|
95 |
} |
|
96 |
||
97 |
S30CertificateVerifyMessage(HandshakeContext context, |
|
98 |
ByteBuffer m) throws IOException { |
|
99 |
super(context); |
|
100 |
||
101 |
// This happens in server side only. |
|
102 |
ServerHandshakeContext shc = (ServerHandshakeContext)context; |
|
103 |
||
104 |
// digitally-signed struct { |
|
105 |
// select(SignatureAlgorithm) { |
|
106 |
// case anonymous: struct { }; |
|
107 |
// case rsa: |
|
108 |
// opaque md5_hash[16]; |
|
109 |
// opaque sha_hash[20]; |
|
110 |
// case dsa: |
|
111 |
// opaque sha_hash[20]; |
|
112 |
// }; |
|
113 |
// } Signature; |
|
114 |
if (m.remaining() < 2) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
115 |
throw shc.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
50768 | 116 |
"Invalid CertificateVerify message: no sufficient data"); |
117 |
} |
|
118 |
||
119 |
// read and verify the signature |
|
120 |
this.signature = Record.getBytes16(m); |
|
121 |
X509Credentials x509Credentials = null; |
|
122 |
for (SSLCredentials cd : shc.handshakeCredentials) { |
|
123 |
if (cd instanceof X509Credentials) { |
|
124 |
x509Credentials = (X509Credentials)cd; |
|
125 |
break; |
|
126 |
} |
|
127 |
} |
|
128 |
||
129 |
if (x509Credentials == null || |
|
130 |
x509Credentials.popPublicKey == null) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
131 |
throw shc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 132 |
"No X509 credentials negotiated for CertificateVerify"); |
133 |
} |
|
134 |
||
135 |
String algorithm = x509Credentials.popPublicKey.getAlgorithm(); |
|
136 |
try { |
|
137 |
Signature signer = |
|
138 |
getSignature(algorithm, x509Credentials.popPublicKey); |
|
139 |
byte[] hashes = shc.handshakeHash.digest(algorithm, |
|
140 |
shc.handshakeSession.getMasterSecret()); |
|
141 |
signer.update(hashes); |
|
142 |
if (!signer.verify(signature)) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
143 |
throw shc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 144 |
"Invalid CertificateVerify message: invalid signature"); |
145 |
} |
|
146 |
} catch (NoSuchAlgorithmException nsae) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
147 |
throw shc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 148 |
"Unsupported signature algorithm (" + algorithm + |
149 |
") used in CertificateVerify handshake message", nsae); |
|
150 |
} catch (GeneralSecurityException gse) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
151 |
throw shc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 152 |
"Cannot verify CertificateVerify signature", gse); |
153 |
} |
|
154 |
} |
|
155 |
||
156 |
@Override |
|
157 |
public SSLHandshake handshakeType() { |
|
158 |
return SSLHandshake.CERTIFICATE_VERIFY; |
|
159 |
} |
|
160 |
||
161 |
@Override |
|
162 |
public int messageLength() { |
|
163 |
return 2 + signature.length; // 2: length of signature |
|
164 |
} |
|
165 |
||
166 |
@Override |
|
167 |
public void send(HandshakeOutStream hos) throws IOException { |
|
168 |
hos.putBytes16(signature); |
|
169 |
} |
|
170 |
||
171 |
@Override |
|
172 |
public String toString() { |
|
173 |
MessageFormat messageFormat = new MessageFormat( |
|
174 |
"\"CertificateVerify\": '{'\n" + |
|
175 |
" \"signature\": '{'\n" + |
|
176 |
"{0}\n" + |
|
177 |
" '}'\n" + |
|
178 |
"'}'", |
|
179 |
Locale.ENGLISH); |
|
180 |
||
181 |
HexDumpEncoder hexEncoder = new HexDumpEncoder(); |
|
182 |
Object[] messageFields = { |
|
183 |
Utilities.indent( |
|
184 |
hexEncoder.encodeBuffer(signature), " ") |
|
185 |
}; |
|
186 |
||
187 |
return messageFormat.format(messageFields); |
|
188 |
} |
|
189 |
||
190 |
/* |
|
191 |
* Get the Signature object appropriate for verification using the |
|
192 |
* given signature algorithm. |
|
193 |
*/ |
|
194 |
private static Signature getSignature(String algorithm, |
|
195 |
Key key) throws GeneralSecurityException { |
|
196 |
Signature signer = null; |
|
197 |
switch (algorithm) { |
|
198 |
case "RSA": |
|
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
53064
diff
changeset
|
199 |
signer = Signature.getInstance(JsseJce.SIGNATURE_RAWRSA); |
50768 | 200 |
break; |
201 |
case "DSA": |
|
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
53064
diff
changeset
|
202 |
signer = Signature.getInstance(JsseJce.SIGNATURE_RAWDSA); |
50768 | 203 |
break; |
204 |
case "EC": |
|
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
53064
diff
changeset
|
205 |
signer = Signature.getInstance(JsseJce.SIGNATURE_RAWECDSA); |
50768 | 206 |
break; |
207 |
default: |
|
208 |
throw new SignatureException("Unrecognized algorithm: " |
|
209 |
+ algorithm); |
|
210 |
} |
|
211 |
||
212 |
if (signer != null) { |
|
213 |
if (key instanceof PublicKey) { |
|
214 |
signer.initVerify((PublicKey)(key)); |
|
215 |
} else { |
|
216 |
signer.initSign((PrivateKey)key); |
|
217 |
} |
|
218 |
} |
|
219 |
||
220 |
return signer; |
|
221 |
} |
|
222 |
} |
|
223 |
||
224 |
/** |
|
225 |
* The "CertificateVerify" handshake message producer. |
|
226 |
*/ |
|
227 |
private static final |
|
228 |
class S30CertificateVerifyProducer implements HandshakeProducer { |
|
229 |
// Prevent instantiation of this class. |
|
230 |
private S30CertificateVerifyProducer() { |
|
231 |
// blank |
|
232 |
} |
|
233 |
||
234 |
@Override |
|
235 |
public byte[] produce(ConnectionContext context, |
|
236 |
HandshakeMessage message) throws IOException { |
|
237 |
// The producing happens in client side only. |
|
238 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
239 |
||
240 |
X509Possession x509Possession = null; |
|
241 |
for (SSLPossession possession : chc.handshakePossessions) { |
|
242 |
if (possession instanceof X509Possession) { |
|
243 |
x509Possession = (X509Possession)possession; |
|
244 |
break; |
|
245 |
} |
|
246 |
} |
|
247 |
||
248 |
if (x509Possession == null || |
|
249 |
x509Possession.popPrivateKey == null) { |
|
250 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
251 |
SSLLogger.fine( |
|
252 |
"No X.509 credentials negotiated for CertificateVerify"); |
|
253 |
} |
|
254 |
||
255 |
return null; |
|
256 |
} |
|
257 |
||
258 |
S30CertificateVerifyMessage cvm = |
|
259 |
new S30CertificateVerifyMessage(chc, x509Possession); |
|
260 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
261 |
SSLLogger.fine( |
|
262 |
"Produced CertificateVerify handshake message", cvm); |
|
263 |
} |
|
264 |
||
265 |
// Output the handshake message. |
|
266 |
cvm.write(chc.handshakeOutput); |
|
267 |
chc.handshakeOutput.flush(); |
|
268 |
||
269 |
// The handshake message has been delivered. |
|
270 |
return null; |
|
271 |
} |
|
272 |
} |
|
273 |
||
274 |
/** |
|
275 |
* The "CertificateVerify" handshake message consumer. |
|
276 |
*/ |
|
277 |
private static final |
|
278 |
class S30CertificateVerifyConsumer implements SSLConsumer { |
|
279 |
// Prevent instantiation of this class. |
|
280 |
private S30CertificateVerifyConsumer() { |
|
281 |
// blank |
|
282 |
} |
|
283 |
||
284 |
@Override |
|
285 |
public void consume(ConnectionContext context, |
|
286 |
ByteBuffer message) throws IOException { |
|
287 |
// The consuming happens in server side only. |
|
288 |
ServerHandshakeContext shc = (ServerHandshakeContext)context; |
|
289 |
S30CertificateVerifyMessage cvm = |
|
290 |
new S30CertificateVerifyMessage(shc, message); |
|
291 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
292 |
SSLLogger.fine( |
|
293 |
"Consuming CertificateVerify handshake message", cvm); |
|
294 |
} |
|
295 |
||
296 |
// |
|
297 |
// update |
|
298 |
// |
|
299 |
// Need no additional validation. |
|
300 |
||
301 |
// |
|
302 |
// produce |
|
303 |
// |
|
304 |
// Need no new handshake message producers here. |
|
305 |
} |
|
306 |
} |
|
307 |
||
308 |
/** |
|
309 |
* The CertificateVerify handshake message (TLS 1.0/1.1). |
|
310 |
*/ |
|
311 |
static final class T10CertificateVerifyMessage extends HandshakeMessage { |
|
312 |
// signature bytes |
|
313 |
private final byte[] signature; |
|
314 |
||
315 |
T10CertificateVerifyMessage(HandshakeContext context, |
|
316 |
X509Possession x509Possession) throws IOException { |
|
317 |
super(context); |
|
318 |
||
319 |
// This happens in client side only. |
|
320 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
321 |
byte[] temproary = null; |
|
322 |
String algorithm = x509Possession.popPrivateKey.getAlgorithm(); |
|
323 |
try { |
|
324 |
Signature signer = |
|
325 |
getSignature(algorithm, x509Possession.popPrivateKey); |
|
326 |
byte[] hashes = chc.handshakeHash.digest(algorithm); |
|
327 |
signer.update(hashes); |
|
328 |
temproary = signer.sign(); |
|
329 |
} catch (NoSuchAlgorithmException nsae) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
330 |
throw chc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 331 |
"Unsupported signature algorithm (" + algorithm + |
332 |
") used in CertificateVerify handshake message", nsae); |
|
333 |
} catch (GeneralSecurityException gse) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
334 |
throw chc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 335 |
"Cannot produce CertificateVerify signature", gse); |
336 |
} |
|
337 |
||
338 |
this.signature = temproary; |
|
339 |
} |
|
340 |
||
341 |
T10CertificateVerifyMessage(HandshakeContext context, |
|
342 |
ByteBuffer m) throws IOException { |
|
343 |
super(context); |
|
344 |
||
345 |
// This happens in server side only. |
|
346 |
ServerHandshakeContext shc = (ServerHandshakeContext)context; |
|
347 |
||
348 |
// digitally-signed struct { |
|
349 |
// select(SignatureAlgorithm) { |
|
350 |
// case anonymous: struct { }; |
|
351 |
// case rsa: |
|
352 |
// opaque md5_hash[16]; |
|
353 |
// opaque sha_hash[20]; |
|
354 |
// case dsa: |
|
355 |
// opaque sha_hash[20]; |
|
356 |
// }; |
|
357 |
// } Signature; |
|
358 |
if (m.remaining() < 2) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
359 |
throw shc.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
50768 | 360 |
"Invalid CertificateVerify message: no sufficient data"); |
361 |
} |
|
362 |
||
363 |
// read and verify the signature |
|
364 |
this.signature = Record.getBytes16(m); |
|
365 |
X509Credentials x509Credentials = null; |
|
366 |
for (SSLCredentials cd : shc.handshakeCredentials) { |
|
367 |
if (cd instanceof X509Credentials) { |
|
368 |
x509Credentials = (X509Credentials)cd; |
|
369 |
break; |
|
370 |
} |
|
371 |
} |
|
372 |
||
373 |
if (x509Credentials == null || |
|
374 |
x509Credentials.popPublicKey == null) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
375 |
throw shc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 376 |
"No X509 credentials negotiated for CertificateVerify"); |
377 |
} |
|
378 |
||
379 |
String algorithm = x509Credentials.popPublicKey.getAlgorithm(); |
|
380 |
try { |
|
381 |
Signature signer = |
|
382 |
getSignature(algorithm, x509Credentials.popPublicKey); |
|
383 |
byte[] hashes = shc.handshakeHash.digest(algorithm); |
|
384 |
signer.update(hashes); |
|
385 |
if (!signer.verify(signature)) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
386 |
throw shc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 387 |
"Invalid CertificateVerify message: invalid signature"); |
388 |
} |
|
389 |
} catch (NoSuchAlgorithmException nsae) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
390 |
throw shc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 391 |
"Unsupported signature algorithm (" + algorithm + |
392 |
") used in CertificateVerify handshake message", nsae); |
|
393 |
} catch (GeneralSecurityException gse) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
394 |
throw shc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 395 |
"Cannot verify CertificateVerify signature", gse); |
396 |
} |
|
397 |
} |
|
398 |
||
399 |
@Override |
|
400 |
public SSLHandshake handshakeType() { |
|
401 |
return SSLHandshake.CERTIFICATE_VERIFY; |
|
402 |
} |
|
403 |
||
404 |
@Override |
|
405 |
public int messageLength() { |
|
406 |
return 2 + signature.length; // 2: length of signature |
|
407 |
} |
|
408 |
||
409 |
@Override |
|
410 |
public void send(HandshakeOutStream hos) throws IOException { |
|
411 |
hos.putBytes16(signature); |
|
412 |
} |
|
413 |
||
414 |
@Override |
|
415 |
public String toString() { |
|
416 |
MessageFormat messageFormat = new MessageFormat( |
|
417 |
"\"CertificateVerify\": '{'\n" + |
|
418 |
" \"signature\": '{'\n" + |
|
419 |
"{0}\n" + |
|
420 |
" '}'\n" + |
|
421 |
"'}'", |
|
422 |
Locale.ENGLISH); |
|
423 |
||
424 |
HexDumpEncoder hexEncoder = new HexDumpEncoder(); |
|
425 |
Object[] messageFields = { |
|
426 |
Utilities.indent( |
|
427 |
hexEncoder.encodeBuffer(signature), " ") |
|
428 |
}; |
|
429 |
||
430 |
return messageFormat.format(messageFields); |
|
431 |
} |
|
432 |
||
433 |
/* |
|
434 |
* Get the Signature object appropriate for verification using the |
|
435 |
* given signature algorithm. |
|
436 |
*/ |
|
437 |
private static Signature getSignature(String algorithm, |
|
438 |
Key key) throws GeneralSecurityException { |
|
439 |
Signature signer = null; |
|
440 |
switch (algorithm) { |
|
441 |
case "RSA": |
|
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
53064
diff
changeset
|
442 |
signer = Signature.getInstance(JsseJce.SIGNATURE_RAWRSA); |
50768 | 443 |
break; |
444 |
case "DSA": |
|
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
53064
diff
changeset
|
445 |
signer = Signature.getInstance(JsseJce.SIGNATURE_RAWDSA); |
50768 | 446 |
break; |
447 |
case "EC": |
|
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
53064
diff
changeset
|
448 |
signer = Signature.getInstance(JsseJce.SIGNATURE_RAWECDSA); |
50768 | 449 |
break; |
450 |
default: |
|
451 |
throw new SignatureException("Unrecognized algorithm: " |
|
452 |
+ algorithm); |
|
453 |
} |
|
454 |
||
455 |
if (signer != null) { |
|
456 |
if (key instanceof PublicKey) { |
|
457 |
signer.initVerify((PublicKey)(key)); |
|
458 |
} else { |
|
459 |
signer.initSign((PrivateKey)key); |
|
460 |
} |
|
461 |
} |
|
462 |
||
463 |
return signer; |
|
464 |
} |
|
465 |
} |
|
466 |
||
467 |
/** |
|
468 |
* The "CertificateVerify" handshake message producer. |
|
469 |
*/ |
|
470 |
private static final |
|
471 |
class T10CertificateVerifyProducer implements HandshakeProducer { |
|
472 |
// Prevent instantiation of this class. |
|
473 |
private T10CertificateVerifyProducer() { |
|
474 |
// blank |
|
475 |
} |
|
476 |
||
477 |
@Override |
|
478 |
public byte[] produce(ConnectionContext context, |
|
479 |
HandshakeMessage message) throws IOException { |
|
480 |
// The producing happens in client side only. |
|
481 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
482 |
X509Possession x509Possession = null; |
|
483 |
for (SSLPossession possession : chc.handshakePossessions) { |
|
484 |
if (possession instanceof X509Possession) { |
|
485 |
x509Possession = (X509Possession)possession; |
|
486 |
break; |
|
487 |
} |
|
488 |
} |
|
489 |
||
490 |
if (x509Possession == null || |
|
491 |
x509Possession.popPrivateKey == null) { |
|
492 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
493 |
SSLLogger.fine( |
|
494 |
"No X.509 credentials negotiated for CertificateVerify"); |
|
495 |
} |
|
496 |
||
497 |
return null; |
|
498 |
} |
|
499 |
||
500 |
T10CertificateVerifyMessage cvm = |
|
501 |
new T10CertificateVerifyMessage(chc, x509Possession); |
|
502 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
503 |
SSLLogger.fine( |
|
504 |
"Produced CertificateVerify handshake message", cvm); |
|
505 |
} |
|
506 |
||
507 |
// Output the handshake message. |
|
508 |
cvm.write(chc.handshakeOutput); |
|
509 |
chc.handshakeOutput.flush(); |
|
510 |
||
511 |
// The handshake message has been delivered. |
|
512 |
return null; |
|
513 |
} |
|
514 |
} |
|
515 |
||
516 |
/** |
|
517 |
* The "CertificateVerify" handshake message consumer. |
|
518 |
*/ |
|
519 |
private static final |
|
520 |
class T10CertificateVerifyConsumer implements SSLConsumer { |
|
521 |
// Prevent instantiation of this class. |
|
522 |
private T10CertificateVerifyConsumer() { |
|
523 |
// blank |
|
524 |
} |
|
525 |
||
526 |
@Override |
|
527 |
public void consume(ConnectionContext context, |
|
528 |
ByteBuffer message) throws IOException { |
|
529 |
// The consuming happens in server side only. |
|
530 |
ServerHandshakeContext shc = (ServerHandshakeContext)context; |
|
531 |
T10CertificateVerifyMessage cvm = |
|
532 |
new T10CertificateVerifyMessage(shc, message); |
|
533 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
534 |
SSLLogger.fine( |
|
535 |
"Consuming CertificateVerify handshake message", cvm); |
|
536 |
} |
|
537 |
||
538 |
// |
|
539 |
// update |
|
540 |
// |
|
541 |
// Need no additional validation. |
|
542 |
||
543 |
// |
|
544 |
// produce |
|
545 |
// |
|
546 |
// Need no new handshake message producers here. } |
|
547 |
} |
|
548 |
} |
|
549 |
||
550 |
/** |
|
551 |
* The CertificateVerify handshake message (TLS 1.2). |
|
552 |
*/ |
|
553 |
static final class T12CertificateVerifyMessage extends HandshakeMessage { |
|
554 |
// the signature algorithm |
|
555 |
private final SignatureScheme signatureScheme; |
|
556 |
||
557 |
// signature bytes |
|
558 |
private final byte[] signature; |
|
559 |
||
560 |
T12CertificateVerifyMessage(HandshakeContext context, |
|
561 |
X509Possession x509Possession) throws IOException { |
|
562 |
super(context); |
|
563 |
||
564 |
// This happens in client side only. |
|
565 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
566 |
this.signatureScheme = SignatureScheme.getPreferableAlgorithm( |
|
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
54417
diff
changeset
|
567 |
chc.algorithmConstraints, |
50768 | 568 |
chc.peerRequestedSignatureSchemes, |
54417
f87041131515
8217610: TLSv1.3 fail with ClassException when EC keys are stored in PKCS11
xuelei
parents:
53734
diff
changeset
|
569 |
x509Possession, |
50768 | 570 |
chc.negotiatedProtocol); |
571 |
if (signatureScheme == null) { |
|
572 |
// Unlikely, the credentials generator should have |
|
573 |
// selected the preferable signature algorithm properly. |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
574 |
throw chc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 575 |
"No preferred signature algorithm for CertificateVerify"); |
576 |
} |
|
577 |
||
578 |
byte[] temproary = null; |
|
579 |
try { |
|
580 |
Signature signer = |
|
581 |
signatureScheme.getSignature(x509Possession.popPrivateKey); |
|
582 |
signer.update(chc.handshakeHash.archived()); |
|
583 |
temproary = signer.sign(); |
|
584 |
} catch (NoSuchAlgorithmException | |
|
585 |
InvalidAlgorithmParameterException nsae) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
586 |
throw chc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 587 |
"Unsupported signature algorithm (" + |
588 |
signatureScheme.name + |
|
589 |
") used in CertificateVerify handshake message", nsae); |
|
590 |
} catch (InvalidKeyException | SignatureException ikse) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
591 |
throw chc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 592 |
"Cannot produce CertificateVerify signature", ikse); |
593 |
} |
|
594 |
||
595 |
this.signature = temproary; |
|
596 |
} |
|
597 |
||
598 |
T12CertificateVerifyMessage(HandshakeContext handshakeContext, |
|
599 |
ByteBuffer m) throws IOException { |
|
600 |
super(handshakeContext); |
|
601 |
||
602 |
// This happens in server side only. |
|
603 |
ServerHandshakeContext shc = |
|
604 |
(ServerHandshakeContext)handshakeContext; |
|
605 |
||
606 |
// struct { |
|
607 |
// SignatureAndHashAlgorithm algorithm; |
|
608 |
// opaque signature<0..2^16-1>; |
|
609 |
// } DigitallySigned; |
|
610 |
if (m.remaining() < 4) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
611 |
throw shc.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
50768 | 612 |
"Invalid CertificateVerify message: no sufficient data"); |
613 |
} |
|
614 |
||
615 |
// SignatureAndHashAlgorithm algorithm |
|
616 |
int ssid = Record.getInt16(m); |
|
617 |
this.signatureScheme = SignatureScheme.valueOf(ssid); |
|
618 |
if (signatureScheme == null) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
619 |
throw shc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 620 |
"Invalid signature algorithm (" + ssid + |
621 |
") used in CertificateVerify handshake message"); |
|
622 |
} |
|
623 |
||
624 |
if (!shc.localSupportedSignAlgs.contains(signatureScheme)) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
625 |
throw shc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 626 |
"Unsupported signature algorithm (" + |
627 |
signatureScheme.name + |
|
628 |
") used in CertificateVerify handshake message"); |
|
629 |
} |
|
630 |
||
631 |
// read and verify the signature |
|
632 |
X509Credentials x509Credentials = null; |
|
633 |
for (SSLCredentials cd : shc.handshakeCredentials) { |
|
634 |
if (cd instanceof X509Credentials) { |
|
635 |
x509Credentials = (X509Credentials)cd; |
|
636 |
break; |
|
637 |
} |
|
638 |
} |
|
639 |
||
640 |
if (x509Credentials == null || |
|
641 |
x509Credentials.popPublicKey == null) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
642 |
throw shc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 643 |
"No X509 credentials negotiated for CertificateVerify"); |
644 |
} |
|
645 |
||
646 |
// opaque signature<0..2^16-1>; |
|
647 |
this.signature = Record.getBytes16(m); |
|
648 |
try { |
|
649 |
Signature signer = |
|
650 |
signatureScheme.getSignature(x509Credentials.popPublicKey); |
|
651 |
signer.update(shc.handshakeHash.archived()); |
|
652 |
if (!signer.verify(signature)) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
653 |
throw shc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 654 |
"Invalid CertificateVerify signature"); |
655 |
} |
|
656 |
} catch (NoSuchAlgorithmException | |
|
657 |
InvalidAlgorithmParameterException nsae) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
658 |
throw shc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 659 |
"Unsupported signature algorithm (" + |
660 |
signatureScheme.name + |
|
661 |
") used in CertificateVerify handshake message", nsae); |
|
662 |
} catch (InvalidKeyException | SignatureException ikse) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
663 |
throw shc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 664 |
"Cannot verify CertificateVerify signature", ikse); |
665 |
} |
|
666 |
} |
|
667 |
||
668 |
@Override |
|
669 |
public SSLHandshake handshakeType() { |
|
670 |
return SSLHandshake.CERTIFICATE_VERIFY; |
|
671 |
} |
|
672 |
||
673 |
@Override |
|
674 |
public int messageLength() { |
|
675 |
return 4 + signature.length; // 2: signature algorithm |
|
676 |
// +2: length of signature |
|
677 |
} |
|
678 |
||
679 |
@Override |
|
680 |
public void send(HandshakeOutStream hos) throws IOException { |
|
681 |
hos.putInt16(signatureScheme.id); |
|
682 |
hos.putBytes16(signature); |
|
683 |
} |
|
684 |
||
685 |
@Override |
|
686 |
public String toString() { |
|
687 |
MessageFormat messageFormat = new MessageFormat( |
|
688 |
"\"CertificateVerify\": '{'\n" + |
|
689 |
" \"signature algorithm\": {0}\n" + |
|
690 |
" \"signature\": '{'\n" + |
|
691 |
"{1}\n" + |
|
692 |
" '}'\n" + |
|
693 |
"'}'", |
|
694 |
Locale.ENGLISH); |
|
695 |
||
696 |
HexDumpEncoder hexEncoder = new HexDumpEncoder(); |
|
697 |
Object[] messageFields = { |
|
698 |
signatureScheme.name, |
|
699 |
Utilities.indent( |
|
700 |
hexEncoder.encodeBuffer(signature), " ") |
|
701 |
}; |
|
702 |
||
703 |
return messageFormat.format(messageFields); |
|
704 |
} |
|
705 |
} |
|
706 |
||
707 |
/** |
|
708 |
* The "CertificateVerify" handshake message producer. |
|
709 |
*/ |
|
710 |
private static final |
|
711 |
class T12CertificateVerifyProducer implements HandshakeProducer { |
|
712 |
// Prevent instantiation of this class. |
|
713 |
private T12CertificateVerifyProducer() { |
|
714 |
// blank |
|
715 |
} |
|
716 |
||
717 |
@Override |
|
718 |
public byte[] produce(ConnectionContext context, |
|
719 |
HandshakeMessage message) throws IOException { |
|
720 |
// The producing happens in client side only. |
|
721 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
722 |
||
723 |
X509Possession x509Possession = null; |
|
724 |
for (SSLPossession possession : chc.handshakePossessions) { |
|
725 |
if (possession instanceof X509Possession) { |
|
726 |
x509Possession = (X509Possession)possession; |
|
727 |
break; |
|
728 |
} |
|
729 |
} |
|
730 |
||
731 |
if (x509Possession == null || |
|
732 |
x509Possession.popPrivateKey == null) { |
|
733 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
734 |
SSLLogger.fine( |
|
735 |
"No X.509 credentials negotiated for CertificateVerify"); |
|
736 |
} |
|
737 |
||
738 |
return null; |
|
739 |
} |
|
740 |
||
741 |
T12CertificateVerifyMessage cvm = |
|
742 |
new T12CertificateVerifyMessage(chc, x509Possession); |
|
743 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
744 |
SSLLogger.fine( |
|
745 |
"Produced CertificateVerify handshake message", cvm); |
|
746 |
} |
|
747 |
||
748 |
// Output the handshake message. |
|
749 |
cvm.write(chc.handshakeOutput); |
|
750 |
chc.handshakeOutput.flush(); |
|
751 |
||
752 |
// The handshake message has been delivered. |
|
753 |
return null; |
|
754 |
} |
|
755 |
} |
|
756 |
||
757 |
/** |
|
758 |
* The "CertificateVerify" handshake message consumer. |
|
759 |
*/ |
|
760 |
private static final |
|
761 |
class T12CertificateVerifyConsumer implements SSLConsumer { |
|
762 |
// Prevent instantiation of this class. |
|
763 |
private T12CertificateVerifyConsumer() { |
|
764 |
// blank |
|
765 |
} |
|
766 |
||
767 |
@Override |
|
768 |
public void consume(ConnectionContext context, |
|
769 |
ByteBuffer message) throws IOException { |
|
770 |
// The consuming happens in server side only. |
|
771 |
ServerHandshakeContext shc = (ServerHandshakeContext)context; |
|
772 |
T12CertificateVerifyMessage cvm = |
|
773 |
new T12CertificateVerifyMessage(shc, message); |
|
774 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
775 |
SSLLogger.fine( |
|
776 |
"Consuming CertificateVerify handshake message", cvm); |
|
777 |
} |
|
778 |
||
779 |
// |
|
780 |
// update |
|
781 |
// |
|
782 |
// Need no additional validation. |
|
783 |
||
784 |
// |
|
785 |
// produce |
|
786 |
// |
|
787 |
// Need no new handshake message producers here. |
|
788 |
} |
|
789 |
} |
|
790 |
||
791 |
/** |
|
792 |
* The CertificateVerify handshake message (TLS 1.3). |
|
793 |
*/ |
|
794 |
static final class T13CertificateVerifyMessage extends HandshakeMessage { |
|
795 |
private static final byte[] serverSignHead = new byte[] { |
|
796 |
// repeated 0x20 for 64 times |
|
797 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
798 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
799 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
800 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
801 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
802 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
803 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
804 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
805 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
806 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
807 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
808 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
809 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
810 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
811 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
812 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
813 |
||
814 |
// "TLS 1.3, server CertificateVerify" + 0x00 |
|
815 |
(byte)0x54, (byte)0x4c, (byte)0x53, (byte)0x20, |
|
816 |
(byte)0x31, (byte)0x2e, (byte)0x33, (byte)0x2c, |
|
817 |
(byte)0x20, (byte)0x73, (byte)0x65, (byte)0x72, |
|
818 |
(byte)0x76, (byte)0x65, (byte)0x72, (byte)0x20, |
|
819 |
(byte)0x43, (byte)0x65, (byte)0x72, (byte)0x74, |
|
820 |
(byte)0x69, (byte)0x66, (byte)0x69, (byte)0x63, |
|
821 |
(byte)0x61, (byte)0x74, (byte)0x65, (byte)0x56, |
|
822 |
(byte)0x65, (byte)0x72, (byte)0x69, (byte)0x66, |
|
823 |
(byte)0x79, (byte)0x00 |
|
824 |
}; |
|
825 |
||
826 |
private static final byte[] clientSignHead = new byte[] { |
|
827 |
// repeated 0x20 for 64 times |
|
828 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
829 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
830 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
831 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
832 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
833 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
834 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
835 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
836 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
837 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
838 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
839 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
840 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
841 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
842 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
843 |
(byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, |
|
844 |
||
845 |
// "TLS 1.3, client CertificateVerify" + 0x00 |
|
846 |
(byte)0x54, (byte)0x4c, (byte)0x53, (byte)0x20, |
|
847 |
(byte)0x31, (byte)0x2e, (byte)0x33, (byte)0x2c, |
|
848 |
(byte)0x20, (byte)0x63, (byte)0x6c, (byte)0x69, |
|
849 |
(byte)0x65, (byte)0x6e, (byte)0x74, (byte)0x20, |
|
850 |
(byte)0x43, (byte)0x65, (byte)0x72, (byte)0x74, |
|
851 |
(byte)0x69, (byte)0x66, (byte)0x69, (byte)0x63, |
|
852 |
(byte)0x61, (byte)0x74, (byte)0x65, (byte)0x56, |
|
853 |
(byte)0x65, (byte)0x72, (byte)0x69, (byte)0x66, |
|
854 |
(byte)0x79, (byte)0x00 |
|
855 |
}; |
|
856 |
||
857 |
||
858 |
// the signature algorithm |
|
859 |
private final SignatureScheme signatureScheme; |
|
860 |
||
861 |
// signature bytes |
|
862 |
private final byte[] signature; |
|
863 |
||
864 |
T13CertificateVerifyMessage(HandshakeContext context, |
|
865 |
X509Possession x509Possession) throws IOException { |
|
866 |
super(context); |
|
867 |
||
868 |
this.signatureScheme = SignatureScheme.getPreferableAlgorithm( |
|
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
54417
diff
changeset
|
869 |
context.algorithmConstraints, |
50768 | 870 |
context.peerRequestedSignatureSchemes, |
54417
f87041131515
8217610: TLSv1.3 fail with ClassException when EC keys are stored in PKCS11
xuelei
parents:
53734
diff
changeset
|
871 |
x509Possession, |
50768 | 872 |
context.negotiatedProtocol); |
873 |
if (signatureScheme == null) { |
|
874 |
// Unlikely, the credentials generator should have |
|
875 |
// selected the preferable signature algorithm properly. |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
876 |
throw context.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 877 |
"No preferred signature algorithm for CertificateVerify"); |
878 |
} |
|
879 |
||
880 |
byte[] hashValue = context.handshakeHash.digest(); |
|
881 |
byte[] contentCovered; |
|
882 |
if (context.sslConfig.isClientMode) { |
|
883 |
contentCovered = Arrays.copyOf(clientSignHead, |
|
884 |
clientSignHead.length + hashValue.length); |
|
885 |
System.arraycopy(hashValue, 0, contentCovered, |
|
886 |
clientSignHead.length, hashValue.length); |
|
887 |
} else { |
|
888 |
contentCovered = Arrays.copyOf(serverSignHead, |
|
889 |
serverSignHead.length + hashValue.length); |
|
890 |
System.arraycopy(hashValue, 0, contentCovered, |
|
891 |
serverSignHead.length, hashValue.length); |
|
892 |
} |
|
893 |
||
894 |
byte[] temproary = null; |
|
895 |
try { |
|
896 |
Signature signer = |
|
897 |
signatureScheme.getSignature(x509Possession.popPrivateKey); |
|
898 |
signer.update(contentCovered); |
|
899 |
temproary = signer.sign(); |
|
900 |
} catch (NoSuchAlgorithmException | |
|
901 |
InvalidAlgorithmParameterException nsae) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
902 |
throw context.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 903 |
"Unsupported signature algorithm (" + |
904 |
signatureScheme.name + |
|
905 |
") used in CertificateVerify handshake message", nsae); |
|
906 |
} catch (InvalidKeyException | SignatureException ikse) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
907 |
throw context.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 908 |
"Cannot produce CertificateVerify signature", ikse); |
909 |
} |
|
910 |
||
911 |
this.signature = temproary; |
|
912 |
} |
|
913 |
||
914 |
T13CertificateVerifyMessage(HandshakeContext context, |
|
915 |
ByteBuffer m) throws IOException { |
|
916 |
super(context); |
|
917 |
||
918 |
// struct { |
|
919 |
// SignatureAndHashAlgorithm algorithm; |
|
920 |
// opaque signature<0..2^16-1>; |
|
921 |
// } DigitallySigned; |
|
922 |
if (m.remaining() < 4) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
923 |
throw context.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
50768 | 924 |
"Invalid CertificateVerify message: no sufficient data"); |
925 |
} |
|
926 |
||
927 |
// SignatureAndHashAlgorithm algorithm |
|
928 |
int ssid = Record.getInt16(m); |
|
929 |
this.signatureScheme = SignatureScheme.valueOf(ssid); |
|
930 |
if (signatureScheme == null) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
931 |
throw context.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 932 |
"Invalid signature algorithm (" + ssid + |
933 |
") used in CertificateVerify handshake message"); |
|
934 |
} |
|
935 |
||
936 |
if (!context.localSupportedSignAlgs.contains(signatureScheme)) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
937 |
throw context.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 938 |
"Unsupported signature algorithm (" + |
939 |
signatureScheme.name + |
|
940 |
") used in CertificateVerify handshake message"); |
|
941 |
} |
|
942 |
||
943 |
// read and verify the signature |
|
944 |
X509Credentials x509Credentials = null; |
|
945 |
for (SSLCredentials cd : context.handshakeCredentials) { |
|
946 |
if (cd instanceof X509Credentials) { |
|
947 |
x509Credentials = (X509Credentials)cd; |
|
948 |
break; |
|
949 |
} |
|
950 |
} |
|
951 |
||
952 |
if (x509Credentials == null || |
|
953 |
x509Credentials.popPublicKey == null) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
954 |
throw context.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 955 |
"No X509 credentials negotiated for CertificateVerify"); |
956 |
} |
|
957 |
||
958 |
// opaque signature<0..2^16-1>; |
|
959 |
this.signature = Record.getBytes16(m); |
|
960 |
||
961 |
byte[] hashValue = context.handshakeHash.digest(); |
|
962 |
byte[] contentCovered; |
|
963 |
if (context.sslConfig.isClientMode) { |
|
964 |
contentCovered = Arrays.copyOf(serverSignHead, |
|
965 |
serverSignHead.length + hashValue.length); |
|
966 |
System.arraycopy(hashValue, 0, contentCovered, |
|
967 |
serverSignHead.length, hashValue.length); |
|
968 |
} else { |
|
969 |
contentCovered = Arrays.copyOf(clientSignHead, |
|
970 |
clientSignHead.length + hashValue.length); |
|
971 |
System.arraycopy(hashValue, 0, contentCovered, |
|
972 |
clientSignHead.length, hashValue.length); |
|
973 |
} |
|
974 |
||
975 |
try { |
|
976 |
Signature signer = |
|
977 |
signatureScheme.getSignature(x509Credentials.popPublicKey); |
|
978 |
signer.update(contentCovered); |
|
979 |
if (!signer.verify(signature)) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
980 |
throw context.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 981 |
"Invalid CertificateVerify signature"); |
982 |
} |
|
983 |
} catch (NoSuchAlgorithmException | |
|
984 |
InvalidAlgorithmParameterException nsae) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
985 |
throw context.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 986 |
"Unsupported signature algorithm (" + |
987 |
signatureScheme.name + |
|
988 |
") used in CertificateVerify handshake message", nsae); |
|
989 |
} catch (InvalidKeyException | SignatureException ikse) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
990 |
throw context.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 991 |
"Cannot verify CertificateVerify signature", ikse); |
992 |
} |
|
993 |
} |
|
994 |
||
995 |
@Override |
|
996 |
public SSLHandshake handshakeType() { |
|
997 |
return SSLHandshake.CERTIFICATE_VERIFY; |
|
998 |
} |
|
999 |
||
1000 |
@Override |
|
1001 |
public int messageLength() { |
|
1002 |
return 4 + signature.length; // 2: signature algorithm |
|
1003 |
// +2: length of signature |
|
1004 |
} |
|
1005 |
||
1006 |
@Override |
|
1007 |
public void send(HandshakeOutStream hos) throws IOException { |
|
1008 |
hos.putInt16(signatureScheme.id); |
|
1009 |
hos.putBytes16(signature); |
|
1010 |
} |
|
1011 |
||
1012 |
@Override |
|
1013 |
public String toString() { |
|
1014 |
MessageFormat messageFormat = new MessageFormat( |
|
1015 |
"\"CertificateVerify\": '{'\n" + |
|
1016 |
" \"signature algorithm\": {0}\n" + |
|
1017 |
" \"signature\": '{'\n" + |
|
1018 |
"{1}\n" + |
|
1019 |
" '}'\n" + |
|
1020 |
"'}'", |
|
1021 |
Locale.ENGLISH); |
|
1022 |
||
1023 |
HexDumpEncoder hexEncoder = new HexDumpEncoder(); |
|
1024 |
Object[] messageFields = { |
|
1025 |
signatureScheme.name, |
|
1026 |
Utilities.indent( |
|
1027 |
hexEncoder.encodeBuffer(signature), " ") |
|
1028 |
}; |
|
1029 |
||
1030 |
return messageFormat.format(messageFields); |
|
1031 |
} |
|
1032 |
} |
|
1033 |
||
1034 |
/** |
|
1035 |
* The "CertificateVerify" handshake message producer. |
|
1036 |
*/ |
|
1037 |
private static final |
|
1038 |
class T13CertificateVerifyProducer implements HandshakeProducer { |
|
1039 |
// Prevent instantiation of this class. |
|
1040 |
private T13CertificateVerifyProducer() { |
|
1041 |
// blank |
|
1042 |
} |
|
1043 |
||
1044 |
@Override |
|
1045 |
public byte[] produce(ConnectionContext context, |
|
1046 |
HandshakeMessage message) throws IOException { |
|
1047 |
// The producing happens in handshake context only. |
|
1048 |
HandshakeContext hc = (HandshakeContext)context; |
|
1049 |
||
1050 |
X509Possession x509Possession = null; |
|
1051 |
for (SSLPossession possession : hc.handshakePossessions) { |
|
1052 |
if (possession instanceof X509Possession) { |
|
1053 |
x509Possession = (X509Possession)possession; |
|
1054 |
break; |
|
1055 |
} |
|
1056 |
} |
|
1057 |
||
1058 |
if (x509Possession == null || |
|
1059 |
x509Possession.popPrivateKey == null) { |
|
1060 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
1061 |
SSLLogger.fine( |
|
1062 |
"No X.509 credentials negotiated for CertificateVerify"); |
|
1063 |
} |
|
1064 |
||
1065 |
return null; |
|
1066 |
} |
|
1067 |
||
1068 |
if (hc.sslConfig.isClientMode) { |
|
1069 |
return onProduceCertificateVerify( |
|
1070 |
(ClientHandshakeContext)context, x509Possession); |
|
1071 |
} else { |
|
1072 |
return onProduceCertificateVerify( |
|
1073 |
(ServerHandshakeContext)context, x509Possession); |
|
1074 |
} |
|
1075 |
} |
|
1076 |
||
1077 |
private byte[] onProduceCertificateVerify(ServerHandshakeContext shc, |
|
1078 |
X509Possession x509Possession) throws IOException { |
|
1079 |
T13CertificateVerifyMessage cvm = |
|
1080 |
new T13CertificateVerifyMessage(shc, x509Possession); |
|
1081 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
1082 |
SSLLogger.fine( |
|
1083 |
"Produced server CertificateVerify handshake message", cvm); |
|
1084 |
} |
|
1085 |
||
1086 |
// Output the handshake message. |
|
1087 |
cvm.write(shc.handshakeOutput); |
|
1088 |
shc.handshakeOutput.flush(); |
|
1089 |
||
1090 |
// The handshake message has been delivered. |
|
1091 |
return null; |
|
1092 |
} |
|
1093 |
||
1094 |
private byte[] onProduceCertificateVerify(ClientHandshakeContext chc, |
|
1095 |
X509Possession x509Possession) throws IOException { |
|
1096 |
T13CertificateVerifyMessage cvm = |
|
1097 |
new T13CertificateVerifyMessage(chc, x509Possession); |
|
1098 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
1099 |
SSLLogger.fine( |
|
1100 |
"Produced client CertificateVerify handshake message", cvm); |
|
1101 |
} |
|
1102 |
||
1103 |
// Output the handshake message. |
|
1104 |
cvm.write(chc.handshakeOutput); |
|
1105 |
chc.handshakeOutput.flush(); |
|
1106 |
||
1107 |
// The handshake message has been delivered. |
|
1108 |
return null; |
|
1109 |
} |
|
1110 |
} |
|
1111 |
||
1112 |
/** |
|
1113 |
* The "CertificateVerify" handshake message consumer. |
|
1114 |
*/ |
|
1115 |
private static final |
|
1116 |
class T13CertificateVerifyConsumer implements SSLConsumer { |
|
1117 |
// Prevent instantiation of this class. |
|
1118 |
private T13CertificateVerifyConsumer() { |
|
1119 |
// blank |
|
1120 |
} |
|
1121 |
||
1122 |
@Override |
|
1123 |
public void consume(ConnectionContext context, |
|
1124 |
ByteBuffer message) throws IOException { |
|
1125 |
// The producing happens in handshake context only. |
|
1126 |
HandshakeContext hc = (HandshakeContext)context; |
|
1127 |
T13CertificateVerifyMessage cvm = |
|
1128 |
new T13CertificateVerifyMessage(hc, message); |
|
1129 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
1130 |
SSLLogger.fine( |
|
1131 |
"Consuming CertificateVerify handshake message", cvm); |
|
1132 |
} |
|
1133 |
||
1134 |
// |
|
1135 |
// update |
|
1136 |
// |
|
1137 |
// Need no additional validation. |
|
1138 |
||
1139 |
// |
|
1140 |
// produce |
|
1141 |
// |
|
1142 |
// Need no new handshake message producers here. |
|
1143 |
} |
|
1144 |
} |
|
1145 |
} |