public boolean isTrusted ( X509Certificate certificate , Store certStore ) throws CryptoException { try { if ( certificate == null ) { throw new CryptoException ( "Invalid input parameter. Certificate can not be null" ) ; } LOGGER . info ( "Checking if certificate with subjectDN={} is trusted" , certificate . getSubjectDN ( ) ) ; Store certificateStore = certStore ; if ( certificateStore == null ) { certificateStore = new JcaCertStore ( new ArrayList ( ) ) ; } checkConstraints ( certSelector , certificate ) ; X509CertSelector selector = new X509CertSelector ( ) ; selector . setCertificate ( certificate ) ; CertStore pkixParamsCertStore = new JcaCertStoreBuilder ( ) . addCertificates ( certificateStore ) . build ( ) ; PKIXBuilderParameters buildParams = new PKIXBuilderParameters ( keyStore , selector ) ; buildParams . addCertStore ( pkixParamsCertStore ) ; buildParams . setRevocationEnabled ( false ) ; CertPathBuilder builder = CertPathBuilder . getInstance ( ALGORITHM_PKIX ) ; PKIXCertPathBuilderResult result = ( PKIXCertPathBuilderResult ) builder . build ( buildParams ) ; // Build certificate path CertPath certPath = result . getCertPath ( ) ; // Set validation parameters PKIXParameters params = new PKIXParameters ( keyStore ) ; params . setRevocationEnabled ( false ) ; // Validate certificate path CertPathValidator validator = CertPathValidator . getInstance ( ALGORITHM_PKIX ) ; validator . validate ( certPath , params ) ; return true ; } catch ( CertPathValidatorException e ) { LOGGER . debug ( "Cert path validation failed" , e ) ; return false ; } catch ( CertPathBuilderException e ) { LOGGER . debug ( "Cert path building failed" , e ) ; return false ; } catch ( GeneralSecurityException e ) { throw new CryptoException ( "General security error occurred. " + e . getMessage ( ) , e ) ; } }
This method is used to check if certificate is trusted or not .