JSSPKIUtility extends ProviderBasedPKIUtility { private void writePemEncoded(byte[] der, OutputStream out, String type) throws IOException { out.write(("-----BEGIN " + type + "-----\n").getBytes(Charsets.UTF_8)); Base64OutputStream b64Out = new Base64OutputStream(out, true, 64, LINE_SEPARATOR); b64Out.write(der); b64Out.eof(); b64Out.flush(); out.write(("-----END " + type + "-----\n").getBytes(Charsets.UTF_8)); } @Inject  JSSPKIUtility(CertificateReader reader, SubjectKeyIdentifierWriter writer, Configuration config); @Override X509Certificate createX509Certificate(String dn, Set<X509ExtensionWrapper> extensions,
        Set<X509ByteExtensionWrapper> byteExtensions, Date startDate, Date endDate, KeyPair clientKeyPair,
        BigInteger serialNumber, String alternateName); @Override X509CRL createX509CRL(List<X509CRLEntryWrapper> entries, BigInteger crlNumber); static AuthorityKeyIdentifierExtension buildAuthorityKeyIdentifier(RSAPublicKey key); static AuthorityKeyIdentifierExtension buildAuthorityKeyIdentifier(X509Certificate caCert); @Override byte[] getPemEncoded(X509Certificate cert); @Override byte[] getPemEncoded(RSAPrivateKey key); @Override byte[] getPemEncoded(X509CRL crl); @Override void writePemEncoded(X509Certificate cert, OutputStream out); @Override void writePemEncoded(RSAPrivateKey key, OutputStream out); @Override void writePemEncoded(X509CRL crl, OutputStream out); static final byte[] LINE_SEPARATOR; static final String SIGNING_ALG_ID; static final String CRL_PEM_NAME; static final String CERTIFICATE_PEM_NAME; static final String PRIVATE_KEY_PEM_NAME; }
@Test public void testWritePemKey() throws Exception { CertificateReader reader = injector.getInstance(CertificateReader.class); RSAPrivateKey key = reader.getCaKey(); ByteArrayOutputStream os = new ByteArrayOutputStream(); jssUtil.writePemEncoded(key, os); String actual = new String(os.toByteArray(), Charsets.UTF_8); CertificateReaderForTesting testingReader = (CertificateReaderForTesting) reader; InputStream is = this.getClass().getClassLoader().getResourceAsStream(testingReader.getKeyFileName()); String expected = CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8)); assertEquals(expected, actual); StringWriter bcExpected = new StringWriter(); try (JcaPEMWriter pemWriter = new JcaPEMWriter(bcExpected)) { pemWriter.writeObject(key); } assertEquals(bcExpected.toString(), actual); }
@Test public void testWritePemEncodedCert() throws Exception { CertificateReader reader = injector.getInstance(CertificateReader.class); X509Certificate ca = reader.getCACert(); ByteArrayOutputStream os = new ByteArrayOutputStream(); jssUtil.writePemEncoded(ca, os); String actual = new String(os.toByteArray(), Charsets.UTF_8); CertificateReaderForTesting testingReader = (CertificateReaderForTesting) reader; InputStream is = this.getClass().getClassLoader().getResourceAsStream(testingReader.getCAFileName()); String expected = CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8)); assertEquals(expected, actual); StringWriter bcExpected = new StringWriter(); try (JcaPEMWriter pemWriter = new JcaPEMWriter(bcExpected)) { pemWriter.writeObject(ca); } assertEquals(bcExpected.toString(), actual); }
@Test public void testWritePemEncodedCrl() throws Exception { InputStream referenceStream = this.getClass().getClassLoader().getResourceAsStream("crl.pem"); CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509CRL crl = (X509CRL) cf.generateCRL(referenceStream); ByteArrayOutputStream os = new ByteArrayOutputStream(); jssUtil.writePemEncoded(crl, os); String actual = new String(os.toByteArray(), Charsets.UTF_8); InputStream is = this.getClass().getClassLoader().getResourceAsStream("crl.pem"); String expected = CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8)); assertEquals(expected, actual); StringWriter bcExpected = new StringWriter(); try (JcaPEMWriter pemWriter = new JcaPEMWriter(bcExpected)) { pemWriter.writeObject(crl); } assertEquals(bcExpected.toString(), actual); }