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