Path: blob/master/test/jdk/sun/security/rsa/SpecTest.java
41149 views
/*1* Copyright (c) 2015, 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 8044199 813723126* @summary Check same KeyPair's private key and public key have same modulus.27* also check public key's public exponent equals to given spec's public28* exponent. Only key size 1024 is tested with RSAKeyGenParameterSpec.F0 (3).29* @run main SpecTest 51230* @run main SpecTest 76831* @run main SpecTest 102432* @run main SpecTest 1024 16797133* @run main SpecTest 204834* @run main/timeout=240 SpecTest 409635* @run main/timeout=240 SpecTest 512036*/37import java.math.BigInteger;38import java.security.KeyPair;39import java.security.KeyPairGenerator;40import java.security.interfaces.RSAKey;41import java.security.interfaces.RSAPrivateKey;42import java.security.interfaces.RSAPublicKey;43import java.security.spec.RSAKeyGenParameterSpec;4445public class SpecTest {4647/**48* ALGORITHM name, fixed as RSA.49*/50private static final String KEYALG = "RSA";5152/**53* JDK default RSA Provider.54*/55private static final String PROVIDER = "SunRsaSign";5657/**58*59* @param kpair test key pair60* @param pubExponent expected public exponent.61* @return true if test passed. false if test failed.62*/63private static boolean specTest(KeyPair kpair, BigInteger pubExponent) {64boolean passed = true;65RSAPrivateKey priv = (RSAPrivateKey) kpair.getPrivate();66RSAPublicKey pub = (RSAPublicKey) kpair.getPublic();6768// test the getModulus method69if ((priv instanceof RSAKey) && (pub instanceof RSAKey)) {70if (!priv.getModulus().equals(pub.getModulus())) {71System.out.println("priv.getModulus() = " + priv.getModulus());72System.out.println("pub.getModulus() = " + pub.getModulus());73passed = false;74}7576if (!pubExponent.equals(pub.getPublicExponent())) {77System.out.println("pubExponent = " + pubExponent);78System.out.println("pub.getPublicExponent() = "79+ pub.getPublicExponent());80passed = false;81}82}83return passed;84}8586public static void main(String[] args) throws Exception {8788int size = 0;8990if (args.length >= 1) {91size = Integer.parseInt(args[0]);92} else {93throw new RuntimeException("Missing keysize to test with");94}9596BigInteger publicExponent97= (args.length >= 2) ? new BigInteger(args[1]) : RSAKeyGenParameterSpec.F4;9899System.out.println("Running test with key size: " + size100+ " and public exponent: " + publicExponent);101102KeyPairGenerator kpg1 = KeyPairGenerator.getInstance(KEYALG, PROVIDER);103kpg1.initialize(new RSAKeyGenParameterSpec(size, publicExponent));104if (!specTest(kpg1.generateKeyPair(), publicExponent)) {105throw new RuntimeException("Test failed.");106}107}108}109110111