552cef88a12e453febfac70e7f4c908361a9f467
[shibboleth/cpp-opensaml.git] / saml / binding / impl / XMLSigningRule.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  * XMLSigningRule.cpp
19  * 
20  * XML Signature checking SecurityPolicyRule
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "binding/XMLSigningRule.h"
26 #include "saml2/core/Assertions.h"
27 #include "saml2/metadata/Metadata.h"
28 #include "saml2/metadata/MetadataProvider.h"
29 #include "security/TrustEngine.h"
30
31 #include <xmltooling/util/NDC.h>
32 #include <xmltooling/util/ReplayCache.h>
33 #include <log4cpp/Category.hh>
34
35 using namespace opensaml::saml2md;
36 using namespace opensaml;
37 using namespace xmltooling;
38 using namespace log4cpp;
39 using namespace std;
40
41 namespace opensaml {
42     SecurityPolicyRule* SAML_DLLLOCAL XMLSigningRuleFactory(const DOMElement* const & e)
43     {
44         return new XMLSigningRule(e);
45     }
46 };
47
48 pair<saml2::Issuer*,const RoleDescriptor*> XMLSigningRule::evaluate(
49     const GenericRequest& request,
50     const XMLObject& message,
51     const MetadataProvider* metadataProvider,
52     const QName* role,
53     const opensaml::TrustEngine* trustEngine,
54     const MessageExtractor& extractor
55     ) const
56 {
57     Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.XMLSigning");
58     log.debug("evaluating message signing policy");
59     
60     pair<saml2::Issuer*,const RoleDescriptor*> ret = pair<saml2::Issuer*,const RoleDescriptor*>(NULL,NULL);  
61     
62     if (!metadataProvider || !role || !trustEngine) {
63         log.debug("ignoring message, no metadata or trust information supplied");
64         return ret;
65     }
66     
67     try {
68         const RootObject& root = dynamic_cast<const RootObject&>(message);
69         if (!root.getSignature()) {
70             log.debug("ignoring unsigned message");
71             return ret;
72         }
73         
74         log.debug("extracting issuer from message");
75         pair<saml2::Issuer*,const XMLCh*> issuerInfo = extractor.getIssuerAndProtocol(message);
76         
77         auto_ptr<saml2::Issuer> issuer(issuerInfo.first);
78         if (!issuerInfo.first || !issuerInfo.second ||
79                 (issuer->getFormat() && !XMLString::equals(issuer->getFormat(), saml2::NameIDType::ENTITY))) {
80             log.warn("issuer identity not estabished, or was not an entityID");
81             return ret;
82         }
83         
84         log.debug("searching metadata for message issuer...");
85         const EntityDescriptor* entity = metadataProvider->getEntityDescriptor(issuer->getName());
86         if (!entity) {
87             auto_ptr_char temp(issuer->getName());
88             log.warn("no metadata found, can't establish identity of issuer (%s)", temp.get());
89             return ret;
90         }
91
92         log.debug("matched message issuer against metadata, searching for applicable role...");
93         const RoleDescriptor* roledesc=entity->getRoleDescriptor(*role, issuerInfo.second);
94         if (!roledesc) {
95             log.warn("unable to find compatible role (%s) in metadata", role->toString().c_str());
96             return ret;
97         }
98
99         if (!trustEngine->validate(*(root.getSignature()), *roledesc, metadataProvider->getKeyResolver())) {
100             log.error("unable to verify signature on message with supplied trust engine");
101             return ret;
102         }
103
104         if (log.isDebugEnabled()) {
105             auto_ptr_char iname(entity->getEntityID());
106             log.debug("message from (%s), signature verified", iname.get());
107         }
108         
109         ret.first = issuer.release();
110         ret.second = roledesc;
111     }
112     catch (bad_cast&) {
113         // Just trap it.
114         log.warn("caught a bad_cast while extracting issuer");
115     }
116     return ret;
117 }