jdk/test/sun/security/pkcs11/ec/ReadPKCS12.java
author ascarpino
Mon, 23 Jan 2017 11:49:01 -0800
changeset 43248 5e15de85a1a0
parent 42693 6645de32a866
permissions -rw-r--r--
8172527: Rename jdk.crypto.token to jdk.crypto.cryptoki Reviewed-by: mchung, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
43248
5e15de85a1a0 8172527: Rename jdk.crypto.token to jdk.crypto.cryptoki
ascarpino
parents: 42693
diff changeset
     2
 * Copyright (c) 2006, 2017, 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: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6405536
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Verify that we can parse ECPrivateKeys from PKCS#12 and use them
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @library ..
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    30
 * @library ../../../../java/security/testlibrary
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 14342
diff changeset
    31
 * @key randomness
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: 30046
diff changeset
    33
 * @run main/othervm ReadPKCS12
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    34
 * @run main/othervm ReadPKCS12 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: 30046
diff changeset
    37
import java.io.BufferedReader;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    38
import java.io.File;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    39
import java.io.FileInputStream;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    40
import java.io.FileOutputStream;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    41
import java.io.FileReader;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    42
import java.io.InputStream;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    43
import java.io.OutputStream;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    44
import java.security.KeyStore;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    45
import java.security.PrivateKey;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    46
import java.security.Provider;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    47
import java.security.PublicKey;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    48
import java.security.Signature;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.security.cert.Certificate;
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    50
import java.security.cert.CertificateException;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    51
import java.security.cert.CertificateFactory;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    52
import java.security.cert.X509Certificate;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    53
import java.util.Collections;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    54
import java.util.HashMap;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    55
import java.util.List;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    56
import java.util.Map;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    57
import java.util.Random;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public class ReadPKCS12 extends PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private final static boolean COPY = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static void main(String[] args) throws Exception {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    64
        main(new ReadPKCS12(), args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    67
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public void main(Provider p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (p.getService("Signature", "SHA1withECDSA") == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            System.out.println("Provider does not support ECDSA, skipping...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
13661
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10328
diff changeset
    73
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10328
diff changeset
    74
        /*
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10328
diff changeset
    75
         * PKCS11Test.main will remove this provider if needed
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10328
diff changeset
    76
         */
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    77
        Providers.setAt(p, 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        CertificateFactory factory = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            // undocumented way to clear the Sun internal certificate cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            factory.generateCertificate(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        KeyStore ks2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (COPY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            ks2 = KeyStore.getInstance("JKS");
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    90
            try (InputStream in = new FileInputStream("keystore.old")) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    91
                ks2.load(in, "passphrase".toCharArray());
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    92
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        File dir = new File(BASE, "pkcs12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        File closedDir = new File(CLOSED_BASE, "pkcs12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    98
        Map<String,char[]> passwords = new HashMap<>();
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    99
        try (BufferedReader reader = new BufferedReader(
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
   100
                new FileReader(new File(BASE, "p12passwords.txt")))) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
   101
            while (true) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
   102
                String line = reader.readLine();
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
   103
                if (line == null) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
   104
                    break;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
   105
                }
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
   106
                line = line.trim();
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
   107
                if ((line.length() == 0) || line.startsWith("#")) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
   108
                    continue;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
   109
                }
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
   110
                String[] s = line.split(" ");
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
   111
                passwords.put(s[0], s[1].toCharArray());
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        for (File file : concat(dir.listFiles(), closedDir.listFiles())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            String name = file.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (file.isFile() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            System.out.println("Reading " + name + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            char[] password = passwords.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (password == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                password = passwords.get("*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
   128
            KeyStore ks;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
   129
            try (InputStream in = new FileInputStream(file)) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
   130
                ks = KeyStore.getInstance("PKCS12");
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
   131
                ks.load(in, password);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
   132
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            List<String> aliases = Collections.list(ks.aliases());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            System.out.println("Aliases: " + aliases);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            for (String alias : aliases) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                PrivateKey privateKey = (PrivateKey)ks.getKey(alias, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                Certificate[] certs = ks.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                PublicKey publicKey = certs[0].getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                System.out.println("Certificates: " + certs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                System.out.println(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                System.out.println(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                if (COPY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    ks2.setKeyEntry(alias, privateKey, "passphrase".toCharArray(), certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                verifyCerts(certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                Random random = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                byte[] data = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                random.nextBytes(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                Signature s = Signature.getInstance("SHA1withECDSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                s.initSign(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                s.update(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                byte[] sig = s.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                s.initVerify(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                s.update(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                if (s.verify(sig) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    throw new Exception("Signature does not verify");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                System.out.println("Verified public/private key match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (COPY) {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
   168
            try (OutputStream out = new FileOutputStream("keystore.new")) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
   169
                ks2.store(out, "passphrase".toCharArray());
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
   170
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        System.out.println("OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    private static void verifyCerts(Certificate[] certs) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        int n = certs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        for (int i = 0; i < n - 1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            X509Certificate cert = (X509Certificate)certs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            X509Certificate issuer = (X509Certificate)certs[i + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if (cert.getIssuerX500Principal().equals(issuer.getSubjectX500Principal()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                throw new Exception("Certificates do not chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            cert.verify(issuer.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            System.out.println("Verified: " + cert.getSubjectX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        X509Certificate last = (X509Certificate)certs[n - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // if self-signed, verify the final cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (last.getIssuerX500Principal().equals(last.getSubjectX500Principal())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            last.verify(last.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            System.out.println("Verified: " + last.getSubjectX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
}