SSPCPP-616 - clean up concatenated string literals
[shibboleth/cpp-opensaml.git] / saml / binding / impl / MessageFlowRule.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * MessageFlowRule.cpp
23  *
24  * SAML replay and freshness checking SecurityPolicyRule.
25  */
26
27 #include "internal.h"
28 #include "exceptions.h"
29 #include "binding/SecurityPolicy.h"
30 #include "binding/SecurityPolicyRule.h"
31
32 #include <xmltooling/logging.h>
33 #include <xmltooling/XMLToolingConfig.h>
34 #include <xmltooling/util/ReplayCache.h>
35 #include <xmltooling/util/XMLHelper.h>
36 #include <xercesc/util/XMLUniDefs.hpp>
37
38 using namespace opensaml;
39 using namespace xmltooling::logging;
40 using namespace xmltooling;
41 using namespace std;
42
43 namespace opensaml {
44     class SAML_DLLLOCAL MessageFlowRule : public SecurityPolicyRule
45     {
46     public:
47         MessageFlowRule(const DOMElement* e);
48         virtual ~MessageFlowRule() {}
49
50         const char* getType() const {
51             return MESSAGEFLOW_POLICY_RULE;
52         }
53         bool evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
54
55     private:
56         bool m_checkReplay;
57         time_t m_expires;
58     };
59
60     SecurityPolicyRule* SAML_DLLLOCAL MessageFlowRuleFactory(const DOMElement* const & e)
61     {
62         return new MessageFlowRule(e);
63     }
64 };
65
66 static const XMLCh checkReplay[] = UNICODE_LITERAL_11(c,h,e,c,k,R,e,p,l,a,y);
67 static const XMLCh expires[] = UNICODE_LITERAL_7(e,x,p,i,r,e,s);
68
69 MessageFlowRule::MessageFlowRule(const DOMElement* e)
70     : m_checkReplay(XMLHelper::getAttrBool(e, true, checkReplay)),
71         m_expires(XMLHelper::getAttrInt(e, XMLToolingConfig::getConfig().clock_skew_secs, expires))
72 {
73 }
74
75 bool MessageFlowRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
76 {
77     Category& log=Category::getInstance(SAML_LOGCAT ".SecurityPolicyRule.MessageFlow");
78     log.debug("evaluating message flow policy (replay checking %s, expiration %lu)", m_checkReplay ? "on" : "off", m_expires);
79
80     time_t now = policy.getTime();
81     time_t skew = XMLToolingConfig::getConfig().clock_skew_secs;
82     time_t issueInstant = policy.getIssueInstant();
83     if (issueInstant == 0) {
84         issueInstant = now;
85     }
86     else {
87         if (issueInstant > now + skew) {
88             log.errorStream() << "rejected not-yet-valid message, timestamp (" << issueInstant <<
89                 "), newest allowed (" << now + skew << ")" << logging::eol;
90             throw SecurityPolicyException("Message rejected, was issued in the future.");
91         }
92         else if (issueInstant < now - skew - m_expires) {
93             log.errorStream() << "rejected expired message, timestamp (" << issueInstant <<
94                 "), oldest allowed (" << (now - skew - m_expires) << ")" << logging::eol;
95             throw SecurityPolicyException("Message expired, was issued too long ago.");
96         }
97     }
98
99     // Check replay.
100     if (m_checkReplay) {
101         const XMLCh* id = policy.getMessageID();
102         if (!id || !*id)
103             return false;
104
105         ReplayCache* replayCache = XMLToolingConfig::getConfig().getReplayCache();
106         if (!replayCache) {
107             log.warn("no ReplayCache available, skipping requested replay check");
108             return false;
109         }
110
111         auto_ptr_char temp(id);
112         if (!replayCache->check("MessageFlow", temp.get(), issueInstant + skew + m_expires)) {
113             log.error("replay detected of message ID (%s)", temp.get());
114             throw SecurityPolicyException("Rejecting replayed message ID ($1).", params(1,temp.get()));
115         }
116         return true;
117     }
118     return false;
119 }