2 * Copyright 2001-2009 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file saml/binding/SecurityPolicy.h
20 * Overall policy used to verify the security of an incoming message.
23 #ifndef __saml_secpol_h__
24 #define __saml_secpol_h__
26 #include <saml/saml2/metadata/MetadataProvider.h>
30 #include <xmltooling/io/GenericRequest.h>
31 #include <xmltooling/security/TrustEngine.h>
33 #if defined (_MSC_VER)
34 #pragma warning( push )
35 #pragma warning( disable : 4250 4251 )
41 class SAML_API Issuer;
44 class SAML_API SecurityPolicyRule;
47 * A policy used to verify the security of an incoming message.
49 * <p>Its security mechanisms may be used to examine the transport layer
50 * (e.g client certificates and HTTP basic auth passwords) or to check the
51 * payload of a request to ensure it meets certain criteria (e.g. valid
52 * digital signature, freshness, replay).
54 * <p>Policy objects can be reused, but are not thread-safe.
56 class SAML_API SecurityPolicy
58 MAKE_NONCOPYABLE(SecurityPolicy);
61 * Constructor for policy.
63 * @param metadataProvider locked MetadataProvider instance
64 * @param role identifies the role (generally IdP or SP) of the policy peer
65 * @param trustEngine TrustEngine to authenticate policy peer
66 * @param validate true iff XML parsing should be done with validation
69 const saml2md::MetadataProvider* metadataProvider=NULL,
70 const xmltooling::QName* role=NULL,
71 const xmltooling::TrustEngine* trustEngine=NULL,
75 virtual ~SecurityPolicy();
78 * Returns the locked MetadataProvider supplied to the policy.
80 * @return the supplied MetadataProvider or NULL
82 const saml2md::MetadataProvider* getMetadataProvider() const {
87 * Returns a reference to a MetadataProvider::Criteria instance suitable for use with the
88 * installed MetadataProvider.
90 * <p>The object will be cleared/reset when returned, so do not mutate it and then
91 * call the method again before using it.
93 * @return reference to a MetadataProvider::Criteria instance
95 virtual saml2md::MetadataProvider::Criteria& getMetadataProviderCriteria() const;
98 * Returns the peer role element/type supplied to the policy.
100 * @return the peer role element/type, or an empty QName
102 const xmltooling::QName* getRole() const {
107 * Returns the TrustEngine supplied to the policy.
109 * @return the supplied TrustEngine or NULL
111 const xmltooling::TrustEngine* getTrustEngine() const {
116 * Returns XML message validation setting.
118 * @return validation flag
120 bool getValidating() const {
125 * Returns flag controlling non-entity issuer support.
127 * @return flag controlling non-entity issuer support
129 bool requireEntityIssuer() const {
134 * Returns the SAML audiences that represent the receiving peer.
136 * @return audience values of the peer processing the message
138 const std::vector<const XMLCh*>& getAudiences() const {
143 * Returns the SAML audiences that represent the receiving peer.
145 * @return audience values of the peer processing the message
147 std::vector<const XMLCh*>& getAudiences() {
152 * Gets the effective time of message processing.
154 * @return the time at which the message is being processed
156 time_t getTime() const {
158 return m_ts = time(NULL);
163 * Gets a mutable array of installed policy rules.
165 * <p>If adding rules, their lifetime must be at least as long as the policy object.
167 * @return mutable array of rules
169 std::vector<const SecurityPolicyRule*>& getRules() {
174 * Sets a locked MetadataProvider for the policy.
176 * @param metadata a locked MetadataProvider or NULL
178 void setMetadataProvider(const saml2md::MetadataProvider* metadata) {
179 m_metadata = metadata;
183 * Sets a MetadataProvider::Criteria instance suitable for use with the
184 * installed MetadataProvider.
186 * <p>The policy will take ownership of the criteria object when this
189 * @param criteria a MetadataProvider::Criteria instance, or NULL
191 void setMetadataProviderCriteria(saml2md::MetadataProvider::Criteria* criteria);
194 * Sets a peer role element/type for to the policy.
196 * @param role the peer role element/type or NULL
198 void setRole(const xmltooling::QName* role) {
200 m_role = role ? new xmltooling::QName(*role) : NULL;
204 * Sets a TrustEngine for the policy.
206 * @param trust a TrustEngine or NULL
208 void setTrustEngine(const xmltooling::TrustEngine* trust) {
213 * Controls schema validation of incoming XML messages.
214 * This is separate from other forms of programmatic validation of objects,
215 * but can detect a much wider range of syntax errors.
217 * @param validate validation setting
219 void setValidating(bool validate=true) {
220 m_validate = validate;
224 * Sets flag controlling non-entity issuer support.
226 * @param entityOnly require that Issuer be in entity format
228 void requireEntityIssuer(bool entityOnly=true) {
229 m_entityOnly = entityOnly;
233 * Sets effective time of message processing.
235 * <p>Assumed to be the time of policy instantiation, can be adjusted to pre- or post-date
236 * message processing.
238 * @param ts the time at which the message is being processed
240 void setTime(time_t ts) {
245 * Evaluates the policy against the given request and message,
246 * possibly populating message information in the policy object.
248 * @param message the incoming message
249 * @param request the protocol request
251 * @throws BindingException raised if the message/request is invalid according to the supplied rules
254 const xmltooling::XMLObject& message, const xmltooling::GenericRequest* request=NULL
258 * Resets the policy object and/or clears any per-message state.
260 * <p>Resets can be complete (the default) or merely clear the previous message ID and timestamp
261 * when evaluating multiple layers of a message.
263 * @param messageOnly true iff security and issuer state should be left in place
265 virtual void reset(bool messageOnly=false);
268 * Resets the policy object and/or clears any per-message state for only this specific class.
270 * <p>Resets can be complete (the default) or merely clear the previous message ID and timestamp
271 * when evaluating multiple layers of a message.
273 * @param messageOnly true iff security and issuer state should be left in place
275 void _reset(bool messageOnly=false);
278 * Returns the message identifier as determined by the registered policies.
280 * @return message identifier as determined by the registered policies
282 const XMLCh* getMessageID() const {
287 * Returns the message timestamp as determined by the registered policies.
289 * @return message timestamp as determined by the registered policies
291 time_t getIssueInstant() const {
292 return m_issueInstant;
296 * Gets the issuer of the message as determined by the registered policies.
298 * @return issuer of the message as determined by the registered policies
300 const saml2::Issuer* getIssuer() const {
305 * Gets the metadata for the role the issuer is operating in.
307 * @return metadata for the role the issuer is operating in
309 const saml2md::RoleDescriptor* getIssuerMetadata() const {
314 * Returns the authentication status of the message as determined by the registered policies.
316 * @return true iff a SecurityPolicyRule has indicated the issuer/message has been authenticated
318 bool isAuthenticated() const {
319 return m_authenticated;
323 * Sets the message identifier as determined by the registered policies.
325 * @param id message identifier
327 void setMessageID(const XMLCh* id) {
328 xercesc::XMLString::release(&m_messageID);
329 m_messageID = xercesc::XMLString::replicate(id);
333 * Sets the message timestamp as determined by the registered policies.
335 * @param issueInstant message timestamp
337 void setIssueInstant(time_t issueInstant) {
338 m_issueInstant = issueInstant;
342 * Sets the issuer of the message as determined by the registered policies.
344 * @param issuer issuer of the message
346 void setIssuer(const saml2::Issuer* issuer);
349 * Sets the issuer of the message as determined by the registered policies.
351 * @param issuer issuer of the message
353 void setIssuer(const XMLCh* issuer);
356 * Sets the metadata for the role the issuer is operating in.
358 * @param issuerRole metadata for the role the issuer is operating in
360 void setIssuerMetadata(const saml2md::RoleDescriptor* issuerRole);
363 * Sets the authentication status of the message as determined by the registered policies.
365 * @param auth indicates whether the issuer/message has been authenticated
367 void setAuthenticated(bool auth) {
368 m_authenticated = auth;
371 /** Allows override of rules for comparing saml2:Issuer information. */
372 class SAML_API IssuerMatchingPolicy {
373 MAKE_NONCOPYABLE(IssuerMatchingPolicy);
375 IssuerMatchingPolicy() {}
376 virtual ~IssuerMatchingPolicy() {}
379 * Returns true iff the two operands "match". Applications can override this method to
380 * support non-standard issuer matching for complex policies.
382 * <p>The default implementation does a basic comparison of the XML content, treating
383 * an unsupplied Format as an "entityID".
385 * @param issuer1 the first Issuer to match
386 * @param issuer2 the second Issuer to match
387 * @return true iff the operands match
389 virtual bool issuerMatches(const saml2::Issuer* issuer1, const saml2::Issuer* issuer2) const;
392 * Returns true iff the two operands "match". Applications can override this method to
393 * support non-standard issuer matching for complex policies.
395 * <p>The default implementation does a basic comparison of the XML content, treating
396 * an unsupplied Format as an "entityID".
398 * @param issuer1 the first Issuer to match
399 * @param issuer2 the second Issuer to match
400 * @return true iff the operands match
402 virtual bool issuerMatches(const saml2::Issuer* issuer1, const XMLCh* issuer2) const;
406 * Returns the IssuerMatchingPolicy in effect.
408 * @return the effective IssuerMatchingPolicy
410 const IssuerMatchingPolicy& getIssuerMatchingPolicy() const {
411 return m_matchingPolicy ? *m_matchingPolicy : m_defaultMatching;
415 * Sets the IssuerMatchingPolicy in effect. Setting no policy will
416 * cause the simple, default approach to be used.
418 * <p>The matching object will be freed by the SecurityPolicy.
420 * @param matchingPolicy the IssuerMatchingPolicy to use
422 void setIssuerMatchingPolicy(IssuerMatchingPolicy* matchingPolicy) {
423 delete m_matchingPolicy;
424 m_matchingPolicy = matchingPolicy;
428 /** A shared matching object that just supports the default matching rules. */
429 static IssuerMatchingPolicy m_defaultMatching;
431 /** Manufactured MetadataProvider::Criteria instance. */
432 mutable saml2md::MetadataProvider::Criteria* m_metadataCriteria;
435 // information extracted from message
437 time_t m_issueInstant;
438 saml2::Issuer* m_issuer;
439 const saml2md::RoleDescriptor* m_issuerRole;
440 bool m_authenticated;
442 // components governing policy rules
443 IssuerMatchingPolicy* m_matchingPolicy;
444 std::vector<const SecurityPolicyRule*> m_rules;
445 const saml2md::MetadataProvider* m_metadata;
446 xmltooling::QName* m_role;
447 const xmltooling::TrustEngine* m_trust;
451 // contextual information
453 std::vector<const XMLCh*> m_audiences;
458 #if defined (_MSC_VER)
459 #pragma warning( pop )
462 #endif /* __saml_secpol_h__ */