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);  }
@Test public void testSetTrustStoreDuplicateCert() { try { crypto.addCACertificateToTrustStore(testCACert, "ca"); } catch (Exception e) { fail("testSetTrustStoreDuplicateCert should not have thrown Exception. Error: " + e.getMessage()); } }
@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()); } }
@Test (expected = InvalidArgumentException.class) public void testAddCACertificateToTrustStoreNullAlias() throws InvalidArgumentException { try { crypto.addCACertificateToTrustStore(new File("something"), null); } catch (CryptoException e) { fail("testAddCACertificateToTrustStoreNoAlias should not throw CryptoException. Error: " + e.getMessage()); } }
@Test (expected = InvalidArgumentException.class) public void testAddCACertificateToTrustStoreBlankAlias() throws InvalidArgumentException { try { crypto.addCACertificateToTrustStore(new File("something"), ""); } catch (CryptoException e) { fail("testAddCACertificateToTrustStoreNoAlias should not throw CryptoException. Error: " + e.getMessage()); } }
@Test (expected = InvalidArgumentException.class) public void testAddCACertificateToTrustStoreEmptyAlias() throws InvalidArgumentException { try { crypto.addCACertificateToTrustStore(new File("something"), ""); } catch (CryptoException e) { fail("testAddCACertificateToTrustStoreEmptyAlias should not throw CryptoException. Error: " + e.getMessage()); } }
@Test public void testAddCACertificateToTrustStoreBadStore() throws Exception { thrown.expect(CryptoException.class); thrown.expectMessage("Unable to add"); KeyStore tmpKeyStore = KeyStore.getInstance(KeyStore.getDefaultType()); KeyStore saveKeyStore = (KeyStore) setField(crypto, "trustStore", tmpKeyStore); try { crypto.addCACertificateToTrustStore(testCACert, "alias"); } finally { setField(crypto, "trustStore", saveKeyStore); } }
@Test (expected = CryptoException.class) public void testAddCACertificateToTrustStoreNoFile() throws CryptoException { try { crypto.addCACertificateToTrustStore(new File("does/not/exist"), "abc"); } catch (InvalidArgumentException e) { fail("testAddCACertificateToTrustStoreNoFile should not throw InvalidArgumentException. Error: " + e.getMessage()); } }
@Test (expected = CryptoException.class) public void testAddCACertificateToTrustStoreInvalidCertFile() throws CryptoException { try { crypto.addCACertificateToTrustStore(new File("/bad-ca1.crt"), "abc"); } catch (InvalidArgumentException e) { fail("testAddCACertificateToTrustStoreInvalidCertFile should not throw InvalidArgumentException. Error: " + e.getMessage()); } }
@Test (expected = InvalidArgumentException.class) public void testAddCACertificateToTrustStoreNoCert() throws InvalidArgumentException { try { crypto.addCACertificateToTrustStore((Certificate) null, "abc"); } catch (CryptoException e) { fail("testAddCACertificateToTrustStoreNoCert should not have thrown CryptoException. Error " + e.getMessage()); } }
@Test public void testAddCACertificateToTrustStoreCertNullAlias() throws Exception { thrown.expect(InvalidArgumentException.class); thrown.expectMessage("You must assign an alias"); crypto.addCACertificateToTrustStore(testCACert, null); }
@Test public void testAddCACertificateToTrustStoreCertEmptyAlias() throws Exception { thrown.expect(InvalidArgumentException.class); thrown.expectMessage("You must assign an alias"); crypto.addCACertificateToTrustStore(testCACert, ""); }
@Test (expected = InvalidArgumentException.class) public void testAddCACertificateToTrustStoreNullFile() throws Exception { crypto.addCACertificateToTrustStore((File) null, "test"); }