Merged trust engines back into a unified version, made metadata roles a "KeyInfoSource".
[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 "saml1/core/Assertions.h"
27 #include "saml1/core/Protocols.h"
28 #include "saml2/core/Protocols.h"
29 #include "saml2/metadata/Metadata.h"
30 #include "saml2/metadata/MetadataProvider.h"
31 #include "signature/SignatureProfileValidator.h"
32
33 #include <xmltooling/util/NDC.h>
34 #include <xmltooling/util/ReplayCache.h>
35 #include <log4cpp/Category.hh>
36
37 using namespace opensaml::saml2md;
38 using namespace opensaml;
39 using namespace xmltooling;
40 using namespace log4cpp;
41 using namespace std;
42
43 namespace opensaml {
44     SecurityPolicyRule* SAML_DLLLOCAL XMLSigningRuleFactory(const DOMElement* const & e)
45     {
46         return new XMLSigningRule(e);
47     }
48 };
49
50 pair<saml2::Issuer*,const RoleDescriptor*> XMLSigningRule::evaluate(
51     const GenericRequest& request,
52     const XMLObject& message,
53     const MetadataProvider* metadataProvider,
54     const QName* role,
55     const TrustEngine* trustEngine
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 SignableObject* signable = dynamic_cast<const SignableObject*>(&message);
70         if (!signable || !signable->getSignature()) {
71             log.debug("ignoring unsigned or unrecognized message");
72             return ret;
73         }
74         
75         log.debug("validating signature profile");
76         try {
77             SignatureProfileValidator sigval;
78             sigval.validate(signable->getSignature());
79         }
80         catch (ValidationException& ve) {
81             log.error("signature profile failed to validate: %s", ve.what());
82             return ret;
83         }
84         
85         
86         log.debug("extracting issuer from message");
87         pair<saml2::Issuer*,const XMLCh*> issuerInfo = getIssuerAndProtocol(message);
88         
89         auto_ptr<saml2::Issuer> issuer(issuerInfo.first);
90         if (!issuerInfo.first || !issuerInfo.second ||
91                 (issuer->getFormat() && !XMLString::equals(issuer->getFormat(), saml2::NameIDType::ENTITY))) {
92             log.warn("issuer identity not estabished, or was not an entityID");
93             return ret;
94         }
95         
96         log.debug("searching metadata for message issuer...");
97         const EntityDescriptor* entity = metadataProvider->getEntityDescriptor(issuer->getName());
98         if (!entity) {
99             auto_ptr_char temp(issuer->getName());
100             log.warn("no metadata found, can't establish identity of issuer (%s)", temp.get());
101             return ret;
102         }
103
104         log.debug("matched message issuer against metadata, searching for applicable role...");
105         const RoleDescriptor* roledesc=entity->getRoleDescriptor(*role, issuerInfo.second);
106         if (!roledesc) {
107             log.warn("unable to find compatible role (%s) in metadata", role->toString().c_str());
108             return ret;
109         }
110
111         if (!trustEngine->validate(*(signable->getSignature()), *roledesc, metadataProvider->getKeyResolver())) {
112             log.error("unable to verify signature on message with supplied trust engine");
113             return ret;
114         }
115
116         if (log.isDebugEnabled()) {
117             auto_ptr_char iname(entity->getEntityID());
118             log.debug("message from (%s), signature verified", iname.get());
119         }
120         
121         ret.first = issuer.release();
122         ret.second = roledesc;
123     }
124     catch (bad_cast&) {
125         // Just trap it.
126         log.warn("caught a bad_cast while extracting issuer");
127     }
128     return ret;
129 }
130
131 pair<saml2::Issuer*,const XMLCh*> XMLSigningRule::getIssuerAndProtocol(const XMLObject& message) const
132 {
133     // We just let any bad casts throw here.
134     
135     saml2::Issuer* issuer;
136
137     // Shortcuts some of the casting.
138     const XMLCh* ns = message.getElementQName().getNamespaceURI();
139     if (ns) {
140         if (XMLString::equals(ns, samlconstants::SAML20P_NS) || XMLString::equals(ns, samlconstants::SAML20_NS)) {
141             // 2.0 namespace should be castable to a specialized 2.0 root.
142             const saml2::RootObject& root = dynamic_cast<const saml2::RootObject&>(message);
143             issuer = root.getIssuer();
144             if (issuer && issuer->getName()) {
145                 return make_pair(issuer->cloneIssuer(), samlconstants::SAML20P_NS);
146             }
147             
148             // No issuer in the message, so we have to try the Response approach. 
149             const vector<saml2::Assertion*>& assertions = dynamic_cast<const saml2p::Response&>(message).getAssertions();
150             if (!assertions.empty()) {
151                 issuer = assertions.front()->getIssuer();
152                 if (issuer && issuer->getName())
153                     return make_pair(issuer->cloneIssuer(), samlconstants::SAML20P_NS);
154             }
155         }
156         else if (XMLString::equals(ns, samlconstants::SAML1P_NS)) {
157             // Should be a samlp:Response, at least in OpenSAML.
158             const vector<saml1::Assertion*>& assertions = dynamic_cast<const saml1p::Response&>(message).getAssertions();
159             if (!assertions.empty()) {
160                 const saml1::Assertion* a = assertions.front();
161                 if (a->getIssuer()) {
162                     issuer = saml2::IssuerBuilder::buildIssuer();
163                     issuer->setName(a->getIssuer());
164                     pair<bool,int> minor = a->getMinorVersion();
165                     return make_pair(
166                         issuer,
167                         (minor.first && minor.second==0) ? samlconstants::SAML10_PROTOCOL_ENUM : samlconstants::SAML11_PROTOCOL_ENUM
168                         );
169                 }
170             }
171         }
172     }
173     return pair<saml2::Issuer*,const XMLCh*>(NULL,NULL);
174 }