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);
~XMLSigningRule()54         virtual ~XMLSigningRule() {}
55 
getType() const56         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 
XMLSigningRuleFactory(const DOMElement * const & e,bool)65     SecurityPolicyRule* SAML_DLLLOCAL XMLSigningRuleFactory(const DOMElement* const & e, bool)
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 
XMLSigningRule(const DOMElement * e)73 XMLSigningRule::XMLSigningRule(const DOMElement* e)
74     : SecurityPolicyRule(e), m_errorFatal(XMLHelper::getAttrBool(e, false, errorFatal))
75 {
76 }
77 
evaluate(const XMLObject & message,const GenericRequest * request,SecurityPolicy & policy) const78 bool XMLSigningRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
79 {
80     if (!SecurityPolicyRule::evaluate(message, request, policy)) {
81         return false;
82     }
83 
84     Category& log=Category::getInstance(SAML_LOGCAT ".SecurityPolicyRule.XMLSigning");
85 
86     if (!policy.getIssuerMetadata()) {
87         log.debug("ignoring message, no issuer metadata supplied");
88         return false;
89     }
90 
91     const SignatureTrustEngine* sigtrust;
92     if (!(sigtrust=dynamic_cast<const SignatureTrustEngine*>(policy.getTrustEngine()))) {
93         log.debug("ignoring message, no SignatureTrustEngine supplied");
94         return false;
95     }
96 
97     const SignableObject* signable = dynamic_cast<const SignableObject*>(&message);
98     if (!signable || !signable->getSignature())
99         return false;
100 
101     log.debug("validating signature profile");
102     try {
103         SignatureProfileValidator sigval;
104         sigval.validateSignature(*(signable->getSignature()));
105     }
106     catch (ValidationException& ve) {
107         log.warn("signature profile failed to validate: %s", ve.what());
108         if (m_errorFatal)
109             throw;
110         return false;
111     }
112 
113     // Set up criteria object.
114     MetadataCredentialCriteria cc(*(policy.getIssuerMetadata()));
115 
116     if (!sigtrust->validate(*(signable->getSignature()), *(policy.getMetadataProvider()), &cc)) {
117         log.warn("unable to verify message signature with supplied trust engine");
118         if (m_errorFatal)
119             throw SecurityPolicyException("Message was signed, but signature could not be verified.");
120         return false;
121     }
122 
123     log.debug("signature verified against message issuer");
124     policy.setAuthenticated(true);
125     return true;
126 }
127