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