Path: blob/master/test/jdk/sun/security/pkcs11/Signature/TestRSAKeyLength.java
41153 views
/*1* Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6695485 824233226* @summary Make sure initSign/initVerify() check RSA key lengths27* @author Yu-Ching Valerie Peng28* @library /test/lib ..29* @modules jdk.crypto.cryptoki30* @run main/othervm TestRSAKeyLength31* @run main/othervm -Djava.security.manager=allow TestRSAKeyLength sm32*/3334import java.security.InvalidKeyException;35import java.security.KeyPair;36import java.security.KeyPairGenerator;37import java.security.PrivateKey;38import java.security.Provider;39import java.security.PublicKey;40import java.security.Signature;41import java.security.SignedObject;4243public class TestRSAKeyLength extends PKCS11Test {4445public static void main(String[] args) throws Exception {46main(new TestRSAKeyLength(), args);47}4849@Override50public void main(Provider p) throws Exception {5152boolean isValidKeyLength[] = {53true, true, true, false, false, true, true, false, false54};55String algos[] = {56"SHA1withRSA", "SHA224withRSA", "SHA256withRSA",57"SHA384withRSA", "SHA512withRSA", "SHA3-224withRSA",58"SHA3-256withRSA", "SHA3-384withRSA", "SHA3-512withRSA"59};60KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);61kpg.initialize(512);62KeyPair kp = kpg.generateKeyPair();63PrivateKey privKey = kp.getPrivate();64PublicKey pubKey = kp.getPublic();6566if (algos.length != isValidKeyLength.length) {67throw new Exception("Internal Error: number of test algos" +68" and results length mismatch!");69}70for (int i = 0; i < algos.length; i++) {71Signature sig = Signature.getInstance(algos[i], p);72System.out.println("Testing RSA signature " + algos[i]);73try {74sig.initSign(privKey);75if (!isValidKeyLength[i]) {76throw new Exception("initSign: Expected IKE not thrown!");77}78} catch (InvalidKeyException ike) {79if (isValidKeyLength[i]) {80throw new Exception("initSign: Unexpected " + ike);81}82}83try {84sig.initVerify(pubKey);85if (!isValidKeyLength[i]) {86throw new RuntimeException("initVerify: Expected IKE not thrown!");87}88new SignedObject("Test string for getSignature test.", privKey, sig);89} catch (InvalidKeyException ike) {90if (isValidKeyLength[i]) {91throw new Exception("initSign: Unexpected " + ike);92}93}94}95}96}979899