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.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 * DelegationRestrictionRule.cpp
24 * SAML DelegationRestriction SecurityPolicyRule.
28 #include "exceptions.h"
29 #include "binding/SecurityPolicyRule.h"
30 #include "saml2/core/Assertions.h"
31 #include "util/SAMLConstants.h"
34 #include <boost/ptr_container/ptr_vector.hpp>
35 #include <xercesc/util/XMLUniDefs.hpp>
36 #include <xmltooling/logging.h>
37 #include <xmltooling/XMLToolingConfig.h>
39 using namespace opensaml::saml2;
40 using namespace opensaml;
41 using namespace xmltooling::logging;
42 using namespace xmltooling;
43 using namespace boost;
48 class SAML_DLLLOCAL DelegationRestrictionRule : public SecurityPolicyRule
51 DelegationRestrictionRule(const DOMElement* e);
53 virtual ~DelegationRestrictionRule() {
55 const char* getType() const {
56 return DELEGATION_POLICY_RULE;
58 bool evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
61 ptr_vector<Delegate> m_delegates;
70 SecurityPolicyRule* SAML_DLLLOCAL DelegationRestrictionRuleFactory(const DOMElement* const & e)
72 return new DelegationRestrictionRule(e);
75 class SAML_DLLLOCAL _isSameDelegate : public binary_function<const Delegate*,const Delegate*,bool>,
76 public unary_function<const Delegate*,bool>
78 const Delegate* m_operand;
79 bool isSameFormat(const XMLCh* f1, const XMLCh* f2) const {
81 f1 = NameIDType::UNSPECIFIED;
83 f2 = NameIDType::UNSPECIFIED;
84 return XMLString::equals(f1, f2);
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()));
93 _isSameDelegate() : m_operand(nullptr) {}
94 _isSameDelegate(const Delegate* d) : m_operand(d) {}
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"
104 if (!d2.getConfirmationMethod() || XMLString::equals(d1->getConfirmationMethod(), d2.getConfirmationMethod())) {
105 return matches(d1->getNameID(), d2.getNameID());
110 // d is from the policy
111 bool operator()(const Delegate& d) const {
112 return this->operator()(m_operand, d);
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);
124 DelegationRestrictionRule::DelegationRestrictionRule(const DOMElement* e)
125 : m_match(MATCH_ANY), m_maxTime(XMLHelper::getAttrInt(e, 0, maxTimeSinceDelegation))
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.");
136 DOMElement* d = XMLHelper::getFirstChildElement(e, samlconstants::SAML20_DELEGATION_CONDITION_NS, Delegate::LOCAL_NAME);
138 auto_ptr<XMLObject> wrapper(XMLObjectBuilder::buildOneFromElement(d));
139 Delegate* down = dynamic_cast<Delegate*>(wrapper.get());
141 m_delegates.push_back(down);
144 d = XMLHelper::getNextSiblingElement(d, samlconstants::SAML20_DELEGATION_CONDITION_NS, Delegate::LOCAL_NAME);
149 bool DelegationRestrictionRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
151 const DelegationRestrictionType* drt=dynamic_cast<const DelegationRestrictionType*>(&message);
154 const vector<Delegate*>& dels = drt->getDelegates();
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())
164 else if (m_match == MATCH_OLDEST) {
165 if (search(dels.begin(), dels.end(), m_delegates.begin(), m_delegates.end(), _isSameDelegate()) != dels.begin())
168 else if (m_match == MATCH_NEWEST) {
169 if (search(dels.rbegin(), dels.rend(), m_delegates.begin(), m_delegates.end(), _isSameDelegate()) != dels.rbegin())
175 return (!dels.empty() && dels.front()->getDelegationInstant() &&
176 (time(nullptr) - dels.front()->getDelegationInstantEpoch() - XMLToolingConfig::getConfig().clock_skew_secs <= m_maxTime));