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