37 System.getProperty("java.home") + |
38 System.getProperty("java.home") + |
38 System.getProperty("file.separator") + "lib" + |
39 System.getProperty("file.separator") + "lib" + |
39 System.getProperty("file.separator") + "security" + |
40 System.getProperty("file.separator") + "security" + |
40 System.getProperty("file.separator") + "cacerts"; |
41 System.getProperty("file.separator") + "cacerts"; |
41 |
42 |
|
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 |
42 public static void main(String[] args) throws Exception { |
67 public static void main(String[] args) throws Exception { |
|
68 md = MessageDigest.getInstance("SHA1"); |
|
69 KeyStore ks = KeyStore.getInstance("JKS"); |
|
70 ks.load(new FileInputStream(cacertsFileName), "changeit".toCharArray()); |
43 |
71 |
|
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 } |
44 // pull all the trusted self-signed CA certs out of the cacerts file |
79 // pull all the trusted self-signed CA certs out of the cacerts file |
45 // and verify their signatures |
80 // and verify their signatures |
46 KeyStore ks = KeyStore.getInstance("JKS"); |
|
47 ks.load(new FileInputStream(cacertsFileName), "changeit".toCharArray()); |
|
48 Enumeration<String> aliases = ks.aliases(); |
81 Enumeration<String> aliases = ks.aliases(); |
49 while (aliases.hasMoreElements()) { |
82 while (aliases.hasMoreElements()) { |
50 String alias = aliases.nextElement(); |
83 String alias = aliases.nextElement(); |
51 System.out.println("Verifying " + alias); |
84 System.out.println("Verifying " + alias); |
52 if (!ks.isCertificateEntry(alias)) |
85 if (!ks.isCertificateEntry(alias)) { |
53 throw new Exception(alias + " is not a trusted cert entry"); |
86 atLeastOneFailed = true; |
|
87 System.err.println(alias + " is not a trusted cert entry"); |
|
88 } |
54 Certificate cert = ks.getCertificate(alias); |
89 Certificate cert = ks.getCertificate(alias); |
55 // remember the GTE CyberTrust CA cert for further tests |
90 // remember the GTE CyberTrust CA cert for further tests |
56 if (alias.equals("gtecybertrustca")) { |
91 if (alias.equals("gtecybertrustca")) { |
57 throw new Exception |
92 atLeastOneFailed = true; |
|
93 System.err.println |
58 ("gtecybertrustca is expired and should be deleted"); |
94 ("gtecybertrustca is expired and should be deleted"); |
59 } |
95 } |
60 cert.verify(cert.getPublicKey()); |
96 cert.verify(cert.getPublicKey()); |
|
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"); |
61 } |
106 } |
62 } |
107 } |
|
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 } |
63 } |
141 } |