89aad2d71f0366893020cef9ce62e42080524728
[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 "saml1/core/Assertions.h"
27 #include "saml1/core/Protocols.h"
28 #include "saml2/core/Assertions.h"
29 #include "saml2/core/Protocols.h"
30
31 using namespace opensaml::saml2md;
32 using namespace opensaml::saml2;
33 using namespace opensaml;
34 using namespace xmltooling;
35 using namespace std;
36
37 namespace opensaml {
38     SAML_DLLLOCAL PluginManager<SecurityPolicyRule,const DOMElement*>::Factory ClientCertAuthRuleFactory;
39     SAML_DLLLOCAL PluginManager<SecurityPolicyRule,const DOMElement*>::Factory MessageFlowRuleFactory;
40     SAML_DLLLOCAL PluginManager<SecurityPolicyRule,const DOMElement*>::Factory SimpleSigningRuleFactory;
41     SAML_DLLLOCAL PluginManager<SecurityPolicyRule,const DOMElement*>::Factory XMLSigningRuleFactory;
42 };
43
44 void SAML_API opensaml::registerSecurityPolicyRules()
45 {
46     SAMLConfig& conf=SAMLConfig::getConfig();
47     conf.SecurityPolicyRuleManager.registerFactory(CLIENTCERTAUTH_POLICY_RULE, ClientCertAuthRuleFactory);
48     conf.SecurityPolicyRuleManager.registerFactory(MESSAGEFLOW_POLICY_RULE, MessageFlowRuleFactory);
49     conf.SecurityPolicyRuleManager.registerFactory(SIMPLESIGNING_POLICY_RULE, SimpleSigningRuleFactory);
50     conf.SecurityPolicyRuleManager.registerFactory(XMLSIGNING_POLICY_RULE, XMLSigningRuleFactory);
51 }
52
53 SecurityPolicy::IssuerMatchingPolicy SecurityPolicy::m_defaultMatching;
54
55 SecurityPolicyRule::MessageExtractor SecurityPolicy::m_defaultExtractor;
56
57 SecurityPolicy::~SecurityPolicy()
58 {
59     delete m_extractor;
60     delete m_matchingPolicy;
61     delete m_issuer;
62 }
63
64 void SecurityPolicy::evaluate(const GenericRequest& request, const XMLObject& message)
65 {
66     for (vector<const SecurityPolicyRule*>::const_iterator i=m_rules.begin(); i!=m_rules.end(); ++i) {
67
68         // Run the rule...
69         pair<Issuer*,const RoleDescriptor*> ident =
70             (*i)->evaluate(request,message,m_metadata,&m_role,m_trust,getMessageExtractor());
71
72         // Make sure returned issuer doesn't conflict.
73          
74         if (ident.first) {
75             if (!getIssuerMatchingPolicy().issuerMatches(ident.first, m_issuer)) {
76                 delete ident.first;
77                 throw BindingException("Policy rules returned differing Issuers.");
78             }
79             delete m_issuer;
80             m_issuer=ident.first;
81         }
82
83         if (ident.second) {
84             if (m_issuerRole && ident.second!=m_issuerRole)
85                 throw BindingException("Policy rules returned differing issuer RoleDescriptors.");
86             m_issuerRole=ident.second;
87         }
88     }
89 }
90
91 void SecurityPolicy::setIssuer(saml2::Issuer* issuer)
92 {
93     if (!getIssuerMatchingPolicy().issuerMatches(issuer, m_issuer)) {
94         delete issuer;
95         throw BindingException("Externally provided Issuer conflicts with policy results.");
96     }
97     
98     delete m_issuer;
99     m_issuer=issuer;
100 }
101
102 void SecurityPolicy::setIssuerMetadata(const RoleDescriptor* issuerRole)
103 {
104     if (issuerRole && m_issuerRole && issuerRole!=m_issuerRole)
105         throw BindingException("Externally provided RoleDescriptor conflicts with policy results.");
106     m_issuerRole=issuerRole;
107 }
108
109 bool SecurityPolicy::IssuerMatchingPolicy::issuerMatches(const Issuer* issuer1, const Issuer* issuer2) const
110 {
111     // NULL matches anything for the purposes of this interface.
112     if (!issuer1 || !issuer2)
113         return true;
114     
115     const XMLCh* op1=issuer1->getName();
116     const XMLCh* op2=issuer2->getName();
117     if (!op1 || !op2 || !XMLString::equals(op1,op2))
118         return false;
119     
120     op1=issuer1->getFormat();
121     op2=issuer2->getFormat();
122     if (!XMLString::equals(op1 ? op1 : NameIDType::ENTITY, op2 ? op2 : NameIDType::ENTITY))
123         return false;
124         
125     op1=issuer1->getNameQualifier();
126     op2=issuer2->getNameQualifier();
127     if (!XMLString::equals(op1 ? op1 : &chNull, op2 ? op2 : &chNull))
128         return false;
129
130     op1=issuer1->getSPNameQualifier();
131     op2=issuer2->getSPNameQualifier();
132     if (!XMLString::equals(op1 ? op1 : &chNull, op2 ? op2 : &chNull))
133         return false;
134     
135     return true;
136 }
137
138
139 pair<saml2::Issuer*,const XMLCh*> SecurityPolicyRule::MessageExtractor::getIssuerAndProtocol(const XMLObject& message) const
140 {
141     // We just let any bad casts throw here.
142     
143     saml2::Issuer* issuer;
144
145     // Shortcuts some of the casting.
146     const XMLCh* ns = message.getElementQName().getNamespaceURI();
147     if (ns) {
148         if (XMLString::equals(ns, samlconstants::SAML20P_NS) || XMLString::equals(ns, samlconstants::SAML20_NS)) {
149             // 2.0 namespace should be castable to a specialized 2.0 root.
150             const saml2::RootObject& root = dynamic_cast<const saml2::RootObject&>(message);
151             issuer = root.getIssuer();
152             if (issuer && issuer->getName()) {
153                 return make_pair(issuer->cloneIssuer(), samlconstants::SAML20P_NS);
154             }
155             
156             // No issuer in the message, so we have to try the Response approach. 
157             const vector<saml2::Assertion*>& assertions = dynamic_cast<const saml2p::Response&>(message).getAssertions();
158             if (!assertions.empty()) {
159                 issuer = assertions.front()->getIssuer();
160                 if (issuer && issuer->getName())
161                     return make_pair(issuer->cloneIssuer(), samlconstants::SAML20P_NS);
162             }
163         }
164         else if (XMLString::equals(ns, samlconstants::SAML1P_NS)) {
165             // Should be a samlp:Response, at least in OpenSAML.
166             const vector<saml1::Assertion*>& assertions = dynamic_cast<const saml1p::Response&>(message).getAssertions();
167             if (!assertions.empty()) {
168                 const saml1::Assertion* a = assertions.front();
169                 if (a->getIssuer()) {
170                     issuer = saml2::IssuerBuilder::buildIssuer();
171                     issuer->setName(a->getIssuer());
172                     pair<bool,int> minor = a->getMinorVersion();
173                     return make_pair(
174                         issuer,
175                         (minor.first && minor.second==0) ? samlconstants::SAML10_PROTOCOL_ENUM : samlconstants::SAML11_PROTOCOL_ENUM
176                         );
177                 }
178             }
179         }
180         else if (XMLString::equals(ns, samlconstants::SAML1_NS)) {
181             // Should be a saml:Assertion.
182             const saml1::Assertion& a = dynamic_cast<const saml1::Assertion&>(message);
183             if (a.getIssuer()) {
184                 issuer = saml2::IssuerBuilder::buildIssuer();
185                 issuer->setName(a.getIssuer());
186                 pair<bool,int> minor = a.getMinorVersion();
187                 return make_pair(
188                     issuer,
189                     (minor.first && minor.second==0) ? samlconstants::SAML10_PROTOCOL_ENUM : samlconstants::SAML11_PROTOCOL_ENUM
190                     );
191             }
192         }
193     }
194     return pair<saml2::Issuer*,const XMLCh*>(NULL,NULL);
195 }