forked from openjdk/jdk25u-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSSLTrafficKeyDerivation.java
More file actions
331 lines (292 loc) · 12.9 KB
/
Copy pathSSLTrafficKeyDerivation.java
File metadata and controls
331 lines (292 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.security.ssl;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.security.GeneralSecurityException;
import java.security.ProviderException;
import javax.crypto.KDF;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.HKDFParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.net.ssl.SSLHandshakeException;
import sun.security.internal.spec.TlsKeyMaterialParameterSpec;
import sun.security.internal.spec.TlsKeyMaterialSpec;
import sun.security.ssl.CipherSuite.HashAlg;
import static sun.security.ssl.CipherSuite.HashAlg.H_NONE;
enum SSLTrafficKeyDerivation implements SSLKeyDerivationGenerator {
SSL30 ("kdf_ssl30", new S30TrafficKeyDerivationGenerator()),
TLS10 ("kdf_tls10", new T10TrafficKeyDerivationGenerator()),
TLS12 ("kdf_tls12", new T12TrafficKeyDerivationGenerator()),
TLS13 ("kdf_tls13", new T13TrafficKeyDerivationGenerator());
final String name;
final SSLKeyDerivationGenerator keyDerivationGenerator;
SSLTrafficKeyDerivation(String name,
SSLKeyDerivationGenerator keyDerivationGenerator) {
this.name = name;
this.keyDerivationGenerator = keyDerivationGenerator;
}
static SSLTrafficKeyDerivation valueOf(ProtocolVersion protocolVersion) {
switch (protocolVersion) {
case SSL30:
return SSLTrafficKeyDerivation.SSL30;
case TLS10:
case TLS11:
case DTLS10:
return SSLTrafficKeyDerivation.TLS10;
case TLS12:
case DTLS12:
return SSLTrafficKeyDerivation.TLS12;
case TLS13:
return SSLTrafficKeyDerivation.TLS13;
}
return null;
}
@Override
public SSLKeyDerivation createKeyDerivation(HandshakeContext context,
SecretKey secretKey) throws IOException {
return keyDerivationGenerator.createKeyDerivation(context, secretKey);
}
private static final class S30TrafficKeyDerivationGenerator
implements SSLKeyDerivationGenerator {
private S30TrafficKeyDerivationGenerator() {
// blank
}
@Override
public SSLKeyDerivation createKeyDerivation(
HandshakeContext context, SecretKey secretKey) throws IOException {
return new LegacyTrafficKeyDerivation(context, secretKey);
}
}
private static final class T10TrafficKeyDerivationGenerator
implements SSLKeyDerivationGenerator {
private T10TrafficKeyDerivationGenerator() {
// blank
}
@Override
public SSLKeyDerivation createKeyDerivation(
HandshakeContext context, SecretKey secretKey) throws IOException {
return new LegacyTrafficKeyDerivation(context, secretKey);
}
}
private static final class T12TrafficKeyDerivationGenerator
implements SSLKeyDerivationGenerator {
private T12TrafficKeyDerivationGenerator() {
// blank
}
@Override
public SSLKeyDerivation createKeyDerivation(
HandshakeContext context, SecretKey secretKey) throws IOException {
return new LegacyTrafficKeyDerivation(context, secretKey);
}
}
private static final class T13TrafficKeyDerivationGenerator
implements SSLKeyDerivationGenerator {
private T13TrafficKeyDerivationGenerator() {
// blank
}
@Override
public SSLKeyDerivation createKeyDerivation(
HandshakeContext context,
SecretKey secretKey) throws IOException {
return new T13TrafficKeyDerivation(context, secretKey);
}
}
static final class T13TrafficKeyDerivation implements SSLKeyDerivation {
private final CipherSuite cs;
private final SecretKey secret;
T13TrafficKeyDerivation(
HandshakeContext context, SecretKey secret) {
this.secret = secret;
this.cs = context.negotiatedCipherSuite;
}
@Override
public SecretKey deriveKey(String type) throws IOException {
KeySchedule ks = KeySchedule.valueOf(type);
try {
KDF hkdf = KDF.getInstance(cs.hashAlg.hkdfAlgorithm);
byte[] hkdfInfo = createHkdfInfo(ks.label, ks.getKeyLength(cs));
HKDFParameterSpec spec = HKDFParameterSpec.expandOnly(secret,
hkdfInfo, ks.getKeyLength(cs));
return hkdf.deriveKey(ks.getAlgorithm(cs, type), spec);
} catch (GeneralSecurityException gse) {
throw new SSLHandshakeException(
"Could not generate secret", gse);
}
}
@Override
public byte[] deriveData(String type) throws IOException {
KeySchedule ks = KeySchedule.valueOf(type);
try {
KDF hkdf = KDF.getInstance(cs.hashAlg.hkdfAlgorithm);
byte[] hkdfInfo = createHkdfInfo(ks.label, ks.getKeyLength(cs));
HKDFParameterSpec spec = HKDFParameterSpec.expandOnly(secret,
hkdfInfo, ks.getKeyLength(cs));
return hkdf.deriveData(spec);
} catch (GeneralSecurityException gse) {
throw new SSLHandshakeException(
"Could not generate secret", gse);
}
}
private static byte[] createHkdfInfo(
byte[] label, int length) {
byte[] info = new byte[4 + label.length];
ByteBuffer m = ByteBuffer.wrap(info);
try {
Record.putInt16(m, length);
Record.putBytes8(m, label);
Record.putInt8(m, 0x00); // zero-length context
} catch (IOException ioe) {
// unlikely
throw new RuntimeException("Unexpected exception", ioe);
}
return info;
}
}
private enum KeySchedule {
// Note that we use enum name as the key name.
TlsKey ("key"),
TlsIv ("iv"),
TlsUpdateNplus1 ("traffic upd");
private final byte[] label;
KeySchedule(String label) {
this.label = ("tls13 " + label).getBytes();
}
int getKeyLength(CipherSuite cs) {
return switch (this) {
case TlsUpdateNplus1 -> cs.hashAlg.hashLength;
case TlsIv -> cs.bulkCipher.ivSize;
case TlsKey -> cs.bulkCipher.keySize;
};
}
String getAlgorithm(CipherSuite cs, String algorithm) {
return this == TlsKey ? cs.bulkCipher.algorithm : algorithm;
}
}
@SuppressWarnings("deprecation")
static final class LegacyTrafficKeyDerivation implements SSLKeyDerivation {
private final TlsKeyMaterialSpec keyMaterialSpec;
LegacyTrafficKeyDerivation(
HandshakeContext context, SecretKey masterSecret) {
CipherSuite cipherSuite = context.negotiatedCipherSuite;
ProtocolVersion protocolVersion = context.negotiatedProtocol;
/*
* For both the read and write sides of the protocol, we use the
* master to generate MAC secrets and cipher keying material. Block
* ciphers need initialization vectors, which we also generate.
*
* First we figure out how much keying material is needed.
*/
int hashSize = cipherSuite.macAlg.size;
boolean is_exportable = cipherSuite.exportable;
SSLCipher cipher = cipherSuite.bulkCipher;
int expandedKeySize = is_exportable ? cipher.expandedKeySize : 0;
// Which algs/params do we need to use?
String keyMaterialAlg;
HashAlg hashAlg;
byte majorVersion = protocolVersion.major;
byte minorVersion = protocolVersion.minor;
if (protocolVersion.isDTLS) {
// Use TLS version number for DTLS key calculation
if (protocolVersion.id == ProtocolVersion.DTLS10.id) {
majorVersion = ProtocolVersion.TLS11.major;
minorVersion = ProtocolVersion.TLS11.minor;
keyMaterialAlg = "SunTlsKeyMaterial";
hashAlg = H_NONE;
} else { // DTLS 1.2+
majorVersion = ProtocolVersion.TLS12.major;
minorVersion = ProtocolVersion.TLS12.minor;
keyMaterialAlg = "SunTls12KeyMaterial";
hashAlg = cipherSuite.hashAlg;
}
} else {
if (protocolVersion.id >= ProtocolVersion.TLS12.id) {
keyMaterialAlg = "SunTls12KeyMaterial";
hashAlg = cipherSuite.hashAlg;
} else {
keyMaterialAlg = "SunTlsKeyMaterial";
hashAlg = H_NONE;
}
}
// TLS v1.1+ and DTLS use an explicit IV in CBC cipher suites to
// protect against the CBC attacks. AEAD/GCM cipher suites in
// TLS v1.2 or later use a fixed IV as the implicit part of the
// partially implicit nonce technique described in RFC 5116.
int ivSize = cipher.ivSize;
if (cipher.cipherType == CipherType.AEAD_CIPHER) {
ivSize = cipher.fixedIvSize;
} else if (
cipher.cipherType == CipherType.BLOCK_CIPHER &&
protocolVersion.useTLS11PlusSpec()) {
ivSize = 0;
}
TlsKeyMaterialParameterSpec spec = new TlsKeyMaterialParameterSpec(
masterSecret, (majorVersion & 0xFF), (minorVersion & 0xFF),
context.clientHelloRandom.randomBytes,
context.serverHelloRandom.randomBytes,
cipher.algorithm, cipher.keySize, expandedKeySize,
ivSize, hashSize,
hashAlg.name, hashAlg.hashLength, hashAlg.blockSize);
try {
KeyGenerator kg = KeyGenerator.getInstance(keyMaterialAlg);
kg.init(spec);
this.keyMaterialSpec = (TlsKeyMaterialSpec)kg.generateKey();
} catch (GeneralSecurityException e) {
throw new ProviderException(e);
}
}
@Override
public SecretKey deriveKey(String type) throws IOException {
switch (type) {
case "clientMacKey":
return keyMaterialSpec.getClientMacKey();
case "serverMacKey":
return keyMaterialSpec.getServerMacKey();
case "clientWriteKey":
return keyMaterialSpec.getClientCipherKey();
case "serverWriteKey":
return keyMaterialSpec.getServerCipherKey();
default:
throw new SSLHandshakeException(
"Cannot deriveKey for " + type);
}
}
@Override
public byte[] deriveData(String type) throws IOException {
switch (type) {
case "clientWriteIv":
IvParameterSpec cliIvSpec = keyMaterialSpec.getClientIv();
return (cliIvSpec == null) ? null : cliIvSpec.getIV();
case "serverWriteIv":
IvParameterSpec srvIvSpec = keyMaterialSpec.getServerIv();
return (srvIvSpec == null) ? null : srvIvSpec.getIV();
default:
throw new SSLHandshakeException(
"Cannot deriveData for " + type);
}
}
}
}