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