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