Moved dest. check back to decoders, policy API changes.
[shibboleth/cpp-opensaml.git] / saml / binding / impl / SecurityPolicy.cpp
1 /*
2  *  Copyright 2001-2006 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  * SecurityPolicy.cpp
19  * 
20  * Overall policy used to verify the security of an incoming message. 
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "binding/SecurityPolicy.h"
26 #include "saml2/core/Assertions.h"
27
28 using namespace opensaml::saml2md;
29 using namespace opensaml::saml2;
30 using namespace opensaml;
31 using namespace xmltooling;
32 using namespace std;
33
34 namespace opensaml {
35     SAML_DLLLOCAL PluginManager<SecurityPolicyRule,const DOMElement*>::Factory MessageFlowRuleFactory;
36     SAML_DLLLOCAL PluginManager<SecurityPolicyRule,const DOMElement*>::Factory SimpleSigningRuleFactory;
37     SAML_DLLLOCAL PluginManager<SecurityPolicyRule,const DOMElement*>::Factory XMLSigningRuleFactory;
38 };
39
40 void SAML_API opensaml::registerSecurityPolicyRules()
41 {
42     SAMLConfig& conf=SAMLConfig::getConfig();
43     conf.SecurityPolicyRuleManager.registerFactory(MESSAGEFLOW_POLICY_RULE, MessageFlowRuleFactory);
44     conf.SecurityPolicyRuleManager.registerFactory(SIMPLESIGNING_POLICY_RULE, SimpleSigningRuleFactory);
45     conf.SecurityPolicyRuleManager.registerFactory(XMLSIGNING_POLICY_RULE, XMLSigningRuleFactory);
46 }
47
48 SecurityPolicy::IssuerMatchingPolicy SecurityPolicy::m_defaultMatching;
49
50 SecurityPolicy::~SecurityPolicy()
51 {
52     delete m_matchingPolicy;
53     delete m_issuer;
54 }
55
56 void SecurityPolicy::evaluate(const GenericRequest& request, const XMLObject& message)
57 {
58     for (vector<const SecurityPolicyRule*>::const_iterator i=m_rules.begin(); i!=m_rules.end(); ++i) {
59
60         // Run the rule...
61         pair<Issuer*,const RoleDescriptor*> ident = (*i)->evaluate(request,message,m_metadata,&m_role,m_trust);
62
63         // Make sure returned issuer doesn't conflict.
64          
65         if (ident.first) {
66             if (!(m_matchingPolicy ? m_matchingPolicy : &m_defaultMatching)->issuerMatches(ident.first, m_issuer)) {
67                 delete ident.first;
68                 throw BindingException("Policy rules returned differing Issuers.");
69             }
70             delete m_issuer;
71             m_issuer=ident.first;
72         }
73
74         if (ident.second) {
75             if (m_issuerRole && ident.second!=m_issuerRole)
76                 throw BindingException("Policy rules returned differing issuer RoleDescriptors.");
77             m_issuerRole=ident.second;
78         }
79     }
80 }
81
82 void SecurityPolicy::setIssuer(saml2::Issuer* issuer)
83 {
84     if (!((m_matchingPolicy ? m_matchingPolicy : &m_defaultMatching))->issuerMatches(issuer, m_issuer)) {
85         delete issuer;
86         throw BindingException("Externally provided Issuer conflicts with policy results.");
87     }
88     
89     delete m_issuer;
90     m_issuer=issuer;
91 }
92
93 void SecurityPolicy::setIssuerMetadata(const RoleDescriptor* issuerRole)
94 {
95     if (issuerRole && m_issuerRole && issuerRole!=m_issuerRole)
96         throw BindingException("Externally provided RoleDescriptor conflicts with policy results.");
97     m_issuerRole=issuerRole;
98 }
99
100 bool SecurityPolicy::IssuerMatchingPolicy::issuerMatches(const Issuer* issuer1, const Issuer* issuer2) const
101 {
102     // NULL matches anything for the purposes of this interface.
103     if (!issuer1 || !issuer2)
104         return true;
105     
106     const XMLCh* op1=issuer1->getName();
107     const XMLCh* op2=issuer2->getName();
108     if (!op1 || !op2 || !XMLString::equals(op1,op2))
109         return false;
110     
111     op1=issuer1->getFormat();
112     op2=issuer2->getFormat();
113     if (!XMLString::equals(op1 ? op1 : NameIDType::ENTITY, op2 ? op2 : NameIDType::ENTITY))
114         return false;
115         
116     op1=issuer1->getNameQualifier();
117     op2=issuer2->getNameQualifier();
118     if (!XMLString::equals(op1 ? op1 : &chNull, op2 ? op2 : &chNull))
119         return false;
120
121     op1=issuer1->getSPNameQualifier();
122     op2=issuer2->getSPNameQualifier();
123     if (!XMLString::equals(op1 ? op1 : &chNull, op2 ? op2 : &chNull))
124         return false;
125     
126     return true;
127 }