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.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 * XML Signature checking SecurityPolicyRule.
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"
37 #include <xmltooling/logging.h>
38 #include <xmltooling/security/SignatureTrustEngine.h>
39 #include <xmltooling/signature/Signature.h>
41 using namespace opensaml::saml2md;
42 using namespace opensaml;
43 using namespace xmltooling::logging;
44 using namespace xmltooling;
47 using xmlsignature::SignatureException;
50 class SAML_DLLLOCAL XMLSigningRule : public SecurityPolicyRule
53 XMLSigningRule(const DOMElement* e);
54 virtual ~XMLSigningRule() {}
56 const char* getType() const {
57 return XMLSIGNING_POLICY_RULE;
59 bool evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
65 SecurityPolicyRule* SAML_DLLLOCAL XMLSigningRuleFactory(const DOMElement* const & e)
67 return new XMLSigningRule(e);
70 static const XMLCh errorFatal[] = UNICODE_LITERAL_10(e,r,r,o,r,F,a,t,a,l);
73 XMLSigningRule::XMLSigningRule(const DOMElement* e) : m_errorFatal(XMLHelper::getAttrBool(e, false, errorFatal))
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);