jdk/test/sun/security/pkcs11/ec/ReadCertificates.java
author tbell
Tue, 11 Nov 2008 22:01:08 -0800
changeset 1546 a02228c60567
parent 2 90ce3da70b43
child 3863 8e0f58b1c072
permissions -rw-r--r--
6764892: VS2008 changes required to compile hotspot sources Summary: Minor changes required to build using the Visual Studio 2008 compiler Reviewed-by: kvn, ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2006-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6405536 6414980
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Make sure that we can parse certificates using various named curves
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *   and verify their signatures
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @library ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class ReadCertificates extends PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static CertificateFactory factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static SecureRandom random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static Collection<X509Certificate> readCertificates(File file) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        System.out.println("Loading " + file.getName() + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        InputStream in = new FileInputStream(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        Collection<X509Certificate> certs = (Collection<X509Certificate>)factory.generateCertificates(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        return certs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        main(new ReadCertificates());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public void main(Provider p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (p.getService("Signature", "SHA1withECDSA") == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            System.out.println("Provider does not support ECDSA, skipping...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        Security.insertProviderAt(p, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        random = new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        factory = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            // clear certificate cache in from a previous run with a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            // provider (undocumented hack for the Sun provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            factory.generateCertificate(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        Map<X500Principal,X509Certificate> certs = new LinkedHashMap<X500Principal,X509Certificate>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        File dir = new File(BASE, "certs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        File closedDir = new File(CLOSED_BASE, "certs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        File[] files = concat(dir.listFiles(), closedDir.listFiles());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        Arrays.sort(files);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        for (File file : files) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if (file.isFile() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            Collection<X509Certificate> certList = readCertificates(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            for (X509Certificate cert : certList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                X509Certificate old = certs.put(cert.getSubjectX500Principal(), cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                if (old != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    System.out.println("Duplicate subject:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    System.out.println("Old Certificate: " + old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    System.out.println("New Certificate: " + cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    throw new Exception(file.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        System.out.println("OK: " + certs.size() + " certificates.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        for (X509Certificate cert : certs.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            X509Certificate issuer = certs.get(cert.getIssuerX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            System.out.println("Verifying " + cert.getSubjectX500Principal() + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            PublicKey key = issuer.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            cert.verify(key, p.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // try some random invalid signatures to make sure we get the correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        System.out.println("Checking incorrect signatures...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        List<X509Certificate> certList = new ArrayList<X509Certificate>(certs.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        for (int i = 0; i < 20; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            X509Certificate cert, signer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                cert = getRandomCert(certList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                signer = getRandomCert(certList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            } while (cert.getIssuerX500Principal().equals(signer.getSubjectX500Principal()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                cert.verify(signer.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                throw new Exception("Verified invalid signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            } catch (SignatureException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                System.out.println("OK: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                System.out.println("OK: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        Security.removeProvider(p.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        System.out.println("OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private static X509Certificate getRandomCert(List<X509Certificate> certs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        int n = random.nextInt(certs.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return certs.get(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
}