jdk/test/sun/security/pkcs11/ec/ReadCertificates.java
author bobv
Thu, 02 Dec 2010 14:00:03 -0500
changeset 7407 47339ceb8cb0
parent 5506 202f599c92aa
child 10328 06c93c42bca0
permissions -rw-r--r--
7004217: Remove IA64 workaround re-introduced with CR6953477 Summary: gcc bug worksaround for IA64 no longer needed Reviewed-by: andrew
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3863
diff changeset
     2
 * Copyright (c) 2006, 2007, Oracle and/or its affiliates. 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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3863
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3863
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3863
diff changeset
    21
 * questions.
2
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();
3863
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents: 2
diff changeset
   103
            // First try the provider under test (if it does not support the
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents: 2
diff changeset
   104
            // necessary algorithm then try any registered provider).
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents: 2
diff changeset
   105
            try {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents: 2
diff changeset
   106
                cert.verify(key, p.getName());
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents: 2
diff changeset
   107
            } catch (NoSuchAlgorithmException e) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents: 2
diff changeset
   108
                System.out.println("Warning: " + e.getMessage() +
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents: 2
diff changeset
   109
                ". Trying another provider...");
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents: 2
diff changeset
   110
                cert.verify(key);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents: 2
diff changeset
   111
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // try some random invalid signatures to make sure we get the correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        System.out.println("Checking incorrect signatures...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        List<X509Certificate> certList = new ArrayList<X509Certificate>(certs.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        for (int i = 0; i < 20; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            X509Certificate cert, signer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                cert = getRandomCert(certList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                signer = getRandomCert(certList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            } while (cert.getIssuerX500Principal().equals(signer.getSubjectX500Principal()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                cert.verify(signer.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                throw new Exception("Verified invalid signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            } catch (SignatureException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                System.out.println("OK: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                System.out.println("OK: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        Security.removeProvider(p.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        System.out.println("OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private static X509Certificate getRandomCert(List<X509Certificate> certs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        int n = random.nextInt(certs.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return certs.get(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
}