Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/mscapi/SmallPrimeExponentP.java
38840 views
/*1* Copyright (c) 2015, 2016, 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* @summary Test prime exponent (p) lengths 63 and 65 bytes with SunMSCAPI.27* The seed 76 has the fastest test execution now (only 5 rounds) and is28* hard-coded in run tag. This number might change if algorithms for29* RSA key pair generation or BigInteger prime searching gets updated.30* @requires os.family == "windows"31* @run main SmallPrimeExponentP 7632*/33import sun.security.tools.keytool.CertAndKeyGen;34import sun.security.x509.X500Name;3536import java.security.KeyStore;37import java.security.SecureRandom;38import java.security.cert.X509Certificate;39import java.security.interfaces.RSAPrivateCrtKey;40import java.util.Random;4142public class SmallPrimeExponentP {4344public static void main(String argv[]) throws Exception {4546long seed = Long.parseLong(argv[0]);47System.out.println("Seed for SecureRandom = " + seed + "L");4849KeyStore ks = KeyStore.getInstance("Windows-MY");50ks.load(null, null);5152CertAndKeyGen ckg = new CertAndKeyGen("RSA", "SHA1withRSA");53ckg.setRandom(new MySecureRandom(seed));5455boolean see63 = false;56boolean see65 = false;57while (!see63 || !see65) {58ckg.generate(1024);59RSAPrivateCrtKey k = (RSAPrivateCrtKey) ckg.getPrivateKey();6061int len = k.getPrimeExponentP().toByteArray().length;62System.out.println("Length of P = " + len);63if (len == 63 || len == 65) {64if (len == 63) {65if (see63) {66continue;67} else {68see63 = true;69}70}71if (len == 65) {72if (see65) {73continue;74} else {75see65 = true;76}77}78ks.setKeyEntry("anything", k, null, new X509Certificate[]{79ckg.getSelfCertificate(new X500Name("CN=Me"), 1000)80});81}82}83ks.store(null, null);84}8586static class MySecureRandom extends SecureRandom {8788final Random random;8990public MySecureRandom(long seed) {91random = new Random(seed);92}9394@Override95public void nextBytes(byte[] bytes) {96random.nextBytes(bytes);97}98}99}100101102