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<xmltooling::xstring>& 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<xmltooling::xstring>& 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 * Returns the message identifier to which the message being evaluated
166 * @return correlated message identifier
168 const XMLCh* getCorrelationID() const {
169 return m_correlationID.c_str();
173 * Gets a mutable array of installed policy rules.
175 * <p>If adding rules, their lifetime must be at least as long as the policy object.
177 * @return mutable array of rules
179 std::vector<const SecurityPolicyRule*>& getRules() {
184 * Sets a locked MetadataProvider for the policy.
186 * @param metadata a locked MetadataProvider or NULL
188 void setMetadataProvider(const saml2md::MetadataProvider* metadata) {
189 m_metadata = metadata;
193 * Sets a MetadataProvider::Criteria instance suitable for use with the
194 * installed MetadataProvider.
196 * <p>The policy will take ownership of the criteria object when this
199 * @param criteria a MetadataProvider::Criteria instance, or NULL
201 void setMetadataProviderCriteria(saml2md::MetadataProvider::Criteria* criteria);
204 * Sets a peer role element/type for to the policy.
206 * @param role the peer role element/type or NULL
208 void setRole(const xmltooling::QName* role) {
210 m_role = role ? new xmltooling::QName(*role) : NULL;
214 * Sets a TrustEngine for the policy.
216 * @param trust a TrustEngine or NULL
218 void setTrustEngine(const xmltooling::TrustEngine* trust) {
223 * Controls schema validation of incoming XML messages.
224 * This is separate from other forms of programmatic validation of objects,
225 * but can detect a much wider range of syntax errors.
227 * @param validate validation setting
229 void setValidating(bool validate=true) {
230 m_validate = validate;
234 * Sets flag controlling non-entity issuer support.
236 * @param entityOnly require that Issuer be in entity format
238 void requireEntityIssuer(bool entityOnly=true) {
239 m_entityOnly = entityOnly;
243 * Sets effective time of message processing.
245 * <p>Assumed to be the time of policy instantiation, can be adjusted to pre- or post-date
246 * message processing.
248 * @param ts the time at which the message is being processed
250 void setTime(time_t ts) {
255 * Sets the message identifier to which the message being evaluated
258 * @param correlationID correlated message identifier
260 void setCorrelationID(const XMLCh* correlationID) {
261 m_correlationID.erase();
263 m_correlationID = correlationID;
267 * Evaluates the policy against the given request and message,
268 * possibly populating message information in the policy object.
270 * @param message the incoming message
271 * @param request the protocol request
273 * @throws BindingException raised if the message/request is invalid according to the supplied rules
275 void evaluate(const xmltooling::XMLObject& message, const xmltooling::GenericRequest* request=NULL);
278 * Resets the policy object and/or clears any per-message state.
280 * <p>Resets can be complete (the default) or merely clear the previous message ID and timestamp
281 * when evaluating multiple layers of a message.
283 * @param messageOnly true iff security and issuer state should be left in place
285 virtual void reset(bool messageOnly=false);
288 * Resets the policy object and/or clears any per-message state for only this specific class.
290 * <p>Resets can be complete (the default) or merely clear the previous message ID and timestamp
291 * when evaluating multiple layers of a message.
293 * @param messageOnly true iff security and issuer state should be left in place
295 void _reset(bool messageOnly=false);
298 * Returns the message identifier as determined by the registered policies.
300 * @return message identifier as determined by the registered policies
302 const XMLCh* getMessageID() const {
303 return m_messageID.c_str();
307 * Returns the message timestamp as determined by the registered policies.
309 * @return message timestamp as determined by the registered policies
311 time_t getIssueInstant() const {
312 return m_issueInstant;
316 * Gets the issuer of the message as determined by the registered policies.
318 * @return issuer of the message as determined by the registered policies
320 const saml2::Issuer* getIssuer() const {
325 * Gets the metadata for the role the issuer is operating in.
327 * @return metadata for the role the issuer is operating in
329 const saml2md::RoleDescriptor* getIssuerMetadata() const {
334 * Returns the authentication status of the message as determined by the registered policies.
336 * @return true iff a SecurityPolicyRule has indicated the issuer/message has been authenticated
338 bool isAuthenticated() const {
339 return m_authenticated;
343 * Sets the message identifier as determined by the registered policies.
345 * @param id message identifier
347 void setMessageID(const XMLCh* id) {
354 * Sets the message timestamp as determined by the registered policies.
356 * @param issueInstant message timestamp
358 void setIssueInstant(time_t issueInstant) {
359 m_issueInstant = issueInstant;
363 * Sets the issuer of the message as determined by the registered policies.
365 * @param issuer issuer of the message
367 void setIssuer(const saml2::Issuer* issuer);
370 * Sets the issuer of the message as determined by the registered policies.
372 * @param issuer issuer of the message
374 void setIssuer(const XMLCh* issuer);
377 * Sets the metadata for the role the issuer is operating in.
379 * @param issuerRole metadata for the role the issuer is operating in
381 void setIssuerMetadata(const saml2md::RoleDescriptor* issuerRole);
384 * Sets the authentication status of the message as determined by the registered policies.
386 * @param auth indicates whether the issuer/message has been authenticated
388 void setAuthenticated(bool auth) {
389 m_authenticated = auth;
392 /** Allows override of rules for comparing saml2:Issuer information. */
393 class SAML_API IssuerMatchingPolicy {
394 MAKE_NONCOPYABLE(IssuerMatchingPolicy);
396 IssuerMatchingPolicy() {}
397 virtual ~IssuerMatchingPolicy() {}
400 * Returns true iff the two operands "match". Applications can override this method to
401 * support non-standard issuer matching for complex policies.
403 * <p>The default implementation does a basic comparison of the XML content, treating
404 * an unsupplied Format as an "entityID".
406 * @param issuer1 the first Issuer to match
407 * @param issuer2 the second Issuer to match
408 * @return true iff the operands match
410 virtual bool issuerMatches(const saml2::Issuer* issuer1, const saml2::Issuer* issuer2) const;
413 * Returns true iff the two operands "match". Applications can override this method to
414 * support non-standard issuer matching for complex policies.
416 * <p>The default implementation does a basic comparison of the XML content, treating
417 * an unsupplied Format as an "entityID".
419 * @param issuer1 the first Issuer to match
420 * @param issuer2 the second Issuer to match
421 * @return true iff the operands match
423 virtual bool issuerMatches(const saml2::Issuer* issuer1, const XMLCh* issuer2) const;
427 * Returns the IssuerMatchingPolicy in effect.
429 * @return the effective IssuerMatchingPolicy
431 const IssuerMatchingPolicy& getIssuerMatchingPolicy() const {
432 return m_matchingPolicy ? *m_matchingPolicy : m_defaultMatching;
436 * Sets the IssuerMatchingPolicy in effect. Setting no policy will
437 * cause the simple, default approach to be used.
439 * <p>The matching object will be freed by the SecurityPolicy.
441 * @param matchingPolicy the IssuerMatchingPolicy to use
443 void setIssuerMatchingPolicy(IssuerMatchingPolicy* matchingPolicy) {
444 delete m_matchingPolicy;
445 m_matchingPolicy = matchingPolicy;
449 /** A shared matching object that just supports the default matching rules. */
450 static IssuerMatchingPolicy m_defaultMatching;
452 /** Manufactured MetadataProvider::Criteria instance. */
453 mutable saml2md::MetadataProvider::Criteria* m_metadataCriteria;
456 // information extracted from message
457 xmltooling::xstring m_messageID;
458 time_t m_issueInstant;
459 saml2::Issuer* m_issuer;
460 const saml2md::RoleDescriptor* m_issuerRole;
461 bool m_authenticated;
463 // components governing policy rules
464 IssuerMatchingPolicy* m_matchingPolicy;
465 std::vector<const SecurityPolicyRule*> m_rules;
466 const saml2md::MetadataProvider* m_metadata;
467 xmltooling::QName* m_role;
468 const xmltooling::TrustEngine* m_trust;
472 // contextual information
474 xmltooling::xstring m_correlationID;
475 std::vector<xmltooling::xstring> m_audiences;
480 #if defined (_MSC_VER)
481 #pragma warning( pop )
484 #endif /* __saml_secpol_h__ */