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/XMLSigningRule.h"
26 #include "saml2/core/Assertions.h"
27 #include "saml2/metadata/Metadata.h"
28 #include "saml2/metadata/MetadataProvider.h"
29 #include "signature/SignatureProfileValidator.h"
31 #include <log4cpp/Category.hh>
33 using namespace opensaml::saml2md;
34 using namespace opensaml;
35 using namespace xmltooling;
36 using namespace log4cpp;
39 using xmlsignature::SignatureException;
42 SecurityPolicyRule* SAML_DLLLOCAL XMLSigningRuleFactory(const DOMElement* const & e)
44 return new XMLSigningRule(e);
47 static const XMLCh errorsFatal[] = UNICODE_LITERAL_11(e,r,r,o,r,s,F,a,t,a,l);
50 XMLSigningRule::XMLSigningRule(const DOMElement* e) : m_errorsFatal(false)
53 const XMLCh* flag = e->getAttributeNS(NULL, errorsFatal);
54 m_errorsFatal = (flag && (*flag==chLatin_t || *flag==chDigit_1));
58 void XMLSigningRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
60 Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.XMLSigning");
61 log.debug("evaluating message signing policy");
63 if (!policy.getIssuerMetadata()) {
64 log.debug("ignoring message, no issuer metadata supplied");
67 else if (!policy.getTrustEngine()) {
68 log.debug("ignoring message, no TrustEngine supplied");
72 const SignableObject* signable = dynamic_cast<const SignableObject*>(&message);
73 if (!signable || !signable->getSignature()) {
74 log.debug("ignoring unsigned or unrecognized message");
78 log.debug("validating signature profile");
80 SignatureProfileValidator sigval;
81 sigval.validate(signable->getSignature());
83 catch (ValidationException& ve) {
84 log.error("signature profile failed to validate: %s", ve.what());
90 if (!policy.getTrustEngine()->validate(
91 *(signable->getSignature()), *(policy.getIssuerMetadata()), policy.getMetadataProvider()->getKeyResolver()
93 log.error("unable to verify message signature with supplied trust engine");
95 throw SignatureException("Message was signed, but signature could not be verified.");
99 log.debug("signature verified against message issuer");
100 policy.setSecure(true);