author | xuelei |
Thu, 24 May 2018 09:21:01 -0700 | |
branch | JDK-8145252-TLS13-branch |
changeset 56603 | f103e0c2be1e |
parent 56559 | a423173d0578 |
permissions | -rw-r--r-- |
56542 | 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.text.MessageFormat; |
|
31 |
import java.util.Locale; |
|
32 |
import sun.security.ssl.ClientHello.ClientHelloMessage; |
|
33 |
import sun.security.ssl.SSLHandshake.HandshakeMessage; |
|
34 |
||
35 |
/** |
|
36 |
* Pack of the HelloVerifyRequest handshake message. |
|
37 |
*/ |
|
38 |
final class HelloVerifyRequest { |
|
39 |
static final SSLConsumer handshakeConsumer = |
|
40 |
new HelloVerifyRequestConsumer(); |
|
41 |
static final HandshakeProducer handshakeProducer = |
|
42 |
new HelloVerifyRequestProducer(); |
|
43 |
||
44 |
/** |
|
45 |
* The HelloVerifyRequest handshake message [RFC 6347]. |
|
46 |
*/ |
|
47 |
static final class HelloVerifyRequestMessage extends HandshakeMessage { |
|
48 |
final int serverVersion; |
|
49 |
final byte[] cookie; |
|
50 |
||
51 |
HelloVerifyRequestMessage(HandshakeContext context, |
|
52 |
HandshakeMessage message) throws IOException { |
|
53 |
super(context); |
|
54 |
// This happens in server side only. |
|
55 |
ServerHandshakeContext shc = |
|
56 |
(ServerHandshakeContext)context; |
|
57 |
ClientHelloMessage clientHello = (ClientHelloMessage)message; |
|
58 |
||
59 |
HelloCookieManager hcMgr = |
|
60 |
shc.sslContext.getHelloCookieManager(ProtocolVersion.DTLS10); |
|
61 |
this.serverVersion = shc.clientHelloVersion; |
|
62 |
this.cookie = hcMgr.createCookie(shc, clientHello); |
|
63 |
} |
|
64 |
||
65 |
HelloVerifyRequestMessage(HandshakeContext context, |
|
66 |
ByteBuffer m) throws IOException { |
|
67 |
super(context); |
|
68 |
// This happens in client side only. |
|
69 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
70 |
||
71 |
// struct { |
|
72 |
// ProtocolVersion server_version; |
|
73 |
// opaque cookie<0..2^8-1>; |
|
74 |
// } HelloVerifyRequest; |
|
75 |
if (m.remaining() < 3) { |
|
76 |
chc.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
|
77 |
"Invalid HelloVerifyRequest: no sufficient data"); |
|
78 |
} |
|
79 |
||
80 |
byte major = m.get(); |
|
81 |
byte minor = m.get(); |
|
82 |
this.serverVersion = ((major & 0xFF) << 8) | (minor & 0xFF); |
|
83 |
this.cookie = Record.getBytes8(m); |
|
84 |
} |
|
85 |
||
86 |
@Override |
|
87 |
public SSLHandshake handshakeType() { |
|
88 |
return SSLHandshake.HELLO_VERIFY_REQUEST; |
|
89 |
} |
|
90 |
||
91 |
@Override |
|
92 |
public int messageLength() { |
|
56559
a423173d0578
rollbak behavior changes of extended secret extension
xuelei
parents:
56542
diff
changeset
|
93 |
return 3 + cookie.length; // 2: the length of protocol version |
a423173d0578
rollbak behavior changes of extended secret extension
xuelei
parents:
56542
diff
changeset
|
94 |
// +1: the cookie length |
56542 | 95 |
} |
96 |
||
97 |
@Override |
|
98 |
public void send(HandshakeOutStream hos) throws IOException { |
|
99 |
hos.putInt8((byte)((serverVersion >>> 8) & 0xFF)); |
|
100 |
hos.putInt8((byte)(serverVersion & 0xFF)); |
|
101 |
hos.putBytes8(cookie); |
|
102 |
} |
|
103 |
||
104 |
@Override |
|
105 |
public String toString() { |
|
106 |
MessageFormat messageFormat = new MessageFormat( |
|
107 |
"\"HelloVerifyRequest\": '{'\n" + |
|
108 |
" \"server version\" : \"{0}\",\n" + |
|
109 |
" \"cookie\" : \"{1}\",\n" + |
|
110 |
"'}'", |
|
111 |
Locale.ENGLISH); |
|
112 |
Object[] messageFields = { |
|
113 |
ProtocolVersion.nameOf(serverVersion), |
|
114 |
Utilities.toHexString(cookie), |
|
115 |
}; |
|
116 |
||
117 |
return messageFormat.format(messageFields); |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
/** |
|
122 |
* The "HelloVerifyRequest" handshake message producer. |
|
123 |
*/ |
|
124 |
private static final |
|
125 |
class HelloVerifyRequestProducer implements HandshakeProducer { |
|
126 |
// Prevent instantiation of this class. |
|
127 |
private HelloVerifyRequestProducer() { |
|
128 |
// blank |
|
129 |
} |
|
130 |
||
131 |
@Override |
|
132 |
public byte[] produce(ConnectionContext context, |
|
133 |
HandshakeMessage message) throws IOException { |
|
134 |
// The producing happens in server side only. |
|
135 |
ServerHandshakeContext shc = (ServerHandshakeContext)context; |
|
136 |
||
137 |
// clean up this producer |
|
138 |
shc.handshakeProducers.remove(SSLHandshake.HELLO_VERIFY_REQUEST.id); |
|
139 |
||
140 |
HelloVerifyRequestMessage hvrm = |
|
141 |
new HelloVerifyRequestMessage(shc, message); |
|
142 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
143 |
SSLLogger.fine( |
|
144 |
"Produced HelloVerifyRequest handshake message", hvrm); |
|
145 |
} |
|
146 |
||
147 |
// Output the handshake message. |
|
148 |
hvrm.write(shc.handshakeOutput); |
|
149 |
shc.handshakeOutput.flush(); |
|
150 |
||
151 |
// update the context |
|
152 |
||
56603 | 153 |
// Stateless, clean up the handshake context as well? |
56542 | 154 |
shc.handshakeHash.finish(); // forgot about the handshake hash |
155 |
shc.handshakeExtensions.clear(); |
|
156 |
||
157 |
// What's the expected response? |
|
158 |
shc.handshakeConsumers.put( |
|
159 |
SSLHandshake.CLIENT_HELLO.id, SSLHandshake.CLIENT_HELLO); |
|
160 |
||
161 |
// The handshake message has been delivered. |
|
162 |
return null; |
|
163 |
} |
|
164 |
} |
|
165 |
||
166 |
/** |
|
167 |
* The "HelloVerifyRequest" handshake message consumer. |
|
168 |
*/ |
|
169 |
private static final class HelloVerifyRequestConsumer |
|
170 |
implements SSLConsumer { |
|
171 |
||
172 |
// Prevent instantiation of this class. |
|
173 |
private HelloVerifyRequestConsumer() { |
|
174 |
// blank |
|
175 |
} |
|
176 |
||
177 |
@Override |
|
178 |
public void consume(ConnectionContext context, |
|
179 |
ByteBuffer message) throws IOException { |
|
180 |
// The consuming happens in client side only. |
|
181 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
182 |
||
183 |
// clean up this consumer |
|
184 |
chc.handshakeConsumers.remove(SSLHandshake.HELLO_VERIFY_REQUEST.id); |
|
185 |
if (!chc.handshakeConsumers.isEmpty()) { |
|
186 |
chc.handshakeConsumers.remove(SSLHandshake.SERVER_HELLO.id); |
|
187 |
} |
|
188 |
if (!chc.handshakeConsumers.isEmpty()) { |
|
189 |
chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
|
190 |
"No more message expected before " + |
|
191 |
"HelloVerifyRequest is processed"); |
|
192 |
} |
|
193 |
||
194 |
// Refresh handshake hash. |
|
195 |
chc.handshakeHash.finish(); // forgot about the handshake hash |
|
196 |
||
197 |
HelloVerifyRequestMessage hvrm = |
|
198 |
new HelloVerifyRequestMessage(chc, message); |
|
199 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
200 |
SSLLogger.fine( |
|
201 |
"Consuming HelloVerifyRequest handshake message", hvrm); |
|
202 |
} |
|
203 |
||
204 |
// Note that HelloVerifyRequest.server_version is used solely to |
|
205 |
// indicate packet formatting, and not as part of version |
|
206 |
// negotiation. Need not to check version values match for |
|
207 |
// HelloVerifyRequest message. |
|
208 |
chc.initialClientHelloMsg.setHelloCookie(hvrm.cookie); |
|
209 |
||
210 |
// |
|
211 |
// produce response handshake message |
|
212 |
// |
|
213 |
SSLHandshake.CLIENT_HELLO.produce(context, hvrm); |
|
214 |
} |
|
215 |
} |
|
216 |
} |
|
217 |