jdk/test/sun/security/pkcs11/Signature/TestDSAKeyLength.java
author ascarpino
Mon, 23 Jan 2017 11:49:01 -0800
changeset 43248 5e15de85a1a0
parent 42693 6645de32a866
permissions -rw-r--r--
8172527: Rename jdk.crypto.token to jdk.crypto.cryptoki Reviewed-by: mchung, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21848
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
     1
/*
43248
5e15de85a1a0 8172527: Rename jdk.crypto.token to jdk.crypto.cryptoki
ascarpino
parents: 42693
diff changeset
     2
 * Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
21848
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
     4
 *
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
     8
 *
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    13
 * accompanied this code).
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    14
 *
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    18
 *
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    21
 * questions.
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    22
 */
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    23
21848
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    24
/*
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    25
 * @test
21962
fb94f324e7f3 8029158: sun/security/pkcs11/Signature/TestDSAKeyLength.java does not compile (or run)
valeriep
parents: 21848
diff changeset
    26
 * @bug 7200306 8029158
21848
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    27
 * @summary verify that P11Signature impl will error out when initialized
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    28
 * with unsupported key sizes
21962
fb94f324e7f3 8029158: sun/security/pkcs11/Signature/TestDSAKeyLength.java does not compile (or run)
valeriep
parents: 21848
diff changeset
    29
 * @library ..
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 21962
diff changeset
    30
 * @key randomness
43248
5e15de85a1a0 8172527: Rename jdk.crypto.token to jdk.crypto.cryptoki
ascarpino
parents: 42693
diff changeset
    31
 * @modules jdk.crypto.cryptoki
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    32
 * @run main/othervm TestDSAKeyLength
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    33
 * @run main/othervm TestDSAKeyLength sm
21848
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    34
 */
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    35
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    36
import java.security.InvalidKeyException;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    37
import java.security.KeyPair;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    38
import java.security.KeyPairGenerator;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    39
import java.security.Provider;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    40
import java.security.SecureRandom;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    41
import java.security.Signature;
21848
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    42
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    43
public class TestDSAKeyLength extends PKCS11Test {
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    44
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    45
    public static void main(String[] args) throws Exception {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    46
        main(new TestDSAKeyLength(), args);
21848
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    47
    }
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    48
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    49
    @Override
21848
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    50
    public void main(Provider provider) throws Exception {
21962
fb94f324e7f3 8029158: sun/security/pkcs11/Signature/TestDSAKeyLength.java does not compile (or run)
valeriep
parents: 21848
diff changeset
    51
        if (isNSS(provider) && getNSSVersion() >= 3.14) {
fb94f324e7f3 8029158: sun/security/pkcs11/Signature/TestDSAKeyLength.java does not compile (or run)
valeriep
parents: 21848
diff changeset
    52
            System.out.println("Skip testing NSS " + getNSSVersion());
fb94f324e7f3 8029158: sun/security/pkcs11/Signature/TestDSAKeyLength.java does not compile (or run)
valeriep
parents: 21848
diff changeset
    53
            return;
fb94f324e7f3 8029158: sun/security/pkcs11/Signature/TestDSAKeyLength.java does not compile (or run)
valeriep
parents: 21848
diff changeset
    54
        }
fb94f324e7f3 8029158: sun/security/pkcs11/Signature/TestDSAKeyLength.java does not compile (or run)
valeriep
parents: 21848
diff changeset
    55
32138
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
    56
        /*
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
    57
         * Use Solaris SPARC 11.2 or later to avoid an intermittent failure
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
    58
         * when running SunPKCS11-Solaris (8044554)
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
    59
         */
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
    60
        if (provider.getName().equals("SunPKCS11-Solaris") &&
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    61
            props.getProperty("os.name").equals("SunOS") &&
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    62
            props.getProperty("os.arch").equals("sparcv9") &&
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    63
            props.getProperty("os.version").compareTo("5.11") <= 0 &&
32138
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
    64
            getDistro().compareTo("11.2") < 0) {
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
    65
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
    66
            System.out.println("SunPKCS11-Solaris provider requires " +
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
    67
                "Solaris SPARC 11.2 or later, skipping");
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
    68
            return;
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
    69
        }
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
    70
21848
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    71
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA", "SUN");
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    72
        kpg.initialize(2048, new SecureRandom());
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    73
        KeyPair pair = kpg.generateKeyPair();
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    74
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    75
        boolean status = true;
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    76
        Signature sig = Signature.getInstance("SHA1withDSA", provider);
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    77
        try {
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    78
            sig.initSign(pair.getPrivate());
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    79
            status = false;
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    80
        } catch (InvalidKeyException ike) {
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    81
            System.out.println("Expected IKE thrown for initSign()");
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    82
        }
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    83
        try {
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    84
            sig.initVerify(pair.getPublic());
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    85
            status = false;
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    86
        } catch (InvalidKeyException ike) {
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    87
            System.out.println("Expected IKE thrown for initVerify()");
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    88
        }
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    89
        if (status) {
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    90
            System.out.println("Test Passed");
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    91
        } else {
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    92
            throw new Exception("Test Failed - expected IKE not thrown");
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    93
        }
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    94
    }
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    95
}