Reducing header overuse, non-inlining selected methods (CPPOST-35).
[shibboleth/cpp-opensaml.git] / saml / saml2 / profile / impl / BearerConfirmationRule.cpp
1 /*
2  *  Copyright 2009 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  * BearerConfirmationRule.cpp
19  *
20  * SAML 2.0 Bearer SubjectConfirmation SecurityPolicyRule
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "binding/SecurityPolicyRule.h"
26 #include "saml2/core/Assertions.h"
27 #include "saml2/profile/SAML2AssertionPolicy.h"
28
29 #include <xmltooling/logging.h>
30 #include <xmltooling/XMLToolingConfig.h>
31 #include <xmltooling/io/HTTPRequest.h>
32
33 using namespace opensaml::saml2;
34 using namespace xmltooling;
35 using namespace std;
36
37 namespace opensaml {
38     namespace saml2 {
39
40         class SAML_DLLLOCAL BearerConfirmationRule : public opensaml::SecurityPolicyRule
41         {
42         public:
43             BearerConfirmationRule(const DOMElement* e);
44
45             virtual ~BearerConfirmationRule() {
46             }
47             const char* getType() const {
48                 return BEARER_POLICY_RULE;
49             }
50             bool evaluate(const XMLObject& message, const GenericRequest* request, opensaml::SecurityPolicy& policy) const;
51         
52         private:
53             bool m_validity, m_recipient, m_correlation, m_fatal;
54         };
55
56         opensaml::SecurityPolicyRule* SAML_DLLLOCAL BearerConfirmationRuleFactory(const DOMElement* const & e)
57         {
58             return new BearerConfirmationRule(e);
59         }
60         
61         static const XMLCh checkValidity[] =    UNICODE_LITERAL_13(c,h,e,c,k,V,a,l,i,d,i,t,y);
62         static const XMLCh checkRecipient[] =   UNICODE_LITERAL_14(c,h,e,c,k,R,e,c,i,p,i,e,n,t);
63         static const XMLCh checkCorrelation[] = UNICODE_LITERAL_16(c,h,e,c,k,C,o,r,r,e,l,a,t,i,o,n);
64         static const XMLCh missingFatal[] =     UNICODE_LITERAL_12(m,i,s,s,i,n,g,F,a,t,a,l);
65     };
66 };
67
68 BearerConfirmationRule::BearerConfirmationRule(const DOMElement* e) : m_validity(true), m_recipient(true), m_correlation(true), m_fatal(true)
69 {
70     const XMLCh* flag = e ? e->getAttributeNS(NULL, checkValidity) : NULL;
71     m_validity = (!flag || (*flag != chLatin_f && *flag != chDigit_0));
72     flag = e ? e->getAttributeNS(NULL, checkRecipient) : NULL;
73     m_recipient = (!flag || (*flag != chLatin_f && *flag != chDigit_0));
74     flag = e ? e->getAttributeNS(NULL, checkCorrelation) : NULL;
75     m_correlation = (!flag || (*flag != chLatin_f && *flag != chDigit_0));
76     flag = e ? e->getAttributeNS(NULL, missingFatal) : NULL;
77     m_fatal = (!flag || (*flag != chLatin_f && *flag != chDigit_0));
78 }
79
80 bool BearerConfirmationRule::evaluate(const XMLObject& message, const GenericRequest* request, opensaml::SecurityPolicy& policy) const
81 {
82     const Assertion* a=dynamic_cast<const Assertion*>(&message);
83     if (!a)
84         return false;
85
86     logging::Category& log = logging::Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.BearerConfirmation");
87
88     const char* msg="assertion is missing bearer SubjectConfirmation";
89     const Subject* subject = a->getSubject();
90     if (subject) {
91         const vector<SubjectConfirmation*>& confs = subject->getSubjectConfirmations();
92         for (vector<SubjectConfirmation*>::const_iterator sc = confs.begin(); sc!=confs.end(); ++sc) {
93             if (XMLString::equals((*sc)->getMethod(), SubjectConfirmation::BEARER)) {
94
95                 const SubjectConfirmationDataType* data = dynamic_cast<const SubjectConfirmationDataType*>((*sc)->getSubjectConfirmationData());
96
97                 if (m_recipient) {
98                     const HTTPRequest* httpRequest = dynamic_cast<const HTTPRequest*>(request);
99                     if (httpRequest && httpRequest->getRequestURL()) {
100                         string dest = httpRequest->getRequestURL();
101                         auto_ptr_XMLCh destination(dest.substr(0,dest.find('?')).c_str());
102                         if (!XMLString::equals(destination.get(), data ? data->getRecipient() : NULL)) {
103                             msg = "bearer confirmation failed with recipient mismatch";
104                             continue;
105                         }
106                     }
107                 }
108
109                 if (m_correlation && policy.getCorrelationID() && *(policy.getCorrelationID())) {
110                     if (!XMLString::equals(policy.getCorrelationID(), data ? data->getInResponseTo() : NULL)) {
111                         msg = "bearer confirmation failed with request correlation mismatch";
112                         continue;
113                     }
114                 }
115
116                 if (m_validity) {
117                     if (!data || !data->getNotOnOrAfter()) {
118                         msg = "bearer SubjectConfirmationData missing NotOnOrAfter attribute";
119                         continue;
120                     }
121                     else if (data->getNotOnOrAfterEpoch() <= policy.getTime() - XMLToolingConfig::getConfig().clock_skew_secs) {
122                         msg = "bearer confirmation has expired";
123                         continue;
124                     }
125
126                     if (data && data->getNotBefore() && policy.getTime() + XMLToolingConfig::getConfig().clock_skew_secs < data->getNotBeforeEpoch()) {
127                         msg = "bearer confirmation not yet valid";
128                         continue;
129                     }
130                 }
131
132                 SAML2AssertionPolicy* saml2policy = dynamic_cast<SAML2AssertionPolicy*>(&policy);
133                 if (saml2policy)
134                     saml2policy->setSubjectConfirmation(*sc);
135                 log.debug("assertion satisfied bearer confirmation requirements");
136                 return true;
137             }
138         }
139     }
140
141     log.error(msg ? msg : "no error message");
142     if (m_fatal)
143         throw SecurityPolicyException("Unable to locate satisfiable bearer SubjectConfirmation in assertion.");
144     return false;
145 }