jdk/src/java.base/share/classes/sun/security/util/AnchorCertificates.java
author ascarpino
Mon, 10 Oct 2016 13:28:44 -0700
changeset 41379 509c0e9a6b47
parent 37726 bbecfff95ec3
child 41973 02ccd977c632
permissions -rw-r--r--
8165101: AnchorCertificates throws NPE when cacerts file not found Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
     1
/*
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
     4
 *
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    10
 *
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    15
 * accompanied this code).
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    16
 *
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    20
 *
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    23
 * questions.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    24
 */
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    25
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    26
package sun.security.util;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    27
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    28
import java.io.File;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    29
import java.io.FileInputStream;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    30
import java.security.AccessController;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    31
import java.security.KeyStore;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    32
import java.security.PrivilegedAction;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    33
import java.security.cert.X509Certificate;
41379
509c0e9a6b47 8165101: AnchorCertificates throws NPE when cacerts file not found
ascarpino
parents: 37726
diff changeset
    34
import java.util.Collections;
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    35
import java.util.Enumeration;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    36
import java.util.HashSet;
41379
509c0e9a6b47 8165101: AnchorCertificates throws NPE when cacerts file not found
ascarpino
parents: 37726
diff changeset
    37
import java.util.Set;
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    38
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    39
import sun.security.x509.X509CertImpl;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    40
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    41
/**
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    42
 * The purpose of this class is to determine the trust anchor certificates is in
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    43
 * the cacerts file.  This is used for PKIX CertPath checking.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    44
 */
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    45
public class AnchorCertificates {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    46
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    47
    private static final Debug debug = Debug.getInstance("certpath");
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    48
    private static final String HASH = "SHA-256";
41379
509c0e9a6b47 8165101: AnchorCertificates throws NPE when cacerts file not found
ascarpino
parents: 37726
diff changeset
    49
    private static Set<String> certs = Collections.emptySet();
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    50
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    51
    static  {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    52
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    53
            @Override
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    54
            public Void run() {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    55
                File f = new File(System.getProperty("java.home"),
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    56
                        "lib/security/cacerts");
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    57
                KeyStore cacerts;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    58
                try {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    59
                    cacerts = KeyStore.getInstance("JKS");
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    60
                    try (FileInputStream fis = new FileInputStream(f)) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    61
                        cacerts.load(fis, "changeit".toCharArray());
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    62
                        certs = new HashSet<>();
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    63
                        Enumeration<String> list = cacerts.aliases();
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    64
                        String alias;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    65
                        while (list.hasMoreElements()) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    66
                            alias = list.nextElement();
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    67
                            // Check if this cert is labeled a trust anchor.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    68
                            if (alias.contains(" [jdk")) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    69
                                X509Certificate cert = (X509Certificate) cacerts
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    70
                                        .getCertificate(alias);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    71
                                certs.add(X509CertImpl.getFingerprint(HASH, cert));
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    72
                            }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    73
                        }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    74
                    }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    75
                } catch (Exception e) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    76
                    if (debug != null) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    77
                        debug.println("Error parsing cacerts");
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    78
                    }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    79
                    e.printStackTrace();
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    80
                }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    81
                return null;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    82
            }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    83
        });
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    84
    }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    85
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    86
    /**
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    87
     * Checks if a certificate is a trust anchor.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    88
     *
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    89
     * @param cert the certificate to check
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    90
     * @return true if the certificate is trusted.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    91
     */
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    92
    public static boolean contains(X509Certificate cert) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    93
        String key = X509CertImpl.getFingerprint(HASH, cert);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    94
        boolean result = certs.contains(key);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    95
        if (result && debug != null) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    96
            debug.println("AnchorCertificate.contains: matched " +
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    97
                    cert.getSubjectDN());
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    98
        }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
    99
        return result;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
   100
    }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
   101
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
   102
    private AnchorCertificates() {}
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents:
diff changeset
   103
}