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