https://issues.shibboleth.net/jira/browse/CPPXT-70
[shibboleth/cpp-xmltooling.git] / xmltooling / security / impl / AbstractPKIXTrustEngine.cpp
index 95964de..dd450b6 100644 (file)
@@ -1,17 +1,21 @@
-/*
- *  Copyright 2006 Internet2
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
+ *
+ * UCAID licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the
+ * License at
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
  */
 
 /**
  */
 
 #include "internal.h"
+#include "logging.h"
 #include "security/AbstractPKIXTrustEngine.h"
 #include "signature/KeyInfo.h"
+#include "signature/Signature.h"
+#include "security/CredentialCriteria.h"
+#include "security/CredentialResolver.h"
+#include "security/KeyInfoResolver.h"
+#include "security/OpenSSLCryptoX509CRL.h"
+#include "security/OpenSSLPathValidator.h"
+#include "security/PKIXPathValidatorParams.h"
+#include "security/SecurityHelper.h"
+#include "security/X509Credential.h"
+#include "signature/SignatureValidator.h"
+#include "util/NDC.h"
+#include "util/PathResolver.h"
 
-#include <log4cpp/Category.hh>
-#include <openssl/x509_vfy.h>
-#include <openssl/x509v3.h>
-#include <xmltooling/security/OpenSSLCryptoX509CRL.h>
-#include <xmltooling/signature/SignatureValidator.h>
-#include <xmltooling/util/NDC.h>
+#include <fstream>
+#include <xercesc/util/XMLUniDefs.hpp>
 #include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
 
 using namespace xmlsignature;
+using namespace xmltooling::logging;
 using namespace xmltooling;
-using namespace log4cpp;
 using namespace std;
 
-AbstractPKIXTrustEngine::AbstractPKIXTrustEngine(const DOMElement* e) : OpenSSLTrustEngine(e), m_inlineResolver(NULL)
+
+namespace xmltooling {
+    // Adapter between TrustEngine and PathValidator
+    class XMLTOOL_DLLLOCAL PKIXParams : public PKIXPathValidatorParams
+    {
+        const AbstractPKIXTrustEngine& m_trust;
+        const AbstractPKIXTrustEngine::PKIXValidationInfoIterator& m_pkixInfo;
+        vector<XSECCryptoX509CRL*> m_crls;
+    public:
+        PKIXParams(
+            const AbstractPKIXTrustEngine& t,
+            const AbstractPKIXTrustEngine::PKIXValidationInfoIterator& pkixInfo,
+            const vector<XSECCryptoX509CRL*>* inlineCRLs
+            ) : m_trust(t), m_pkixInfo(pkixInfo) {
+            if (inlineCRLs && !inlineCRLs->empty()) {
+                m_crls = *inlineCRLs;
+                m_crls.insert(m_crls.end(), pkixInfo.getCRLs().begin(), pkixInfo.getCRLs().end());
+            }
+        }
+
+        virtual ~PKIXParams() {}
+
+        int getVerificationDepth() const {
+            return m_pkixInfo.getVerificationDepth();
+        }
+        bool isAnyPolicyInhibited() const {
+            return m_trust.m_anyPolicyInhibit;
+        }
+        bool isPolicyMappingInhibited() const {
+            return m_trust.m_policyMappingInhibit;
+        }
+        const set<string>& getPolicies() const {
+            return m_trust.m_policyOIDs;
+        }
+        const vector<XSECCryptoX509*>& getTrustAnchors() const {
+            return m_pkixInfo.getTrustAnchors();
+        }
+        PKIXPathValidatorParams::revocation_t getRevocationChecking() const {
+            if (m_trust.m_checkRevocation.empty() || m_trust.m_checkRevocation == "off")
+                return PKIXPathValidatorParams::REVOCATION_OFF;
+            else if (m_trust.m_checkRevocation == "entityOnly")
+                return PKIXPathValidatorParams::REVOCATION_ENTITYONLY;
+            else if (m_trust.m_checkRevocation == "fullChain")
+                return PKIXPathValidatorParams::REVOCATION_FULLCHAIN;
+            return PKIXPathValidatorParams::REVOCATION_OFF;
+        }
+        const vector<XSECCryptoX509CRL*>& getCRLs() const {
+            return m_crls.empty() ? m_pkixInfo.getCRLs() : m_crls;
+        }
+    };
+
+
+    static XMLCh fullCRLChain[] =                  UNICODE_LITERAL_12(f,u,l,l,C,R,L,C,h,a,i,n);
+    static XMLCh checkRevocation[] =       UNICODE_LITERAL_15(c,h,e,c,k,R,e,v,o,c,a,t,i,o,n);
+    static XMLCh policyMappingInhibit[] =   UNICODE_LITERAL_20(p,o,l,i,c,y,M,a,p,p,i,n,g,I,n,h,i,b,i,t);
+    static XMLCh anyPolicyInhibit[] =      UNICODE_LITERAL_16(a,n,y,P,o,l,i,c,y,I,n,h,i,b,i,t);
+    static XMLCh _PathValidator[] =         UNICODE_LITERAL_13(P,a,t,h,V,a,l,i,d,a,t,o,r);
+    static XMLCh PolicyOID[] =                     UNICODE_LITERAL_9(P,o,l,i,c,y,O,I,D);
+    static XMLCh TrustedName[] =                   UNICODE_LITERAL_11(T,r,u,s,t,e,d,N,a,m,e);
+    static XMLCh type[] =                   UNICODE_LITERAL_4(t,y,p,e);
+};
+
+AbstractPKIXTrustEngine::PKIXValidationInfoIterator::PKIXValidationInfoIterator()
 {
-    m_inlineResolver = XMLToolingConfig::getConfig().KeyResolverManager.newPlugin(INLINE_KEY_RESOLVER,NULL);
 }
 
