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