CryptoPrimitives implements CryptoSuite { @Override public boolean verify(byte[] pemCertificate, String signatureAlgorithm, byte[] signature, byte[] plainText) throws CryptoException { boolean isVerified = false; if (plainText == null || signature == null || pemCertificate == null) { return false; } if (config.extraLogLevel(10)) { if (null != diagnosticFileDumper) { StringBuilder sb = new StringBuilder(10000); sb.append("plaintext in hex: " + DatatypeConverter.printHexBinary(plainText)); sb.append("\n"); sb.append("signature in hex: " + DatatypeConverter.printHexBinary(signature)); sb.append("\n"); sb.append("PEM cert in hex: " + DatatypeConverter.printHexBinary(pemCertificate)); logger.trace("verify : " + diagnosticFileDumper.createDiagnosticFile(sb.toString())); } } try { X509Certificate certificate = getX509Certificate(pemCertificate); if (certificate != null) { isVerified = validateCertificate(certificate); if (isVerified) { Signature sig = Signature.getInstance(signatureAlgorithm); sig.initVerify(certificate); sig.update(plainText); isVerified = sig.verify(signature); } } } catch (InvalidKeyException e) { CryptoException ex = new CryptoException("Cannot verify signature. Error is: " + e.getMessage() + "\r\nCertificate: " + DatatypeConverter.printHexBinary(pemCertificate), e); logger.error(ex.getMessage(), ex); throw ex; } catch (NoSuchAlgorithmException | SignatureException e) { CryptoException ex = new CryptoException("Cannot verify. Signature algorithm is invalid. Error is: " + e.getMessage(), e); logger.error(ex.getMessage(), ex); throw ex; } return isVerified; } 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 testVerifyNullInput() { try { assertFalse(crypto.verify(null, SIGNING_ALGORITHM, null, null)); } catch (CryptoException e) { fail("testVerifyNullInput should not have thrown exception. Error: " + e.getMessage()); } }
@Test (expected = CryptoException.class) public void testVerifyBadCert() throws CryptoException { byte[] badCert = new byte[] {(byte) 0x00}; crypto.verify(badCert, SIGNING_ALGORITHM, sig, plainText); }
@Test (expected = CryptoException.class) public void testVerifyBadSig() throws CryptoException { byte[] badSig = new byte[] {(byte) 0x00}; crypto.verify(pemCert, SIGNING_ALGORITHM, badSig, plainText); }
@Test public void testVerifyBadPlaintext() { byte[] badPlainText = new byte[] {(byte) 0x00}; try { assertFalse(crypto.verify(pemCert, SIGNING_ALGORITHM, sig, badPlainText)); } catch (CryptoException e) { fail("testVerifyBadPlaintext should not have thrown exception. Error: " + e.getMessage()); } }
@Test public void testVerify() { try { assertTrue(crypto.verify(pemCert, SIGNING_ALGORITHM, sig, plainText)); } catch (CryptoException e) { fail("testVerify should not have thrown exception. Error: " + e.getMessage()); } }