0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @see KeyStoreCompatibilityMode.sh
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.security.Provider;
0N/Aimport java.security.*;
0N/A
0N/Apublic class KeyStoreCompatibilityMode {
0N/A
0N/A private static final String KEYSTORE_COMPATIBILITY_MODE_PROP =
0N/A "sun.security.mscapi.keyStoreCompatibilityMode";
0N/A
0N/A private static boolean mode;
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A // Check if the provider is available
0N/A try {
0N/A Class.forName("sun.security.mscapi.SunMSCAPI");
0N/A
0N/A } catch (Exception e) {
0N/A System.out.println(
0N/A "The SunMSCAPI provider is not available on this platform: " +
0N/A e);
0N/A return;
0N/A }
0N/A
0N/A if (args.length > 0 && "-disable".equals(args[0])) {
0N/A mode = false;
0N/A } else {
0N/A mode = true;
0N/A }
0N/A
0N/A Provider p = Security.getProvider("SunMSCAPI");
0N/A
0N/A System.out.println("SunMSCAPI provider classname is " +
0N/A p.getClass().getName());
0N/A System.out.println(KEYSTORE_COMPATIBILITY_MODE_PROP + " = " +
0N/A System.getProperty(KEYSTORE_COMPATIBILITY_MODE_PROP));
0N/A
0N/A KeyStore myKeyStore = KeyStore.getInstance("Windows-MY", p);
0N/A KeyStore myKeyStore2 = KeyStore.getInstance("Windows-MY", p);
0N/A KeyStore rootKeyStore = KeyStore.getInstance("Windows-ROOT", p);
0N/A KeyStore rootKeyStore2 = KeyStore.getInstance("Windows-ROOT", p);
0N/A
0N/A InputStream inStream = new ByteArrayInputStream(new byte[1]);
0N/A OutputStream outStream = new ByteArrayOutputStream();
0N/A char[] password = new char[1];
0N/A
0N/A // Checking keystore load operations
0N/A testLoadStore(myKeyStore, null, null, true);
0N/A testLoadStore(myKeyStore2, null, password, true);
0N/A testLoadStore(rootKeyStore, inStream, null, true);
0N/A testLoadStore(rootKeyStore2, inStream, password, true);
0N/A
0N/A // Checking keystore store operations
0N/A testLoadStore(myKeyStore, null, null, false);
0N/A testLoadStore(myKeyStore2, null, password, false);
0N/A testLoadStore(rootKeyStore, outStream, null, false);
0N/A testLoadStore(rootKeyStore2, outStream, password, false);
0N/A }
0N/A
0N/A private static void testLoadStore(KeyStore keyStore, Object stream,
0N/A char[] password, boolean doLoad) throws Exception {
0N/A
0N/A String streamValue = stream == null ? "null" : "non-null";
0N/A String passwordValue = password == null ? "null" : "non-null";
0N/A
0N/A System.out.println("Checking " + (doLoad ? "load" : "store") +
0N/A "(stream=" + streamValue + ", password=" + passwordValue + ")...");
0N/A
0N/A try {
0N/A
0N/A if (doLoad) {
0N/A keyStore.load((InputStream) stream, password);
0N/A } else {
0N/A keyStore.store((OutputStream) stream, password);
0N/A }
0N/A
0N/A if (!mode && keyStore != null && password != null) {
0N/A throw new Exception(
0N/A "Expected an IOException to be thrown by KeyStore.load");
0N/A }
0N/A
0N/A } catch (IOException ioe) {
0N/A // When mode=false the exception is expected.
0N/A if (mode) {
0N/A throw ioe;
0N/A } else {
0N/A System.out.println("caught the expected exception: " + ioe);
0N/A }
0N/A
0N/A } catch (KeyStoreException kse) {
0N/A // store will fail if load has previously failed
0N/A if (doLoad) {
0N/A throw kse;
0N/A } else {
0N/A System.out.println("caught the expected exception: " + kse);
0N/A }
0N/A }
0N/A }
0N/A}