6e9b6aebf251e055dfe814b11459548671c0e706
[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 MessageRoutingRuleFactory;
37     SAML_DLLLOCAL PluginManager<SecurityPolicyRule,const DOMElement*>::Factory MessageSigningRuleFactory;
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(MESSAGEROUTING_POLICY_RULE, MessageRoutingRuleFactory);
45     conf.SecurityPolicyRuleManager.registerFactory(MESSAGESIGNING_POLICY_RULE, MessageSigningRuleFactory);
46 }
47
48 SecurityPolicy::~SecurityPolicy()
49 {
50     delete m_issuer;
51 }
52
53 void SecurityPolicy::evaluate(const GenericRequest& request, const XMLObject& message)
54 {
55     for (vector<const SecurityPolicyRule*>::const_iterator i=m_rules.begin(); i!=m_rules.end(); ++i) {
56
57         // Run the rule...
58         pair<Issuer*,const RoleDescriptor*> ident = (*i)->evaluate(request,message,m_metadata,&m_role,m_trust);
59
60         // Make sure returned issuer doesn't conflict.
61          
62         if (ident.first) {
63             if (!issuerMatches(ident.first, m_issuer)) {
64                 delete ident.first;
65                 throw BindingException("Policy rules returned differing Issuers.");
66             }
67             delete m_issuer;
68             m_issuer=ident.first;
69         }
70
71         if (ident.second) {
72             if (m_issuerRole && ident.second!=m_issuerRole)
73                 throw BindingException("Policy rules returned differing issuer RoleDescriptors.");
74             m_issuerRole=ident.second;
75         }
76     }
77 }
78
79 void SecurityPolicy::setIssuer(saml2::Issuer* issuer)
80 {
81     if (!issuerMatches(issuer, m_issuer)) {
82         delete issuer;
83         throw BindingException("Externally provided Issuer conflicts with policy results.");
84     }
85     
86     delete m_issuer;
87     m_issuer=issuer;
88 }
89
90 void SecurityPolicy::setIssuerMetadata(const RoleDescriptor* issuerRole)
91 {
92     if (issuerRole && m_issuerRole && issuerRole!=m_issuerRole)
93         throw BindingException("Externally provided RoleDescriptor conflicts with policy results.");
94     m_issuerRole=issuerRole;
95 }
96
97 bool SecurityPolicy::issuerMatches(const Issuer* issuer1, const Issuer* issuer2) const
98 {
99     // NULL matches anything for the purposes of this interface.
100     if (!issuer1 || !issuer2)
101         return true;
102     
103     const XMLCh* op1=issuer1->getName();
104     const XMLCh* op2=issuer2->getName();
105     if (!op1 || !op2 || !XMLString::equals(op1,op2))
106         return false;
107     
108     op1=issuer1->getFormat();
109     op2=issuer2->getFormat();
110     if (!XMLString::equals(op1 ? op1 : NameIDType::ENTITY, op2 ? op2 : NameIDType::ENTITY))
111         return false;
112         
113     op1=issuer1->getNameQualifier();
114     op2=issuer2->getNameQualifier();
115     if (!XMLString::equals(op1 ? op1 : &chNull, op2 ? op2 : &chNull))
116         return false;
117
118     op1=issuer1->getSPNameQualifier();
119     op2=issuer2->getSPNameQualifier();
120     if (!XMLString::equals(op1 ? op1 : &chNull, op2 ? op2 : &chNull))
121         return false;
122     
123     return true;
124 }