1 /*
   2  * Copyright (c) 2005, 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  * @see KeyStoreCompatibilityMode.sh
  26  */
  27 
  28 import java.io.*;
  29 import java.security.Provider;
  30 import java.security.*;
  31 
  32 public class KeyStoreCompatibilityMode {
  33 
  34     private static final String KEYSTORE_COMPATIBILITY_MODE_PROP =
  35         "sun.security.mscapi.keyStoreCompatibilityMode";
  36 
  37     private static boolean mode;
  38 
  39     public static void main(String[] args) throws Exception {
  40 
  41         // Check if the provider is available
  42         try {
  43             Class.forName("sun.security.mscapi.SunMSCAPI");
  44 
  45         } catch (Exception e) {
  46             System.out.println(
  47                 "The SunMSCAPI provider is not available on this platform: " +
  48                 e);
  49             return;
  50         }
  51 
  52         if (args.length > 0 && "-disable".equals(args[0])) {
  53             mode = false;
  54         } else {
  55             mode = true;
  56         }
  57 
  58         Provider p = Security.getProvider("SunMSCAPI");
  59 
  60         System.out.println("SunMSCAPI provider classname is " +
  61             p.getClass().getName());
  62         System.out.println(KEYSTORE_COMPATIBILITY_MODE_PROP + " = " +
  63             System.getProperty(KEYSTORE_COMPATIBILITY_MODE_PROP));
  64 
  65         KeyStore myKeyStore = KeyStore.getInstance("Windows-MY", p);
  66         KeyStore myKeyStore2 = KeyStore.getInstance("Windows-MY", p);
  67         KeyStore rootKeyStore = KeyStore.getInstance("Windows-ROOT", p);
  68         KeyStore rootKeyStore2 = KeyStore.getInstance("Windows-ROOT", p);
  69 
  70         InputStream inStream = new ByteArrayInputStream(new byte[1]);
  71         OutputStream outStream = new ByteArrayOutputStream();
  72         char[] password = new char[1];
  73 
  74         // Checking keystore load operations
  75         testLoadStore(myKeyStore, null, null, true);
  76         testLoadStore(myKeyStore2, null, password, true);
  77         testLoadStore(rootKeyStore, inStream, null, true);
  78         testLoadStore(rootKeyStore2, inStream, password, true);
  79 
  80         // Checking keystore store operations
  81         testLoadStore(myKeyStore, null, null, false);
  82         testLoadStore(myKeyStore2, null, password, false);
  83         testLoadStore(rootKeyStore, outStream, null, false);
  84         testLoadStore(rootKeyStore2, outStream, password, false);
  85     }
  86 
  87     private static void testLoadStore(KeyStore keyStore, Object stream,
  88         char[] password, boolean doLoad) throws Exception {
  89 
  90         String streamValue = stream == null ? "null" : "non-null";
  91         String passwordValue = password == null ? "null" : "non-null";
  92 
  93         System.out.println("Checking " + (doLoad ? "load" : "store") +
  94             "(stream=" + streamValue + ", password=" + passwordValue + ")...");
  95 
  96         try {
  97 
  98             if (doLoad) {
  99                 keyStore.load((InputStream) stream, password);
 100             } else {
 101                 keyStore.store((OutputStream) stream, password);
 102             }
 103 
 104             if (!mode && keyStore != null && password != null) {
 105                 throw new Exception(
 106                     "Expected an IOException to be thrown by KeyStore.load");
 107             }
 108 
 109         } catch (IOException ioe) {
 110             // When mode=false the exception is expected.
 111             if (mode) {
 112                 throw ioe;
 113             } else {
 114                 System.out.println("caught the expected exception: " + ioe);
 115             }
 116 
 117         } catch (KeyStoreException kse) {
 118             // store will fail if load has previously failed
 119             if (doLoad) {
 120                 throw kse;
 121             } else {
 122                 System.out.println("caught the expected exception: " + kse);
 123             }
 124         }
 125     }
 126 }