test/jdk/sun/security/rsa/TestKeyPairGenerator.java
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
parent 47216 71c04702a3d5
equal deleted inserted replaced
56541:92cbbfc996f3 56542:56aaa6cb3693
     1 /*
     1 /*
     2  * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /**
    24 /**
    25  * @test
    25  * @test
    26  * @bug 4853305 4865198 4888410 4963723
    26  * @bug 4853305 4865198 4888410 4963723 8146293
    27  * @summary Verify that the RSA KeyPairGenerator works
    27  * @summary Verify that the RSA KeyPairGenerator works
       
    28  * @library /test/lib
       
    29  * @build jdk.test.lib.SigTestUtil
       
    30  * @run main TestKeyPairGenerator
    28  * @author Andreas Sterbenz
    31  * @author Andreas Sterbenz
    29  * @key randomness
    32  * @key randomness
    30  */
    33  */
    31 
    34 
    32 import java.io.*;
    35 import java.io.*;
    35 
    38 
    36 import java.security.*;
    39 import java.security.*;
    37 import java.security.interfaces.*;
    40 import java.security.interfaces.*;
    38 import java.security.spec.*;
    41 import java.security.spec.*;
    39 
    42 
       
    43 import jdk.test.lib.SigTestUtil;
       
    44 import static jdk.test.lib.SigTestUtil.SignatureType;
       
    45 
    40 public class TestKeyPairGenerator {
    46 public class TestKeyPairGenerator {
    41 
    47 
    42     private static Provider provider;
    48     private static Provider provider;
    43 
    49 
    44     private static byte[] data;
    50     private static byte[] data;
    45 
    51 
    46     private static void testSignature(String algorithm, PrivateKey privateKey, PublicKey publicKey) throws Exception {
    52     private static void testSignature(SignatureType type, String mdAlg,
    47         System.out.println("Testing " + algorithm + "...");
    53             PrivateKey privateKey, PublicKey publicKey) throws
    48         Signature s = Signature.getInstance(algorithm, provider);
    54             NoSuchAlgorithmException, InvalidKeyException, SignatureException {
       
    55         System.out.println("Testing against " + mdAlg + "...");
       
    56         String sigAlg = SigTestUtil.generateSigAlg(type, mdAlg);
       
    57         Signature s = Signature.getInstance(sigAlg, provider);
    49         s.initSign(privateKey);
    58         s.initSign(privateKey);
    50         s.update(data);
    59         s.update(data);
    51         byte[] sig = s.sign();
    60         byte[] sig = s.sign();
    52         s.initVerify(publicKey);
    61         s.initVerify(publicKey);
    53         s.update(data);
    62         s.update(data);
    54         boolean result = s.verify(sig);
    63         boolean result = s.verify(sig);
    55         if (result == false) {
    64         if (result == false) {
    56             throw new Exception("Verification failed");
    65             throw new RuntimeException("Verification failed");
    57         }
    66         }
    58     }
    67     }
    59 
    68 
    60     private static void test(PrivateKey privateKey, PublicKey publicKey) throws Exception {
    69     private static void test(PrivateKey privateKey, PublicKey publicKey) throws Exception {
    61         testSignature("MD2withRSA", privateKey, publicKey);
    70 
    62         testSignature("MD5withRSA", privateKey, publicKey);
    71         int testSize = ((RSAPublicKey)publicKey).getModulus().bitLength();
    63         testSignature("SHA1withRSA", privateKey, publicKey);
    72         System.out.println("modulus size = " + testSize);
    64         testSignature("SHA224withRSA", privateKey, publicKey);
    73 
    65         testSignature("SHA256withRSA", privateKey, publicKey);
    74         Iterable<String> md_alg_pkcs15 =
    66         RSAPublicKey rsaKey = (RSAPublicKey)publicKey;
    75             SigTestUtil.getDigestAlgorithms(SignatureType.RSA, testSize);
    67         if (rsaKey.getModulus().bitLength() > 512) {
    76         md_alg_pkcs15.forEach(mdAlg -> {
    68             // for SHA384 and SHA512 the data is too long for 512 bit keys
    77             try {
    69             testSignature("SHA384withRSA", privateKey, publicKey);
    78                 testSignature(SignatureType.RSA, mdAlg, privateKey, publicKey);
    70             testSignature("SHA512withRSA", privateKey, publicKey);
    79             } catch (NoSuchAlgorithmException | InvalidKeyException |
       
    80                      SignatureException ex) {
       
    81                 throw new RuntimeException(ex);
       
    82             }
    71         }
    83         }
       
    84         );
    72     }
    85     }
    73 
    86 
    74     // regression test for 4865198
    87     // regression test for 4865198
    75     private static void testInvalidSignature(KeyPair kp1, KeyPair kp2) throws Exception {
    88     private static void testInvalidSignature(KeyPair kp1, KeyPair kp2) throws Exception {
    76         System.out.println("Testing signature with incorrect key...");
    89         System.out.println("Testing signature with incorrect key...");