jdk/test/sun/security/pkcs11/ec/ReadPKCS12.java
author ascarpino
Mon, 29 Jul 2013 13:43:24 -0700
changeset 19067 5271291b7121
parent 14342 8435a30053c1
child 30046 cf2c86e1819e
permissions -rw-r--r--
8020424: The NSS version should be detected before running crypto tests Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 13661
diff changeset
     2
 * Copyright (c) 2006, 2012, 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
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
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
2
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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class ReadPKCS12 extends PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private final static boolean COPY = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        main(new ReadPKCS12());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public void main(Provider p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        if (p.getService("Signature", "SHA1withECDSA") == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            System.out.println("Provider does not support ECDSA, skipping...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
13661
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10328
diff changeset
    56
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10328
diff changeset
    57
        /*
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10328
diff changeset
    58
         * PKCS11Test.main will remove this provider if needed
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10328
diff changeset
    59
         */
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    60
        Providers.setAt(p, 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        CertificateFactory factory = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            // undocumented way to clear the Sun internal certificate cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            factory.generateCertificate(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        KeyStore ks2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (COPY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            ks2 = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            InputStream in = new FileInputStream("keystore.old");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            ks2.load(in, "passphrase".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        File dir = new File(BASE, "pkcs12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        File closedDir = new File(CLOSED_BASE, "pkcs12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        Map<String,char[]> passwords = new HashMap<String,char[]>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        BufferedReader reader = new BufferedReader(new FileReader((new File(BASE, "p12passwords.txt"))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            String line = reader.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (line == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            line = line.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            if ((line.length() == 0) || line.startsWith("#")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            String[] s = line.split(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            passwords.put(s[0], s[1].toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        reader.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        for (File file : concat(dir.listFiles(), closedDir.listFiles())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            String name = file.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            if (file.isFile() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            System.out.println("Reading " + name + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            char[] password = passwords.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (password == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                password = passwords.get("*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            InputStream in = new FileInputStream(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            KeyStore ks = KeyStore.getInstance("PKCS12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            ks.load(in, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            List<String> aliases = Collections.list(ks.aliases());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            System.out.println("Aliases: " + aliases);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            for (String alias : aliases) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                PrivateKey privateKey = (PrivateKey)ks.getKey(alias, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                Certificate[] certs = ks.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                PublicKey publicKey = certs[0].getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                System.out.println("Certificates: " + certs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                System.out.println(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                System.out.println(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                if (COPY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    ks2.setKeyEntry(alias, privateKey, "passphrase".toCharArray(), certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                verifyCerts(certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                Random random = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                byte[] data = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                random.nextBytes(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                Signature s = Signature.getInstance("SHA1withECDSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                s.initSign(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                s.update(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                byte[] sig = s.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                s.initVerify(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                s.update(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                if (s.verify(sig) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    throw new Exception("Signature does not verify");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                System.out.println("Verified public/private key match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (COPY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            OutputStream out = new FileOutputStream("keystore.new");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            ks2.store(out, "passphrase".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        System.out.println("OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private static void verifyCerts(Certificate[] certs) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        int n = certs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        for (int i = 0; i < n - 1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            X509Certificate cert = (X509Certificate)certs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            X509Certificate issuer = (X509Certificate)certs[i + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (cert.getIssuerX500Principal().equals(issuer.getSubjectX500Principal()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                throw new Exception("Certificates do not chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            cert.verify(issuer.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            System.out.println("Verified: " + cert.getSubjectX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        X509Certificate last = (X509Certificate)certs[n - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        // if self-signed, verify the final cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (last.getIssuerX500Principal().equals(last.getSubjectX500Principal())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            last.verify(last.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            System.out.println("Verified: " + last.getSubjectX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
}