test/jdk/sun/security/pkcs11/Cipher/TestRSACipherWrap.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:
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
     1
/*
51460
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 47216
diff changeset
     2
 * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
     4
 *
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
     8
 *
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    13
 * accompanied this code).
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    14
 *
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3111
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3111
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3111
diff changeset
    21
 * questions.
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    22
 */
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    23
40975
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 35379
diff changeset
    24
/*
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    25
 * @test
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    26
 * @bug 6572331 6994008
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    27
 * @summary basic test for RSA cipher key wrapping functionality
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    28
 * @author Valerie Peng
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: 8578
diff changeset
    31
 * @run main/othervm TestRSACipherWrap
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 8578
diff changeset
    32
 * @run main/othervm TestRSACipherWrap sm
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    33
 */
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    34
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 8578
diff changeset
    35
import java.security.GeneralSecurityException;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 8578
diff changeset
    36
import java.security.InvalidParameterException;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 8578
diff changeset
    37
import java.security.Key;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 8578
diff changeset
    38
import java.security.KeyPair;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 8578
diff changeset
    39
import java.security.KeyPairGenerator;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 8578
diff changeset
    40
import java.security.Provider;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 8578
diff changeset
    41
import java.util.Arrays;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 8578
diff changeset
    42
import javax.crypto.Cipher;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 8578
diff changeset
    43
import javax.crypto.KeyGenerator;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 8578
diff changeset
    44
import javax.crypto.SecretKey;
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    45
import javax.crypto.spec.SecretKeySpec;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    46
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    47
public class TestRSACipherWrap extends PKCS11Test {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    48
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    49
    private static final String[] RSA_ALGOS =
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    50
        { "RSA/ECB/PKCS1Padding", "RSA" };
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    51
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 8578
diff changeset
    52
    @Override
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    53
    public void main(Provider p) throws Exception {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    54
        try {
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    55
            Cipher.getInstance(RSA_ALGOS[0], p);
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    56
        } catch (GeneralSecurityException e) {
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    57
            System.out.println(RSA_ALGOS[0] + " unsupported, skipping");
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    58
            return;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    59
        }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    60
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    61
        kpg.initialize(1024);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    62
        KeyPair kp = kpg.generateKeyPair();
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    63
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    64
        for (String rsaAlgo: RSA_ALGOS) {
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    65
            Cipher cipherPKCS11 = Cipher.getInstance(rsaAlgo, p);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    66
            Cipher cipherJce = Cipher.getInstance(rsaAlgo, "SunJCE");
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    67
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    68
            String algos[] = {"AES", "RC2", "Blowfish"};
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    69
            int keySizes[] = {128, 256};
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    70
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    71
            for (int j = 0; j < algos.length; j++) {
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    72
                String algorithm = algos[j];
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    73
                KeyGenerator keygen =
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    74
                    KeyGenerator.getInstance(algorithm);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    75
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    76
                for (int i = 0; i < keySizes.length; i++) {
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    77
                    SecretKey secretKey = null;
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    78
                    System.out.print("Generate " + keySizes[i] + "-bit " +
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    79
                        algorithm + " key using ");
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    80
                    try {
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    81
                        keygen.init(keySizes[i]);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    82
                        secretKey = keygen.generateKey();
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    83
                        System.out.println(keygen.getProvider().getName());
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    84
                    } catch (InvalidParameterException ipe) {
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    85
                        secretKey = new SecretKeySpec(new byte[32], algorithm);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    86
                        System.out.println("SecretKeySpec class");
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    87
                    }
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    88
                    test(kp, secretKey, cipherPKCS11, cipherJce);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    89
                    test(kp, secretKey, cipherPKCS11, cipherPKCS11);
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 5506
diff changeset
    90
                    test(kp, secretKey, cipherJce, cipherPKCS11);
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    91
                }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    92
            }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    93
        }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    94
    }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    95
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    96
    private static void test(KeyPair kp, SecretKey secretKey,
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    97
            Cipher wrapCipher, Cipher unwrapCipher)
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    98
            throws Exception {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
    99
        String algo = secretKey.getAlgorithm();
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   100
        wrapCipher.init(Cipher.WRAP_MODE, kp.getPublic());
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   101
        byte[] wrappedKey = wrapCipher.wrap(secretKey);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   102
        unwrapCipher.init(Cipher.UNWRAP_MODE, kp.getPrivate());
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   103
        Key unwrappedKey =
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   104
                unwrapCipher.unwrap(wrappedKey, algo, Cipher.SECRET_KEY);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   105
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   106
        System.out.println("Test " + wrapCipher.getProvider().getName() +
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   107
                "/" + unwrapCipher.getProvider().getName() + ": ");
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   108
        if (!Arrays.equals(secretKey.getEncoded(),
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   109
                unwrappedKey.getEncoded())) {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   110
            throw new Exception("Test Failed!");
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   111
        }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   112
        System.out.println("Passed");
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   113
    }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   114
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   115
    public static void main(String[] args) throws Exception {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 8578
diff changeset
   116
        main(new TestRSACipherWrap(), args);
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   117
    }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents:
diff changeset
   118
}