X-Git-Url: http://www.project-moonshot.org/gitweb/?p=shibboleth%2Fcpp-opensaml.git;a=blobdiff_plain;f=saml%2Fbinding%2Fimpl%2FMessageFlowRule.cpp;h=4ef7632f0991b8c1203b60763e303027029730b3;hp=0e8cea356a7ba10b17fea07e566e25f2e5398bcf;hb=c3cd4ec3fa87d0ad3c6f65c1a5e15f548b1b6cc2;hpb=1bc8e721db3a50294df852662e1eddcdbdae8f9f diff --git a/saml/binding/impl/MessageFlowRule.cpp b/saml/binding/impl/MessageFlowRule.cpp index 0e8cea3..4ef7632 100644 --- a/saml/binding/impl/MessageFlowRule.cpp +++ b/saml/binding/impl/MessageFlowRule.cpp @@ -1,6 +1,6 @@ /* - * Copyright 2001-2006 Internet2 - * + * Copyright 2001-2009 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 @@ -16,25 +16,40 @@ /** * MessageFlowRule.cpp - * + * * SAML replay and freshness checking SecurityPolicyRule */ #include "internal.h" #include "exceptions.h" -#include "RootObject.h" -#include "binding/MessageFlowRule.h" +#include "binding/SecurityPolicyRule.h" -#include +#include #include -#include +#include using namespace opensaml; +using namespace xmltooling::logging; using namespace xmltooling; -using namespace log4cpp; using namespace std; namespace opensaml { + class SAML_DLLLOCAL MessageFlowRule : public SecurityPolicyRule + { + public: + MessageFlowRule(const DOMElement* e); + virtual ~MessageFlowRule() {} + + const char* getType() const { + return MESSAGEFLOW_POLICY_RULE; + } + bool evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const; + + private: + bool m_checkReplay; + time_t m_expires; + }; + SecurityPolicyRule* SAML_DLLLOCAL MessageFlowRuleFactory(const DOMElement* const & e) { return new MessageFlowRule(e); @@ -57,51 +72,48 @@ MessageFlowRule::MessageFlowRule(const DOMElement* e) } } -pair MessageFlowRule::evaluate( - const GenericRequest& request, - const XMLObject& message, - const saml2md::MetadataProvider* metadataProvider, - const QName* role, - const opensaml::TrustEngine* trustEngine - ) const -{ - try { - const RootObject& obj = dynamic_cast(message); - check(obj.getID(), obj.getIssueInstantEpoch()); - } - catch (bad_cast&) { - throw BindingException("Message was not of a recognized SAML root type."); - } - return pair(NULL,NULL); -} - -void MessageFlowRule::check(const XMLCh* id, time_t issueInstant) const +bool MessageFlowRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const { Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.MessageFlow"); log.debug("evaluating message flow policy (replay checking %s, expiration %lu)", m_checkReplay ? "on" : "off", m_expires); - + + time_t now = policy.getTime(); time_t skew = XMLToolingConfig::getConfig().clock_skew_secs; - time_t now = time(NULL); - if (issueInstant > now + skew) { - log.error("rejected not-yet-valid message, timestamp (%lu), now (%lu)", issueInstant, now + skew); - throw BindingException("Message rejected, was issued in the future."); + time_t issueInstant = policy.getIssueInstant(); + if (issueInstant == 0) { + issueInstant = now; } - else if (issueInstant < now - skew - m_expires) { - log.error("rejected expired message, timestamp (%lu), oldest allowed (%lu)", issueInstant, now - skew - m_expires); - throw BindingException("Message expired, was issued too long ago."); + else { + if (issueInstant > now + skew) { + log.errorStream() << "rejected not-yet-valid message, timestamp (" << issueInstant << + "), newest allowed (" << now + skew << ")" << logging::eol; + throw SecurityPolicyException("Message rejected, was issued in the future."); + } + else if (issueInstant < now - skew - m_expires) { + log.errorStream() << "rejected expired message, timestamp (" << issueInstant << + "), oldest allowed (" << (now - skew - m_expires) << ")" << logging::eol; + throw SecurityPolicyException("Message expired, was issued too long ago."); + } } - + // Check replay. if (m_checkReplay) { + const XMLCh* id = policy.getMessageID(); + if (!id || !*id) + return false; + ReplayCache* replayCache = XMLToolingConfig::getConfig().getReplayCache(); - if (!replayCache) - throw BindingException("No ReplayCache instance available."); - else if (!id) - throw BindingException("Message did not contain an identifier."); - auto_ptr_char temp(id); - if (!replayCache->check("SAML", temp.get(), issueInstant + skew + m_expires)) { + if (!replayCache) { + log.warn("no ReplayCache available, skipping requested replay check"); + return false; + } + + auto_ptr_char temp(id); + if (!replayCache->check("MessageFlow", temp.get(), issueInstant + skew + m_expires)) { log.error("replay detected of message ID (%s)", temp.get()); - throw BindingException("Rejecting replayed message ID ($1).", params(1,temp.get())); + throw SecurityPolicyException("Rejecting replayed message ID ($1).", params(1,temp.get())); } + return true; } + return false; }