Reducing header overuse, non-inlining selected methods (CPPOST-35).
[shibboleth/cpp-opensaml.git] / saml / profile / impl / ConditionsRule.cpp
1 /*
2  *  Copyright 2009 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  * ConditionsRule.cpp
19  *
20  * SAML Conditions SecurityPolicyRule.
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "binding/SecurityPolicy.h"
26 #include "binding/SecurityPolicyRule.h"
27 #include "saml1/core/Assertions.h"
28 #include "saml2/core/Assertions.h"
29
30 #include <xmltooling/logging.h>
31 #include <xmltooling/XMLToolingConfig.h>
32 #include <xmltooling/util/ParserPool.h>
33
34 using namespace opensaml;
35 using namespace xmltooling::logging;
36 using namespace xmltooling;
37 using namespace std;
38
39 namespace opensaml {
40     class SAML_DLLLOCAL ConditionsRule : public SecurityPolicyRule
41     {
42     public:
43         ConditionsRule(const DOMElement* e);
44
45         virtual ~ConditionsRule() {
46             for_each(m_rules.begin(), m_rules.end(), xmltooling::cleanup<SecurityPolicyRule>());
47             if (m_doc)
48                 m_doc->release();
49         }
50         const char* getType() const {
51             return CONDITIONS_POLICY_RULE;
52         }
53         bool evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
54
55     private:
56         DOMDocument* m_doc;
57         vector<SecurityPolicyRule*> m_rules;
58     };
59
60     SecurityPolicyRule* SAML_DLLLOCAL ConditionsRuleFactory(const DOMElement* const & e)
61     {
62         return new ConditionsRule(e);
63     }
64
65     static const XMLCh Rule[] =     UNICODE_LITERAL_10(P,o,l,i,c,y,R,u,l,e);
66     static const XMLCh type[] =     UNICODE_LITERAL_4(t,y,p,e);
67
68     const char config[] =
69         "<PolicyRule type=\"Conditions\" xmlns:saml2=\"urn:oasis:names:tc:SAML:2.0:assertion\" xmlns:saml=\"urn:oasis:names:tc:SAML:1.0:assertion\">"
70             "<PolicyRule type=\"Audience\"/>"
71             "<PolicyRule type=\"Ignore\">saml:DoNotCacheCondition</PolicyRule>"
72             "<PolicyRule type=\"Ignore\">saml2:OneTimeUse</PolicyRule>"
73             "<PolicyRule type=\"Ignore\">saml2:ProxyRestriction</PolicyRule>"
74         "</PolicyRule>";
75 };
76
77 ConditionsRule::ConditionsRule(const DOMElement* e) : m_doc(NULL)
78 {
79     Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.Conditions");
80
81     if (!e || !e->hasChildNodes()) {
82         // Default the configuration.
83         istringstream in(config);
84         m_doc = XMLToolingConfig::getConfig().getParser().parse(in);
85         e = m_doc->getDocumentElement();
86     }
87
88     e = XMLHelper::getFirstChildElement(e, Rule);
89     while (e) {
90         auto_ptr_char temp(e->getAttributeNS(NULL, type));
91         if (temp.get() && *temp.get()) {
92             try {
93                 log.info("building SecurityPolicyRule of type %s", temp.get());
94                 m_rules.push_back(SAMLConfig::getConfig().SecurityPolicyRuleManager.newPlugin(temp.get(),e));
95             }
96             catch (exception& ex) {
97                 log.crit("error building SecurityPolicyRule: %s", ex.what());
98             }
99         }
100         e = XMLHelper::getNextSiblingElement(e, Rule);
101     }
102 }
103
104 bool ConditionsRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
105 {
106     const saml2::Assertion* a2=dynamic_cast<const saml2::Assertion*>(&message);
107     if (a2) {
108         const saml2::Conditions* conds = a2->getConditions();
109         if (!conds)
110             return true;
111
112         // First verify the time conditions, using the specified timestamp.
113         time_t now = policy.getTime();
114         unsigned int skew = XMLToolingConfig::getConfig().clock_skew_secs;
115         time_t t = conds->getNotBeforeEpoch();
116         if (now + skew < t)
117             throw SecurityPolicyException("Assertion is not yet valid.");
118         t = conds->getNotOnOrAfterEpoch();
119         if (t <= now - skew)
120             throw SecurityPolicyException("Assertion is no longer valid.");
121
122         // Now we process conditions, starting with the known types and then extensions.
123
124         bool valid;
125
126         const vector<saml2::AudienceRestriction*>& acvec = conds->getAudienceRestrictions();
127         for (vector<saml2::AudienceRestriction*>::const_iterator ac = acvec.begin(); ac != acvec.end(); ++ac) {
128             valid = false;
129             for (vector<SecurityPolicyRule*>::const_iterator r = m_rules.begin(); !valid && r != m_rules.end(); ++r)
130                 valid = (*r)->evaluate(*(*ac), request, policy);
131             if (!valid)
132                 throw SecurityPolicyException("AudienceRestriction condition not successfully validated by policy.");
133         }
134
135         const vector<saml2::OneTimeUse*>& otvec = conds->getOneTimeUses();
136         for (vector<saml2::OneTimeUse*>::const_iterator ot = otvec.begin(); ot!=otvec.end(); ++ot) {
137             valid = false;
138             for (vector<SecurityPolicyRule*>::const_iterator r = m_rules.begin(); !valid && r != m_rules.end(); ++r)
139                 valid = (*r)->evaluate(*(*ot), request, policy);
140             if (!valid)
141                 throw SecurityPolicyException("OneTimeUse condition not successfully validated by policy.");
142         }
143
144         const vector<saml2::ProxyRestriction*> pvec = conds->getProxyRestrictions();
145         for (vector<saml2::ProxyRestriction*>::const_iterator p = pvec.begin(); p != pvec.end(); ++p) {
146             valid = false;
147             for (vector<SecurityPolicyRule*>::const_iterator r = m_rules.begin(); !valid && r != m_rules.end(); ++r)
148                 valid = (*r)->evaluate(*(*p), request, policy);
149             if (!valid)
150                 throw SecurityPolicyException("ProxyRestriction condition not successfully validated by policy.");
151         }
152
153         const vector<saml2::Condition*>& convec = conds->getConditions();
154         for (vector<saml2::Condition*>::const_iterator c = convec.begin(); c != convec.end(); ++c) {
155             valid = false;
156             for (vector<SecurityPolicyRule*>::const_iterator r = m_rules.begin(); !valid && r != m_rules.end(); ++r)
157                 valid = (*r)->evaluate(*(*c), request, policy);
158             if (!valid) {
159                 throw SecurityPolicyException(
160                     "Extension condition ($1) not successfully validated by policy.",
161                     params(1,((*c)->getSchemaType() ? (*c)->getSchemaType()->toString().c_str() : "Unknown Type"))
162                     );
163             }
164         }
165
166         return true;
167     }
168
169     const saml1::Assertion* a1=dynamic_cast<const saml1::Assertion*>(&message);
170     if (a1) {
171         const saml1::Conditions* conds = a1->getConditions();
172         if (!conds)
173             return true;
174
175         // First verify the time conditions, using the specified timestamp.
176         time_t now = policy.getTime();
177         unsigned int skew = XMLToolingConfig::getConfig().clock_skew_secs;
178         time_t t = conds->getNotBeforeEpoch();
179         if (now + skew < t)
180             throw SecurityPolicyException("Assertion is not yet valid.");
181         t = conds->getNotOnOrAfterEpoch();
182         if (t <= now - skew)
183             throw SecurityPolicyException("Assertion is no longer valid.");
184
185         // Now we process conditions, starting with the known types and then extensions.
186
187         bool valid;
188
189         const vector<saml1::AudienceRestrictionCondition*>& acvec = conds->getAudienceRestrictionConditions();
190         for (vector<saml1::AudienceRestrictionCondition*>::const_iterator ac = acvec.begin(); ac != acvec.end(); ++ac) {
191             valid = false;
192             for (vector<SecurityPolicyRule*>::const_iterator r = m_rules.begin(); !valid && r != m_rules.end(); ++r)
193                 valid = (*r)->evaluate(*(*ac), request, policy);
194             if (!valid)
195                 throw SecurityPolicyException("AudienceRestrictionCondition not successfully validated by policy.");
196         }
197
198         const vector<saml1::DoNotCacheCondition*>& dncvec = conds->getDoNotCacheConditions();
199         for (vector<saml1::DoNotCacheCondition*>::const_iterator dnc = dncvec.begin(); dnc != dncvec.end(); ++dnc) {
200             valid = false;
201             for (vector<SecurityPolicyRule*>::const_iterator r = m_rules.begin(); !valid && r != m_rules.end(); ++r)
202                 valid = (*r)->evaluate(*(*dnc), request, policy);
203             if (!valid)
204                 throw SecurityPolicyException("DoNotCacheCondition not successfully validated by policy.");
205         }
206
207         const vector<saml1::Condition*>& convec = conds->getConditions();
208         for (vector<saml1::Condition*>::const_iterator c = convec.begin(); c != convec.end(); ++c) {
209             valid = false;
210             for (vector<SecurityPolicyRule*>::const_iterator r = m_rules.begin(); !valid && r != m_rules.end(); ++r)
211                 valid = (*r)->evaluate(*(*c), request, policy);
212             if (!valid) {
213                 throw SecurityPolicyException(
214                     "Extension condition ($1) not successfully validated by policy.",
215                     params(1,((*c)->getSchemaType() ? (*c)->getSchemaType()->toString().c_str() : (*c)->getElementQName().toString().c_str()))
216                     );
217             }
218         }
219
220         return true;
221     }
222
223     return false;
224 }