66e92a7c3c02c737f180cc4ac733f5f066264834
[shibboleth/cpp-opensaml.git] / saml / saml2 / profile / impl / DelegationRestrictionRule.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  * DelegationRestrictionRule.cpp
23  *
24  * SAML DelegationRestriction SecurityPolicyRule.
25  */
26
27 #include "internal.h"
28 #include "exceptions.h"
29 #include "binding/SecurityPolicyRule.h"
30 #include "saml2/core/Assertions.h"
31 #include "util/SAMLConstants.h"
32
33 #include <ctime>
34 #include <boost/ptr_container/ptr_vector.hpp>
35 #include <xercesc/util/XMLUniDefs.hpp>
36 #include <xmltooling/logging.h>
37 #include <xmltooling/XMLToolingConfig.h>
38
39 using namespace opensaml::saml2;
40 using namespace opensaml;
41 using namespace xmltooling::logging;
42 using namespace xmltooling;
43 using namespace boost;
44 using namespace std;
45
46 namespace opensaml {
47     namespace saml2 {
48         class SAML_DLLLOCAL DelegationRestrictionRule : public SecurityPolicyRule
49         {
50         public:
51             DelegationRestrictionRule(const DOMElement* e);
52
53             virtual ~DelegationRestrictionRule() {
54             }
55             const char* getType() const {
56                 return DELEGATION_POLICY_RULE;
57             }
58             bool evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
59
60         private:
61             ptr_vector<Delegate> m_delegates;
62             enum {
63                 MATCH_ANY,
64                 MATCH_NEWEST,
65                 MATCH_OLDEST
66             } m_match;
67             time_t m_maxTime;
68         };
69
70         SecurityPolicyRule* SAML_DLLLOCAL DelegationRestrictionRuleFactory(const DOMElement* const & e)
71         {
72             return new DelegationRestrictionRule(e);
73         }
74
75         class SAML_DLLLOCAL _isSameDelegate : public binary_function<const Delegate*,const Delegate*,bool>,
76             public unary_function<const Delegate*,bool>
77         {
78             const Delegate* m_operand;
79             bool isSameFormat(const XMLCh* f1, const XMLCh* f2) const {
80                 if (!f1 || !*f1)
81                     f1 = NameIDType::UNSPECIFIED;
82                 if (!f2 || !*f2)
83                     f2 = NameIDType::UNSPECIFIED;
84                 return XMLString::equals(f1, f2);
85             }
86             bool matches(const NameID* n1, const NameID* n2) const {
87                 return (isSameFormat(n1->getFormat(), n2->getFormat()) &&
88                         XMLString::equals(n1->getName(), n2->getName()) &&
89                         XMLString::equals(n1->getNameQualifier(), n2->getNameQualifier()) &&
90                         XMLString::equals(n1->getSPNameQualifier(), n2->getSPNameQualifier()));
91             }
92         public:
93             _isSameDelegate() : m_operand(nullptr) {}
94             _isSameDelegate(const Delegate* d) : m_operand(d) {}
95
96             // d1 is the input from the message, d2 is from the policy
97             bool operator()(const Delegate* d1, const Delegate& d2) const {
98                 if (!d1->getNameID()) {
99                     Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.DelegationRestriction").error(
100                         "rule doesn't support evaluation of BaseID or EncryptedID in a Delegate"
101                         );
102                     return false;
103                 }
104                 if (!d2.getConfirmationMethod() || XMLString::equals(d1->getConfirmationMethod(), d2.getConfirmationMethod())) {
105                     return matches(d1->getNameID(), d2.getNameID());
106                 }
107                 return false;
108             }
109
110             // d is from the policy
111             bool operator()(const Delegate& d) const {
112                 return this->operator()(m_operand, d);
113             }
114         };
115
116         static XMLCh match[] =  UNICODE_LITERAL_5(m,a,t,c,h);
117         static XMLCh any[] =    UNICODE_LITERAL_8(a,n,y,O,r,d,e,r);
118         static XMLCh newest[] = UNICODE_LITERAL_6(n,e,w,e,s,t);
119         static XMLCh oldest[] = UNICODE_LITERAL_6(o,l,d,e,s,t);
120         static XMLCh maxTimeSinceDelegation[] = UNICODE_LITERAL_22(m,a,x,T,i,m,e,S,i,n,c,e,D,e,l,e,g,a,t,i,o,n);
121     }
122 };
123
124 DelegationRestrictionRule::DelegationRestrictionRule(const DOMElement* e)
125     : m_match(MATCH_ANY), m_maxTime(XMLHelper::getAttrInt(e, 0, maxTimeSinceDelegation))
126 {
127     if (e) {
128         const XMLCh* m = e ? e->getAttributeNS(nullptr, match) : nullptr;
129         if (XMLString::equals(m, newest))
130             m_match = MATCH_NEWEST;
131         else if (XMLString::equals(m, oldest))
132             m_match = MATCH_OLDEST;
133         else if (m && *m && !XMLString::equals(m, any))
134             throw SecurityPolicyException("Invalid value for \"match\" attribute in Delegation rule.");
135
136         DOMElement* d = XMLHelper::getFirstChildElement(e, samlconstants::SAML20_DELEGATION_CONDITION_NS, Delegate::LOCAL_NAME);
137         while (d) {
138             auto_ptr<XMLObject> wrapper(XMLObjectBuilder::buildOneFromElement(d));
139             Delegate* down = dynamic_cast<Delegate*>(wrapper.get());
140             if (down) {
141                 m_delegates.push_back(down);
142                 wrapper.release();
143             }
144             d = XMLHelper::getNextSiblingElement(d, samlconstants::SAML20_DELEGATION_CONDITION_NS, Delegate::LOCAL_NAME);
145         }
146     }
147 }
148
149 bool DelegationRestrictionRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
150 {
151     const DelegationRestrictionType* drt=dynamic_cast<const DelegationRestrictionType*>(&message);
152     if (!drt)
153         return false;
154     const vector<Delegate*>& dels = drt->getDelegates();
155
156     if (!m_delegates.empty()) {
157         if (m_match == MATCH_ANY) {
158             // Each Delegate in the condition MUST match an embedded Delegate.
159             for (vector<Delegate*>::const_iterator d1 = dels.begin(); d1 != dels.end(); ++d1) {
160                 if (find_if(m_delegates.begin(), m_delegates.end(), _isSameDelegate(*d1)) == m_delegates.end())
161                     return false;
162             }
163         }
164         else if (m_match == MATCH_OLDEST) {
165             if (search(dels.begin(), dels.end(), m_delegates.begin(), m_delegates.end(), _isSameDelegate()) != dels.begin())
166                 return false;
167         }
168         else if (m_match == MATCH_NEWEST) {
169             if (search(dels.rbegin(), dels.rend(), m_delegates.begin(), m_delegates.end(), _isSameDelegate()) != dels.rbegin())
170                 return false;
171         }
172     }
173
174     if (m_maxTime > 0) {
175         return (!dels.empty() && dels.front()->getDelegationInstant() &&
176             (time(nullptr) - dels.front()->getDelegationInstantEpoch() - XMLToolingConfig::getConfig().clock_skew_secs <= m_maxTime));
177     }
178
179     return true;
180 }