Allow message-only policy rules, basic SAML SOAP client.
[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 xmltooling::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. The lifetime of the policy rules
71          * must be at least as long as the policy object.
72          *
73          * @param rules             reference to array of policy rules to use 
74          * @param metadataProvider  locked MetadataProvider instance
75          * @param role              identifies the role (generally IdP or SP) of the policy peer 
76          * @param trustEngine       TrustEngine to authenticate policy peer
77          */
78         SecurityPolicy(
79             const std::vector<const SecurityPolicyRule*>& rules,
80             const saml2md::MetadataProvider* metadataProvider=NULL,
81             const xmltooling::QName* role=NULL,
82             const xmltooling::TrustEngine* trustEngine=NULL
83             ) : m_issuer(NULL), m_issuerRole(NULL), m_matchingPolicy(NULL), m_rules(rules), m_metadata(metadataProvider),
84                 m_role(role ? *role : xmltooling::QName()), m_trust(trustEngine) {
85         }
86
87         virtual ~SecurityPolicy();
88
89         /**
90          * Returns the locked MetadataProvider supplied to the policy.
91          * 
92          * @return the supplied MetadataProvider or NULL
93          */
94         const saml2md::MetadataProvider* getMetadataProvider() const {
95             return m_metadata;
96         }
97
98         /**
99          * Returns the peer role element/type supplied to the policy.
100          * 
101          * @return the peer role element/type, or an empty QName
102          */
103         const xmltooling::QName* getRole() const {
104             return &m_role;
105         }
106
107         /**
108          * Returns the TrustEngine supplied to the policy.
109          * 
110          * @return the supplied TrustEngine or NULL
111          */
112         const xmltooling::TrustEngine* getTrustEngine() const {
113             return m_trust;
114         }
115
116         /**
117          * Adds a SecurityPolicyRule to the policy. The lifetime of the policy rule
118          * must be at least as long as the policy object.
119          * 
120          * @param rule  SecurityPolicyRule to add
121          */
122         void addRule(const SecurityPolicyRule* rule) {
123             m_rules.push_back(rule);
124         }
125
126         /**
127          * Sets a locked MetadataProvider for the policy.
128          * 
129          * @param metadata a locked MetadataProvider or NULL
130          */
131         void setMetadataProvider(const saml2md::MetadataProvider* metadata) {
132             m_metadata = metadata;
133         }
134
135         /**
136          * Sets a peer role element/type for to the policy.
137          * 
138          * @param role the peer role element/type or NULL
139          */
140         void setRole(const xmltooling::QName* role) {
141             m_role = (role ? *role : xmltooling::QName());
142         }
143
144         /**
145          * Sets a TrustEngine for the policy.
146          * 
147          * @param trust a TrustEngine or NULL
148          */
149         void setTrustEngine(const xmltooling::TrustEngine* trust) {
150             m_trust = trust;
151         }
152
153         /**
154          * Evaluates the policy against the given request and message,
155          * possibly populating issuer information in the policy object.
156          * 
157          * @param message           the incoming message
158          * @param request           the protocol request
159          * 
160          * @throws BindingException thrown if the request/message do not meet the requirements of this policy
161          */
162         void evaluate(const xmltooling::XMLObject& message, const GenericRequest* request=NULL);
163
164         /**
165          * Gets the issuer of the message as determined by the registered policies.
166          * 
167          * @return issuer of the message as determined by the registered policies
168          */
169         const saml2::Issuer* getIssuer() const {
170             return m_issuer;
171         }
172
173         /**
174          * Gets the metadata for the role the issuer is operating in.
175          * 
176          * @return metadata for the role the issuer is operating in
177          */
178         const saml2md::RoleDescriptor* getIssuerMetadata() const {
179             return m_issuerRole;
180         }
181
182         /**
183          * Sets the issuer of the message as determined by external factors.
184          * The policy object takes ownership of the Issuer object.
185          * 
186          * @param issuer issuer of the message
187          */
188         void setIssuer(saml2::Issuer* issuer);
189         
190         /**
191          * Sets the metadata for the role the issuer is operating in.
192          * 
193          * @param issuerRole metadata for the role the issuer is operating in
194          */
195         void setIssuerMetadata(const saml2md::RoleDescriptor* issuerRole);
196         
197         /** Allows override of rules for comparing saml2:Issuer information. */
198         class SAML_API IssuerMatchingPolicy {
199             MAKE_NONCOPYABLE(IssuerMatchingPolicy);
200         public:
201             IssuerMatchingPolicy() {}
202             virtual ~IssuerMatchingPolicy() {}
203             
204             /**
205              * Returns true iff the two operands "match". Applications can override this method to
206              * support non-standard issuer matching for complex policies. 
207              * 
208              * <p>The default implementation does a basic comparison of the XML content, treating
209              * an unsupplied Format as an "entityID".
210              * 
211              * @param issuer1   the first Issuer to match
212              * @param issuer2   the second Issuer to match
213              * @return  true iff the operands match
214              */
215             virtual bool issuerMatches(const saml2::Issuer* issuer1, const saml2::Issuer* issuer2) const;
216         };
217
218         /**
219          * Returns the IssuerMatchingPolicy in effect.
220          * 
221          * @return the effective IssuerMatchingPolicy
222          */
223         const IssuerMatchingPolicy& getIssuerMatchingPolicy() const {
224             return m_matchingPolicy ? *m_matchingPolicy : m_defaultMatching;
225         }
226
227         /**
228          * Sets the IssuerMatchingPolicy in effect. Setting no policy will
229          * cause the simple, default approach to be used.
230          * 
231          * <p>The matching object will be freed by the SecurityPolicy.
232          * 
233          * @param matchingPolicy the IssuerMatchingPolicy to use
234          */
235         void setIssuerMatchingPolicy(IssuerMatchingPolicy* matchingPolicy) {
236             delete m_matchingPolicy;
237             m_matchingPolicy = matchingPolicy;
238         }
239
240     protected:
241         /** A shared matching object that just supports the default matching rules. */
242         static IssuerMatchingPolicy m_defaultMatching;
243
244     private:
245         saml2::Issuer* m_issuer;
246         const saml2md::RoleDescriptor* m_issuerRole;
247         
248         IssuerMatchingPolicy* m_matchingPolicy;
249         std::vector<const SecurityPolicyRule*> m_rules;
250         const saml2md::MetadataProvider* m_metadata;
251         xmltooling::QName m_role;
252         const xmltooling::TrustEngine* m_trust;
253     };
254
255 };
256
257 #if defined (_MSC_VER)
258     #pragma warning( pop )
259 #endif
260
261 #endif /* __saml_secpol_h__ */