author | ascarpino |
Sun, 13 May 2018 08:52:25 -0700 | |
branch | JDK-8145252-TLS13-branch |
changeset 56544 | ad120e0dfcfb |
parent 56542 | 56aaa6cb3693 |
child 56660 | 66c803c3ce32 |
permissions | -rw-r--r-- |
56542 | 1 |
/* |
2 |
* Copyright (c) 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 |
||
32 |
import sun.security.ssl.SSLHandshake.HandshakeMessage; |
|
33 |
import sun.security.ssl.SSLCipher.SSLReadCipher; |
|
34 |
import sun.security.ssl.SSLCipher.SSLWriteCipher; |
|
35 |
||
36 |
import javax.crypto.SecretKey; |
|
37 |
import javax.crypto.spec.IvParameterSpec; |
|
38 |
||
39 |
/** |
|
40 |
* Pack of the KeyUpdate handshake message. |
|
41 |
*/ |
|
42 |
final class KeyUpdate { |
|
43 |
static final SSLProducer kickstartProducer = |
|
44 |
new KeyUpdateKickstartProducer(); |
|
45 |
||
46 |
static final SSLConsumer handshakeConsumer = |
|
47 |
new KeyUpdateConsumer(); |
|
48 |
static final HandshakeProducer handshakeProducer = |
|
49 |
new KeyUpdateProducer(); |
|
50 |
||
51 |
/** |
|
52 |
* The KeyUpdate handshake message. |
|
53 |
* |
|
54 |
* The KeyUpdate handshake message is used to indicate that the sender is |
|
55 |
* updating its sending cryptographic keys. |
|
56 |
* |
|
57 |
* enum { |
|
58 |
* update_not_requested(0), update_requested(1), (255) |
|
59 |
* } KeyUpdateRequest; |
|
60 |
* |
|
61 |
* struct { |
|
62 |
* KeyUpdateRequest request_update; |
|
63 |
* } KeyUpdate; |
|
64 |
*/ |
|
65 |
static final class KeyUpdateMessage extends HandshakeMessage { |
|
66 |
static final byte NOTREQUSTED = 0; |
|
67 |
static final byte REQUSTED = 1; |
|
68 |
private byte status; |
|
69 |
||
56544
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
70 |
KeyUpdateMessage(PostHandshakeContext context, byte status) { |
56542 | 71 |
super(context); |
72 |
this.status = status; |
|
73 |
if (status > 1) { |
|
74 |
new IOException("KeyUpdate message value invalid: " + status); |
|
75 |
} |
|
76 |
} |
|
56544
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
77 |
KeyUpdateMessage(PostHandshakeContext context, ByteBuffer m) |
56542 | 78 |
throws IOException{ |
79 |
super(context); |
|
80 |
||
81 |
if (m.remaining() != 1) { |
|
82 |
throw new IOException("KeyUpdate has an unexpected length of "+ |
|
83 |
m.remaining()); |
|
84 |
} |
|
85 |
||
86 |
status = m.get(); |
|
87 |
if (status > 1) { |
|
88 |
new IOException("KeyUpdate message value invalid: " + status); |
|
89 |
} |
|
90 |
} |
|
91 |
||
92 |
@Override |
|
93 |
public SSLHandshake handshakeType() { |
|
94 |
return SSLHandshake.KEY_UPDATE; |
|
95 |
} |
|
96 |
||
97 |
@Override |
|
98 |
public int messageLength() { |
|
99 |
// one byte enum |
|
100 |
return 1; |
|
101 |
} |
|
102 |
||
103 |
@Override |
|
104 |
public void send(HandshakeOutStream s) throws IOException { |
|
105 |
s.write(status); |
|
106 |
} |
|
107 |
||
108 |
@Override |
|
109 |
public String toString() { |
|
110 |
throw new UnsupportedOperationException("Not supported yet."); |
|
111 |
} |
|
112 |
||
113 |
byte getStatus() { |
|
114 |
return status; |
|
115 |
} |
|
116 |
} |
|
117 |
||
118 |
private static final |
|
119 |
class KeyUpdateKickstartProducer implements SSLProducer { |
|
120 |
// Prevent instantiation of this class. |
|
121 |
private KeyUpdateKickstartProducer() { |
|
122 |
// blank |
|
123 |
} |
|
124 |
||
125 |
// Produce kickstart handshake message. |
|
126 |
@Override |
|
127 |
public byte[] produce(ConnectionContext context) throws IOException { |
|
56544
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
128 |
PostHandshakeContext hc = (PostHandshakeContext)context; |
56542 | 129 |
handshakeProducer.produce(hc, |
130 |
new KeyUpdateMessage(hc, KeyUpdateMessage.REQUSTED)); |
|
131 |
return null; |
|
132 |
} |
|
133 |
} |
|
134 |
||
135 |
/** |
|
136 |
* The "KeyUpdate" handshake message consumer. |
|
137 |
*/ |
|
138 |
private static final class KeyUpdateConsumer implements SSLConsumer { |
|
139 |
// Prevent instantiation of this class. |
|
140 |
private KeyUpdateConsumer() { |
|
141 |
// blank |
|
142 |
} |
|
143 |
||
144 |
@Override |
|
145 |
public void consume(ConnectionContext context, |
|
146 |
ByteBuffer message) throws IOException { |
|
147 |
// The consuming happens in client side only. |
|
56544
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
148 |
PostHandshakeContext hc = (PostHandshakeContext)context; |
56542 | 149 |
KeyUpdateMessage km = new KeyUpdateMessage(hc, message); |
150 |
||
151 |
if (km.getStatus() == KeyUpdateMessage.NOTREQUSTED) { |
|
152 |
return; |
|
153 |
} |
|
154 |
||
155 |
SSLTrafficKeyDerivation kdg = |
|
156 |
SSLTrafficKeyDerivation.valueOf(hc.conContext.protocolVersion); |
|
157 |
if (kdg == null) { |
|
158 |
// unlikely |
|
159 |
hc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
160 |
"Not supported key derivation: " + |
|
161 |
hc.conContext.protocolVersion); |
|
162 |
return; |
|
163 |
} |
|
164 |
||
165 |
SSLKeyDerivation skd = kdg.createKeyDerivation(hc, |
|
166 |
hc.conContext.inputRecord.readCipher.baseSecret); |
|
167 |
SecretKey nplus1 = skd.deriveKey("TlsUpdateNplus1", null); |
|
168 |
if (skd == null) { |
|
169 |
// unlikely |
|
170 |
hc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
171 |
"no key derivation"); |
|
172 |
return; |
|
173 |
} |
|
174 |
||
175 |
SSLKeyDerivation kd = kdg.createKeyDerivation(hc, nplus1); |
|
176 |
SecretKey key = kd.deriveKey("TlsKey", null); |
|
177 |
IvParameterSpec ivSpec = |
|
178 |
new IvParameterSpec(kd.deriveKey("TlsIv", null) |
|
179 |
.getEncoded()); |
|
180 |
try { |
|
181 |
SSLReadCipher rc = |
|
182 |
hc.negotiatedCipherSuite.bulkCipher.createReadCipher( |
|
183 |
Authenticator.valueOf(hc.conContext.protocolVersion), |
|
184 |
hc.conContext.protocolVersion, key, ivSpec, |
|
185 |
hc.sslContext.getSecureRandom()); |
|
186 |
hc.conContext.inputRecord.changeReadCiphers(rc); |
|
187 |
hc.conContext.inputRecord.readCipher.baseSecret = nplus1; |
|
188 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
189 |
SSLLogger.fine("KeyUpdate: read key updated"); |
|
190 |
} |
|
191 |
||
192 |
} catch (GeneralSecurityException e) { |
|
193 |
throw new IOException(e); |
|
194 |
} |
|
195 |
||
196 |
// Send Reply |
|
197 |
handshakeProducer.produce(hc, |
|
198 |
new KeyUpdateMessage(hc, KeyUpdateMessage.NOTREQUSTED)); |
|
199 |
} |
|
200 |
} |
|
201 |
||
202 |
/** |
|
203 |
* The "KeyUpdate" handshake message producer. |
|
204 |
*/ |
|
205 |
private static final class KeyUpdateProducer implements HandshakeProducer { |
|
206 |
// Prevent instantiation of this class. |
|
207 |
private KeyUpdateProducer() { |
|
208 |
// blank |
|
209 |
} |
|
210 |
||
211 |
@Override |
|
212 |
public byte[] produce(ConnectionContext context, |
|
213 |
HandshakeMessage message) throws IOException { |
|
214 |
// The producing happens in server side only. |
|
56544
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
215 |
PostHandshakeContext hc = (PostHandshakeContext)context; |
56542 | 216 |
KeyUpdateMessage km = (KeyUpdateMessage)message; |
217 |
SecretKey secret; |
|
218 |
||
219 |
if (km.getStatus() == KeyUpdateMessage.REQUSTED) { |
|
220 |
secret = hc.conContext.outputRecord.writeCipher.baseSecret; |
|
221 |
} else { |
|
222 |
km.write(hc.handshakeOutput); |
|
223 |
hc.handshakeOutput.flush(); |
|
224 |
return null; |
|
225 |
} |
|
226 |
||
227 |
SSLTrafficKeyDerivation kdg = |
|
228 |
SSLTrafficKeyDerivation.valueOf(hc.conContext.protocolVersion); |
|
229 |
if (kdg == null) { |
|
230 |
// unlikely |
|
231 |
hc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
232 |
"Not supported key derivation: " + |
|
233 |
hc.conContext.protocolVersion); |
|
234 |
return null; |
|
235 |
} |
|
236 |
// update the application traffic read keys. |
|
237 |
SSLKeyDerivation skd = kdg.createKeyDerivation(hc, secret); |
|
238 |
if (skd == null) { |
|
239 |
// unlikely |
|
240 |
hc.conContext.fatal(Alert.INTERNAL_ERROR,"no key derivation"); |
|
241 |
return null; |
|
242 |
} |
|
243 |
SecretKey nplus1 = skd.deriveKey("TlsUpdateNplus1", null); |
|
244 |
SSLKeyDerivation kd = |
|
245 |
kdg.createKeyDerivation(hc, nplus1); |
|
246 |
SecretKey key = kd.deriveKey("TlsKey", null); |
|
247 |
IvParameterSpec ivSpec = |
|
248 |
new IvParameterSpec(kd.deriveKey("TlsIv", null) |
|
249 |
.getEncoded()); |
|
250 |
||
251 |
SSLWriteCipher wc; |
|
252 |
try { |
|
253 |
wc = hc.negotiatedCipherSuite.bulkCipher.createWriteCipher( |
|
254 |
Authenticator.valueOf(hc.conContext.protocolVersion), |
|
255 |
hc.conContext.protocolVersion, key, ivSpec, |
|
256 |
hc.sslContext.getSecureRandom()); |
|
257 |
||
258 |
} catch (GeneralSecurityException gse){ |
|
259 |
hc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
260 |
"Failure to derive application secrets", gse); |
|
261 |
return null; |
|
262 |
} |
|
263 |
||
264 |
km.write(hc.handshakeOutput); |
|
265 |
hc.handshakeOutput.flush(); |
|
266 |
hc.conContext.outputRecord.changeWriteCiphers(wc, false); |
|
267 |
hc.conContext.outputRecord.writeCipher.baseSecret = nplus1; |
|
268 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
269 |
SSLLogger.fine("KeyUpdate: write key updated"); |
|
270 |
} |
|
56544
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
271 |
hc.free(); |
56542 | 272 |
return null; |
273 |
} |
|
274 |
} |
|
275 |
} |