CryptoPrimitives implements CryptoSuite { boolean validateCertificate(byte[] certPEM) { if (certPEM == null) { return false; } try { X509Certificate certificate = getX509Certificate(certPEM); if (null == certificate) { throw new Exception("Certificate transformation returned null"); } return validateCertificate(certificate); } catch (Exception e) { logger.error("Cannot validate certificate. Error is: " + e.getMessage() + "\r\nCertificate (PEM, hex): " + DatatypeConverter.printHexBinary(certPEM)); return false; } } 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 testValidateNullCertificateByteArray() { assertFalse(crypto.validateCertificate((byte[]) null)); }
@Test public void testValidateNullCertificate() { assertFalse(crypto.validateCertificate((Certificate) null)); }
@Test public void testValidateCertificateByteArray() { assertTrue(crypto.validateCertificate(pemCert)); }
@Test public void testValidateBadCertificateByteArray() { try { BufferedInputStream bis = new BufferedInputStream(this.getClass().getResourceAsStream("/notsigned.crt")); byte[] certBytes = IOUtils.toByteArray(bis); assertFalse(crypto.validateCertificate(certBytes)); } catch (IOException e) { Assert.fail("cannot read cert file"); } }
@Test public void testValidateNotSignedCertificate() { try { BufferedInputStream bis = new BufferedInputStream(this.getClass().getResourceAsStream("/notsigned.crt")); Certificate cert = cf.generateCertificate(bis); assertFalse(crypto.validateCertificate(cert)); } catch (CertificateException e) { Assert.fail("cannot read cert file"); } }
@Test public void testValidateInvalidCertificate() throws IOException, CertificateException { assertFalse(crypto.validateCertificate(invalidPemCert)); }
@Test public void testValidateCertificate() { try { BufferedInputStream pem = new BufferedInputStream(new ByteArrayInputStream(pemCert)); X509Certificate cert = (X509Certificate) cf.generateCertificate(pem); assertTrue(crypto.validateCertificate(cert)); } catch (CertificateException e) { Assert.fail("cannot read cert file"); } }