-/*
- * Copyright 2001-2006 Internet2
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
+ *
+ * UCAID licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the
+ * License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
*/
/**
* XMLSigningRule.cpp
*
- * XML Signature checking SecurityPolicyRule
+ * XML Signature checking SecurityPolicyRule.
*/
#include "internal.h"
#include "exceptions.h"
-#include "binding/XMLSigningRule.h"
-#include "saml1/core/Assertions.h"
-#include "saml1/core/Protocols.h"
-#include "saml2/core/Protocols.h"
+#include "binding/SecurityPolicy.h"
+#include "binding/SecurityPolicyRule.h"
+#include "saml2/core/Assertions.h"
#include "saml2/metadata/Metadata.h"
+#include "saml2/metadata/MetadataCredentialCriteria.h"
#include "saml2/metadata/MetadataProvider.h"
#include "signature/SignatureProfileValidator.h"
-#include <xmltooling/util/NDC.h>
-#include <xmltooling/util/ReplayCache.h>
-#include <log4cpp/Category.hh>
+#include <xmltooling/logging.h>
+#include <xmltooling/security/SignatureTrustEngine.h>
+#include <xmltooling/signature/Signature.h>
using namespace opensaml::saml2md;
using namespace opensaml;
+using namespace xmltooling::logging;
using namespace xmltooling;
-using namespace log4cpp;
using namespace std;
+using xmlsignature::SignatureException;
+
namespace opensaml {
+ class SAML_DLLLOCAL XMLSigningRule : public SecurityPolicyRule
+ {
+ public:
+ XMLSigningRule(const DOMElement* e);
+ virtual ~XMLSigningRule() {}
+
+ const char* getType() const {
+ return XMLSIGNING_POLICY_RULE;
+ }
+ bool evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
+
+ private:
+ bool m_errorFatal;
+ };
+
SecurityPolicyRule* SAML_DLLLOCAL XMLSigningRuleFactory(const DOMElement* const & e)
{
return new XMLSigningRule(e);
}
+
+ static const XMLCh errorFatal[] = UNICODE_LITERAL_10(e,r,r,o,r,F,a,t,a,l);
};
-pair<saml2::Issuer*,const RoleDescriptor*> XMLSigningRule::evaluate(
- const GenericRequest& request,
- const XMLObject& message,
- const MetadataProvider* metadataProvider,
- const QName* role,
- const TrustEngine* trustEngine
- ) const
+XMLSigningRule::XMLSigningRule(const DOMElement* e) : m_errorFatal(XMLHelper::getAttrBool(e, false, errorFatal))
{
- Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.XMLSigning");
- log.debug("evaluating message signing policy");
-
- pair<saml2::Issuer*,const RoleDescriptor*> ret = pair<saml2::Issuer*,const RoleDescriptor*>(NULL,NULL);
+}
+
+bool XMLSigningRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
+{
+ Category& log=Category::getInstance(SAML_LOGCAT ".SecurityPolicyRule.XMLSigning");
- if (!metadataProvider || !role || !trustEngine) {
- log.debug("ignoring message, no metadata or trust information supplied");
- return ret;
+ if (!policy.getIssuerMetadata()) {
+ log.debug("ignoring message, no issuer metadata supplied");
+ return false;
+ }
+
+ const SignatureTrustEngine* sigtrust;
+ if (!(sigtrust=dynamic_cast<const SignatureTrustEngine*>(policy.getTrustEngine()))) {
+ log.debug("ignoring message, no SignatureTrustEngine supplied");
+ return false;
}
+ const SignableObject* signable = dynamic_cast<const SignableObject*>(&message);
+ if (!signable || !signable->getSignature())
+ return false;
+
+ log.debug("validating signature profile");
try {
- const SignableObject* signable = dynamic_cast<const SignableObject*>(&message);
- if (!signable || !signable->getSignature()) {
- log.debug("ignoring unsigned or unrecognized message");
- return ret;
- }
-
- log.debug("validating signature profile");
- try {
- SignatureProfileValidator sigval;
- sigval.validate(signable->getSignature());
- }
- catch (ValidationException& ve) {
- log.error("signature profile failed to validate: %s", ve.what());
- return ret;
- }
-
-
- log.debug("extracting issuer from message");
- pair<saml2::Issuer*,const XMLCh*> issuerInfo = getIssuerAndProtocol(message);
-
- auto_ptr<saml2::Issuer> issuer(issuerInfo.first);
- if (!issuerInfo.first || !issuerInfo.second ||
- (issuer->getFormat() && !XMLString::equals(issuer->getFormat(), saml2::NameIDType::ENTITY))) {
- log.warn("issuer identity not estabished, or was not an entityID");
- return ret;
- }
-
- log.debug("searching metadata for message issuer...");
- const EntityDescriptor* entity = metadataProvider->getEntityDescriptor(issuer->getName());
- if (!entity) {
- auto_ptr_char temp(issuer->getName());
- log.warn("no metadata found, can't establish identity of issuer (%s)", temp.get());
- return ret;
- }
-
- log.debug("matched message issuer against metadata, searching for applicable role...");
- const RoleDescriptor* roledesc=entity->getRoleDescriptor(*role, issuerInfo.second);
- if (!roledesc) {
- log.warn("unable to find compatible role (%s) in metadata", role->toString().c_str());
- return ret;
- }
-
- if (!trustEngine->validate(*(signable->getSignature()), *roledesc, metadataProvider->getKeyResolver())) {
- log.error("unable to verify signature on message with supplied trust engine");
- return ret;
- }
-
- if (log.isDebugEnabled()) {
- auto_ptr_char iname(entity->getEntityID());
- log.debug("message from (%s), signature verified", iname.get());
- }
-
- ret.first = issuer.release();
- ret.second = roledesc;
+ SignatureProfileValidator sigval;
+ sigval.validateSignature(*(signable->getSignature()));
}
- catch (bad_cast&) {
- // Just trap it.
- log.warn("caught a bad_cast while extracting issuer");
+ catch (ValidationException& ve) {
+ log.error("signature profile failed to validate: %s", ve.what());
+ if (m_errorFatal)
+ throw;
+ return false;
}
- return ret;
-}
-
-pair<saml2::Issuer*,const XMLCh*> XMLSigningRule::getIssuerAndProtocol(const XMLObject& message) const
-{
- // We just let any bad casts throw here.
- saml2::Issuer* issuer;
+ // Set up criteria object.
+ MetadataCredentialCriteria cc(*(policy.getIssuerMetadata()));
- // Shortcuts some of the casting.
- const XMLCh* ns = message.getElementQName().getNamespaceURI();
- if (ns) {
- if (XMLString::equals(ns, samlconstants::SAML20P_NS) || XMLString::equals(ns, samlconstants::SAML20_NS)) {
- // 2.0 namespace should be castable to a specialized 2.0 root.
- const saml2::RootObject& root = dynamic_cast<const saml2::RootObject&>(message);
- issuer = root.getIssuer();
- if (issuer && issuer->getName()) {
- return make_pair(issuer->cloneIssuer(), samlconstants::SAML20P_NS);
- }
-
- // No issuer in the message, so we have to try the Response approach.
- const vector<saml2::Assertion*>& assertions = dynamic_cast<const saml2p::Response&>(message).getAssertions();
- if (!assertions.empty()) {
- issuer = assertions.front()->getIssuer();
- if (issuer && issuer->getName())
- return make_pair(issuer->cloneIssuer(), samlconstants::SAML20P_NS);
- }
- }
- else if (XMLString::equals(ns, samlconstants::SAML1P_NS)) {
- // Should be a samlp:Response, at least in OpenSAML.
- const vector<saml1::Assertion*>& assertions = dynamic_cast<const saml1p::Response&>(message).getAssertions();
- if (!assertions.empty()) {
- const saml1::Assertion* a = assertions.front();
- if (a->getIssuer()) {
- issuer = saml2::IssuerBuilder::buildIssuer();
- issuer->setName(a->getIssuer());
- pair<bool,int> minor = a->getMinorVersion();
- return make_pair(
- issuer,
- (minor.first && minor.second==0) ? samlconstants::SAML10_PROTOCOL_ENUM : samlconstants::SAML11_PROTOCOL_ENUM
- );
- }
- }
- }
+ if (!sigtrust->validate(*(signable->getSignature()), *(policy.getMetadataProvider()), &cc)) {
+ log.error("unable to verify message signature with supplied trust engine");
+ if (m_errorFatal)
+ throw SecurityPolicyException("Message was signed, but signature could not be verified.");
+ return false;
}
- return pair<saml2::Issuer*,const XMLCh*>(NULL,NULL);
+
+ log.debug("signature verified against message issuer");
+ policy.setAuthenticated(true);
+ return true;
}