jdk/src/share/classes/sun/security/tools/KeyStoreUtil.java
author michaelm
Thu, 24 Oct 2013 20:39:21 +0100
changeset 22339 e91bfaf4360d
parent 14182 3041082abb40
child 24868 89d9bd9eba96
permissions -rw-r--r--
8011786: Better applet networking Reviewed-by: alanb, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
     2
 * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.tools;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    28
import java.io.BufferedReader;
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    29
import java.io.File;
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    30
import java.io.FileInputStream;
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    31
import java.io.IOException;
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    32
import java.io.InputStreamReader;
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    33
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    34
import java.net.URL;
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    35
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    36
import java.security.KeyStore;
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    37
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    38
import java.text.Collator;
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    39
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
    40
import java.util.Locale;
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
    41
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p> This class provides several utilities to <code>KeyStore</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @since 1.6.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class KeyStoreUtil {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private KeyStoreUtil() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        // this class is not meant to be instantiated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    53
    private static final String JKS = "jks";
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    54
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    55
    private static final Collator collator = Collator.getInstance();
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    56
    static {
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    57
        // this is for case insensitive string comparisons
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    58
        collator.setStrength(Collator.PRIMARY);
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    59
    };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Returns true if KeyStore has a password. This is true except for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * MSCAPI KeyStores
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public static boolean isWindowsKeyStore(String storetype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return storetype.equalsIgnoreCase("Windows-MY")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                || storetype.equalsIgnoreCase("Windows-ROOT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Returns standard-looking names for storetype
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public static String niceStoreTypeName(String storetype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (storetype.equalsIgnoreCase("Windows-MY")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            return "Windows-MY";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        } else if(storetype.equalsIgnoreCase("Windows-ROOT")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            return "Windows-ROOT";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        } else {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
    79
            return storetype.toUpperCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    82
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    83
    /**
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    84
     * Returns the keystore with the configured CA certificates.
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    85
     */
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    86
    public static KeyStore getCacertsKeyStore()
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    87
        throws Exception
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    88
    {
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    89
        String sep = File.separator;
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    90
        File file = new File(System.getProperty("java.home") + sep
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    91
                             + "lib" + sep + "security" + sep
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    92
                             + "cacerts");
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    93
        if (!file.exists()) {
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    94
            return null;
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    95
        }
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    96
        KeyStore caks = null;
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    97
        try (FileInputStream fis = new FileInputStream(file)) {
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    98
            caks = KeyStore.getInstance(JKS);
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
    99
            caks.load(fis, null);
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   100
        }
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   101
        return caks;
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   102
    }
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   103
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   104
    public static char[] getPassWithModifier(String modifier, String arg,
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   105
                                             java.util.ResourceBundle rb) {
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   106
        if (modifier == null) {
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   107
            return arg.toCharArray();
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   108
        } else if (collator.compare(modifier, "env") == 0) {
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   109
            String value = System.getenv(arg);
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   110
            if (value == null) {
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   111
                System.err.println(rb.getString(
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   112
                        "Cannot.find.environment.variable.") + arg);
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   113
                return null;
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   114
            } else {
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   115
                return value.toCharArray();
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   116
            }
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   117
        } else if (collator.compare(modifier, "file") == 0) {
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   118
            try {
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   119
                URL url = null;
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   120
                try {
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   121
                    url = new URL(arg);
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   122
                } catch (java.net.MalformedURLException mue) {
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   123
                    File f = new File(arg);
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   124
                    if (f.exists()) {
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   125
                        url = f.toURI().toURL();
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   126
                    } else {
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   127
                        System.err.println(rb.getString(
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   128
                                "Cannot.find.file.") + arg);
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   129
                        return null;
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   130
                    }
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   131
                }
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   132
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   133
                try (BufferedReader br =
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   134
                     new BufferedReader(new InputStreamReader(
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   135
                         url.openStream()))) {
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   136
                    String value = br.readLine();
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   137
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   138
                    if (value == null) {
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   139
                        return new char[0];
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   140
                    }
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   141
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   142
                    return value.toCharArray();
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   143
                }
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   144
            } catch (IOException ioe) {
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   145
                System.err.println(ioe);
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   146
                return null;
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   147
            }
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   148
        } else {
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   149
            System.err.println(rb.getString("Unknown.password.type.") +
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   150
                    modifier);
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   151
            return null;
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   152
        }
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10369
diff changeset
   153
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
}