Major revamp of credential and trust handling code, PKIX engine still needs work.
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / SignatureMetadataFilter.cpp
1 /*
2  *  Copyright 2001-2007 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  * SignatureMetadataFilter.cpp
19  * 
20  * Filters out unsigned or mis-signed elements.
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/security/Credential.h>
31 #include <xmltooling/security/CredentialCriteria.h>
32 #include <xmltooling/security/CredentialResolver.h>
33 #include <xmltooling/signature/SignatureValidator.h>
34 #include <xmltooling/util/NDC.h>
35
36 using namespace opensaml::saml2md;
37 using namespace opensaml;
38 using namespace xmlsignature;
39 using namespace xmltooling;
40 using namespace log4cpp;
41 using namespace std;
42
43 namespace opensaml {
44     namespace saml2md {
45                 
46         class SAML_DLLLOCAL SignatureMetadataFilter : public MetadataFilter
47         {
48         public:
49             SignatureMetadataFilter(const DOMElement* e);
50             ~SignatureMetadataFilter() {
51                 delete m_credResolver;
52             }
53             
54             const char* getId() const { return SIGNATURE_METADATA_FILTER; }
55             void doFilter(XMLObject& xmlObject) const;
56
57         private:
58             void doFilter(EntitiesDescriptor& entities, bool rootObject=false) const;
59             void verifySignature(Signature* sig) const {
60                 if (sig) {
61                     m_profileValidator.validate(sig);
62                     m_sigValidator.validate(sig);
63                 }
64             }
65             
66             CredentialResolver* m_credResolver;
67             SignatureProfileValidator m_profileValidator;
68             mutable SignatureValidator m_sigValidator;
69         }; 
70
71         MetadataFilter* SAML_DLLLOCAL SignatureMetadataFilterFactory(const DOMElement* const & e)
72         {
73             return new SignatureMetadataFilter(e);
74         }
75
76     };
77 };
78
79 static const XMLCh _CredentialResolver[] =  UNICODE_LITERAL_18(C,r,e,d,e,n,t,i,a,l,R,e,s,o,l,v,e,r);
80 static const XMLCh type[] =                 UNICODE_LITERAL_4(t,y,p,e);
81
82 SignatureMetadataFilter::SignatureMetadataFilter(const DOMElement* e) : m_credResolver(NULL)
83 {
84     e = XMLHelper::getFirstChildElement(e, _CredentialResolver);
85     auto_ptr_char t(e ? e->getAttributeNS(NULL,type) : NULL);
86     if (t.get()) {
87         m_credResolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(t.get(),e);
88     }
89     else
90         throw MetadataFilterException("Missing <CredentialResolver> element, or no type attribute found");
91 }
92
93 void SignatureMetadataFilter::doFilter(XMLObject& xmlObject) const
94 {
95 #ifdef _DEBUG
96     NDC ndc("doFilter");
97 #endif
98     
99     CredentialCriteria cc;
100     cc.setUsage(CredentialCriteria::SIGNING_CREDENTIAL);
101     Locker locker(m_credResolver);
102     m_sigValidator.setCredential(m_credResolver->resolve(&cc));
103
104     try {
105         EntitiesDescriptor& entities = dynamic_cast<EntitiesDescriptor&>(xmlObject);
106         doFilter(entities, true);
107         return;
108     }
109     catch (bad_cast) {
110     }
111
112     try {
113         EntityDescriptor& entity = dynamic_cast<EntityDescriptor&>(xmlObject);
114         if (!entity.getSignature())
115             throw MetadataFilterException("Root metadata element was unsigned.");
116         verifySignature(entity.getSignature());
117     }
118     catch (bad_cast) {
119     }
120      
121     throw MetadataFilterException("SignatureMetadataFilter was given an improper metadata instance to filter.");
122 }
123
124 void SignatureMetadataFilter::doFilter(EntitiesDescriptor& entities, bool rootObject) const
125 {
126     Category& log=Category::getInstance(SAML_LOGCAT".Metadata");
127     
128     Signature* sig = entities.getSignature();
129     if (!sig && rootObject)
130         throw MetadataFilterException("Root metadata element was unsigned.");
131     verifySignature(sig);
132     
133     VectorOf(EntityDescriptor) v=entities.getEntityDescriptors();
134     for (VectorOf(EntityDescriptor)::size_type i=0; i<v.size(); ) {
135         try {
136             verifySignature(v[i]->getSignature());
137             i++;
138         }
139         catch (XMLToolingException& e) {
140             auto_ptr_char id(v[i]->getEntityID());
141             log.info("filtering out entity (%s) after failed signature check: ", id.get(), e.what());
142             v.erase(v.begin() + i);
143         }
144     }
145     
146     VectorOf(EntitiesDescriptor) w=entities.getEntitiesDescriptors();
147     for (VectorOf(EntitiesDescriptor)::size_type j=0; j<w.size(); ) {
148         try {
149             verifySignature(w[j]->getSignature());
150             j++;
151         }
152         catch (XMLToolingException& e) {
153             auto_ptr_char name(w[j]->getName());
154             log.info("filtering out group (%s) after failed signature check: ", name.get(), e.what());
155             w.erase(w.begin() + j);
156         }
157     }
158 }