author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 25859 | jdk/src/java.base/share/classes/sun/security/util/UntrustedCertificates.java@3317bb8137f4 |
child 50817 | fa1e04811ff6 |
permissions | -rw-r--r-- |
11900 | 1 |
/* |
16114 | 2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
11900 | 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 |
||
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 | 32 |
import java.security.cert.X509Certificate; |
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 | 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 |
||
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 | 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 | 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) { |
|
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 | 109 |
} |
110 |
} |
|
20177
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
16886
diff
changeset
|
111 |
return props.containsKey(key); |
11900 | 112 |
} |
113 |
||
20177
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
16886
diff
changeset
|
114 |
private UntrustedCertificates() {} |
11900 | 115 |
} |