Revamped binding classes with security policy layer.
[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 "RootObject.h"
26 #include "binding/MessageFlowRule.h"
27
28 #include <xmltooling/util/NDC.h>
29 #include <xmltooling/util/ReplayCache.h>
30 #include <log4cpp/Category.hh>
31
32 using namespace opensaml;
33 using namespace xmltooling;
34 using namespace log4cpp;
35 using namespace std;
36
37 namespace opensaml {
38     SecurityPolicyRule* SAML_DLLLOCAL MessageFlowRuleFactory(const DOMElement* const & e)
39     {
40         return new MessageFlowRule(e);
41     }
42 };
43
44 static const XMLCh checkReplay[] = UNICODE_LITERAL_11(c,h,e,c,k,R,e,p,l,a,y);
45 static const XMLCh expires[] = UNICODE_LITERAL_7(e,x,p,i,r,e,s);
46
47 MessageFlowRule::MessageFlowRule(const DOMElement* e)
48     : m_checkReplay(true), m_expires(XMLToolingConfig::getConfig().clock_skew_secs)
49 {
50     if (e) {
51         const XMLCh* attr = e->getAttributeNS(NULL, checkReplay);
52         if (attr && (*attr==chLatin_f || *attr==chDigit_0))
53             m_checkReplay = false;
54         attr = e->getAttributeNS(NULL, expires);
55         if (attr)
56             m_expires = XMLString::parseInt(attr);
57     }
58 }
59
60 pair<saml2::Issuer*,const saml2md::RoleDescriptor*> MessageFlowRule::evaluate(
61     const GenericRequest& request,
62     const XMLObject& message,
63     const saml2md::MetadataProvider* metadataProvider,
64     const QName* role,
65     const opensaml::TrustEngine* trustEngine
66     ) const
67 {
68     try {
69         const RootObject& obj = dynamic_cast<const RootObject&>(message);
70         check(obj.getID(), obj.getIssueInstantEpoch());
71     }
72     catch (bad_cast&) {
73         throw BindingException("Message was not of a recognized SAML root type.");
74     }
75     return pair<saml2::Issuer*,const saml2md::RoleDescriptor*>(NULL,NULL);
76 }
77
78 void MessageFlowRule::check(const XMLCh* id, time_t issueInstant) const
79 {
80     Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.MessageFlow");
81     log.debug("evaluating message flow policy (replay checking %s, expiration %lu)", m_checkReplay ? "on" : "off", m_expires);
82     
83     time_t skew = XMLToolingConfig::getConfig().clock_skew_secs;
84     time_t now = time(NULL);
85     if (issueInstant > now + skew) {
86         log.error("rejected not-yet-valid message, timestamp (%lu), now (%lu)", issueInstant, now + skew);
87         throw BindingException("Message rejected, was issued in the future.");
88     }
89     else if (issueInstant < now - skew - m_expires) {
90         log.error("rejected expired message, timestamp (%lu), oldest allowed (%lu)", issueInstant, now - skew - m_expires);
91         throw BindingException("Message expired, was issued too long ago.");
92     }
93     
94     // Check replay.
95     if (m_checkReplay) {
96         ReplayCache* replayCache = XMLToolingConfig::getConfig().getReplayCache();
97         if (!replayCache)
98             throw BindingException("No ReplayCache instance available.");
99         else if (!id)
100             throw BindingException("Message did not contain an identifier.");
101         auto_ptr_char temp(id);  
102         if (!replayCache->check("SAML", temp.get(), issueInstant + skew + m_expires)) {
103             log.error("replay detected of message ID (%s)", temp.get());
104             throw BindingException("Rejecting replayed message ID ($1).", params(1,temp.get()));
105         }
106     }
107 }