@Override
    public KeySelectorResult select(KeyInfo keyInfo,
            KeySelector.Purpose purpose,
            AlgorithmMethod method,
            XMLCryptoContext context)
    throws KeySelectorException {
        for(XMLStructure info : (List<XMLStructure>)keyInfo.getContent()) {
            if (!(info instanceof X509Data)) {
                continue;
            }
            X509Data x509Data = (X509Data) info;
            List<X509Certificate> certs = (List<X509Certificate>) x509Data.getContent();
            try {
                KeyStore ks = KeyStore.getInstance("JKS");
                ks.load(new FileInputStream(keyStoreDir+"cacerts.jks"), "changeit".toCharArray());
                PKIXParameters params = new PKIXParameters(ks);
                Set<TrustAnchor> trustAnchors = params.getTrustAnchors();
                //TODO set this property as environment variable instead
                System.setProperty("com.sun.security.enableCRLDP", "true");
                verifyChain(certs.get(0), trustAnchors, certs, checkRevocation);
            } catch (GeneralSecurityException | IOException ex ) {
                Logger.getLogger(X509KeySelector.class.getName()).log(Level.SEVERE, null, ex);
                throw new KeySelectorException("Key could not be selected", ex);
            }
            Iterator xi = x509Data.getContent().iterator();
            while (xi.hasNext()) {
                Object o = xi.next();
                if (!(o instanceof X509Certificate)) {
                    continue;
                }
                final PublicKey key = ((X509Certificate) o).getPublicKey();
                // Make sure the algorithm is compatible
                // with the method.
                if (algEquals(method.getAlgorithm(), key.getAlgorithm())) {
                    return new KeySelectorResult() {
                        @Override
                        public Key getKey() {
                            return key;
                        }
                    };
                }
            }
        }
        throw new KeySelectorException("No key found!");
    }