Path: blob/master/test/jdk/sun/security/rsa/TestKeyPairGenerator.java
41152 views
/*1* Copyright (c) 2003, 2018, 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 4853305 4865198 4888410 4963723 814629326* @summary Verify that the RSA KeyPairGenerator works27* @library /test/lib28* @build jdk.test.lib.SigTestUtil29* @run main TestKeyPairGenerator30* @author Andreas Sterbenz31* @key randomness32*/3334import java.io.*;35import java.util.*;36import java.math.BigInteger;3738import java.security.*;39import java.security.interfaces.*;40import java.security.spec.*;4142import jdk.test.lib.SigTestUtil;43import static jdk.test.lib.SigTestUtil.SignatureType;4445public class TestKeyPairGenerator {4647private static Provider provider;4849private static byte[] data;5051private static void testSignature(SignatureType type, String mdAlg,52PrivateKey privateKey, PublicKey publicKey) throws53NoSuchAlgorithmException, InvalidKeyException, SignatureException {54System.out.println("Testing against " + mdAlg + "...");55String sigAlg = SigTestUtil.generateSigAlg(type, mdAlg);56Signature s = Signature.getInstance(sigAlg, provider);57s.initSign(privateKey);58s.update(data);59byte[] sig = s.sign();60s.initVerify(publicKey);61s.update(data);62boolean result = s.verify(sig);63if (result == false) {64throw new RuntimeException("Verification failed");65}66}6768private static void test(PrivateKey privateKey, PublicKey publicKey) throws Exception {6970int testSize = ((RSAPublicKey)publicKey).getModulus().bitLength();71System.out.println("modulus size = " + testSize);7273Iterable<String> md_alg_pkcs15 =74SigTestUtil.getDigestAlgorithms(SignatureType.RSA, testSize);75md_alg_pkcs15.forEach(mdAlg -> {76try {77testSignature(SignatureType.RSA, mdAlg, privateKey, publicKey);78} catch (NoSuchAlgorithmException | InvalidKeyException |79SignatureException ex) {80throw new RuntimeException(ex);81}82}83);84}8586// regression test for 486519887private static void testInvalidSignature(KeyPair kp1, KeyPair kp2) throws Exception {88System.out.println("Testing signature with incorrect key...");89Signature sig = Signature.getInstance("MD5withRSA", provider);90sig.initSign(kp1.getPrivate());91byte[] data = new byte[100];92sig.update(data);93byte[] signature = sig.sign();94sig.initVerify(kp1.getPublic());95sig.update(data);96if (sig.verify(signature) == false) {97throw new Exception("verification failed");98}99sig.initVerify(kp2.getPublic());100sig.update(data);101// verify needs to return false and not throw an Exception102try {103if (sig.verify(signature)) {104throw new Exception("verification unexpectedly succeeded");105}106} catch (SignatureException se) {107// Yet another kind of failure, OK.108}109}110111public static void main(String[] args) throws Exception {112long start = System.currentTimeMillis();113provider = Security.getProvider("SunRsaSign");114data = new byte[2048];115// keypair generation is very slow, test only a few short keys116int[] keyLengths = {512, 512, 1024};117BigInteger[] pubExps = {null, BigInteger.valueOf(3), null};118KeyPair[] keyPairs = new KeyPair[3];119new Random().nextBytes(data);120KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", provider);121for (int i = 0; i < keyLengths.length; i++) {122int len = keyLengths[i];123BigInteger exp = pubExps[i];124System.out.println("Generating " + len + " bit keypair...");125if (exp == null) {126kpg.initialize(len);127} else {128kpg.initialize(new RSAKeyGenParameterSpec(len, exp));129}130KeyPair kp = kpg.generateKeyPair();131keyPairs[i] = kp;132RSAPublicKey publicKey = (RSAPublicKey)kp.getPublic();133System.out.println(publicKey);134RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey)kp.getPrivate();135if (publicKey.getModulus().equals(privateKey.getModulus()) == false) {136throw new Exception("Moduli do not match");137}138if (publicKey.getPublicExponent().equals(privateKey.getPublicExponent()) == false) {139throw new Exception("Exponents do not match");140}141int keyLen = publicKey.getModulus().bitLength();142if ((keyLen > len) || (keyLen < len - 1)) {143throw new Exception("Incorrect key length: " + keyLen);144}145if (exp != null) {146if (exp.equals(publicKey.getPublicExponent()) == false) {147throw new Exception("Incorrect exponent");148}149}150test(privateKey, publicKey);151}152testInvalidSignature(keyPairs[0], keyPairs[1]);153testInvalidSignature(keyPairs[0], keyPairs[2]);154testInvalidSignature(keyPairs[2], keyPairs[0]);155long stop = System.currentTimeMillis();156System.out.println("All tests passed (" + (stop - start) + " ms).");157}158}159160161