1 /*
2  * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /**
25  * @test
26  * @bug 8044199 8137231
27  * @summary Check same KeyPair's private key and public key have same modulus.
28  * also check public key's public exponent equals to given spec's public
29  * exponent. Only key size 1024 is tested with RSAKeyGenParameterSpec.F0 (3).
30  * @run main SpecTest 512
31  * @run main SpecTest 768
32  * @run main SpecTest 1024
33  * @run main SpecTest 1024 167971
34  * @run main SpecTest 2048
35  * @run main/timeout=240 SpecTest 4096
36  * @run main/timeout=240 SpecTest 5120
37  */
38 import java.math.BigInteger;
39 import java.security.KeyPair;
40 import java.security.KeyPairGenerator;
41 import java.security.interfaces.RSAKey;
42 import java.security.interfaces.RSAPrivateKey;
43 import java.security.interfaces.RSAPublicKey;
44 import java.security.spec.RSAKeyGenParameterSpec;
45 
46 public class SpecTest {
47 
48     /**
49      * ALGORITHM name, fixed as RSA.
50      */
51     private static final String KEYALG = "RSA";
52 
53     /**
54      * JDK default RSA Provider.
55      */
56     private static final String PROVIDER = "SunRsaSign";
57 
58     /**
59      *
60      * @param kpair test key pair
61      * @param pubExponent expected public exponent.
62      * @return true if test passed. false if test failed.
63      */
specTest(KeyPair kpair, BigInteger pubExponent)64     private static boolean specTest(KeyPair kpair, BigInteger pubExponent) {
65         boolean passed = true;
66         RSAPrivateKey priv = (RSAPrivateKey) kpair.getPrivate();
67         RSAPublicKey pub = (RSAPublicKey) kpair.getPublic();
68 
69         // test the getModulus method
70         if ((priv instanceof RSAKey) && (pub instanceof RSAKey)) {
71             if (!priv.getModulus().equals(pub.getModulus())) {
72                 System.out.println("priv.getModulus() = " + priv.getModulus());
73                 System.out.println("pub.getModulus() = " + pub.getModulus());
74                 passed = false;
75             }
76 
77             if (!pubExponent.equals(pub.getPublicExponent())) {
78                 System.out.println("pubExponent = " + pubExponent);
79                 System.out.println("pub.getPublicExponent() = "
80                         + pub.getPublicExponent());
81                 passed = false;
82             }
83         }
84         return passed;
85     }
86 
main(String[] args)87     public static void main(String[] args) throws Exception {
88 
89         int size = 0;
90 
91         if (args.length >= 1) {
92             size = Integer.parseInt(args[0]);
93         } else {
94             throw new RuntimeException("Missing keysize to test with");
95         }
96 
97         BigInteger publicExponent
98                 = (args.length >= 2) ? new BigInteger(args[1]) : RSAKeyGenParameterSpec.F4;
99 
100         System.out.println("Running test with key size: " + size
101                 + " and public exponent: " + publicExponent);
102 
103         KeyPairGenerator kpg1 = KeyPairGenerator.getInstance(KEYALG, PROVIDER);
104         kpg1.initialize(new RSAKeyGenParameterSpec(size, publicExponent));
105         if (!specTest(kpg1.generateKeyPair(), publicExponent)) {
106             throw new RuntimeException("Test failed.");
107         }
108     }
109 }
110