@Test public void testSetTrustStoreDuplicateCertUsingFile() { try { java.net.URL certUrl = this.getClass().getResource("/ca.crt"); String certData = org.apache.commons.io.IOUtils.toString(certUrl, "UTF-8"); File tempFile = tempFolder.newFile("temp.txt"); Path tempPath = Paths.get(tempFile.getAbsolutePath()); Files.write(tempPath, certData.getBytes()); crypto.addCACertificateToTrustStore(tempFile, "ca"); } catch (Exception e) { fail("testSetTrustStoreDuplicateCert should not have thrown Exception. Error: " + e.getMessage()); } }
public void addCACertificateToTrustStore(File caCertPem, String alias) throws CryptoException, InvalidArgumentException { if (caCertPem == null) { throw new InvalidArgumentException("The certificate cannot be null"); } if (alias == null || alias.isEmpty()) { throw new InvalidArgumentException("You must assign an alias to a certificate when adding to the trust store"); } BufferedInputStream bis; try { bis = new BufferedInputStream(new ByteArrayInputStream(FileUtils.readFileToByteArray(caCertPem))); Certificate caCert = cf.generateCertificate(bis); addCACertificateToTrustStore(caCert, alias); } catch (CertificateException | IOException e) { throw new CryptoException("Unable to add CA certificate to trust store. Error: " + e.getMessage(), e); } }
CryptoPrimitives implements CryptoSuite { public void addCACertificateToTrustStore(File caCertPem, String alias) throws CryptoException, InvalidArgumentException { if (caCertPem == null) { throw new InvalidArgumentException("The certificate cannot be null"); } if (alias == null || alias.isEmpty()) { throw new InvalidArgumentException("You must assign an alias to a certificate when adding to the trust store"); } BufferedInputStream bis; try { bis = new BufferedInputStream(new ByteArrayInputStream(FileUtils.readFileToByteArray(caCertPem))); Certificate caCert = cf.generateCertificate(bis); addCACertificateToTrustStore(caCert, alias); } catch (CertificateException | IOException e) { throw new CryptoException("Unable to add CA certificate to trust store. Error: " + e.getMessage(), e); } } }
CryptoPrimitives implements CryptoSuite { public void addCACertificateToTrustStore(File caCertPem, String alias) throws CryptoException, InvalidArgumentException { if (caCertPem == null) { throw new InvalidArgumentException("The certificate cannot be null"); } if (alias == null || alias.isEmpty()) { throw new InvalidArgumentException("You must assign an alias to a certificate when adding to the trust store"); } BufferedInputStream bis; try { bis = new BufferedInputStream(new ByteArrayInputStream(FileUtils.readFileToByteArray(caCertPem))); Certificate caCert = cf.generateCertificate(bis); addCACertificateToTrustStore(caCert, alias); } catch (CertificateException | IOException e) { throw new CryptoException("Unable to add CA certificate to trust store. Error: " + e.getMessage(), e); } } CryptoPrimitives(); }
CryptoPrimitives implements CryptoSuite { public void addCACertificateToTrustStore(File caCertPem, String alias) throws CryptoException, InvalidArgumentException { if (caCertPem == null) { throw new InvalidArgumentException("The certificate cannot be null"); } if (alias == null || alias.isEmpty()) { throw new InvalidArgumentException("You must assign an alias to a certificate when adding to the trust store"); } BufferedInputStream bis; try { bis = new BufferedInputStream(new ByteArrayInputStream(FileUtils.readFileToByteArray(caCertPem))); Certificate caCert = cf.generateCertificate(bis); addCACertificateToTrustStore(caCert, alias); } catch (CertificateException | IOException e) { throw new CryptoException("Unable to add CA certificate to trust store. Error: " + e.getMessage(), e); } } CryptoPrimitives(); Certificate bytesToCertificate(byte[] certBytes); PrivateKey bytesToPrivateKey(byte[] pemKey); @Override boolean verify(byte[] pemCertificate, String signatureAlgorithm, byte[] signature, byte[] plainText); KeyStore getTrustStore(); void addCACertificateToTrustStore(File caCertPem, String alias); void addCACertificateToTrustStore(byte[] bytes, String alias); @Override void loadCACertificates(Collection<Certificate> certificates); @Override void loadCACertificatesAsBytes(Collection<byte[]> certificatesBytes); @Override KeyPair keyGen(); @Override byte[] sign(PrivateKey key, byte[] data); String generateCertificationRequest(String subject, KeyPair pair); @Override byte[] hash(byte[] input); @Override CryptoSuiteFactory getCryptoSuiteFactory(); void init(); @Override Properties getProperties(); byte[] certificateToDER(String certificatePEM); }
CryptoPrimitives implements CryptoSuite { public void addCACertificateToTrustStore(File caCertPem, String alias) throws CryptoException, InvalidArgumentException { if (caCertPem == null) { throw new InvalidArgumentException("The certificate cannot be null"); } if (alias == null || alias.isEmpty()) { throw new InvalidArgumentException("You must assign an alias to a certificate when adding to the trust store"); } BufferedInputStream bis; try { bis = new BufferedInputStream(new ByteArrayInputStream(FileUtils.readFileToByteArray(caCertPem))); Certificate caCert = cf.generateCertificate(bis); addCACertificateToTrustStore(caCert, alias); } catch (CertificateException | IOException e) { throw new CryptoException("Unable to add CA certificate to trust store. Error: " + e.getMessage(), e); } } CryptoPrimitives(); Certificate bytesToCertificate(byte[] certBytes); PrivateKey bytesToPrivateKey(byte[] pemKey); @Override boolean verify(byte[] pemCertificate, String signatureAlgorithm, byte[] signature, byte[] plainText); KeyStore getTrustStore(); void addCACertificateToTrustStore(File caCertPem, String alias); void addCACertificateToTrustStore(byte[] bytes, String alias); @Override void loadCACertificates(Collection<Certificate> certificates); @Override void loadCACertificatesAsBytes(Collection<byte[]> certificatesBytes); @Override KeyPair keyGen(); @Override byte[] sign(PrivateKey key, byte[] data); String generateCertificationRequest(String subject, KeyPair pair); @Override byte[] hash(byte[] input); @Override CryptoSuiteFactory getCryptoSuiteFactory(); void init(); @Override Properties getProperties(); byte[] certificateToDER(String certificatePEM);  }