1 /* 2 * Copyright (c) 2005, 2015, 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 if (args.length > 0 && "-disable".equals(args[0])) { 42 mode = false; 43 } else { 44 mode = true; 45 } 46 47 Provider p = Security.getProvider("SunMSCAPI"); 48 49 System.out.println("SunMSCAPI provider classname is " + 50 p.getClass().getName()); 51 System.out.println(KEYSTORE_COMPATIBILITY_MODE_PROP + " = " + 52 System.getProperty(KEYSTORE_COMPATIBILITY_MODE_PROP)); 53 54 KeyStore myKeyStore = KeyStore.getInstance("Windows-MY", p); 55 KeyStore myKeyStore2 = KeyStore.getInstance("Windows-MY", p); 56 KeyStore rootKeyStore = KeyStore.getInstance("Windows-ROOT", p); 57 KeyStore rootKeyStore2 = KeyStore.getInstance("Windows-ROOT", p); 58 59 InputStream inStream = new ByteArrayInputStream(new byte[1]); 60 OutputStream outStream = new ByteArrayOutputStream(); 61 char[] password = new char[1]; 62 63 // Checking keystore load operations 64 testLoadStore(myKeyStore, null, null, true); 65 testLoadStore(myKeyStore2, null, password, true); 66 testLoadStore(rootKeyStore, inStream, null, true); 67 testLoadStore(rootKeyStore2, inStream, password, true); 68 69 // Checking keystore store operations 70 testLoadStore(myKeyStore, null, null, false); 71 testLoadStore(myKeyStore2, null, password, false); 72 testLoadStore(rootKeyStore, outStream, null, false); 73 testLoadStore(rootKeyStore2, outStream, password, false); 74 } 75 76 private static void testLoadStore(KeyStore keyStore, Object stream, 77 char[] password, boolean doLoad) throws Exception { 78 79 String streamValue = stream == null ? "null" : "non-null"; 80 String passwordValue = password == null ? "null" : "non-null"; 81 82 System.out.println("Checking " + (doLoad ? "load" : "store") + 83 "(stream=" + streamValue + ", password=" + passwordValue + ")..."); 84 85 try { 86 87 if (doLoad) { 88 keyStore.load((InputStream) stream, password); 89 } else { 90 keyStore.store((OutputStream) stream, password); 91 } 92 93 if (!mode && keyStore != null && password != null) { 94 throw new Exception( 95 "Expected an IOException to be thrown by KeyStore.load"); 96 } 97 98 } catch (IOException ioe) { 99 // When mode=false the exception is expected. 100 if (mode) { 101 throw ioe; 102 } else { 103 System.out.println("caught the expected exception: " + ioe); 104 } 105 106 } catch (KeyStoreException kse) { 107 // store will fail if load has previously failed 108 if (doLoad) { 109 throw kse; 110 } else { 111 System.out.println("caught the expected exception: " + kse); 112 } 113 } 114 } 115 }