Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Signature/TestRSAKeyLength.java
38855 views
/*1* Copyright (c) 2010, 2016, 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* @test %W% %E%25* @bug 669548526* @summary Make sure initSign/initVerify() check RSA key lengths27* @author Yu-Ching Valerie Peng28* @library ..29* @run main/othervm TestRSAKeyLength30* @run main/othervm TestRSAKeyLength sm31*/3233import java.security.InvalidKeyException;34import java.security.KeyPair;35import java.security.KeyPairGenerator;36import java.security.PrivateKey;37import java.security.Provider;38import java.security.PublicKey;39import java.security.Signature;40import java.security.SignedObject;4142public class TestRSAKeyLength extends PKCS11Test {4344public static void main(String[] args) throws Exception {45main(new TestRSAKeyLength(), args);46}4748@Override49public void main(Provider p) throws Exception {50boolean isValidKeyLength[] = { true, true, true, false, false };51String algos[] = { "SHA1withRSA", "SHA224withRSA", "SHA256withRSA",52"SHA384withRSA", "SHA512withRSA" };53KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);54kpg.initialize(512);55KeyPair kp = kpg.generateKeyPair();56PrivateKey privKey = kp.getPrivate();57PublicKey pubKey = kp.getPublic();5859if (algos.length != isValidKeyLength.length) {60throw new Exception("Internal Error: number of test algos" +61" and results length mismatch!");62}63for (int i = 0; i < algos.length; i++) {64Signature sig = Signature.getInstance(algos[i], p);65System.out.println("Testing RSA signature " + algos[i]);66try {67sig.initSign(privKey);68if (!isValidKeyLength[i]) {69throw new Exception("initSign: Expected IKE not thrown!");70}71} catch (InvalidKeyException ike) {72if (isValidKeyLength[i]) {73throw new Exception("initSign: Unexpected " + ike);74}75}76try {77sig.initVerify(pubKey);78if (!isValidKeyLength[i]) {79throw new RuntimeException("initVerify: Expected IKE not thrown!");80}81new SignedObject("Test string for getSignature test.", privKey, sig);82} catch (InvalidKeyException ike) {83if (isValidKeyLength[i]) {84throw new Exception("initSign: Unexpected " + ike);85}86}87}88}89}909192