1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package tests.api.javax.net.ssl; 19 20 import java.io.FileNotFoundException; 21 import java.security.KeyManagementException; 22 import java.security.KeyStore; 23 import java.security.KeyStoreException; 24 import java.security.NoSuchAlgorithmException; 25 import java.security.NoSuchProviderException; 26 import java.security.Provider; 27 import java.security.SecureRandom; 28 import java.security.UnrecoverableKeyException; 29 30 import javax.net.ssl.KeyManager; 31 import javax.net.ssl.KeyManagerFactory; 32 import javax.net.ssl.SSLContext; 33 import javax.net.ssl.SSLContextSpi; 34 import javax.net.ssl.SSLEngine; 35 import javax.net.ssl.SSLServerSocketFactory; 36 import javax.net.ssl.SSLSessionContext; 37 import javax.net.ssl.SSLSocketFactory; 38 import javax.net.ssl.TrustManager; 39 import javax.net.ssl.TrustManagerFactory; 40 41 import junit.framework.TestCase; 42 43 import org.apache.harmony.security.tests.support.SpiEngUtils; 44 import org.apache.harmony.xnet.tests.support.MySSLContextSpi; 45 46 /** 47 * Tests for <code>SSLContext</code> class constructors and methods. 48 * 49 */ 50 public class SSLContext1Test extends TestCase { 51 52 private static String srvSSLContext = "SSLContext"; 53 public static String defaultProtocol = "TLS"; 54 private static final String NotSupportMsg = "Default protocol is not supported"; 55 private static String defaultProviderName = null; 56 private static Provider defaultProvider = null; 57 private static final String[] invalidValues = SpiEngUtils.invalidValues; 58 private static boolean DEFSupported = false; 59 private static String[] validValues = new String[3]; 60 static { 61 defaultProvider = SpiEngUtils.isSupport(defaultProtocol, srvSSLContext); 62 DEFSupported = (defaultProvider != null); 63 if (DEFSupported) { 64 defaultProviderName = (DEFSupported ? defaultProvider.getName() 65 : null); 66 validValues[0] = defaultProtocol; 67 validValues[1] = defaultProtocol.toUpperCase(); 68 validValues[2] = defaultProtocol.toLowerCase(); 69 } else { 70 defaultProtocol = null; 71 } 72 } 73 74 protected SSLContext[] createSSLCon() { 75 if (!DEFSupported) { 76 fail(defaultProtocol + " protocol is not supported"); 77 return null; 78 } 79 SSLContext[] sslC = new SSLContext[3]; 80 try { 81 sslC[0] = SSLContext.getInstance(defaultProtocol); 82 sslC[1] = SSLContext.getInstance(defaultProtocol, defaultProvider); 83 sslC[2] = SSLContext.getInstance(defaultProtocol, 84 defaultProviderName); 85 return sslC; 86 } catch (Exception e) { 87 e.printStackTrace(); 88 return null; 89 } 90 } 91 92 /** 93 * Test for <code>SSLContext</code> constructor Assertion: returns 94 * SSLContext object 95 */ 96 public void test_ConstructorLjavax_net_ssl_SSLContextSpiLjava_security_ProviderLjava_lang_String() 97 throws NoSuchAlgorithmException, 98 KeyManagementException { 99 if (!DEFSupported) { 100 fail(NotSupportMsg); 101 return; 102 } 103 SSLContextSpi spi = new MySSLContextSpi(); 104 SSLContext sslContext = new MySslContext(spi, defaultProvider, 105 defaultProtocol); 106 assertEquals("Incorrect protocol", defaultProtocol, 107 sslContext.getProtocol()); 108 assertEquals("Incorrect provider", defaultProvider, 109 sslContext.getProvider()); 110 TrustManager[] tm = null; 111 KeyManager[] km = null; 112 sslContext.init(km, tm, new SecureRandom()); 113 assertNotNull("No SSLEngine created", 114 sslContext.createSSLEngine()); 115 assertNotNull("No SSLEngine created", 116 sslContext.createSSLEngine("host", 8888)); 117 try { 118 sslContext.init(km, tm, null); 119 fail("KeyManagementException should be thrown for null " 120 + "SecureRandom"); 121 } catch (KeyManagementException e) { 122 } 123 124 sslContext = new MySslContext(null, null, null); 125 assertNull("Incorrect protocol", sslContext.getProtocol()); 126 assertNull("Incorrect provider", sslContext.getProvider()); 127 try { 128 sslContext.createSSLEngine(); 129 fail("NullPointerException should be thrown"); 130 } catch (NullPointerException e) { 131 } 132 try { 133 sslContext.getSocketFactory(); 134 fail("NullPointerException should be thrown"); 135 } catch (NullPointerException e) { 136 } 137 } 138 139 /** 140 * @throws KeyManagementException 141 * javax.net.ssl.SSLContext#createSSLEngine() 142 */ 143 public void test_createSSLEngine() throws KeyManagementException { 144 if (!DEFSupported) fail(NotSupportMsg); 145 SSLContextSpi spi = new MySSLContextSpi(); 146 SSLContext sslContext = new MySslContext(spi, defaultProvider, 147 defaultProtocol); 148 sslContext.init(null, null, new SecureRandom()); 149 SSLEngine sslEngine = sslContext.createSSLEngine(); 150 assertNotNull("SSL engine is null", sslEngine); 151 } 152 153 /** 154 * @throws KeyManagementException 155 * javax.net.ssl.SSLContext#createSSLEngine(java.lang.String, int) 156 */ 157 public void test_createSSLEngineLjava_lang_StringI() 158 throws KeyManagementException { 159 if (!DEFSupported) fail(NotSupportMsg); 160 SSLContextSpi spi = new MySSLContextSpi(); 161 SSLContext sslContext = new MySslContext(spi, defaultProvider, 162 defaultProtocol); 163 sslContext.init(null, null, new SecureRandom()); 164 SSLEngine sslEngine = sslContext.createSSLEngine("www.fortify.net", 80); 165 assertNotNull("SSL engine is null", sslEngine); 166 } 167 168 /** 169 * Test for <code>getClientSessionContext()</code> 170 * <code>getServiceSessionContext()</code> 171 * methods Assertion: returns correspondent object 172 * @throws KeyManagementException 173 */ 174 public void test_getClientSessionContext() throws NoSuchAlgorithmException, KeyManagementException { 175 if (!DEFSupported) { 176 fail(NotSupportMsg); 177 return; 178 } 179 SSLContext[] sslC = createSSLCon(); 180 assertNotNull("SSLContext objects were not created", sslC); 181 for (int i = 0; i < sslC.length; i++) { 182 sslC[i].init(null, null, null); 183 assertNotNull("Client session is incorrectly instantiated: " + i, 184 sslC[i].getClientSessionContext()); 185 assertNotNull("Server session is incorrectly instantiated: " + i, 186 sslC[i].getServerSessionContext()); 187 } 188 } 189 190 /** 191 * Test for <code>getInstance(String protocol)</code> method Assertion: 192 * returns SSLContext object 193 */ 194 public void test_getInstanceLjava_lang_String01() 195 throws NoSuchAlgorithmException { 196 if (!DEFSupported) { 197 fail(NotSupportMsg); 198 return; 199 } 200 SSLContext sslContext; 201 for (int i = 0; i < validValues.length; i++) { 202 sslContext = SSLContext.getInstance(validValues[i]); 203 assertNotNull("No SSLContext created", sslContext); 204 assertEquals("Invalid protocol", validValues[i], 205 sslContext.getProtocol()); 206 } 207 } 208 209 /** 210 * Test for <code>getInstance(String protocol)</code> method Assertion: 211 * throws NullPointerException when protocol is null; throws 212 * NoSuchAlgorithmException when protocol is not correct; 213 */ 214 public void test_getInstanceLjava_lang_String02() { 215 try { 216 SSLContext.getInstance(null); 217 fail("NoSuchAlgorithmException or NullPointerException should be thrown (protocol is null"); 218 } catch (NoSuchAlgorithmException e) { 219 } catch (NullPointerException e) { 220 } 221 for (int i = 0; i < invalidValues.length; i++) { 222 try { 223 SSLContext.getInstance(invalidValues[i]); 224 fail("NoSuchAlgorithmException was not thrown as expected for provider: " 225 .concat(invalidValues[i])); 226 } catch (NoSuchAlgorithmException e) { 227 } 228 } 229 } 230 231 /** 232 * Test for <code>getInstance(String protocol, String provider)</code> 233 * method Assertion: throws IllegalArgumentException when provider is null 234 * or empty 235 */ 236 public void test_getInstanceLjava_lang_StringLjava_lang_String01() throws NoSuchProviderException, 237 NoSuchAlgorithmException { 238 if (!DEFSupported) { 239 fail(NotSupportMsg); 240 return; 241 } 242 String provider = null; 243 for (int i = 0; i < validValues.length; i++) { 244 try { 245 SSLContext.getInstance(defaultProtocol, provider); 246 fail("IllegalArgumentException must be thrown when provider is null"); 247 } catch (IllegalArgumentException e) { 248 } 249 try { 250 SSLContext.getInstance(defaultProtocol, ""); 251 fail("IllegalArgumentException must be thrown when provider is empty"); 252 } catch (IllegalArgumentException e) { 253 } 254 } 255 } 256 257 /** 258 * Test for <code>getInstance(String protocol, String provider)</code> 259 * method Assertion: throws NullPointerException when protocol is null; 260 * throws NoSuchAlgorithmException when protocol is not correct; 261 */ 262 public void test_getInstanceLjava_lang_StringLjava_lang_String02() throws NoSuchProviderException { 263 if (!DEFSupported) { 264 fail(NotSupportMsg); 265 return; 266 } 267 try { 268 SSLContext.getInstance(null, defaultProviderName); 269 fail("NoSuchAlgorithmException or NullPointerException should be thrown (protocol is null"); 270 } catch (NoSuchAlgorithmException e) { 271 } catch (NullPointerException e) { 272 } 273 for (int i = 0; i < invalidValues.length; i++) { 274 try { 275 SSLContext.getInstance(invalidValues[i], defaultProviderName); 276 fail("NoSuchAlgorithmException was not thrown as expected (protocol: " 277 .concat(invalidValues[i]).concat(")")); 278 } catch (NoSuchAlgorithmException e) { 279 } 280 } 281 } 282 283 /** 284 * Test for <code>getInstance(String protocol, String provider)</code> 285 * method Assertion: throws NoSuchProviderException when provider has 286 * invalid value 287 */ 288 public void test_getInstanceLjava_lang_StringLjava_lang_String03() throws NoSuchAlgorithmException { 289 if (!DEFSupported) { 290 fail(NotSupportMsg); 291 return; 292 } 293 for (int i = 1; i < invalidValues.length; i++) { 294 for (int j = 0; j < validValues.length; j++) { 295 try { 296 SSLContext.getInstance(validValues[j], invalidValues[i]); 297 fail("NuSuchProviderException must be thrown (protocol: " 298 .concat(validValues[j]).concat(" provider: ") 299 .concat(invalidValues[i]).concat(")")); 300 } catch (NoSuchProviderException e) { 301 } 302 } 303 } 304 } 305 306 /** 307 * Test for <code>getInstance(String protocol, String provider)</code> 308 * method Assertion: returns instance of SSLContext 309 */ 310 public void test_getInstanceLjava_lang_StringLjava_lang_String04() throws NoSuchAlgorithmException, 311 NoSuchProviderException { 312 if (!DEFSupported) { 313 fail(NotSupportMsg); 314 return; 315 } 316 SSLContext sslContext; 317 for (int i = 0; i < validValues.length; i++) { 318 sslContext = SSLContext.getInstance(validValues[i], 319 defaultProviderName); 320 assertNotNull("Not SSLContext created", sslContext); 321 assertEquals("Invalid protocol", 322 validValues[i], sslContext.getProtocol()); 323 assertEquals("Invalid provider", 324 defaultProvider, sslContext.getProvider()); 325 } 326 } 327 328 /** 329 * Test for <code>getInstance(String protocol, Provider provider)</code> 330 * method Assertion: throws IllegalArgumentException when provider is null 331 */ 332 public void test_getInstanceLjava_lang_StringLjava_security_Provider01() throws NoSuchAlgorithmException { 333 if (!DEFSupported) { 334 fail(NotSupportMsg); 335 return; 336 } 337 Provider provider = null; 338 for (int i = 0; i < validValues.length; i++) { 339 try { 340 SSLContext.getInstance(validValues[i], provider); 341 fail("IllegalArgumentException must be thrown when provider is null"); 342 } catch (IllegalArgumentException e) { 343 } 344 } 345 } 346 347 /** 348 * Test for <code>getInstance(String protocol, Provider provider)</code> 349 * method Assertion: throws NullPointerException when protocol is null; 350 * throws NoSuchAlgorithmException when protocol is not correct; 351 */ 352 public void test_getInstanceLjava_lang_StringLjava_security_Provider02() { 353 if (!DEFSupported) { 354 fail(NotSupportMsg); 355 return; 356 } 357 try { 358 SSLContext.getInstance(null, defaultProvider); 359 fail("NoSuchAlgorithmException or NullPointerException should be thrown (protocol is null"); 360 } catch (NoSuchAlgorithmException e) { 361 } catch (NullPointerException e) { 362 } 363 for (int i = 0; i < invalidValues.length; i++) { 364 try { 365 SSLContext.getInstance(invalidValues[i], defaultProvider); 366 fail("Expected NoSuchAlgorithmException was not thrown as expected"); 367 } catch (NoSuchAlgorithmException e) { 368 } 369 } 370 } 371 372 /** 373 * Test for <code>getInstance(String protocol, Provider provider)</code> 374 * method Assertion: returns instance of SSLContext 375 */ 376 public void test_getInstanceLjava_lang_StringLjava_security_Provider03() throws NoSuchAlgorithmException { 377 if (!DEFSupported) { 378 fail(NotSupportMsg); 379 return; 380 } 381 SSLContext sslContext; 382 for (int i = 0; i < validValues.length; i++) { 383 sslContext = SSLContext 384 .getInstance(validValues[i], defaultProvider); 385 assertNotNull("Not SSLContext created", sslContext); 386 assertEquals("Invalid protocol", validValues[i], sslContext.getProtocol()); 387 assertEquals("Invalid provider", defaultProvider, sslContext.getProvider()); 388 } 389 } 390 391 /** 392 * @throws NoSuchAlgorithmException 393 * @throws NoSuchProviderException 394 * javax.net.ssl.SSLContext#getProtocol() 395 */ 396 public void test_getProtocol() 397 throws NoSuchAlgorithmException, NoSuchProviderException { 398 if (!DEFSupported) fail(NotSupportMsg); 399 SSLContextSpi spi = new MySSLContextSpi(); 400 SSLContext sslContext = new MySslContext(spi, defaultProvider, 401 defaultProtocol); 402 assertEquals("Incorrect protocol", 403 defaultProtocol, sslContext.getProtocol()); 404 sslContext = new MySslContext(spi, defaultProvider, 405 null); 406 assertNull("Incorrect protocol", sslContext.getProtocol()); 407 sslContext = SSLContext.getInstance(defaultProtocol); 408 assertEquals("Incorrect protocol", 409 defaultProtocol, sslContext.getProtocol()); 410 sslContext = SSLContext.getInstance(defaultProtocol, defaultProvider); 411 assertEquals("Incorrect protocol", 412 defaultProtocol, sslContext.getProtocol()); 413 sslContext = SSLContext.getInstance(defaultProtocol, defaultProviderName); 414 assertEquals("Incorrect protocol", 415 defaultProtocol, sslContext.getProtocol()); 416 } 417 418 /** 419 * @throws NoSuchAlgorithmException 420 * @throws NoSuchProviderException 421 * javax.net.ssl.SSLContext#getProvider() 422 */ 423 public void test_getProvider() 424 throws NoSuchAlgorithmException, NoSuchProviderException { 425 if (!DEFSupported) fail(NotSupportMsg); 426 SSLContextSpi spi = new MySSLContextSpi(); 427 SSLContext sslContext = new MySslContext(spi, defaultProvider, 428 defaultProtocol); 429 assertEquals("Incorrect provider", 430 defaultProvider, sslContext.getProvider()); 431 sslContext = SSLContext.getInstance(defaultProtocol, defaultProvider); 432 assertEquals("Incorrect provider", 433 defaultProvider, sslContext.getProvider()); 434 sslContext = SSLContext.getInstance(defaultProtocol, defaultProviderName); 435 assertEquals("Incorrect provider", 436 defaultProvider, sslContext.getProvider()); 437 } 438 439 /** 440 * javax.net.ssl.SSLContext#getServletSessionContext() 441 */ 442 public void test_getServerSessionContext() throws NoSuchAlgorithmException, 443 KeyManagementException, KeyStoreException, 444 UnrecoverableKeyException { 445 if (!DEFSupported) fail(NotSupportMsg); 446 SSLContext[] sslC = createSSLCon(); 447 assertNotNull("SSLContext objects were not created", sslC); 448 String tAlg = TrustManagerFactory.getDefaultAlgorithm(); 449 String kAlg = KeyManagerFactory.getDefaultAlgorithm(); 450 if (tAlg == null) 451 fail("TrustManagerFactory default algorithm is not defined"); 452 if (kAlg == null) 453 fail("KeyManagerFactory default algorithm is not defined"); 454 KeyManagerFactory kmf = KeyManagerFactory.getInstance(kAlg); 455 kmf.init(null, new char[11]); 456 TrustManagerFactory tmf = TrustManagerFactory.getInstance(tAlg); 457 KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); 458 tmf.init(ks); 459 TrustManager[] tms = tmf.getTrustManagers(); 460 for (SSLContext sslCi : sslC) { 461 sslCi.init(kmf.getKeyManagers(), tms, new SecureRandom()); 462 assertNotNull("Server context is incorrectly instantiated", sslCi 463 .getServerSessionContext()); 464 } 465 } 466 467 /** 468 * Test for <code>getServerSocketFactory()</code> 469 * <code>getSocketFactory()</code> 470 * <code>init(KeyManager[] km, TrustManager[] tm, SecureRandom random)</code> 471 * methods Assertion: returns correspondent object 472 * 473 */ 474 public void test_getServerSocketFactory() throws NoSuchAlgorithmException, 475 KeyManagementException, KeyStoreException, 476 UnrecoverableKeyException { 477 if (!DEFSupported) { 478 fail(NotSupportMsg); 479 return; 480 } 481 SSLContext[] sslC = createSSLCon(); 482 assertNotNull("SSLContext objects were not created", sslC); 483 String tAlg = TrustManagerFactory.getDefaultAlgorithm(); 484 String kAlg = KeyManagerFactory.getDefaultAlgorithm(); 485 if (tAlg == null) { 486 fail("TrustManagerFactory default algorithm is not defined"); 487 return; 488 } 489 if (kAlg == null) { 490 fail("KeyManagerFactory default algorithm is not defined"); 491 return; 492 } 493 KeyManagerFactory kmf = KeyManagerFactory.getInstance(kAlg); 494 KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); 495 try { 496 ks.load(null, null); 497 } catch (Exception e) { 498 fail(e + " was thrown for method load(null, null)"); 499 } 500 kmf.init(ks, new char[10]); 501 KeyManager[] kms = kmf.getKeyManagers(); 502 TrustManagerFactory tmf = TrustManagerFactory.getInstance(tAlg); 503 tmf.init(ks); 504 TrustManager[] tms = tmf.getTrustManagers(); 505 for (int i = 0; i < sslC.length; i++) { 506 sslC[i].init(kms, tms, new SecureRandom()); 507 assertNotNull("No SSLServerSocketFactory available", 508 sslC[i].getServerSocketFactory()); 509 assertNotNull("No SSLSocketFactory available", 510 sslC[i].getSocketFactory()); 511 } 512 } 513 514 /** 515 * javax.net.ssl.SSLContext#getSocketFactory() 516 */ 517 public void test_getSocketFactory() throws NoSuchAlgorithmException, 518 KeyManagementException, KeyStoreException, 519 UnrecoverableKeyException { 520 if (!DEFSupported) fail(NotSupportMsg); 521 SSLContext[] sslC = createSSLCon(); 522 assertNotNull("SSLContext objects were not created", sslC); 523 String tAlg = TrustManagerFactory.getDefaultAlgorithm(); 524 String kAlg = KeyManagerFactory.getDefaultAlgorithm(); 525 if (tAlg == null) 526 fail("TrustManagerFactory default algorithm is not defined"); 527 if (kAlg == null) 528 fail("KeyManagerFactory default algorithm is not defined"); 529 KeyManagerFactory kmf = KeyManagerFactory.getInstance(kAlg); 530 kmf.init(null, new char[11]); 531 TrustManagerFactory tmf = TrustManagerFactory.getInstance(tAlg); 532 KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); 533 tmf.init(ks); 534 TrustManager[] tms = tmf.getTrustManagers(); 535 for (SSLContext sslCi : sslC) { 536 sslCi.init(kmf.getKeyManagers(), tms, new SecureRandom()); 537 assertNotNull("Socket factory is incorrectly instantiated", 538 sslCi.getSocketFactory()); 539 } 540 } 541 542 /** 543 * @throws NoSuchAlgorithmException 544 * @throws KeyStoreException 545 * @throws FileNotFoundException 546 * @throws KeyManagementException 547 * javax.net.ssl.SSLContext# 548 * init(javax.net.ssl.KeyManager[], javax.net.ssl.TrustManager[], 549 * java.security.SecureRandom) 550 */ 551 public void test_init$Ljavax_net_ssl_KeyManager$Ljavax_net_ssl_TrustManagerLjava_security_SecureRandom() 552 throws Exception { 553 if (!DEFSupported) fail(NotSupportMsg); 554 SSLContextSpi spi = new MySSLContextSpi(); 555 SSLContext sslContext = new MySslContext(spi, defaultProvider, 556 defaultProtocol); 557 try { 558 sslContext.createSSLEngine(); 559 fail("Expected RuntimeException was not thrown"); 560 } catch (RuntimeException rte) { 561 // expected 562 } 563 564 try { 565 sslContext.init(null, null, null); 566 fail("KeyManagementException wasn't thrown"); 567 } catch (KeyManagementException kme) { 568 //expected 569 } 570 571 try { 572 String tAlg = TrustManagerFactory.getDefaultAlgorithm(); 573 String kAlg = KeyManagerFactory.getDefaultAlgorithm(); 574 if (tAlg == null) 575 fail("TrustManagerFactory default algorithm is not defined"); 576 if (kAlg == null) 577 fail("KeyManagerFactory default algorithm is not defined"); 578 KeyManagerFactory kmf = KeyManagerFactory.getInstance(kAlg); 579 kmf.init(null, new char[11]); 580 TrustManagerFactory tmf = TrustManagerFactory.getInstance(tAlg); 581 KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); 582 tmf.init(ks); 583 TrustManager[] tms = tmf.getTrustManagers(); 584 sslContext.init(kmf.getKeyManagers(), tms, new SecureRandom()); 585 } catch (Exception e) { 586 System.out.println("EE = " + e); 587 } 588 } 589 } 590 591 /** 592 * Addifional class to verify SSLContext constructor 593 */ 594 595 class MySslContext extends SSLContext { 596 public MySslContext(SSLContextSpi spi, Provider prov, String alg) { 597 super(spi, prov, alg); 598 } 599 } 600