2
|
1 |
/*
|
1632
|
2 |
* Copyright 2002-2008 Sun Microsystems, Inc. All Rights Reserved.
|
2
|
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.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
1632
|
26 |
* @bug 4400624 6321453 6728890 6732157 6754779 6768559
|
2
|
27 |
* @summary Make sure all self-signed root cert signatures are valid
|
|
28 |
*/
|
|
29 |
import java.io.FileInputStream;
|
|
30 |
import java.security.KeyStore;
|
1632
|
31 |
import java.security.MessageDigest;
|
2
|
32 |
import java.security.cert.*;
|
|
33 |
import java.util.*;
|
|
34 |
|
|
35 |
public class VerifyCACerts {
|
|
36 |
|
|
37 |
private final static String cacertsFileName =
|
|
38 |
System.getProperty("java.home") +
|
|
39 |
System.getProperty("file.separator") + "lib" +
|
|
40 |
System.getProperty("file.separator") + "security" +
|
|
41 |
System.getProperty("file.separator") + "cacerts";
|
|
42 |
|
1632
|
43 |
private static boolean atLeastOneFailed = false;
|
|
44 |
|
|
45 |
private static MessageDigest md;
|
|
46 |
|
|
47 |
// map of cert alias to SHA1 fingerprint
|
|
48 |
private static Map<String, String> fpMap = new HashMap<String, String>();
|
|
49 |
|
|
50 |
private static String[][] entries = {
|
|
51 |
{ "swisssignsilverg2ca", "9B:AA:E5:9F:56:EE:21:CB:43:5A:BE:25:93:DF:A7:F0:40:D1:1D:CB"},
|
|
52 |
{ "swisssigngoldg2ca", "D8:C5:38:8A:B7:30:1B:1B:6E:D4:7A:E6:45:25:3A:6F:9F:1A:27:61"},
|
|
53 |
{ "swisssignplatinumg2ca", "56:E0:FA:C0:3B:8F:18:23:55:18:E5:D3:11:CA:E8:C2:43:31:AB:66"},
|
|
54 |
{ "verisigntsaca", "BE:36:A4:56:2F:B2:EE:05:DB:B3:D3:23:23:AD:F4:45:08:4E:D6:56"},
|
|
55 |
{ "camerfirmachambersignca", "4A:BD:EE:EC:95:0D:35:9C:89:AE:C7:52:A1:2C:5B:29:F6:D6:AA:0C"},
|
|
56 |
{ "camerfirmachambersca", "78:6A:74:AC:76:AB:14:7F:9C:6A:30:50:BA:9E:A8:7E:FE:9A:CE:3C"},
|
|
57 |
{ "camerfirmachamberscommerceca", "6E:3A:55:A4:19:0C:19:5C:93:84:3C:C0:DB:72:2E:31:30:61:F0:B1"},
|
|
58 |
{ "deutschetelekomrootca2", "85:A4:08:C0:9C:19:3E:5D:51:58:7D:CD:D6:13:30:FD:8C:DE:37:BF"},
|
|
59 |
};
|
|
60 |
|
|
61 |
static {
|
|
62 |
for (String[] entry : entries) {
|
|
63 |
fpMap.put(entry[0], entry[1]);
|
|
64 |
}
|
|
65 |
};
|
|
66 |
|
2
|
67 |
public static void main(String[] args) throws Exception {
|
1632
|
68 |
md = MessageDigest.getInstance("SHA1");
|
|
69 |
KeyStore ks = KeyStore.getInstance("JKS");
|
|
70 |
ks.load(new FileInputStream(cacertsFileName), "changeit".toCharArray());
|
2
|
71 |
|
1632
|
72 |
// check that all entries in the map are in the keystore
|
|
73 |
for (String alias : fpMap.keySet()) {
|
|
74 |
if (!ks.isCertificateEntry(alias)) {
|
|
75 |
atLeastOneFailed = true;
|
|
76 |
System.err.println(alias + " is not in cacerts");
|
|
77 |
}
|
|
78 |
}
|
2
|
79 |
// pull all the trusted self-signed CA certs out of the cacerts file
|
|
80 |
// and verify their signatures
|
|
81 |
Enumeration<String> aliases = ks.aliases();
|
|
82 |
while (aliases.hasMoreElements()) {
|
|
83 |
String alias = aliases.nextElement();
|
|
84 |
System.out.println("Verifying " + alias);
|
1632
|
85 |
if (!ks.isCertificateEntry(alias)) {
|
|
86 |
atLeastOneFailed = true;
|
|
87 |
System.err.println(alias + " is not a trusted cert entry");
|
|
88 |
}
|
2
|
89 |
Certificate cert = ks.getCertificate(alias);
|
|
90 |
// remember the GTE CyberTrust CA cert for further tests
|
|
91 |
if (alias.equals("gtecybertrustca")) {
|
1632
|
92 |
atLeastOneFailed = true;
|
|
93 |
System.err.println
|
2
|
94 |
("gtecybertrustca is expired and should be deleted");
|
|
95 |
}
|
|
96 |
cert.verify(cert.getPublicKey());
|
1632
|
97 |
if (!checkFingerprint(alias, cert)) {
|
|
98 |
atLeastOneFailed = true;
|
|
99 |
System.err.println
|
|
100 |
(alias + " SHA1 fingerprint is incorrect");
|
|
101 |
}
|
|
102 |
}
|
|
103 |
|
|
104 |
if (atLeastOneFailed) {
|
|
105 |
throw new Exception("At least one cacert test failed");
|
2
|
106 |
}
|
|
107 |
}
|
1632
|
108 |
|
|
109 |
private static boolean checkFingerprint(String alias, Certificate cert)
|
|
110 |
throws Exception {
|
|
111 |
String fingerprint = fpMap.get(alias);
|
|
112 |
if (fingerprint == null) {
|
|
113 |
// no entry for alias
|
|
114 |
return true;
|
|
115 |
}
|
|
116 |
System.out.println("Checking fingerprint of " + alias);
|
|
117 |
byte[] digest = md.digest(cert.getEncoded());
|
|
118 |
return fingerprint.equals(toHexString(digest));
|
|
119 |
}
|
|
120 |
|
|
121 |
private static String toHexString(byte[] block) {
|
|
122 |
StringBuffer buf = new StringBuffer();
|
|
123 |
int len = block.length;
|
|
124 |
for (int i = 0; i < len; i++) {
|
|
125 |
byte2hex(block[i], buf);
|
|
126 |
if (i < len-1) {
|
|
127 |
buf.append(":");
|
|
128 |
}
|
|
129 |
}
|
|
130 |
return buf.toString();
|
|
131 |
}
|
|
132 |
|
|
133 |
private static void byte2hex(byte b, StringBuffer buf) {
|
|
134 |
char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
|
|
135 |
'9', 'A', 'B', 'C', 'D', 'E', 'F' };
|
|
136 |
int high = ((b & 0xf0) >> 4);
|
|
137 |
int low = (b & 0x0f);
|
|
138 |
buf.append(hexChars[high]);
|
|
139 |
buf.append(hexChars[low]);
|
|
140 |
}
|
2
|
141 |
}
|