f1850903fe1c6e88c267d3c04586b2229186e573
[shibboleth/cpp-opensaml.git] / saml / binding / impl / ClientCertAuthRule.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  * ClientCertAuthRule.cpp
19  * 
20  * TLS client authentication SecurityPolicyRule
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "binding/SecurityPolicyRule.h"
26 #include "saml2/metadata/Metadata.h"
27 #include "saml2/metadata/MetadataProvider.h"
28
29 #include <xmltooling/security/X509TrustEngine.h>
30 #include <xmltooling/util/ReplayCache.h>
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     class SAML_DLLLOCAL ClientCertAuthRule : public SecurityPolicyRule
41     {
42     public:
43         ClientCertAuthRule(const DOMElement* e) {}
44         virtual ~ClientCertAuthRule() {}
45         
46         void evaluate(const xmltooling::XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
47     };
48
49     SecurityPolicyRule* SAML_DLLLOCAL ClientCertAuthRuleFactory(const DOMElement* const & e)
50     {
51         return new ClientCertAuthRule(e);
52     }
53 };
54
55 void ClientCertAuthRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
56 {
57     Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.ClientCertAuth");
58     
59     if (!request)
60         return;
61     
62     if (!policy.getIssuerMetadata()) {
63         log.debug("ignoring message, no issuer metadata supplied");
64         return;
65     }
66
67     const X509TrustEngine* x509trust;
68     if (!(x509trust=dynamic_cast<const X509TrustEngine*>(policy.getTrustEngine()))) {
69         log.debug("ignoring message, no X509TrustEngine supplied");
70         return;
71     }
72     
73     const std::vector<XSECCryptoX509*>& chain = request->getClientCertificates();
74     if (chain.empty())
75         return;
76     
77     if (!x509trust->validate(chain.front(), chain, *(policy.getIssuerMetadata()), true,
78             policy.getMetadataProvider()->getKeyResolver())) {
79         log.error("unable to verify certificate chain with supplied trust engine");
80         return;
81     }
82     
83     log.debug("client certificate verified against message issuer");
84     policy.setSecure(true);
85 }