Moved dest. check back to decoders, policy API changes.
[shibboleth/cpp-opensaml.git] / saml / binding / SecurityPolicy.h
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  * @file saml/binding/SecurityPolicy.h
19  * 
20  * Overall policy used to verify the security of an incoming message.
21  */
22
23 #ifndef __saml_secpol_h__
24 #define __saml_secpol_h__
25
26 #include <saml/binding/SecurityPolicyRule.h>
27 #include <vector>
28
29 #if defined (_MSC_VER)
30     #pragma warning( push )
31     #pragma warning( disable : 4250 4251 )
32 #endif
33
34 namespace opensaml {
35
36     namespace saml2md {
37         class SAML_API MetadataProvider;
38     };
39     
40     /**
41      * A policy used to verify the security of an incoming message.
42      * 
43      * <p>Its security mechanisms may be used to examine the transport layer
44      * (e.g client certificates and HTTP basic auth passwords) or to check the
45      * payload of a request to ensure it meets certain criteria (e.g. valid
46      * digital signature, freshness, replay).
47      * 
48      * <p>Policy objects can be reused, but are not thread-safe. 
49      */
50     class SAML_API SecurityPolicy
51     {
52         MAKE_NONCOPYABLE(SecurityPolicy);
53     public:
54         /**
55          * Constructor for policy.
56          *
57          * @param metadataProvider  locked MetadataProvider instance
58          * @param role              identifies the role (generally IdP or SP) of the policy peer 
59          * @param trustEngine       TrustEngine to authenticate policy peer
60          */
61         SecurityPolicy(
62             const saml2md::MetadataProvider* metadataProvider=NULL,
63             const xmltooling::QName* role=NULL,
64             const TrustEngine* trustEngine=NULL
65             ) : m_issuer(NULL), m_issuerRole(NULL), m_matchingPolicy(NULL), m_metadata(metadataProvider),
66                 m_role(role ? *role : xmltooling::QName()), m_trust(trustEngine) {
67         }
68
69         /**
70          * Constructor for policy using existing rules.
71          *
72          * @param rules             reference to array of policy rules to use 
73          * @param metadataProvider  locked MetadataProvider instance
74          * @param role              identifies the role (generally IdP or SP) of the policy peer 
75          * @param trustEngine       TrustEngine to authenticate policy peer
76          */
77         SecurityPolicy(
78             const std::vector<const SecurityPolicyRule*>& rules,
79             const saml2md::MetadataProvider* metadataProvider=NULL,
80             const xmltooling::QName* role=NULL,
81             const TrustEngine* trustEngine=NULL
82             ) : m_issuer(NULL), m_issuerRole(NULL), m_matchingPolicy(NULL), m_rules(rules), m_metadata(metadataProvider),
83                 m_role(role ? *role : xmltooling::QName()), m_trust(trustEngine) {
84         }
85
86         virtual ~SecurityPolicy();
87
88         /**
89          * Returns the locked MetadataProvider supplied to the policy.
90          * 
91          * @return the supplied MetadataProvider or NULL
92          */
93         const saml2md::MetadataProvider* getMetadataProvider() const {
94             return m_metadata;
95         }
96
97         /**
98          * Returns the peer role element/type supplied to the policy.
99          * 
100          * @return the peer role element/type, or an empty QName
101          */
102         const xmltooling::QName* getRole() const {
103             return &m_role;
104         }
105
106         /**
107          * Returns the TrustEngine supplied to the policy.
108          * 
109          * @return the supplied TrustEngine or NULL
110          */
111         const TrustEngine* getTrustEngine() const {
112             return m_trust;
113         }
114
115         /**
116          * Sets a locked MetadataProvider for the policy.
117          * 
118          * @param metadata a locked MetadataProvider or NULL
119          */
120         void setMetadataProvider(const saml2md::MetadataProvider* metadata) {
121             m_metadata = metadata;
122         }
123
124         /**
125          * Sets a peer role element/type for to the policy.
126          * 
127          * @param role the peer role element/type or NULL
128          */
129         void setRole(const xmltooling::QName* role) {
130             m_role = (role ? *role : xmltooling::QName());
131         }
132
133         /**
134          * Sets a TrustEngine for the policy.
135          * 
136          * @param trust a TrustEngine or NULL
137          */
138         void setTrustEngine(const TrustEngine* trust) {
139             m_trust = trust;
140         }
141
142         /**
143          * Evaluates the rule against the given request and message,
144          * possibly populating issuer information in the policy object.
145          * 
146          * @param request           the protocol request
147          * @param message           the incoming message
148          * @return the identity of the message issuer, in one or more of two forms, or NULL
149          * 
150          * @throws BindingException thrown if the request/message do not meet the requirements of this rule
151          */
152         void evaluate(const GenericRequest& request, const xmltooling::XMLObject& message);
153
154         /**
155          * Gets the issuer of the message as determined by the registered policies.
156          * 
157          * @return issuer of the message as determined by the registered policies
158          */
159         const saml2::Issuer* getIssuer() const {
160             return m_issuer;
161         }
162
163         /**
164          * Gets the metadata for the role the issuer is operating in.
165          * 
166          * @return metadata for the role the issuer is operating in
167          */
168         const saml2md::RoleDescriptor* getIssuerMetadata() const {
169             return m_issuerRole;
170         }
171
172         /**
173          * Sets the issuer of the message as determined by external factors.
174          * The policy object takes ownership of the Issuer object.
175          * 
176          * @param issuer issuer of the message
177          */
178         void setIssuer(saml2::Issuer* issuer);
179         
180         /**
181          * Sets the metadata for the role the issuer is operating in.
182          * 
183          * @param issuerRole metadata for the role the issuer is operating in
184          */
185         void setIssuerMetadata(const saml2md::RoleDescriptor* issuerRole);
186         
187         /** Allows override of rules for comparing saml2:Issuer information. */
188         class SAML_API IssuerMatchingPolicy {
189             MAKE_NONCOPYABLE(IssuerMatchingPolicy);
190         public:
191             IssuerMatchingPolicy() {}
192             virtual ~IssuerMatchingPolicy() {}
193             
194             /**
195              * Returns true iff the two operands "match". Applications can override this method to
196              * support non-standard issuer matching for complex policies. 
197              * 
198              * <p>The default implementation does a basic comparison of the XML content, treating
199              * an unsupplied Format as an "entityID".
200              * 
201              * @param issuer1   the first Issuer to match
202              * @param issuer2   the second Issuer to match
203              * @return  true iff the operands match
204              */
205             virtual bool issuerMatches(const saml2::Issuer* issuer1, const saml2::Issuer* issuer2) const;
206         };
207
208         /**
209          * Returns the IssuerMatchingPolicy in effect.
210          * 
211          * @return the effective IssuerMatchingPolicy
212          */
213         const IssuerMatchingPolicy* getIssuerMatchingPolicy() const {
214             return m_matchingPolicy ? m_matchingPolicy : &m_defaultMatching;
215         }
216
217         /**
218          * Sets the IssuerMatchingPolicy in effect. Setting no policy will
219          * cause the simple, default approach to be used.
220          * 
221          * <p>The matching object will be freed by the SecurityPolicy.
222          * 
223          * @param matchingPolicy the IssuerMatchingPolicy to use
224          */
225         void getIssuerMatchingPolicy(IssuerMatchingPolicy* matchingPolicy) {
226             delete m_matchingPolicy;
227             m_matchingPolicy = matchingPolicy;
228         }
229
230     protected:
231         /** A shared matching object that just supports the default matching rules. */
232         static IssuerMatchingPolicy m_defaultMatching;
233
234     private:
235         saml2::Issuer* m_issuer;
236         const saml2md::RoleDescriptor* m_issuerRole;
237         
238         IssuerMatchingPolicy* m_matchingPolicy;
239         std::vector<const SecurityPolicyRule*> m_rules;
240         const saml2md::MetadataProvider* m_metadata;
241         xmltooling::QName m_role;
242         const TrustEngine* m_trust;
243     };
244
245 };
246
247 #if defined (_MSC_VER)
248     #pragma warning( pop )
249 #endif
250
251 #endif /* __saml_secpol_h__ */