1 /*
   2  * Copyright (c) 2002, 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 /*
  25  * @test
  26  * @bug 4667976
  27  * @compile JavaxSSLContextImpl.java ComSSLContextImpl.java
  28  *      JavaxTrustManagerFactoryImpl.java ComTrustManagerFactoryImpl.java
  29  *      JavaxKeyManagerFactoryImpl.java ComKeyManagerFactoryImpl.java
  30  * @summary brokenness in the com.sun.net.ssl.SSLSecurity wrappers
  31  */
  32 
  33 import java.security.*;
  34 import com.sun.net.ssl.*;
  35 
  36 public class ProviderTest {
  37 
  38     public static void main(String args[]) throws Exception {
  39         SSLContext sslc;
  40         TrustManagerFactory tmf;
  41         KeyManagerFactory kmf;
  42 
  43         Provider extraProvider = new MyProvider();
  44         Security.addProvider(extraProvider);
  45         try {
  46             System.out.println("getting a javax SSLContext");
  47             sslc = SSLContext.getInstance("javax");
  48             sslc.init(null, null, null);
  49             System.out.println("\ngetting a com SSLContext");
  50             sslc = SSLContext.getInstance("com");
  51             sslc.init(null, null, null);
  52 
  53             System.out.println("\ngetting a javax TrustManagerFactory");
  54             tmf = TrustManagerFactory.getInstance("javax");
  55             tmf.init((KeyStore) null);
  56             System.out.println("\ngetting a com TrustManagerFactory");
  57             tmf = TrustManagerFactory.getInstance("com");
  58             tmf.init((KeyStore) null);
  59 
  60             System.out.println("\ngetting a javax KeyManagerFactory");
  61             kmf = KeyManagerFactory.getInstance("javax");
  62             kmf.init((KeyStore) null, null);
  63             System.out.println("\ngetting a com KeyManagerFactory");
  64             kmf = KeyManagerFactory.getInstance("com");
  65             kmf.init((KeyStore) null, null);
  66         } finally {
  67             Security.removeProvider(extraProvider.getName());
  68         }
  69     }
  70 }
  71 
  72 class MyProvider extends Provider {
  73 
  74     private static String info = "Brad's provider";
  75 
  76     /**
  77      * Installs the JSSE provider.
  78      */
  79     public static synchronized void install()
  80     {
  81         /* nop. Remove this method in the future. */
  82     }
  83 
  84     public MyProvider()
  85     {
  86         super("BRAD", 1.0, info);
  87 
  88         AccessController.doPrivileged(new java.security.PrivilegedAction() {
  89             public Object run() {
  90 
  91                 put("SSLContext.javax", "JavaxSSLContextImpl");
  92                 put("SSLContext.com",   "ComSSLContextImpl");
  93                 put("TrustManagerFactory.javax",
  94                                         "JavaxTrustManagerFactoryImpl");
  95                 put("TrustManagerFactory.com",
  96                                         "ComTrustManagerFactoryImpl");
  97                 put("KeyManagerFactory.javax",
  98                                         "JavaxKeyManagerFactoryImpl");
  99                 put("KeyManagerFactory.com",
 100                                         "ComKeyManagerFactoryImpl");
 101 
 102                 return null;
 103             }
 104         });
 105 
 106     }
 107 }