test/jdk/sun/security/pkcs11/ec/ReadCertificates.java
author jjiang
Wed, 15 Aug 2018 18:41:18 +0800
changeset 51460 97e361fe3433
parent 47216 71c04702a3d5
permissions -rw-r--r--
8164639: Configure PKCS11 tests to use user-supplied NSS libraries Summary: Provide system property "test.nss.lib.paths" for specifying a set of absolute paths to the custom NSS lib directories Reviewed-by: weijun, rhalade
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51460
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 47216
diff changeset
     2
 * Copyright (c) 2006, 2018, 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
40975
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 35379
diff changeset
    24
/*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
25811
f4d231e2c1bf 8051972: sun/security/pkcs11/ec/ReadCertificates.java fails intermittently
vinnie
parents: 21977
diff changeset
    26
 * @bug 6405536 6414980 8051972
2
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
51460
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 47216
diff changeset
    30
 * @library /test/lib ..
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    31
 * @library ../../../../java/security/testlibrary
43248
5e15de85a1a0 8172527: Rename jdk.crypto.token to jdk.crypto.cryptoki
ascarpino
parents: 42693
diff changeset
    32
 * @modules jdk.crypto.cryptoki
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    33
 * @run main/othervm ReadCertificates
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    34
 * @run main/othervm ReadCertificates sm policy
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    37
import java.io.File;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    38
import java.io.FileInputStream;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    39
import java.io.InputStream;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    40
import java.security.InvalidKeyException;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    41
import java.security.NoSuchAlgorithmException;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    42
import java.security.NoSuchProviderException;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    43
import java.security.Provider;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    44
import java.security.PublicKey;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    45
import java.security.SecureRandom;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    46
import java.security.SignatureException;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    47
import java.security.cert.CertificateException;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    48
import java.security.cert.CertificateFactory;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    49
import java.security.cert.X509Certificate;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    50
import java.security.interfaces.ECPublicKey;
21977
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
    51
import java.security.spec.ECParameterSpec;
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    52
import java.util.ArrayList;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    53
import java.util.Arrays;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    54
import java.util.Collection;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    55
import java.util.LinkedHashMap;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    56
import java.util.List;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    57
import java.util.Map;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
public class ReadCertificates extends PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static CertificateFactory factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static SecureRandom random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static Collection<X509Certificate> readCertificates(File file) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        System.out.println("Loading " + file.getName() + "...");
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    68
        Collection<X509Certificate> certs;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    69
        try (InputStream in = new FileInputStream(file)) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    70
            certs = (Collection<X509Certificate>)factory.generateCertificates(in);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    71
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return certs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public static void main(String[] args) throws Exception {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    76
        main(new ReadCertificates(), args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
    79
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public void main(Provider p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (p.getService("Signature", "SHA1withECDSA") == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            System.out.println("Provider does not support ECDSA, skipping...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
13661
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10328
diff changeset
    85
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10328
diff changeset
    86
        /*
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10328
diff changeset
    87
         * PKCS11Test.main will remove this provider if needed
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10328
diff changeset
    88
         */
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    89
        Providers.setAt(p, 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        random = new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        factory = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            // clear certificate cache in from a previous run with a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            // provider (undocumented hack for the Sun provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            factory.generateCertificate(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
   100
        Map<X500Principal,X509Certificate> certs = new LinkedHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        File dir = new File(BASE, "certs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        File closedDir = new File(CLOSED_BASE, "certs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        File[] files = concat(dir.listFiles(), closedDir.listFiles());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        Arrays.sort(files);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        for (File file : files) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (file.isFile() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            Collection<X509Certificate> certList = readCertificates(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            for (X509Certificate cert : certList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                X509Certificate old = certs.put(cert.getSubjectX500Principal(), cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                if (old != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    System.out.println("Duplicate subject:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    System.out.println("Old Certificate: " + old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    System.out.println("New Certificate: " + cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    throw new Exception(file.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        System.out.println("OK: " + certs.size() + " certificates.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
21977
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   123
        // Get supported curves
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
   124
        List<ECParameterSpec> supportedEC = getKnownCurves(p);
21977
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   125
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   126
        System.out.println("Test Certs:\n");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        for (X509Certificate cert : certs.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            X509Certificate issuer = certs.get(cert.getIssuerX500Principal());
21977
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   129
            System.out.print("Verifying " + cert.getSubjectX500Principal() +
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   130
                    "...  ");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            PublicKey key = issuer.getPublicKey();
21977
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   132
            // Check if curve is supported
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   133
            if (issuer.getPublicKey() instanceof ECPublicKey) {
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   134
                if (!checkSupport(supportedEC,
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   135
                        ((ECPublicKey)key).getParams())) {
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   136
                    System.out.println("Curve not found. Skipped.");
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 14342
diff changeset
   137
                    continue;
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 14342
diff changeset
   138
                }
3863
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents: 2
diff changeset
   139
            }
21977
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   140
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   141
           try {
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   142
               cert.verify(key, p.getName());
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   143
               System.out.println("Pass.");
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   144
           } catch (NoSuchAlgorithmException e) {
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   145
               System.out.println("Warning: " + e.getMessage() +
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   146
                   ". Trying another provider...");
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   147
               cert.verify(key);
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
   148
           } catch (CertificateException | InvalidKeyException |
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
   149
                    NoSuchProviderException | SignatureException e) {
21977
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   150
               System.out.println(e.getMessage());
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   151
               if (key instanceof ECPublicKey) {
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   152
                   System.out.println("Failed.\n\tCurve: " +
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   153
                           ((ECPublicKey)key).getParams() +
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   154
                           "\n\tSignature Alg: " + cert.getSigAlgName());
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   155
               } else {
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   156
                   System.out.println("Key: "+key.toString());
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   157
               }
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   158
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   159
               System.err.println("Verifying " + cert.getSubjectX500Principal());
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   160
               e.printStackTrace();
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   161
           }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        // try some random invalid signatures to make sure we get the correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        System.out.println("Checking incorrect signatures...");
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
   167
        List<X509Certificate> certList = new ArrayList<>(certs.values());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        for (int i = 0; i < 20; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            X509Certificate cert, signer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                cert = getRandomCert(certList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                signer = getRandomCert(certList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            } while (cert.getIssuerX500Principal().equals(signer.getSubjectX500Principal()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            try {
25811
f4d231e2c1bf 8051972: sun/security/pkcs11/ec/ReadCertificates.java fails intermittently
vinnie
parents: 21977
diff changeset
   175
                PublicKey signerPublicKey = signer.getPublicKey();
f4d231e2c1bf 8051972: sun/security/pkcs11/ec/ReadCertificates.java fails intermittently
vinnie
parents: 21977
diff changeset
   176
                cert.verify(signerPublicKey);
f4d231e2c1bf 8051972: sun/security/pkcs11/ec/ReadCertificates.java fails intermittently
vinnie
parents: 21977
diff changeset
   177
                // Ignore false positives
f4d231e2c1bf 8051972: sun/security/pkcs11/ec/ReadCertificates.java fails intermittently
vinnie
parents: 21977
diff changeset
   178
                if (cert.getPublicKey().equals(signerPublicKey)) {
f4d231e2c1bf 8051972: sun/security/pkcs11/ec/ReadCertificates.java fails intermittently
vinnie
parents: 21977
diff changeset
   179
                    System.out.println("OK: self-signed certificate detected");
f4d231e2c1bf 8051972: sun/security/pkcs11/ec/ReadCertificates.java fails intermittently
vinnie
parents: 21977
diff changeset
   180
                } else {
f4d231e2c1bf 8051972: sun/security/pkcs11/ec/ReadCertificates.java fails intermittently
vinnie
parents: 21977
diff changeset
   181
                    throw new Exception("Verified invalid signature");
f4d231e2c1bf 8051972: sun/security/pkcs11/ec/ReadCertificates.java fails intermittently
vinnie
parents: 21977
diff changeset
   182
                }
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 25811
diff changeset
   183
            } catch (SignatureException | InvalidKeyException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                System.out.println("OK: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        System.out.println("OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private static X509Certificate getRandomCert(List<X509Certificate> certs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        int n = random.nextInt(certs.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return certs.get(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
}