2 * Copyright 2001-2009 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/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"
33 #include <xmltooling/logging.h>
34 #include <xmltooling/security/SignatureTrustEngine.h>
35 #include <xmltooling/signature/Signature.h>
37 using namespace opensaml::saml2md;
38 using namespace opensaml;
39 using namespace xmltooling::logging;
40 using namespace xmltooling;
43 using xmlsignature::SignatureException;
46 class SAML_DLLLOCAL XMLSigningRule : public SecurityPolicyRule
49 XMLSigningRule(const DOMElement* e);
50 virtual ~XMLSigningRule() {}
52 const char* getType() const {
53 return XMLSIGNING_POLICY_RULE;
55 bool evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
61 SecurityPolicyRule* SAML_DLLLOCAL XMLSigningRuleFactory(const DOMElement* const & e)
63 return new XMLSigningRule(e);
66 static const XMLCh errorFatal[] = UNICODE_LITERAL_10(e,r,r,o,r,F,a,t,a,l);
69 XMLSigningRule::XMLSigningRule(const DOMElement* e) : m_errorFatal(false)
72 const XMLCh* flag = e->getAttributeNS(NULL, errorFatal);
73 m_errorFatal = (flag && (*flag==chLatin_t || *flag==chDigit_1));
77 bool XMLSigningRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
79 Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.XMLSigning");
81 if (!policy.getIssuerMetadata()) {
82 log.debug("ignoring message, no issuer metadata supplied");
86 const SignatureTrustEngine* sigtrust;
87 if (!(sigtrust=dynamic_cast<const SignatureTrustEngine*>(policy.getTrustEngine()))) {
88 log.debug("ignoring message, no SignatureTrustEngine supplied");
92 const SignableObject* signable = dynamic_cast<const SignableObject*>(&message);
93 if (!signable || !signable->getSignature())
96 log.debug("validating signature profile");
98 SignatureProfileValidator sigval;
99 sigval.validateSignature(*(signable->getSignature()));
101 catch (ValidationException& ve) {
102 log.error("signature profile failed to validate: %s", ve.what());
108 // Set up criteria object.
109 MetadataCredentialCriteria cc(*(policy.getIssuerMetadata()));
111 if (!sigtrust->validate(*(signable->getSignature()), *(policy.getMetadataProvider()), &cc)) {
112 log.error("unable to verify message signature with supplied trust engine");
114 throw SecurityPolicyException("Message was signed, but signature could not be verified.");
118 log.debug("signature verified against message issuer");
119 policy.setAuthenticated(true);