Xerces 3 revisions.
[shibboleth/cpp-xmltooling.git] / xmltooling / security / impl / AbstractPKIXTrustEngine.cpp
index 4ee5691..791290b 100644 (file)
  */
 
 #include "internal.h"
+#include "logging.h"
 #include "security/AbstractPKIXTrustEngine.h"
 #include "signature/KeyInfo.h"
 
-#include <log4cpp/Category.hh>
 #include <openssl/x509_vfy.h>
 #include <openssl/x509v3.h>
 #include <xmltooling/security/CredentialCriteria.h>
 #include <xmltooling/security/X509Credential.h>
 #include <xmltooling/signature/SignatureValidator.h>
 #include <xmltooling/util/NDC.h>
+#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;
 
 
@@ -52,13 +53,17 @@ namespace {
     }
 
     static bool XMLTOOL_DLLLOCAL validate(
-        X509* EE, STACK_OF(X509)* untrusted, AbstractPKIXTrustEngine::PKIXValidationInfoIterator* pkixInfo
+        X509* EE,
+        STACK_OF(X509)* untrusted,
+        AbstractPKIXTrustEngine::PKIXValidationInfoIterator* pkixInfo,
+        bool fullCRLChain,
+        const vector<XSECCryptoX509CRL*>* inlineCRLs=NULL
         )
     {
         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");
+        log.debug("supplying PKIX Validation information");
     
         // We need this for CRL support.
         X509_STORE* store=X509_STORE_new();
@@ -66,32 +71,53 @@ namespace {
             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.
+        int count=0;
         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());
+                ++count;
             }
         }
 
+        log.debug("supplied (%d) CA certificate(s)", count);
+
+#if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
+        count=0;
+        if (inlineCRLs) {
+            for (vector<XSECCryptoX509CRL*>::const_iterator j=inlineCRLs->begin(); j!=inlineCRLs->end(); ++j) {
+                if ((*j)->getProviderName()==DSIGConstants::s_unicodeStrPROVOpenSSL) {
+                    // owned by store
+                    X509_STORE_add_crl(store, X509_CRL_dup(static_cast<OpenSSLCryptoX509CRL*>(*j)->getOpenSSLX509CRL()));
+                    ++count;
+                }
+            }
+        }
         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()));
+                ++count;
             }
         }
-     
+        log.debug("supplied (%d) CRL(s)", count);
+        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
+        if ((inlineCRLs && !inlineCRLs->empty()) || !pkixInfo->getCRLs().empty()) {
+            log.warn("OpenSSL versions < 0.9.7 do not support CRL checking");
+        }
+#endif
+
         // AFAICT, EE and untrusted are passed in but not owned by the ctx.
-    #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
+#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");
@@ -99,9 +125,9 @@ namespace {
             X509_STORE_free(store);
             return false;
         }
-    #else
+#else
         X509_STORE_CTX_init(&ctx,store,EE,untrusted);
-    #endif
+#endif
     
         // Seems to be most efficient to just pass in the CA stack.
         X509_STORE_CTX_trusted_stack(&ctx,CAstack);
@@ -136,6 +162,13 @@ namespace {
     }
 };
 
+AbstractPKIXTrustEngine::AbstractPKIXTrustEngine(const xercesc::DOMElement* e) : TrustEngine(e), m_fullCRLChain(false)
+{
+    static XMLCh fullCRLChain[] = UNICODE_LITERAL_12(f,u,l,l,C,R,L,C,h,a,i,n);
+    const XMLCh* flag = e ? e->getAttributeNS(NULL, fullCRLChain) : NULL;
+    m_fullCRLChain = (flag && (*flag == xercesc::chLatin_t || *flag == xercesc::chDigit_1));
+}
+
 bool AbstractPKIXTrustEngine::checkEntityNames(
     X509* certEE, const CredentialResolver& credResolver, const CredentialCriteria& criteria
     ) const
@@ -169,7 +202,7 @@ bool AbstractPKIXTrustEngine::checkEntityNames(
             buf[len] = '\0';
             subjectstr+=buf;
         }
-        log.debugStream() << "certificate subject: " << subjectstr << CategoryStream::ENDLINE;
+        log.debugStream() << "certificate subject: " << subjectstr << logging::eol;
         // 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);
         BIO_flush(b2);
@@ -243,15 +276,16 @@ bool AbstractPKIXTrustEngine::checkEntityNames(
     return false;
 }
 
-bool AbstractPKIXTrustEngine::validate(
+bool AbstractPKIXTrustEngine::validateWithCRLs(
     X509* certEE,
     STACK_OF(X509)* certChain,
     const CredentialResolver& credResolver,
-    CredentialCriteria* criteria
+    CredentialCriteria* criteria,
+    const std::vector<XSECCryptoX509CRL*>* inlineCRLs
     ) const
 {
 #ifdef _DEBUG
-    NDC ndc("validate");
+    NDC ndc("validateWithCRLs");
 #endif
     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX");
 
@@ -262,8 +296,8 @@ bool AbstractPKIXTrustEngine::validate(
 
     if (criteria && criteria->getPeerName() && *(criteria->getPeerName())) {
         log.debug("checking that the certificate name is acceptable");
-        if (criteria->getUsage()==CredentialCriteria::UNSPECIFIED_CREDENTIAL)
-            criteria->setUsage(CredentialCriteria::SIGNING_CREDENTIAL);
+        if (criteria->getUsage()==Credential::UNSPECIFIED_CREDENTIAL)
+            criteria->setUsage(Credential::SIGNING_CREDENTIAL);
         if (!checkEntityNames(certEE,credResolver,*criteria)) {
             log.error("certificate name was not acceptable");
             return false;
@@ -274,7 +308,7 @@ bool AbstractPKIXTrustEngine::validate(
 
     auto_ptr<PKIXValidationInfoIterator> pkix(getPKIXValidationInfoIterator(credResolver, criteria));
     while (pkix->next()) {
-        if (::validate(certEE,certChain,pkix.get())) {
+        if (::validate(certEE,certChain,pkix.get(),m_fullCRLChain,inlineCRLs)) {
             return true;
         }
     }
@@ -284,6 +318,16 @@ bool AbstractPKIXTrustEngine::validate(
 }
 
 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 CredentialResolver& credResolver,
@@ -332,7 +376,7 @@ bool AbstractPKIXTrustEngine::validate(
 
     // Pull the certificate chain out of the signature.
     X509Credential* x509cred;
-    auto_ptr<Credential> cred(inlineResolver->resolve(&sig,X509Credential::RESOLVE_CERTS));
+    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;
@@ -362,11 +406,22 @@ bool AbstractPKIXTrustEngine::validate(
         }
     }
     
-    if (certEE)
-        return validate(certEE,certs,credResolver,criteria);
-        
-    log.debug("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(