test/jdk/lib/security/CheckBlacklistedCerts.java
author mikael
Tue, 02 Oct 2018 22:36:08 -0700
changeset 51994 7577686cc9bd
parent 47216 71c04702a3d5
child 52075 40aa2d50d116
permissions -rw-r--r--
8211350: Remove jprt support Reviewed-by: dholmes, erikj, mchung, alanb, jjg
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
/*
51994
7577686cc9bd 8211350: Remove jprt support
mikael
parents: 47216
diff changeset
     2
 * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
20177
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
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 25409
diff changeset
    28
 * @modules java.base/sun.security.util
20177
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
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    31
import sun.security.util.UntrustedCertificates;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    32
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    33
import java.io.*;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    34
import java.security.KeyStore;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    35
import java.security.cert.*;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    36
import java.util.*;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    37
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    38
public class CheckBlacklistedCerts {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    39
    public static void main(String[] args) throws Exception {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    40
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    41
        String home = System.getProperty("java.home");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    42
        boolean failed = false;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    43
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    44
        // Root CAs should always be trusted
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    45
        File file = new File(home, "lib/security/cacerts");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    46
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    47
        try (FileInputStream fis = new FileInputStream(file)) {
30902
cf3d869e9f79 8081565: javac lint warnings in jdk testlibrary
rriggs
parents: 30820
diff changeset
    48
            ks.load(fis, null);
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    49
        }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    50
        System.out.println("Check for cacerts: " + ks.size());
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    51
        for (String alias: Collections.list(ks.aliases())) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    52
            X509Certificate cert = (X509Certificate)ks.getCertificate(alias);
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    53
            if (UntrustedCertificates.isUntrusted(cert)) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    54
                System.out.print(alias + " is untrusted");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    55
                failed = true;
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
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    59
        // All certs in the pem files
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    60
        Set<Certificate> blacklisted = new HashSet<>();
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    61
51994
7577686cc9bd 8211350: Remove jprt support
mikael
parents: 47216
diff changeset
    62
        // Assumes the full src is available
20177
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?
31184
1ed0333a7723 8060103: CheckBlacklistedCerts.java thinks its openjdk build
rhalade
parents: 30902
diff changeset
    71
        String prop = System.getProperty("java.runtime.name");
1ed0333a7723 8060103: CheckBlacklistedCerts.java thinks its openjdk build
rhalade
parents: 30902
diff changeset
    72
        if (prop != null && prop.startsWith("OpenJDK")) {
1ed0333a7723 8060103: CheckBlacklistedCerts.java thinks its openjdk build
rhalade
parents: 30902
diff changeset
    73
            System.out.println("This is a OpenJDK build.");
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    74
            blacklists = Arrays.copyOf(blacklists, 1);
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    75
        }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    76
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    77
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    78
        for (File blacklist: blacklists) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    79
            System.out.print("Check for " + blacklist + ": ");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    80
            if (!blacklist.exists()) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    81
                System.out.println("does not exist");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    82
            } else {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    83
                try (FileInputStream fis = new FileInputStream(blacklist)) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    84
                    Collection<? extends Certificate> certs
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    85
                            = cf.generateCertificates(fis);
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    86
                    System.out.println(certs.size());
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    87
                    for (Certificate c: certs) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    88
                        blacklisted.add(c);
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    89
                        X509Certificate cert = ((X509Certificate)c);
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    90
                        if (!UntrustedCertificates.isUntrusted(cert)) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    91
                            System.out.println(cert.getSubjectDN() + " is trusted");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    92
                            failed = true;
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
        }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    98
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
    99
        // Check the blacklisted.certs file itself
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   100
        file = new File(home, "lib/security/blacklisted.certs");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   101
        System.out.print("Check for " + file + ": ");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   102
        try (BufferedReader reader = new BufferedReader(
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   103
                new InputStreamReader(new FileInputStream(file)))) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   104
            int acount = 0;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   105
            int ccount = 0;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   106
            while (true) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   107
                String line = reader.readLine();
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   108
                if (line == null) break;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   109
                if (line.startsWith("Algorithm")) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   110
                    acount++;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   111
                } else if (!line.isEmpty() && !line.startsWith("#")) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   112
                    ccount++;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   113
                }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   114
            }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   115
            System.out.println(acount + " algs, " + ccount + " certs" );
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   116
            if (acount != 1) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   117
                System.out.println("There are " + acount + " algorithms");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   118
                failed = true;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   119
            }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   120
            if (ccount != blacklisted.size()
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   121
                    && !blacklisted.isEmpty()) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   122
                System.out.println("Wrong blacklisted.certs size: "
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   123
                        + ccount + " fingerprints, "
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   124
                        + blacklisted.size() + " certs");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   125
                failed = true;
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
        }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   128
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   129
        if (failed) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   130
            throw new Exception("Failed");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   131
        }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   132
    }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff changeset
   133
}