Path: blob/master/test/jdk/sun/security/rsa/TestKeyPairGeneratorExponent.java
41149 views
/*1* Copyright (c) 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 821601226* @summary Tests the RSA public key exponent for KeyPairGenerator27* @run main/timeout=60 TestKeyPairGeneratorExponent28*/2930import java.math.BigInteger;3132import java.security.*;33import java.security.interfaces.*;34import java.security.spec.*;3536public class TestKeyPairGeneratorExponent {37private static int keyLen = 512;3839private static BigInteger[] validExponents = new BigInteger[] {40RSAKeyGenParameterSpec.F0,41RSAKeyGenParameterSpec.F4,42BigInteger.ONE.shiftLeft(keyLen - 1).subtract(BigInteger.ONE)43};4445private static BigInteger[] invalidExponents = new BigInteger[] {46BigInteger.valueOf(-1),47BigInteger.ZERO,48BigInteger.ONE,49// without this fix, an even value causes an infinite loop50BigInteger.valueOf(4)51};5253public static void testValidExponents(KeyPairGenerator kpg,54BigInteger exponent) {55System.out.println("Testing exponent = " + exponent.toString(16));56try {57kpg.initialize(new RSAKeyGenParameterSpec(keyLen, exponent));58kpg.generateKeyPair();59System.out.println("OK, key pair generated");60} catch(InvalidAlgorithmParameterException iape){61throw new RuntimeException("Error: Unexpected Exception: " + iape);62}63}6465public static void testInvalidExponents(KeyPairGenerator kpg,66BigInteger exponent) {67System.out.println("Testing exponent = " + exponent.toString(16));68try {69kpg.initialize(new RSAKeyGenParameterSpec(keyLen, exponent));70kpg.generateKeyPair();71throw new RuntimeException("Error: Expected IAPE not thrown.");72} catch(InvalidAlgorithmParameterException iape){73// Expected InvalidAlgorithmParameterException was thrown74System.out.println("OK, expected IAPE thrown");75} catch(Exception e) {76e.printStackTrace();77throw new RuntimeException("Error: unexpected exception " + e);78}79}8081public static void main(String[] args) throws Exception {82KeyPairGenerator kpg =83KeyPairGenerator.getInstance("RSA", "SunRsaSign");8485for(BigInteger validExponent : validExponents) {86testValidExponents(kpg, validExponent);87}8889for(BigInteger invalidExponent : invalidExponents) {90testInvalidExponents(kpg, invalidExponent);91}92}93}949596