test/jdk/sun/security/mscapi/SmallPrimeExponentP.java
changeset 47216 71c04702a3d5
parent 46155 a6288a07bb53
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2015, 2017, 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 8023546 8151834
       
    27  * @modules java.base/sun.security.x509
       
    28  *          java.base/sun.security.tools.keytool
       
    29  * @summary Test prime exponent (p) lengths 63 and 65 bytes with SunMSCAPI.
       
    30  *         The seed 76 has the fastest test execution now (only 5 rounds) and is
       
    31  *         hard-coded in run tag. This number might change if algorithms for
       
    32  *         RSA key pair generation or BigInteger prime searching gets updated.
       
    33  * @requires os.family == "windows"
       
    34  * @run main SmallPrimeExponentP 76
       
    35  */
       
    36 import sun.security.tools.keytool.CertAndKeyGen;
       
    37 import sun.security.x509.X500Name;
       
    38 
       
    39 import java.security.KeyStore;
       
    40 import java.security.SecureRandom;
       
    41 import java.security.cert.X509Certificate;
       
    42 import java.security.interfaces.RSAPrivateCrtKey;
       
    43 import java.util.Random;
       
    44 
       
    45 public class SmallPrimeExponentP {
       
    46 
       
    47     public static void main(String argv[]) throws Exception {
       
    48 
       
    49         long seed = Long.parseLong(argv[0]);
       
    50         System.out.println("Seed for SecureRandom = " + seed + "L");
       
    51 
       
    52         KeyStore ks = KeyStore.getInstance("Windows-MY");
       
    53         ks.load(null, null);
       
    54 
       
    55         CertAndKeyGen ckg = new CertAndKeyGen("RSA", "SHA1withRSA");
       
    56         ckg.setRandom(new MySecureRandom(seed));
       
    57 
       
    58         String alias = "anything";
       
    59         int count = 0;
       
    60 
       
    61         boolean see63 = false;
       
    62         boolean see65 = false;
       
    63         while (!see63 || !see65) {
       
    64             ckg.generate(1024);
       
    65             RSAPrivateCrtKey k = (RSAPrivateCrtKey) ckg.getPrivateKey();
       
    66 
       
    67             int len = k.getPrimeExponentP().toByteArray().length;
       
    68             System.out.println("Length of P = " + len);
       
    69             if (len == 63 || len == 65) {
       
    70                 if (len == 63) {
       
    71                     if (see63) {
       
    72                         continue;
       
    73                     } else {
       
    74                         see63 = true;
       
    75                     }
       
    76                 }
       
    77                 if (len == 65) {
       
    78                     if (see65) {
       
    79                         continue;
       
    80                     } else {
       
    81                         see65 = true;
       
    82                     }
       
    83                 }
       
    84                 ks.setKeyEntry(alias, k, null, new X509Certificate[]{
       
    85                     ckg.getSelfCertificate(new X500Name("CN=Me"), 1000)
       
    86                 });
       
    87                 count++;
       
    88             }
       
    89         }
       
    90 
       
    91         // Because of JDK-8185844, it has to reload the key store after
       
    92         // deleting an entry.
       
    93         for (int i = 0; i < count; i++) {
       
    94             ks.deleteEntry(alias);
       
    95             ks.load(null, null);
       
    96         }
       
    97     }
       
    98 
       
    99     static class MySecureRandom extends SecureRandom {
       
   100 
       
   101         final Random random;
       
   102 
       
   103         public MySecureRandom(long seed) {
       
   104             random = new Random(seed);
       
   105         }
       
   106 
       
   107         @Override
       
   108         public void nextBytes(byte[] bytes) {
       
   109             random.nextBytes(bytes);
       
   110         }
       
   111     }
       
   112 }