test/jdk/sun/security/pkcs11/KeyGenerator/TestKeyGenerator.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 43248 jdk/test/sun/security/pkcs11/KeyGenerator/TestKeyGenerator.java@5e15de85a1a0
child 51460 97e361fe3433
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
43248
5e15de85a1a0 8172527: Rename jdk.crypto.token to jdk.crypto.cryptoki
ascarpino
parents: 42693
diff changeset
     2
 * Copyright (c) 2003, 2017, 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
7286
4e48fc5922c6 6720456: New 4150 may have larger blowfish keysizes
valeriep
parents: 5506
diff changeset
    26
 * @bug 4917233 6461727 6490213 6720456
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary test the KeyGenerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @library ..
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: 7668
diff changeset
    31
 * @run main/othervm TestKeyGenerator
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 7668
diff changeset
    32
 * @run main/othervm TestKeyGenerator 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: 7668
diff changeset
    35
import java.security.InvalidParameterException;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 7668
diff changeset
    36
import java.security.NoSuchAlgorithmException;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 7668
diff changeset
    37
import java.security.Provider;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 7668
diff changeset
    38
import java.security.ProviderException;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 7668
diff changeset
    39
import javax.crypto.KeyGenerator;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 7668
diff changeset
    40
import javax.crypto.SecretKey;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
enum TestResult {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    PASS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    FAIL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    TBD
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class TestKeyGenerator extends PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public static void main(String[] args) throws Exception {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 7668
diff changeset
    51
        main(new TestKeyGenerator(), args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private TestResult test(String algorithm, int keyLen, Provider p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                      TestResult expected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        TestResult actual = TestResult.TBD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        System.out.println("Testing " + algorithm + ", " + keyLen + " bits...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        KeyGenerator kg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            kg = KeyGenerator.getInstance(algorithm, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            System.out.println("Not supported, skipping: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            return TestResult.PASS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            kg.init(keyLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            actual = TestResult.PASS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        } catch (InvalidParameterException ipe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            actual = TestResult.FAIL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (actual == TestResult.PASS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                SecretKey key = kg.generateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                if (expected == TestResult.FAIL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    throw new Exception("Generated " + key +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                        " using invalid key length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            } catch (ProviderException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                throw (Exception) (new Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    ("key generation failed using valid length").initCause(e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (expected != TestResult.TBD && expected != actual) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            throw new Exception("Expected to " + expected + ", but " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                actual);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return actual;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 7668
diff changeset
    92
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public void main(Provider p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        test("DES", 0, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        test("DES", 56, p, TestResult.PASS); // ensure JCE-Compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        test("DES", 64, p, TestResult.PASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        test("DES", 128, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        test("DESede", 0, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        // Special handling since not all PKCS11 providers support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // 2-key DESede, e.g. SunPKCS11-Solaris.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        TestResult temp = test("DESede", 112, p, TestResult.TBD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        test("DESede", 128, p, temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        test("DESede", 168, p, TestResult.PASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        test("DESede", 192, p, TestResult.PASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        test("DESede", 64, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        test("DESede", 256, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        // Different PKCS11 impls have different ranges
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // of supported key sizes for variable-key-length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // algorithms.
7286
4e48fc5922c6 6720456: New 4150 may have larger blowfish keysizes
valeriep
parents: 5506
diff changeset
   112
        // Solaris> Blowfish: 32-128 or even 448 bits, RC4: 8-128 bits or as much as 2048 bits
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // NSS>     Blowfish: n/a,         RC4: 8-2048 bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // However, we explicitly disallowed key sizes less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // than 40-bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        test("Blowfish", 0, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        test("Blowfish", 24, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        test("Blowfish", 32, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        test("Blowfish", 40, p, TestResult.PASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        test("Blowfish", 128, p, TestResult.PASS);
7286
4e48fc5922c6 6720456: New 4150 may have larger blowfish keysizes
valeriep
parents: 5506
diff changeset
   122
        test("Blowfish", 136, p, TestResult.TBD);
4e48fc5922c6 6720456: New 4150 may have larger blowfish keysizes
valeriep
parents: 5506
diff changeset
   123
        test("Blowfish", 448, p, TestResult.TBD);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        test("Blowfish", 456, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        test("ARCFOUR", 0, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        test("ARCFOUR", 32, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        test("ARCFOUR", 40, p, TestResult.PASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        test("ARCFOUR", 128, p, TestResult.PASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (p.getName().equals("SunPKCS11-Solaris")) {
7286
4e48fc5922c6 6720456: New 4150 may have larger blowfish keysizes
valeriep
parents: 5506
diff changeset
   132
            test("ARCFOUR", 1024, p, TestResult.TBD);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } else if (p.getName().equals("SunPKCS11-NSS")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            test("ARCFOUR", 1024, p, TestResult.PASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            test("ARCFOUR", 2048, p, TestResult.PASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            test("ARCFOUR", 2056, p, TestResult.FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
}