Code example for TrustAnchor

    private void doBuild(X509Certificate userCert) throws Exception {
        // get the set of trusted CA certificates (only one in this instance) 
        HashSet trustAnchors = new HashSet();
        X509Certificate trustedCert = getTrustedCertificate();
        trustAnchors.add(new TrustAnchor(trustedCert, null));
        // put together a CertStore (repository of the certificates and CRLs) 
        ArrayList certs = new ArrayList();
        CollectionCertStoreParameters certStoreParams = new CollectionCertStoreParameters(certs);
        CertStore certStore = CertStore.getInstance("Collection", certStoreParams);
        // specify the target certificate via a CertSelector 
        X509CertSelector certSelector = new X509CertSelector();
        certSelector.setSubject(userCert.getSubjectDN().getName()); // seems to be required
        // build a valid cerificate path 
Contextual code suggestions in your IDE  Get Codota for Java