2234N/A/*
2362N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2234N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2234N/A *
2234N/A * This code is free software; you can redistribute it and/or modify it
2234N/A * under the terms of the GNU General Public License version 2 only, as
2234N/A * published by the Free Software Foundation.
2234N/A *
2234N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2234N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2234N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2234N/A * version 2 for more details (a copy is included in the LICENSE file that
2234N/A * accompanied this code).
2234N/A *
2234N/A * You should have received a copy of the GNU General Public License version
2234N/A * 2 along with this work; if not, write to the Free Software Foundation,
2234N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2234N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2234N/A */
2234N/A
2234N/A/**
2234N/A * @test %W% %E%
2234N/A * @bug 6695485
2234N/A * @summary Make sure initSign/initVerify() check RSA key lengths
2234N/A * @author Yu-Ching Valerie Peng
2234N/A * @library ..
2234N/A */
2234N/A
2234N/Aimport java.security.*;
2234N/A
2234N/Apublic class TestRSAKeyLength extends PKCS11Test {
2234N/A public static void main(String[] args) throws Exception {
2234N/A main(new TestRSAKeyLength());
2234N/A }
2234N/A public void main(Provider p) throws Exception {
2234N/A boolean isValidKeyLength[] = { true, true, false, false };
2234N/A String algos[] = { "SHA1withRSA", "SHA256withRSA",
2234N/A "SHA384withRSA", "SHA512withRSA" };
2234N/A KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);
2234N/A kpg.initialize(512);
2234N/A KeyPair kp = kpg.generateKeyPair();
2234N/A PrivateKey privKey = kp.getPrivate();
2234N/A PublicKey pubKey = kp.getPublic();
2234N/A
2234N/A for (int i = 0; i < algos.length; i++) {
2234N/A Signature sig = Signature.getInstance(algos[i], p);
2234N/A System.out.println("Testing RSA signature " + algos[i]);
2234N/A try {
2234N/A sig.initSign(privKey);
2234N/A if (!isValidKeyLength[i]) {
2234N/A throw new Exception("initSign: Expected IKE not thrown!");
2234N/A }
2234N/A } catch (InvalidKeyException ike) {
2234N/A if (isValidKeyLength[i]) {
2234N/A throw new Exception("initSign: Unexpected " + ike);
2234N/A }
2234N/A }
2234N/A try {
2234N/A sig.initVerify(pubKey);
2234N/A if (!isValidKeyLength[i]) {
2234N/A throw new RuntimeException("initVerify: Expected IKE not thrown!");
2234N/A }
2234N/A new SignedObject("Test string for getSignature test.", privKey, sig);
2234N/A } catch (InvalidKeyException ike) {
2234N/A if (isValidKeyLength[i]) {
2234N/A throw new Exception("initSign: Unexpected " + ike);
2234N/A }
2234N/A }
2234N/A }
2234N/A }
2234N/A}