src/java.base/share/classes/sun/security/util/UntrustedCertificates.java
author lana
Sun, 14 Jan 2018 22:25:53 -0800
changeset 48536 d7995ed9627d
parent 47216 71c04702a3d5
child 50817 fa1e04811ff6
permissions -rw-r--r--
8194717: JDK10 L10n resource file update - msgdrop 10 Reviewed-by: joehw Contributed-by: li.jiang@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11900
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
     1
/*
16114
83963213d5fd 8007688: Blacklist known bad certificate
valeriep
parents: 11900
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
11900
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
     4
 *
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    10
 *
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    15
 * accompanied this code).
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    16
 *
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    20
 *
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    23
 * questions.
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    24
 */
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    25
package sun.security.util;
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    26
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    27
import java.io.*;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    28
import java.security.AccessController;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    29
import java.security.MessageDigest;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    30
import java.security.NoSuchAlgorithmException;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    31
import java.security.PrivilegedAction;
11900
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    32
import java.security.cert.X509Certificate;
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    33
import java.security.cert.CertificateException;
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    34
import java.util.*;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    35
import sun.security.x509.X509CertImpl;
11900
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    36
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    37
/**
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    38
 * A utility class to check if a certificate is untrusted. This is an internal
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    39
 * mechanism that explicitly marks a certificate as untrusted, normally in the
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    40
 * case that a certificate is known to be used for malicious reasons.
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    41
 *
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    42
 * <b>Attention</b>: This check is NOT meant to replace the standard PKI-defined
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    43
 * validation check, neither is it used as an alternative to CRL.
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    44
 */
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    45
public final class UntrustedCertificates {
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    46
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    47
    private static final Debug debug = Debug.getInstance("certpath");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    48
    private static final String ALGORITHM_KEY = "Algorithm";
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    49
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    50
    private static final Properties props = new Properties();
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    51
    private static final String algorithm;
11900
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    52
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    53
    static {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    54
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    55
            @Override
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    56
            public Void run() {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    57
                File f = new File(System.getProperty("java.home"),
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    58
                        "lib/security/blacklisted.certs");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    59
                try (FileInputStream fin = new FileInputStream(f)) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    60
                    props.load(fin);
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    61
                    // It's said that the fingerprint could contain colons
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    62
                    for (Map.Entry<Object,Object> e: props.entrySet()) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    63
                        e.setValue(stripColons(e.getValue()));
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    64
                    }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    65
                } catch (IOException fnfe) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    66
                    if (debug != null) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    67
                        debug.println("Error parsing blacklisted.certs");
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    68
                    }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    69
                }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    70
                return null;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    71
            }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    72
        });
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    73
        algorithm = props.getProperty(ALGORITHM_KEY);
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    74
    }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    75
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    76
    private static String stripColons(Object input) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    77
        String s = (String)input;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    78
        char[] letters = s.toCharArray();
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    79
        int pos = 0;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    80
        for (int i = 0; i < letters.length; i++) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    81
            if (letters[i] != ':') {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    82
                if (i != pos) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    83
                    letters[pos] = letters[i];
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    84
                }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    85
                pos++;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    86
            }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    87
        }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    88
        if (pos == letters.length) return s;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    89
        else return new String(letters, 0, pos);
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    90
    }
11900
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    91
    /**
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    92
     * Checks if a certificate is untrusted.
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    93
     *
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    94
     * @param cert the certificate to check
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    95
     * @return true if the certificate is untrusted.
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    96
     */
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
    97
    public static boolean isUntrusted(X509Certificate cert) {
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    98
        if (algorithm == null) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
    99
            return false;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
   100
        }
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
   101
        String key;
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
   102
        if (cert instanceof X509CertImpl) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
   103
            key = ((X509CertImpl)cert).getFingerprint(algorithm);
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
   104
        } else {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
   105
            try {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
   106
                key = new X509CertImpl(cert.getEncoded()).getFingerprint(algorithm);
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
   107
            } catch (CertificateException cee) {
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
   108
                return false;
11900
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
   109
            }
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
   110
        }
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
   111
        return props.containsKey(key);
11900
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
   112
    }
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
   113
20177
8ea486a4f36e 8011402: Move blacklisting certificate logic from hard code to data
weijun
parents: 16886
diff changeset
   114
    private UntrustedCertificates() {}
11900
9b1d5bef8038 7123519: problems with certification path
xuelei
parents:
diff changeset
   115
}