jdk/test/sun/security/pkcs11/ec/ReadPKCS12.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
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
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 ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.cert.Certificate;
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 ReadPKCS12 extends PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private final static boolean COPY = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        main(new ReadPKCS12());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public void main(Provider p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        if (p.getService("Signature", "SHA1withECDSA") == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            System.out.println("Provider does not support ECDSA, skipping...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        Security.insertProviderAt(p, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        CertificateFactory factory = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            // undocumented way to clear the Sun internal certificate cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            factory.generateCertificate(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        KeyStore ks2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (COPY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            ks2 = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            InputStream in = new FileInputStream("keystore.old");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            ks2.load(in, "passphrase".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        File dir = new File(BASE, "pkcs12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        File closedDir = new File(CLOSED_BASE, "pkcs12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        Map<String,char[]> passwords = new HashMap<String,char[]>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        BufferedReader reader = new BufferedReader(new FileReader((new File(BASE, "p12passwords.txt"))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            String line = reader.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            if (line == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            line = line.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            if ((line.length() == 0) || line.startsWith("#")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            String[] s = line.split(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            passwords.put(s[0], s[1].toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        reader.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        for (File file : concat(dir.listFiles(), closedDir.listFiles())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            String name = file.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if (file.isFile() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            System.out.println("Reading " + name + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            char[] password = passwords.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (password == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                password = passwords.get("*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            InputStream in = new FileInputStream(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            KeyStore ks = KeyStore.getInstance("PKCS12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            ks.load(in, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            List<String> aliases = Collections.list(ks.aliases());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            System.out.println("Aliases: " + aliases);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            for (String alias : aliases) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                PrivateKey privateKey = (PrivateKey)ks.getKey(alias, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                Certificate[] certs = ks.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                PublicKey publicKey = certs[0].getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                System.out.println("Certificates: " + certs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                System.out.println(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                System.out.println(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                if (COPY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    ks2.setKeyEntry(alias, privateKey, "passphrase".toCharArray(), certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                verifyCerts(certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                Random random = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                byte[] data = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                random.nextBytes(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                Signature s = Signature.getInstance("SHA1withECDSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                s.initSign(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                s.update(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                byte[] sig = s.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                s.initVerify(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                s.update(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                if (s.verify(sig) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    throw new Exception("Signature does not verify");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                System.out.println("Verified public/private key match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (COPY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            OutputStream out = new FileOutputStream("keystore.new");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            ks2.store(out, "passphrase".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        Security.removeProvider(p.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        System.out.println("OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private static void verifyCerts(Certificate[] certs) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        int n = certs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        for (int i = 0; i < n - 1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            X509Certificate cert = (X509Certificate)certs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            X509Certificate issuer = (X509Certificate)certs[i + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            if (cert.getIssuerX500Principal().equals(issuer.getSubjectX500Principal()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                throw new Exception("Certificates do not chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            cert.verify(issuer.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            System.out.println("Verified: " + cert.getSubjectX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        X509Certificate last = (X509Certificate)certs[n - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // if self-signed, verify the final cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (last.getIssuerX500Principal().equals(last.getSubjectX500Principal())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            last.verify(last.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            System.out.println("Verified: " + last.getSubjectX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
}