jdk/src/java.base/share/classes/sun/security/validator/KeyStores.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 25859 3317bb8137f4
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2002, 2006, 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.validator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.action.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Collection of static utility methods related to KeyStores.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class KeyStores {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private KeyStores() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // in the future, all accesses to the system cacerts keystore should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // go through this class. but not right now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/*
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
    51
    private static final String javaHome =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        (String)AccessController.doPrivileged(new GetPropertyAction("java.home"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
    54
    private static final char SEP = File.separatorChar;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static KeyStore caCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static KeyStore getKeyStore(String type, String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            char[] password) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (type == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            type = "JKS";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            KeyStore ks = KeyStore.getInstance(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            FileInputStream in = (FileInputStream)AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                                        (new OpenFileInputStreamAction(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            ks.load(in, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            return ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            // XXX
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            throw new IOException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            throw (IOException)e.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Return a KeyStore with the contents of the lib/security/cacerts file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * The file is only opened once per JVM invocation and the contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * cached subsequently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
    82
    public static synchronized KeyStore getCaCerts() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (caCerts != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            return caCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        String name = javaHome + SEP + "lib" + SEP + "security" + SEP + "cacerts";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        caCerts = getKeyStore(null, name, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return caCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Return a Set with all trusted X509Certificates contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * this KeyStore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public static Set<X509Certificate> getTrustedCerts(KeyStore ks) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        Set<X509Certificate> set = new HashSet<X509Certificate>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            for (Enumeration<String> e = ks.aliases(); e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                String alias = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                if (ks.isCertificateEntry(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    Certificate cert = ks.getCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    if (cert instanceof X509Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                        set.add((X509Certificate)cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                } else if (ks.isKeyEntry(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    Certificate[] certs = ks.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    if ((certs != null) && (certs.length > 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                            (certs[0] instanceof X509Certificate)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        set.add((X509Certificate)certs[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        } catch (KeyStoreException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
}