KeySigner { public X509Certificate generateCertificate(String subject, PublicKey key) throws SigningException { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); X509V3CertificateGenerator cert = new X509V3CertificateGenerator(); cert.setSerialNumber(BigInteger.valueOf(1)); cert.setSubjectDN(new X509Principal("CN=" + subject)); cert.setIssuerDN(new X509Principal(issuer)); cert.setPublicKey(key); cert.setNotBefore(new Date()); cert.setNotAfter(new Date(System.currentTimeMillis() + 1000L * 60L * 60L * 24L * days)); cert.setSignatureAlgorithm(algorithm); try { return cert.generate(signingKey, "BC"); } catch (CertificateEncodingException e) { throw new SigningException(e); } catch (InvalidKeyException e) { throw new SigningException(e); } catch (IllegalStateException e) { throw new SigningException(e); } catch (NoSuchProviderException e) { throw new SigningException(e); } catch (NoSuchAlgorithmException e) { throw new SigningException("Unknown algorithm", e); } catch (SignatureException e) { throw new SigningException(e); } } KeySigner(PrivateKey signingKey, String dn, int days, String algorithm); X509Certificate generateCertificate(String subject, PublicKey key); static final String DEFAULT_ALGORITHM; }
@Test public void testPutYourCertificateInPullYourCertificateOut() throws Exception { X509Certificate cert = signer.generateCertificate("trusted", trustedKey); store.setCertificateEntry(ALIAS, cert); Certificate retrieved = store.getCertificate(ALIAS); PublicKey fromStore = retrieved.getPublicKey(); assertEquals(fromStore, trustedKey); }