< prev index next >

test/sun/security/mscapi/AccessKeyStore.java

Print this page
rev 11258 : 8139436: sun.security.mscapi.KeyStore might load incomplete data
Reviewed-by: vinnie, weijun
   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 AccessKeyStore.sh
  26  */
  27 
  28 import java.security.Provider;
  29 import java.security.*;
  30 import java.security.cert.*;
  31 import java.security.cert.Certificate;
  32 import java.security.interfaces.RSAKey;
  33 import java.util.Enumeration;
  34 
  35 public class AccessKeyStore {
  36 
  37     public static void main(String[] args) throws Exception {
  38 
  39         // Check if the provider is available
  40         try {
  41             Class.forName("sun.security.mscapi.SunMSCAPI");
  42 
  43         } catch (Exception e) {
  44             System.out.println(
  45                 "The SunMSCAPI provider is not available on this platform: " +
  46                 e);
  47             return;
  48         }
  49 
  50         // Check that a security manager has been installed
  51         if (System.getSecurityManager() == null) {
  52             throw new Exception("A security manager has not been installed");
  53         }
  54 
  55         Provider p = Security.getProvider("SunMSCAPI");
  56 
  57         System.out.println("SunMSCAPI provider classname is " +
  58             p.getClass().getName());
  59 
  60         KeyStore keyStore = KeyStore.getInstance("Windows-MY", p);
  61 
  62         /*
  63          * If a SecurityManager exists then this will trigger a
  64          * SecurityException if the following permission has not
  65          * been granted:
  66          *
  67          *     SecurityPermission("authProvider.SunMSCAPI")
  68          */
  69         try {
  70 
  71             keyStore.load(null, null);
  72 
  73             if (args.length > 0 && "-deny".equals(args[0])) {
  74                 throw new Exception(
  75                     "Expected KeyStore.load to throw a SecurityException");
  76             }
  77 
  78         } catch (SecurityException se) {
  79 
  80             if (args.length > 0 && "-deny".equals(args[0])) {
  81                 System.out.println("Caught the expected exception: " + se);
  82                 return;
  83             } else {
  84                 throw se;
  85             }
  86         }
  87 
  88         int i = 0;
  89         for (Enumeration e = keyStore.aliases(); e.hasMoreElements(); ) {
  90             String alias = (String) e.nextElement();
  91             displayEntry(keyStore, alias, i++);
  92         }
  93     }
  94 
  95     private static void displayEntry(KeyStore keyStore, String alias,
  96         int index) throws KeyStoreException, NoSuchAlgorithmException  {
  97 
  98         if (keyStore.isKeyEntry(alias)) {
  99             System.out.println("[" + index + "]\n    " + alias +
 100                 " [key-entry]\n");
 101 
 102             try {
 103 
 104                 Key key = keyStore.getKey(alias, null);
 105 
 106                 if (key instanceof RSAKey) {
 107                     System.out.println("    Key type: " + key.getAlgorithm() +
 108                         " (" + ((RSAKey)key).getModulus().bitLength() +
 109                         " bit)\n");
 110                 } else {


   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 AccessKeyStore.sh
  26  */
  27 
  28 import java.security.Provider;
  29 import java.security.*;
  30 import java.security.cert.*;
  31 import java.security.cert.Certificate;
  32 import java.security.interfaces.RSAKey;
  33 import java.util.Enumeration;
  34 
  35 public class AccessKeyStore {
  36 
  37     public static void main(String[] args) throws Exception {
  38 











  39         // Check that a security manager has been installed
  40         if (System.getSecurityManager() == null) {
  41             throw new Exception("A security manager has not been installed");
  42         }
  43 
  44         Provider p = Security.getProvider("SunMSCAPI");
  45 
  46         System.out.println("SunMSCAPI provider classname is " +
  47             p.getClass().getName());
  48 
  49         KeyStore keyStore = KeyStore.getInstance("Windows-MY", p);
  50 
  51         /*
  52          * If a SecurityManager exists then this will trigger a
  53          * SecurityException if the following permission has not
  54          * been granted:
  55          *
  56          *     SecurityPermission("authProvider.SunMSCAPI")
  57          */
  58         try {
  59 
  60             keyStore.load(null, null);
  61 
  62             if (args.length > 0 && "-deny".equals(args[0])) {
  63                 throw new Exception(
  64                     "Expected KeyStore.load to throw a SecurityException");
  65             }
  66 
  67         } catch (SecurityException se) {
  68 
  69             if (args.length > 0 && "-deny".equals(args[0])) {
  70                 System.out.println("Caught the expected exception: " + se);
  71                 return;
  72             } else {
  73                 throw se;
  74             }
  75         }
  76 
  77         int i = 0;
  78         for (Enumeration<String> e = keyStore.aliases(); e.hasMoreElements(); ) {
  79             String alias = e.nextElement();
  80             displayEntry(keyStore, alias, i++);
  81         }
  82     }
  83 
  84     private static void displayEntry(KeyStore keyStore, String alias,
  85         int index) throws KeyStoreException, NoSuchAlgorithmException  {
  86 
  87         if (keyStore.isKeyEntry(alias)) {
  88             System.out.println("[" + index + "]\n    " + alias +
  89                 " [key-entry]\n");
  90 
  91             try {
  92 
  93                 Key key = keyStore.getKey(alias, null);
  94 
  95                 if (key instanceof RSAKey) {
  96                     System.out.println("    Key type: " + key.getAlgorithm() +
  97                         " (" + ((RSAKey)key).getModulus().bitLength() +
  98                         " bit)\n");
  99                 } else {


< prev index next >