KeyStoreGenerator { public static KeyStore createKeyStore(KeyPair keyPair, BadRandom random) throws CertificateException, IOException, InvalidKeyException, KeyStoreException, NoSuchAlgorithmException, NoSuchProviderException, SignatureException, OperatorCreationException { PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); X500Name issuerName = new X500Name("CN=127.0.0.1, O=TLS-Attacker, L=RUB, ST=NRW, C=DE"); X500Name subjectName = issuerName; BigInteger serial = BigInteger.valueOf(random.nextInt()); Date before = new Date(System.currentTimeMillis() - 5000); Date after = new Date(System.currentTimeMillis() + 600000); X509v3CertificateBuilder builder = new JcaX509v3CertificateBuilder(issuerName, serial, before, after, subjectName, publicKey); builder.addExtension(Extension.basicConstraints, true, new BasicConstraints(true)); KeyUsage usage = new KeyUsage(KeyUsage.keyCertSign | KeyUsage.digitalSignature | KeyUsage.keyEncipherment | KeyUsage.dataEncipherment); builder.addExtension(Extension.keyUsage, false, usage); ASN1EncodableVector purposes = new ASN1EncodableVector(); purposes.add(KeyPurposeId.id_kp_serverAuth); purposes.add(KeyPurposeId.id_kp_clientAuth); purposes.add(KeyPurposeId.anyExtendedKeyUsage); builder.addExtension(Extension.extendedKeyUsage, false, new DERSequence(purposes)); String algorithm = createSigningAlgorithm(keyPair); X509Certificate cert = signCertificate(algorithm, builder, privateKey); cert.checkValidity(new Date()); cert.verify(publicKey); KeyStore keyStore = KeyStore.getInstance("JKS"); keyStore.load(null, null); keyStore.setKeyEntry(ALIAS, privateKey, PASSWORD.toCharArray(), new java.security.cert.Certificate[] { cert }); return keyStore; } private  KeyStoreGenerator(); static KeyPair createRSAKeyPair(int bits, BadRandom random); static KeyPair createECKeyPair(int bits, BadRandom random); static KeyPair createGost01KeyPair(String curve, BadRandom random); static KeyPair createGost12KeyPair(String curve, BadRandom random); static KeyStore createKeyStore(KeyPair keyPair, BadRandom random); static final String PASSWORD; static final String ALIAS; }
@Test public void testCreateKeyStore() throws Exception { KeyPair k = KeyStoreGenerator.createRSAKeyPair(1024, random); KeyStore ks = KeyStoreGenerator.createKeyStore(k, random); assertNotNull(ks); k = KeyStoreGenerator.createECKeyPair(256, random); ks = KeyStoreGenerator.createKeyStore(k, random); assertNotNull(ks); }