Major revamp of credential and trust handling code, PKIX engine still needs work.
[shibboleth/cpp-opensaml.git] / saml / binding / impl / XMLSigningRule.cpp
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * XMLSigningRule.cpp
19  * 
20  * XML Signature checking SecurityPolicyRule
21  */
22
23 #include "internal.h"
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"
31
32 #include <log4cpp/Category.hh>
33
34 using namespace opensaml::saml2md;
35 using namespace opensaml;
36 using namespace xmltooling;
37 using namespace log4cpp;
38 using namespace std;
39
40 using xmlsignature::SignatureException;
41
42 namespace opensaml {
43     class SAML_DLLLOCAL XMLSigningRule : public SecurityPolicyRule
44     {
45     public:
46         XMLSigningRule(const DOMElement* e);
47         virtual ~XMLSigningRule() {}
48         
49         void evaluate(const xmltooling::XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
50
51     private:
52         bool m_errorsFatal;
53     };
54
55     SecurityPolicyRule* SAML_DLLLOCAL XMLSigningRuleFactory(const DOMElement* const & e)
56     {
57         return new XMLSigningRule(e);
58     }
59     
60     static const XMLCh errorsFatal[] = UNICODE_LITERAL_11(e,r,r,o,r,s,F,a,t,a,l);
61 };
62
63 XMLSigningRule::XMLSigningRule(const DOMElement* e) : m_errorsFatal(false)
64 {
65     if (e) {
66         const XMLCh* flag = e->getAttributeNS(NULL, errorsFatal);
67         m_errorsFatal = (flag && (*flag==chLatin_t || *flag==chDigit_1)); 
68     }
69 }
70
71 void XMLSigningRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
72 {
73     Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.XMLSigning");
74     
75     if (!policy.getIssuerMetadata()) {
76         log.debug("ignoring message, no issuer metadata supplied");
77         return;
78     }
79     else if (!policy.getTrustEngine()) {
80         log.debug("ignoring message, no TrustEngine supplied");
81         return;
82     }
83     
84     const SignableObject* signable = dynamic_cast<const SignableObject*>(&message);
85     if (!signable || !signable->getSignature())
86         return;
87     
88     log.debug("validating signature profile");
89     try {
90         SignatureProfileValidator sigval;
91         sigval.validateSignature(*(signable->getSignature()));
92     }
93     catch (ValidationException& ve) {
94         log.error("signature profile failed to validate: %s", ve.what());
95         if (m_errorsFatal)
96             throw;
97         return;
98     }
99     
100     // Set up criteria object, including peer name to enforce cert name checking.
101     MetadataCredentialCriteria cc(*(policy.getIssuerMetadata()));
102     auto_ptr_char pn(policy.getIssuer()->getName());
103     cc.setPeerName(pn.get());
104
105     if (!policy.getTrustEngine()->validate(*(signable->getSignature()), *(policy.getMetadataProvider()), &cc)) {
106         log.error("unable to verify message signature with supplied trust engine");
107         if (m_errorsFatal)
108             throw SignatureException("Message was signed, but signature could not be verified.");
109         return;
110     }
111
112     log.debug("signature verified against message issuer");
113     policy.setSecure(true);
114 }