Migrate to xmlsec 1.3 release, shrink a few headers.
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / SignatureMetadataFilter.cpp
1 /*
2  *  Copyright 2001-2006 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * BlacklistMetadataFilter.cpp
19  * 
20  * Removes blacklisted entities from a metadata instance
21  */
22
23 #include "internal.h"
24 #include "saml2/metadata/Metadata.h"
25 #include "saml2/metadata/MetadataFilter.h"
26 #include "signature/SignatureProfileValidator.h"
27
28 #include <log4cpp/Category.hh>
29
30 #include <xmltooling/util/NDC.h>
31 #include <xmltooling/signature/SignatureValidator.h>
32
33 using namespace opensaml::saml2md;
34 using namespace opensaml;
35 using namespace xmlsignature;
36 using namespace xmltooling;
37 using namespace log4cpp;
38 using namespace std;
39
40 namespace opensaml {
41     namespace saml2md {
42                 
43         class SAML_DLLLOCAL SignatureMetadataFilter : public MetadataFilter
44         {
45         public:
46             SignatureMetadataFilter(const DOMElement* e);
47             ~SignatureMetadataFilter() {
48                 delete m_sigValidator;
49             }
50             
51             const char* getId() const { return SIGNATURE_METADATA_FILTER; }
52             void doFilter(XMLObject& xmlObject) const;
53
54         private:
55             void doFilter(EntitiesDescriptor& entities, bool rootObject=false) const;
56             void verifySignature(Signature* sig) const {
57                 if (sig) {
58                     m_profileValidator.validate(sig);
59                     m_sigValidator->validate(sig);
60                 }
61             }
62             
63             SignatureProfileValidator m_profileValidator;
64             SignatureValidator* m_sigValidator;
65         }; 
66
67         MetadataFilter* SAML_DLLLOCAL SignatureMetadataFilterFactory(const DOMElement* const & e)
68         {
69             return new SignatureMetadataFilter(e);
70         }
71
72     };
73 };
74
75 static const XMLCh GenericKeyResolver[] =   UNICODE_LITERAL_11(K,e,y,R,e,s,o,l,v,e,r);
76 static const XMLCh type[] =                 UNICODE_LITERAL_4(t,y,p,e);
77
78 SignatureMetadataFilter::SignatureMetadataFilter(const DOMElement* e) : m_sigValidator(NULL)
79 {
80     e = XMLHelper::getFirstChildElement(e, GenericKeyResolver);
81     auto_ptr_char t(e ? e->getAttributeNS(NULL,type) : NULL);
82     if (t.get()) {
83         auto_ptr<KeyResolver> kr(XMLToolingConfig::getConfig().KeyResolverManager.newPlugin(t.get(),e));
84         m_sigValidator = new SignatureValidator(kr.get());
85         kr.release();
86     }
87     else
88         throw MetadataFilterException("missing <KeyResolver> element, or no type attribute found");
89 }
90
91 void SignatureMetadataFilter::doFilter(XMLObject& xmlObject) const
92 {
93 #ifdef _DEBUG
94     NDC ndc("doFilter");
95 #endif
96     
97     try {
98         EntitiesDescriptor& entities = dynamic_cast<EntitiesDescriptor&>(xmlObject);
99         doFilter(entities, true);
100         return;
101     }
102     catch (bad_cast) {
103     }
104
105     try {
106         EntityDescriptor& entity = dynamic_cast<EntityDescriptor&>(xmlObject);
107         if (!entity.getSignature())
108             throw MetadataFilterException("Root metadata element was unsigned.");
109         verifySignature(entity.getSignature());
110     }
111     catch (bad_cast) {
112     }
113      
114     throw MetadataFilterException("SignatureMetadataFilter was given an improper metadata instance to filter.");
115 }
116
117 void SignatureMetadataFilter::doFilter(EntitiesDescriptor& entities, bool rootObject) const
118 {
119     Category& log=Category::getInstance(SAML_LOGCAT".Metadata");
120     
121     Signature* sig = entities.getSignature();
122     if (!sig && rootObject)
123         throw MetadataFilterException("Root metadata element was unsigned.");
124     verifySignature(sig);
125     
126     VectorOf(EntityDescriptor) v=entities.getEntityDescriptors();
127     for (VectorOf(EntityDescriptor)::size_type i=0; i<v.size(); ) {
128         try {
129             verifySignature(v[i]->getSignature());
130             i++;
131         }
132         catch (XMLToolingException& e) {
133             auto_ptr_char id(v[i]->getEntityID());
134             log.info("filtering out entity (%s) after failed signature check: ", id.get(), e.what());
135             v.erase(v.begin() + i);
136         }
137     }
138     
139     VectorOf(EntitiesDescriptor) w=entities.getEntitiesDescriptors();
140     for (VectorOf(EntitiesDescriptor)::size_type j=0; j<w.size(); ) {
141         try {
142             verifySignature(w[j]->getSignature());
143             j++;
144         }
145         catch (XMLToolingException& e) {
146             auto_ptr_char name(w[j]->getName());
147             log.info("filtering out group (%s) after failed signature check: ", name.get(), e.what());
148             w.erase(w.begin() + j);
149         }
150     }
151 }