-AbstractPKIXTrustEngine::~AbstractPKIXTrustEngine()
+AbstractPKIXTrustEngine::PKIXValidationInfoIterator::~PKIXValidationInfoIterator()
 {
-    delete m_inlineResolver;
 }
 
-namespace {
-    static int XMLTOOL_DLLLOCAL error_callback(int ok, X509_STORE_CTX* ctx)
-    {
-        if (!ok)
-            Category::getInstance("OpenSSL").error("path validation failure: %s", X509_verify_cert_error_string(ctx->error));
-        return ok;
+AbstractPKIXTrustEngine::AbstractPKIXTrustEngine(const xercesc::DOMElement* e)
+       : TrustEngine(e),
+               m_checkRevocation(XMLHelper::getAttrString(e, nullptr, checkRevocation)),
+               m_fullCRLChain(XMLHelper::getAttrBool(e, false, fullCRLChain)),
+               m_policyMappingInhibit(XMLHelper::getAttrBool(e, false, policyMappingInhibit)),
+               m_anyPolicyInhibit(XMLHelper::getAttrBool(e, false, anyPolicyInhibit))
+{
+    if (m_fullCRLChain) {
+        Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX").warn(
+            "fullCRLChain option is deprecated, setting checkRevocation to \"fullChain\""
+            );
+        m_checkRevocation = "fullChain";
+    }
+    else if (m_checkRevocation == "fullChain") {
+        m_fullCRLChain = true; // in case anything's using this
     }
 
-    static bool XMLTOOL_DLLLOCAL validate(
-        X509* EE, STACK_OF(X509)* untrusted, AbstractPKIXTrustEngine::PKIXValidationInfoIterator* pkixInfo
-        )
-    {
-        Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
-    
-        // First we build a stack of CA certs. These objects are all referenced in place.
-        log.debug("building CA list from PKIX Validation information");
-    
-        // We need this for CRL support.
-        X509_STORE* store=X509_STORE_new();
-        if (!store) {
-            log_openssl();
-            return false;
-        }
-    #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
-        X509_STORE_set_flags(store,X509_V_FLAG_CRL_CHECK_ALL);
-    #endif
-    
-        STACK_OF(X509)* CAstack = sk_X509_new_null();
-        
-        // This contains the state of the validate operation.
-        X509_STORE_CTX ctx;
-        
-        const vector<XSECCryptoX509*>& CAcerts = pkixInfo->getTrustAnchors();
-        for (vector<XSECCryptoX509*>::const_iterator i=CAcerts.begin(); i!=CAcerts.end(); ++i) {
-            if ((*i)->getProviderName()==DSIGConstants::s_unicodeStrPROVOpenSSL) {
-                sk_X509_push(CAstack,static_cast<OpenSSLCryptoX509*>(*i)->getOpenSSLX509());
+    xercesc::DOMElement* c = XMLHelper::getFirstChildElement(e);
+    while (c) {
+        if (c->hasChildNodes()) {
+            auto_ptr_char v(c->getTextContent());
+            if (v.get() && *v.get()) {
+                if (XMLString::equals(c->getLocalName(), PolicyOID))
+                    m_policyOIDs.insert(v.get());
+                else if (XMLString::equals(c->getLocalName(), TrustedName))
+                    m_trustedNames.insert(v.get());
             }
         }
-
-        const vector<XSECCryptoX509CRL*>& crls = pkixInfo->getCRLs();
-        for (vector<XSECCryptoX509CRL*>::const_iterator j=crls.begin(); j!=crls.end(); ++j) {
-            if ((*j)->getProviderName()==DSIGConstants::s_unicodeStrPROVOpenSSL) {
-                // owned by store
-                X509_STORE_add_crl(
-                    store,
-                    X509_CRL_dup(static_cast<OpenSSLCryptoX509CRL*>(*j)->getOpenSSLX509CRL())
-                    );
+        else if (XMLString::equals(c->getLocalName(), _PathValidator)) {
+            try {
+                string t = XMLHelper::getAttrString(c, nullptr, type);
+                if (!t.empty()) {
+                    Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX").info(
+                        "building PathValidator of type %s", t.c_str()
+                        );
+                    PathValidator* pv = XMLToolingConfig::getConfig().PathValidatorManager.newPlugin(t.c_str(), c);
+                    OpenSSLPathValidator* ospv = dynamic_cast<OpenSSLPathValidator*>(pv);
+                    if (!ospv) {
+                        delete pv;
+                        throw XMLSecurityException("PathValidator doesn't support OpenSSL interface.");
+                    }
+                    m_pathValidators.push_back(ospv);
+                }
             }
-        }
-     
-        // AFAICT, EE and untrusted are passed in but not owned by the ctx.
-    #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
-        if (X509_STORE_CTX_init(&ctx,store,EE,untrusted)!=1) {
-            log_openssl();
-            log.error("unable to initialize X509_STORE_CTX");
-            sk_X509_free(CAstack);
-            X509_STORE_free(store);
-            return false;
-        }
-    #else
-        X509_STORE_CTX_init(&ctx,store,EE,untrusted);
-    #endif
-    
-        // Seems to be most efficient to just pass in the CA stack.
-        X509_STORE_CTX_trusted_stack(&ctx,CAstack);
-        X509_STORE_CTX_set_depth(&ctx,100);    // we check the depth down below
-        X509_STORE_CTX_set_verify_cb(&ctx,error_callback);
-        
-        int ret=X509_verify_cert(&ctx);
-        if (ret==1) {
-            // Now see if the depth was acceptable by counting the number of intermediates.
-            int depth=sk_X509_num(ctx.chain)-2;
-            if (pkixInfo->getVerificationDepth() < depth) {
-                log.error(
-                    "certificate chain was too long (%d intermediates, only %d allowed)",
-                    (depth==-1) ? 0 : depth,
-                    pkixInfo->getVerificationDepth()
+            catch (exception& ex) {
+                Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX").error(
+                    "error building PathValidator: %s", ex.what()
                     );
-                ret=0;
             }
         }
-        
-        // Clean up...
-        X509_STORE_CTX_cleanup(&ctx);
-        X509_STORE_free(store);
-        sk_X509_free(CAstack);
-    
-        if (ret==1) {
-            log.info("successfully validated certificate chain");
-            return true;
-        }
-        
-        return false;
+        c = XMLHelper::getNextSiblingElement(c);
     }
-};
 
-bool AbstractPKIXTrustEngine::checkEntityNames(X509* certEE, const KeyInfoSource& keyInfoSource) const
+    if (m_pathValidators.empty()) {
+        m_pathValidators.push_back(
+            dynamic_cast<OpenSSLPathValidator*>(
+                XMLToolingConfig::getConfig().PathValidatorManager.newPlugin(PKIX_PATHVALIDATOR, e)
+                )
+            );
+    }
+}
+
+AbstractPKIXTrustEngine::~AbstractPKIXTrustEngine()
 {
-    Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
-    
-    // Build a list of acceptable names. Transcode the possible key "names" to UTF-8.
-    // For some simple cases, this should handle UTF-8 encoded DNs in certificates.
-    vector<string> keynames;
-    auto_ptr<KeyInfoIterator> keyInfoIter(keyInfoSource.getKeyInfoIterator());
-    while (keyInfoIter->hasNext()) {
-        const KeyInfo* keyInfo = keyInfoIter->next();
-        const vector<KeyName*>& knames=keyInfo->getKeyNames();
-        for (vector<KeyName*>::const_iterator kn_i=knames.begin(); kn_i!=knames.end(); ++kn_i) {
-            const XMLCh* n=(*kn_i)->getName();
-            if (n && *n) {
-                char* kn=toUTF8(n);
-                keynames.push_back(kn);
-                delete[] kn;
+    for_each(m_pathValidators.begin(), m_pathValidators.end(), xmltooling::cleanup<PathValidator>());
+}
+
+bool AbstractPKIXTrustEngine::checkEntityNames(
+    X509* certEE, const CredentialResolver& credResolver, const CredentialCriteria& criteria
+    ) const
+{
+    Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX");
+
+    // We resolve to a set of trusted credentials.
+    vector<const Credential*> creds;
+    credResolver.resolve(creds,&criteria);
+
+    // Build a list of acceptable names.
+    set<string> trustednames = m_trustedNames;
+    if (log.isDebugEnabled()) {
+        for (set<string>::const_iterator n=m_trustedNames.begin(); n!=m_trustedNames.end(); n++) {
+            log.debug("adding to list of trusted names (%s)", n->c_str());
+        }
+    }
+    if (criteria.getPeerName()) {
+        trustednames.insert(criteria.getPeerName());
+        log.debug("adding to list of trusted names (%s)", criteria.getPeerName());
+    }
+    for (vector<const Credential*>::const_iterator cred = creds.begin(); cred!=creds.end(); ++cred) {
+        trustednames.insert((*cred)->getKeyNames().begin(), (*cred)->getKeyNames().end());
+        if (log.isDebugEnabled()) {
+            for (set<string>::const_iterator n=(*cred)->getKeyNames().begin(); n!=(*cred)->getKeyNames().end(); n++) {
+                log.debug("adding to list of trusted names (%s)", n->c_str());
             }
         }
     }
 
-    string peername = keyInfoSource.getName();
-    if (!peername.empty())
-        keynames.push_back(peername);
-    
-    char buf[256];
     X509_NAME* subject=X509_get_subject_name(certEE);
     if (subject) {
         // One way is a direct match to the subject DN.
         // Seems that the way to do the compare is to write the X509_NAME into a BIO.
         BIO* b = BIO_new(BIO_s_mem());
         BIO* b2 = BIO_new(BIO_s_mem());
-        BIO_set_mem_eof_return(b, 0);
-        BIO_set_mem_eof_return(b2, 0);
         // The flags give us LDAP order instead of X.500, with a comma separator.
-        int len=X509_NAME_print_ex(b,subject,0,XN_FLAG_RFC2253);
-        string subjectstr,subjectstr2;
+        X509_NAME_print_ex(b,subject,0,XN_FLAG_RFC2253);
         BIO_flush(b);
-        while ((len = BIO_read(b, buf, 255)) > 0) {
-            buf[len] = '\0';
-            subjectstr+=buf;
-        }
-        log.infoStream() << "certificate subject: " << subjectstr << CategoryStream::ENDLINE;
         // The flags give us LDAP order instead of X.500, with a comma plus space separator.
-        len=X509_NAME_print_ex(b2,subject,0,XN_FLAG_RFC2253 + XN_FLAG_SEP_CPLUS_SPC - XN_FLAG_SEP_COMMA_PLUS);
+        X509_NAME_print_ex(b2,subject,0,XN_FLAG_RFC2253 + XN_FLAG_SEP_CPLUS_SPC - XN_FLAG_SEP_COMMA_PLUS);
         BIO_flush(b2);
-        while ((len = BIO_read(b2, buf, 255)) > 0) {
-            buf[len] = '\0';
-            subjectstr2+=buf;
+
+        BUF_MEM* bptr=nullptr;
+        BUF_MEM* bptr2=nullptr;
+        BIO_get_mem_ptr(b, &bptr);
+        BIO_get_mem_ptr(b2, &bptr2);
+
+        if (bptr && bptr->length > 0 && log.isDebugEnabled()) {
+            string subjectstr(bptr->data, bptr->length);
+            log.debug("certificate subject: %s", subjectstr.c_str());
         }
         
         // Check each keyname.
-        for (vector<string>::const_iterator n=keynames.begin(); n!=keynames.end(); n++) {
+        for (set<string>::const_iterator n=trustednames.begin(); bptr && bptr2 && n!=trustednames.end(); n++) {
 #ifdef HAVE_STRCASECMP
-            if (!strcasecmp(n->c_str(),subjectstr.c_str()) || !strcasecmp(n->c_str(),subjectstr2.c_str())) {
+            if ((n->length() == bptr->length && !strncasecmp(n->c_str(), bptr->data, bptr->length)) ||
+                (n->length() == bptr2->length && !strncasecmp(n->c_str(), bptr2->data, bptr2->length))) {
 #else
-            if (!stricmp(n->c_str(),subjectstr.c_str()) || !stricmp(n->c_str(),subjectstr2.c_str())) {
+            if ((n->length() == bptr->length && !strnicmp(n->c_str(), bptr->data, bptr->length)) ||
+                (n->length() == bptr2->length && !strnicmp(n->c_str(), bptr2->data, bptr2->length))) {
 #endif
-                log.info("matched full subject DN to a key name (%s)", n->c_str());
+                log.debug("matched full subject DN to a key name (%s)", n->c_str());
                 BIO_free(b);
                 BIO_free(b2);
                 return true;
@@ -212,7 +260,7 @@ bool AbstractPKIXTrustEngine::checkEntityNames(X509* certEE, const KeyInfoSource
         BIO_free(b2);
 
         log.debug("unable to match DN, trying TLS subjectAltName match");
-        STACK_OF(GENERAL_NAME)* altnames=(STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(certEE, NID_subject_alt_name, NULL, NULL);
+        STACK_OF(GENERAL_NAME)* altnames=(STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(certEE, NID_subject_alt_name, nullptr, nullptr);
         if (altnames) {
             int numalts = sk_GENERAL_NAME_num(altnames);
             for (int an=0; an<numalts; an++) {
@@ -220,15 +268,14 @@ bool AbstractPKIXTrustEngine::checkEntityNames(X509* certEE, const KeyInfoSource
                 if (check->type==GEN_DNS || check->type==GEN_URI) {
                     const char* altptr = (char*)ASN1_STRING_data(check->d.ia5);
                     const int altlen = ASN1_STRING_length(check->d.ia5);
-                    
-                    for (vector<string>::const_iterator n=keynames.begin(); n!=keynames.end(); n++) {
+                    for (set<string>::const_iterator n=trustednames.begin(); n!=trustednames.end(); n++) {
 #ifdef HAVE_STRCASECMP
-                        if ((check->type==GEN_DNS && !strncasecmp(altptr,n->c_str(),altlen))
+                        if ((check->type==GEN_DNS && n->length()==altlen && !strncasecmp(altptr,n->c_str(),altlen))
 #else
-                        if ((check->type==GEN_DNS && !strnicmp(altptr,n->c_str(),altlen))
+                        if ((check->type==GEN_DNS && n->length()==altlen && !strnicmp(altptr,n->c_str(),altlen))
 #endif
-                                || (check->type==GEN_URI && !strncmp(altptr,n->c_str(),altlen))) {
-                            log.info("matched DNS/URI subjectAltName to a key name (%s)", n->c_str());
+                                || (check->type==GEN_URI && n->length()==altlen && !strncmp(altptr,n->c_str(),altlen))) {
+                            log.debug("matched DNS/URI subjectAltName to a key name (%s)", n->c_str());
                             GENERAL_NAMES_free(altnames);
                             return true;
                         }
@@ -239,49 +286,88 @@ bool AbstractPKIXTrustEngine::checkEntityNames(X509* certEE, const KeyInfoSource
         GENERAL_NAMES_free(altnames);
             
         log.debug("unable to match subjectAltName, trying TLS CN match");
-        memset(buf,0,sizeof(buf));
-        if (X509_NAME_get_text_by_NID(subject,NID_commonName,buf,255)>0) {
-            for (vector<string>::const_iterator n=keynames.begin(); n!=keynames.end(); n++) {
+
+        // Fetch the last CN RDN.
+        char* peer_CN = nullptr;
+        int j,i = -1;
+        while ((j=X509_NAME_get_index_by_NID(subject, NID_commonName, i)) >= 0)
+            i = j;
+        if (i >= 0) {
+            ASN1_STRING* tmp = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(subject, i));
+            // Copied in from libcurl.
+            /* In OpenSSL 0.9.7d and earlier, ASN1_STRING_to_UTF8 fails if the input
+               is already UTF-8 encoded. We check for this case and copy the raw
+               string manually to avoid the problem. */
+            if(tmp && ASN1_STRING_type(tmp) == V_ASN1_UTF8STRING) {
+                j = ASN1_STRING_length(tmp);
+                if(j >= 0) {
+                    peer_CN = (char*)OPENSSL_malloc(j + 1);
+                    memcpy(peer_CN, ASN1_STRING_data(tmp), j);
+                    peer_CN[j] = '\0';
+                }
+            }
+            else /* not a UTF8 name */ {
+                j = ASN1_STRING_to_UTF8(reinterpret_cast<unsigned char**>(&peer_CN), tmp);
+            }
+
+            for (set<string>::const_iterator n=trustednames.begin(); n!=trustednames.end(); n++) {
 #ifdef HAVE_STRCASECMP
-                if (!strcasecmp(buf,n->c_str())) {
+                if (n->length() == j && !strncasecmp(peer_CN, n->c_str(), j)) {
 #else
-                if (!stricmp(buf,n->c_str())) {
+                if (n->length() == j && !strnicmp(peer_CN, n->c_str(), j)) {
 #endif
-                    log.info("matched subject CN to a key name (%s)", n->c_str());
+                    log.debug("matched subject CN to a key name (%s)", n->c_str());
+                    if(peer_CN)
+                        OPENSSL_free(peer_CN);
                     return true;
                 }
             }
+            if(peer_CN)
+                OPENSSL_free(peer_CN);
         }
-        else
+        else {
             log.warn("no common name in certificate subject");
+        }
     }
-    else
+    else {
         log.error("certificate has no subject?!");
+    }
     
     return false;
 }
 
-bool AbstractPKIXTrustEngine::validate(
+bool AbstractPKIXTrustEngine::validateWithCRLs(
     X509* certEE,
     STACK_OF(X509)* certChain,
-    const KeyInfoSource& keyInfoSource,
-    bool checkName,
-    const KeyResolver* keyResolver
+    const CredentialResolver& credResolver,
+    CredentialCriteria* criteria,
+    const vector<XSECCryptoX509CRL*>* inlineCRLs
     ) const
 {
 #ifdef _DEBUG
-    NDC ndc("validate");
+    NDC ndc("validateWithCRLs");
 #endif
-    Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
+    Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX");
 
     if (!certEE) {
         log.error("X.509 credential was NULL, unable to perform validation");
         return false;
     }
 
-    if (checkName) {
+    if (criteria && criteria->getPeerName() && *(criteria->getPeerName())) {
+        log.debug("checking that the certificate name is acceptable");
+        if (criteria && criteria->getUsage()==Credential::UNSPECIFIED_CREDENTIAL)
+            criteria->setUsage(Credential::SIGNING_CREDENTIAL);
+        if (!checkEntityNames(certEE,credResolver,*criteria)) {
+            log.error("certificate name was not acceptable");
+            return false;
+        }
+    }
+    else if (!m_trustedNames.empty()) {
         log.debug("checking that the certificate name is acceptable");
-        if (!checkEntityNames(certEE,keyInfoSource)) {
+        CredentialCriteria cc;
+        cc.setUsage(Credential::SIGNING_CREDENTIAL);
+        if (!checkEntityNames(certEE,credResolver,cc)) {
             log.error("certificate name was not acceptable");
             return false;
         }
@@ -289,66 +375,86 @@ bool AbstractPKIXTrustEngine::validate(
     
     log.debug("performing certificate path validation...");
 
-    auto_ptr<PKIXValidationInfoIterator> pkix(
-        getPKIXValidationInfoIterator(keyInfoSource, *(keyResolver ? keyResolver : m_inlineResolver))
-        );
+    auto_ptr<PKIXValidationInfoIterator> pkix(getPKIXValidationInfoIterator(credResolver, criteria));
     while (pkix->next()) {
-        if (::validate(certEE,certChain,pkix.get())) {
-            return true;
+        PKIXParams params(*this, *pkix.get(), inlineCRLs);
+        for (vector<OpenSSLPathValidator*>::const_iterator v = m_pathValidators.begin(); v != m_pathValidators.end(); ++v) {
+            if ((*v)->validate(certEE, certChain, params)) {
+                return true;
+            }
         }
     }
 
-    log.error("failed to validate certificate chain using supplied PKIX information");
+    log.debug("failed to validate certificate chain using supplied PKIX information");
     return false;
 }
 
 bool AbstractPKIXTrustEngine::validate(
+    X509* certEE,
+    STACK_OF(X509)* certChain,
+    const CredentialResolver& credResolver,
+    CredentialCriteria* criteria
+    ) const
+{
+    return validateWithCRLs(certEE,certChain,credResolver,criteria);
+}
+
+bool AbstractPKIXTrustEngine::validate(
     XSECCryptoX509* certEE,
     const vector<XSECCryptoX509*>& certChain,
-    const KeyInfoSource& keyInfoSource,
-    bool checkName,
-    const KeyResolver* keyResolver
+    const CredentialResolver& credResolver,
+    CredentialCriteria* criteria
     ) const
 {
-    if (!certEE) {
 #ifdef _DEBUG
         NDC ndc("validate");
 #endif
-        Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine").error("X.509 credential was NULL, unable to perform validation");
+    if (!certEE) {
+        Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX").error("X.509 credential was NULL, unable to perform validation");
         return false;
     }
     else if (certEE->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
-#ifdef _DEBUG
-        NDC ndc("validate");
-#endif
-        Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine").error("only the OpenSSL XSEC provider is supported");
+        Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX").error("only the OpenSSL XSEC provider is supported");
         return false;
     }
 
     STACK_OF(X509)* untrusted=sk_X509_new_null();
-    for (vector<XSECCryptoX509*>::const_iterator i=certChain.begin(); i!=certChain.end(); ++i) {
+    for (vector<XSECCryptoX509*>::const_iterator i=certChain.begin(); i!=certChain.end(); ++i)
         sk_X509_push(untrusted,static_cast<OpenSSLCryptoX509*>(*i)->getOpenSSLX509());
-    }
 
-    bool ret = validate(static_cast<OpenSSLCryptoX509*>(certEE)->getOpenSSLX509(),untrusted,keyInfoSource,checkName,keyResolver);
+    bool ret = validate(static_cast<OpenSSLCryptoX509*>(certEE)->getOpenSSLX509(), untrusted, credResolver, criteria);
     sk_X509_free(untrusted);
     return ret;
 }
 
 bool AbstractPKIXTrustEngine::validate(
     Signature& sig,
-    const KeyInfoSource& keyInfoSource,
-    const KeyResolver* keyResolver
+    const CredentialResolver& credResolver,
+    CredentialCriteria* criteria
     ) const
 {
 #ifdef _DEBUG
     NDC ndc("validate");
 #endif
-    Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
+    Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX");
+
+    const KeyInfoResolver* inlineResolver = m_keyInfoResolver;
+    if (!inlineResolver)
+        inlineResolver = XMLToolingConfig::getConfig().getKeyInfoResolver();
+    if (!inlineResolver) {
+        log.error("unable to perform PKIX validation, no KeyInfoResolver available");
+        return false;
+    }
 
-    // Pull the certificate chain out of the signature using an inline KeyResolver.
-    KeyResolver::ResolvedCertificates certs;
-    if (0==m_inlineResolver->resolveCertificates(&sig, certs)) {
+    // Pull the certificate chain out of the signature.
+    X509Credential* x509cred;
+    auto_ptr<Credential> cred(inlineResolver->resolve(&sig,X509Credential::RESOLVE_CERTS|X509Credential::RESOLVE_CRLS));
+    if (!cred.get() || !(x509cred=dynamic_cast<X509Credential*>(cred.get()))) {
+        log.error("unable to perform PKIX validation, signature does not contain any certificates");
+        return false;
+    }
+    const vector<XSECCryptoX509*>& certs = x509cred->getEntityCertificateChain();
+    if (certs.empty()) {
         log.error("unable to perform PKIX validation, signature does not contain any certificates");
         return false;
     }
@@ -357,25 +463,37 @@ bool AbstractPKIXTrustEngine::validate(
 
     // Find and save off a pointer to the certificate that unlocks the object.
     // Most of the time, this will be the first one anyway.
-    XSECCryptoX509* certEE=NULL;
+    XSECCryptoX509* certEE=nullptr;
     SignatureValidator keyValidator;
-    for (vector<XSECCryptoX509*>::const_iterator i=certs.v().begin(); !certEE && i!=certs.v().end(); ++i) {
+    for (vector<XSECCryptoX509*>::const_iterator i=certs.begin(); !certEE && i!=certs.end(); ++i) {
         try {
-            keyValidator.setKey((*i)->clonePublicKey());
+            auto_ptr<XSECCryptoKey> key((*i)->clonePublicKey());
+            keyValidator.setKey(key.get());
             keyValidator.validate(&sig);
-            log.info("signature verified with key inside signature, attempting certificate validation...");
+            log.debug("signature verified with key inside signature, attempting certificate validation...");
             certEE=(*i);
         }
-        catch (ValidationException&) {
-            // trap failures
+        catch (ValidationException& ex) {
+            log.debug(ex.what());
         }
     }
     
-    if (certEE)
-        return validate(certEE,certs.v(),keyInfoSource,true,keyResolver);
-        
-    log.error("failed to verify signature with embedded certificates");
-    return false;
+    if (!certEE) {
+        log.debug("failed to verify signature with embedded certificates");
+        return false;
+    }
+    else if (certEE->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
+        log.error("only the OpenSSL XSEC provider is supported");
+        return false;
+    }
+
+    STACK_OF(X509)* untrusted=sk_X509_new_null();
+    for (vector<XSECCryptoX509*>::const_iterator i=certs.begin(); i!=certs.end(); ++i)
+        sk_X509_push(untrusted,static_cast<OpenSSLCryptoX509*>(*i)->getOpenSSLX509());
+    const vector<XSECCryptoX509CRL*>& crls = x509cred->getCRLs();
+    bool ret = validateWithCRLs(static_cast<OpenSSLCryptoX509*>(certEE)->getOpenSSLX509(), untrusted, credResolver, criteria, &crls);
+    sk_X509_free(untrusted);
+    return ret;
 }
 
 bool AbstractPKIXTrustEngine::validate(
@@ -384,18 +502,37 @@ bool AbstractPKIXTrustEngine::validate(
     KeyInfo* keyInfo,
     const char* in,
     unsigned int in_len,
-    const KeyInfoSource& keyInfoSource,
-    const KeyResolver* keyResolver
+    const CredentialResolver& credResolver,
+    CredentialCriteria* criteria
     ) const
 {
 #ifdef _DEBUG
     NDC ndc("validate");
 #endif
-    Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
+    Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX");
 
-    // Pull the certificate chain out of the KeyInfo using an inline KeyResolver.
-    KeyResolver::ResolvedCertificates certs;
-    if (!keyInfo || 0==m_inlineResolver->resolveCertificates(keyInfo, certs)) {
+    if (!keyInfo) {
+        log.error("unable to perform PKIX validation, KeyInfo not present");
+        return false;
+    }
+
+    const KeyInfoResolver* inlineResolver = m_keyInfoResolver;
+    if (!inlineResolver)
+        inlineResolver = XMLToolingConfig::getConfig().getKeyInfoResolver();
+    if (!inlineResolver) {
+        log.error("unable to perform PKIX validation, no KeyInfoResolver available");
+        return false;
+    }
+
+    // Pull the certificate chain out of the signature.
+    X509Credential* x509cred;
+    auto_ptr<Credential> cred(inlineResolver->resolve(keyInfo,X509Credential::RESOLVE_CERTS));
+    if (!cred.get() || !(x509cred=dynamic_cast<X509Credential*>(cred.get()))) {
+        log.error("unable to perform PKIX validation, KeyInfo does not contain any certificates");
+        return false;
+    }
+    const vector<XSECCryptoX509*>& certs = x509cred->getEntityCertificateChain();
+    if (certs.empty()) {
         log.error("unable to perform PKIX validation, KeyInfo does not contain any certificates");
         return false;
     }
@@ -404,24 +541,34 @@ bool AbstractPKIXTrustEngine::validate(
 
     // Find and save off a pointer to the certificate that unlocks the object.
     // Most of the time, this will be the first one anyway.
-    XSECCryptoX509* certEE=NULL;
-    SignatureValidator keyValidator;
-    for (vector<XSECCryptoX509*>::const_iterator i=certs.v().begin(); !certEE && i!=certs.v().end(); ++i) {
+    XSECCryptoX509* certEE=nullptr;
+    for (vector<XSECCryptoX509*>::const_iterator i=certs.begin(); !certEE && i!=certs.end(); ++i) {
         try {
             auto_ptr<XSECCryptoKey> key((*i)->clonePublicKey());
             if (Signature::verifyRawSignature(key.get(), sigAlgorithm, sig, in, in_len)) {
-                log.info("signature verified with key inside signature, attempting certificate validation...");
+                log.debug("signature verified with key inside signature, attempting certificate validation...");
                 certEE=(*i);
             }
         }
-        catch (SignatureException&) {
-            // trap failures
+        catch (SignatureException& ex) {
+            log.debug(ex.what());
         }
     }
-    
-    if (certEE)
-        return validate(certEE,certs.v(),keyInfoSource,true,keyResolver);
-        
-    log.error("failed to verify signature with embedded certificates");
-    return false;
+
+    if (!certEE) {
+        log.debug("failed to verify signature with embedded certificates");
+        return false;
+    }
+    else if (certEE->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
+        log.error("only the OpenSSL XSEC provider is supported");
+        return false;
+    }
+
+    STACK_OF(X509)* untrusted=sk_X509_new_null();
+    for (vector<XSECCryptoX509*>::const_iterator i=certs.begin(); i!=certs.end(); ++i)
+        sk_X509_push(untrusted,static_cast<OpenSSLCryptoX509*>(*i)->getOpenSSLX509());
+    const vector<XSECCryptoX509CRL*>& crls = x509cred->getCRLs();
+    bool ret = validateWithCRLs(static_cast<OpenSSLCryptoX509*>(certEE)->getOpenSSLX509(), untrusted, credResolver, criteria, &crls);
+    sk_X509_free(untrusted);
+    return ret;
 }