1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
/*
* Copyright 2001-2007 Internet2
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file saml/binding/SecurityPolicy.h
*
* Overall policy used to verify the security of an incoming message.
*/
#ifndef __saml_secpol_h__
#define __saml_secpol_h__
#include <saml/base.h>
#include <ctime>
#include <vector>
#include <xmltooling/XMLObject.h>
#include <xmltooling/io/GenericRequest.h>
#include <xmltooling/security/TrustEngine.h>
#if defined (_MSC_VER)
#pragma warning( push )
#pragma warning( disable : 4250 4251 )
#endif
namespace opensaml {
namespace saml2 {
class SAML_API Issuer;
};
namespace saml2md {
class SAML_API MetadataProvider;
class SAML_API RoleDescriptor;
};
class SAML_API SecurityPolicyRule;
/**
* A policy used to verify the security of an incoming message.
*
* <p>Its security mechanisms may be used to examine the transport layer
* (e.g client certificates and HTTP basic auth passwords) or to check the
* payload of a request to ensure it meets certain criteria (e.g. valid
* digital signature, freshness, replay).
*
* <p>Policy objects can be reused, but are not thread-safe.
*/
class SAML_API SecurityPolicy
{
MAKE_NONCOPYABLE(SecurityPolicy);
public:
/**
* Constructor for policy.
*
* @param metadataProvider locked MetadataProvider instance
* @param role identifies the role (generally IdP or SP) of the policy peer
* @param trustEngine TrustEngine to authenticate policy peer
* @param validate true iff XML parsing should be done with validation
*/
SecurityPolicy(
const saml2md::MetadataProvider* metadataProvider=NULL,
const xmltooling::QName* role=NULL,
const xmltooling::TrustEngine* trustEngine=NULL,
bool validate=true
) : m_messageID(NULL), m_issueInstant(0), m_issuer(NULL), m_issuerRole(NULL), m_authenticated(false),
m_matchingPolicy(NULL), m_metadata(metadataProvider), m_role(NULL), m_trust(trustEngine), m_validate(validate), m_entityOnly(true) {
if (role)
m_role = new xmltooling::QName(*role);
}
virtual ~SecurityPolicy();
/**
* Returns the locked MetadataProvider supplied to the policy.
*
* @return the supplied MetadataProvider or NULL
*/
const saml2md::MetadataProvider* getMetadataProvider() const {
return m_metadata;
}
/**
* Returns the peer role element/type supplied to the policy.
*
* @return the peer role element/type, or an empty QName
*/
const xmltooling::QName* getRole() const {
return m_role;
}
/**
* Returns the TrustEngine supplied to the policy.
*
* @return the supplied TrustEngine or NULL
*/
const xmltooling::TrustEngine* getTrustEngine() const {
return m_trust;
}
/**
* Returns XML message validation setting.
*
* @return validation flag
*/
bool getValidating() const {
return m_validate;
}
/**
* Returns flag controlling non-entity issuer support.
*
* @return flag controlling non-entity issuer support
*/
bool requireEntityIssuer() const {
return m_entityOnly;
}
/**
* Gets a mutable array of installed policy rules.
*
* <p>If adding rules, their lifetime must be at least as long as the policy object.
*
* @return mutable array of rules
*/
std::vector<const SecurityPolicyRule*>& getRules() {
return m_rules;
}
/**
* Sets a locked MetadataProvider for the policy.
*
* @param metadata a locked MetadataProvider or NULL
*/
void setMetadataProvider(const saml2md::MetadataProvider* metadata) {
m_metadata = metadata;
}
/**
* Sets a peer role element/type for to the policy.
*
* @param role the peer role element/type or NULL
*/
void setRole(const xmltooling::QName* role) {
delete m_role;
m_role = role ? new xmltooling::QName(*role) : NULL;
}
/**
* Sets a TrustEngine for the policy.
*
* @param trust a TrustEngine or NULL
*/
void setTrustEngine(const xmltooling::TrustEngine* trust) {
m_trust = trust;
}
/**
* Controls schema validation of incoming XML messages.
* This is separate from other forms of programmatic validation of objects,
* but can detect a much wider range of syntax errors.
*
* @param validate validation setting
*/
void setValidating(bool validate=true) {
m_validate = validate;
}
/**
* Sets flag controlling non-entity issuer support.
*
* @param entityOnly require that Issuer be in entity format
*/
void requireEntityIssuer(bool entityOnly=true) {
m_entityOnly = entityOnly;
}
/**
* Evaluates the policy against the given request and message,
* possibly populating message information in the policy object.
*
* @param message the incoming message
* @param request the protocol request
*
* @throws BindingException raised if the message/request is invalid according to the supplied rules
*/
void evaluate(
const xmltooling::XMLObject& message, const xmltooling::GenericRequest* request=NULL
);
/**
* Resets the policy object and/or clears any per-message state.
*
* <p>Resets can be complete (the default) or merely clear the previous message ID and timestamp
* when evaluating multiple layers of a message.
*
* @param messageOnly true iff security and issuer state should be left in place
*/
void reset(bool messageOnly=false);
/**
* Returns the message identifier as determined by the registered policies.
*
* @return message identifier as determined by the registered policies
*/
const XMLCh* getMessageID() const {
return m_messageID;
}
/**
* Returns the message timestamp as determined by the registered policies.
*
* @return message timestamp as determined by the registered policies
*/
time_t getIssueInstant() const {
return m_issueInstant;
}
/**
* Gets the issuer of the message as determined by the registered policies.
*
* @return issuer of the message as determined by the registered policies
*/
const saml2::Issuer* getIssuer() const {
return m_issuer;
}
/**
* Gets the metadata for the role the issuer is operating in.
*
* @return metadata for the role the issuer is operating in
*/
const saml2md::RoleDescriptor* getIssuerMetadata() const {
return m_issuerRole;
}
/**
* Returns the authentication status of the message as determined by the registered policies.
*
* @return true iff a SecurityPolicyRule has indicated the issuer/message has been authenticated
*/
bool isAuthenticated() const {
return m_authenticated;
}
/**
* Sets the message identifier as determined by the registered policies.
*
* @param id message identifier
*/
void setMessageID(const XMLCh* id) {
xercesc::XMLString::release(&m_messageID);
m_messageID = xercesc::XMLString::replicate(id);
}
/**
* Sets the message timestamp as determined by the registered policies.
*
* @param issueInstant message timestamp
*/
void setIssueInstant(time_t issueInstant) {
m_issueInstant = issueInstant;
}
/**
* Sets the issuer of the message as determined by the registered policies.
*
* @param issuer issuer of the message
*/
void setIssuer(const saml2::Issuer* issuer);
/**
* Sets the issuer of the message as determined by the registered policies.
*
* @param issuer issuer of the message
*/
void setIssuer(const XMLCh* issuer);
/**
* Sets the metadata for the role the issuer is operating in.
*
* @param issuerRole metadata for the role the issuer is operating in
*/
void setIssuerMetadata(const saml2md::RoleDescriptor* issuerRole);
/**
* Sets the authentication status of the message as determined by the registered policies.
*
* @param auth indicates whether the issuer/message has been authenticated
*/
void setAuthenticated(bool auth) {
m_authenticated = auth;
}
/** Allows override of rules for comparing saml2:Issuer information. */
class SAML_API IssuerMatchingPolicy {
MAKE_NONCOPYABLE(IssuerMatchingPolicy);
public:
IssuerMatchingPolicy() {}
virtual ~IssuerMatchingPolicy() {}
/**
* Returns true iff the two operands "match". Applications can override this method to
* support non-standard issuer matching for complex policies.
*
* <p>The default implementation does a basic comparison of the XML content, treating
* an unsupplied Format as an "entityID".
*
* @param issuer1 the first Issuer to match
* @param issuer2 the second Issuer to match
* @return true iff the operands match
*/
virtual bool issuerMatches(const saml2::Issuer* issuer1, const saml2::Issuer* issuer2) const;
/**
* Returns true iff the two operands "match". Applications can override this method to
* support non-standard issuer matching for complex policies.
*
* <p>The default implementation does a basic comparison of the XML content, treating
* an unsupplied Format as an "entityID".
*
* @param issuer1 the first Issuer to match
* @param issuer2 the second Issuer to match
* @return true iff the operands match
*/
virtual bool issuerMatches(const saml2::Issuer* issuer1, const XMLCh* issuer2) const;
};
/**
* Returns the IssuerMatchingPolicy in effect.
*
* @return the effective IssuerMatchingPolicy
*/
const IssuerMatchingPolicy& getIssuerMatchingPolicy() const {
return m_matchingPolicy ? *m_matchingPolicy : m_defaultMatching;
}
/**
* Sets the IssuerMatchingPolicy in effect. Setting no policy will
* cause the simple, default approach to be used.
*
* <p>The matching object will be freed by the SecurityPolicy.
*
* @param matchingPolicy the IssuerMatchingPolicy to use
*/
void setIssuerMatchingPolicy(IssuerMatchingPolicy* matchingPolicy) {
delete m_matchingPolicy;
m_matchingPolicy = matchingPolicy;
}
protected:
/** A shared matching object that just supports the default matching rules. */
static IssuerMatchingPolicy m_defaultMatching;
private:
// information extracted from message
XMLCh* m_messageID;
time_t m_issueInstant;
saml2::Issuer* m_issuer;
const saml2md::RoleDescriptor* m_issuerRole;
bool m_authenticated;
// components governing policy rules
IssuerMatchingPolicy* m_matchingPolicy;
std::vector<const SecurityPolicyRule*> m_rules;
const saml2md::MetadataProvider* m_metadata;
xmltooling::QName* m_role;
const xmltooling::TrustEngine* m_trust;
bool m_validate;
bool m_entityOnly;
};
};
#if defined (_MSC_VER)
#pragma warning( pop )
#endif
#endif /* __saml_secpol_h__ */
|