Update copyright.
[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/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"
30
31 #include <log4cpp/Category.hh>
32
33 using namespace opensaml::saml2md;
34 using namespace opensaml;
35 using namespace xmltooling;
36 using namespace log4cpp;
37 using namespace std;
38
39 namespace opensaml {
40     SecurityPolicyRule* SAML_DLLLOCAL XMLSigningRuleFactory(const DOMElement* const & e)
41     {
42         return new XMLSigningRule(e);
43     }
44 };
45
46 void XMLSigningRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
47 {
48     Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.XMLSigning");
49     log.debug("evaluating message signing policy");
50     
51     if (!policy.getIssuerMetadata()) {
52         log.debug("ignoring message, no issuer metadata supplied");
53         return;
54     }
55     else if (!policy.getTrustEngine()) {
56         log.debug("ignoring message, no TrustEngine supplied");
57         return;
58     }
59     
60     const SignableObject* signable = dynamic_cast<const SignableObject*>(&message);
61     if (!signable || !signable->getSignature()) {
62         log.debug("ignoring unsigned or unrecognized message");
63         return;
64     }
65     
66     log.debug("validating signature profile");
67     try {
68         SignatureProfileValidator sigval;
69         sigval.validate(signable->getSignature());
70     }
71     catch (ValidationException& ve) {
72         log.error("signature profile failed to validate: %s", ve.what());
73         return;
74     }
75     
76     if (!policy.getTrustEngine()->validate(
77             *(signable->getSignature()), *(policy.getIssuerMetadata()), policy.getMetadataProvider()->getKeyResolver()
78             )) {
79         log.error("unable to verify message signature with supplied trust engine");
80         return;
81     }
82
83     log.debug("signature verified against message issuer");
84     policy.setSecure(true);
85 }