jdk/test/sun/security/pkcs11/Signature/TestDSAKeyLength.java
author valeriep
Mon, 25 Nov 2013 11:56:53 -0800
changeset 21848 3902d25a64b1
child 21962 fb94f324e7f3
permissions -rw-r--r--
7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init() Summary: Add key length checks to P11Signature class Reviewed-by: mullan
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
/*
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
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
 */
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    23
/*
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    24
 * @test
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    25
 * @bug 7200306
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    26
 * @run main/othervm/timeout=250 TestDSAKeyLength
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
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    29
 */
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    30
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    31
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    32
import java.security.*;
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    33
import java.security.spec.*;
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    34
import java.security.interfaces.*;
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    35
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    36
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
    37
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    38
    public static void main(String[] args) throws Exception {
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    39
        main(new TestDSAKeyLength());
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    40
    }
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    41
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    42
    public void main(Provider provider) throws Exception {
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    43
        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
    44
        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
    45
        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
    46
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    47
        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
    48
        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
    49
        try {
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    50
            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
    51
            status = false;
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    52
        } 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
    53
            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
    54
        }
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    55
        try {
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    56
            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
    57
            status = false;
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    58
        } 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
    59
            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
    60
        }
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    61
        if (status) {
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    62
            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
    63
        } else {
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    64
            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
    65
        }
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    66
    }
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents:
diff changeset
    67
}