Path: blob/master/test/jdk/sun/security/mscapi/SmallPrimeExponentP.java
41152 views
/*1* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8023546 815183426* @modules java.base/sun.security.x50927* java.base/sun.security.tools.keytool28* @summary Test prime exponent (p) lengths 63 and 65 bytes with SunMSCAPI.29* The seed 76 has the fastest test execution now (only 5 rounds) and is30* hard-coded in run tag. This number might change if algorithms for31* RSA key pair generation or BigInteger prime searching gets updated.32* @requires os.family == "windows"33* @run main SmallPrimeExponentP 7634*/35import sun.security.tools.keytool.CertAndKeyGen;36import sun.security.x509.X500Name;3738import java.security.KeyStore;39import java.security.SecureRandom;40import java.security.cert.X509Certificate;41import java.security.interfaces.RSAPrivateCrtKey;42import java.util.Random;4344public class SmallPrimeExponentP {4546public static void main(String argv[]) throws Exception {4748long seed = Long.parseLong(argv[0]);49System.out.println("Seed for SecureRandom = " + seed + "L");5051KeyStore ks = KeyStore.getInstance("Windows-MY");52ks.load(null, null);5354CertAndKeyGen ckg = new CertAndKeyGen("RSA", "SHA1withRSA");55ckg.setRandom(new MySecureRandom(seed));5657String alias = "anything";58int count = 0;5960boolean see63 = false;61boolean see65 = false;62while (!see63 || !see65) {63ckg.generate(1024);64RSAPrivateCrtKey k = (RSAPrivateCrtKey) ckg.getPrivateKey();6566int len = k.getPrimeExponentP().toByteArray().length;67System.out.println("Length of P = " + len);68if (len == 63 || len == 65) {69if (len == 63) {70if (see63) {71continue;72} else {73see63 = true;74}75}76if (len == 65) {77if (see65) {78continue;79} else {80see65 = true;81}82}83ks.setKeyEntry(alias, k, null, new X509Certificate[]{84ckg.getSelfCertificate(new X500Name("CN=Me"), 1000)85});86count++;87}88}8990// Because of JDK-8185844, it has to reload the key store after91// deleting an entry.92for (int i = 0; i < count; i++) {93ks.deleteEntry(alias);94ks.load(null, null);95}96}9798static class MySecureRandom extends SecureRandom {99100final Random random;101102public MySecureRandom(long seed) {103random = new Random(seed);104}105106@Override107public void nextBytes(byte[] bytes) {108random.nextBytes(bytes);109}110}111}112113114