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