author | wetmore |
Tue, 21 Aug 2018 11:30:48 -0700 | |
changeset 51574 | ed52ea83f830 |
parent 50768 | 68fa3d4026ea |
child 51773 | 720fd6544b03 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
50768 | 2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.ssl; |
|
27 |
||
50768 | 28 |
import java.nio.ByteBuffer; |
29 |
import java.security.AccessController; |
|
30 |
import java.security.GeneralSecurityException; |
|
31 |
import java.security.InvalidAlgorithmParameterException; |
|
32 |
import java.security.InvalidKeyException; |
|
33 |
import java.security.Key; |
|
34 |
import java.security.PrivilegedAction; |
|
35 |
import java.security.SecureRandom; |
|
36 |
import java.security.Security; |
|
37 |
import java.security.spec.AlgorithmParameterSpec; |
|
38 |
import java.util.AbstractMap.SimpleImmutableEntry; |
|
16913 | 39 |
import java.util.Arrays; |
50768 | 40 |
import java.util.HashMap; |
41 |
import java.util.Map; |
|
42 |
import javax.crypto.BadPaddingException; |
|
43 |
import javax.crypto.Cipher; |
|
44 |
import javax.crypto.IllegalBlockSizeException; |
|
45 |
import javax.crypto.SecretKey; |
|
46 |
import javax.crypto.ShortBufferException; |
|
16913 | 47 |
import javax.crypto.spec.GCMParameterSpec; |
50768 | 48 |
import javax.crypto.spec.IvParameterSpec; |
49 |
import sun.security.ssl.Authenticator.MAC; |
|
50 |
import static sun.security.ssl.CipherType.*; |
|
51 |
import static sun.security.ssl.JsseJce.*; |
|
2 | 52 |
|
50768 | 53 |
enum SSLCipher { |
54 |
// exportable ciphers |
|
55 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
|
56 |
B_NULL("NULL", NULL_CIPHER, 0, 0, 0, 0, true, true, |
|
57 |
(Map.Entry<ReadCipherGenerator, |
|
58 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
59 |
new SimpleImmutableEntry<ReadCipherGenerator, ProtocolVersion[]>( |
|
60 |
new NullReadCipherGenerator(), |
|
61 |
ProtocolVersion.PROTOCOLS_OF_NONE |
|
62 |
), |
|
63 |
new SimpleImmutableEntry<ReadCipherGenerator, ProtocolVersion[]>( |
|
64 |
new NullReadCipherGenerator(), |
|
65 |
ProtocolVersion.PROTOCOLS_TO_13 |
|
66 |
) |
|
67 |
}), |
|
68 |
(Map.Entry<WriteCipherGenerator, |
|
69 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
70 |
new SimpleImmutableEntry<WriteCipherGenerator, ProtocolVersion[]>( |
|
71 |
new NullWriteCipherGenerator(), |
|
72 |
ProtocolVersion.PROTOCOLS_OF_NONE |
|
73 |
), |
|
74 |
new SimpleImmutableEntry<WriteCipherGenerator, ProtocolVersion[]>( |
|
75 |
new NullWriteCipherGenerator(), |
|
76 |
ProtocolVersion.PROTOCOLS_TO_13 |
|
77 |
) |
|
78 |
})), |
|
2 | 79 |
|
50768 | 80 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
81 |
B_RC4_40(CIPHER_RC4, STREAM_CIPHER, 5, 16, 0, 0, true, true, |
|
82 |
(Map.Entry<ReadCipherGenerator, |
|
83 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
84 |
new SimpleImmutableEntry<ReadCipherGenerator, ProtocolVersion[]>( |
|
85 |
new StreamReadCipherGenerator(), |
|
86 |
ProtocolVersion.PROTOCOLS_TO_10 |
|
87 |
) |
|
88 |
}), |
|
89 |
(Map.Entry<WriteCipherGenerator, |
|
90 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
91 |
new SimpleImmutableEntry<WriteCipherGenerator, ProtocolVersion[]>( |
|
92 |
new StreamWriteCipherGenerator(), |
|
93 |
ProtocolVersion.PROTOCOLS_TO_10 |
|
94 |
) |
|
95 |
})), |
|
2 | 96 |
|
50768 | 97 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
98 |
B_RC2_40("RC2", BLOCK_CIPHER, 5, 16, 8, 0, false, true, |
|
99 |
(Map.Entry<ReadCipherGenerator, |
|
100 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
101 |
new SimpleImmutableEntry<ReadCipherGenerator, ProtocolVersion[]>( |
|
102 |
new StreamReadCipherGenerator(), |
|
103 |
ProtocolVersion.PROTOCOLS_TO_10 |
|
104 |
) |
|
105 |
}), |
|
106 |
(Map.Entry<WriteCipherGenerator, |
|
107 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
108 |
new SimpleImmutableEntry<WriteCipherGenerator, ProtocolVersion[]>( |
|
109 |
new StreamWriteCipherGenerator(), |
|
110 |
ProtocolVersion.PROTOCOLS_TO_10 |
|
111 |
) |
|
112 |
})), |
|
2 | 113 |
|
50768 | 114 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
115 |
B_DES_40(CIPHER_DES, BLOCK_CIPHER, 5, 8, 8, 0, true, true, |
|
116 |
(Map.Entry<ReadCipherGenerator, |
|
117 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
118 |
new SimpleImmutableEntry<ReadCipherGenerator, ProtocolVersion[]>( |
|
119 |
new T10BlockReadCipherGenerator(), |
|
120 |
ProtocolVersion.PROTOCOLS_TO_10 |
|
121 |
) |
|
122 |
}), |
|
123 |
(Map.Entry<WriteCipherGenerator, |
|
124 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
125 |
new SimpleImmutableEntry<WriteCipherGenerator, ProtocolVersion[]>( |
|
126 |
new T10BlockWriteCipherGenerator(), |
|
127 |
ProtocolVersion.PROTOCOLS_TO_10 |
|
128 |
) |
|
129 |
})), |
|
2 | 130 |
|
50768 | 131 |
// domestic strength ciphers |
132 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
|
133 |
B_RC4_128(CIPHER_RC4, STREAM_CIPHER, 16, 16, 0, 0, true, false, |
|
134 |
(Map.Entry<ReadCipherGenerator, |
|
135 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
136 |
new SimpleImmutableEntry<ReadCipherGenerator, ProtocolVersion[]>( |
|
137 |
new StreamReadCipherGenerator(), |
|
138 |
ProtocolVersion.PROTOCOLS_TO_12 |
|
139 |
) |
|
140 |
}), |
|
141 |
(Map.Entry<WriteCipherGenerator, |
|
142 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
143 |
new SimpleImmutableEntry<WriteCipherGenerator, ProtocolVersion[]>( |
|
144 |
new StreamWriteCipherGenerator(), |
|
145 |
ProtocolVersion.PROTOCOLS_TO_12 |
|
146 |
) |
|
147 |
})), |
|
148 |
||
149 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
|
150 |
B_DES(CIPHER_DES, BLOCK_CIPHER, 8, 8, 8, 0, true, false, |
|
151 |
(Map.Entry<ReadCipherGenerator, |
|
152 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
153 |
new SimpleImmutableEntry<ReadCipherGenerator, ProtocolVersion[]>( |
|
154 |
new T10BlockReadCipherGenerator(), |
|
155 |
ProtocolVersion.PROTOCOLS_TO_10 |
|
156 |
), |
|
157 |
new SimpleImmutableEntry<ReadCipherGenerator, ProtocolVersion[]>( |
|
158 |
new T11BlockReadCipherGenerator(), |
|
159 |
ProtocolVersion.PROTOCOLS_OF_11 |
|
160 |
) |
|
161 |
}), |
|
162 |
(Map.Entry<WriteCipherGenerator, |
|
163 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
164 |
new SimpleImmutableEntry<WriteCipherGenerator, ProtocolVersion[]>( |
|
165 |
new T10BlockWriteCipherGenerator(), |
|
166 |
ProtocolVersion.PROTOCOLS_TO_10 |
|
167 |
), |
|
168 |
new SimpleImmutableEntry<WriteCipherGenerator, ProtocolVersion[]>( |
|
169 |
new T11BlockWriteCipherGenerator(), |
|
170 |
ProtocolVersion.PROTOCOLS_OF_11 |
|
171 |
) |
|
172 |
})), |
|
2 | 173 |
|
50768 | 174 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
175 |
B_3DES(CIPHER_3DES, BLOCK_CIPHER, 24, 24, 8, 0, true, false, |
|
176 |
(Map.Entry<ReadCipherGenerator, |
|
177 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
178 |
new SimpleImmutableEntry<ReadCipherGenerator, ProtocolVersion[]>( |
|
179 |
new T10BlockReadCipherGenerator(), |
|
180 |
ProtocolVersion.PROTOCOLS_TO_10 |
|
181 |
), |
|
182 |
new SimpleImmutableEntry<ReadCipherGenerator, ProtocolVersion[]>( |
|
183 |
new T11BlockReadCipherGenerator(), |
|
184 |
ProtocolVersion.PROTOCOLS_11_12 |
|
185 |
) |
|
186 |
}), |
|
187 |
(Map.Entry<WriteCipherGenerator, |
|
188 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
189 |
new SimpleImmutableEntry<WriteCipherGenerator, ProtocolVersion[]>( |
|
190 |
new T10BlockWriteCipherGenerator(), |
|
191 |
ProtocolVersion.PROTOCOLS_TO_10 |
|
192 |
), |
|
193 |
new SimpleImmutableEntry<WriteCipherGenerator, ProtocolVersion[]>( |
|
194 |
new T11BlockWriteCipherGenerator(), |
|
195 |
ProtocolVersion.PROTOCOLS_11_12 |
|
196 |
) |
|
197 |
})), |
|
198 |
||
199 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
|
200 |
B_IDEA("IDEA", BLOCK_CIPHER, 16, 16, 8, 0, false, false, |
|
201 |
(Map.Entry<ReadCipherGenerator, |
|
202 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
203 |
new SimpleImmutableEntry<ReadCipherGenerator, ProtocolVersion[]>( |
|
204 |
null, |
|
205 |
ProtocolVersion.PROTOCOLS_TO_12 |
|
206 |
) |
|
207 |
}), |
|
208 |
(Map.Entry<WriteCipherGenerator, |
|
209 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
210 |
new SimpleImmutableEntry<WriteCipherGenerator, ProtocolVersion[]>( |
|
211 |
null, |
|
212 |
ProtocolVersion.PROTOCOLS_TO_12 |
|
213 |
) |
|
214 |
})), |
|
215 |
||
216 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
|
217 |
B_AES_128(CIPHER_AES, BLOCK_CIPHER, 16, 16, 16, 0, true, false, |
|
218 |
(Map.Entry<ReadCipherGenerator, |
|
219 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
220 |
new SimpleImmutableEntry<ReadCipherGenerator, ProtocolVersion[]>( |
|
221 |
new T10BlockReadCipherGenerator(), |
|
222 |
ProtocolVersion.PROTOCOLS_TO_10 |
|
223 |
), |
|
224 |
new SimpleImmutableEntry<ReadCipherGenerator, ProtocolVersion[]>( |
|
225 |
new T11BlockReadCipherGenerator(), |
|
226 |
ProtocolVersion.PROTOCOLS_11_12 |
|
227 |
) |
|
228 |
}), |
|
229 |
(Map.Entry<WriteCipherGenerator, |
|
230 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
231 |
new SimpleImmutableEntry<WriteCipherGenerator, ProtocolVersion[]>( |
|
232 |
new T10BlockWriteCipherGenerator(), |
|
233 |
ProtocolVersion.PROTOCOLS_TO_10 |
|
234 |
), |
|
235 |
new SimpleImmutableEntry<WriteCipherGenerator, ProtocolVersion[]>( |
|
236 |
new T11BlockWriteCipherGenerator(), |
|
237 |
ProtocolVersion.PROTOCOLS_11_12 |
|
238 |
) |
|
239 |
})), |
|
240 |
||
241 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
|
242 |
B_AES_256(CIPHER_AES, BLOCK_CIPHER, 32, 32, 16, 0, true, false, |
|
243 |
(Map.Entry<ReadCipherGenerator, |
|
244 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
245 |
new SimpleImmutableEntry<ReadCipherGenerator, ProtocolVersion[]>( |
|
246 |
new T10BlockReadCipherGenerator(), |
|
247 |
ProtocolVersion.PROTOCOLS_TO_10 |
|
248 |
), |
|
249 |
new SimpleImmutableEntry<ReadCipherGenerator, ProtocolVersion[]>( |
|
250 |
new T11BlockReadCipherGenerator(), |
|
251 |
ProtocolVersion.PROTOCOLS_11_12 |
|
252 |
) |
|
253 |
}), |
|
254 |
(Map.Entry<WriteCipherGenerator, |
|
255 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
256 |
new SimpleImmutableEntry<WriteCipherGenerator, ProtocolVersion[]>( |
|
257 |
new T10BlockWriteCipherGenerator(), |
|
258 |
ProtocolVersion.PROTOCOLS_TO_10 |
|
259 |
), |
|
260 |
new SimpleImmutableEntry<WriteCipherGenerator, ProtocolVersion[]>( |
|
261 |
new T11BlockWriteCipherGenerator(), |
|
262 |
ProtocolVersion.PROTOCOLS_11_12 |
|
263 |
) |
|
264 |
})), |
|
2 | 265 |
|
50768 | 266 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
267 |
B_AES_128_GCM(CIPHER_AES_GCM, AEAD_CIPHER, 16, 16, 12, 4, true, false, |
|
268 |
(Map.Entry<ReadCipherGenerator, |
|
269 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
270 |
new SimpleImmutableEntry<ReadCipherGenerator, ProtocolVersion[]>( |
|
271 |
new T12GcmReadCipherGenerator(), |
|
272 |
ProtocolVersion.PROTOCOLS_OF_12 |
|
273 |
) |
|
274 |
}), |
|
275 |
(Map.Entry<WriteCipherGenerator, |
|
276 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
277 |
new SimpleImmutableEntry<WriteCipherGenerator, ProtocolVersion[]>( |
|
278 |
new T12GcmWriteCipherGenerator(), |
|
279 |
ProtocolVersion.PROTOCOLS_OF_12 |
|
280 |
) |
|
281 |
})), |
|
2 | 282 |
|
50768 | 283 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
284 |
B_AES_256_GCM(CIPHER_AES_GCM, AEAD_CIPHER, 32, 32, 12, 4, true, false, |
|
285 |
(Map.Entry<ReadCipherGenerator, |
|
286 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
287 |
new SimpleImmutableEntry<ReadCipherGenerator, ProtocolVersion[]>( |
|
288 |
new T12GcmReadCipherGenerator(), |
|
289 |
ProtocolVersion.PROTOCOLS_OF_12 |
|
290 |
) |
|
291 |
}), |
|
292 |
(Map.Entry<WriteCipherGenerator, |
|
293 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
294 |
new SimpleImmutableEntry<WriteCipherGenerator, ProtocolVersion[]>( |
|
295 |
new T12GcmWriteCipherGenerator(), |
|
296 |
ProtocolVersion.PROTOCOLS_OF_12 |
|
297 |
) |
|
298 |
})), |
|
2 | 299 |
|
50768 | 300 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
301 |
B_AES_128_GCM_IV(CIPHER_AES_GCM, AEAD_CIPHER, 16, 16, 12, 0, true, false, |
|
302 |
(Map.Entry<ReadCipherGenerator, |
|
303 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
304 |
new SimpleImmutableEntry<ReadCipherGenerator, ProtocolVersion[]>( |
|
305 |
new T13GcmReadCipherGenerator(), |
|
306 |
ProtocolVersion.PROTOCOLS_OF_13 |
|
307 |
) |
|
308 |
}), |
|
309 |
(Map.Entry<WriteCipherGenerator, |
|
310 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
311 |
new SimpleImmutableEntry<WriteCipherGenerator, ProtocolVersion[]>( |
|
312 |
new T13GcmWriteCipherGenerator(), |
|
313 |
ProtocolVersion.PROTOCOLS_OF_13 |
|
314 |
) |
|
315 |
})), |
|
7039 | 316 |
|
50768 | 317 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
318 |
B_AES_256_GCM_IV(CIPHER_AES_GCM, AEAD_CIPHER, 32, 32, 12, 0, true, false, |
|
319 |
(Map.Entry<ReadCipherGenerator, |
|
320 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
321 |
new SimpleImmutableEntry<ReadCipherGenerator, ProtocolVersion[]>( |
|
322 |
new T13GcmReadCipherGenerator(), |
|
323 |
ProtocolVersion.PROTOCOLS_OF_13 |
|
324 |
) |
|
325 |
}), |
|
326 |
(Map.Entry<WriteCipherGenerator, |
|
327 |
ProtocolVersion[]>[])(new Map.Entry[] { |
|
328 |
new SimpleImmutableEntry<WriteCipherGenerator, ProtocolVersion[]>( |
|
329 |
new T13GcmWriteCipherGenerator(), |
|
330 |
ProtocolVersion.PROTOCOLS_OF_13 |
|
331 |
) |
|
332 |
})); |
|
333 |
||
334 |
// descriptive name including key size, e.g. AES/128 |
|
335 |
final String description; |
|
336 |
||
337 |
// JCE cipher transformation string, e.g. AES/CBC/NoPadding |
|
338 |
final String transformation; |
|
339 |
||
340 |
// algorithm name, e.g. AES |
|
341 |
final String algorithm; |
|
342 |
||
343 |
// supported and compile time enabled. Also see isAvailable() |
|
344 |
final boolean allowed; |
|
345 |
||
346 |
// number of bytes of entropy in the key |
|
347 |
final int keySize; |
|
16913 | 348 |
|
50768 | 349 |
// length of the actual cipher key in bytes. |
350 |
// for non-exportable ciphers, this is the same as keySize |
|
351 |
final int expandedKeySize; |
|
352 |
||
353 |
// size of the IV |
|
354 |
final int ivSize; |
|
355 |
||
356 |
// size of fixed IV |
|
357 |
// |
|
358 |
// record_iv_length = ivSize - fixedIvSize |
|
359 |
final int fixedIvSize; |
|
360 |
||
361 |
// exportable under 512/40 bit rules |
|
362 |
final boolean exportable; |
|
363 |
||
364 |
// Is the cipher algorithm of Cipher Block Chaining (CBC) mode? |
|
365 |
final CipherType cipherType; |
|
16913 | 366 |
|
50768 | 367 |
// size of the authentication tag, only applicable to cipher suites in |
368 |
// Galois Counter Mode (GCM) |
|
369 |
// |
|
370 |
// As far as we know, all supported GCM cipher suites use 128-bits |
|
371 |
// authentication tags. |
|
372 |
final int tagSize = 16; |
|
373 |
||
374 |
// runtime availability |
|
375 |
private final boolean isAvailable; |
|
376 |
||
377 |
private final Map.Entry<ReadCipherGenerator, |
|
378 |
ProtocolVersion[]>[] readCipherGenerators; |
|
379 |
private final Map.Entry<WriteCipherGenerator, |
|
380 |
ProtocolVersion[]>[] writeCipherGenerators; |
|
381 |
||
51574
ed52ea83f830
8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents:
50768
diff
changeset
|
382 |
// Map of Ciphers listed in jdk.tls.keyLimits |
50768 | 383 |
private static final HashMap<String, Long> cipherLimits = new HashMap<>(); |
384 |
||
51574
ed52ea83f830
8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents:
50768
diff
changeset
|
385 |
// Keywords found on the jdk.tls.keyLimits security property. |
50768 | 386 |
final static String tag[] = {"KEYUPDATE"}; |
16913 | 387 |
|
50768 | 388 |
static { |
389 |
final long max = 4611686018427387904L; // 2^62 |
|
390 |
String prop = AccessController.doPrivileged( |
|
391 |
new PrivilegedAction<String>() { |
|
392 |
@Override |
|
393 |
public String run() { |
|
394 |
return Security.getProperty("jdk.tls.keyLimits"); |
|
395 |
} |
|
396 |
}); |
|
397 |
||
398 |
if (prop != null) { |
|
399 |
String propvalue[] = prop.split(","); |
|
400 |
||
401 |
for (String entry : propvalue) { |
|
402 |
int index; |
|
403 |
// If this is not a UsageLimit, goto to next entry. |
|
404 |
String values[] = entry.trim().toUpperCase().split(" "); |
|
405 |
||
406 |
if (values[1].contains(tag[0])) { |
|
407 |
index = 0; |
|
408 |
} else { |
|
409 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
51574
ed52ea83f830
8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents:
50768
diff
changeset
|
410 |
SSLLogger.fine("jdk.tls.keyLimits: Unknown action: " + |
50768 | 411 |
entry); |
412 |
} |
|
413 |
continue; |
|
414 |
} |
|
16913 | 415 |
|
50768 | 416 |
long size; |
417 |
int i = values[2].indexOf("^"); |
|
418 |
try { |
|
419 |
if (i >= 0) { |
|
420 |
size = (long) Math.pow(2, |
|
421 |
Integer.parseInt(values[2].substring(i + 1))); |
|
422 |
} else { |
|
423 |
size = Long.parseLong(values[2]); |
|
424 |
} |
|
425 |
if (size < 1 || size > max) { |
|
51574
ed52ea83f830
8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents:
50768
diff
changeset
|
426 |
throw new NumberFormatException( |
ed52ea83f830
8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents:
50768
diff
changeset
|
427 |
"Length exceeded limits"); |
50768 | 428 |
} |
429 |
} catch (NumberFormatException e) { |
|
430 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
51574
ed52ea83f830
8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents:
50768
diff
changeset
|
431 |
SSLLogger.fine("jdk.tls.keyLimits: " + e.getMessage() + |
50768 | 432 |
": " + entry); |
433 |
} |
|
434 |
continue; |
|
435 |
} |
|
436 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
51574
ed52ea83f830
8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents:
50768
diff
changeset
|
437 |
SSLLogger.fine("jdk.tls.keyLimits: entry = " + entry + |
50768 | 438 |
". " + values[0] + ":" + tag[index] + " = " + size); |
439 |
} |
|
440 |
cipherLimits.put(values[0] + ":" + tag[index], size); |
|
441 |
} |
|
442 |
} |
|
443 |
} |
|
10915 | 444 |
|
50768 | 445 |
private SSLCipher(String transformation, |
446 |
CipherType cipherType, int keySize, |
|
447 |
int expandedKeySize, int ivSize, |
|
448 |
int fixedIvSize, boolean allowed, boolean exportable, |
|
449 |
Map.Entry<ReadCipherGenerator, |
|
450 |
ProtocolVersion[]>[] readCipherGenerators, |
|
451 |
Map.Entry<WriteCipherGenerator, |
|
452 |
ProtocolVersion[]>[] writeCipherGenerators) { |
|
453 |
this.transformation = transformation; |
|
454 |
String[] splits = transformation.split("/"); |
|
455 |
this.algorithm = splits[0]; |
|
456 |
this.cipherType = cipherType; |
|
457 |
this.description = this.algorithm + "/" + (keySize << 3); |
|
458 |
this.keySize = keySize; |
|
459 |
this.ivSize = ivSize; |
|
460 |
this.fixedIvSize = fixedIvSize; |
|
461 |
this.allowed = allowed; |
|
462 |
||
463 |
this.expandedKeySize = expandedKeySize; |
|
464 |
this.exportable = exportable; |
|
465 |
||
466 |
// availability of this bulk cipher |
|
467 |
// |
|
468 |
// We assume all supported ciphers are always available since they are |
|
469 |
// shipped with the SunJCE provider. However, AES/256 is unavailable |
|
470 |
// when the default JCE policy jurisdiction files are installed because |
|
471 |
// of key length restrictions. |
|
472 |
this.isAvailable = allowed && isUnlimited(keySize, transformation); |
|
473 |
||
474 |
this.readCipherGenerators = readCipherGenerators; |
|
475 |
this.writeCipherGenerators = writeCipherGenerators; |
|
476 |
} |
|
477 |
||
478 |
SSLReadCipher createReadCipher(Authenticator authenticator, |
|
479 |
ProtocolVersion protocolVersion, |
|
480 |
SecretKey key, IvParameterSpec iv, |
|
481 |
SecureRandom random) throws GeneralSecurityException { |
|
482 |
if (readCipherGenerators.length == 0) { |
|
483 |
return null; |
|
484 |
} |
|
7039 | 485 |
|
50768 | 486 |
ReadCipherGenerator rcg = null; |
487 |
for (Map.Entry<ReadCipherGenerator, |
|
488 |
ProtocolVersion[]> me : readCipherGenerators) { |
|
489 |
for (ProtocolVersion pv : me.getValue()) { |
|
490 |
if (protocolVersion == pv) { |
|
491 |
rcg = me.getKey(); |
|
492 |
} |
|
493 |
} |
|
494 |
} |
|
495 |
||
496 |
if (rcg != null) { |
|
497 |
return rcg.createCipher(this, authenticator, |
|
498 |
protocolVersion, transformation, key, iv, random); |
|
499 |
} |
|
500 |
return null; |
|
501 |
} |
|
502 |
||
503 |
SSLWriteCipher createWriteCipher(Authenticator authenticator, |
|
504 |
ProtocolVersion protocolVersion, |
|
505 |
SecretKey key, IvParameterSpec iv, |
|
506 |
SecureRandom random) throws GeneralSecurityException { |
|
507 |
if (readCipherGenerators.length == 0) { |
|
508 |
return null; |
|
509 |
} |
|
510 |
||
511 |
WriteCipherGenerator rcg = null; |
|
512 |
for (Map.Entry<WriteCipherGenerator, |
|
513 |
ProtocolVersion[]> me : writeCipherGenerators) { |
|
514 |
for (ProtocolVersion pv : me.getValue()) { |
|
515 |
if (protocolVersion == pv) { |
|
516 |
rcg = me.getKey(); |
|
517 |
} |
|
518 |
} |
|
519 |
} |
|
520 |
||
521 |
if (rcg != null) { |
|
522 |
return rcg.createCipher(this, authenticator, |
|
523 |
protocolVersion, transformation, key, iv, random); |
|
524 |
} |
|
525 |
return null; |
|
2 | 526 |
} |
527 |
||
528 |
/** |
|
50768 | 529 |
* Test if this bulk cipher is available. For use by CipherSuite. |
2 | 530 |
*/ |
50768 | 531 |
boolean isAvailable() { |
532 |
return this.isAvailable; |
|
533 |
} |
|
7039 | 534 |
|
50768 | 535 |
private static boolean isUnlimited(int keySize, String transformation) { |
536 |
int keySizeInBits = keySize * 8; |
|
537 |
if (keySizeInBits > 128) { // need the JCE unlimited |
|
538 |
// strength jurisdiction policy |
|
539 |
try { |
|
540 |
if (Cipher.getMaxAllowedKeyLength( |
|
541 |
transformation) < keySizeInBits) { |
|
542 |
return false; |
|
543 |
} |
|
544 |
} catch (Exception e) { |
|
545 |
return false; |
|
7039 | 546 |
} |
50768 | 547 |
} |
548 |
||
549 |
return true; |
|
550 |
} |
|
551 |
||
552 |
@Override |
|
553 |
public String toString() { |
|
554 |
return description; |
|
555 |
} |
|
556 |
||
557 |
interface ReadCipherGenerator { |
|
558 |
SSLReadCipher createCipher(SSLCipher sslCipher, |
|
559 |
Authenticator authenticator, |
|
560 |
ProtocolVersion protocolVersion, String algorithm, |
|
561 |
Key key, AlgorithmParameterSpec params, |
|
562 |
SecureRandom random) throws GeneralSecurityException; |
|
563 |
} |
|
564 |
||
565 |
abstract static class SSLReadCipher { |
|
566 |
final Authenticator authenticator; |
|
567 |
final ProtocolVersion protocolVersion; |
|
568 |
boolean keyLimitEnabled = false; |
|
569 |
long keyLimitCountdown = 0; |
|
570 |
SecretKey baseSecret; |
|
571 |
||
572 |
SSLReadCipher(Authenticator authenticator, |
|
573 |
ProtocolVersion protocolVersion) { |
|
574 |
this.authenticator = authenticator; |
|
575 |
this.protocolVersion = protocolVersion; |
|
576 |
} |
|
7039 | 577 |
|
50768 | 578 |
static final SSLReadCipher nullTlsReadCipher() { |
579 |
try { |
|
580 |
return B_NULL.createReadCipher( |
|
581 |
Authenticator.nullTlsMac(), |
|
582 |
ProtocolVersion.NONE, null, null, null); |
|
583 |
} catch (GeneralSecurityException gse) { |
|
584 |
// unlikely |
|
585 |
throw new RuntimeException("Cannot create NULL SSLCipher", gse); |
|
586 |
} |
|
587 |
} |
|
588 |
||
589 |
static final SSLReadCipher nullDTlsReadCipher() { |
|
590 |
try { |
|
591 |
return B_NULL.createReadCipher( |
|
592 |
Authenticator.nullDtlsMac(), |
|
593 |
ProtocolVersion.NONE, null, null, null); |
|
594 |
} catch (GeneralSecurityException gse) { |
|
595 |
// unlikely |
|
596 |
throw new RuntimeException("Cannot create NULL SSLCipher", gse); |
|
597 |
} |
|
598 |
} |
|
599 |
||
600 |
abstract Plaintext decrypt(byte contentType, ByteBuffer bb, |
|
601 |
byte[] sequence) throws GeneralSecurityException; |
|
602 |
||
603 |
void dispose() { |
|
604 |
// blank |
|
605 |
} |
|
606 |
||
607 |
abstract int estimateFragmentSize(int packetSize, int headerSize); |
|
608 |
||
609 |
boolean isNullCipher() { |
|
610 |
return false; |
|
611 |
} |
|
612 |
||
613 |
/** |
|
614 |
* Check if processed bytes have reached the key usage limit. |
|
615 |
* If key usage limit is not be monitored, return false. |
|
616 |
*/ |
|
617 |
public boolean atKeyLimit() { |
|
618 |
if (keyLimitCountdown >= 0) { |
|
619 |
return false; |
|
7039 | 620 |
} |
621 |
||
50768 | 622 |
// Turn off limit checking as KeyUpdate will be occurring |
623 |
keyLimitEnabled = false; |
|
624 |
return true; |
|
2 | 625 |
} |
626 |
} |
|
627 |
||
50768 | 628 |
interface WriteCipherGenerator { |
629 |
SSLWriteCipher createCipher(SSLCipher sslCipher, |
|
630 |
Authenticator authenticator, |
|
631 |
ProtocolVersion protocolVersion, String algorithm, |
|
632 |
Key key, AlgorithmParameterSpec params, |
|
633 |
SecureRandom random) throws GeneralSecurityException; |
|
634 |
} |
|
635 |
||
636 |
abstract static class SSLWriteCipher { |
|
637 |
final Authenticator authenticator; |
|
638 |
final ProtocolVersion protocolVersion; |
|
639 |
boolean keyLimitEnabled = false; |
|
640 |
long keyLimitCountdown = 0; |
|
641 |
SecretKey baseSecret; |
|
642 |
||
643 |
SSLWriteCipher(Authenticator authenticator, |
|
644 |
ProtocolVersion protocolVersion) { |
|
645 |
this.authenticator = authenticator; |
|
646 |
this.protocolVersion = protocolVersion; |
|
647 |
} |
|
648 |
||
649 |
abstract int encrypt(byte contentType, ByteBuffer bb); |
|
650 |
||
651 |
static final SSLWriteCipher nullTlsWriteCipher() { |
|
652 |
try { |
|
653 |
return B_NULL.createWriteCipher( |
|
654 |
Authenticator.nullTlsMac(), |
|
655 |
ProtocolVersion.NONE, null, null, null); |
|
656 |
} catch (GeneralSecurityException gse) { |
|
657 |
// unlikely |
|
658 |
throw new RuntimeException( |
|
659 |
"Cannot create NULL SSL write Cipher", gse); |
|
660 |
} |
|
661 |
} |
|
662 |
||
663 |
static final SSLWriteCipher nullDTlsWriteCipher() { |
|
664 |
try { |
|
665 |
return B_NULL.createWriteCipher( |
|
666 |
Authenticator.nullDtlsMac(), |
|
667 |
ProtocolVersion.NONE, null, null, null); |
|
668 |
} catch (GeneralSecurityException gse) { |
|
669 |
// unlikely |
|
670 |
throw new RuntimeException( |
|
671 |
"Cannot create NULL SSL write Cipher", gse); |
|
672 |
} |
|
673 |
} |
|
674 |
||
675 |
void dispose() { |
|
676 |
// blank |
|
677 |
} |
|
678 |
||
679 |
abstract int getExplicitNonceSize(); |
|
680 |
abstract int calculateFragmentSize(int packetLimit, int headerSize); |
|
681 |
abstract int calculatePacketSize(int fragmentSize, int headerSize); |
|
682 |
||
683 |
boolean isCBCMode() { |
|
684 |
return false; |
|
685 |
} |
|
686 |
||
687 |
boolean isNullCipher() { |
|
688 |
return false; |
|
689 |
} |
|
690 |
||
691 |
/** |
|
692 |
* Check if processed bytes have reached the key usage limit. |
|
693 |
* If key usage limit is not be monitored, return false. |
|
694 |
*/ |
|
695 |
public boolean atKeyLimit() { |
|
696 |
if (keyLimitCountdown >= 0) { |
|
697 |
return false; |
|
698 |
} |
|
699 |
||
700 |
// Turn off limit checking as KeyUpdate will be occurring |
|
701 |
keyLimitEnabled = false; |
|
702 |
return true; |
|
703 |
} |
|
704 |
} |
|
705 |
||
706 |
private static final |
|
707 |
class NullReadCipherGenerator implements ReadCipherGenerator { |
|
708 |
@Override |
|
709 |
public SSLReadCipher createCipher(SSLCipher sslCipher, |
|
710 |
Authenticator authenticator, |
|
711 |
ProtocolVersion protocolVersion, String algorithm, |
|
712 |
Key key, AlgorithmParameterSpec params, |
|
713 |
SecureRandom random) throws GeneralSecurityException { |
|
714 |
return new NullReadCipher(authenticator, protocolVersion); |
|
2 | 715 |
} |
7039 | 716 |
|
50768 | 717 |
static final class NullReadCipher extends SSLReadCipher { |
718 |
NullReadCipher(Authenticator authenticator, |
|
719 |
ProtocolVersion protocolVersion) { |
|
720 |
super(authenticator, protocolVersion); |
|
721 |
} |
|
722 |
||
723 |
@Override |
|
724 |
public Plaintext decrypt(byte contentType, ByteBuffer bb, |
|
725 |
byte[] sequence) throws GeneralSecurityException { |
|
726 |
MAC signer = (MAC)authenticator; |
|
727 |
if (signer.macAlg().size != 0) { |
|
728 |
checkStreamMac(signer, bb, contentType, sequence); |
|
729 |
} else { |
|
730 |
authenticator.increaseSequenceNumber(); |
|
731 |
} |
|
732 |
||
733 |
return new Plaintext(contentType, |
|
734 |
ProtocolVersion.NONE.major, ProtocolVersion.NONE.minor, |
|
735 |
-1, -1L, bb.slice()); |
|
736 |
} |
|
737 |
||
738 |
@Override |
|
739 |
int estimateFragmentSize(int packetSize, int headerSize) { |
|
740 |
int macLen = ((MAC)authenticator).macAlg().size; |
|
741 |
return packetSize - headerSize - macLen; |
|
742 |
} |
|
743 |
||
744 |
@Override |
|
745 |
boolean isNullCipher() { |
|
746 |
return true; |
|
747 |
} |
|
748 |
} |
|
749 |
} |
|
750 |
||
751 |
private static final |
|
752 |
class NullWriteCipherGenerator implements WriteCipherGenerator { |
|
753 |
@Override |
|
754 |
public SSLWriteCipher createCipher(SSLCipher sslCipher, |
|
755 |
Authenticator authenticator, |
|
756 |
ProtocolVersion protocolVersion, String algorithm, |
|
757 |
Key key, AlgorithmParameterSpec params, |
|
758 |
SecureRandom random) throws GeneralSecurityException { |
|
759 |
return new NullWriteCipher(authenticator, protocolVersion); |
|
760 |
} |
|
761 |
||
762 |
static final class NullWriteCipher extends SSLWriteCipher { |
|
763 |
NullWriteCipher(Authenticator authenticator, |
|
764 |
ProtocolVersion protocolVersion) { |
|
765 |
super(authenticator, protocolVersion); |
|
766 |
} |
|
767 |
||
768 |
@Override |
|
769 |
public int encrypt(byte contentType, ByteBuffer bb) { |
|
770 |
// add message authentication code |
|
771 |
MAC signer = (MAC)authenticator; |
|
772 |
if (signer.macAlg().size != 0) { |
|
773 |
addMac(signer, bb, contentType); |
|
774 |
} else { |
|
775 |
authenticator.increaseSequenceNumber(); |
|
776 |
} |
|
777 |
||
778 |
int len = bb.remaining(); |
|
779 |
bb.position(bb.limit()); |
|
780 |
return len; |
|
781 |
} |
|
782 |
||
783 |
||
784 |
@Override |
|
785 |
int getExplicitNonceSize() { |
|
786 |
return 0; |
|
787 |
} |
|
788 |
||
789 |
@Override |
|
790 |
int calculateFragmentSize(int packetLimit, int headerSize) { |
|
791 |
int macLen = ((MAC)authenticator).macAlg().size; |
|
792 |
return packetLimit - headerSize - macLen; |
|
793 |
} |
|
794 |
||
795 |
@Override |
|
796 |
int calculatePacketSize(int fragmentSize, int headerSize) { |
|
797 |
int macLen = ((MAC)authenticator).macAlg().size; |
|
798 |
return fragmentSize + headerSize + macLen; |
|
799 |
} |
|
800 |
||
801 |
@Override |
|
802 |
boolean isNullCipher() { |
|
803 |
return true; |
|
804 |
} |
|
2 | 805 |
} |
806 |
} |
|
807 |
||
50768 | 808 |
private static final |
809 |
class StreamReadCipherGenerator implements ReadCipherGenerator { |
|
810 |
@Override |
|
811 |
public SSLReadCipher createCipher(SSLCipher sslCipher, |
|
812 |
Authenticator authenticator, |
|
813 |
ProtocolVersion protocolVersion, String algorithm, |
|
814 |
Key key, AlgorithmParameterSpec params, |
|
815 |
SecureRandom random) throws GeneralSecurityException { |
|
816 |
return new StreamReadCipher(authenticator, protocolVersion, |
|
817 |
algorithm, key, params, random); |
|
7039 | 818 |
} |
819 |
||
50768 | 820 |
static final class StreamReadCipher extends SSLReadCipher { |
821 |
private final Cipher cipher; |
|
822 |
||
823 |
StreamReadCipher(Authenticator authenticator, |
|
824 |
ProtocolVersion protocolVersion, String algorithm, |
|
825 |
Key key, AlgorithmParameterSpec params, |
|
826 |
SecureRandom random) throws GeneralSecurityException { |
|
827 |
super(authenticator, protocolVersion); |
|
828 |
this.cipher = JsseJce.getCipher(algorithm); |
|
829 |
cipher.init(Cipher.DECRYPT_MODE, key, params, random); |
|
830 |
} |
|
831 |
||
832 |
@Override |
|
833 |
public Plaintext decrypt(byte contentType, ByteBuffer bb, |
|
834 |
byte[] sequence) throws GeneralSecurityException { |
|
835 |
int len = bb.remaining(); |
|
836 |
int pos = bb.position(); |
|
837 |
ByteBuffer dup = bb.duplicate(); |
|
838 |
try { |
|
839 |
if (len != cipher.update(dup, bb)) { |
|
840 |
// catch BouncyCastle buffering error |
|
841 |
throw new RuntimeException( |
|
842 |
"Unexpected number of plaintext bytes"); |
|
843 |
} |
|
844 |
if (bb.position() != dup.position()) { |
|
845 |
throw new RuntimeException( |
|
846 |
"Unexpected ByteBuffer position"); |
|
847 |
} |
|
848 |
} catch (ShortBufferException sbe) { |
|
849 |
// catch BouncyCastle buffering error |
|
850 |
throw new RuntimeException("Cipher buffering error in " + |
|
851 |
"JCE provider " + cipher.getProvider().getName(), sbe); |
|
852 |
} |
|
853 |
bb.position(pos); |
|
854 |
if (SSLLogger.isOn && SSLLogger.isOn("plaintext")) { |
|
855 |
SSLLogger.fine( |
|
856 |
"Plaintext after DECRYPTION", bb.duplicate()); |
|
857 |
} |
|
858 |
||
859 |
MAC signer = (MAC)authenticator; |
|
860 |
if (signer.macAlg().size != 0) { |
|
861 |
checkStreamMac(signer, bb, contentType, sequence); |
|
862 |
} else { |
|
863 |
authenticator.increaseSequenceNumber(); |
|
864 |
} |
|
865 |
||
866 |
return new Plaintext(contentType, |
|
867 |
ProtocolVersion.NONE.major, ProtocolVersion.NONE.minor, |
|
868 |
-1, -1L, bb.slice()); |
|
869 |
} |
|
870 |
||
871 |
@Override |
|
872 |
void dispose() { |
|
873 |
if (cipher != null) { |
|
874 |
try { |
|
875 |
cipher.doFinal(); |
|
876 |
} catch (Exception e) { |
|
877 |
// swallow all types of exceptions. |
|
878 |
} |
|
879 |
} |
|
880 |
} |
|
881 |
||
882 |
@Override |
|
883 |
int estimateFragmentSize(int packetSize, int headerSize) { |
|
884 |
int macLen = ((MAC)authenticator).macAlg().size; |
|
885 |
return packetSize - headerSize - macLen; |
|
886 |
} |
|
887 |
} |
|
7039 | 888 |
} |
889 |
||
50768 | 890 |
private static final |
891 |
class StreamWriteCipherGenerator implements WriteCipherGenerator { |
|
892 |
@Override |
|
893 |
public SSLWriteCipher createCipher(SSLCipher sslCipher, |
|
894 |
Authenticator authenticator, |
|
895 |
ProtocolVersion protocolVersion, String algorithm, |
|
896 |
Key key, AlgorithmParameterSpec params, |
|
897 |
SecureRandom random) throws GeneralSecurityException { |
|
898 |
return new StreamWriteCipher(authenticator, |
|
899 |
protocolVersion, algorithm, key, params, random); |
|
2 | 900 |
} |
7039 | 901 |
|
50768 | 902 |
static final class StreamWriteCipher extends SSLWriteCipher { |
903 |
private final Cipher cipher; |
|
904 |
||
905 |
StreamWriteCipher(Authenticator authenticator, |
|
906 |
ProtocolVersion protocolVersion, String algorithm, |
|
907 |
Key key, AlgorithmParameterSpec params, |
|
908 |
SecureRandom random) throws GeneralSecurityException { |
|
909 |
super(authenticator, protocolVersion); |
|
910 |
this.cipher = JsseJce.getCipher(algorithm); |
|
911 |
cipher.init(Cipher.ENCRYPT_MODE, key, params, random); |
|
912 |
} |
|
913 |
||
914 |
@Override |
|
915 |
public int encrypt(byte contentType, ByteBuffer bb) { |
|
916 |
// add message authentication code |
|
917 |
MAC signer = (MAC)authenticator; |
|
918 |
if (signer.macAlg().size != 0) { |
|
919 |
addMac(signer, bb, contentType); |
|
920 |
} else { |
|
921 |
authenticator.increaseSequenceNumber(); |
|
922 |
} |
|
923 |
||
924 |
if (SSLLogger.isOn && SSLLogger.isOn("plaintext")) { |
|
925 |
SSLLogger.finest( |
|
926 |
"Padded plaintext before ENCRYPTION", bb.duplicate()); |
|
927 |
} |
|
928 |
||
929 |
int len = bb.remaining(); |
|
930 |
ByteBuffer dup = bb.duplicate(); |
|
931 |
try { |
|
932 |
if (len != cipher.update(dup, bb)) { |
|
933 |
// catch BouncyCastle buffering error |
|
934 |
throw new RuntimeException( |
|
935 |
"Unexpected number of plaintext bytes"); |
|
936 |
} |
|
937 |
if (bb.position() != dup.position()) { |
|
938 |
throw new RuntimeException( |
|
939 |
"Unexpected ByteBuffer position"); |
|
940 |
} |
|
941 |
} catch (ShortBufferException sbe) { |
|
942 |
// catch BouncyCastle buffering error |
|
943 |
throw new RuntimeException("Cipher buffering error in " + |
|
944 |
"JCE provider " + cipher.getProvider().getName(), sbe); |
|
945 |
} |
|
946 |
||
947 |
return len; |
|
948 |
} |
|
949 |
||
950 |
@Override |
|
951 |
void dispose() { |
|
952 |
if (cipher != null) { |
|
953 |
try { |
|
954 |
cipher.doFinal(); |
|
955 |
} catch (Exception e) { |
|
956 |
// swallow all types of exceptions. |
|
957 |
} |
|
958 |
} |
|
959 |
} |
|
960 |
||
961 |
@Override |
|
962 |
int getExplicitNonceSize() { |
|
963 |
return 0; |
|
2 | 964 |
} |
16913 | 965 |
|
50768 | 966 |
@Override |
967 |
int calculateFragmentSize(int packetLimit, int headerSize) { |
|
968 |
int macLen = ((MAC)authenticator).macAlg().size; |
|
969 |
return packetLimit - headerSize - macLen; |
|
970 |
} |
|
971 |
||
972 |
@Override |
|
973 |
int calculatePacketSize(int fragmentSize, int headerSize) { |
|
974 |
int macLen = ((MAC)authenticator).macAlg().size; |
|
975 |
return fragmentSize + headerSize + macLen; |
|
976 |
} |
|
977 |
} |
|
978 |
} |
|
2 | 979 |
|
50768 | 980 |
private static final |
981 |
class T10BlockReadCipherGenerator implements ReadCipherGenerator { |
|
982 |
@Override |
|
983 |
public SSLReadCipher createCipher(SSLCipher sslCipher, |
|
984 |
Authenticator authenticator, |
|
985 |
ProtocolVersion protocolVersion, String algorithm, |
|
986 |
Key key, AlgorithmParameterSpec params, |
|
987 |
SecureRandom random) throws GeneralSecurityException { |
|
988 |
return new BlockReadCipher(authenticator, |
|
989 |
protocolVersion, algorithm, key, params, random); |
|
990 |
} |
|
991 |
||
992 |
static final class BlockReadCipher extends SSLReadCipher { |
|
993 |
private final Cipher cipher; |
|
994 |
||
995 |
BlockReadCipher(Authenticator authenticator, |
|
996 |
ProtocolVersion protocolVersion, String algorithm, |
|
997 |
Key key, AlgorithmParameterSpec params, |
|
998 |
SecureRandom random) throws GeneralSecurityException { |
|
999 |
super(authenticator, protocolVersion); |
|
1000 |
this.cipher = JsseJce.getCipher(algorithm); |
|
1001 |
cipher.init(Cipher.DECRYPT_MODE, key, params, random); |
|
2 | 1002 |
} |
16913 | 1003 |
|
50768 | 1004 |
@Override |
1005 |
public Plaintext decrypt(byte contentType, ByteBuffer bb, |
|
1006 |
byte[] sequence) throws GeneralSecurityException { |
|
1007 |
BadPaddingException reservedBPE = null; |
|
16913 | 1008 |
|
50768 | 1009 |
// sanity check length of the ciphertext |
1010 |
MAC signer = (MAC)authenticator; |
|
1011 |
int cipheredLength = bb.remaining(); |
|
1012 |
int tagLen = signer.macAlg().size; |
|
1013 |
if (tagLen != 0) { |
|
1014 |
if (!sanityCheck(tagLen, bb.remaining())) { |
|
1015 |
reservedBPE = new BadPaddingException( |
|
1016 |
"ciphertext sanity check failed"); |
|
1017 |
} |
|
1018 |
} |
|
1019 |
// decryption |
|
1020 |
int len = bb.remaining(); |
|
1021 |
int pos = bb.position(); |
|
1022 |
ByteBuffer dup = bb.duplicate(); |
|
16913 | 1023 |
try { |
50768 | 1024 |
if (len != cipher.update(dup, bb)) { |
1025 |
// catch BouncyCastle buffering error |
|
1026 |
throw new RuntimeException( |
|
1027 |
"Unexpected number of plaintext bytes"); |
|
1028 |
} |
|
1029 |
||
1030 |
if (bb.position() != dup.position()) { |
|
1031 |
throw new RuntimeException( |
|
1032 |
"Unexpected ByteBuffer position"); |
|
1033 |
} |
|
1034 |
} catch (ShortBufferException sbe) { |
|
1035 |
// catch BouncyCastle buffering error |
|
1036 |
throw new RuntimeException("Cipher buffering error in " + |
|
1037 |
"JCE provider " + cipher.getProvider().getName(), sbe); |
|
1038 |
} |
|
1039 |
||
1040 |
if (SSLLogger.isOn && SSLLogger.isOn("plaintext")) { |
|
1041 |
SSLLogger.fine( |
|
1042 |
"Padded plaintext after DECRYPTION", |
|
1043 |
bb.duplicate().position(pos)); |
|
1044 |
} |
|
1045 |
||
1046 |
// remove the block padding |
|
1047 |
int blockSize = cipher.getBlockSize(); |
|
1048 |
bb.position(pos); |
|
1049 |
try { |
|
1050 |
removePadding(bb, tagLen, blockSize, protocolVersion); |
|
1051 |
} catch (BadPaddingException bpe) { |
|
1052 |
if (reservedBPE == null) { |
|
1053 |
reservedBPE = bpe; |
|
1054 |
} |
|
16913 | 1055 |
} |
50768 | 1056 |
|
1057 |
// Requires message authentication code for null, stream and |
|
1058 |
// block cipher suites. |
|
1059 |
try { |
|
1060 |
if (tagLen != 0) { |
|
1061 |
checkCBCMac(signer, bb, |
|
1062 |
contentType, cipheredLength, sequence); |
|
1063 |
} else { |
|
1064 |
authenticator.increaseSequenceNumber(); |
|
1065 |
} |
|
1066 |
} catch (BadPaddingException bpe) { |
|
1067 |
if (reservedBPE == null) { |
|
1068 |
reservedBPE = bpe; |
|
1069 |
} |
|
1070 |
} |
|
1071 |
||
1072 |
// Is it a failover? |
|
1073 |
if (reservedBPE != null) { |
|
1074 |
throw reservedBPE; |
|
1075 |
} |
|
1076 |
||
1077 |
return new Plaintext(contentType, |
|
1078 |
ProtocolVersion.NONE.major, ProtocolVersion.NONE.minor, |
|
1079 |
-1, -1L, bb.slice()); |
|
1080 |
} |
|
1081 |
||
1082 |
@Override |
|
1083 |
void dispose() { |
|
1084 |
if (cipher != null) { |
|
1085 |
try { |
|
1086 |
cipher.doFinal(); |
|
1087 |
} catch (Exception e) { |
|
1088 |
// swallow all types of exceptions. |
|
1089 |
} |
|
16913 | 1090 |
} |
50768 | 1091 |
} |
1092 |
||
1093 |
@Override |
|
1094 |
int estimateFragmentSize(int packetSize, int headerSize) { |
|
1095 |
int macLen = ((MAC)authenticator).macAlg().size; |
|
1096 |
||
1097 |
// No padding for a maximum fragment. |
|
1098 |
// |
|
1099 |
// 1 byte padding length field: 0x00 |
|
1100 |
return packetSize - headerSize - macLen - 1; |
|
2 | 1101 |
} |
50768 | 1102 |
|
1103 |
/** |
|
1104 |
* Sanity check the length of a fragment before decryption. |
|
1105 |
* |
|
1106 |
* In CBC mode, check that the fragment length is one or multiple |
|
1107 |
* times of the block size of the cipher suite, and is at least |
|
1108 |
* one (one is the smallest size of padding in CBC mode) bigger |
|
1109 |
* than the tag size of the MAC algorithm except the explicit IV |
|
1110 |
* size for TLS 1.1 or later. |
|
1111 |
* |
|
1112 |
* In non-CBC mode, check that the fragment length is not less than |
|
1113 |
* the tag size of the MAC algorithm. |
|
1114 |
* |
|
1115 |
* @return true if the length of a fragment matches above |
|
1116 |
* requirements |
|
1117 |
*/ |
|
1118 |
private boolean sanityCheck(int tagLen, int fragmentLen) { |
|
1119 |
int blockSize = cipher.getBlockSize(); |
|
1120 |
if ((fragmentLen % blockSize) == 0) { |
|
1121 |
int minimal = tagLen + 1; |
|
1122 |
minimal = (minimal >= blockSize) ? minimal : blockSize; |
|
1123 |
||
1124 |
return (fragmentLen >= minimal); |
|
1125 |
} |
|
1126 |
||
1127 |
return false; |
|
1128 |
} |
|
2 | 1129 |
} |
1130 |
} |
|
1131 |
||
50768 | 1132 |
private static final |
1133 |
class T10BlockWriteCipherGenerator implements WriteCipherGenerator { |
|
1134 |
@Override |
|
1135 |
public SSLWriteCipher createCipher(SSLCipher sslCipher, |
|
1136 |
Authenticator authenticator, |
|
1137 |
ProtocolVersion protocolVersion, String algorithm, |
|
1138 |
Key key, AlgorithmParameterSpec params, |
|
1139 |
SecureRandom random) throws GeneralSecurityException { |
|
1140 |
return new BlockWriteCipher(authenticator, |
|
1141 |
protocolVersion, algorithm, key, params, random); |
|
16913 | 1142 |
} |
7039 | 1143 |
|
50768 | 1144 |
static final class BlockWriteCipher extends SSLWriteCipher { |
1145 |
private final Cipher cipher; |
|
1146 |
||
1147 |
BlockWriteCipher(Authenticator authenticator, |
|
1148 |
ProtocolVersion protocolVersion, String algorithm, |
|
1149 |
Key key, AlgorithmParameterSpec params, |
|
1150 |
SecureRandom random) throws GeneralSecurityException { |
|
1151 |
super(authenticator, protocolVersion); |
|
1152 |
this.cipher = JsseJce.getCipher(algorithm); |
|
1153 |
cipher.init(Cipher.ENCRYPT_MODE, key, params, random); |
|
2 | 1154 |
} |
1155 |
||
50768 | 1156 |
@Override |
1157 |
public int encrypt(byte contentType, ByteBuffer bb) { |
|
1158 |
int pos = bb.position(); |
|
1159 |
||
1160 |
// add message authentication code |
|
1161 |
MAC signer = (MAC)authenticator; |
|
1162 |
if (signer.macAlg().size != 0) { |
|
1163 |
addMac(signer, bb, contentType); |
|
1164 |
} else { |
|
1165 |
authenticator.increaseSequenceNumber(); |
|
1166 |
} |
|
1167 |
||
1168 |
int blockSize = cipher.getBlockSize(); |
|
1169 |
int len = addPadding(bb, blockSize); |
|
1170 |
bb.position(pos); |
|
1171 |
||
1172 |
if (SSLLogger.isOn && SSLLogger.isOn("plaintext")) { |
|
1173 |
SSLLogger.fine( |
|
1174 |
"Padded plaintext before ENCRYPTION", |
|
1175 |
bb.duplicate()); |
|
1176 |
} |
|
1177 |
||
1178 |
ByteBuffer dup = bb.duplicate(); |
|
1179 |
try { |
|
1180 |
if (len != cipher.update(dup, bb)) { |
|
1181 |
// catch BouncyCastle buffering error |
|
1182 |
throw new RuntimeException( |
|
1183 |
"Unexpected number of plaintext bytes"); |
|
1184 |
} |
|
1185 |
||
1186 |
if (bb.position() != dup.position()) { |
|
1187 |
throw new RuntimeException( |
|
1188 |
"Unexpected ByteBuffer position"); |
|
1189 |
} |
|
1190 |
} catch (ShortBufferException sbe) { |
|
1191 |
// catch BouncyCastle buffering error |
|
1192 |
throw new RuntimeException("Cipher buffering error in " + |
|
1193 |
"JCE provider " + cipher.getProvider().getName(), sbe); |
|
1194 |
} |
|
1195 |
||
1196 |
return len; |
|
2 | 1197 |
} |
1198 |
||
50768 | 1199 |
@Override |
1200 |
void dispose() { |
|
1201 |
if (cipher != null) { |
|
1202 |
try { |
|
1203 |
cipher.doFinal(); |
|
1204 |
} catch (Exception e) { |
|
1205 |
// swallow all types of exceptions. |
|
1206 |
} |
|
1207 |
} |
|
1208 |
} |
|
1209 |
||
1210 |
@Override |
|
1211 |
int getExplicitNonceSize() { |
|
1212 |
return 0; |
|
2 | 1213 |
} |
50768 | 1214 |
|
1215 |
@Override |
|
1216 |
int calculateFragmentSize(int packetLimit, int headerSize) { |
|
1217 |
int macLen = ((MAC)authenticator).macAlg().size; |
|
1218 |
int blockSize = cipher.getBlockSize(); |
|
1219 |
int fragLen = packetLimit - headerSize; |
|
1220 |
fragLen -= (fragLen % blockSize); // cannot hold a block |
|
1221 |
// No padding for a maximum fragment. |
|
1222 |
fragLen -= 1; // 1 byte padding length field: 0x00 |
|
1223 |
fragLen -= macLen; |
|
1224 |
return fragLen; |
|
1225 |
} |
|
1226 |
||
1227 |
@Override |
|
1228 |
int calculatePacketSize(int fragmentSize, int headerSize) { |
|
1229 |
int macLen = ((MAC)authenticator).macAlg().size; |
|
1230 |
int blockSize = cipher.getBlockSize(); |
|
1231 |
int paddedLen = fragmentSize + macLen + 1; |
|
1232 |
if ((paddedLen % blockSize) != 0) { |
|
1233 |
paddedLen += blockSize - 1; |
|
1234 |
paddedLen -= paddedLen % blockSize; |
|
1235 |
} |
|
1236 |
||
1237 |
return headerSize + paddedLen; |
|
1238 |
} |
|
1239 |
||
1240 |
@Override |
|
1241 |
boolean isCBCMode() { |
|
1242 |
return true; |
|
1243 |
} |
|
2 | 1244 |
} |
1245 |
} |
|
1246 |
||
50768 | 1247 |
// For TLS 1.1 and 1.2 |
1248 |
private static final |
|
1249 |
class T11BlockReadCipherGenerator implements ReadCipherGenerator { |
|
1250 |
@Override |
|
1251 |
public SSLReadCipher createCipher(SSLCipher sslCipher, |
|
1252 |
Authenticator authenticator, ProtocolVersion protocolVersion, |
|
1253 |
String algorithm, Key key, AlgorithmParameterSpec params, |
|
1254 |
SecureRandom random) throws GeneralSecurityException { |
|
1255 |
return new BlockReadCipher(authenticator, protocolVersion, |
|
1256 |
sslCipher, algorithm, key, params, random); |
|
2 | 1257 |
} |
7039 | 1258 |
|
50768 | 1259 |
static final class BlockReadCipher extends SSLReadCipher { |
1260 |
private final Cipher cipher; |
|
1261 |
||
1262 |
BlockReadCipher(Authenticator authenticator, |
|
1263 |
ProtocolVersion protocolVersion, |
|
1264 |
SSLCipher sslCipher, String algorithm, |
|
1265 |
Key key, AlgorithmParameterSpec params, |
|
1266 |
SecureRandom random) throws GeneralSecurityException { |
|
1267 |
super(authenticator, protocolVersion); |
|
1268 |
this.cipher = JsseJce.getCipher(algorithm); |
|
1269 |
if (params == null) { |
|
1270 |
params = new IvParameterSpec(new byte[sslCipher.ivSize]); |
|
16913 | 1271 |
} |
50768 | 1272 |
cipher.init(Cipher.DECRYPT_MODE, key, params, random); |
2 | 1273 |
} |
16113 | 1274 |
|
50768 | 1275 |
@Override |
1276 |
public Plaintext decrypt(byte contentType, ByteBuffer bb, |
|
1277 |
byte[] sequence) throws GeneralSecurityException { |
|
1278 |
BadPaddingException reservedBPE = null; |
|
1279 |
||
1280 |
// sanity check length of the ciphertext |
|
1281 |
MAC signer = (MAC)authenticator; |
|
1282 |
int cipheredLength = bb.remaining(); |
|
1283 |
int tagLen = signer.macAlg().size; |
|
1284 |
if (tagLen != 0) { |
|
1285 |
if (!sanityCheck(tagLen, bb.remaining())) { |
|
1286 |
reservedBPE = new BadPaddingException( |
|
1287 |
"ciphertext sanity check failed"); |
|
1288 |
} |
|
1289 |
} |
|
1290 |
||
1291 |
// decryption |
|
1292 |
int len = bb.remaining(); |
|
1293 |
int pos = bb.position(); |
|
1294 |
ByteBuffer dup = bb.duplicate(); |
|
1295 |
try { |
|
1296 |
if (len != cipher.update(dup, bb)) { |
|
1297 |
// catch BouncyCastle buffering error |
|
1298 |
throw new RuntimeException( |
|
1299 |
"Unexpected number of plaintext bytes"); |
|
1300 |
} |
|
1301 |
||
1302 |
if (bb.position() != dup.position()) { |
|
1303 |
throw new RuntimeException( |
|
1304 |
"Unexpected ByteBuffer position"); |
|
1305 |
} |
|
1306 |
} catch (ShortBufferException sbe) { |
|
1307 |
// catch BouncyCastle buffering error |
|
1308 |
throw new RuntimeException("Cipher buffering error in " + |
|
1309 |
"JCE provider " + cipher.getProvider().getName(), sbe); |
|
1310 |
} |
|
1311 |
||
1312 |
if (SSLLogger.isOn && SSLLogger.isOn("plaintext")) { |
|
1313 |
SSLLogger.fine( |
|
1314 |
"Padded plaintext after DECRYPTION", |
|
1315 |
bb.duplicate().position(pos)); |
|
1316 |
} |
|
7039 | 1317 |
|
50768 | 1318 |
// Ignore the explicit nonce. |
1319 |
bb.position(pos + cipher.getBlockSize()); |
|
1320 |
pos = bb.position(); |
|
1321 |
||
1322 |
// remove the block padding |
|
1323 |
int blockSize = cipher.getBlockSize(); |
|
1324 |
bb.position(pos); |
|
1325 |
try { |
|
1326 |
removePadding(bb, tagLen, blockSize, protocolVersion); |
|
1327 |
} catch (BadPaddingException bpe) { |
|
1328 |
if (reservedBPE == null) { |
|
1329 |
reservedBPE = bpe; |
|
1330 |
} |
|
1331 |
} |
|
1332 |
||
1333 |
// Requires message authentication code for null, stream and |
|
1334 |
// block cipher suites. |
|
1335 |
try { |
|
1336 |
if (tagLen != 0) { |
|
1337 |
checkCBCMac(signer, bb, |
|
1338 |
contentType, cipheredLength, sequence); |
|
1339 |
} else { |
|
1340 |
authenticator.increaseSequenceNumber(); |
|
1341 |
} |
|
1342 |
} catch (BadPaddingException bpe) { |
|
1343 |
if (reservedBPE == null) { |
|
1344 |
reservedBPE = bpe; |
|
1345 |
} |
|
1346 |
} |
|
1347 |
||
1348 |
// Is it a failover? |
|
1349 |
if (reservedBPE != null) { |
|
1350 |
throw reservedBPE; |
|
1351 |
} |
|
1352 |
||
1353 |
return new Plaintext(contentType, |
|
1354 |
ProtocolVersion.NONE.major, ProtocolVersion.NONE.minor, |
|
1355 |
-1, -1L, bb.slice()); |
|
1356 |
} |
|
1357 |
||
1358 |
@Override |
|
1359 |
void dispose() { |
|
1360 |
if (cipher != null) { |
|
1361 |
try { |
|
1362 |
cipher.doFinal(); |
|
1363 |
} catch (Exception e) { |
|
1364 |
// swallow all types of exceptions. |
|
7039 | 1365 |
} |
1366 |
} |
|
2 | 1367 |
} |
50768 | 1368 |
|
1369 |
@Override |
|
1370 |
int estimateFragmentSize(int packetSize, int headerSize) { |
|
1371 |
int macLen = ((MAC)authenticator).macAlg().size; |
|
1372 |
||
1373 |
// No padding for a maximum fragment. |
|
1374 |
// |
|
1375 |
// 1 byte padding length field: 0x00 |
|
1376 |
int nonceSize = cipher.getBlockSize(); |
|
1377 |
return packetSize - headerSize - nonceSize - macLen - 1; |
|
1378 |
} |
|
1379 |
||
1380 |
/** |
|
1381 |
* Sanity check the length of a fragment before decryption. |
|
1382 |
* |
|
1383 |
* In CBC mode, check that the fragment length is one or multiple |
|
1384 |
* times of the block size of the cipher suite, and is at least |
|
1385 |
* one (one is the smallest size of padding in CBC mode) bigger |
|
1386 |
* than the tag size of the MAC algorithm except the explicit IV |
|
1387 |
* size for TLS 1.1 or later. |
|
1388 |
* |
|
1389 |
* In non-CBC mode, check that the fragment length is not less than |
|
1390 |
* the tag size of the MAC algorithm. |
|
1391 |
* |
|
1392 |
* @return true if the length of a fragment matches above |
|
1393 |
* requirements |
|
1394 |
*/ |
|
1395 |
private boolean sanityCheck(int tagLen, int fragmentLen) { |
|
1396 |
int blockSize = cipher.getBlockSize(); |
|
1397 |
if ((fragmentLen % blockSize) == 0) { |
|
1398 |
int minimal = tagLen + 1; |
|
1399 |
minimal = (minimal >= blockSize) ? minimal : blockSize; |
|
1400 |
minimal += blockSize; |
|
1401 |
||
1402 |
return (fragmentLen >= minimal); |
|
1403 |
} |
|
1404 |
||
1405 |
return false; |
|
1406 |
} |
|
2 | 1407 |
} |
1408 |
} |
|
1409 |
||
50768 | 1410 |
// For TLS 1.1 and 1.2 |
1411 |
private static final |
|
1412 |
class T11BlockWriteCipherGenerator implements WriteCipherGenerator { |
|
1413 |
@Override |
|
1414 |
public SSLWriteCipher createCipher(SSLCipher sslCipher, |
|
1415 |
Authenticator authenticator, ProtocolVersion protocolVersion, |
|
1416 |
String algorithm, Key key, AlgorithmParameterSpec params, |
|
1417 |
SecureRandom random) throws GeneralSecurityException { |
|
1418 |
return new BlockWriteCipher(authenticator, protocolVersion, |
|
1419 |
sslCipher, algorithm, key, params, random); |
|
2 | 1420 |
} |
1421 |
||
50768 | 1422 |
static final class BlockWriteCipher extends SSLWriteCipher { |
1423 |
private final Cipher cipher; |
|
1424 |
private final SecureRandom random; |
|
1425 |
||
1426 |
BlockWriteCipher(Authenticator authenticator, |
|
1427 |
ProtocolVersion protocolVersion, |
|
1428 |
SSLCipher sslCipher, String algorithm, |
|
1429 |
Key key, AlgorithmParameterSpec params, |
|
1430 |
SecureRandom random) throws GeneralSecurityException { |
|
1431 |
super(authenticator, protocolVersion); |
|
1432 |
this.cipher = JsseJce.getCipher(algorithm); |
|
1433 |
this.random = random; |
|
1434 |
if (params == null) { |
|
1435 |
params = new IvParameterSpec(new byte[sslCipher.ivSize]); |
|
1436 |
} |
|
1437 |
cipher.init(Cipher.ENCRYPT_MODE, key, params, random); |
|
1438 |
} |
|
1439 |
||
1440 |
@Override |
|
1441 |
public int encrypt(byte contentType, ByteBuffer bb) { |
|
1442 |
// To be unique and aware of overflow-wrap, sequence number |
|
1443 |
// is used as the nonce_explicit of block cipher suites. |
|
1444 |
int pos = bb.position(); |
|
1445 |
||
1446 |
// add message authentication code |
|
1447 |
MAC signer = (MAC)authenticator; |
|
1448 |
if (signer.macAlg().size != 0) { |
|
1449 |
addMac(signer, bb, contentType); |
|
1450 |
} else { |
|
1451 |
authenticator.increaseSequenceNumber(); |
|
1452 |
} |
|
1453 |
||
1454 |
// DON'T WORRY, the nonce spaces are considered already. |
|
1455 |
byte[] nonce = new byte[cipher.getBlockSize()]; |
|
1456 |
random.nextBytes(nonce); |
|
1457 |
pos = pos - nonce.length; |
|
1458 |
bb.position(pos); |
|
1459 |
bb.put(nonce); |
|
1460 |
bb.position(pos); |
|
1461 |
||
1462 |
int blockSize = cipher.getBlockSize(); |
|
1463 |
int len = addPadding(bb, blockSize); |
|
1464 |
bb.position(pos); |
|
1465 |
||
1466 |
if (SSLLogger.isOn && SSLLogger.isOn("plaintext")) { |
|
1467 |
SSLLogger.fine( |
|
1468 |
"Padded plaintext before ENCRYPTION", |
|
1469 |
bb.duplicate()); |
|
1470 |
} |
|
1471 |
||
1472 |
ByteBuffer dup = bb.duplicate(); |
|
16913 | 1473 |
try { |
50768 | 1474 |
if (len != cipher.update(dup, bb)) { |
1475 |
// catch BouncyCastle buffering error |
|
1476 |
throw new RuntimeException( |
|
1477 |
"Unexpected number of plaintext bytes"); |
|
1478 |
} |
|
1479 |
||
1480 |
if (bb.position() != dup.position()) { |
|
1481 |
throw new RuntimeException( |
|
1482 |
"Unexpected ByteBuffer position"); |
|
1483 |
} |
|
1484 |
} catch (ShortBufferException sbe) { |
|
1485 |
// catch BouncyCastle buffering error |
|
1486 |
throw new RuntimeException("Cipher buffering error in " + |
|
1487 |
"JCE provider " + cipher.getProvider().getName(), sbe); |
|
1488 |
} |
|
1489 |
||
1490 |
return len; |
|
1491 |
} |
|
1492 |
||
1493 |
@Override |
|
1494 |
void dispose() { |
|
1495 |
if (cipher != null) { |
|
1496 |
try { |
|
1497 |
cipher.doFinal(); |
|
1498 |
} catch (Exception e) { |
|
1499 |
// swallow all types of exceptions. |
|
1500 |
} |
|
1501 |
} |
|
1502 |
} |
|
1503 |
||
1504 |
@Override |
|
1505 |
int getExplicitNonceSize() { |
|
1506 |
return cipher.getBlockSize(); |
|
1507 |
} |
|
1508 |
||
1509 |
@Override |
|
1510 |
int calculateFragmentSize(int packetLimit, int headerSize) { |
|
1511 |
int macLen = ((MAC)authenticator).macAlg().size; |
|
1512 |
int blockSize = cipher.getBlockSize(); |
|
1513 |
int fragLen = packetLimit - headerSize - blockSize; |
|
1514 |
fragLen -= (fragLen % blockSize); // cannot hold a block |
|
1515 |
// No padding for a maximum fragment. |
|
1516 |
fragLen -= 1; // 1 byte padding length field: 0x00 |
|
1517 |
fragLen -= macLen; |
|
1518 |
return fragLen; |
|
1519 |
} |
|
1520 |
||
1521 |
@Override |
|
1522 |
int calculatePacketSize(int fragmentSize, int headerSize) { |
|
1523 |
int macLen = ((MAC)authenticator).macAlg().size; |
|
1524 |
int blockSize = cipher.getBlockSize(); |
|
1525 |
int paddedLen = fragmentSize + macLen + 1; |
|
1526 |
if ((paddedLen % blockSize) != 0) { |
|
1527 |
paddedLen += blockSize - 1; |
|
1528 |
paddedLen -= paddedLen % blockSize; |
|
1529 |
} |
|
1530 |
||
1531 |
return headerSize + blockSize + paddedLen; |
|
1532 |
} |
|
1533 |
||
1534 |
@Override |
|
1535 |
boolean isCBCMode() { |
|
1536 |
return true; |
|
1537 |
} |
|
1538 |
} |
|
1539 |
} |
|
1540 |
||
1541 |
private static final |
|
1542 |
class T12GcmReadCipherGenerator implements ReadCipherGenerator { |
|
1543 |
@Override |
|
1544 |
public SSLReadCipher createCipher(SSLCipher sslCipher, |
|
1545 |
Authenticator authenticator, |
|
1546 |
ProtocolVersion protocolVersion, String algorithm, |
|
1547 |
Key key, AlgorithmParameterSpec params, |
|
1548 |
SecureRandom random) throws GeneralSecurityException { |
|
1549 |
return new GcmReadCipher(authenticator, protocolVersion, sslCipher, |
|
1550 |
algorithm, key, params, random); |
|
1551 |
} |
|
1552 |
||
1553 |
static final class GcmReadCipher extends SSLReadCipher { |
|
1554 |
private final Cipher cipher; |
|
1555 |
private final int tagSize; |
|
1556 |
private final Key key; |
|
1557 |
private final byte[] fixedIv; |
|
1558 |
private final int recordIvSize; |
|
1559 |
private final SecureRandom random; |
|
1560 |
||
1561 |
GcmReadCipher(Authenticator authenticator, |
|
1562 |
ProtocolVersion protocolVersion, |
|
1563 |
SSLCipher sslCipher, String algorithm, |
|
1564 |
Key key, AlgorithmParameterSpec params, |
|
1565 |
SecureRandom random) throws GeneralSecurityException { |
|
1566 |
super(authenticator, protocolVersion); |
|
1567 |
this.cipher = JsseJce.getCipher(algorithm); |
|
1568 |
this.tagSize = sslCipher.tagSize; |
|
1569 |
this.key = key; |
|
1570 |
this.fixedIv = ((IvParameterSpec)params).getIV(); |
|
1571 |
this.recordIvSize = sslCipher.ivSize - sslCipher.fixedIvSize; |
|
1572 |
this.random = random; |
|
1573 |
||
1574 |
// DON'T initialize the cipher for AEAD! |
|
1575 |
} |
|
1576 |
||
1577 |
@Override |
|
1578 |
public Plaintext decrypt(byte contentType, ByteBuffer bb, |
|
1579 |
byte[] sequence) throws GeneralSecurityException { |
|
1580 |
if (bb.remaining() < (recordIvSize + tagSize)) { |
|
1581 |
throw new BadPaddingException( |
|
1582 |
"Insufficient buffer remaining for AEAD cipher " + |
|
1583 |
"fragment (" + bb.remaining() + "). Needs to be " + |
|
1584 |
"more than or equal to IV size (" + recordIvSize + |
|
1585 |
") + tag size (" + tagSize + ")"); |
|
1586 |
} |
|
1587 |
||
1588 |
// initialize the AEAD cipher for the unique IV |
|
1589 |
byte[] iv = Arrays.copyOf(fixedIv, |
|
1590 |
fixedIv.length + recordIvSize); |
|
1591 |
bb.get(iv, fixedIv.length, recordIvSize); |
|
1592 |
GCMParameterSpec spec = new GCMParameterSpec(tagSize * 8, iv); |
|
1593 |
try { |
|
1594 |
cipher.init(Cipher.DECRYPT_MODE, key, spec, random); |
|
1595 |
} catch (InvalidKeyException | |
|
1596 |
InvalidAlgorithmParameterException ikae) { |
|
1597 |
// unlikely to happen |
|
1598 |
throw new RuntimeException( |
|
1599 |
"invalid key or spec in GCM mode", ikae); |
|
1600 |
} |
|
1601 |
||
1602 |
// update the additional authentication data |
|
1603 |
byte[] aad = authenticator.acquireAuthenticationBytes( |
|
1604 |
contentType, bb.remaining() - tagSize, |
|
1605 |
sequence); |
|
1606 |
cipher.updateAAD(aad); |
|
1607 |
||
1608 |
// DON'T decrypt the nonce_explicit for AEAD mode. The buffer |
|
1609 |
// position has moved out of the nonce_explicit range. |
|
1610 |
int len, pos = bb.position(); |
|
1611 |
ByteBuffer dup = bb.duplicate(); |
|
1612 |
try { |
|
1613 |
len = cipher.doFinal(dup, bb); |
|
16913 | 1614 |
} catch (IllegalBlockSizeException ibse) { |
1615 |
// unlikely to happen |
|
1616 |
throw new RuntimeException( |
|
1617 |
"Cipher error in AEAD mode \"" + ibse.getMessage() + |
|
1618 |
" \"in JCE provider " + cipher.getProvider().getName()); |
|
50768 | 1619 |
} catch (ShortBufferException sbe) { |
1620 |
// catch BouncyCastle buffering error |
|
1621 |
throw new RuntimeException("Cipher buffering error in " + |
|
1622 |
"JCE provider " + cipher.getProvider().getName(), sbe); |
|
16913 | 1623 |
} |
50768 | 1624 |
// reset the limit to the end of the decrypted data |
1625 |
bb.position(pos); |
|
1626 |
bb.limit(pos + len); |
|
1627 |
||
1628 |
if (SSLLogger.isOn && SSLLogger.isOn("plaintext")) { |
|
1629 |
SSLLogger.fine( |
|
1630 |
"Plaintext after DECRYPTION", bb.duplicate()); |
|
1631 |
} |
|
1632 |
||
1633 |
return new Plaintext(contentType, |
|
1634 |
ProtocolVersion.NONE.major, ProtocolVersion.NONE.minor, |
|
1635 |
-1, -1L, bb.slice()); |
|
1636 |
} |
|
1637 |
||
1638 |
@Override |
|
1639 |
void dispose() { |
|
1640 |
if (cipher != null) { |
|
1641 |
try { |
|
1642 |
cipher.doFinal(); |
|
1643 |
} catch (Exception e) { |
|
1644 |
// swallow all types of exceptions. |
|
1645 |
} |
|
1646 |
} |
|
1647 |
} |
|
1648 |
||
1649 |
@Override |
|
1650 |
int estimateFragmentSize(int packetSize, int headerSize) { |
|
1651 |
return packetSize - headerSize - recordIvSize - tagSize; |
|
1652 |
} |
|
1653 |
} |
|
1654 |
} |
|
1655 |
||
1656 |
private static final |
|
1657 |
class T12GcmWriteCipherGenerator implements WriteCipherGenerator { |
|
1658 |
@Override |
|
1659 |
public SSLWriteCipher createCipher(SSLCipher sslCipher, |
|
1660 |
Authenticator authenticator, |
|
1661 |
ProtocolVersion protocolVersion, String algorithm, |
|
1662 |
Key key, AlgorithmParameterSpec params, |
|
1663 |
SecureRandom random) throws GeneralSecurityException { |
|
1664 |
return new GcmWriteCipher(authenticator, protocolVersion, sslCipher, |
|
1665 |
algorithm, key, params, random); |
|
1666 |
} |
|
1667 |
||
1668 |
private static final class GcmWriteCipher extends SSLWriteCipher { |
|
1669 |
private final Cipher cipher; |
|
1670 |
private final int tagSize; |
|
1671 |
private final Key key; |
|
1672 |
private final byte[] fixedIv; |
|
1673 |
private final int recordIvSize; |
|
1674 |
private final SecureRandom random; |
|
1675 |
||
1676 |
GcmWriteCipher(Authenticator authenticator, |
|
1677 |
ProtocolVersion protocolVersion, |
|
1678 |
SSLCipher sslCipher, String algorithm, |
|
1679 |
Key key, AlgorithmParameterSpec params, |
|
1680 |
SecureRandom random) throws GeneralSecurityException { |
|
1681 |
super(authenticator, protocolVersion); |
|
1682 |
this.cipher = JsseJce.getCipher(algorithm); |
|
1683 |
this.tagSize = sslCipher.tagSize; |
|
1684 |
this.key = key; |
|
1685 |
this.fixedIv = ((IvParameterSpec)params).getIV(); |
|
1686 |
this.recordIvSize = sslCipher.ivSize - sslCipher.fixedIvSize; |
|
1687 |
this.random = random; |
|
1688 |
||
1689 |
// DON'T initialize the cipher for AEAD! |
|
1690 |
} |
|
1691 |
||
1692 |
@Override |
|
1693 |
public int encrypt(byte contentType, |
|
1694 |
ByteBuffer bb) { |
|
1695 |
// To be unique and aware of overflow-wrap, sequence number |
|
1696 |
// is used as the nonce_explicit of AEAD cipher suites. |
|
1697 |
byte[] nonce = authenticator.sequenceNumber(); |
|
1698 |
||
1699 |
// initialize the AEAD cipher for the unique IV |
|
1700 |
byte[] iv = Arrays.copyOf(fixedIv, |
|
1701 |
fixedIv.length + nonce.length); |
|
1702 |
System.arraycopy(nonce, 0, iv, fixedIv.length, nonce.length); |
|
1703 |
||
1704 |
GCMParameterSpec spec = new GCMParameterSpec(tagSize * 8, iv); |
|
1705 |
try { |
|
1706 |
cipher.init(Cipher.ENCRYPT_MODE, key, spec, random); |
|
1707 |
} catch (InvalidKeyException | |
|
1708 |
InvalidAlgorithmParameterException ikae) { |
|
1709 |
// unlikely to happen |
|
1710 |
throw new RuntimeException( |
|
1711 |
"invalid key or spec in GCM mode", ikae); |
|
1712 |
} |
|
1713 |
||
1714 |
// Update the additional authentication data, using the |
|
1715 |
// implicit sequence number of the authenticator. |
|
1716 |
byte[] aad = authenticator.acquireAuthenticationBytes( |
|
1717 |
contentType, bb.remaining(), null); |
|
1718 |
cipher.updateAAD(aad); |
|
1719 |
||
1720 |
// DON'T WORRY, the nonce spaces are considered already. |
|
1721 |
bb.position(bb.position() - nonce.length); |
|
1722 |
bb.put(nonce); |
|
1723 |
||
1724 |
// DON'T encrypt the nonce for AEAD mode. |
|
1725 |
int len, pos = bb.position(); |
|
1726 |
if (SSLLogger.isOn && SSLLogger.isOn("plaintext")) { |
|
1727 |
SSLLogger.fine( |
|
1728 |
"Plaintext before ENCRYPTION", |
|
1729 |
bb.duplicate()); |
|
1730 |
} |
|
1731 |
||
1732 |
ByteBuffer dup = bb.duplicate(); |
|
1733 |
int outputSize = cipher.getOutputSize(dup.remaining()); |
|
1734 |
if (outputSize > bb.remaining()) { |
|
1735 |
// Need to expand the limit of the output buffer for |
|
1736 |
// the authentication tag. |
|
1737 |
// |
|
1738 |
// DON'T worry about the buffer's capacity, we have |
|
1739 |
// reserved space for the authentication tag. |
|
1740 |
bb.limit(pos + outputSize); |
|
1741 |
} |
|
1742 |
||
1743 |
try { |
|
1744 |
len = cipher.doFinal(dup, bb); |
|
1745 |
} catch (IllegalBlockSizeException | |
|
1746 |
BadPaddingException | ShortBufferException ibse) { |
|
1747 |
// unlikely to happen |
|
1748 |
throw new RuntimeException( |
|
1749 |
"Cipher error in AEAD mode in JCE provider " + |
|
1750 |
cipher.getProvider().getName(), ibse); |
|
1751 |
} |
|
1752 |
||
1753 |
if (len != outputSize) { |
|
1754 |
throw new RuntimeException( |
|
1755 |
"Cipher buffering error in JCE provider " + |
|
1756 |
cipher.getProvider().getName()); |
|
1757 |
} |
|
1758 |
||
1759 |
return len + nonce.length; |
|
1760 |
} |
|
1761 |
||
1762 |
@Override |
|
1763 |
void dispose() { |
|
1764 |
if (cipher != null) { |
|
1765 |
try { |
|
1766 |
cipher.doFinal(); |
|
1767 |
} catch (Exception e) { |
|
1768 |
// swallow all types of exceptions. |
|
1769 |
} |
|
1770 |
} |
|
1771 |
} |
|
1772 |
||
1773 |
@Override |
|
1774 |
int getExplicitNonceSize() { |
|
1775 |
return recordIvSize; |
|
1776 |
} |
|
1777 |
||
1778 |
@Override |
|
1779 |
int calculateFragmentSize(int packetLimit, int headerSize) { |
|
1780 |
return packetLimit - headerSize - recordIvSize - tagSize; |
|
1781 |
} |
|
1782 |
||
1783 |
@Override |
|
1784 |
int calculatePacketSize(int fragmentSize, int headerSize) { |
|
1785 |
return fragmentSize + headerSize + recordIvSize + tagSize; |
|
1786 |
} |
|
1787 |
} |
|
1788 |
} |
|
1789 |
||
1790 |
private static final |
|
1791 |
class T13GcmReadCipherGenerator implements ReadCipherGenerator { |
|
1792 |
||
1793 |
@Override |
|
1794 |
public SSLReadCipher createCipher(SSLCipher sslCipher, |
|
1795 |
Authenticator authenticator, ProtocolVersion protocolVersion, |
|
1796 |
String algorithm, Key key, AlgorithmParameterSpec params, |
|
1797 |
SecureRandom random) throws GeneralSecurityException { |
|
1798 |
return new GcmReadCipher(authenticator, protocolVersion, sslCipher, |
|
1799 |
algorithm, key, params, random); |
|
1800 |
} |
|
1801 |
||
1802 |
static final class GcmReadCipher extends SSLReadCipher { |
|
1803 |
private final Cipher cipher; |
|
1804 |
private final int tagSize; |
|
1805 |
private final Key key; |
|
1806 |
private final byte[] iv; |
|
1807 |
private final SecureRandom random; |
|
1808 |
||
1809 |
GcmReadCipher(Authenticator authenticator, |
|
1810 |
ProtocolVersion protocolVersion, |
|
1811 |
SSLCipher sslCipher, String algorithm, |
|
1812 |
Key key, AlgorithmParameterSpec params, |
|
1813 |
SecureRandom random) throws GeneralSecurityException { |
|
1814 |
super(authenticator, protocolVersion); |
|
1815 |
this.cipher = JsseJce.getCipher(algorithm); |
|
1816 |
this.tagSize = sslCipher.tagSize; |
|
1817 |
this.key = key; |
|
1818 |
this.iv = ((IvParameterSpec)params).getIV(); |
|
1819 |
this.random = random; |
|
1820 |
||
1821 |
keyLimitCountdown = cipherLimits.getOrDefault( |
|
1822 |
algorithm.toUpperCase() + ":" + tag[0], 0L); |
|
1823 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
1824 |
SSLLogger.fine("KeyLimit read side: algorithm = " + |
|
1825 |
algorithm.toUpperCase() + ":" + tag[0] + |
|
1826 |
"\ncountdown value = " + keyLimitCountdown); |
|
1827 |
} |
|
1828 |
if (keyLimitCountdown > 0) { |
|
1829 |
keyLimitEnabled = true; |
|
1830 |
} |
|
1831 |
// DON'T initialize the cipher for AEAD! |
|
1832 |
} |
|
1833 |
||
1834 |
@Override |
|
1835 |
public Plaintext decrypt(byte contentType, ByteBuffer bb, |
|
1836 |
byte[] sequence) throws GeneralSecurityException { |
|
1837 |
// An implementation may receive an unencrypted record of type |
|
1838 |
// change_cipher_spec consisting of the single byte value 0x01 |
|
1839 |
// at any time after the first ClientHello message has been |
|
1840 |
// sent or received and before the peer's Finished message has |
|
1841 |
// been received and MUST simply drop it without further |
|
1842 |
// processing. |
|
1843 |
if (contentType == ContentType.CHANGE_CIPHER_SPEC.id) { |
|
1844 |
return new Plaintext(contentType, |
|
1845 |
ProtocolVersion.NONE.major, ProtocolVersion.NONE.minor, |
|
1846 |
-1, -1L, bb.slice()); |
|
1847 |
} |
|
1848 |
||
1849 |
if (bb.remaining() <= tagSize) { |
|
1850 |
throw new BadPaddingException( |
|
1851 |
"Insufficient buffer remaining for AEAD cipher " + |
|
1852 |
"fragment (" + bb.remaining() + "). Needs to be " + |
|
1853 |
"more than tag size (" + tagSize + ")"); |
|
1854 |
} |
|
1855 |
||
1856 |
byte[] sn = sequence; |
|
1857 |
if (sn == null) { |
|
1858 |
sn = authenticator.sequenceNumber(); |
|
1859 |
} |
|
1860 |
byte[] nonce = iv.clone(); |
|
1861 |
int offset = nonce.length - sn.length; |
|
1862 |
for (int i = 0; i < sn.length; i++) { |
|
1863 |
nonce[offset + i] ^= sn[i]; |
|
1864 |
} |
|
1865 |
||
1866 |
// initialize the AEAD cipher for the unique IV |
|
1867 |
GCMParameterSpec spec = |
|
1868 |
new GCMParameterSpec(tagSize * 8, nonce); |
|
1869 |
try { |
|
1870 |
cipher.init(Cipher.DECRYPT_MODE, key, spec, random); |
|
1871 |
} catch (InvalidKeyException | |
|
1872 |
InvalidAlgorithmParameterException ikae) { |
|
1873 |
// unlikely to happen |
|
1874 |
throw new RuntimeException( |
|
1875 |
"invalid key or spec in GCM mode", ikae); |
|
1876 |
} |
|
1877 |
||
1878 |
// Update the additional authentication data, using the |
|
1879 |
// implicit sequence number of the authenticator. |
|
1880 |
byte[] aad = authenticator.acquireAuthenticationBytes( |
|
1881 |
contentType, bb.remaining(), sn); |
|
1882 |
cipher.updateAAD(aad); |
|
1883 |
||
1884 |
int len, pos = bb.position(); |
|
1885 |
ByteBuffer dup = bb.duplicate(); |
|
1886 |
try { |
|
1887 |
len = cipher.doFinal(dup, bb); |
|
1888 |
} catch (IllegalBlockSizeException ibse) { |
|
1889 |
// unlikely to happen |
|
1890 |
throw new RuntimeException( |
|
1891 |
"Cipher error in AEAD mode \"" + ibse.getMessage() + |
|
1892 |
" \"in JCE provider " + cipher.getProvider().getName()); |
|
1893 |
} catch (ShortBufferException sbe) { |
|
16913 | 1894 |
// catch BouncyCastle buffering error |
50768 | 1895 |
throw new RuntimeException("Cipher buffering error in " + |
1896 |
"JCE provider " + cipher.getProvider().getName(), sbe); |
|
1897 |
} |
|
1898 |
// reset the limit to the end of the decrypted data |
|
1899 |
bb.position(pos); |
|
1900 |
bb.limit(pos + len); |
|
1901 |
||
1902 |
// remove inner plaintext padding |
|
1903 |
int i = bb.limit() - 1; |
|
1904 |
for (; i > 0 && bb.get(i) == 0; i--) { |
|
1905 |
// blank |
|
1906 |
} |
|
1907 |
if (i < (pos + 1)) { |
|
1908 |
throw new BadPaddingException( |
|
1909 |
"Incorrect inner plaintext: no content type"); |
|
1910 |
} |
|
1911 |
contentType = bb.get(i); |
|
1912 |
bb.limit(i); |
|
1913 |
||
1914 |
if (SSLLogger.isOn && SSLLogger.isOn("plaintext")) { |
|
1915 |
SSLLogger.fine( |
|
1916 |
"Plaintext after DECRYPTION", bb.duplicate()); |
|
1917 |
} |
|
1918 |
if (keyLimitEnabled) { |
|
1919 |
keyLimitCountdown -= len; |
|
1920 |
} |
|
1921 |
||
1922 |
return new Plaintext(contentType, |
|
1923 |
ProtocolVersion.NONE.major, ProtocolVersion.NONE.minor, |
|
1924 |
-1, -1L, bb.slice()); |
|
1925 |
} |
|
1926 |
||
1927 |
@Override |
|
1928 |
void dispose() { |
|
1929 |
if (cipher != null) { |
|
1930 |
try { |
|
1931 |
cipher.doFinal(); |
|
1932 |
} catch (Exception e) { |
|
1933 |
// swallow all types of exceptions. |
|
1934 |
} |
|
16913 | 1935 |
} |
2 | 1936 |
} |
1937 |
||
50768 | 1938 |
@Override |
1939 |
int estimateFragmentSize(int packetSize, int headerSize) { |
|
1940 |
return packetSize - headerSize - tagSize; |
|
1941 |
} |
|
1942 |
} |
|
1943 |
} |
|
16913 | 1944 |
|
50768 | 1945 |
private static final |
1946 |
class T13GcmWriteCipherGenerator implements WriteCipherGenerator { |
|
1947 |
@Override |
|
1948 |
public SSLWriteCipher createCipher(SSLCipher sslCipher, |
|
1949 |
Authenticator authenticator, ProtocolVersion protocolVersion, |
|
1950 |
String algorithm, Key key, AlgorithmParameterSpec params, |
|
1951 |
SecureRandom random) throws GeneralSecurityException { |
|
1952 |
return new GcmWriteCipher(authenticator, protocolVersion, sslCipher, |
|
1953 |
algorithm, key, params, random); |
|
1954 |
} |
|
1955 |
||
1956 |
private static final class GcmWriteCipher extends SSLWriteCipher { |
|
1957 |
private final Cipher cipher; |
|
1958 |
private final int tagSize; |
|
1959 |
private final Key key; |
|
1960 |
private final byte[] iv; |
|
1961 |
private final SecureRandom random; |
|
2 | 1962 |
|
50768 | 1963 |
GcmWriteCipher(Authenticator authenticator, |
1964 |
ProtocolVersion protocolVersion, |
|
1965 |
SSLCipher sslCipher, String algorithm, |
|
1966 |
Key key, AlgorithmParameterSpec params, |
|
1967 |
SecureRandom random) throws GeneralSecurityException { |
|
1968 |
super(authenticator, protocolVersion); |
|
1969 |
this.cipher = JsseJce.getCipher(algorithm); |
|
1970 |
this.tagSize = sslCipher.tagSize; |
|
1971 |
this.key = key; |
|
1972 |
this.iv = ((IvParameterSpec)params).getIV(); |
|
1973 |
this.random = random; |
|
2 | 1974 |
|
50768 | 1975 |
keyLimitCountdown = cipherLimits.getOrDefault( |
1976 |
algorithm.toUpperCase() + ":" + tag[0], 0L); |
|
1977 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
1978 |
SSLLogger.fine("KeyLimit write side: algorithm = " |
|
1979 |
+ algorithm.toUpperCase() + ":" + tag[0] + |
|
1980 |
"\ncountdown value = " + keyLimitCountdown); |
|
1981 |
} |
|
1982 |
if (keyLimitCountdown > 0) { |
|
1983 |
keyLimitEnabled = true; |
|
1984 |
} |
|
1985 |
||
1986 |
// DON'T initialize the cipher for AEAD! |
|
2 | 1987 |
} |
1988 |
||
50768 | 1989 |
@Override |
1990 |
public int encrypt(byte contentType, |
|
1991 |
ByteBuffer bb) { |
|
1992 |
byte[] sn = authenticator.sequenceNumber(); |
|
1993 |
byte[] nonce = iv.clone(); |
|
1994 |
int offset = nonce.length - sn.length; |
|
1995 |
for (int i = 0; i < sn.length; i++) { |
|
1996 |
nonce[offset + i] ^= sn[i]; |
|
1997 |
} |
|
1998 |
||
1999 |
// initialize the AEAD cipher for the unique IV |
|
2000 |
GCMParameterSpec spec = |
|
2001 |
new GCMParameterSpec(tagSize * 8, nonce); |
|
2002 |
try { |
|
2003 |
cipher.init(Cipher.ENCRYPT_MODE, key, spec, random); |
|
2004 |
} catch (InvalidKeyException | |
|
2005 |
InvalidAlgorithmParameterException ikae) { |
|
2006 |
// unlikely to happen |
|
2007 |
throw new RuntimeException( |
|
2008 |
"invalid key or spec in GCM mode", ikae); |
|
2009 |
} |
|
2010 |
||
2011 |
// Update the additional authentication data, using the |
|
2012 |
// implicit sequence number of the authenticator. |
|
2013 |
int outputSize = cipher.getOutputSize(bb.remaining()); |
|
2014 |
byte[] aad = authenticator.acquireAuthenticationBytes( |
|
2015 |
contentType, outputSize, sn); |
|
2016 |
cipher.updateAAD(aad); |
|
2017 |
||
2018 |
int len, pos = bb.position(); |
|
2019 |
if (SSLLogger.isOn && SSLLogger.isOn("plaintext")) { |
|
2020 |
SSLLogger.fine( |
|
2021 |
"Plaintext before ENCRYPTION", |
|
2022 |
bb.duplicate()); |
|
2023 |
} |
|
7039 | 2024 |
|
50768 | 2025 |
ByteBuffer dup = bb.duplicate(); |
2026 |
if (outputSize > bb.remaining()) { |
|
2027 |
// Need to expand the limit of the output buffer for |
|
2028 |
// the authentication tag. |
|
2029 |
// |
|
2030 |
// DON'T worry about the buffer's capacity, we have |
|
2031 |
// reserved space for the authentication tag. |
|
2032 |
bb.limit(pos + outputSize); |
|
2033 |
} |
|
2034 |
||
2035 |
try { |
|
2036 |
len = cipher.doFinal(dup, bb); |
|
2037 |
} catch (IllegalBlockSizeException | |
|
2038 |
BadPaddingException | ShortBufferException ibse) { |
|
2039 |
// unlikely to happen |
|
2040 |
throw new RuntimeException( |
|
2041 |
"Cipher error in AEAD mode in JCE provider " + |
|
2042 |
cipher.getProvider().getName(), ibse); |
|
2043 |
} |
|
2044 |
||
2045 |
if (len != outputSize) { |
|
2046 |
throw new RuntimeException( |
|
2047 |
"Cipher buffering error in JCE provider " + |
|
2048 |
cipher.getProvider().getName()); |
|
2049 |
} |
|
2050 |
||
2051 |
if (keyLimitEnabled) { |
|
2052 |
keyLimitCountdown -= len; |
|
2053 |
} |
|
2054 |
return len; |
|
2055 |
} |
|
2056 |
||
2057 |
@Override |
|
2058 |
void dispose() { |
|
2059 |
if (cipher != null) { |
|
2060 |
try { |
|
2061 |
cipher.doFinal(); |
|
2062 |
} catch (Exception e) { |
|
2063 |
// swallow all types of exceptions. |
|
7039 | 2064 |
} |
2065 |
} |
|
2 | 2066 |
} |
50768 | 2067 |
|
2068 |
@Override |
|
2069 |
int getExplicitNonceSize() { |
|
2070 |
return 0; |
|
2071 |
} |
|
2072 |
||
2073 |
@Override |
|
2074 |
int calculateFragmentSize(int packetLimit, int headerSize) { |
|
2075 |
return packetLimit - headerSize - tagSize; |
|
2076 |
} |
|
2077 |
||
2078 |
@Override |
|
2079 |
int calculatePacketSize(int fragmentSize, int headerSize) { |
|
2080 |
return fragmentSize + headerSize + tagSize; |
|
2081 |
} |
|
2 | 2082 |
} |
2083 |
} |
|
2084 |
||
50768 | 2085 |
private static void addMac(MAC signer, |
2086 |
ByteBuffer destination, byte contentType) { |
|
2087 |
if (signer.macAlg().size != 0) { |
|
2088 |
int dstContent = destination.position(); |
|
2089 |
byte[] hash = signer.compute(contentType, destination, false); |
|
2090 |
||
2091 |
/* |
|
2092 |
* position was advanced to limit in MAC compute above. |
|
2093 |
* |
|
2094 |
* Mark next area as writable (above layers should have |
|
2095 |
* established that we have plenty of room), then write |
|
2096 |
* out the hash. |
|
2097 |
*/ |
|
2098 |
destination.limit(destination.limit() + hash.length); |
|
2099 |
destination.put(hash); |
|
2100 |
||
2101 |
// reset the position and limit |
|
2102 |
destination.position(dstContent); |
|
2103 |
} |
|
2104 |
} |
|
2105 |
||
2106 |
// for null and stream cipher |
|
2107 |
private static void checkStreamMac(MAC signer, ByteBuffer bb, |
|
2108 |
byte contentType, byte[] sequence) throws BadPaddingException { |
|
2109 |
int tagLen = signer.macAlg().size; |
|
2110 |
||
2111 |
// Requires message authentication code for null, stream and |
|
2112 |
// block cipher suites. |
|
2113 |
if (tagLen != 0) { |
|
2114 |
int contentLen = bb.remaining() - tagLen; |
|
2115 |
if (contentLen < 0) { |
|
2116 |
throw new BadPaddingException("bad record"); |
|
2117 |
} |
|
2118 |
||
2119 |
// Run MAC computation and comparison on the payload. |
|
2120 |
// |
|
2121 |
// MAC data would be stripped off during the check. |
|
2122 |
if (checkMacTags(contentType, bb, signer, sequence, false)) { |
|
2123 |
throw new BadPaddingException("bad record MAC"); |
|
2124 |
} |
|
2125 |
} |
|
2126 |
} |
|
2 | 2127 |
|
50768 | 2128 |
// for CBC cipher |
2129 |
private static void checkCBCMac(MAC signer, ByteBuffer bb, |
|
2130 |
byte contentType, int cipheredLength, |
|
2131 |
byte[] sequence) throws BadPaddingException { |
|
2132 |
BadPaddingException reservedBPE = null; |
|
2133 |
int tagLen = signer.macAlg().size; |
|
2134 |
int pos = bb.position(); |
|
2135 |
||
2136 |
if (tagLen != 0) { |
|
2137 |
int contentLen = bb.remaining() - tagLen; |
|
2138 |
if (contentLen < 0) { |
|
2139 |
reservedBPE = new BadPaddingException("bad record"); |
|
2140 |
||
2141 |
// set offset of the dummy MAC |
|
2142 |
contentLen = cipheredLength - tagLen; |
|
2143 |
bb.limit(pos + cipheredLength); |
|
2144 |
} |
|
2 | 2145 |
|
50768 | 2146 |
// Run MAC computation and comparison on the payload. |
2147 |
// |
|
2148 |
// MAC data would be stripped off during the check. |
|
2149 |
if (checkMacTags(contentType, bb, signer, sequence, false)) { |
|
2150 |
if (reservedBPE == null) { |
|
2151 |
reservedBPE = |
|
2152 |
new BadPaddingException("bad record MAC"); |
|
2153 |
} |
|
2154 |
} |
|
2155 |
||
2156 |
// Run MAC computation and comparison on the remainder. |
|
2157 |
int remainingLen = calculateRemainingLen( |
|
2158 |
signer, cipheredLength, contentLen); |
|
2159 |
||
2160 |
// NOTE: remainingLen may be bigger (less than 1 block of the |
|
2161 |
// hash algorithm of the MAC) than the cipheredLength. |
|
2162 |
// |
|
2163 |
// Is it possible to use a static buffer, rather than allocate |
|
2164 |
// it dynamically? |
|
2165 |
remainingLen += signer.macAlg().size; |
|
2166 |
ByteBuffer temporary = ByteBuffer.allocate(remainingLen); |
|
2167 |
||
2168 |
// Won't need to worry about the result on the remainder. And |
|
2169 |
// then we won't need to worry about what's actual data to |
|
2170 |
// check MAC tag on. We start the check from the header of the |
|
2171 |
// buffer so that we don't need to construct a new byte buffer. |
|
2172 |
checkMacTags(contentType, temporary, signer, sequence, true); |
|
2 | 2173 |
} |
2174 |
||
50768 | 2175 |
// Is it a failover? |
2176 |
if (reservedBPE != null) { |
|
2177 |
throw reservedBPE; |
|
2 | 2178 |
} |
2179 |
} |
|
2180 |
||
2181 |
/* |
|
50768 | 2182 |
* Run MAC computation and comparison |
2183 |
*/ |
|
2184 |
private static boolean checkMacTags(byte contentType, ByteBuffer bb, |
|
2185 |
MAC signer, byte[] sequence, boolean isSimulated) { |
|
2186 |
int tagLen = signer.macAlg().size; |
|
2187 |
int position = bb.position(); |
|
2188 |
int lim = bb.limit(); |
|
2189 |
int macOffset = lim - tagLen; |
|
2190 |
||
2191 |
bb.limit(macOffset); |
|
2192 |
byte[] hash = signer.compute(contentType, bb, sequence, isSimulated); |
|
2193 |
if (hash == null || tagLen != hash.length) { |
|
2194 |
// Something is wrong with MAC implementation. |
|
2195 |
throw new RuntimeException("Internal MAC error"); |
|
2196 |
} |
|
2197 |
||
2198 |
bb.position(macOffset); |
|
2199 |
bb.limit(lim); |
|
2200 |
try { |
|
2201 |
int[] results = compareMacTags(bb, hash); |
|
2202 |
return (results[0] != 0); |
|
2203 |
} finally { |
|
2204 |
// reset to the data |
|
2205 |
bb.position(position); |
|
2206 |
bb.limit(macOffset); |
|
2207 |
} |
|
2208 |
} |
|
2209 |
||
2210 |
/* |
|
2211 |
* A constant-time comparison of the MAC tags. |
|
2 | 2212 |
* |
50768 | 2213 |
* Please DON'T change the content of the ByteBuffer parameter! |
2 | 2214 |
*/ |
50768 | 2215 |
private static int[] compareMacTags(ByteBuffer bb, byte[] tag) { |
2216 |
// An array of hits is used to prevent Hotspot optimization for |
|
2217 |
// the purpose of a constant-time check. |
|
2218 |
int[] results = {0, 0}; // {missed #, matched #} |
|
2219 |
||
2220 |
// The caller ensures there are enough bytes available in the buffer. |
|
2221 |
// So we won't need to check the remaining of the buffer. |
|
2222 |
for (byte t : tag) { |
|
2223 |
if (bb.get() != t) { |
|
2224 |
results[0]++; // mismatched bytes |
|
2225 |
} else { |
|
2226 |
results[1]++; // matched bytes |
|
2227 |
} |
|
2228 |
} |
|
2229 |
||
2230 |
return results; |
|
2231 |
} |
|
2232 |
||
2233 |
/* |
|
2234 |
* Calculate the length of a dummy buffer to run MAC computation |
|
2235 |
* and comparison on the remainder. |
|
2236 |
* |
|
2237 |
* The caller MUST ensure that the fullLen is not less than usedLen. |
|
2238 |
*/ |
|
2239 |
private static int calculateRemainingLen( |
|
2240 |
MAC signer, int fullLen, int usedLen) { |
|
2241 |
||
2242 |
int blockLen = signer.macAlg().hashBlockSize; |
|
2243 |
int minimalPaddingLen = signer.macAlg().minimalPaddingSize; |
|
2244 |
||
2245 |
// (blockLen - minimalPaddingLen) is the maximum message size of |
|
2246 |
// the last block of hash function operation. See FIPS 180-4, or |
|
2247 |
// MD5 specification. |
|
2248 |
fullLen += 13 - (blockLen - minimalPaddingLen); |
|
2249 |
usedLen += 13 - (blockLen - minimalPaddingLen); |
|
2250 |
||
2251 |
// Note: fullLen is always not less than usedLen, and blockLen |
|
2252 |
// is always bigger than minimalPaddingLen, so we don't worry |
|
2253 |
// about negative values. 0x01 is added to the result to ensure |
|
2254 |
// that the return value is positive. The extra one byte does |
|
2255 |
// not impact the overall MAC compression function evaluations. |
|
2256 |
return 0x01 + (int)(Math.ceil(fullLen/(1.0d * blockLen)) - |
|
2257 |
Math.ceil(usedLen/(1.0d * blockLen))) * blockLen; |
|
2258 |
} |
|
2259 |
||
2 | 2260 |
private static int addPadding(ByteBuffer bb, int blockSize) { |
2261 |
||
2262 |
int len = bb.remaining(); |
|
2263 |
int offset = bb.position(); |
|
2264 |
||
2265 |
int newlen = len + 1; |
|
2266 |
byte pad; |
|
2267 |
int i; |
|
2268 |
||
2269 |
if ((newlen % blockSize) != 0) { |
|
2270 |
newlen += blockSize - 1; |
|
2271 |
newlen -= newlen % blockSize; |
|
2272 |
} |
|
2273 |
pad = (byte) (newlen - len); |
|
2274 |
||
2275 |
/* |
|
2276 |
* Update the limit to what will be padded. |
|
2277 |
*/ |
|
2278 |
bb.limit(newlen + offset); |
|
2279 |
||
2280 |
/* |
|
2281 |
* TLS version of the padding works for both SSLv3 and TLSv1 |
|
2282 |
*/ |
|
2283 |
for (i = 0, offset += len; i < pad; i++) { |
|
2284 |
bb.put(offset++, (byte) (pad - 1)); |
|
2285 |
} |
|
2286 |
||
2287 |
bb.position(offset); |
|
2288 |
bb.limit(offset); |
|
2289 |
||
2290 |
return newlen; |
|
2291 |
} |
|
2292 |
||
2293 |
private static int removePadding(ByteBuffer bb, |
|
16113 | 2294 |
int tagLen, int blockSize, |
2295 |
ProtocolVersion protocolVersion) throws BadPaddingException { |
|
2 | 2296 |
int len = bb.remaining(); |
2297 |
int offset = bb.position(); |
|
2298 |
||
2299 |
// last byte is length byte (i.e. actual padding length - 1) |
|
2300 |
int padOffset = offset + len - 1; |
|
16113 | 2301 |
int padLen = bb.get(padOffset) & 0xFF; |
2 | 2302 |
|
16113 | 2303 |
int newLen = len - (padLen + 1); |
2304 |
if ((newLen - tagLen) < 0) { |
|
2305 |
// If the buffer is not long enough to contain the padding plus |
|
2306 |
// a MAC tag, do a dummy constant-time padding check. |
|
2307 |
// |
|
2308 |
// Note that it is a dummy check, so we won't care about what is |
|
2309 |
// the actual padding data. |
|
2310 |
checkPadding(bb.duplicate(), (byte)(padLen & 0xFF)); |
|
2311 |
||
2312 |
throw new BadPaddingException("Invalid Padding length: " + padLen); |
|
2 | 2313 |
} |
2314 |
||
16113 | 2315 |
// The padding data should be filled with the padding length value. |
2316 |
int[] results = checkPadding( |
|
27292
7ff4b24b33ce
4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents:
25859
diff
changeset
|
2317 |
bb.duplicate().position(offset + newLen), |
16113 | 2318 |
(byte)(padLen & 0xFF)); |
30904 | 2319 |
if (protocolVersion.useTLS10PlusSpec()) { |
16113 | 2320 |
if (results[0] != 0) { // padding data has invalid bytes |
2321 |
throw new BadPaddingException("Invalid TLS padding data"); |
|
2 | 2322 |
} |
2323 |
} else { // SSLv3 |
|
2324 |
// SSLv3 requires 0 <= length byte < block size |
|
2325 |
// some implementations do 1 <= length byte <= block size, |
|
2326 |
// so accept that as well |
|
2327 |
// v3 does not require any particular value for the other bytes |
|
16113 | 2328 |
if (padLen > blockSize) { |
40544
807dd9a425db
8150530: Improve javax.crypto.BadPaddingException messages
coffeys
parents:
34826
diff
changeset
|
2329 |
throw new BadPaddingException("Padding length (" + |
807dd9a425db
8150530: Improve javax.crypto.BadPaddingException messages
coffeys
parents:
34826
diff
changeset
|
2330 |
padLen + ") of SSLv3 message should not be bigger " + |
807dd9a425db
8150530: Improve javax.crypto.BadPaddingException messages
coffeys
parents:
34826
diff
changeset
|
2331 |
"than the block size (" + blockSize + ")"); |
2 | 2332 |
} |
2333 |
} |
|
2334 |
||
50768 | 2335 |
// Reset buffer limit to remove padding. |
16113 | 2336 |
bb.limit(offset + newLen); |
2 | 2337 |
|
16113 | 2338 |
return newLen; |
2 | 2339 |
} |
1763
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
2340 |
|
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
2341 |
/* |
50768 | 2342 |
* A constant-time check of the padding. |
10915 | 2343 |
* |
50768 | 2344 |
* NOTE that we are checking both the padding and the padLen bytes here. |
16913 | 2345 |
* |
50768 | 2346 |
* The caller MUST ensure that the bb parameter has remaining. |
16913 | 2347 |
*/ |
50768 | 2348 |
private static int[] checkPadding(ByteBuffer bb, byte pad) { |
2349 |
if (!bb.hasRemaining()) { |
|
2350 |
throw new RuntimeException("hasRemaining() must be positive"); |
|
16913 | 2351 |
} |
2352 |
||
50768 | 2353 |
// An array of hits is used to prevent Hotspot optimization for |
2354 |
// the purpose of a constant-time check. |
|
2355 |
int[] results = {0, 0}; // {missed #, matched #} |
|
2356 |
bb.mark(); |
|
2357 |
for (int i = 0; i <= 256; bb.reset()) { |
|
2358 |
for (; bb.hasRemaining() && i <= 256; i++) { |
|
2359 |
if (bb.get() != pad) { |
|
2360 |
results[0]++; // mismatched padding data |
|
2361 |
} else { |
|
2362 |
results[1]++; // matched padding data |
|
16913 | 2363 |
} |
30904 | 2364 |
} |
2365 |
} |
|
2366 |
||
50768 | 2367 |
return results; |
30904 | 2368 |
} |
50768 | 2369 |
} |
30904 | 2370 |