jdk/test/sun/security/pkcs11/Signature/TestRSAKeyLength.java
author valeriep
Tue, 08 May 2012 17:57:48 -0700
changeset 12685 8a448b5b9006
parent 5506 202f599c92aa
child 18804 8561f2098727
permissions -rw-r--r--
4963723: Implement SHA-224 Summary: Add support for SHA-224, SHA224withRSA, SHA224withECDSA, HmacSHA224 and OAEPwithSHA-224AndMGF1Padding. Reviewed-by: vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5155
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
     1
/*
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
     2
 * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
5155
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
     4
 *
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
     8
 *
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    13
 * accompanied this code).
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    14
 *
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5155
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5155
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5155
diff changeset
    21
 * questions.
5155
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    22
 */
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    23
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    24
/**
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    25
 * @test %W% %E%
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    26
 * @bug 6695485
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    27
 * @summary Make sure initSign/initVerify() check RSA key lengths
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    28
 * @author Yu-Ching Valerie Peng
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    29
 * @library ..
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    30
 */
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    31
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    32
import java.security.*;
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    33
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    34
public class TestRSAKeyLength extends PKCS11Test {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    35
    public static void main(String[] args) throws Exception {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    36
        main(new TestRSAKeyLength());
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    37
    }
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    38
    public void main(Provider p) throws Exception {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    39
        boolean isValidKeyLength[] = { true, true, false, false };
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    40
        String algos[] = { "SHA1withRSA", "SHA224withRSA", "SHA256withRSA",
5155
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    41
                           "SHA384withRSA", "SHA512withRSA" };
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    42
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    43
        kpg.initialize(512);
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    44
        KeyPair kp = kpg.generateKeyPair();
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    45
        PrivateKey privKey = kp.getPrivate();
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    46
        PublicKey pubKey = kp.getPublic();
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    47
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    48
        for (int i = 0; i < algos.length; i++) {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    49
            Signature sig = Signature.getInstance(algos[i], p);
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    50
            System.out.println("Testing RSA signature " + algos[i]);
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    51
            try {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    52
                sig.initSign(privKey);
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    53
                if (!isValidKeyLength[i]) {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    54
                    throw new Exception("initSign: Expected IKE not thrown!");
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    55
                }
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    56
            } catch (InvalidKeyException ike) {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    57
                if (isValidKeyLength[i]) {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    58
                    throw new Exception("initSign: Unexpected " + ike);
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    59
                }
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    60
            }
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    61
            try {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    62
                sig.initVerify(pubKey);
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    63
                if (!isValidKeyLength[i]) {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    64
                    throw new RuntimeException("initVerify: Expected IKE not thrown!");
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    65
                }
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    66
                new SignedObject("Test string for getSignature test.", privKey, sig);
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    67
            } catch (InvalidKeyException ike) {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    68
                if (isValidKeyLength[i]) {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    69
                    throw new Exception("initSign: Unexpected " + ike);
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    70
                }
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    71
            }
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    72
        }
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    73
    }
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
diff changeset
    74
}