3ab67a5bfdb2333ed56e7a3334db8897f5d6b686
[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 #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(false)
70 {
71     if (e) {
72         const XMLCh* flag = e->getAttributeNS(NULL, errorFatal);
73         m_errorFatal = (flag && (*flag==chLatin_t || *flag==chDigit_1)); 
74     }
75 }
76
77 bool XMLSigningRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
78 {
79     Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.XMLSigning");
80     
81     if (!policy.getIssuerMetadata()) {
82         log.debug("ignoring message, no issuer metadata supplied");
83         return false;
84     }
85
86     const SignatureTrustEngine* sigtrust;
87     if (!(sigtrust=dynamic_cast<const SignatureTrustEngine*>(policy.getTrustEngine()))) {
88         log.debug("ignoring message, no SignatureTrustEngine supplied");
89         return false;
90     }
91     
92     const SignableObject* signable = dynamic_cast<const SignableObject*>(&message);
93     if (!signable || !signable->getSignature())
94         return false;
95     
96     log.debug("validating signature profile");
97     try {
98         SignatureProfileValidator sigval;
99         sigval.validateSignature(*(signable->getSignature()));
100     }
101     catch (ValidationException& ve) {
102         log.error("signature profile failed to validate: %s", ve.what());
103         if (m_errorFatal)
104             throw;
105         return false;
106     }
107     
108     // Set up criteria object.
109     MetadataCredentialCriteria cc(*(policy.getIssuerMetadata()));
110
111     if (!sigtrust->validate(*(signable->getSignature()), *(policy.getMetadataProvider()), &cc)) {
112         log.error("unable to verify message signature with supplied trust engine");
113         if (m_errorFatal)
114             throw SecurityPolicyException("Message was signed, but signature could not be verified.");
115         return false;
116     }
117
118     log.debug("signature verified against message issuer");
119     policy.setAuthenticated(true);
120     return true;
121 }