Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/mscapi/AccessKeyStore.java
38840 views
/*1* Copyright (c) 2005, 2015, 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* @see AccessKeyStore.sh25*/2627import java.security.Provider;28import java.security.*;29import java.security.cert.*;30import java.security.cert.Certificate;31import java.security.interfaces.RSAKey;32import java.util.Enumeration;3334public class AccessKeyStore {3536public static void main(String[] args) throws Exception {3738// Check that a security manager has been installed39if (System.getSecurityManager() == null) {40throw new Exception("A security manager has not been installed");41}4243Provider p = Security.getProvider("SunMSCAPI");4445System.out.println("SunMSCAPI provider classname is " +46p.getClass().getName());4748KeyStore keyStore = KeyStore.getInstance("Windows-MY", p);4950/*51* If a SecurityManager exists then this will trigger a52* SecurityException if the following permission has not53* been granted:54*55* SecurityPermission("authProvider.SunMSCAPI")56*/57try {5859keyStore.load(null, null);6061if (args.length > 0 && "-deny".equals(args[0])) {62throw new Exception(63"Expected KeyStore.load to throw a SecurityException");64}6566} catch (SecurityException se) {6768if (args.length > 0 && "-deny".equals(args[0])) {69System.out.println("Caught the expected exception: " + se);70return;71} else {72throw se;73}74}7576int i = 0;77for (Enumeration<String> e = keyStore.aliases(); e.hasMoreElements(); ) {78String alias = e.nextElement();79displayEntry(keyStore, alias, i++);80}81}8283private static void displayEntry(KeyStore keyStore, String alias,84int index) throws KeyStoreException, NoSuchAlgorithmException {8586if (keyStore.isKeyEntry(alias)) {87System.out.println("[" + index + "]\n " + alias +88" [key-entry]\n");8990try {9192Key key = keyStore.getKey(alias, null);9394if (key instanceof RSAKey) {95System.out.println(" Key type: " + key.getAlgorithm() +96" (" + ((RSAKey)key).getModulus().bitLength() +97" bit)\n");98} else {99System.out.println(" Key type: " + key.getAlgorithm() +100"\n");101}102103} catch (UnrecoverableKeyException e) {104System.out.println(" Key type: Unknown\n");105}106107Certificate[] chain = keyStore.getCertificateChain(alias);108if (chain != null) {109System.out.println(" Certificate chain: ");110for (int i = 0; i < chain.length; i ++) {111System.out.println(" ["+ (i + 1) + "]");112displayCert(chain[i], " ");113}114}115116} else {117System.out.println("[" + index + "]\n " + alias +118" [trusted-cert-entry]\n");119Certificate[] chain = keyStore.getCertificateChain(alias);120if (chain != null) {121System.out.println(" Certificate chain: ");122for (int i = 0; i < chain.length; i ++) {123System.out.println(" ["+ (i + 1) + "]");124displayCert(chain[i], " ");125}126}127}128System.out.println("-------------------------------------------------");129}130131private static void displayCert(Certificate cert, String tab) {132if (cert instanceof X509Certificate) {133X509Certificate x = (X509Certificate) cert;134System.out.println(135tab + "Owner: " + x.getSubjectDN().toString() + "\n" +136tab + "Issuer: " + x.getIssuerDN().toString() + "\n" +137tab + "Serial number: " + x.getSerialNumber().toString(16) +138"\n"+139tab + "Valid from: " + x.getNotBefore().toString() + "\n" +140tab + " until: " + x.getNotAfter().toString());141} else {142System.out.println(tab + "[unknown certificate format]");143}144System.out.println();145}146}147148149