author | iignatyev |
Thu, 18 May 2017 10:22:19 -0700 | |
changeset 45160 | ec88d39f0eb1 |
parent 31184 | 1ed0333a7723 |
permissions | -rw-r--r-- |
20177
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
1 |
/* |
31184
1ed0333a7723
8060103: CheckBlacklistedCerts.java thinks its openjdk build
rhalade
parents:
30902
diff
changeset
|
2 |
* Copyright (c) 2013, 2015, 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 | 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 | 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 |
|
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
62 |
// 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
|
63 |
// a -testonly JPRT job. |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
64 |
File[] blacklists = { |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
65 |
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
|
66 |
"../../../make/data/blacklistedcertsconverter/blacklisted.certs.pem"), |
20177
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
67 |
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
|
68 |
"../../../make/closed/data/blacklistedcertsconverter/blacklisted.certs.pem") |
20177
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 |
|
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
71 |
// Is this an OPENJDK build? |
31184
1ed0333a7723
8060103: CheckBlacklistedCerts.java thinks its openjdk build
rhalade
parents:
30902
diff
changeset
|
72 |
String prop = System.getProperty("java.runtime.name"); |
1ed0333a7723
8060103: CheckBlacklistedCerts.java thinks its openjdk build
rhalade
parents:
30902
diff
changeset
|
73 |
if (prop != null && prop.startsWith("OpenJDK")) { |
1ed0333a7723
8060103: CheckBlacklistedCerts.java thinks its openjdk build
rhalade
parents:
30902
diff
changeset
|
74 |
System.out.println("This is a OpenJDK build."); |
20177
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
75 |
blacklists = Arrays.copyOf(blacklists, 1); |
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 |
|
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
78 |
CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
79 |
for (File blacklist: blacklists) { |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
80 |
System.out.print("Check for " + blacklist + ": "); |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
81 |
if (!blacklist.exists()) { |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
82 |
System.out.println("does not exist"); |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
83 |
} else { |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
84 |
try (FileInputStream fis = new FileInputStream(blacklist)) { |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
85 |
Collection<? extends Certificate> certs |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
86 |
= cf.generateCertificates(fis); |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
87 |
System.out.println(certs.size()); |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
88 |
for (Certificate c: certs) { |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
89 |
blacklisted.add(c); |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
90 |
X509Certificate cert = ((X509Certificate)c); |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
91 |
if (!UntrustedCertificates.isUntrusted(cert)) { |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
92 |
System.out.println(cert.getSubjectDN() + " is trusted"); |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
93 |
failed = true; |
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 |
|
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
100 |
// Check the blacklisted.certs file itself |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
101 |
file = new File(home, "lib/security/blacklisted.certs"); |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
102 |
System.out.print("Check for " + file + ": "); |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
103 |
try (BufferedReader reader = new BufferedReader( |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
104 |
new InputStreamReader(new FileInputStream(file)))) { |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
105 |
int acount = 0; |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
106 |
int ccount = 0; |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
107 |
while (true) { |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
108 |
String line = reader.readLine(); |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
109 |
if (line == null) break; |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
110 |
if (line.startsWith("Algorithm")) { |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
111 |
acount++; |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
112 |
} else if (!line.isEmpty() && !line.startsWith("#")) { |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
113 |
ccount++; |
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 |
} |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
116 |
System.out.println(acount + " algs, " + ccount + " certs" ); |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
117 |
if (acount != 1) { |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
118 |
System.out.println("There are " + acount + " algorithms"); |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
119 |
failed = true; |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
120 |
} |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
121 |
if (ccount != blacklisted.size() |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
122 |
&& !blacklisted.isEmpty()) { |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
123 |
System.out.println("Wrong blacklisted.certs size: " |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
124 |
+ ccount + " fingerprints, " |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
125 |
+ blacklisted.size() + " certs"); |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
126 |
failed = true; |
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 |
|
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
130 |
if (failed) { |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
131 |
throw new Exception("Failed"); |
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 |
} |
8ea486a4f36e
8011402: Move blacklisting certificate logic from hard code to data
weijun
parents:
diff
changeset
|
134 |
} |