123921a8f4fbb5d45c846827c0c16942d36be7af
[shibboleth/cpp-opensaml.git] / saml / binding / impl / MessageFlowRule.cpp
1 /*
2  *  Copyright 2001-2010 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  * MessageFlowRule.cpp
19  *
20  * SAML replay and freshness checking SecurityPolicyRule.
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "binding/SecurityPolicy.h"
26 #include "binding/SecurityPolicyRule.h"
27
28 #include <xmltooling/logging.h>
29 #include <xmltooling/XMLToolingConfig.h>
30 #include <xmltooling/util/ReplayCache.h>
31 #include <xmltooling/util/XMLHelper.h>
32 #include <xercesc/util/XMLUniDefs.hpp>
33
34 using namespace opensaml;
35 using namespace xmltooling::logging;
36 using namespace xmltooling;
37 using namespace std;
38
39 namespace opensaml {
40     class SAML_DLLLOCAL MessageFlowRule : public SecurityPolicyRule
41     {
42     public:
43         MessageFlowRule(const DOMElement* e);
44         virtual ~MessageFlowRule() {}
45
46         const char* getType() const {
47             return MESSAGEFLOW_POLICY_RULE;
48         }
49         bool evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
50
51     private:
52         bool m_checkReplay;
53         time_t m_expires;
54     };
55
56     SecurityPolicyRule* SAML_DLLLOCAL MessageFlowRuleFactory(const DOMElement* const & e)
57     {
58         return new MessageFlowRule(e);
59     }
60 };
61
62 static const XMLCh checkReplay[] = UNICODE_LITERAL_11(c,h,e,c,k,R,e,p,l,a,y);
63 static const XMLCh expires[] = UNICODE_LITERAL_7(e,x,p,i,r,e,s);
64
65 MessageFlowRule::MessageFlowRule(const DOMElement* e)
66     : m_checkReplay(XMLHelper::getAttrBool(e, true, checkReplay)),
67         m_expires(XMLHelper::getAttrInt(e, XMLToolingConfig::getConfig().clock_skew_secs, expires))
68 {
69 }
70
71 bool MessageFlowRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
72 {
73     Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.MessageFlow");
74     log.debug("evaluating message flow policy (replay checking %s, expiration %lu)", m_checkReplay ? "on" : "off", m_expires);
75
76     time_t now = policy.getTime();
77     time_t skew = XMLToolingConfig::getConfig().clock_skew_secs;
78     time_t issueInstant = policy.getIssueInstant();
79     if (issueInstant == 0) {
80         issueInstant = now;
81     }
82     else {
83         if (issueInstant > now + skew) {
84             log.errorStream() << "rejected not-yet-valid message, timestamp (" << issueInstant <<
85                 "), newest allowed (" << now + skew << ")" << logging::eol;
86             throw SecurityPolicyException("Message rejected, was issued in the future.");
87         }
88         else if (issueInstant < now - skew - m_expires) {
89             log.errorStream() << "rejected expired message, timestamp (" << issueInstant <<
90                 "), oldest allowed (" << (now - skew - m_expires) << ")" << logging::eol;
91             throw SecurityPolicyException("Message expired, was issued too long ago.");
92         }
93     }
94
95     // Check replay.
96     if (m_checkReplay) {
97         const XMLCh* id = policy.getMessageID();
98         if (!id || !*id)
99             return false;
100
101         ReplayCache* replayCache = XMLToolingConfig::getConfig().getReplayCache();
102         if (!replayCache) {
103             log.warn("no ReplayCache available, skipping requested replay check");
104             return false;
105         }
106
107         auto_ptr_char temp(id);
108         if (!replayCache->check("MessageFlow", temp.get(), issueInstant + skew + m_expires)) {
109             log.error("replay detected of message ID (%s)", temp.get());
110             throw SecurityPolicyException("Rejecting replayed message ID ($1).", params(1,temp.get()));
111         }
112         return true;
113     }
114     return false;
115 }