Path: blob/master/test/jdk/sun/security/tools/keytool/Serial64.java
41152 views
/*1* Copyright (c) 2019, 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 8221257 822227526* @summary Improve serial number generation mechanism for keytool -gencert27* @library /test/lib28* @key randomness29*/3031import jdk.test.lib.SecurityTools;3233import java.io.File;34import java.io.FileInputStream;35import java.math.BigInteger;36import java.security.KeyStore;37import java.security.cert.CertificateFactory;38import java.security.cert.X509Certificate;3940public class Serial64 {4142public static void main(String[] args) throws Exception {4344boolean see64 = false;4546see64 |= see64(genkeypair("ca"));47see64 |= see64(genkeypair("user"));4849keytool("-certreq -alias user -file req");5051for (int i = 3; i <= 30; i++) {52see64 |= see64(gencert());53if (i >= 10 && see64) {54// As long as we have generated >=10 (non-negative) SNs and55// at least one is 64 bit it's good to go.56return;57}58}5960// None is 64 bit. There is a chance of 2^-30 we reach here.61// Or, maybe we do have a bug?62throw new RuntimeException("No 64-bit serial number");63}6465static boolean see64(BigInteger sn) {66System.out.println(sn.toString(16));6768if (sn.signum() != 1) {69throw new RuntimeException("Must be positive");70}71return sn.bitLength() == 64;72}7374static void keytool(String s) throws Exception {75SecurityTools.keytool("-storepass changeit -keypass changeit "76+ "-keystore ks -keyalg rsa " + s)77.shouldHaveExitValue(0);78}7980static BigInteger genkeypair(String a) throws Exception {81keytool("-genkeypair -alias " + a + " -dname CN=" + a);82return ((X509Certificate)KeyStore.getInstance(83new File("ks"), "changeit".toCharArray())84.getCertificate(a)).getSerialNumber();85}8687static BigInteger gencert() throws Exception {88keytool("-gencert -alias ca -infile req -outfile cert");89try (FileInputStream fis = new FileInputStream("cert")) {90return ((X509Certificate)CertificateFactory.getInstance("X.509")91.generateCertificate(fis)).getSerialNumber();92}93}94}959697