Allow simplified syntax to specify a signing certificate.
[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 static const XMLCh certificate[] =          UNICODE_LITERAL_11(c,e,r,t,i,f,i,c,a,t,e);
82 static const XMLCh Certificate[] =          UNICODE_LITERAL_11(C,e,r,t,i,f,i,c,a,t,e);
83 static const XMLCh Path[] =                 UNICODE_LITERAL_4(P,a,t,h);
84
85 SignatureMetadataFilter::SignatureMetadataFilter(const DOMElement* e) : m_credResolver(NULL)
86 {
87     if (e && e->hasAttributeNS(NULL,certificate)) {
88         // Dummy up a file resolver.
89         DOMElement* dummy = e->getOwnerDocument()->createElementNS(NULL,_CredentialResolver);
90         DOMElement* child = e->getOwnerDocument()->createElementNS(NULL,Certificate);
91         dummy->appendChild(child);
92         DOMElement* path = e->getOwnerDocument()->createElementNS(NULL,Path);
93         child->appendChild(path);
94         path->appendChild(e->getOwnerDocument()->createTextNode(e->getAttributeNS(NULL,certificate)));
95         m_credResolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(FILESYSTEM_CREDENTIAL_RESOLVER,dummy);
96         return;
97     }
98
99     e = e ? XMLHelper::getFirstChildElement(e, _CredentialResolver) : NULL;
100     auto_ptr_char t(e ? e->getAttributeNS(NULL,type) : NULL);
101     if (t.get()) {
102         m_credResolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(t.get(),e);
103     }
104     else
105         throw MetadataFilterException("Missing <CredentialResolver> element, or no type attribute found");
106 }
107
108 void SignatureMetadataFilter::doFilter(XMLObject& xmlObject) const
109 {
110 #ifdef _DEBUG
111     NDC ndc("doFilter");
112 #endif
113     
114     CredentialCriteria cc;
115     cc.setUsage(CredentialCriteria::SIGNING_CREDENTIAL);
116     Locker locker(m_credResolver);
117     m_sigValidator.setCredential(m_credResolver->resolve(&cc));
118
119     try {
120         EntitiesDescriptor& entities = dynamic_cast<EntitiesDescriptor&>(xmlObject);
121         doFilter(entities, true);
122         return;
123     }
124     catch (bad_cast) {
125     }
126
127     try {
128         EntityDescriptor& entity = dynamic_cast<EntityDescriptor&>(xmlObject);
129         if (!entity.getSignature())
130             throw MetadataFilterException("Root metadata element was unsigned.");
131         verifySignature(entity.getSignature());
132     }
133     catch (bad_cast) {
134     }
135      
136     throw MetadataFilterException("SignatureMetadataFilter was given an improper metadata instance to filter.");
137 }
138
139 void SignatureMetadataFilter::doFilter(EntitiesDescriptor& entities, bool rootObject) const
140 {
141     Category& log=Category::getInstance(SAML_LOGCAT".Metadata");
142     
143     Signature* sig = entities.getSignature();
144     if (!sig && rootObject)
145         throw MetadataFilterException("Root metadata element was unsigned.");
146     verifySignature(sig);
147     
148     VectorOf(EntityDescriptor) v=entities.getEntityDescriptors();
149     for (VectorOf(EntityDescriptor)::size_type i=0; i<v.size(); ) {
150         try {
151             verifySignature(v[i]->getSignature());
152             i++;
153         }
154         catch (XMLToolingException& e) {
155             auto_ptr_char id(v[i]->getEntityID());
156             log.info("filtering out entity (%s) after failed signature check: ", id.get(), e.what());
157             v.erase(v.begin() + i);
158         }
159     }
160     
161     VectorOf(EntitiesDescriptor) w=entities.getEntitiesDescriptors();
162     for (VectorOf(EntitiesDescriptor)::size_type j=0; j<w.size(); ) {
163         try {
164             verifySignature(w[j]->getSignature());
165             j++;
166         }
167         catch (XMLToolingException& e) {
168             auto_ptr_char name(w[j]->getName());
169             log.info("filtering out group (%s) after failed signature check: ", name.get(), e.what());
170             w.erase(w.begin() + j);
171         }
172     }
173 }