Multi-line svn commit, see body.
[shibboleth/cpp-opensaml.git] / saml / binding / impl / XMLSigningRule.cpp
1 /*
2  *  Copyright 2001-2007 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/SecurityPolicyRule.h"
26 #include "saml2/core/Assertions.h"
27 #include "saml2/metadata/Metadata.h"
28 #include "saml2/metadata/MetadataCredentialCriteria.h"
29 #include "saml2/metadata/MetadataProvider.h"
30 #include "signature/SignatureProfileValidator.h"
31
32 #include <xmltooling/logging.h>
33 #include <xmltooling/security/SignatureTrustEngine.h>
34
35 using namespace opensaml::saml2md;
36 using namespace opensaml;
37 using namespace xmltooling::logging;
38 using namespace xmltooling;
39 using namespace std;
40
41 using xmlsignature::SignatureException;
42
43 namespace opensaml {
44     class SAML_DLLLOCAL XMLSigningRule : public SecurityPolicyRule
45     {
46     public:
47         XMLSigningRule(const DOMElement* e);
48         virtual ~XMLSigningRule() {}
49         
50         const char* getType() const {
51             return XMLSIGNING_POLICY_RULE;
52         }
53         void evaluate(const XMLObject& message, const GenericRequest* request, const XMLCh* protocol, SecurityPolicy& policy) const;
54
55     private:
56         bool m_errorsFatal;
57     };
58
59     SecurityPolicyRule* SAML_DLLLOCAL XMLSigningRuleFactory(const DOMElement* const & e)
60     {
61         return new XMLSigningRule(e);
62     }
63     
64     static const XMLCh errorsFatal[] = UNICODE_LITERAL_11(e,r,r,o,r,s,F,a,t,a,l);
65 };
66
67 XMLSigningRule::XMLSigningRule(const DOMElement* e) : m_errorsFatal(false)
68 {
69     if (e) {
70         const XMLCh* flag = e->getAttributeNS(NULL, errorsFatal);
71         m_errorsFatal = (flag && (*flag==chLatin_t || *flag==chDigit_1)); 
72     }
73 }
74
75 void XMLSigningRule::evaluate(
76     const XMLObject& message, const GenericRequest* request, const XMLCh* protocol, SecurityPolicy& policy
77     ) 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;
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;
90     }
91     
92     const SignableObject* signable = dynamic_cast<const SignableObject*>(&message);
93     if (!signable || !signable->getSignature())
94         return;
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_errorsFatal)
104             throw;
105         return;
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_errorsFatal)
114             throw SignatureException("Message was signed, but signature could not be verified.");
115         return;
116     }
117
118     log.debug("signature verified against message issuer");
119     policy.setSecure(true);
120 }