https://issues.shibboleth.net/jira/browse/CPPXT-78
[shibboleth/cpp-xmltooling.git] / xmltooling / security / impl / AbstractPKIXTrustEngine.cpp
index e5c207f..25cdcb1 100644 (file)
@@ -1,17 +1,21 @@
-/*
- *  Copyright 2001-2010 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.
  */
 
 /**
@@ -71,26 +75,92 @@ namespace {
         return s;
     }
 
-    static void XMLTOOL_DLLLOCAL getRemoteCRLs(vector<XSECCryptoX509CRL*>& crls, const char* cdpuri, Category& log) {
+    static time_t XMLTOOL_DLLLOCAL getCRLTime(const ASN1_TIME *a)
+    {
+        struct tm t;
+        memset(&t, 0, sizeof(t));
+        // RFC 5280, sections 5.1.2.4 and 5.1.2.5 require thisUpdate and nextUpdate
+        // to be encoded as UTCTime until 2049, and RFC 5280 section 4.1.2.5.1
+        // further restricts the format to "YYMMDDHHMMSSZ" ("even where the number
+        // of seconds is zero").
+        // As long as OpenSSL doesn't provide any API to convert ASN1_TIME values
+        // time_t, we therefore have to parse it ourselves, unfortunately.
+        if (sscanf((const char*)a->data, "%2d%2d%2d%2d%2d%2dZ",
+            &t.tm_year, &t.tm_mon, &t.tm_mday,
+            &t.tm_hour, &t.tm_min, &t.tm_sec) == 6) {
+            if (t.tm_year <= 50) {
+                // RFC 5280, section 4.1.2.5.1
+                t.tm_year += 100;
+            }
+            t.tm_mon--;
+#if defined(HAVE_TIMEGM)
+            return timegm(&t);
+#else
+            // Windows, and hopefully most others...?
+            return mktime(&t) - timezone;
+#endif
+        }
+        return (time_t)-1;
+    }
+
+    static bool XMLTOOL_DLLLOCAL isFreshCRL(XSECCryptoX509CRL *c, Category* log=nullptr)
+    {
+        // eventually, these should be made configurable
+        #define MIN_SECS_REMAINING 86400
+        #define MIN_PERCENT_REMAINING 10
+        if (c) {
+            const X509_CRL* crl = static_cast<OpenSSLCryptoX509CRL*>(c)->getOpenSSLX509CRL();
+            time_t thisUpdate = getCRLTime(X509_CRL_get_lastUpdate(crl));
+            time_t nextUpdate = getCRLTime(X509_CRL_get_nextUpdate(crl));
+            time_t now = time(nullptr);
+
+            if (thisUpdate < 0 || nextUpdate < 0) {
+                // we failed to parse at least one of the fields (they were not encoded
+                // as required by RFC 5280, actually)
+                time_t exp = now + MIN_SECS_REMAINING;
+                if (log) {
+                    log->warn("isFreshCRL (issuer '%s'): improperly encoded thisUpdate or nextUpdate field - falling back to simple time comparison",
+                              (X509_NAME_to_string(X509_CRL_get_issuer(crl))).c_str());
+                }
+                return (X509_cmp_time(X509_CRL_get_nextUpdate(crl), &exp) > 0) ? true : false;
+            }
+            else {
+                if (log && log->isDebugEnabled()) {
+                    log->debug("isFreshCRL (issuer '%s'): %.0f seconds until nextUpdate (%3.2f%% elapsed since thisUpdate)",
+                              (X509_NAME_to_string(X509_CRL_get_issuer(crl))).c_str(),
+                              difftime(nextUpdate, now), (difftime(now, thisUpdate) * 100) / difftime(nextUpdate, thisUpdate));
+                }
+
+                // consider it recent enough if there are at least MIN_SECS_REMAINING
+                // to the nextUpdate, and at least MIN_PERCENT_REMAINING of its
+                // overall "validity" are remaining to the nextUpdate
+                return (now + MIN_SECS_REMAINING < nextUpdate) &&
+                        ((difftime(nextUpdate, now) * 100) / difftime(nextUpdate, thisUpdate) > MIN_PERCENT_REMAINING);
+            }
+        }
+        return false;
+    }
+
+    static XSECCryptoX509CRL* XMLTOOL_DLLLOCAL getRemoteCRLs(const char* cdpuri, Category& log) {
         // This is a temporary CRL cache implementation to avoid breaking binary compatibility
         // for the library. Caching can't rely on any member objects within the TrustEngine,
         // including locks, so we're using the global library lock for the time being.
         // All other state is kept in the file system.
 
+        // minimum number of seconds between re-attempting a download from one particular CRLDP
+        #define MIN_RETRY_WAIT 60
+
         // The filenames for the CRL cache are based on a hash of the CRL location.
         string cdpfile = SecurityHelper::doHash("SHA1", cdpuri, strlen(cdpuri)) + ".crl";
         XMLToolingConfig::getConfig().getPathResolver()->resolve(cdpfile, PathResolver::XMLTOOLING_RUN_FILE);
-        string cdpstaging = cdpfile + ".bak";
-        string counterfile = cdpfile + ".cnt";
-
-        // Need to move this to a configurable parameter once we can break binary compatibility.
-        // Ideally this would be based on a percentage of the original CRL window, but OpenSSL
-        // doesn't provide much in the way of ASN1_TIME parsing functions. It does support adding
-        // a fixed time value and comparing against known times.
-        #define MIN_SECS_REMAINING 86400;
-        long counter = 0;
+        string cdpstaging = cdpfile + ".tmp";
+        string tsfile = cdpfile + ".ts";
+
+        time_t now = time(nullptr);
+        vector<XSECCryptoX509CRL*> crls;
+
         try {
-            // While holding the lock, check for a cached copy of the CRL, and check its validity.
+            // While holding the lock, check for a cached copy of the CRL, and remove "expired" ones.
             Locker glock(&XMLToolingConfig::getConfig());
 #ifdef WIN32
             struct _stat stat_buf;
@@ -101,113 +171,98 @@ namespace {
 #endif
                 SecurityHelper::loadCRLsFromFile(crls, cdpfile.c_str());
                 if (crls.empty() || crls.front()->getProviderName() != DSIGConstants::s_unicodeStrPROVOpenSSL ||
-                    X509_cmp_time(X509_CRL_get_nextUpdate(static_cast<OpenSSLCryptoX509CRL*>(crls.front())->getOpenSSLX509CRL()), nullptr) < 0) {
+                    X509_cmp_time(X509_CRL_get_nextUpdate(static_cast<OpenSSLCryptoX509CRL*>(crls.front())->getOpenSSLX509CRL()), &now) < 0) {
                     for_each(crls.begin(), crls.end(), xmltooling::cleanup<XSECCryptoX509CRL>());
                     crls.clear();
                     remove(cdpfile.c_str());    // may as well delete the local copy
-                    remove(counterfile.c_str());
-                    log.info("cached CRL(s) from (%s) have expired", cdpuri);
-                }
-                else {
-                    // Look for a file containing the allowable time remaining on the CRL.
-                    // We store this counter in the file system because of the binary compatibility issue.
-                    try {
-                        ifstream countersrc(counterfile.c_str());
-                        if (countersrc)
-                            countersrc >> counter;
-                    }
-                    catch (exception&) {
-                        counter = 0;
-                    }
-                    if (counter == 0)
-                        counter = MIN_SECS_REMAINING;
-
-                    // See if the time left is under the counter threshold.
-                    time_t exp = time(nullptr) + counter;
-                    if (X509_cmp_time(X509_CRL_get_nextUpdate(static_cast<OpenSSLCryptoX509CRL*>(crls.front())->getOpenSSLX509CRL()), &exp) < 0) {
-                        for_each(crls.begin(), crls.end(), xmltooling::cleanup<XSECCryptoX509CRL>());
-                        crls.clear();
-                        log.info("cached CRL(s) from (%s) will expire within %ld seconds, attempting to update them", cdpuri, counter);
-                    }
+                    remove(tsfile.c_str());
+                    log.info("deleting cached CRL from %s with nextUpdate field in the past", cdpuri);
                 }
             }
         }
         catch (exception& ex) {
-            log.error("exception loading cached copy of CRL at (%s): %s", cdpuri, ex.what());
+            log.error("exception loading cached copy of CRL from %s: %s", cdpuri, ex.what());
         }
 
-        if (crls.empty()) {
+        if (crls.empty() || !isFreshCRL(crls.front(), &log)) {
+            bool updateTimestamp = true;
             try {
                 // If we get here, the cached copy didn't exist yet, or it's time to refresh.
-                SOAPTransport::Address addr("AbstractPKIXTrustEngine", cdpuri, cdpuri);
-                string scheme(addr.m_endpoint, strchr(addr.m_endpoint,':') - addr.m_endpoint);
-                auto_ptr<SOAPTransport> soap(XMLToolingConfig::getConfig().SOAPTransportManager.newPlugin(scheme.c_str(), addr));
-                soap->send();
-                istream& msg = soap->receive();
-                Locker glock(&XMLToolingConfig::getConfig());
-                ofstream out(cdpstaging.c_str(), fstream::trunc|fstream::binary);
-                out << msg.rdbuf();
-                out.close();
-                SecurityHelper::loadCRLsFromFile(crls, cdpstaging.c_str());
-                if (crls.empty() || crls.front()->getProviderName() != DSIGConstants::s_unicodeStrPROVOpenSSL ||
-                    X509_cmp_time(X509_CRL_get_nextUpdate(static_cast<OpenSSLCryptoX509CRL*>(crls.front())->getOpenSSLX509CRL()), nullptr) < 0) {
-                    // The "new" CRLs weren't usable, so get rid of them.
-                    for_each(crls.begin(), crls.end(), xmltooling::cleanup<XSECCryptoX509CRL>());
-                    crls.clear();
-                    remove(cdpstaging.c_str());
-                    log.error("updated CRL(s) from (%s) have already expired", cdpuri);
-
-                    // If counter isn't 0, then we were attempting an update of still-valid CRLs, so reload the old ones
-                    // and cut the counter in half for next time.
-                    if (counter > 0) {
-                        SecurityHelper::loadCRLsFromFile(crls, cdpfile.c_str());
-                        ofstream countersink(counterfile.c_str(), fstream::trunc);
-                        counter /= 2;
-                        countersink << counter;
-                        log.info("failed CRL update attempt, reducing threshold to %ld seconds", counter);
-                    }
+                // To limit the rate of unsuccessful attempts when a CRLDP is unreachable,
+                // we remember the timestamp of the last attempt (both successful/unsuccessful).
+                // We store this in the file system because of the binary compatibility issue.
+                time_t ts = 0;
+                try {
+                    Locker glock(&XMLToolingConfig::getConfig());
+                    ifstream tssrc(tsfile.c_str());
+                    if (tssrc)
+                        tssrc >> ts;
+                }
+                catch (exception&) {
+                    ts = 0;
                 }
-                else {
-                    // If counter isn't zero, we reloaded; see if the new CRLs are "more" valid than before.
-                    if (counter > 0) {
-                        time_t exp = time(nullptr) + counter;
-                        if (X509_cmp_time(X509_CRL_get_nextUpdate(static_cast<OpenSSLCryptoX509CRL*>(crls.front())->getOpenSSLX509CRL()), &exp) < 0) {
-                            // Still invalid past the acceptable interval, so they're the same as what we had.
-                            // Remove the extra copy, and cut the counter in half for next time.
-                            remove(cdpstaging.c_str());
-                            ofstream countersink(counterfile.c_str(), fstream::trunc);
-                            counter /= 2;
-                            countersink << counter;
-                            log.info("remote CRL(s) unchanged, reducing threshold to %ld seconds", counter);
-                        }
-                        else {
-                            counter = 0;
-                            log.info("remote CRL(s) updated");
-                        }
-                    }
 
-                    if (counter == 0) {
-                        // "Commit" the new CRLs.
+                if (difftime(now, ts) > MIN_RETRY_WAIT) {
+                    SOAPTransport::Address addr("AbstractPKIXTrustEngine", cdpuri, cdpuri);
+                    string scheme(addr.m_endpoint, strchr(addr.m_endpoint,':') - addr.m_endpoint);
+                    auto_ptr<SOAPTransport> soap(XMLToolingConfig::getConfig().SOAPTransportManager.newPlugin(scheme.c_str(), addr));
+                    soap->send();
+                    istream& msg = soap->receive();
+                    Locker glock(&XMLToolingConfig::getConfig());
+                    ofstream out(cdpstaging.c_str(), fstream::trunc|fstream::binary);
+                    out << msg.rdbuf();
+                    out.close();
+                    SecurityHelper::loadCRLsFromFile(crls, cdpstaging.c_str());
+                    if (crls.empty() || crls.front()->getProviderName() != DSIGConstants::s_unicodeStrPROVOpenSSL ||
+                        X509_cmp_time(X509_CRL_get_nextUpdate(static_cast<OpenSSLCryptoX509CRL*>(crls.front())->getOpenSSLX509CRL()), &now) < 0) {
+                        // The "new" CRL wasn't usable, so get rid of it.
+                        for_each(crls.begin(), crls.end(), xmltooling::cleanup<XSECCryptoX509CRL>());
+                        crls.clear();
+                        remove(cdpstaging.c_str());
+                        log.error("ignoring CRL retrieved from %s with nextUpdate field in the past", cdpuri);
+                    }
+                    else {
+                        // "Commit" the new CRL. Note that we might add a CRL which doesn't pass
+                        // isFreshCRL, but that's preferrable over adding none at all.
+                        log.info("CRL refreshed from %s", cdpuri);
                         remove(cdpfile.c_str());
-                        remove(counterfile.c_str());
                         if (rename(cdpstaging.c_str(), cdpfile.c_str()) != 0)
                             log.error("unable to rename CRL staging file");
                     }
                 }
+                else {
+                    updateTimestamp = false;    // don't update if we're within the backoff window
+                }
             }
             catch (exception& ex) {
-                log.error("exception downloading/caching CRL at (%s): %s", cdpuri, ex.what());
+                log.error("exception downloading/caching CRL from %s: %s", cdpuri, ex.what());
+            }
+
+            if (updateTimestamp) {
+                // update the timestamp file
+                Locker glock(&XMLToolingConfig::getConfig());
+                ofstream tssink(tsfile.c_str(), fstream::trunc);
+                tssink << now;
+                tssink.close();
             }
         }
+
+        if (crls.empty())
+            return nullptr;
+        for_each(crls.begin() + 1, crls.end(), xmltooling::cleanup<XSECCryptoX509CRL>());
+        return crls.front();
     }
 
     static bool XMLTOOL_DLLLOCAL validate(
         X509* EE,
         STACK_OF(X509)* untrusted,
         AbstractPKIXTrustEngine::PKIXValidationInfoIterator* pkixInfo,
-               bool useCRL,
+        bool useCRL,
         bool fullCRLChain,
-        const vector<XSECCryptoX509CRL*>* inlineCRLs=nullptr
+        const vector<XSECCryptoX509CRL*>* inlineCRLs=nullptr,
+        bool policyMappingInhibit=false,
+        bool anyPolicyInhibit=false,
+        const set<string>* policyOIDs=nullptr
         )
     {
         Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
@@ -221,6 +276,62 @@ namespace {
             log_openssl();
             return false;
         }
+
+        // PKIX policy checking (cf. RFCs 3280/5280 section 6)
+        if (policyMappingInhibit || anyPolicyInhibit || (policyOIDs && !policyOIDs->empty())) {
+#if (OPENSSL_VERSION_NUMBER < 0x00908000L)
+            log.error("PKIX policy checking option is configured, but OpenSSL version is less than 0.9.8");
+            X509_STORE_free(store);
+            return false;
+#else
+            unsigned long pflags = 0;
+            X509_VERIFY_PARAM *vpm = X509_VERIFY_PARAM_new();
+            if (!vpm) {
+                log_openssl();
+                X509_STORE_free(store);
+                return false;
+            }
+
+            // populate the "user-initial-policy-set" input variable
+            if (policyOIDs && !policyOIDs->empty()) {
+                for (set<string>::const_iterator o=policyOIDs->begin(); o!=policyOIDs->end(); o++) {
+                    ASN1_OBJECT *oid = OBJ_txt2obj(o->c_str(), 1);
+                    if (oid && X509_VERIFY_PARAM_add0_policy(vpm, oid)) {
+                        log.debug("OID (%s) added to set of acceptable policies", o->c_str());
+                    }
+                    else {
+                        log_openssl();
+                        log.error("unable to parse/configure policy OID value (%s)", o->c_str());
+                        if (oid)
+                            ASN1_OBJECT_free(oid);
+                        X509_VERIFY_PARAM_free(vpm);
+                        X509_STORE_free(store);
+                        return false;
+                    }
+                }
+                // when the user has supplied at least one policy OID, he obviously wants to check
+                // for an explicit policy ("initial-explicit-policy")
+                pflags |= X509_V_FLAG_EXPLICIT_POLICY;
+            }
+
+            // "initial-policy-mapping-inhibit" input variable
+            if (policyMappingInhibit)
+                pflags |= X509_V_FLAG_INHIBIT_MAP;
+            // "initial-any-policy-inhibit" input variable
+            if (anyPolicyInhibit)
+                pflags |= X509_V_FLAG_INHIBIT_ANY;
+
+            if (!X509_VERIFY_PARAM_set_flags(vpm, pflags) || !X509_STORE_set1_param(store, vpm)) {
+                log_openssl();
+                log.error("unable to set PKIX policy checking parameters");
+                X509_VERIFY_PARAM_free(vpm);
+                X509_STORE_free(store);
+                return false;
+            }
+
+            X509_VERIFY_PARAM_free(vpm);
+#endif
+        }
     
         // This contains the state of the validate operation.
         int count=0;
@@ -274,15 +385,16 @@ namespace {
             // the CRL in that case. If we end up not adding a CRL for a particular link in the chain, the
             // validation will fail (if the fullChain option was set).
             set<string> crlissuers;
+            time_t now = time(nullptr);
             if (inlineCRLs) {
                 for (vector<XSECCryptoX509CRL*>::const_iterator j=inlineCRLs->begin(); j!=inlineCRLs->end(); ++j) {
                     if ((*j)->getProviderName()==DSIGConstants::s_unicodeStrPROVOpenSSL &&
-                        (X509_cmp_time(X509_CRL_get_nextUpdate(static_cast<OpenSSLCryptoX509CRL*>(*j)->getOpenSSLX509CRL()), nullptr)==1)) {
+                        (X509_cmp_time(X509_CRL_get_nextUpdate(static_cast<OpenSSLCryptoX509CRL*>(*j)->getOpenSSLX509CRL()), &now) > 0)) {
                         // owned by store
                         X509_STORE_add_crl(store, X509_CRL_dup(static_cast<OpenSSLCryptoX509CRL*>(*j)->getOpenSSLX509CRL()));
                         string crlissuer(X509_NAME_to_string(X509_CRL_get_issuer(static_cast<OpenSSLCryptoX509CRL*>(*j)->getOpenSSLX509CRL())));
                         if (!crlissuer.empty()) {
-                            log.debug("added CRL issued by (%s)", crlissuer.c_str());
+                            log.debug("added inline CRL issued by (%s)", crlissuer.c_str());
                             crlissuers.insert(crlissuer);
                         }
                     }
@@ -291,7 +403,7 @@ namespace {
             const vector<XSECCryptoX509CRL*>& crls = pkixInfo->getCRLs();
             for (vector<XSECCryptoX509CRL*>::const_iterator j=crls.begin(); j!=crls.end(); ++j) {
                 if ((*j)->getProviderName()==DSIGConstants::s_unicodeStrPROVOpenSSL &&
-                    (X509_cmp_time(X509_CRL_get_nextUpdate(static_cast<OpenSSLCryptoX509CRL*>(*j)->getOpenSSLX509CRL()), nullptr)==1)) {
+                    (X509_cmp_time(X509_CRL_get_nextUpdate(static_cast<OpenSSLCryptoX509CRL*>(*j)->getOpenSSLX509CRL()), &now) > 0)) {
                     // owned by store
                     X509_STORE_add_crl(store, X509_CRL_dup(static_cast<OpenSSLCryptoX509CRL*>(*j)->getOpenSSLX509CRL()));
                     string crlissuer(X509_NAME_to_string(X509_CRL_get_issuer(static_cast<OpenSSLCryptoX509CRL*>(*j)->getOpenSSLX509CRL())));
@@ -306,7 +418,7 @@ namespace {
                 X509 *cert = sk_X509_value(untrusted, i);
                 string crlissuer(X509_NAME_to_string(X509_get_issuer_name(cert)));
                 if (crlissuers.count(crlissuer)) {
-                   // We already have a CRL for this cert, so skip it.
+                   // We already have a CRL for this cert, so skip CRLDP processing for this one.
                    continue;
                 }
 
@@ -320,51 +432,33 @@ namespace {
                         GENERAL_NAME* gen = sk_GENERAL_NAME_value(dp->distpoint->name.fullname, iii);
                         // Only consider HTTP URIs, and stop after the first one we find.
 #ifdef HAVE_STRCASECMP
-                        if (gen->type == GEN_URI && (!strncasecmp((const char*)gen->d.ia5->data, "http:", 5) ||
-                                !strncasecmp((const char*)gen->d.ia5->data, "https:", 6))) {
+                        if (gen->type == GEN_URI && (!strncasecmp((const char*)gen->d.ia5->data, "http:", 5))) {
 #else
-                        if (gen->type == GEN_URI && (!strnicmp((const char*)gen->d.ia5->data, "http:", 5) ||
-                                !strnicmp((const char*)gen->d.ia5->data, "https:", 6))) {
+                        if (gen->type == GEN_URI && (!strnicmp((const char*)gen->d.ia5->data, "http:", 5))) {
 #endif
                             const char* cdpuri = (const char*)gen->d.ia5->data;
-                            vector<XSECCryptoX509CRL*> crls;
-                            getRemoteCRLs(crls, cdpuri, log);
-                            for (vector<XSECCryptoX509CRL*>::const_iterator j = crls.begin(); j != crls.end(); ++j) {
-                                if ((*j)->getProviderName()==DSIGConstants::s_unicodeStrPROVOpenSSL &&
-                                    X509_cmp_time(X509_CRL_get_nextUpdate(static_cast<OpenSSLCryptoX509CRL*>(*j)->getOpenSSLX509CRL()), nullptr) > 0) {
-                                    // owned by store
-                                    X509_STORE_add_crl(store, X509_CRL_dup(static_cast<OpenSSLCryptoX509CRL*>(*j)->getOpenSSLX509CRL()));
-                                    log.debug("added CRL issued by (%s)", crlissuer.c_str());
-                                    crlissuers.insert(crlissuer);
-                                    foundUsableCDP = true;
-                                }
+                            auto_ptr<XSECCryptoX509CRL> crl(getRemoteCRLs(cdpuri, log));
+                            if (crl.get() && crl->getProviderName()==DSIGConstants::s_unicodeStrPROVOpenSSL &&
+                                (isFreshCRL(crl.get()) || (ii == sk_DIST_POINT_num(dps)-1 && iii == sk_GENERAL_NAME_num(dp->distpoint->name.fullname)-1))) {
+                                // owned by store
+                                X509_STORE_add_crl(store, X509_CRL_dup(static_cast<OpenSSLCryptoX509CRL*>(crl.get())->getOpenSSLX509CRL()));
+                                log.debug("added CRL issued by (%s)", crlissuer.c_str());
+                                crlissuers.insert(crlissuer);
+                                foundUsableCDP = true;
                             }
-                            for_each(crls.begin(), crls.end(), xmltooling::cleanup<XSECCryptoX509CRL>());
                         }
                     }
                 }
                 sk_DIST_POINT_free(dps);
             }
 
-            if (count > 0) {
-                X509_STORE_set_flags(store, fullCRLChain ? (X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL) : (X509_V_FLAG_CRL_CHECK));
-                   }
-                   else {
-                           log.warn("CRL checking is enabled, but none were supplied");
-                X509_STORE_CTX_cleanup(&ctx);
-                X509_STORE_free(store);
-                sk_X509_free(CAstack);
-                return false;
-                   }
-#else
-                       log.warn("CRL checking is enabled, but OpenSSL version is too old");
-            X509_STORE_CTX_cleanup(&ctx);
-            X509_STORE_free(store);
-            sk_X509_free(CAstack);
-            return false;
-#endif
             // Do a second pass verify with CRLs in place.
+            X509_STORE_CTX_set_flags(&ctx, fullCRLChain ? (X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL) : (X509_V_FLAG_CRL_CHECK));
             ret=X509_verify_cert(&ctx);
+#else
+            log.warn("CRL checking is enabled, but OpenSSL version is too old");
+            ret = 0;
+#endif
         }
 
         // Clean up...
@@ -380,8 +474,12 @@ namespace {
         return false;
     }
 
-    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 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 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);
 };
 
 AbstractPKIXTrustEngine::PKIXValidationInfoIterator::PKIXValidationInfoIterator()
@@ -394,8 +492,10 @@ AbstractPKIXTrustEngine::PKIXValidationInfoIterator::~PKIXValidationInfoIterator
 
 AbstractPKIXTrustEngine::AbstractPKIXTrustEngine(const xercesc::DOMElement* e)
        : TrustEngine(e),
+               m_checkRevocation(XMLHelper::getAttrString(e, nullptr, checkRevocation)),
                m_fullCRLChain(XMLHelper::getAttrBool(e, false, fullCRLChain)),
-               m_checkRevocation(XMLHelper::getAttrString(e, nullptr, checkRevocation))
+               m_policyMappingInhibit(XMLHelper::getAttrBool(e, false, policyMappingInhibit)),
+               m_anyPolicyInhibit(XMLHelper::getAttrBool(e, false, anyPolicyInhibit))
 {
     if (m_fullCRLChain) {
         Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX").warn(
@@ -406,6 +506,20 @@ AbstractPKIXTrustEngine::AbstractPKIXTrustEngine(const xercesc::DOMElement* e)
     else if (m_checkRevocation == "fullChain") {
         m_fullCRLChain = true; // in case anything's using this
     }
+
+    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());
+            }
+        }
+        c = XMLHelper::getNextSiblingElement(c);
+    }
 }
 
 AbstractPKIXTrustEngine::~AbstractPKIXTrustEngine()
@@ -423,10 +537,24 @@ bool AbstractPKIXTrustEngine::checkEntityNames(
     credResolver.resolve(creds,&criteria);
 
     // Build a list of acceptable names.
-    set<string> trustednames;
-    trustednames.insert(criteria.getPeerName());
-    for (vector<const Credential*>::const_iterator cred = creds.begin(); cred!=creds.end(); ++cred)
+    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());
+            }
+        }
+    }
 
     X509_NAME* subject=X509_get_subject_name(certEE);
     if (subject) {
@@ -564,9 +692,9 @@ bool AbstractPKIXTrustEngine::validateWithCRLs(
         return false;
     }
 
-    if (criteria && criteria->getPeerName() && *(criteria->getPeerName())) {
+    if ((criteria && criteria->getPeerName() && *(criteria->getPeerName())) || !m_trustedNames.empty()) {
         log.debug("checking that the certificate name is acceptable");
-        if (criteria->getUsage()==Credential::UNSPECIFIED_CREDENTIAL)
+        if (criteria && criteria->getUsage()==Credential::UNSPECIFIED_CREDENTIAL)
             criteria->setUsage(Credential::SIGNING_CREDENTIAL);
         if (!checkEntityNames(certEE,credResolver,*criteria)) {
             log.error("certificate name was not acceptable");
@@ -584,7 +712,10 @@ bool AbstractPKIXTrustEngine::validateWithCRLs(
                                pkix.get(),
                                (m_checkRevocation=="entityOnly" || m_checkRevocation=="fullChain"),
                                (m_checkRevocation=="fullChain"),
-                               (m_checkRevocation=="entityOnly" || m_checkRevocation=="fullChain") ? inlineCRLs : nullptr
+                               (m_checkRevocation=="entityOnly" || m_checkRevocation=="fullChain") ? inlineCRLs : nullptr,
+                               m_policyMappingInhibit,
+                               m_anyPolicyInhibit,
+                               &m_policyOIDs
                                )) {
             return true;
         }