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 <xercesc/util/XMLUniDefs.hpp>
35 #include <xmltooling/logging.h>
36 #include <xmltooling/XMLToolingConfig.h>
38 using namespace opensaml::saml2;
39 using namespace opensaml;
40 using namespace xmltooling::logging;
41 using namespace xmltooling;
46 class SAML_DLLLOCAL DelegationRestrictionRule : public SecurityPolicyRule
49 DelegationRestrictionRule(const DOMElement* e);
51 virtual ~DelegationRestrictionRule() {
52 for_each(m_delegates.begin(), m_delegates.end(), xmltooling::cleanup<Delegate>());
54 const char* getType() const {
55 return DELEGATION_POLICY_RULE;
57 bool evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
60 vector<Delegate*> m_delegates;
69 SecurityPolicyRule* SAML_DLLLOCAL DelegationRestrictionRuleFactory(const DOMElement* const & e)
71 return new DelegationRestrictionRule(e);
74 class SAML_DLLLOCAL _isSameDelegate : public binary_function<const Delegate*,const Delegate*,bool>,
75 public unary_function<const Delegate*,bool>
77 const Delegate* m_operand;
78 bool isSameFormat(const XMLCh* f1, const XMLCh* f2) const {
80 f1 = NameIDType::UNSPECIFIED;
82 f2 = NameIDType::UNSPECIFIED;
83 return XMLString::equals(f1, f2);
85 bool matches(const NameID* n1, const NameID* n2) const {
86 return (isSameFormat(n1->getFormat(), n2->getFormat()) &&
87 XMLString::equals(n1->getName(), n2->getName()) &&
88 XMLString::equals(n1->getNameQualifier(), n2->getNameQualifier()) &&
89 XMLString::equals(n1->getSPNameQualifier(), n2->getSPNameQualifier()));
92 _isSameDelegate() : m_operand(nullptr) {}
93 _isSameDelegate(const Delegate* d) : m_operand(d) {}
95 // d1 is the input from the message, d2 is from the policy
96 bool operator()(const Delegate* d1, const Delegate* d2) const {
97 if (!d1->getNameID()) {
98 Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.DelegationRestriction").error(
99 "rule doesn't support evaluation of BaseID or EncryptedID in a Delegate"
103 if (!d2->getConfirmationMethod() || XMLString::equals(d1->getConfirmationMethod(), d2->getConfirmationMethod())) {
104 return matches(d1->getNameID(), d2->getNameID());
109 // d is from the policy
110 bool operator()(const Delegate* d) const {
111 return this->operator()(m_operand, d);
115 static XMLCh match[] = UNICODE_LITERAL_5(m,a,t,c,h);
116 static XMLCh any[] = UNICODE_LITERAL_8(a,n,y,O,r,d,e,r);
117 static XMLCh newest[] = UNICODE_LITERAL_6(n,e,w,e,s,t);
118 static XMLCh oldest[] = UNICODE_LITERAL_6(o,l,d,e,s,t);
119 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);
123 DelegationRestrictionRule::DelegationRestrictionRule(const DOMElement* e)
124 : m_match(MATCH_ANY), m_maxTime(XMLHelper::getAttrInt(e, 0, maxTimeSinceDelegation))
127 const XMLCh* m = e ? e->getAttributeNS(nullptr, match) : nullptr;
128 if (XMLString::equals(m, newest))
129 m_match = MATCH_NEWEST;
130 else if (XMLString::equals(m, oldest))
131 m_match = MATCH_OLDEST;
132 else if (m && *m && !XMLString::equals(m, any))
133 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);
148 for_each(m_delegates.begin(), m_delegates.end(), xmltooling::cleanup<Delegate>());
154 bool DelegationRestrictionRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
156 const DelegationRestrictionType* drt=dynamic_cast<const DelegationRestrictionType*>(&message);
159 const vector<Delegate*>& dels = drt->getDelegates();
161 if (!m_delegates.empty()) {
162 if (m_match == MATCH_ANY) {
163 // Each Delegate in the condition MUST match an embedded Delegate.
164 for (vector<Delegate*>::const_iterator d1 = dels.begin(); d1 != dels.end(); ++d1) {
165 if (find_if(m_delegates.begin(), m_delegates.end(), _isSameDelegate(*d1)) == m_delegates.end())
169 else if (m_match == MATCH_OLDEST) {
170 if (search(dels.begin(), dels.end(), m_delegates.begin(), m_delegates.end(), _isSameDelegate()) != dels.begin())
173 else if (m_match == MATCH_NEWEST) {
174 if (search(dels.rbegin(), dels.rend(), m_delegates.begin(), m_delegates.end(), _isSameDelegate()) != dels.rbegin())
180 return (!dels.empty() && dels.front()->getDelegationInstant() &&
181 (time(nullptr) - dels.front()->getDelegationInstantEpoch() - XMLToolingConfig::getConfig().clock_skew_secs <= m_maxTime));