Reducing header overuse, non-inlining selected methods (CPPOST-35).
[shibboleth/cpp-opensaml.git] / saml / binding / impl / XMLSigningRule.cpp
1 /*
2  *  Copyright 2001-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  * XMLSigningRule.cpp
19  * 
20  * XML Signature checking SecurityPolicyRule
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "binding/SecurityPolicy.h"
26 #include "binding/SecurityPolicyRule.h"
27 #include "saml2/core/Assertions.h"
28 #include "saml2/metadata/Metadata.h"
29 #include "saml2/metadata/MetadataCredentialCriteria.h"
30 #include "saml2/metadata/MetadataProvider.h"
31 #include "signature/SignatureProfileValidator.h"
32
33 #include <xmltooling/logging.h>
34 #include <xmltooling/security/SignatureTrustEngine.h>
35
36 using namespace opensaml::saml2md;
37 using namespace opensaml;
38 using namespace xmltooling::logging;
39 using namespace xmltooling;
40 using namespace std;
41
42 using xmlsignature::SignatureException;
43
44 namespace opensaml {
45     class SAML_DLLLOCAL XMLSigningRule : public SecurityPolicyRule
46     {
47     public:
48         XMLSigningRule(const DOMElement* e);
49         virtual ~XMLSigningRule() {}
50         
51         const char* getType() const {
52             return XMLSIGNING_POLICY_RULE;
53         }
54         bool evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
55
56     private:
57         bool m_errorFatal;
58     };
59
60     SecurityPolicyRule* SAML_DLLLOCAL XMLSigningRuleFactory(const DOMElement* const & e)
61     {
62         return new XMLSigningRule(e);
63     }
64     
65     static const XMLCh errorFatal[] = UNICODE_LITERAL_10(e,r,r,o,r,F,a,t,a,l);
66 };
67
68 XMLSigningRule::XMLSigningRule(const DOMElement* e) : m_errorFatal(false)
69 {
70     if (e) {
71         const XMLCh* flag = e->getAttributeNS(NULL, errorFatal);
72         m_errorFatal = (flag && (*flag==chLatin_t || *flag==chDigit_1)); 
73     }
74 }
75
76 bool XMLSigningRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
77 {
78     Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.XMLSigning");
79     
80     if (!policy.getIssuerMetadata()) {
81         log.debug("ignoring message, no issuer metadata supplied");
82         return false;
83     }
84
85     const SignatureTrustEngine* sigtrust;
86     if (!(sigtrust=dynamic_cast<const SignatureTrustEngine*>(policy.getTrustEngine()))) {
87         log.debug("ignoring message, no SignatureTrustEngine supplied");
88         return false;
89     }
90     
91     const SignableObject* signable = dynamic_cast<const SignableObject*>(&message);
92     if (!signable || !signable->getSignature())
93         return false;
94     
95     log.debug("validating signature profile");
96     try {
97         SignatureProfileValidator sigval;
98         sigval.validateSignature(*(signable->getSignature()));
99     }
100     catch (ValidationException& ve) {
101         log.error("signature profile failed to validate: %s", ve.what());
102         if (m_errorFatal)
103             throw;
104         return false;
105     }
106     
107     // Set up criteria object.
108     MetadataCredentialCriteria cc(*(policy.getIssuerMetadata()));
109
110     if (!sigtrust->validate(*(signable->getSignature()), *(policy.getMetadataProvider()), &cc)) {
111         log.error("unable to verify message signature with supplied trust engine");
112         if (m_errorFatal)
113             throw SecurityPolicyException("Message was signed, but signature could not be verified.");
114         return false;
115     }
116
117     log.debug("signature verified against message issuer");
118     policy.setAuthenticated(true);
119     return true;
120 }