Path: blob/master/test/jdk/sun/security/mscapi/AccessKeyStore.java
41152 views
/*1* Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 6324295 6931562 815411326* @modules jdk.crypto.mscapi27* @run main/othervm/java.security.policy==access.policy AccessKeyStore pass28* @run main/othervm/java.security.policy==noaccess.policy AccessKeyStore fail29* @summary Confirm that right permissions are granted to access keystores.30*/3132import java.security.Provider;33import java.security.*;34import java.security.cert.*;35import java.security.cert.Certificate;36import java.security.interfaces.RSAKey;37import java.util.Enumeration;3839public class AccessKeyStore {4041public static void main(String[] args) throws Exception {4243// Check for security manager and required arg(s)44if (System.getSecurityManager() == null) {45throw new Exception("Missing security manager");46}47if (args.length <= 0) {48throw new Exception("Missing expected test status");49}50boolean shouldPass = args[0].equalsIgnoreCase("pass");5152Provider p = Security.getProvider("SunMSCAPI");53System.out.println("SunMSCAPI provider classname is " +54p.getClass().getName());5556KeyStore keyStore = KeyStore.getInstance("Windows-MY", p);5758/*59* If a SecurityManager exists then this will trigger a60* SecurityException if the following permission has not61* been granted:62*63* SecurityPermission("authProvider.SunMSCAPI")64*/65try {66keyStore.load(null, null);67if (!shouldPass) {68throw new Exception(69"Expected KeyStore.load to throw a SecurityException");70}71} catch (SecurityException se) {72if (!shouldPass) {73System.out.println("Expected exception thrown: " + se);74return;75} else {76throw se;77}78}7980int i = 0;81for (Enumeration<String> e = keyStore.aliases(); e.hasMoreElements(); ) {82String alias = e.nextElement();83displayEntry(keyStore, alias, i++);84}85}8687private static void displayEntry(KeyStore keyStore, String alias,88int index) throws KeyStoreException, NoSuchAlgorithmException {8990if (keyStore.isKeyEntry(alias)) {91System.out.println("[" + index + "]\n " + alias +92" [key-entry]\n");9394try {9596Key key = keyStore.getKey(alias, null);9798if (key instanceof RSAKey) {99System.out.println(" Key type: " + key.getAlgorithm() +100" (" + ((RSAKey)key).getModulus().bitLength() +101" bit)\n");102} else {103System.out.println(" Key type: " + key.getAlgorithm() +104"\n");105}106107} catch (UnrecoverableKeyException e) {108System.out.println(" Key type: Unknown\n");109}110111Certificate[] chain = keyStore.getCertificateChain(alias);112if (chain != null) {113System.out.println(" Certificate chain: ");114for (int i = 0; i < chain.length; i ++) {115System.out.println(" ["+ (i + 1) + "]");116displayCert(chain[i], " ");117}118}119120} else {121System.out.println("[" + index + "]\n " + alias +122" [trusted-cert-entry]\n");123Certificate[] chain = keyStore.getCertificateChain(alias);124if (chain != null) {125System.out.println(" Certificate chain: ");126for (int i = 0; i < chain.length; i ++) {127System.out.println(" ["+ (i + 1) + "]");128displayCert(chain[i], " ");129}130}131}132System.out.println("-------------------------------------------------");133}134135private static void displayCert(Certificate cert, String tab) {136if (cert instanceof X509Certificate) {137X509Certificate x = (X509Certificate) cert;138System.out.println(139tab + "Owner: " + x.getSubjectDN().toString() + "\n" +140tab + "Issuer: " + x.getIssuerDN().toString() + "\n" +141tab + "Serial number: " + x.getSerialNumber().toString(16) +142"\n"+143tab + "Valid from: " + x.getNotBefore().toString() + "\n" +144tab + " until: " + x.getNotAfter().toString());145} else {146System.out.println(tab + "[unknown certificate format]");147}148System.out.println();149}150}151152153