ef98f7ba17d312873c7dac774757148f6c7cb122
[shibboleth/cpp-opensaml.git] / saml / binding / impl / SecurityPolicy.cpp
1 /*
2  *  Copyright 2001-2007 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/SecurityPolicyRule.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 ClientCertAuthRuleFactory;
36     SAML_DLLLOCAL PluginManager<SecurityPolicyRule,const DOMElement*>::Factory MessageFlowRuleFactory;
37     SAML_DLLLOCAL PluginManager<SecurityPolicyRule,const DOMElement*>::Factory SAML1MessageRuleFactory;
38     SAML_DLLLOCAL PluginManager<SecurityPolicyRule,const DOMElement*>::Factory SAML2MessageRuleFactory;
39     SAML_DLLLOCAL PluginManager<SecurityPolicyRule,const DOMElement*>::Factory SimpleSigningRuleFactory;
40     SAML_DLLLOCAL PluginManager<SecurityPolicyRule,const DOMElement*>::Factory XMLSigningRuleFactory;
41 };
42
43 void SAML_API opensaml::registerSecurityPolicyRules()
44 {
45     SAMLConfig& conf=SAMLConfig::getConfig();
46     conf.SecurityPolicyRuleManager.registerFactory(CLIENTCERTAUTH_POLICY_RULE, ClientCertAuthRuleFactory);
47     conf.SecurityPolicyRuleManager.registerFactory(MESSAGEFLOW_POLICY_RULE, MessageFlowRuleFactory);
48     conf.SecurityPolicyRuleManager.registerFactory(SAML1MESSAGE_POLICY_RULE, SAML1MessageRuleFactory);
49     conf.SecurityPolicyRuleManager.registerFactory(SAML2MESSAGE_POLICY_RULE, SAML2MessageRuleFactory);
50     conf.SecurityPolicyRuleManager.registerFactory(SIMPLESIGNING_POLICY_RULE, SimpleSigningRuleFactory);
51     conf.SecurityPolicyRuleManager.registerFactory(XMLSIGNING_POLICY_RULE, XMLSigningRuleFactory);
52 }
53
54 SecurityPolicy::IssuerMatchingPolicy SecurityPolicy::m_defaultMatching;
55
56 SecurityPolicy::~SecurityPolicy()
57 {
58     reset();
59 }
60
61 void SecurityPolicy::reset()
62 {
63     delete m_messageQName;
64     XMLString::release(&m_messageID);
65     delete m_issuer;
66     m_messageQName=NULL;
67     m_messageID=NULL;
68     m_issueInstant=0;
69     m_issuer=NULL;
70     m_issuerRole=NULL;
71     m_secure=false;
72 }
73
74 void SecurityPolicy::evaluate(const XMLObject& message, const GenericRequest* request)
75 {
76     for (vector<const SecurityPolicyRule*>::const_iterator i=m_rules.begin(); i!=m_rules.end(); ++i)
77         (*i)->evaluate(message,request,*this);
78 }
79
80 void SecurityPolicy::setIssuer(saml2::Issuer* issuer)
81 {
82     if (!getIssuerMatchingPolicy().issuerMatches(issuer, m_issuer))
83         throw SecurityPolicyException("A rule supplied an Issuer that conflicts with previous results.");
84     
85     delete m_issuer;
86     m_issuer=issuer;
87 }
88
89 void SecurityPolicy::setIssuerMetadata(const RoleDescriptor* issuerRole)
90 {
91     if (issuerRole && m_issuerRole && issuerRole!=m_issuerRole)
92         throw SecurityPolicyException("A rule supplied a RoleDescriptor that conflicts with previous results.");
93     m_issuerRole=issuerRole;
94 }
95
96 bool SecurityPolicy::IssuerMatchingPolicy::issuerMatches(const Issuer* issuer1, const Issuer* issuer2) const
97 {
98     // NULL matches anything for the purposes of this interface.
99     if (!issuer1 || !issuer2)
100         return true;
101     
102     const XMLCh* op1=issuer1->getName();
103     const XMLCh* op2=issuer2->getName();
104     if (!op1 || !op2 || !XMLString::equals(op1,op2))
105         return false;
106     
107     op1=issuer1->getFormat();
108     op2=issuer2->getFormat();
109     if (!XMLString::equals(op1 ? op1 : NameIDType::ENTITY, op2 ? op2 : NameIDType::ENTITY))
110         return false;
111         
112     op1=issuer1->getNameQualifier();
113     op2=issuer2->getNameQualifier();
114     if (!XMLString::equals(op1 ? op1 : &chNull, op2 ? op2 : &chNull))
115         return false;
116
117     op1=issuer1->getSPNameQualifier();
118     op2=issuer2->getSPNameQualifier();
119     if (!XMLString::equals(op1 ? op1 : &chNull, op2 ? op2 : &chNull))
120         return false;
121     
122     return true;
123 }