1 /*
   2  * Copyright (c) 2003, 2015, 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 4856966
  27  * @summary Verify that the RSA KeyPairGenerator works
  28  * @author Andreas Sterbenz
  29  * @library ..
  30  * @run main/othervm TestKeyPairGenerator
  31  */
  32 
  33 import java.io.*;
  34 import java.util.*;
  35 import java.math.BigInteger;
  36 
  37 import java.security.*;
  38 import java.security.interfaces.*;
  39 import java.security.spec.*;
  40 
  41 public class TestKeyPairGenerator extends PKCS11Test {
  42 
  43     private static Provider provider;
  44 
  45     private static byte[] data;
  46 
  47     private static void testSignature(String algorithm, PrivateKey privateKey, PublicKey publicKey) throws Exception {
  48         System.out.println("Testing " + algorithm + "...");
  49         Signature s = Signature.getInstance(algorithm, provider);
  50         s.initSign(privateKey);
  51         s.update(data);
  52         byte[] sig = s.sign();
  53         s.initVerify(publicKey);
  54         s.update(data);
  55         boolean result = s.verify(sig);
  56         if (result == false) {
  57             throw new Exception("Verification failed");
  58         }
  59     }
  60 
  61     private static void test(PrivateKey privateKey, PublicKey publicKey) throws Exception {
  62         testSignature("MD2withRSA", privateKey, publicKey);
  63         testSignature("MD5withRSA", privateKey, publicKey);
  64         testSignature("SHA1withRSA", privateKey, publicKey);
  65         testSignature("SHA224withRSA", privateKey, publicKey);
  66         testSignature("SHA256withRSA", privateKey, publicKey);
  67         RSAPublicKey rsaKey = (RSAPublicKey)publicKey;
  68         if (rsaKey.getModulus().bitLength() > 512) {
  69             // for SHA384 and SHA512 the data is too long for 512 bit keys
  70             testSignature("SHA384withRSA", privateKey, publicKey);
  71             testSignature("SHA512withRSA", privateKey, publicKey);
  72         }
  73     }
  74 
  75     // regression test for 4865198
  76     private static void testInvalidSignature(KeyPair kp1, KeyPair kp2) throws Exception {
  77         System.out.println("Testing signature with incorrect key...");
  78         Signature sig = Signature.getInstance("MD5withRSA", provider);
  79         sig.initSign(kp1.getPrivate());
  80         byte[] data = new byte[100];
  81         sig.update(data);
  82         byte[] signature = sig.sign();
  83         sig.initVerify(kp1.getPublic());
  84         sig.update(data);
  85         if (sig.verify(signature) == false) {
  86             throw new Exception("verification failed");
  87         }
  88         sig.initVerify(kp2.getPublic());
  89         sig.update(data);
  90         // verify needs to return false and not throw an Exception
  91         if (sig.verify(signature)) {
  92             throw new Exception("verification unexpectedly succeeded");
  93         }
  94     }
  95 
  96     public static void main(String[] args) throws Exception {
  97         main(new TestKeyPairGenerator());
  98     }
  99 
 100     public void main(Provider p) throws Exception {
 101         long start = System.currentTimeMillis();
 102         provider = p;
 103         data = new byte[2048];
 104         // keypair generation is very slow, test only a few short keys
 105         int[] keyLengths = {512, 512, 1024};
 106         BigInteger[] pubExps = {null, BigInteger.valueOf(3), null};
 107         KeyPair[] keyPairs = new KeyPair[3];
 108         new Random().nextBytes(data);
 109         KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", provider);
 110         for (int i = 0; i < keyLengths.length; i++) {
 111             int len = keyLengths[i];
 112             BigInteger exp = pubExps[i];
 113             System.out.println("Generating " + len + " bit keypair...");
 114             if (exp == null) {
 115                 kpg.initialize(len);
 116             } else {
 117                 kpg.initialize(new RSAKeyGenParameterSpec(len, exp));
 118             }
 119             KeyPair kp = kpg.generateKeyPair();
 120             keyPairs[i] = kp;
 121             RSAPublicKey publicKey = (RSAPublicKey)kp.getPublic();
 122             System.out.println(publicKey);
 123             RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey)kp.getPrivate();
 124             if (publicKey.getModulus().equals(privateKey.getModulus()) == false) {
 125                 throw new Exception("Moduli do not match");
 126             }
 127             if (publicKey.getPublicExponent().equals(privateKey.getPublicExponent()) == false) {
 128                 throw new Exception("Exponents do not match");
 129             }
 130             int keyLen = publicKey.getModulus().bitLength();
 131             if ((keyLen > len) || (keyLen < len - 1)) {
 132                 throw new Exception("Incorrect key length: " + keyLen);
 133             }
 134             if (exp != null) {
 135                 if (exp.equals(publicKey.getPublicExponent()) == false) {
 136                     throw new Exception("Incorrect exponent");
 137                 }
 138             }
 139             test(privateKey, publicKey);
 140         }
 141         testInvalidSignature(keyPairs[0], keyPairs[1]);
 142         testInvalidSignature(keyPairs[0], keyPairs[2]);
 143         long stop = System.currentTimeMillis();
 144         System.out.println("All tests passed (" + (stop - start) + " ms).");
 145     }
 146 }