< prev index next >

src/share/classes/sun/security/tools/KeyStoreUtil.java

Print this page
rev 1513 : 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
Reviewed-by: alanb, weijun, mullan
   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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.tools;
  27 












  28 /**
  29  * <p> This class provides several utilities to <code>KeyStore</code>.
  30  *
  31  * @since 1.6.0
  32  */
  33 public class KeyStoreUtil {
  34 
  35     // Class and methods marked as public so that they can be
  36     // accessed by JarSigner, which although lies in a package
  37     // with the same name, but bundled in tools.jar and loaded
  38     // by another class loader, hence in a different *runtime*
  39     // package.
  40     //
  41     // See JVM Spec, 5.3 and 5.4.4
  42 
  43     private KeyStoreUtil() {
  44         // this class is not meant to be instantiated
  45     }
  46 







  47 
  48     /**
  49      * Returns true if KeyStore has a password. This is true except for
  50      * MSCAPI KeyStores
  51      */
  52     public static boolean isWindowsKeyStore(String storetype) {
  53         return storetype.equalsIgnoreCase("Windows-MY")
  54                 || storetype.equalsIgnoreCase("Windows-ROOT");
  55     }
  56 
  57     /**
  58      * Returns standard-looking names for storetype
  59      */
  60     public static String niceStoreTypeName(String storetype) {
  61         if (storetype.equalsIgnoreCase("Windows-MY")) {
  62             return "Windows-MY";
  63         } else if(storetype.equalsIgnoreCase("Windows-ROOT")) {
  64             return "Windows-ROOT";
  65         } else {
  66             return storetype.toUpperCase();














































































  67         }
  68     }
  69 }
   1 /*
   2  * Copyright (c) 2005, 2012, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.tools;
  27 
  28 import java.io.BufferedReader;
  29 import java.io.File;
  30 import java.io.FileInputStream;
  31 import java.io.IOException;
  32 import java.io.InputStreamReader;
  33 
  34 import java.net.URL;
  35 
  36 import java.security.KeyStore;
  37 
  38 import java.text.Collator;
  39 
  40 /**
  41  * <p> This class provides several utilities to <code>KeyStore</code>.
  42  *
  43  * @since 1.6.0
  44  */
  45 public class KeyStoreUtil {
  46 








  47     private KeyStoreUtil() {
  48         // this class is not meant to be instantiated
  49     }
  50 
  51     private static final String JKS = "jks";
  52 
  53     private static final Collator collator = Collator.getInstance();
  54     static {
  55         // this is for case insensitive string comparisons
  56         collator.setStrength(Collator.PRIMARY);
  57     };
  58 
  59     /**
  60      * Returns true if KeyStore has a password. This is true except for
  61      * MSCAPI KeyStores
  62      */
  63     public static boolean isWindowsKeyStore(String storetype) {
  64         return storetype.equalsIgnoreCase("Windows-MY")
  65                 || storetype.equalsIgnoreCase("Windows-ROOT");
  66     }
  67 
  68     /**
  69      * Returns standard-looking names for storetype
  70      */
  71     public static String niceStoreTypeName(String storetype) {
  72         if (storetype.equalsIgnoreCase("Windows-MY")) {
  73             return "Windows-MY";
  74         } else if(storetype.equalsIgnoreCase("Windows-ROOT")) {
  75             return "Windows-ROOT";
  76         } else {
  77             return storetype.toUpperCase();
  78         }
  79     }
  80 
  81     /**
  82      * Returns the keystore with the configured CA certificates.
  83      */
  84     public static KeyStore getCacertsKeyStore()
  85         throws Exception
  86     {
  87         String sep = File.separator;
  88         File file = new File(System.getProperty("java.home") + sep
  89                              + "lib" + sep + "security" + sep
  90                              + "cacerts");
  91         if (!file.exists()) {
  92             return null;
  93         }
  94         FileInputStream fis = null;
  95         KeyStore caks = null;
  96         try {
  97             fis = new FileInputStream(file);
  98             caks = KeyStore.getInstance(JKS);
  99             caks.load(fis, null);
 100         } finally {
 101             if (fis != null) { fis.close(); }
 102         }
 103         return caks;
 104     }
 105 
 106     public static char[] getPassWithModifier(String modifier, String arg,
 107                                              java.util.ResourceBundle rb) {
 108         if (modifier == null) {
 109             return arg.toCharArray();
 110         } else if (collator.compare(modifier, "env") == 0) {
 111             String value = System.getenv(arg);
 112             if (value == null) {
 113                 System.err.println(rb.getString(
 114                         "Cannot.find.environment.variable.") + arg);
 115                 return null;
 116             } else {
 117                 return value.toCharArray();
 118             }
 119         } else if (collator.compare(modifier, "file") == 0) {
 120             try {
 121                 URL url = null;
 122                 try {
 123                     url = new URL(arg);
 124                 } catch (java.net.MalformedURLException mue) {
 125                     File f = new File(arg);
 126                     if (f.exists()) {
 127                         url = f.toURI().toURL();
 128                     } else {
 129                         System.err.println(rb.getString(
 130                                 "Cannot.find.file.") + arg);
 131                         return null;
 132                     }
 133                 }
 134 
 135                 BufferedReader br = null;
 136                 try {
 137                     br = new BufferedReader(new InputStreamReader(url.openStream()));
 138                     String value = br.readLine();
 139 
 140                     if (value == null) {
 141                         return new char[0];
 142                     }
 143 
 144                     return value.toCharArray();
 145                 } finally {
 146                     if (br != null) { br.close(); }
 147                 }
 148             } catch (IOException ioe) {
 149                 System.err.println(ioe);
 150                 return null;
 151             }
 152         } else {
 153             System.err.println(rb.getString("Unknown.password.type.") +
 154                     modifier);
 155             return null;
 156         }
 157     }
 158 }
< prev index next >