2 * Copyright 2001-2007 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,
73 ) : m_metadataCriteria(NULL), m_messageID(NULL), m_issueInstant(0), m_issuer(NULL), m_issuerRole(NULL), m_authenticated(false),
74 m_matchingPolicy(NULL), m_metadata(metadataProvider), m_role(NULL), m_trust(trustEngine), m_validate(validate), m_entityOnly(true) {
76 m_role = new xmltooling::QName(*role);
79 virtual ~SecurityPolicy();
82 * Returns the locked MetadataProvider supplied to the policy.
84 * @return the supplied MetadataProvider or NULL
86 const saml2md::MetadataProvider* getMetadataProvider() const {
91 * Returns a reference to a MetadataProvider::Criteria instance suitable for use with the
92 * installed MetadataProvider.
94 * @return reference to a MetadataProvider::Criteria instance
96 virtual saml2md::MetadataProvider::Criteria& getMetadataProviderCriteria() const;
99 * Returns the peer role element/type supplied to the policy.
101 * @return the peer role element/type, or an empty QName
103 const xmltooling::QName* getRole() const {
108 * Returns the TrustEngine supplied to the policy.
110 * @return the supplied TrustEngine or NULL
112 const xmltooling::TrustEngine* getTrustEngine() const {
117 * Returns XML message validation setting.
119 * @return validation flag
121 bool getValidating() const {
126 * Returns flag controlling non-entity issuer support.
128 * @return flag controlling non-entity issuer support
130 bool requireEntityIssuer() const {
135 * Gets a mutable array of installed policy rules.
137 * <p>If adding rules, their lifetime must be at least as long as the policy object.
139 * @return mutable array of rules
141 std::vector<const SecurityPolicyRule*>& getRules() {
146 * Sets a locked MetadataProvider for the policy.
148 * @param metadata a locked MetadataProvider or NULL
150 void setMetadataProvider(const saml2md::MetadataProvider* metadata) {
151 m_metadata = metadata;
155 * Sets a peer role element/type for to the policy.
157 * @param role the peer role element/type or NULL
159 void setRole(const xmltooling::QName* role) {
161 m_role = role ? new xmltooling::QName(*role) : NULL;
165 * Sets a TrustEngine for the policy.
167 * @param trust a TrustEngine or NULL
169 void setTrustEngine(const xmltooling::TrustEngine* trust) {
174 * Controls schema validation of incoming XML messages.
175 * This is separate from other forms of programmatic validation of objects,
176 * but can detect a much wider range of syntax errors.
178 * @param validate validation setting
180 void setValidating(bool validate=true) {
181 m_validate = validate;
185 * Sets flag controlling non-entity issuer support.
187 * @param entityOnly require that Issuer be in entity format
189 void requireEntityIssuer(bool entityOnly=true) {
190 m_entityOnly = entityOnly;
194 * Evaluates the policy against the given request and message,
195 * possibly populating message information in the policy object.
197 * @param message the incoming message
198 * @param request the protocol request
200 * @throws BindingException raised if the message/request is invalid according to the supplied rules
203 const xmltooling::XMLObject& message, const xmltooling::GenericRequest* request=NULL
207 * Resets the policy object and/or clears any per-message state.
209 * <p>Resets can be complete (the default) or merely clear the previous message ID and timestamp
210 * when evaluating multiple layers of a message.
212 * @param messageOnly true iff security and issuer state should be left in place
214 void reset(bool messageOnly=false);
217 * Returns the message identifier as determined by the registered policies.
219 * @return message identifier as determined by the registered policies
221 const XMLCh* getMessageID() const {
226 * Returns the message timestamp as determined by the registered policies.
228 * @return message timestamp as determined by the registered policies
230 time_t getIssueInstant() const {
231 return m_issueInstant;
235 * Gets the issuer of the message as determined by the registered policies.
237 * @return issuer of the message as determined by the registered policies
239 const saml2::Issuer* getIssuer() const {
244 * Gets the metadata for the role the issuer is operating in.
246 * @return metadata for the role the issuer is operating in
248 const saml2md::RoleDescriptor* getIssuerMetadata() const {
253 * Returns the authentication status of the message as determined by the registered policies.
255 * @return true iff a SecurityPolicyRule has indicated the issuer/message has been authenticated
257 bool isAuthenticated() const {
258 return m_authenticated;
262 * Sets the message identifier as determined by the registered policies.
264 * @param id message identifier
266 void setMessageID(const XMLCh* id) {
267 xercesc::XMLString::release(&m_messageID);
268 m_messageID = xercesc::XMLString::replicate(id);
272 * Sets the message timestamp as determined by the registered policies.
274 * @param issueInstant message timestamp
276 void setIssueInstant(time_t issueInstant) {
277 m_issueInstant = issueInstant;
281 * Sets the issuer of the message as determined by the registered policies.
283 * @param issuer issuer of the message
285 void setIssuer(const saml2::Issuer* issuer);
288 * Sets the issuer of the message as determined by the registered policies.
290 * @param issuer issuer of the message
292 void setIssuer(const XMLCh* issuer);
295 * Sets the metadata for the role the issuer is operating in.
297 * @param issuerRole metadata for the role the issuer is operating in
299 void setIssuerMetadata(const saml2md::RoleDescriptor* issuerRole);
302 * Sets the authentication status of the message as determined by the registered policies.
304 * @param auth indicates whether the issuer/message has been authenticated
306 void setAuthenticated(bool auth) {
307 m_authenticated = auth;
310 /** Allows override of rules for comparing saml2:Issuer information. */
311 class SAML_API IssuerMatchingPolicy {
312 MAKE_NONCOPYABLE(IssuerMatchingPolicy);
314 IssuerMatchingPolicy() {}
315 virtual ~IssuerMatchingPolicy() {}
318 * Returns true iff the two operands "match". Applications can override this method to
319 * support non-standard issuer matching for complex policies.
321 * <p>The default implementation does a basic comparison of the XML content, treating
322 * an unsupplied Format as an "entityID".
324 * @param issuer1 the first Issuer to match
325 * @param issuer2 the second Issuer to match
326 * @return true iff the operands match
328 virtual bool issuerMatches(const saml2::Issuer* issuer1, const saml2::Issuer* issuer2) const;
331 * Returns true iff the two operands "match". Applications can override this method to
332 * support non-standard issuer matching for complex policies.
334 * <p>The default implementation does a basic comparison of the XML content, treating
335 * an unsupplied Format as an "entityID".
337 * @param issuer1 the first Issuer to match
338 * @param issuer2 the second Issuer to match
339 * @return true iff the operands match
341 virtual bool issuerMatches(const saml2::Issuer* issuer1, const XMLCh* issuer2) const;
345 * Returns the IssuerMatchingPolicy in effect.
347 * @return the effective IssuerMatchingPolicy
349 const IssuerMatchingPolicy& getIssuerMatchingPolicy() const {
350 return m_matchingPolicy ? *m_matchingPolicy : m_defaultMatching;
354 * Sets the IssuerMatchingPolicy in effect. Setting no policy will
355 * cause the simple, default approach to be used.
357 * <p>The matching object will be freed by the SecurityPolicy.
359 * @param matchingPolicy the IssuerMatchingPolicy to use
361 void setIssuerMatchingPolicy(IssuerMatchingPolicy* matchingPolicy) {
362 delete m_matchingPolicy;
363 m_matchingPolicy = matchingPolicy;
367 /** A shared matching object that just supports the default matching rules. */
368 static IssuerMatchingPolicy m_defaultMatching;
370 /** Manufactured MetadataProvider::Criteria instance. */
371 mutable saml2md::MetadataProvider::Criteria* m_metadataCriteria;
374 // information extracted from message
376 time_t m_issueInstant;
377 saml2::Issuer* m_issuer;
378 const saml2md::RoleDescriptor* m_issuerRole;
379 bool m_authenticated;
381 // components governing policy rules
382 IssuerMatchingPolicy* m_matchingPolicy;
383 std::vector<const SecurityPolicyRule*> m_rules;
384 const saml2md::MetadataProvider* m_metadata;
385 xmltooling::QName* m_role;
386 const xmltooling::TrustEngine* m_trust;
393 #if defined (_MSC_VER)
394 #pragma warning( pop )
397 #endif /* __saml_secpol_h__ */