diff --git a/jdk/test/lib/security/cacerts/VerifyCACerts.java b/jdk/test/lib/security/cacerts/VerifyCACerts.java index 774908cca54..a5559a0e755 100644 --- a/jdk/test/lib/security/cacerts/VerifyCACerts.java +++ b/jdk/test/lib/security/cacerts/VerifyCACerts.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2006 Sun Microsystems, Inc. All Rights Reserved. + * Copyright 2002-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,11 +23,12 @@ /** * @test - * @bug 4400624 6321453 + * @bug 4400624 6321453 6728890 6732157 6754779 6768559 * @summary Make sure all self-signed root cert signatures are valid */ import java.io.FileInputStream; import java.security.KeyStore; +import java.security.MessageDigest; import java.security.cert.*; import java.util.*; @@ -39,25 +40,102 @@ public class VerifyCACerts { System.getProperty("file.separator") + "security" + System.getProperty("file.separator") + "cacerts"; - public static void main(String[] args) throws Exception { + private static boolean atLeastOneFailed = false; - // pull all the trusted self-signed CA certs out of the cacerts file - // and verify their signatures + private static MessageDigest md; + + // map of cert alias to SHA1 fingerprint + private static Map fpMap = new HashMap(); + + private static String[][] entries = { + { "swisssignsilverg2ca", "9B:AA:E5:9F:56:EE:21:CB:43:5A:BE:25:93:DF:A7:F0:40:D1:1D:CB"}, + { "swisssigngoldg2ca", "D8:C5:38:8A:B7:30:1B:1B:6E:D4:7A:E6:45:25:3A:6F:9F:1A:27:61"}, + { "swisssignplatinumg2ca", "56:E0:FA:C0:3B:8F:18:23:55:18:E5:D3:11:CA:E8:C2:43:31:AB:66"}, + { "verisigntsaca", "BE:36:A4:56:2F:B2:EE:05:DB:B3:D3:23:23:AD:F4:45:08:4E:D6:56"}, + { "camerfirmachambersignca", "4A:BD:EE:EC:95:0D:35:9C:89:AE:C7:52:A1:2C:5B:29:F6:D6:AA:0C"}, + { "camerfirmachambersca", "78:6A:74:AC:76:AB:14:7F:9C:6A:30:50:BA:9E:A8:7E:FE:9A:CE:3C"}, + { "camerfirmachamberscommerceca", "6E:3A:55:A4:19:0C:19:5C:93:84:3C:C0:DB:72:2E:31:30:61:F0:B1"}, + { "deutschetelekomrootca2", "85:A4:08:C0:9C:19:3E:5D:51:58:7D:CD:D6:13:30:FD:8C:DE:37:BF"}, + }; + + static { + for (String[] entry : entries) { + fpMap.put(entry[0], entry[1]); + } + }; + + public static void main(String[] args) throws Exception { + md = MessageDigest.getInstance("SHA1"); KeyStore ks = KeyStore.getInstance("JKS"); ks.load(new FileInputStream(cacertsFileName), "changeit".toCharArray()); + + // check that all entries in the map are in the keystore + for (String alias : fpMap.keySet()) { + if (!ks.isCertificateEntry(alias)) { + atLeastOneFailed = true; + System.err.println(alias + " is not in cacerts"); + } + } + // pull all the trusted self-signed CA certs out of the cacerts file + // and verify their signatures Enumeration aliases = ks.aliases(); while (aliases.hasMoreElements()) { String alias = aliases.nextElement(); System.out.println("Verifying " + alias); - if (!ks.isCertificateEntry(alias)) - throw new Exception(alias + " is not a trusted cert entry"); + if (!ks.isCertificateEntry(alias)) { + atLeastOneFailed = true; + System.err.println(alias + " is not a trusted cert entry"); + } Certificate cert = ks.getCertificate(alias); // remember the GTE CyberTrust CA cert for further tests if (alias.equals("gtecybertrustca")) { - throw new Exception + atLeastOneFailed = true; + System.err.println ("gtecybertrustca is expired and should be deleted"); } cert.verify(cert.getPublicKey()); + if (!checkFingerprint(alias, cert)) { + atLeastOneFailed = true; + System.err.println + (alias + " SHA1 fingerprint is incorrect"); + } + } + + if (atLeastOneFailed) { + throw new Exception("At least one cacert test failed"); } } + + private static boolean checkFingerprint(String alias, Certificate cert) + throws Exception { + String fingerprint = fpMap.get(alias); + if (fingerprint == null) { + // no entry for alias + return true; + } + System.out.println("Checking fingerprint of " + alias); + byte[] digest = md.digest(cert.getEncoded()); + return fingerprint.equals(toHexString(digest)); + } + + private static String toHexString(byte[] block) { + StringBuffer buf = new StringBuffer(); + int len = block.length; + for (int i = 0; i < len; i++) { + byte2hex(block[i], buf); + if (i < len-1) { + buf.append(":"); + } + } + return buf.toString(); + } + + private static void byte2hex(byte b, StringBuffer buf) { + char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', + '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + int high = ((b & 0xf0) >> 4); + int low = (b & 0x0f); + buf.append(hexChars[high]); + buf.append(hexChars[low]); + } }