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 
~DelegationRestrictionRule()53             virtual ~DelegationRestrictionRule() {
54             }
getType() const55             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 
DelegationRestrictionRuleFactory(const DOMElement * const & e,bool)70         SecurityPolicyRule* SAML_DLLLOCAL DelegationRestrictionRuleFactory(const DOMElement* const & e, bool)
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;
isSameFormat(const XMLCh * f1,const XMLCh * f2) const79             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             }
matches(const NameID * n1,const NameID * n2) const86             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:
_isSameDelegate()93             _isSameDelegate() : m_operand(nullptr) {}
_isSameDelegate(const Delegate * d)94             _isSameDelegate(const Delegate* d) : m_operand(d) {}
95 
96             // d1 is the input from the message, d2 is from the policy
operator ()(const Delegate * d1,const Delegate & d2) const97             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
operator ()(const Delegate & d) const111             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 
DelegationRestrictionRule(const DOMElement * e)124 DelegationRestrictionRule::DelegationRestrictionRule(const DOMElement* e)
125     : SecurityPolicyRule(e), 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 
evaluate(const XMLObject & message,const GenericRequest * request,SecurityPolicy & policy) const149 bool DelegationRestrictionRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
150 {
151     if (!SecurityPolicyRule::evaluate(message, request, policy)) {
152         return false;
153     }
154 
155     const DelegationRestrictionType* drt=dynamic_cast<const DelegationRestrictionType*>(&message);
156     if (!drt)
157         return false;
158     const vector<Delegate*>& dels = drt->getDelegates();
159 
160     if (!m_delegates.empty()) {
161         if (m_match == MATCH_ANY) {
162             // Each Delegate in the condition MUST match an embedded Delegate.
163             for (vector<Delegate*>::const_iterator d1 = dels.begin(); d1 != dels.end(); ++d1) {
164                 if (find_if(m_delegates.begin(), m_delegates.end(), _isSameDelegate(*d1)) == m_delegates.end())
165                     return false;
166             }
167         }
168         else if (m_match == MATCH_OLDEST) {
169             if (search(dels.begin(), dels.end(), m_delegates.begin(), m_delegates.end(), _isSameDelegate()) != dels.begin())
170                 return false;
171         }
172         else if (m_match == MATCH_NEWEST) {
173             if (search(dels.rbegin(), dels.rend(), m_delegates.begin(), m_delegates.end(), _isSameDelegate()) != dels.rbegin())
174                 return false;
175         }
176     }
177 
178     if (m_maxTime > 0) {
179         return (!dels.empty() && dels.front()->getDelegationInstant() &&
180             (time(nullptr) - dels.front()->getDelegationInstantEpoch() - XMLToolingConfig::getConfig().clock_skew_secs <= m_maxTime));
181     }
182 
183     return true;
184 }
185