Path: blob/master/test/jdk/sun/security/rsa/GenKeyStore.java
41149 views
/*1* Copyright (c) 2012, 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*/222324// program to generate rsakeys.ks. does not need to run during testing25// checked into the workspace so that the keystore file can be recreated26// in the future if needed.2728// @author Andreas Sterbenz2930import java.io.*;31import java.math.BigInteger;32import java.util.*;3334import java.security.*;35import java.security.cert.*;36import java.security.interfaces.*;37import java.security.spec.*;3839import sun.security.x509.*;4041public class GenKeyStore {4243static final char[] password = "test12".toCharArray();4445private static X509Certificate getCertificate(String suffix, PublicKey publicKey, PrivateKey privateKey) throws Exception {46X500Name name = new X500Name("CN=Dummy Certificate " + suffix);47String algorithm = "SHA1with" + publicKey.getAlgorithm();48Date date = new Date();49AlgorithmId algID = AlgorithmId.getAlgorithmId(algorithm);5051X509CertInfo certInfo = new X509CertInfo();5253certInfo.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V1));54certInfo.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(1));55certInfo.set(X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(algID));56certInfo.set(X509CertInfo.SUBJECT, name);57certInfo.set(X509CertInfo.ISSUER, name);58certInfo.set(X509CertInfo.KEY, new CertificateX509Key(publicKey));59certInfo.set(X509CertInfo.VALIDITY, new CertificateValidity(date, date));6061X509CertImpl cert = new X509CertImpl(certInfo);62cert.sign(privateKey, algorithm);6364return cert;65}6667private static void addToKeyStore(KeyStore ks, KeyPair kp, String name) throws Exception {68PublicKey pubKey = kp.getPublic();69PrivateKey privKey = kp.getPrivate();70X509Certificate cert = getCertificate(name, pubKey, privKey);71ks.setKeyEntry(name, privKey, password, new X509Certificate[] {cert});72}7374private static void generateKeyPair(KeyStore ks, int keyLength, String alias) throws Exception {75System.out.println("Generating " + keyLength + " keypair " + alias + "...");76KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign");77kpg.initialize(keyLength);78KeyPair kp = kpg.generateKeyPair();79addToKeyStore(ks, kp, alias);80}8182static KeyStore ks;8384public static void main(String[] args) throws Exception {85long start = System.currentTimeMillis();8687KeyStore ks = KeyStore.getInstance("JKS");88ks.load(null, null);8990generateKeyPair(ks, 512, "rsa512a");91generateKeyPair(ks, 512, "rsa512b");92generateKeyPair(ks, 1024, "rsa1024a");93generateKeyPair(ks, 1024, "rsa1024b");94generateKeyPair(ks, 2048, "rsa2048a");95generateKeyPair(ks, 2048, "rsa2048b");96generateKeyPair(ks, 4096, "rsa4096a");9798// only one 4096 bit keys and none longer than that99// that would slow down the other tests too much100// on old machines101// generateKeyPair(ks, 4096, "rsa4096b");102// generateKeyPair(ks, 8192, "rsa8192a");103// generateKeyPair(ks, 8192, "rsa8192b");104105OutputStream out = new FileOutputStream("rsakeys.ks");106ks.store(out, password);107out.close();108109long stop = System.currentTimeMillis();110System.out.println("Done (" + (stop - start) + " ms).");111}112113}114115116