https://issues.shibboleth.net/jira/browse/SSPCPP-132
[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,string,const DOMElement*>::Factory ClientCertAuthRuleFactory;
36     SAML_DLLLOCAL PluginManager<SecurityPolicyRule,string,const DOMElement*>::Factory MessageFlowRuleFactory;
37     SAML_DLLLOCAL PluginManager<SecurityPolicyRule,string,const DOMElement*>::Factory NullSecurityRuleFactory;
38     SAML_DLLLOCAL PluginManager<SecurityPolicyRule,string,const DOMElement*>::Factory SimpleSigningRuleFactory;
39     SAML_DLLLOCAL PluginManager<SecurityPolicyRule,string,const DOMElement*>::Factory XMLSigningRuleFactory;
40 };
41
42 void SAML_API opensaml::registerSecurityPolicyRules()
43 {
44     SAMLConfig& conf=SAMLConfig::getConfig();
45     conf.SecurityPolicyRuleManager.registerFactory(CLIENTCERTAUTH_POLICY_RULE, ClientCertAuthRuleFactory);
46     conf.SecurityPolicyRuleManager.registerFactory(MESSAGEFLOW_POLICY_RULE, MessageFlowRuleFactory);
47     conf.SecurityPolicyRuleManager.registerFactory(NULLSECURITY_POLICY_RULE, NullSecurityRuleFactory);
48     conf.SecurityPolicyRuleManager.registerFactory(SIMPLESIGNING_POLICY_RULE, SimpleSigningRuleFactory);
49     conf.SecurityPolicyRuleManager.registerFactory(XMLSIGNING_POLICY_RULE, XMLSigningRuleFactory);
50 }
51
52 SecurityPolicy::IssuerMatchingPolicy SecurityPolicy::m_defaultMatching;
53
54 SecurityPolicy::~SecurityPolicy()
55 {
56     reset(false);
57 }
58
59 void SecurityPolicy::reset(bool messageOnly)
60 {
61     XMLString::release(&m_messageID);
62     m_messageID=NULL;
63     m_issueInstant=0;
64     delete m_metadataCriteria;
65     m_metadataCriteria=NULL;
66     if (!messageOnly) {
67         delete m_issuer;
68         m_issuer=NULL;
69         m_issuerRole=NULL;
70         m_authenticated=false;
71     }
72 }
73
74 MetadataProvider::Criteria& SecurityPolicy::getMetadataProviderCriteria() const
75 {
76     if (!m_metadataCriteria)
77         m_metadataCriteria=new MetadataProvider::Criteria();
78     return *m_metadataCriteria;
79 }
80
81 void SecurityPolicy::evaluate(const XMLObject& message, const GenericRequest* request)
82 {
83     for (vector<const SecurityPolicyRule*>::const_iterator i=m_rules.begin(); i!=m_rules.end(); ++i)
84         (*i)->evaluate(message,request,*this);
85 }
86
87 void SecurityPolicy::setIssuer(const Issuer* issuer)
88 {
89     if (!getIssuerMatchingPolicy().issuerMatches(m_issuer, issuer))
90         throw SecurityPolicyException("An Issuer was supplied that conflicts with previous results.");
91
92     if (!m_issuer) {
93         if (m_entityOnly && issuer->getFormat() && !XMLString::equals(issuer->getFormat(), NameIDType::ENTITY))
94             throw SecurityPolicyException("A non-entity Issuer was supplied, violating policy.");
95         m_issuerRole = NULL;
96         m_issuer=issuer->cloneIssuer();
97     }
98 }
99
100 void SecurityPolicy::setIssuer(const XMLCh* issuer)
101 {
102     if (!getIssuerMatchingPolicy().issuerMatches(m_issuer, issuer))
103         throw SecurityPolicyException("An Issuer was supplied that conflicts with previous results.");
104
105     if (!m_issuer && issuer && *issuer) {
106         m_issuerRole = NULL;
107         m_issuer = IssuerBuilder::buildIssuer();
108         m_issuer->setName(issuer);
109     }
110 }
111
112 void SecurityPolicy::setIssuerMetadata(const RoleDescriptor* issuerRole)
113 {
114     if (issuerRole && m_issuerRole && issuerRole!=m_issuerRole)
115         throw SecurityPolicyException("A rule supplied a RoleDescriptor that conflicts with previous results.");
116     m_issuerRole=issuerRole;
117 }
118
119 bool SecurityPolicy::IssuerMatchingPolicy::issuerMatches(const Issuer* issuer1, const Issuer* issuer2) const
120 {
121     // NULL matches anything for the purposes of this interface.
122     if (!issuer1 || !issuer2)
123         return true;
124
125     const XMLCh* op1=issuer1->getName();
126     const XMLCh* op2=issuer2->getName();
127     if (!op1 || !op2 || !XMLString::equals(op1,op2))
128         return false;
129
130     op1=issuer1->getFormat();
131     op2=issuer2->getFormat();
132     if (!XMLString::equals(op1 ? op1 : NameIDType::ENTITY, op2 ? op2 : NameIDType::ENTITY))
133         return false;
134
135     op1=issuer1->getNameQualifier();
136     op2=issuer2->getNameQualifier();
137     if (!XMLString::equals(op1 ? op1 : &chNull, op2 ? op2 : &chNull))
138         return false;
139
140     op1=issuer1->getSPNameQualifier();
141     op2=issuer2->getSPNameQualifier();
142     if (!XMLString::equals(op1 ? op1 : &chNull, op2 ? op2 : &chNull))
143         return false;
144
145     return true;
146 }
147
148 bool SecurityPolicy::IssuerMatchingPolicy::issuerMatches(const Issuer* issuer1, const XMLCh* issuer2) const
149 {
150     // NULL matches anything for the purposes of this interface.
151     if (!issuer1 || !issuer2 || !*issuer2)
152         return true;
153
154     const XMLCh* op1=issuer1->getName();
155     if (!op1 || !XMLString::equals(op1,issuer2))
156         return false;
157
158     op1=issuer1->getFormat();
159     if (op1 && *op1 && !XMLString::equals(op1, NameIDType::ENTITY))
160         return false;
161
162     op1=issuer1->getNameQualifier();
163     if (op1 && *op1)
164         return false;
165
166     op1=issuer1->getSPNameQualifier();
167     if (op1 && *op1)
168         return false;
169
170     return true;
171 }