1 /* |
|
2 * Copyright (c) 2015, 2016, 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. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 package sun.security.ssl; |
|
25 |
|
26 import java.security.cert.*; |
|
27 import java.util.*; |
|
28 import java.nio.ByteBuffer; |
|
29 import javax.security.auth.x500.X500Principal; |
|
30 import sun.security.provider.certpath.ResponderId; |
|
31 import sun.security.provider.certpath.OCSPNonceExtension; |
|
32 |
|
33 /* |
|
34 * Checks that the hash value for a certificate's issuer name is generated |
|
35 * correctly. Requires any certificate that is not self-signed. |
|
36 * |
|
37 * NOTE: this test uses Sun private classes which are subject to change. |
|
38 */ |
|
39 public class OCSPStatusRequestTests { |
|
40 |
|
41 private static final boolean debug = false; |
|
42 |
|
43 // The default (no Responder IDs or Extensions) |
|
44 private static final byte[] DEF_OCSPREQ_BYTES = { 0, 0, 0, 0 }; |
|
45 |
|
46 // OCSP Extension with one Responder ID (byName: CN=OCSP Signer) and |
|
47 // a nonce extension (32 bytes). |
|
48 private static final byte[] OCSPREQ_1RID_1EXT = { |
|
49 0, 28, 0, 26, -95, 24, 48, 22, |
|
50 49, 20, 48, 18, 6, 3, 85, 4, |
|
51 3, 19, 11, 79, 67, 83, 80, 32, |
|
52 83, 105, 103, 110, 101, 114, 0, 51, |
|
53 48, 49, 48, 47, 6, 9, 43, 6, |
|
54 1, 5, 5, 7, 48, 1, 2, 4, |
|
55 34, 4, 32, -34, -83, -66, -17, -34, |
|
56 -83, -66, -17, -34, -83, -66, -17, -34, |
|
57 -83, -66, -17, -34, -83, -66, -17, -34, |
|
58 -83, -66, -17, -34, -83, -66, -17, -34, |
|
59 -83, -66, -17 |
|
60 }; |
|
61 |
|
62 public static void main(String[] args) throws Exception { |
|
63 Map<String, TestCase> testList = |
|
64 new LinkedHashMap<String, TestCase>() {{ |
|
65 put("CTOR (default)", testCtorDefault); |
|
66 put("CTOR (Responder Id and Extension)", testCtorRidsExts); |
|
67 put("CTOR (HandshakeInStream)", testCtorInStream); |
|
68 put("CTOR (byte array)", testCtorByteArray); |
|
69 put("Length tests", testLength); |
|
70 put("Equals tests", testEquals); |
|
71 }}; |
|
72 |
|
73 TestUtils.runTests(testList); |
|
74 } |
|
75 |
|
76 // Test the default constructor and its encoding |
|
77 public static final TestCase testCtorDefault = new TestCase() { |
|
78 @Override |
|
79 public Map.Entry<Boolean, String> runTest() { |
|
80 Boolean pass = Boolean.FALSE; |
|
81 String message = null; |
|
82 try { |
|
83 // Create a OCSPStatusRequest with a single ResponderId |
|
84 // and Extension |
|
85 OCSPStatusRequest osrDefault = new OCSPStatusRequest(); |
|
86 HandshakeOutStream hsout = new HandshakeOutStream(null); |
|
87 osrDefault.send(hsout); |
|
88 System.out.println("Encoded Result:"); |
|
89 TestUtils.dumpBytes(hsout.toByteArray()); |
|
90 |
|
91 TestUtils.valueCheck(DEF_OCSPREQ_BYTES, hsout.toByteArray()); |
|
92 pass = Boolean.TRUE; |
|
93 } catch (Exception e) { |
|
94 e.printStackTrace(System.out); |
|
95 message = e.getClass().getName(); |
|
96 } |
|
97 |
|
98 return new AbstractMap.SimpleEntry<>(pass, message); |
|
99 } |
|
100 }; |
|
101 |
|
102 // Test the constructor form that allows the user to specify zero |
|
103 // or more ResponderId objects and/or Extensions |
|
104 public static final TestCase testCtorRidsExts = new TestCase() { |
|
105 @Override |
|
106 public Map.Entry<Boolean, String> runTest() { |
|
107 Boolean pass = Boolean.FALSE; |
|
108 String message = null; |
|
109 try { |
|
110 List<ResponderId> ridList = new LinkedList<ResponderId>() {{ |
|
111 add(new ResponderId(new X500Principal("CN=OCSP Signer"))); |
|
112 }}; |
|
113 List<Extension> extList = new LinkedList<Extension>() {{ |
|
114 add(new OCSPNonceExtension(32)); |
|
115 }}; |
|
116 |
|
117 // Default-style OCSPStatusRequest using both empty Lists and |
|
118 // null inputs |
|
119 OCSPStatusRequest osrDef1 = |
|
120 new OCSPStatusRequest(new LinkedList<ResponderId>(), |
|
121 null); |
|
122 OCSPStatusRequest osrDef2 = |
|
123 new OCSPStatusRequest(null, |
|
124 new LinkedList<Extension>()); |
|
125 HandshakeOutStream hsout = new HandshakeOutStream(null); |
|
126 osrDef1.send(hsout); |
|
127 System.out.println("Encoded Result:"); |
|
128 TestUtils.dumpBytes(hsout.toByteArray()); |
|
129 TestUtils.valueCheck(DEF_OCSPREQ_BYTES, hsout.toByteArray()); |
|
130 |
|
131 hsout.reset(); |
|
132 osrDef2.send(hsout); |
|
133 System.out.println("Encoded Result:"); |
|
134 TestUtils.dumpBytes(hsout.toByteArray()); |
|
135 TestUtils.valueCheck(DEF_OCSPREQ_BYTES, hsout.toByteArray()); |
|
136 |
|
137 hsout.reset(); |
|
138 OCSPStatusRequest osrWithItems = |
|
139 new OCSPStatusRequest(ridList, extList); |
|
140 osrWithItems.send(hsout); |
|
141 System.out.println("Encoded Result:"); |
|
142 byte[] encodedData = hsout.toByteArray(); |
|
143 TestUtils.dumpBytes(encodedData); |
|
144 // Check everything except the last 32 bytes (nonce data) |
|
145 TestUtils.valueCheck(OCSPREQ_1RID_1EXT, encodedData, 0, 0, |
|
146 encodedData.length - 32); |
|
147 |
|
148 pass = Boolean.TRUE; |
|
149 } catch (Exception e) { |
|
150 e.printStackTrace(System.out); |
|
151 message = e.getClass().getName(); |
|
152 } |
|
153 |
|
154 return new AbstractMap.SimpleEntry<>(pass, message); |
|
155 } |
|
156 }; |
|
157 |
|
158 // Test the constructor that builds the ob ject using data from |
|
159 // a HandshakeInStream |
|
160 public static final TestCase testCtorInStream = new TestCase() { |
|
161 @Override |
|
162 public Map.Entry<Boolean, String> runTest() { |
|
163 Boolean pass = Boolean.FALSE; |
|
164 String message = null; |
|
165 try { |
|
166 ResponderId checkRid = |
|
167 new ResponderId(new X500Principal("CN=OCSP Signer")); |
|
168 Extension checkExt = new OCSPNonceExtension(32); |
|
169 |
|
170 HandshakeInStream hsis = new HandshakeInStream(); |
|
171 hsis.incomingRecord(ByteBuffer.wrap(OCSPREQ_1RID_1EXT)); |
|
172 OCSPStatusRequest osr = new OCSPStatusRequest(hsis); |
|
173 |
|
174 List<ResponderId> ridList = osr.getResponderIds(); |
|
175 List<Extension> extList = osr.getExtensions(); |
|
176 |
|
177 if (ridList.size() != 1 || !ridList.contains(checkRid)) { |
|
178 throw new RuntimeException("Responder list mismatch"); |
|
179 } else if (extList.size() != 1 || |
|
180 !extList.get(0).getId().equals(checkExt.getId())) { |
|
181 throw new RuntimeException("Extension list mismatch"); |
|
182 } |
|
183 |
|
184 pass = Boolean.TRUE; |
|
185 } catch (Exception e) { |
|
186 e.printStackTrace(System.out); |
|
187 message = e.getClass().getName(); |
|
188 } |
|
189 |
|
190 return new AbstractMap.SimpleEntry<>(pass, message); |
|
191 } |
|
192 }; |
|
193 |
|
194 // Test the constructor form that takes the data from a byte array |
|
195 public static final TestCase testCtorByteArray = new TestCase() { |
|
196 @Override |
|
197 public Map.Entry<Boolean, String> runTest() { |
|
198 Boolean pass = Boolean.FALSE; |
|
199 String message = null; |
|
200 try { |
|
201 ResponderId checkRid = |
|
202 new ResponderId(new X500Principal("CN=OCSP Signer")); |
|
203 Extension checkExt = new OCSPNonceExtension(32); |
|
204 |
|
205 OCSPStatusRequest osr = |
|
206 new OCSPStatusRequest(OCSPREQ_1RID_1EXT); |
|
207 |
|
208 List<ResponderId> ridList = osr.getResponderIds(); |
|
209 List<Extension> extList = osr.getExtensions(); |
|
210 |
|
211 if (ridList.size() != 1 || !ridList.contains(checkRid)) { |
|
212 throw new RuntimeException("Responder list mismatch"); |
|
213 } else if (extList.size() != 1 || |
|
214 !extList.get(0).getId().equals(checkExt.getId())) { |
|
215 throw new RuntimeException("Extension list mismatch"); |
|
216 } |
|
217 pass = Boolean.TRUE; |
|
218 } catch (Exception e) { |
|
219 e.printStackTrace(System.out); |
|
220 message = e.getClass().getName(); |
|
221 } |
|
222 |
|
223 return new AbstractMap.SimpleEntry<>(pass, message); |
|
224 } |
|
225 }; |
|
226 |
|
227 // Test the length functions for both default and non-default |
|
228 // OCSPStatusRequest objects |
|
229 public static final TestCase testLength = new TestCase() { |
|
230 @Override |
|
231 public Map.Entry<Boolean, String> runTest() { |
|
232 Boolean pass = Boolean.FALSE; |
|
233 String message = null; |
|
234 try { |
|
235 HandshakeInStream hsis = new HandshakeInStream(); |
|
236 hsis.incomingRecord(ByteBuffer.wrap(OCSPREQ_1RID_1EXT)); |
|
237 OCSPStatusRequest osr = new OCSPStatusRequest(hsis); |
|
238 OCSPStatusRequest osrDefault = new OCSPStatusRequest(); |
|
239 |
|
240 if (osrDefault.length() != DEF_OCSPREQ_BYTES.length) { |
|
241 throw new RuntimeException("Invalid length for default: " + |
|
242 "Expected" + DEF_OCSPREQ_BYTES.length + |
|
243 ", received " + osrDefault.length()); |
|
244 } else if (osr.length() != OCSPREQ_1RID_1EXT.length) { |
|
245 throw new RuntimeException("Invalid length for default: " + |
|
246 "Expected" + OCSPREQ_1RID_1EXT.length + |
|
247 ", received " + osr.length()); |
|
248 } |
|
249 |
|
250 pass = Boolean.TRUE; |
|
251 } catch (Exception e) { |
|
252 e.printStackTrace(System.out); |
|
253 message = e.getClass().getName(); |
|
254 } |
|
255 |
|
256 return new AbstractMap.SimpleEntry<>(pass, message); |
|
257 } |
|
258 }; |
|
259 |
|
260 // Test the equals method with default and non-default objects |
|
261 public static final TestCase testEquals = new TestCase() { |
|
262 @Override |
|
263 public Map.Entry<Boolean, String> runTest() { |
|
264 Boolean pass = Boolean.FALSE; |
|
265 String message = null; |
|
266 try { |
|
267 // Make two different lists with the same ResponderId values |
|
268 // and also make a extension list |
|
269 List<ResponderId> ridList1 = new LinkedList<ResponderId>() {{ |
|
270 add(new ResponderId(new X500Principal("CN=OCSP Signer"))); |
|
271 }}; |
|
272 List<ResponderId> ridList2 = new LinkedList<ResponderId>() {{ |
|
273 add(new ResponderId(new X500Principal("CN=OCSP Signer"))); |
|
274 }}; |
|
275 List<Extension> extList = new LinkedList<Extension>() {{ |
|
276 add(new OCSPNonceExtension(32)); |
|
277 }}; |
|
278 |
|
279 // We expect two default OCSP objects to be equal |
|
280 OCSPStatusRequest osrDefault = new OCSPStatusRequest(); |
|
281 if (!osrDefault.equals(new OCSPStatusRequest())) { |
|
282 throw new RuntimeException("Default OCSPStatusRequest" + |
|
283 " equality test failed"); |
|
284 } |
|
285 |
|
286 // null test (expect false return) |
|
287 if (osrDefault.equals(null)) { |
|
288 throw new RuntimeException("OCSPStatusRequest matched" + |
|
289 " unexpectedly"); |
|
290 } |
|
291 |
|
292 // Self-reference test |
|
293 OCSPStatusRequest osrSelfRef = osrDefault; |
|
294 if (!osrDefault.equals(osrSelfRef)) { |
|
295 throw new RuntimeException("Default OCSPStatusRequest" + |
|
296 " equality test failed"); |
|
297 } |
|
298 |
|
299 // Two OCSPStatusRequests with matching ResponderIds should |
|
300 // be considered equal |
|
301 OCSPStatusRequest osrByList1 = |
|
302 new OCSPStatusRequest(ridList1, null); |
|
303 OCSPStatusRequest osrByList2 = new OCSPStatusRequest(ridList2, |
|
304 Collections.emptyList()); |
|
305 if (!osrByList1.equals(osrByList2)) { |
|
306 throw new RuntimeException("Single Responder ID " + |
|
307 "OCSPStatusRequest equality test failed"); |
|
308 } |
|
309 |
|
310 // We expect OCSPStatusRequests with different nonces to be |
|
311 // considered unequal. |
|
312 HandshakeInStream hsis = new HandshakeInStream(); |
|
313 hsis.incomingRecord(ByteBuffer.wrap(OCSPREQ_1RID_1EXT)); |
|
314 OCSPStatusRequest osrStream = new OCSPStatusRequest(hsis); |
|
315 OCSPStatusRequest osrRidExt = new OCSPStatusRequest(ridList1, |
|
316 extList); |
|
317 if (osrStream.equals(osrRidExt)) { |
|
318 throw new RuntimeException("OCSPStatusRequest matched" + |
|
319 " unexpectedly"); |
|
320 } |
|
321 |
|
322 pass = Boolean.TRUE; |
|
323 } catch (Exception e) { |
|
324 e.printStackTrace(System.out); |
|
325 message = e.getClass().getName(); |
|
326 } |
|
327 |
|
328 return new AbstractMap.SimpleEntry<>(pass, message); |
|
329 } |
|
330 }; |
|
331 |
|
332 } |
|