jdk/test/lib/security/CheckBlacklistedCerts.java
author ihse
Wed, 25 Feb 2015 14:48:41 +0100
changeset 29064 d5e775680e97
parent 25409 ce5485fce3d2
child 30820 0d4717a011d3
permissions -rw-r--r--
8073862: BASIC_FIXUP_EXECUTABLE should not fail on empty path Reviewed-by: erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
     1
/*
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
     4
 *
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
     8
 *
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    13
 * accompanied this code).
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    14
 *
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    18
 *
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    21
 * questions.
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    22
 */
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    23
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    24
/*
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    25
 * @test
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    26
 * @bug 8011402
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    27
 * @summary Move blacklisting certificate logic from hard code to data
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    28
 */
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    29
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    30
import sun.security.util.UntrustedCertificates;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    31
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    32
import java.io.*;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    33
import java.security.KeyStore;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    34
import java.security.cert.*;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    35
import java.util.*;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    36
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    37
public class CheckBlacklistedCerts {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    38
    public static void main(String[] args) throws Exception {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    39
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    40
        String home = System.getProperty("java.home");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    41
        boolean failed = false;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    42
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    43
        // Root CAs should always be trusted
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    44
        File file = new File(home, "lib/security/cacerts");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    45
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    46
        try (FileInputStream fis = new FileInputStream(file)) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    47
            ks.load(new FileInputStream(file), null);
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    48
        }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    49
        System.out.println("Check for cacerts: " + ks.size());
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    50
        for (String alias: Collections.list(ks.aliases())) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    51
            X509Certificate cert = (X509Certificate)ks.getCertificate(alias);
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    52
            if (UntrustedCertificates.isUntrusted(cert)) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    53
                System.out.print(alias + " is untrusted");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    54
                failed = true;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    55
            }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    56
        }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    57
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    58
        // All certs in the pem files
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    59
        Set<Certificate> blacklisted = new HashSet<>();
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    60
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    61
        // Hopefully src comes with test, but it might be missing if doing
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    62
        // a -testonly JPRT job.
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    63
        File[] blacklists = {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    64
            new File(System.getProperty("test.src"),
25409
ce5485fce3d2 8049936: Update the CheckBlacklistedCerts.java test to find new location of blacklisted.certs.pem
weijun
parents: 20177
diff changeset
    65
                "../../../make/data/blacklistedcertsconverter/blacklisted.certs.pem"),
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    66
            new File(System.getProperty("test.src"),
25409
ce5485fce3d2 8049936: Update the CheckBlacklistedCerts.java test to find new location of blacklisted.certs.pem
weijun
parents: 20177
diff changeset
    67
                "../../../make/closed/data/blacklistedcertsconverter/blacklisted.certs.pem")
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    68
        };
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    69
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    70
        // Is this an OPENJDK build?
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    71
        if (!new File(home, "lib/security/local_policy.jar").exists()) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    72
            blacklists = Arrays.copyOf(blacklists, 1);
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    73
        }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    74
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    75
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    76
        for (File blacklist: blacklists) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    77
            System.out.print("Check for " + blacklist + ": ");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    78
            if (!blacklist.exists()) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    79
                System.out.println("does not exist");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    80
            } else {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    81
                try (FileInputStream fis = new FileInputStream(blacklist)) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    82
                    Collection<? extends Certificate> certs
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    83
                            = cf.generateCertificates(fis);
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    84
                    System.out.println(certs.size());
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    85
                    for (Certificate c: certs) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    86
                        blacklisted.add(c);
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    87
                        X509Certificate cert = ((X509Certificate)c);
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    88
                        if (!UntrustedCertificates.isUntrusted(cert)) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    89
                            System.out.println(cert.getSubjectDN() + " is trusted");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    90
                            failed = true;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    91
                        }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    92
                    }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    93
                }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    94
            }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    95
        }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    96
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    97
        // Check the blacklisted.certs file itself
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    98
        file = new File(home, "lib/security/blacklisted.certs");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    99
        System.out.print("Check for " + file + ": ");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   100
        try (BufferedReader reader = new BufferedReader(
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   101
                new InputStreamReader(new FileInputStream(file)))) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   102
            int acount = 0;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   103
            int ccount = 0;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   104
            while (true) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   105
                String line = reader.readLine();
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   106
                if (line == null) break;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   107
                if (line.startsWith("Algorithm")) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   108
                    acount++;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   109
                } else if (!line.isEmpty() && !line.startsWith("#")) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   110
                    ccount++;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   111
                }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   112
            }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   113
            System.out.println(acount + " algs, " + ccount + " certs" );
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   114
            if (acount != 1) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   115
                System.out.println("There are " + acount + " algorithms");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   116
                failed = true;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   117
            }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   118
            if (ccount != blacklisted.size()
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   119
                    && !blacklisted.isEmpty()) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   120
                System.out.println("Wrong blacklisted.certs size: "
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   121
                        + ccount + " fingerprints, "
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   122
                        + blacklisted.size() + " certs");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   123
                failed = true;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   124
            }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   125
        }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   126
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   127
        if (failed) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   128
            throw new Exception("Failed");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   129
        }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   130
    }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   131
}