test/jdk/sun/security/pkcs11/ec/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) 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 6405536
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test the P11ECKeyFactory
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: 21596
diff changeset
    31
 * @run main/othervm TestKeyFactory
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 21596
diff changeset
    32
 * @run main/othervm TestKeyFactory sm
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: 21596
diff changeset
    35
import java.security.Key;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 21596
diff changeset
    36
import java.security.KeyFactory;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 21596
diff changeset
    37
import java.security.KeyPair;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 21596
diff changeset
    38
import java.security.KeyPairGenerator;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 21596
diff changeset
    39
import java.security.PrivateKey;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 21596
diff changeset
    40
import java.security.Provider;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 21596
diff changeset
    41
import java.security.PublicKey;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 21596
diff changeset
    42
import java.security.spec.ECPrivateKeySpec;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 21596
diff changeset
    43
import java.security.spec.ECPublicKeySpec;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 21596
diff changeset
    44
import java.security.spec.KeySpec;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 21596
diff changeset
    45
import java.security.spec.PKCS8EncodedKeySpec;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 21596
diff changeset
    46
import java.security.spec.X509EncodedKeySpec;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 21596
diff changeset
    47
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class TestKeyFactory extends PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Test that key1 (reference key) and key2 (key to be tested) are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static void testKey(Key key1, Key key2) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        if (key2.getAlgorithm().equals("EC") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            throw new Exception("Algorithm not EC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (key1 instanceof PublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            if (key2.getFormat().equals("X.509") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                throw new Exception("Format not X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        } else if (key1 instanceof PrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            if (key2.getFormat().equals("PKCS#8") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                throw new Exception("Format not PKCS#8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (key1.equals(key2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            System.out.println("key1: " + key1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            System.out.println("key2: " + key2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            System.out.println("enc1: " + toString(key1.getEncoded()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            System.out.println("enc2: " + toString(key2.getEncoded()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            throw new Exception("Keys not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (Arrays.equals(key1.getEncoded(), key2.getEncoded()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throw new Exception("Encodings not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static void testPublic(KeyFactory kf, PublicKey key) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        System.out.println("Testing public key...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        PublicKey key2 = (PublicKey)kf.translateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        KeySpec keySpec = kf.getKeySpec(key, ECPublicKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        PublicKey key3 = kf.generatePublic(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        KeySpec x509Spec = kf.getKeySpec(key, X509EncodedKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        PublicKey key4 = kf.generatePublic(x509Spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        KeySpec x509Spec2 = new X509EncodedKeySpec(key.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        PublicKey key5 = kf.generatePublic(x509Spec2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        testKey(key, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        testKey(key, key2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        testKey(key, key3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        testKey(key, key4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        testKey(key, key5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private static void testPrivate(KeyFactory kf, PrivateKey key) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        System.out.println("Testing private key...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        PrivateKey key2 = (PrivateKey)kf.translateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        KeySpec keySpec = kf.getKeySpec(key, ECPrivateKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        PrivateKey key3 = kf.generatePrivate(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        KeySpec pkcs8Spec = kf.getKeySpec(key, PKCS8EncodedKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        PrivateKey key4 = kf.generatePrivate(pkcs8Spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        KeySpec pkcs8Spec2 = new PKCS8EncodedKeySpec(key.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        PrivateKey key5 = kf.generatePrivate(pkcs8Spec2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        testKey(key, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        testKey(key, key2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        testKey(key, key3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        testKey(key, key4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        testKey(key, key5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private static void test(KeyFactory kf, Key key) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (key.getAlgorithm().equals("EC") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            throw new Exception("Not an EC key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (key instanceof PublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            testPublic(kf, (PublicKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        } else if (key instanceof PrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            testPrivate(kf, (PrivateKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public static void main(String[] args) throws Exception {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 21596
diff changeset
   124
        main(new TestKeyFactory(), args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 21596
diff changeset
   127
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public void main(Provider p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (p.getService("KeyFactory", "EC") == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            System.out.println("Provider does not support EC, skipping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 5506
diff changeset
   133
        int[] keyLengths = {192, 163, 409, 521};
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 5506
diff changeset
   134
        int len = 0;
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 5506
diff changeset
   135
        if (getNSSECC() == ECCState.Basic) {
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 5506
diff changeset
   136
            System.out.println("NSS Basic ECC only. Skipping 192, 163, & 409");
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 5506
diff changeset
   137
            len = 3;
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 5506
diff changeset
   138
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        KeyFactory kf = KeyFactory.getInstance("EC", p);
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 5506
diff changeset
   140
        for (; keyLengths.length > len ; len++) {
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 5506
diff changeset
   141
            System.out.println("Length "+keyLengths[len]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p);
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 5506
diff changeset
   143
            kpg.initialize(keyLengths[len]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            KeyPair kp = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            test(kf, kp.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            test(kf, kp.getPublic());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
}