Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/ssl/ChangeCipherSpec.java
41159 views
1
/*
2
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.security.ssl;
27
28
import java.io.IOException;
29
import java.nio.ByteBuffer;
30
import java.security.GeneralSecurityException;
31
import java.security.InvalidKeyException;
32
import java.security.NoSuchAlgorithmException;
33
import javax.crypto.SecretKey;
34
import javax.crypto.spec.IvParameterSpec;
35
import javax.net.ssl.SSLException;
36
import sun.security.ssl.SSLCipher.SSLReadCipher;
37
import sun.security.ssl.SSLCipher.SSLWriteCipher;
38
import sun.security.ssl.SSLHandshake.HandshakeMessage;
39
import sun.security.ssl.SSLTrafficKeyDerivation.LegacyTrafficKeyDerivation;
40
41
/**
42
* Pack of the ChangeCipherSpec message.
43
*/
44
final class ChangeCipherSpec {
45
static final SSLConsumer t10Consumer =
46
new T10ChangeCipherSpecConsumer();
47
static final HandshakeProducer t10Producer =
48
new T10ChangeCipherSpecProducer();
49
static final SSLConsumer t13Consumer =
50
new T13ChangeCipherSpecConsumer();
51
52
/**
53
* The "ChangeCipherSpec" message producer.
54
*/
55
private static final
56
class T10ChangeCipherSpecProducer implements HandshakeProducer {
57
// Prevent instantiation of this class.
58
private T10ChangeCipherSpecProducer() {
59
// blank
60
}
61
62
@Override
63
public byte[] produce(ConnectionContext context,
64
HandshakeMessage message) throws IOException {
65
HandshakeContext hc = (HandshakeContext)context;
66
SSLKeyDerivation kd = hc.handshakeKeyDerivation;
67
68
if (!(kd instanceof LegacyTrafficKeyDerivation)) {
69
throw new UnsupportedOperationException("Not supported.");
70
}
71
LegacyTrafficKeyDerivation tkd = (LegacyTrafficKeyDerivation)kd;
72
CipherSuite ncs = hc.negotiatedCipherSuite;
73
Authenticator writeAuthenticator;
74
if (ncs.bulkCipher.cipherType == CipherType.AEAD_CIPHER) {
75
writeAuthenticator =
76
Authenticator.valueOf(hc.negotiatedProtocol);
77
} else {
78
try {
79
writeAuthenticator = Authenticator.valueOf(
80
hc.negotiatedProtocol, ncs.macAlg,
81
tkd.getTrafficKey(hc.sslConfig.isClientMode ?
82
"clientMacKey" : "serverMacKey"));
83
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
84
// unlikely
85
throw new SSLException("Algorithm missing: ", e);
86
}
87
}
88
89
SecretKey writeKey =
90
tkd.getTrafficKey(hc.sslConfig.isClientMode ?
91
"clientWriteKey" : "serverWriteKey");
92
SecretKey writeIv =
93
tkd.getTrafficKey(hc.sslConfig.isClientMode ?
94
"clientWriteIv" : "serverWriteIv");
95
IvParameterSpec iv = (writeIv == null) ? null :
96
new IvParameterSpec(writeIv.getEncoded());
97
SSLWriteCipher writeCipher;
98
try {
99
writeCipher = ncs.bulkCipher.createWriteCipher(
100
writeAuthenticator,
101
hc.negotiatedProtocol, writeKey, iv,
102
hc.sslContext.getSecureRandom());
103
} catch (GeneralSecurityException gse) {
104
// unlikely
105
throw new SSLException("Algorithm missing: ", gse);
106
}
107
108
if (writeCipher == null) {
109
throw hc.conContext.fatal(Alert.ILLEGAL_PARAMETER,
110
"Illegal cipher suite (" + ncs +
111
") and protocol version (" + hc.negotiatedProtocol + ")");
112
}
113
114
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
115
SSLLogger.fine("Produced ChangeCipherSpec message");
116
}
117
118
hc.conContext.outputRecord.changeWriteCiphers(writeCipher, true);
119
120
// The handshake message has been delivered.
121
return null;
122
}
123
}
124
125
/**
126
* The "ChangeCipherSpec" message producer.
127
*/
128
private static final
129
class T10ChangeCipherSpecConsumer implements SSLConsumer {
130
// Prevent instantiation of this class.
131
private T10ChangeCipherSpecConsumer() {
132
// blank
133
}
134
135
@Override
136
public void consume(ConnectionContext context,
137
ByteBuffer message) throws IOException {
138
TransportContext tc = (TransportContext)context;
139
140
// This consumer can be used only once.
141
tc.consumers.remove(ContentType.CHANGE_CIPHER_SPEC.id);
142
143
// parse
144
if (message.remaining() != 1 || message.get() != 1) {
145
throw tc.fatal(Alert.UNEXPECTED_MESSAGE,
146
"Malformed or unexpected ChangeCipherSpec message");
147
}
148
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
149
SSLLogger.fine("Consuming ChangeCipherSpec message");
150
}
151
152
// validate
153
if (tc.handshakeContext == null) {
154
throw tc.fatal(Alert.HANDSHAKE_FAILURE,
155
"Unexpected ChangeCipherSpec message");
156
}
157
158
159
HandshakeContext hc = tc.handshakeContext;
160
161
if (hc.handshakeKeyDerivation == null) {
162
throw tc.fatal(Alert.UNEXPECTED_MESSAGE,
163
"Unexpected ChangeCipherSpec message");
164
}
165
166
SSLKeyDerivation kd = hc.handshakeKeyDerivation;
167
if (kd instanceof LegacyTrafficKeyDerivation) {
168
LegacyTrafficKeyDerivation tkd = (LegacyTrafficKeyDerivation)kd;
169
CipherSuite ncs = hc.negotiatedCipherSuite;
170
Authenticator readAuthenticator;
171
if (ncs.bulkCipher.cipherType == CipherType.AEAD_CIPHER) {
172
readAuthenticator =
173
Authenticator.valueOf(hc.negotiatedProtocol);
174
} else {
175
try {
176
readAuthenticator = Authenticator.valueOf(
177
hc.negotiatedProtocol, ncs.macAlg,
178
tkd.getTrafficKey(hc.sslConfig.isClientMode ?
179
"serverMacKey" : "clientMacKey"));
180
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
181
// unlikely
182
throw new SSLException("Algorithm missing: ", e);
183
}
184
}
185
186
SecretKey readKey =
187
tkd.getTrafficKey(hc.sslConfig.isClientMode ?
188
"serverWriteKey" : "clientWriteKey");
189
SecretKey readIv =
190
tkd.getTrafficKey(hc.sslConfig.isClientMode ?
191
"serverWriteIv" : "clientWriteIv");
192
IvParameterSpec iv = (readIv == null) ? null :
193
new IvParameterSpec(readIv.getEncoded());
194
SSLReadCipher readCipher;
195
try {
196
readCipher = ncs.bulkCipher.createReadCipher(
197
readAuthenticator,
198
hc.negotiatedProtocol, readKey, iv,
199
hc.sslContext.getSecureRandom());
200
} catch (GeneralSecurityException gse) {
201
// unlikely
202
throw new SSLException("Algorithm missing: ", gse);
203
}
204
205
if (readCipher == null) {
206
throw hc.conContext.fatal(Alert.ILLEGAL_PARAMETER,
207
"Illegal cipher suite (" + hc.negotiatedCipherSuite +
208
") and protocol version (" + hc.negotiatedProtocol +
209
")");
210
}
211
212
tc.inputRecord.changeReadCiphers(readCipher);
213
} else {
214
throw new UnsupportedOperationException("Not supported.");
215
}
216
}
217
}
218
219
private static final
220
class T13ChangeCipherSpecConsumer implements SSLConsumer {
221
// Prevent instantiation of this class.
222
private T13ChangeCipherSpecConsumer() {
223
// blank
224
}
225
226
// An implementation may receive an unencrypted record of type
227
// change_cipher_spec consisting of the single byte value 0x01
228
// at any time after the first ClientHello message has been
229
// sent or received and before the peer's Finished message has
230
// been received and MUST simply drop it without further
231
// processing.
232
@Override
233
public void consume(ConnectionContext context,
234
ByteBuffer message) throws IOException {
235
TransportContext tc = (TransportContext)context;
236
237
// This consumer can be used only once.
238
tc.consumers.remove(ContentType.CHANGE_CIPHER_SPEC.id);
239
240
// parse
241
if (message.remaining() != 1 || message.get() != 1) {
242
throw tc.fatal(Alert.UNEXPECTED_MESSAGE,
243
"Malformed or unexpected ChangeCipherSpec message");
244
}
245
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
246
SSLLogger.fine("Consuming ChangeCipherSpec message");
247
}
248
249
// no further processing
250
}
251
}
252
}
253
254