test/jdk/sun/security/pkcs11/Secmod/AddPrivateKey.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: 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 6414980
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test that the PKCS#11 KeyStore handles RSA, DSA, and EC keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Andreas Sterbenz
51460
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 47216
diff changeset
    29
 * @library /test/lib ..
43248
5e15de85a1a0 8172527: Rename jdk.crypto.token to jdk.crypto.cryptoki
ascarpino
parents: 42693
diff changeset
    30
 * @modules jdk.crypto.cryptoki
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    31
 * @run main/othervm AddPrivateKey
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    32
 * @run main/othervm AddPrivateKey sm policy
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    35
import java.io.File;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    36
import java.io.FileInputStream;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    37
import java.io.InputStream;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    38
import java.security.KeyFactory;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    39
import java.security.KeyStore;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    40
import java.security.KeyStore.PasswordProtection;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    41
import java.security.KeyStore.PrivateKeyEntry;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    42
import java.security.KeyStoreException;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    43
import java.security.PrivateKey;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    44
import java.security.Provider;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    45
import java.security.PublicKey;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    46
import java.security.Security;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    47
import java.security.Signature;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    48
import java.security.cert.X509Certificate;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    49
import java.util.Arrays;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    50
import java.util.Collections;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    51
import java.util.List;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
// this test is currently only run for the NSS KeyStore provider, but it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
// is really a generic KeyStore test so it should be modified to run for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
// all providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public class AddPrivateKey extends SecmodTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
32635
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
    58
    private static final String ALIAS1 = "entry1";
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
    59
    private static final String ALIAS2 = "entry2";
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
    60
    private static final String ALIAS3 = "entry3";
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
    61
    private static final int MAX_LINE = 85;
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
    62
    private static final int DATA_LENGTH = 4096;
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
    63
    private static final byte[] DATA = generateData(DATA_LENGTH);
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
    64
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public static void main(String[] args) throws Exception {
51460
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 47216
diff changeset
    66
        if (args.length > 1 && "sm".equals(args[0])) {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 47216
diff changeset
    67
            System.setProperty("java.security.policy",
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 47216
diff changeset
    68
                    BASE + File.separator + args[1]);
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 47216
diff changeset
    69
        }
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 47216
diff changeset
    70
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (initSecmod() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        String configName = BASE + SEP + "nss.cfg";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        Provider p = getSunPKCS11(configName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        boolean supportsEC = (p.getService("KeyFactory", "EC") != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        System.out.println(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        Security.addProvider(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    84
        if (args.length > 1 && "sm".equals(args[0])) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    85
            System.setSecurityManager(new SecurityManager());
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    86
        }
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    87
32635
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
    88
        KeyStore ks = KeyStore.getInstance(PKCS11, p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        ks.load(null, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        for (String alias : aliases(ks)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            System.out.println("Deleting: " + alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            ks.deleteEntry(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        KeyStore jks = KeyStore.getInstance("JKS");
32635
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
    96
        try (InputStream in = new FileInputStream(BASE + SEP + "keystore.jks")) {
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
    97
            char[] jkspass = "passphrase".toCharArray();
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
    98
            jks.load(in, jkspass);
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
    99
            for (String alias : Collections.list(jks.aliases())) {
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   100
                if (jks.entryInstanceOf(alias, PrivateKeyEntry.class)) {
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   101
                    PrivateKeyEntry entry = (PrivateKeyEntry)jks.getEntry(alias,
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   102
                            new PasswordProtection(jkspass));
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   103
                    String algorithm = entry.getPrivateKey().getAlgorithm();
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   104
                    System.out.printf("-Entry %s (%s)%n", alias, algorithm);
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   105
                    if ((supportsEC == false) && algorithm.equals("EC")) {
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   106
                        System.out.println("EC not supported by provider, "
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   107
                                + "skipping");
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   108
                        continue;
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   109
                    }
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   110
                    if ((supportsEC == false) && algorithm.equals("DSA")) {
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   111
                        System.out.println("Provider does not appear to have "
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   112
                                + "CKA_NETSCAPE_DB fix, skipping");
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   113
                        continue;
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   114
                    }
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   115
                    test(p, entry);
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   116
                } // else ignore
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   117
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        System.out.println("OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private static List<String> aliases(KeyStore ks) throws KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return Collections.list(ks.aliases());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private static void test(Provider p, PrivateKeyEntry entry) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        PrivateKey key = entry.getPrivateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        X509Certificate[] chain = (X509Certificate[])entry.getCertificateChain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        PublicKey publicKey = chain[0].getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        System.out.println(toString(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        sign(p, key, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        KeyStore ks = KeyStore.getInstance("PKCS11", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        ks.load(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (ks.size() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw new Exception("KeyStore not empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        List<String> aliases;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // test 1: add entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        ks.setKeyEntry(ALIAS1, key, null, chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        aliases = aliases(ks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (aliases.size() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            throw new Exception("size not 1: " + aliases);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (aliases.get(0).equals(ALIAS1) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            throw new Exception("alias mismatch: " + aliases);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        PrivateKey key2 = (PrivateKey)ks.getKey(ALIAS1, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        System.out.println(toString(key2));
32635
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   152
        X509Certificate[] chain2 =
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   153
                (X509Certificate[]) ks.getCertificateChain(ALIAS1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (Arrays.equals(chain, chain2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throw new Exception("chain mismatch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        sign(p, key2, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        ks.deleteEntry(ALIAS1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (ks.size() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            throw new Exception("KeyStore not empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        // test 2: translate to session object, then add entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        KeyFactory kf = KeyFactory.getInstance(key.getAlgorithm(), p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        PrivateKey key3 = (PrivateKey)kf.translateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        System.out.println(toString(key3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        sign(p, key3, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        ks.setKeyEntry(ALIAS2, key3, null, chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        aliases = aliases(ks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (aliases.size() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            throw new Exception("size not 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (aliases.get(0).equals(ALIAS2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            throw new Exception("alias mismatch: " + aliases);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        PrivateKey key4 = (PrivateKey)ks.getKey(ALIAS2, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        System.out.println(toString(key4));
32635
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   181
        X509Certificate[] chain4 = (X509Certificate[])
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   182
                ks.getCertificateChain(ALIAS2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (Arrays.equals(chain, chain4) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            throw new Exception("chain mismatch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        sign(p, key4, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // test 3: change alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        ks.setKeyEntry(ALIAS3, key3, null, chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        aliases = aliases(ks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (aliases.size() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw new Exception("size not 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (aliases.get(0).equals(ALIAS3) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            throw new Exception("alias mismatch: " + aliases);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        PrivateKey key5 = (PrivateKey)ks.getKey(ALIAS3, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        System.out.println(toString(key5));
32635
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   200
        X509Certificate[] chain5 = (X509Certificate[])
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   201
                ks.getCertificateChain(ALIAS3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (Arrays.equals(chain, chain5) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            throw new Exception("chain mismatch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        sign(p, key5, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        ks.deleteEntry(ALIAS3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (ks.size() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            throw new Exception("KeyStore not empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        System.out.println("OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
32635
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   215
    private static void sign(Provider p, PrivateKey privateKey,
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   216
            PublicKey publicKey) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        String keyAlg = privateKey.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        String alg;
32635
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   219
        switch (keyAlg) {
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   220
            case "RSA":
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   221
                alg = "SHA1withRSA";
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   222
                break;
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   223
            case "DSA":
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   224
                alg = "SHA1withDSA";
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   225
                break;
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   226
            case "EC":
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   227
                alg = "SHA1withECDSA";
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   228
                break;
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   229
            default:
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 30046
diff changeset
   230
                throw new Exception("Unknown algorithm " + keyAlg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        Signature s = Signature.getInstance(alg, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        s.initSign(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        s.update(DATA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        byte[] sig = s.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        s.initVerify(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        s.update(DATA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (s.verify(sig) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            throw new Exception("Signature did not verify");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    private static String toString(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        String s = String.valueOf(o).split("\n")[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return (s.length() <= MAX_LINE) ? s : s.substring(0, MAX_LINE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
}