Factor out issuer/protocol extraction.
[shibboleth/cpp-opensaml.git] / saml / binding / SecurityPolicyRule.h
1 /*
2  *  Copyright 2001-2006 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * @file saml/binding/SecurityPolicyRule.h
19  * 
20  * Policy rules that secure and authenticate bindings.
21  */
22
23 #ifndef __saml_secrule_h__
24 #define __saml_secrule_h__
25
26 #include <saml/binding/GenericRequest.h>
27 #include <xmltooling/XMLObject.h>
28
29 namespace opensaml {
30     class SAML_API TrustEngine;
31     
32     namespace saml2 {
33         class SAML_API Issuer;
34     };
35     namespace saml2md {
36         class SAML_API MetadataProvider;
37         class SAML_API RoleDescriptor;
38     };
39     
40     /**
41      * A rule that a protocol request and message must meet in order to be valid and secure.
42      * 
43      * <p>Rules must be stateless and thread-safe across evaluations. Evaluation should not
44      * result in an exception if the request/message properties do not apply to the rule
45      * (e.g. particular security mechanisms that are not present). 
46      */
47     class SAML_API SecurityPolicyRule
48     {
49         MAKE_NONCOPYABLE(SecurityPolicyRule);
50     protected:
51         SecurityPolicyRule() {}
52     public:
53         virtual ~SecurityPolicyRule() {}
54
55         /** Allows override of code for extracting saml2:Issuer and protocol information. */
56         class SAML_API MessageExtractor {
57             MAKE_NONCOPYABLE(MessageExtractor);
58         public:
59             MessageExtractor() {}
60             virtual ~MessageExtractor() {}
61             
62             /**
63              * Examines the message and/or its contents and extracts the issuer's claimed
64              * identity along with a protocol identifier. Conventions may be needed
65              * to properly encode non-SAML2 issuer information into a compatible form. 
66              * 
67              * <p>The caller is responsible for freeing the Issuer object.
68              * 
69              * @param message       message to examine
70              * @return  a pair consisting of a SAML 2.0 Issuer object and a protocol constant.
71              * @throws std::bad_cast thrown if the message is not of an expected type
72              */
73             virtual std::pair<saml2::Issuer*,const XMLCh*> getIssuerAndProtocol(const xmltooling::XMLObject& message) const;
74         };
75
76         /**
77          * Evaluates the rule against the given request and message. If an Issuer is
78          * returned, the caller is responsible for freeing the Issuer object.
79          * 
80          * @param request           the protocol request
81          * @param message           the incoming message
82          * @param metadataProvider  locked MetadataProvider instance to authenticate the message
83          * @param role              identifies the role (generally IdP or SP) of the peer who issued the message 
84          * @param trustEngine       TrustEngine to authenticate the message
85          * @param extractor         MessageExtractor to use in examining message
86          * @return the identity of the message issuer, in two forms, or NULL
87          * 
88          * @throws BindingException thrown if the request/message do not meet the requirements of this rule
89          */
90         virtual std::pair<saml2::Issuer*,const saml2md::RoleDescriptor*> evaluate(
91             const GenericRequest& request,
92             const xmltooling::XMLObject& message,
93             const saml2md::MetadataProvider* metadataProvider,
94             const xmltooling::QName* role,
95             const TrustEngine* trustEngine,
96             const MessageExtractor& extractor
97             ) const=0;
98     };
99
100     /**
101      * Registers SecurityPolicyRule plugins into the runtime.
102      */
103     void SAML_API registerSecurityPolicyRules();
104
105     /**
106      * SecurityPolicyRule for replay detection and freshness checking.
107      * 
108      * <p>A ReplayCache instance must be available from the runtime, unless
109      * a "checkReplay" XML attribute is set to "0" or "false" when instantiating
110      * the policy rule.
111      * 
112      * <p>Messages must have been issued in the past, but no more than 60 seconds ago,
113      * or up to a number of seconds set by an "expires" XML attribute when
114      * instantiating the policy rule.
115      */
116     #define MESSAGEFLOW_POLICY_RULE  "org.opensaml.binding.MessageFlowRule"
117
118     /**
119      * SecurityPolicyRule for protocol message "blob" signing.
120      * 
121      * Allows the message issuer to be authenticated using a non-XML digital signature
122      * over the message body. The transport layer is not considered.
123      */
124     #define SIMPLESIGNING_POLICY_RULE  "org.opensaml.binding.SimpleSigningRule"
125
126     /**
127      * SecurityPolicyRule for protocol message XML signing.
128      * 
129      * Allows the message issuer to be authenticated using an XML digital signature
130      * over the message. The transport layer is not considered.
131      */
132     #define XMLSIGNING_POLICY_RULE  "org.opensaml.binding.XMLSigningRule"
133 };
134
135 #endif /* __saml_secrule_h__ */