0ac85910f39654fb0fd620f7ab6575383a86a36e
[shibboleth/cpp-opensaml.git] / saml / binding / impl / XMLSigningRule.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * XMLSigningRule.cpp
23  * 
24  * XML Signature checking SecurityPolicyRule.
25  */
26
27 #include "internal.h"
28 #include "exceptions.h"
29 #include "binding/SecurityPolicy.h"
30 #include "binding/SecurityPolicyRule.h"
31 #include "saml2/core/Assertions.h"
32 #include "saml2/metadata/Metadata.h"
33 #include "saml2/metadata/MetadataCredentialCriteria.h"
34 #include "saml2/metadata/MetadataProvider.h"
35 #include "signature/SignatureProfileValidator.h"
36
37 #include <xmltooling/logging.h>
38 #include <xmltooling/security/SignatureTrustEngine.h>
39 #include <xmltooling/signature/Signature.h>
40
41 using namespace opensaml::saml2md;
42 using namespace opensaml;
43 using namespace xmltooling::logging;
44 using namespace xmltooling;
45 using namespace std;
46
47 using xmlsignature::SignatureException;
48
49 namespace opensaml {
50     class SAML_DLLLOCAL XMLSigningRule : public SecurityPolicyRule
51     {
52     public:
53         XMLSigningRule(const DOMElement* e);
54         virtual ~XMLSigningRule() {}
55         
56         const char* getType() const {
57             return XMLSIGNING_POLICY_RULE;
58         }
59         bool evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
60
61     private:
62         bool m_errorFatal;
63     };
64
65     SecurityPolicyRule* SAML_DLLLOCAL XMLSigningRuleFactory(const DOMElement* const & e)
66     {
67         return new XMLSigningRule(e);
68     }
69     
70     static const XMLCh errorFatal[] = UNICODE_LITERAL_10(e,r,r,o,r,F,a,t,a,l);
71 };
72
73 XMLSigningRule::XMLSigningRule(const DOMElement* e) : m_errorFatal(XMLHelper::getAttrBool(e, false, errorFatal))
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 }