test/jdk/sun/security/pkcs11/rsa/TestKeyFactory.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) 2003, 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 4856966
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test KeyFactory of the new RSA provider
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
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    31
 * @run main/othervm TestKeyFactory
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    32
 * @run main/othervm TestKeyFactory sm rsakeys.ks.policy
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class TestKeyFactory extends PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final char[] password = "test12".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static KeyStore getKeyStore() throws Exception {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    46
        KeyStore ks;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    47
        try (InputStream in = new FileInputStream(new File(BASE, "rsakeys.ks"))) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    48
            ks = KeyStore.getInstance("JKS");
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    49
            ks.load(in, password);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    50
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        return ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Test that key1 (reference key) and key2 (key to be tested) are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static void testKey(Key key1, Key key2) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (key2.getAlgorithm().equals("RSA") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            throw new Exception("Algorithm not RSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (key1 instanceof PublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            if (key2.getFormat().equals("X.509") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                throw new Exception("Format not X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        } else if (key1 instanceof PrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            if (key2.getFormat().equals("PKCS#8") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                throw new Exception("Format not PKCS#8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (key1.equals(key2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            throw new Exception("Keys not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (Arrays.equals(key1.getEncoded(), key2.getEncoded()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            throw new Exception("Encodings not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static void testPublic(KeyFactory kf, PublicKey key) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        System.out.println("Testing public key...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        PublicKey key2 = (PublicKey)kf.translateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        KeySpec rsaSpec = kf.getKeySpec(key, RSAPublicKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        PublicKey key3 = kf.generatePublic(rsaSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        KeySpec x509Spec = kf.getKeySpec(key, X509EncodedKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        PublicKey key4 = kf.generatePublic(x509Spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        KeySpec x509Spec2 = new X509EncodedKeySpec(key.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        PublicKey key5 = kf.generatePublic(x509Spec2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        testKey(key, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        testKey(key, key2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        testKey(key, key3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        testKey(key, key4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        testKey(key, key5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static void testPrivate(KeyFactory kf, PrivateKey key) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        System.out.println("Testing private key...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        PrivateKey key2 = (PrivateKey)kf.translateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        KeySpec rsaSpec = kf.getKeySpec(key, RSAPrivateCrtKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        PrivateKey key3 = kf.generatePrivate(rsaSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        KeySpec pkcs8Spec = kf.getKeySpec(key, PKCS8EncodedKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        PrivateKey key4 = kf.generatePrivate(pkcs8Spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        KeySpec pkcs8Spec2 = new PKCS8EncodedKeySpec(key.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        PrivateKey key5 = kf.generatePrivate(pkcs8Spec2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        testKey(key, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        testKey(key, key2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        testKey(key, key3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        testKey(key, key4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        testKey(key, key5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // XXX PKCS#11 providers may not support non-CRT keys (e.g. NSS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
//      KeySpec rsaSpec2 = kf.getKeySpec(key, RSAPrivateKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
//      PrivateKey key6 = kf.generatePrivate(rsaSpec2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
//      RSAPrivateKey rsaKey = (RSAPrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
//      KeySpec rsaSpec3 = new RSAPrivateKeySpec(rsaKey.getModulus(), rsaKey.getPrivateExponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
//      PrivateKey key7 = kf.generatePrivate(rsaSpec3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
//      testKey(key6, key6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
//      testKey(key6, key7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private static void test(KeyFactory kf, Key key) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (key.getAlgorithm().equals("RSA") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            System.out.println("Not an RSA key, ignoring");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (key instanceof PublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            testPublic(kf, (PublicKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        } else if (key instanceof PrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            testPrivate(kf, (PrivateKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public static void main(String[] args) throws Exception {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
   132
        main(new TestKeyFactory(), args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
   135
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public void main(Provider p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        long start = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        KeyStore ks = getKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        KeyFactory kf = KeyFactory.getInstance("RSA", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            String alias = (String)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            Key key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (ks.isKeyEntry(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                test(kf, ks.getKey(alias, password));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                test(kf, ks.getCertificate(alias).getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        long stop = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        System.out.println("All tests passed (" + (stop - start) + " ms).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
}