2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * @file saml/binding/SecurityPolicy.h
24 * Overall policy used to verify the security of an incoming message.
27 #ifndef __saml_secpol_h__
28 #define __saml_secpol_h__
30 #include <saml/saml2/metadata/MetadataProvider.h>
34 #include <xmltooling/unicode.h>
36 #if defined (_MSC_VER)
37 #pragma warning( push )
38 #pragma warning( disable : 4250 4251 )
41 namespace xmltooling {
42 class XMLTOOL_API GenericRequest;
43 class XMLTOOL_API TrustEngine;
49 class SAML_API Issuer;
52 class SAML_API SecurityPolicyRule;
55 * A policy used to verify the security of an incoming message.
57 * <p>Its security mechanisms may be used to examine the transport layer
58 * (e.g client certificates and HTTP basic auth passwords) or to check the
59 * payload of a request to ensure it meets certain criteria (e.g. valid
60 * digital signature, freshness, replay).
62 * <p>Policy objects can be reused, but are not thread-safe.
64 class SAML_API SecurityPolicy
66 MAKE_NONCOPYABLE(SecurityPolicy);
69 * Constructor for policy.
71 * @param metadataProvider locked MetadataProvider instance
72 * @param role identifies the role (generally IdP or SP) of the policy peer
73 * @param trustEngine TrustEngine to authenticate policy peer
74 * @param validate true iff XML parsing should be done with validation
77 const saml2md::MetadataProvider* metadataProvider=nullptr,
78 const xmltooling::QName* role=nullptr,
79 const xmltooling::TrustEngine* trustEngine=nullptr,
83 virtual ~SecurityPolicy();
86 * Returns the locked MetadataProvider supplied to the policy.
88 * @return the supplied MetadataProvider or nullptr
90 const saml2md::MetadataProvider* getMetadataProvider() const;
93 * Returns a reference to a MetadataProvider::Criteria instance suitable for use with the
94 * installed MetadataProvider.
96 * <p>The object will be cleared/reset when returned, so do not mutate it and then
97 * call the method again before using it.
99 * @return reference to a MetadataProvider::Criteria instance
101 virtual saml2md::MetadataProvider::Criteria& getMetadataProviderCriteria() const;
104 * Returns the peer role element/type supplied to the policy.
106 * @return the peer role element/type, or an empty QName
108 const xmltooling::QName* getRole() const;
111 * Returns the TrustEngine supplied to the policy.
113 * @return the supplied TrustEngine or nullptr
115 const xmltooling::TrustEngine* getTrustEngine() const;
118 * Returns XML message validation setting.
120 * @return validation flag
122 bool getValidating() const;
125 * Returns flag controlling non-entity issuer support.
127 * @return flag controlling non-entity issuer support
129 bool requireEntityIssuer() const;
132 * Returns the SAML audiences that represent the receiving peer.
134 * @return audience values of the peer processing the message
136 const std::vector<xmltooling::xstring>& getAudiences() const;
139 * Returns the SAML audiences that represent the receiving peer.
141 * @return audience values of the peer processing the message
143 std::vector<xmltooling::xstring>& getAudiences();
146 * Gets the effective time of message processing.
148 * @return the time at which the message is being processed
150 time_t getTime() const;
153 * Returns the message identifier to which the message being evaluated
156 * @return correlated message identifier
158 const XMLCh* getCorrelationID() const;
161 * Gets a mutable array of installed policy rules.
163 * <p>If adding rules, their lifetime must be at least as long as the policy object.
165 * @return mutable array of rules
167 std::vector<const SecurityPolicyRule*>& getRules();
170 * Sets a locked MetadataProvider for the policy.
172 * @param metadata a locked MetadataProvider or nullptr
174 void setMetadataProvider(const saml2md::MetadataProvider* metadata);
177 * Sets a MetadataProvider::Criteria instance suitable for use with the
178 * installed MetadataProvider.
180 * <p>The policy will take ownership of the criteria object when this
183 * @param criteria a MetadataProvider::Criteria instance, or nullptr
185 void setMetadataProviderCriteria(saml2md::MetadataProvider::Criteria* criteria);
188 * Sets a peer role element/type for to the policy.
190 * @param role the peer role element/type or nullptr
192 void setRole(const xmltooling::QName* role);
195 * Sets a TrustEngine for the policy.
197 * @param trust a TrustEngine or nullptr
199 void setTrustEngine(const xmltooling::TrustEngine* trust);
202 * Controls schema validation of incoming XML messages.
203 * This is separate from other forms of programmatic validation of objects,
204 * but can detect a much wider range of syntax errors.
206 * @param validate validation setting
208 void setValidating(bool validate=true);
211 * Sets flag controlling non-entity issuer support.
213 * @param entityOnly require that Issuer be in entity format
215 void requireEntityIssuer(bool entityOnly=true);
218 * Sets effective time of message processing.
220 * <p>Assumed to be the time of policy instantiation, can be adjusted to pre- or post-date
221 * message processing.
223 * @param ts the time at which the message is being processed
225 void setTime(time_t ts);
228 * Sets the message identifier to which the message being evaluated
231 * @param correlationID correlated message identifier
233 void setCorrelationID(const XMLCh* correlationID);
236 * Evaluates the policy against the given request and message,
237 * possibly populating message information in the policy object.
239 * @param message the incoming message
240 * @param request the protocol request
242 * @throws BindingException raised if the message/request is invalid according to the supplied rules
244 void evaluate(const xmltooling::XMLObject& message, const xmltooling::GenericRequest* request=nullptr);
247 * Resets the policy object and/or clears any per-message state.
249 * <p>Resets can be complete (the default) or merely clear the previous message ID and timestamp
250 * when evaluating multiple layers of a message.
252 * @param messageOnly true iff security and issuer state should be left in place
254 virtual void reset(bool messageOnly=false);
257 * Resets the policy object and/or clears any per-message state for only this specific class.
259 * <p>Resets can be complete (the default) or merely clear the previous message ID and timestamp
260 * when evaluating multiple layers of a message.
262 * @param messageOnly true iff security and issuer state should be left in place
264 void _reset(bool messageOnly=false);
267 * Returns the message identifier as determined by the registered policies.
269 * @return message identifier as determined by the registered policies
271 const XMLCh* getMessageID() const;
274 * Returns the message timestamp as determined by the registered policies.
276 * @return message timestamp as determined by the registered policies
278 time_t getIssueInstant() const;
281 * Gets the issuer of the message as determined by the registered policies.
283 * @return issuer of the message as determined by the registered policies
285 const saml2::Issuer* getIssuer() const;
288 * Gets the metadata for the role the issuer is operating in.
290 * @return metadata for the role the issuer is operating in
292 const saml2md::RoleDescriptor* getIssuerMetadata() const;
295 * Returns the authentication status of the message as determined by the registered policies.
297 * @return true iff a SecurityPolicyRule has indicated the issuer/message has been authenticated
299 bool isAuthenticated() const;
302 * Sets the message identifier as determined by the registered policies.
304 * @param id message identifier
306 void setMessageID(const XMLCh* id);
309 * Sets the message timestamp as determined by the registered policies.
311 * @param issueInstant message timestamp
313 void setIssueInstant(time_t issueInstant);
316 * Sets the issuer of the message as determined by the registered policies.
318 * @param issuer issuer of the message
320 void setIssuer(const saml2::Issuer* issuer);
323 * Sets the issuer of the message as determined by the registered policies.
325 * @param issuer issuer of the message
327 void setIssuer(const XMLCh* issuer);
330 * Sets the metadata for the role the issuer is operating in.
332 * @param issuerRole metadata for the role the issuer is operating in
334 void setIssuerMetadata(const saml2md::RoleDescriptor* issuerRole);
337 * Sets the authentication status of the message as determined by the registered policies.
339 * @param auth indicates whether the issuer/message has been authenticated
341 void setAuthenticated(bool auth);
343 /** Allows override of rules for comparing saml2:Issuer information. */
344 class SAML_API IssuerMatchingPolicy {
345 MAKE_NONCOPYABLE(IssuerMatchingPolicy);
347 IssuerMatchingPolicy();
348 virtual ~IssuerMatchingPolicy();
351 * Returns true iff the two operands "match". Applications can override this method to
352 * support non-standard issuer matching for complex policies.
354 * <p>The default implementation does a basic comparison of the XML content, treating
355 * an unsupplied Format as an "entityID".
357 * @param issuer1 the first Issuer to match
358 * @param issuer2 the second Issuer to match
359 * @return true iff the operands match
361 virtual bool issuerMatches(const saml2::Issuer* issuer1, const saml2::Issuer* issuer2) const;
364 * Returns true iff the two operands "match". Applications can override this method to
365 * support non-standard issuer matching for complex policies.
367 * <p>The default implementation does a basic comparison of the XML content, treating
368 * an unsupplied Format as an "entityID".
370 * @param issuer1 the first Issuer to match
371 * @param issuer2 the second Issuer to match
372 * @return true iff the operands match
374 virtual bool issuerMatches(const saml2::Issuer* issuer1, const XMLCh* issuer2) const;
378 * Returns the IssuerMatchingPolicy in effect.
380 * @return the effective IssuerMatchingPolicy
382 const IssuerMatchingPolicy& getIssuerMatchingPolicy() const;
385 * Sets the IssuerMatchingPolicy in effect. Setting no policy will
386 * cause the simple, default approach to be used.
388 * <p>The matching object will be freed by the SecurityPolicy.
390 * @param matchingPolicy the IssuerMatchingPolicy to use
392 void setIssuerMatchingPolicy(IssuerMatchingPolicy* matchingPolicy);
395 /** A shared matching object that just supports the default matching rules. */
396 static IssuerMatchingPolicy m_defaultMatching;
398 /** Manufactured MetadataProvider::Criteria instance. */
399 mutable saml2md::MetadataProvider::Criteria* m_metadataCriteria;
402 // information extracted from message
403 xmltooling::xstring m_messageID;
404 time_t m_issueInstant;
405 std::auto_ptr<saml2::Issuer> m_issuer;
406 const saml2md::RoleDescriptor* m_issuerRole;
407 bool m_authenticated;
409 // components governing policy rules
410 std::auto_ptr<IssuerMatchingPolicy> m_matchingPolicy;
411 std::vector<const SecurityPolicyRule*> m_rules;
412 const saml2md::MetadataProvider* m_metadata;
413 std::auto_ptr<xmltooling::QName> m_role;
414 const xmltooling::TrustEngine* m_trust;
418 // contextual information
420 xmltooling::xstring m_correlationID;
421 std::vector<xmltooling::xstring> m_audiences;
426 #if defined (_MSC_VER)
427 #pragma warning( pop )
430 #endif /* __saml_secpol_h__ */