2 * Copyright 2001-2007 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * XML Signature checking SecurityPolicyRule
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"
32 #include <xmltooling/logging.h>
33 #include <xmltooling/security/SignatureTrustEngine.h>
35 using namespace opensaml::saml2md;
36 using namespace opensaml;
37 using namespace xmltooling::logging;
38 using namespace xmltooling;
41 using xmlsignature::SignatureException;
44 class SAML_DLLLOCAL XMLSigningRule : public SecurityPolicyRule
47 XMLSigningRule(const DOMElement* e);
48 virtual ~XMLSigningRule() {}
50 const char* getType() const {
51 return XMLSIGNING_POLICY_RULE;
53 void evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
59 SecurityPolicyRule* SAML_DLLLOCAL XMLSigningRuleFactory(const DOMElement* const & e)
61 return new XMLSigningRule(e);
64 static const XMLCh errorFatal[] = UNICODE_LITERAL_10(e,r,r,o,r,F,a,t,a,l);
67 XMLSigningRule::XMLSigningRule(const DOMElement* e) : m_errorFatal(false)
70 const XMLCh* flag = e->getAttributeNS(NULL, errorFatal);
71 m_errorFatal = (flag && (*flag==chLatin_t || *flag==chDigit_1));
75 void XMLSigningRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
77 Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.XMLSigning");
79 if (!policy.getIssuerMetadata()) {
80 log.debug("ignoring message, no issuer metadata supplied");
84 const SignatureTrustEngine* sigtrust;
85 if (!(sigtrust=dynamic_cast<const SignatureTrustEngine*>(policy.getTrustEngine()))) {
86 log.debug("ignoring message, no SignatureTrustEngine supplied");
90 const SignableObject* signable = dynamic_cast<const SignableObject*>(&message);
91 if (!signable || !signable->getSignature())
94 log.debug("validating signature profile");
96 SignatureProfileValidator sigval;
97 sigval.validateSignature(*(signable->getSignature()));
99 catch (ValidationException& ve) {
100 log.error("signature profile failed to validate: %s", ve.what());
106 // Set up criteria object.
107 MetadataCredentialCriteria cc(*(policy.getIssuerMetadata()));
109 if (!sigtrust->validate(*(signable->getSignature()), *(policy.getMetadataProvider()), &cc)) {
110 log.error("unable to verify message signature with supplied trust engine");
112 throw SecurityPolicyException("Message was signed, but signature could not be verified.");
116 log.debug("signature verified against message issuer");
117 policy.setAuthenticated(true);