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