0N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A
0N/A// program to generate rsakeys.ks. does not need to run during testing
0N/A// checked into the workspace so that the keystore file can be recreated
0N/A// in the future if needed.
0N/A
0N/A// @author Andreas Sterbenz
0N/A
0N/Aimport java.io.*;
0N/Aimport java.math.BigInteger;
0N/Aimport java.util.*;
0N/A
0N/Aimport java.security.*;
0N/Aimport java.security.cert.*;
0N/Aimport java.security.interfaces.*;
0N/Aimport java.security.spec.*;
0N/A
0N/Aimport sun.security.x509.*;
0N/A
0N/Apublic class GenKeyStore {
0N/A
0N/A static final char[] password = "test12".toCharArray();
0N/A
0N/A private static X509Certificate getCertificate(String suffix, PublicKey publicKey, PrivateKey privateKey) throws Exception {
0N/A X500Name name = new X500Name("CN=Dummy Certificate " + suffix);
0N/A String algorithm = "SHA1with" + publicKey.getAlgorithm();
0N/A Date date = new Date();
0N/A AlgorithmId algID = AlgorithmId.getAlgorithmId(algorithm);
0N/A
0N/A X509CertInfo certInfo = new X509CertInfo();
0N/A
0N/A certInfo.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V1));
0N/A certInfo.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(1));
0N/A certInfo.set(X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(algID));
0N/A certInfo.set(X509CertInfo.SUBJECT, new CertificateSubjectName(name));
0N/A certInfo.set(X509CertInfo.ISSUER, new CertificateIssuerName(name));
0N/A certInfo.set(X509CertInfo.KEY, new CertificateX509Key(publicKey));
0N/A certInfo.set(X509CertInfo.VALIDITY, new CertificateValidity(date, date));
0N/A
0N/A X509CertImpl cert = new X509CertImpl(certInfo);
0N/A cert.sign(privateKey, algorithm);
0N/A
0N/A return cert;
0N/A }
0N/A
0N/A private static void addToKeyStore(KeyStore ks, KeyPair kp, String name) throws Exception {
0N/A PublicKey pubKey = kp.getPublic();
0N/A PrivateKey privKey = kp.getPrivate();
0N/A X509Certificate cert = getCertificate(name, pubKey, privKey);
0N/A ks.setKeyEntry(name, privKey, password, new X509Certificate[] {cert});
0N/A }
0N/A
0N/A private static void generateKeyPair(KeyStore ks, int keyLength, String alias) throws Exception {
0N/A System.out.println("Generating " + keyLength + " keypair " + alias + "...");
0N/A KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign");
0N/A kpg.initialize(keyLength);
0N/A KeyPair kp = kpg.generateKeyPair();
0N/A addToKeyStore(ks, kp, alias);
0N/A }
0N/A
0N/A static KeyStore ks;
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A long start = System.currentTimeMillis();
0N/A
0N/A KeyStore ks = KeyStore.getInstance("JKS");
0N/A ks.load(null, null);
0N/A
0N/A generateKeyPair(ks, 512, "rsa512a");
0N/A generateKeyPair(ks, 512, "rsa512b");
0N/A generateKeyPair(ks, 1024, "rsa1024a");
0N/A generateKeyPair(ks, 1024, "rsa1024b");
0N/A generateKeyPair(ks, 2048, "rsa2048a");
0N/A generateKeyPair(ks, 2048, "rsa2048b");
0N/A generateKeyPair(ks, 4096, "rsa4096a");
0N/A
0N/A // only one 4096 bit keys and none longer than that
0N/A // that would slow down the other tests too much
0N/A // on old machines
0N/A// generateKeyPair(ks, 4096, "rsa4096b");
0N/A// generateKeyPair(ks, 8192, "rsa8192a");
0N/A// generateKeyPair(ks, 8192, "rsa8192b");
0N/A
0N/A OutputStream out = new FileOutputStream("rsakeys.ks");
0N/A ks.store(out, password);
0N/A out.close();
0N/A
0N/A long stop = System.currentTimeMillis();
0N/A System.out.println("Done (" + (stop - start) + " ms).");
0N/A }
0N/A
0N/A}