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);
~MessageFlowRule()48         virtual ~MessageFlowRule() {}
49 
getType() const50         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         logging::Category& m_log;
57         bool m_checkReplay, m_correlation, m_blockUnsolicited;
58         time_t m_expires;
59     };
60 
MessageFlowRuleFactory(const DOMElement * const & e,bool)61     SecurityPolicyRule* SAML_DLLLOCAL MessageFlowRuleFactory(const DOMElement* const & e, bool)
62     {
63         return new MessageFlowRule(e);
64     }
65 
66     static const XMLCh blockUnsolicited[] = UNICODE_LITERAL_16(b,l,o,c,k,U,n,s,o,l,i,c,i,t,e,d);
67     static const XMLCh checkReplay[] =      UNICODE_LITERAL_11(c,h,e,c,k,R,e,p,l,a,y);
68     static const XMLCh checkCorrelation[] = UNICODE_LITERAL_16(c,h,e,c,k,C,o,r,r,e,l,a,t,i,o,n);
69     static const XMLCh expires[] =          UNICODE_LITERAL_7(e,x,p,i,r,e,s);
70 };
71 
MessageFlowRule(const DOMElement * e)72 MessageFlowRule::MessageFlowRule(const DOMElement* e) : SecurityPolicyRule(e),
73     m_log(logging::Category::getInstance(SAML_LOGCAT ".SecurityPolicyRule.MessageFlow")),
74         m_checkReplay(XMLHelper::getAttrBool(e, true, checkReplay)),
75         m_correlation(XMLHelper::getAttrBool(e, false, checkCorrelation)),
76         m_blockUnsolicited(XMLHelper::getAttrBool(e, false, blockUnsolicited)),
77         m_expires(XMLHelper::getAttrInt(e, XMLToolingConfig::getConfig().clock_skew_secs, expires))
78 {
79     if (m_blockUnsolicited && !m_correlation) {
80         m_correlation = true;
81         m_log.info("enabling request/response correlation checking to block unsolicited responses");
82     }
83 }
84 
evaluate(const XMLObject & message,const GenericRequest * request,SecurityPolicy & policy) const85 bool MessageFlowRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
86 {
87     if (!SecurityPolicyRule::evaluate(message, request, policy)) {
88         return false;
89     }
90 
91     Category& log=Category::getInstance(SAML_LOGCAT ".SecurityPolicyRule.MessageFlow");
92     log.debug("evaluating message flow policy (correlation %s, replay checking %s, expiration %lu)",
93         m_correlation ? "on" : "off", m_checkReplay ? "on" : "off", m_expires);
94 
95     time_t now = policy.getTime();
96     time_t skew = XMLToolingConfig::getConfig().clock_skew_secs;
97     time_t issueInstant = policy.getIssueInstant();
98     if (issueInstant == 0) {
99         issueInstant = now;
100     }
101     else {
102         if (issueInstant > now + skew) {
103             log.warnStream() << "rejected not-yet-valid message, timestamp (" << issueInstant <<
104                 "), newest allowed (" << now + skew << ")" << logging::eol;
105             throw SecurityPolicyException("Message rejected, was issued in the future.");
106         }
107         else if (issueInstant < now - skew - m_expires) {
108             log.warnStream() << "rejected expired message, timestamp (" << issueInstant <<
109                 "), oldest allowed (" << (now - skew - m_expires) << ")" << logging::eol;
110             throw SecurityPolicyException("Message expired, was issued too long ago.");
111         }
112     }
113 
114     if (m_correlation) {
115         if (policy.getCorrelationID() && *(policy.getCorrelationID())) {
116             if (XMLString::equals(policy.getCorrelationID(), policy.getInResponseTo())) {
117                 log.debug("request/response correlation validated");
118             }
119             else {
120                 auto_ptr_char requestID(policy.getCorrelationID());
121                 log.warn("response correlation ID did not match request ID (%s)", requestID.get());
122                 throw SecurityPolicyException("Rejecting non-correlated response to request ID.");
123             }
124         }
125         else if (policy.getInResponseTo() && *(policy.getInResponseTo())) {
126             log.warn("request/response correlation failed due to lack of request ID to compare");
127             throw SecurityPolicyException("Response correlation failed with lack of correlation ID.");
128         }
129         else if (m_blockUnsolicited) {
130             log.warn("unsolicited response rejected by policy");
131             throw SecurityPolicyException("Unsolicited response rejected by policy.");
132         }
133         else {
134             log.debug("unsolicited message accepted");
135         }
136     }
137     else {
138         log.debug("ignoring InResponseTo, correlation checking is disabled");
139     }
140 
141     // Check replay.
142     if (m_checkReplay) {
143         const XMLCh* id = policy.getMessageID();
144         if (!id || !*id)
145             return false;
146 
147         ReplayCache* replayCache = XMLToolingConfig::getConfig().getReplayCache();
148         if (!replayCache) {
149             log.warn("no ReplayCache available, skipping requested replay check");
150             return false;
151         }
152 
153         auto_ptr_char temp(id);
154         if (!replayCache->check("MessageFlow", temp.get(), issueInstant + skew + m_expires)) {
155             log.error("replay detected of message ID (%s)", temp.get());
156             throw SecurityPolicyException("Rejecting replayed message ID ($1).", params(1,temp.get()));
157         }
158         return true;
159     }
160     return false;
161 }
162