Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/mscapi/SmallPrimeExponentP.java
38840 views
1
/*
2
* Copyright (c) 2015, 2016, 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
* @summary Test prime exponent (p) lengths 63 and 65 bytes with SunMSCAPI.
28
* The seed 76 has the fastest test execution now (only 5 rounds) and is
29
* hard-coded in run tag. This number might change if algorithms for
30
* RSA key pair generation or BigInteger prime searching gets updated.
31
* @requires os.family == "windows"
32
* @run main SmallPrimeExponentP 76
33
*/
34
import sun.security.tools.keytool.CertAndKeyGen;
35
import sun.security.x509.X500Name;
36
37
import java.security.KeyStore;
38
import java.security.SecureRandom;
39
import java.security.cert.X509Certificate;
40
import java.security.interfaces.RSAPrivateCrtKey;
41
import java.util.Random;
42
43
public class SmallPrimeExponentP {
44
45
public static void main(String argv[]) throws Exception {
46
47
long seed = Long.parseLong(argv[0]);
48
System.out.println("Seed for SecureRandom = " + seed + "L");
49
50
KeyStore ks = KeyStore.getInstance("Windows-MY");
51
ks.load(null, null);
52
53
CertAndKeyGen ckg = new CertAndKeyGen("RSA", "SHA1withRSA");
54
ckg.setRandom(new MySecureRandom(seed));
55
56
boolean see63 = false;
57
boolean see65 = false;
58
while (!see63 || !see65) {
59
ckg.generate(1024);
60
RSAPrivateCrtKey k = (RSAPrivateCrtKey) ckg.getPrivateKey();
61
62
int len = k.getPrimeExponentP().toByteArray().length;
63
System.out.println("Length of P = " + len);
64
if (len == 63 || len == 65) {
65
if (len == 63) {
66
if (see63) {
67
continue;
68
} else {
69
see63 = true;
70
}
71
}
72
if (len == 65) {
73
if (see65) {
74
continue;
75
} else {
76
see65 = true;
77
}
78
}
79
ks.setKeyEntry("anything", k, null, new X509Certificate[]{
80
ckg.getSelfCertificate(new X500Name("CN=Me"), 1000)
81
});
82
}
83
}
84
ks.store(null, null);
85
}
86
87
static class MySecureRandom extends SecureRandom {
88
89
final Random random;
90
91
public MySecureRandom(long seed) {
92
random = new Random(seed);
93
}
94
95
@Override
96
public void nextBytes(byte[] bytes) {
97
random.nextBytes(bytes);
98
}
99
}
100
}
101
102