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