Change license header, remove stale pkg files.
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / SignatureMetadataFilter.cpp
index f736923..6b4e82b 100644 (file)
@@ -1,17 +1,21 @@
-/*
- *  Copyright 2001-2008 Internet2
+/**
+ * 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.
  *
- * 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
+ * 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 "signature/SignatureProfileValidator.h"
 
 #include <xmltooling/logging.h>
+#include <xmltooling/XMLToolingConfig.h>
 #include <xmltooling/security/Credential.h>
 #include <xmltooling/security/CredentialCriteria.h>
 #include <xmltooling/security/CredentialResolver.h>
 #include <xmltooling/security/SignatureTrustEngine.h>
+#include <xmltooling/signature/Signature.h>
 #include <xmltooling/signature/SignatureValidator.h>
 #include <xmltooling/util/NDC.h>
 
@@ -52,9 +58,9 @@ namespace opensaml {
             Lockable* lock() {return this;}
             void unlock() {}
 
-            const Credential* resolve(const CredentialCriteria* criteria=NULL) const {return NULL;}
+            const Credential* resolve(const CredentialCriteria* criteria=nullptr) const {return nullptr;}
             vector<const Credential*>::size_type resolve(
-                vector<const Credential*>& results, const CredentialCriteria* criteria=NULL
+                vector<const Credential*>& results, const CredentialCriteria* criteria=nullptr
                 ) const {return 0;}
         };
 
@@ -75,7 +81,7 @@ namespace opensaml {
             void doFilter(EntityDescriptor& entity, bool rootObject=false) const;
             void verifySignature(Signature* sig, const XMLCh* peerName) const;
 
-            bool m_verifyRoles;
+            bool m_verifyRoles,m_verifyName;
             CredentialResolver* m_credResolver;
             SignatureTrustEngine* m_trust;
             SignatureProfileValidator m_profileValidator;
@@ -97,35 +103,40 @@ static const XMLCh certificate[] =          UNICODE_LITERAL_11(c,e,r,t,i,f,i,c,a
 static const XMLCh Certificate[] =          UNICODE_LITERAL_11(C,e,r,t,i,f,i,c,a,t,e);
 static const XMLCh Path[] =                 UNICODE_LITERAL_4(P,a,t,h);
 static const XMLCh verifyRoles[] =          UNICODE_LITERAL_11(v,e,r,i,f,y,R,o,l,e,s);
+static const XMLCh verifyName[] =           UNICODE_LITERAL_10(v,e,r,i,f,y,N,a,m,e);
 
 SignatureMetadataFilter::SignatureMetadataFilter(const DOMElement* e)
-    : m_verifyRoles(false), m_credResolver(NULL), m_trust(NULL), m_log(Category::getInstance(SAML_LOGCAT".MetadataFilter.Signature"))
+    : m_verifyRoles(XMLHelper::getAttrBool(e, false, verifyRoles)),
+        m_verifyName(XMLHelper::getAttrBool(e, true, verifyName)),
+        m_credResolver(nullptr), m_trust(nullptr),
+        m_log(Category::getInstance(SAML_LOGCAT".MetadataFilter.Signature"))
 {
-    const XMLCh* flag = e ? e->getAttributeNS(NULL,verifyRoles) : NULL;
-    m_verifyRoles = (flag && (*flag == chLatin_t || *flag == chDigit_1));
-
-    if (e && e->hasAttributeNS(NULL,certificate)) {
+    if (e && e->hasAttributeNS(nullptr,certificate)) {
         // Use a file-based credential resolver rooted here.
-        m_credResolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(FILESYSTEM_CREDENTIAL_RESOLVER,e);
+        m_credResolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(FILESYSTEM_CREDENTIAL_RESOLVER, e);
         return;
     }
 
-    DOMElement* sub = e ? XMLHelper::getFirstChildElement(e, _CredentialResolver) : NULL;
-    auto_ptr_char t(sub ? sub->getAttributeNS(NULL,type) : NULL);
-    if (t.get()) {
-        m_credResolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(t.get(),sub);
-        return;
+    DOMElement* sub = XMLHelper::getFirstChildElement(e, _CredentialResolver);
+    if (sub) {
+        string t = XMLHelper::getAttrString(sub, nullptr, type);
+        if (!t.empty()) {
+            m_credResolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(t.c_str(), sub);
+            return;
+        }
     }
 
-    sub = e ? XMLHelper::getFirstChildElement(e, _TrustEngine) : NULL;
-    auto_ptr_char t2(sub ? sub->getAttributeNS(NULL,type) : NULL);
-    if (t2.get()) {
-        TrustEngine* trust = XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(t2.get(),sub);
-        if (!(m_trust = dynamic_cast<SignatureTrustEngine*>(trust))) {
-            delete trust;
-            throw MetadataFilterException("TrustEngine-based SignatureMetadataFilter requires a SignatureTrustEngine plugin.");
+    sub = XMLHelper::getFirstChildElement(e, _TrustEngine);
+    if (sub) {
+        string t = XMLHelper::getAttrString(sub, nullptr, type);
+        if (!t.empty()) {
+            TrustEngine* trust = XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(t.c_str(), sub);
+            if (!(m_trust = dynamic_cast<SignatureTrustEngine*>(trust))) {
+                delete trust;
+                throw MetadataFilterException("TrustEngine-based SignatureMetadataFilter requires a SignatureTrustEngine plugin.");
+            }
+            return;
         }
-        return;
     }
 
     throw MetadataFilterException("SignatureMetadataFilter configuration requires <CredentialResolver> or <TrustEngine> element.");
@@ -351,7 +362,7 @@ void SignatureMetadataFilter::doFilter(EntityDescriptor& entity, bool rootObject
         catch (exception& e) {
             auto_ptr_char id(entity.getEntityID());
             m_log.warn("filtering out affiliation from entity (%s) after failed signature check: %s", id.get(), e.what());
-            entity.setAffiliationDescriptor(NULL);
+            entity.setAffiliationDescriptor(nullptr);
         }
     }
 }
@@ -367,12 +378,12 @@ void SignatureMetadataFilter::verifySignature(Signature* sig, const XMLCh* peerN
     CredentialCriteria cc;
     cc.setUsage(Credential::SIGNING_CREDENTIAL);
     cc.setSignature(*sig, CredentialCriteria::KEYINFO_EXTRACTION_KEY);
-    if (peerName) {
-        auto_ptr_char pname(peerName);
-        cc.setPeerName(pname.get());
-    }
 
     if (m_credResolver) {
+        if (peerName) {
+            auto_ptr_char pname(peerName);
+            cc.setPeerName(pname.get());
+        }
         Locker locker(m_credResolver);
         vector<const Credential*> creds;
         if (m_credResolver->resolve(creds,&cc)) {
@@ -386,13 +397,17 @@ void SignatureMetadataFilter::verifySignature(Signature* sig, const XMLCh* peerN
                 catch (exception&) {
                 }
             }
-            throw MetadataFilterException("CredentialResolver did not supply a successful verification key.");
+            throw MetadataFilterException("Unable to verify signature with supplied key(s).");
         }
         else {
-            throw MetadataFilterException("CredentialResolver did not supply a successful verification key.");
+            throw MetadataFilterException("CredentialResolver did not supply any candidate keys.");
         }
     }
     else if (m_trust) {
+        if (m_verifyName && peerName) {
+            auto_ptr_char pname(peerName);
+            cc.setPeerName(pname.get());
+        }
         DummyCredentialResolver dummy;
         if (m_trust->validate(*sig, dummy, &cc))
             return;