test/jdk/sun/security/mscapi/SmallPrimeExponentP.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 46155 jdk/test/sun/security/mscapi/SmallPrimeExponentP.java@a6288a07bb53
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
     1
/*
46155
a6288a07bb53 8185620: MSCAPI test leaves too many entries in keystore
mli
parents: 37360
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
     4
 *
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
     8
 *
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    13
 * accompanied this code).
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    14
 *
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    18
 *
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    21
 * questions.
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    22
 */
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    23
37360
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    24
 /*
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    25
 * @test
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    26
 * @bug 8023546 8151834
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    27
 * @modules java.base/sun.security.x509
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    28
 *          java.base/sun.security.tools.keytool
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    29
 * @summary Test prime exponent (p) lengths 63 and 65 bytes with SunMSCAPI.
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    30
 *         The seed 76 has the fastest test execution now (only 5 rounds) and is
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    31
 *         hard-coded in run tag. This number might change if algorithms for
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    32
 *         RSA key pair generation or BigInteger prime searching gets updated.
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    33
 * @requires os.family == "windows"
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    34
 * @run main SmallPrimeExponentP 76
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    35
 */
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    36
import sun.security.tools.keytool.CertAndKeyGen;
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    37
import sun.security.x509.X500Name;
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    38
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    39
import java.security.KeyStore;
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    40
import java.security.SecureRandom;
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    41
import java.security.cert.X509Certificate;
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    42
import java.security.interfaces.RSAPrivateCrtKey;
37360
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    43
import java.util.Random;
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    44
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    45
public class SmallPrimeExponentP {
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    46
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    47
    public static void main(String argv[]) throws Exception {
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    48
37360
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    49
        long seed = Long.parseLong(argv[0]);
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    50
        System.out.println("Seed for SecureRandom = " + seed + "L");
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    51
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    52
        KeyStore ks = KeyStore.getInstance("Windows-MY");
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    53
        ks.load(null, null);
37360
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    54
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    55
        CertAndKeyGen ckg = new CertAndKeyGen("RSA", "SHA1withRSA");
37360
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    56
        ckg.setRandom(new MySecureRandom(seed));
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    57
46155
a6288a07bb53 8185620: MSCAPI test leaves too many entries in keystore
mli
parents: 37360
diff changeset
    58
        String alias = "anything";
a6288a07bb53 8185620: MSCAPI test leaves too many entries in keystore
mli
parents: 37360
diff changeset
    59
        int count = 0;
a6288a07bb53 8185620: MSCAPI test leaves too many entries in keystore
mli
parents: 37360
diff changeset
    60
37360
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    61
        boolean see63 = false;
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    62
        boolean see65 = false;
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    63
        while (!see63 || !see65) {
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    64
            ckg.generate(1024);
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    65
            RSAPrivateCrtKey k = (RSAPrivateCrtKey) ckg.getPrivateKey();
37360
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    66
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    67
            int len = k.getPrimeExponentP().toByteArray().length;
37360
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    68
            System.out.println("Length of P = " + len);
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    69
            if (len == 63 || len == 65) {
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    70
                if (len == 63) {
37360
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    71
                    if (see63) {
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    72
                        continue;
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    73
                    } else {
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    74
                        see63 = true;
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    75
                    }
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    76
                }
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    77
                if (len == 65) {
37360
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    78
                    if (see65) {
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    79
                        continue;
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    80
                    } else {
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    81
                        see65 = true;
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    82
                    }
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    83
                }
46155
a6288a07bb53 8185620: MSCAPI test leaves too many entries in keystore
mli
parents: 37360
diff changeset
    84
                ks.setKeyEntry(alias, k, null, new X509Certificate[]{
37360
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    85
                    ckg.getSelfCertificate(new X500Name("CN=Me"), 1000)
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    86
                });
46155
a6288a07bb53 8185620: MSCAPI test leaves too many entries in keystore
mli
parents: 37360
diff changeset
    87
                count++;
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    88
            }
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    89
        }
46155
a6288a07bb53 8185620: MSCAPI test leaves too many entries in keystore
mli
parents: 37360
diff changeset
    90
a6288a07bb53 8185620: MSCAPI test leaves too many entries in keystore
mli
parents: 37360
diff changeset
    91
        // Because of JDK-8185844, it has to reload the key store after
a6288a07bb53 8185620: MSCAPI test leaves too many entries in keystore
mli
parents: 37360
diff changeset
    92
        // deleting an entry.
a6288a07bb53 8185620: MSCAPI test leaves too many entries in keystore
mli
parents: 37360
diff changeset
    93
        for (int i = 0; i < count; i++) {
a6288a07bb53 8185620: MSCAPI test leaves too many entries in keystore
mli
parents: 37360
diff changeset
    94
            ks.deleteEntry(alias);
a6288a07bb53 8185620: MSCAPI test leaves too many entries in keystore
mli
parents: 37360
diff changeset
    95
            ks.load(null, null);
a6288a07bb53 8185620: MSCAPI test leaves too many entries in keystore
mli
parents: 37360
diff changeset
    96
        }
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
    97
    }
37360
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    98
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
    99
    static class MySecureRandom extends SecureRandom {
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
   100
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
   101
        final Random random;
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
   102
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
   103
        public MySecureRandom(long seed) {
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
   104
            random = new Random(seed);
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
   105
        }
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
   106
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
   107
        @Override
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
   108
        public void nextBytes(byte[] bytes) {
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
   109
            random.nextBytes(bytes);
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
   110
        }
ca6e631dfa5a 8151834: Test SmallPrimeExponentP.java times out intermittently
rhalade
parents: 36627
diff changeset
   111
    }
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents:
diff changeset
   112
}