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