2 * Copyright 2001-2006 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * XML Signature checking SecurityPolicyRule
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 "security/TrustEngine.h"
33 #include <xmltooling/util/NDC.h>
34 #include <xmltooling/util/ReplayCache.h>
35 #include <log4cpp/Category.hh>
37 using namespace opensaml::saml2md;
38 using namespace opensaml;
39 using namespace xmltooling;
40 using namespace log4cpp;
44 SecurityPolicyRule* SAML_DLLLOCAL XMLSigningRuleFactory(const DOMElement* const & e)
46 return new XMLSigningRule(e);
50 pair<saml2::Issuer*,const RoleDescriptor*> XMLSigningRule::evaluate(
51 const GenericRequest& request,
52 const XMLObject& message,
53 const MetadataProvider* metadataProvider,
55 const opensaml::TrustEngine* trustEngine
58 Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.XMLSigning");
59 log.debug("evaluating message signing policy");
61 pair<saml2::Issuer*,const RoleDescriptor*> ret = pair<saml2::Issuer*,const RoleDescriptor*>(NULL,NULL);
63 if (!metadataProvider || !role || !trustEngine) {
64 log.debug("ignoring message, no metadata or trust information supplied");
69 const SignableObject* signable = dynamic_cast<const SignableObject*>(&message);
70 if (!signable || !signable->getSignature()) {
71 log.debug("ignoring unsigned or unrecognized message");
75 log.debug("extracting issuer from message");
76 pair<saml2::Issuer*,const XMLCh*> issuerInfo = getIssuerAndProtocol(message);
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");
85 log.debug("searching metadata for message issuer...");
86 const EntityDescriptor* entity = metadataProvider->getEntityDescriptor(issuer->getName());
88 auto_ptr_char temp(issuer->getName());
89 log.warn("no metadata found, can't establish identity of issuer (%s)", temp.get());
93 log.debug("matched message issuer against metadata, searching for applicable role...");
94 const RoleDescriptor* roledesc=entity->getRoleDescriptor(*role, issuerInfo.second);
96 log.warn("unable to find compatible role (%s) in metadata", role->toString().c_str());
100 if (!trustEngine->validate(*(signable->getSignature()), *roledesc, metadataProvider->getKeyResolver())) {
101 log.error("unable to verify signature on message with supplied trust engine");
105 if (log.isDebugEnabled()) {
106 auto_ptr_char iname(entity->getEntityID());
107 log.debug("message from (%s), signature verified", iname.get());
110 ret.first = issuer.release();
111 ret.second = roledesc;
115 log.warn("caught a bad_cast while extracting issuer");
120 pair<saml2::Issuer*,const XMLCh*> XMLSigningRule::getIssuerAndProtocol(const XMLObject& message) const
122 // We just let any bad casts throw here.
124 saml2::Issuer* issuer;
126 // Shortcuts some of the casting.
127 const XMLCh* ns = message.getElementQName().getNamespaceURI();
129 if (XMLString::equals(ns, samlconstants::SAML20P_NS) || XMLString::equals(ns, samlconstants::SAML20_NS)) {
130 // 2.0 namespace should be castable to a specialized 2.0 root.
131 const saml2::RootObject& root = dynamic_cast<const saml2::RootObject&>(message);
132 issuer = root.getIssuer();
133 if (issuer && issuer->getName()) {
134 return make_pair(issuer->cloneIssuer(), samlconstants::SAML20P_NS);
137 // No issuer in the message, so we have to try the Response approach.
138 const vector<saml2::Assertion*>& assertions = dynamic_cast<const saml2p::Response&>(message).getAssertions();
139 if (!assertions.empty()) {
140 issuer = assertions.front()->getIssuer();
141 if (issuer && issuer->getName())
142 return make_pair(issuer->cloneIssuer(), samlconstants::SAML20P_NS);
145 else if (XMLString::equals(ns, samlconstants::SAML1P_NS)) {
146 // Should be a samlp:Response, at least in OpenSAML.
147 const vector<saml1::Assertion*>& assertions = dynamic_cast<const saml1p::Response&>(message).getAssertions();
148 if (!assertions.empty()) {
149 const saml1::Assertion* a = assertions.front();
150 if (a->getIssuer()) {
151 issuer = saml2::IssuerBuilder::buildIssuer();
152 issuer->setName(a->getIssuer());
153 pair<bool,int> minor = a->getMinorVersion();
156 (minor.first && minor.second==0) ? samlconstants::SAML10_PROTOCOL_ENUM : samlconstants::SAML11_PROTOCOL_ENUM
162 return pair<saml2::Issuer*,const XMLCh*>(NULL,NULL);