Convert logging to log4shib via compile time switch.
[shibboleth/opensaml2.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/MetadataCredentialCriteria.h"
28 #include "saml2/metadata/MetadataProvider.h"
29
30 #include <xmltooling/logging.h>
31 #include <xmltooling/security/X509TrustEngine.h>
32 #include <xmltooling/util/ReplayCache.h>
33
34 using namespace opensaml::saml2md;
35 using namespace opensaml;
36 using namespace xmltooling::logging;
37 using namespace xmltooling;
38 using namespace std;
39
40 namespace opensaml {
41     class SAML_DLLLOCAL ClientCertAuthRule : public SecurityPolicyRule
42     {
43     public:
44         ClientCertAuthRule(const DOMElement* e) {}
45         virtual ~ClientCertAuthRule() {}
46         
47         void evaluate(const xmltooling::XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
48     };
49
50     SecurityPolicyRule* SAML_DLLLOCAL ClientCertAuthRuleFactory(const DOMElement* const & e)
51     {
52         return new ClientCertAuthRule(e);
53     }
54 };
55
56 void ClientCertAuthRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
57 {
58     Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.ClientCertAuth");
59     
60     if (!request)
61         return;
62     
63     if (!policy.getIssuerMetadata()) {
64         log.debug("ignoring message, no issuer metadata supplied");
65         return;
66     }
67
68     const X509TrustEngine* x509trust;
69     if (!(x509trust=dynamic_cast<const X509TrustEngine*>(policy.getTrustEngine()))) {
70         log.debug("ignoring message, no X509TrustEngine supplied");
71         return;
72     }
73     
74     const std::vector<XSECCryptoX509*>& chain = request->getClientCertificates();
75     if (chain.empty())
76         return;
77     
78     // Set up criteria object, including peer name to enforce cert name checking.
79     MetadataCredentialCriteria cc(*(policy.getIssuerMetadata()));
80     auto_ptr_char pn(policy.getIssuer()->getName());
81     cc.setPeerName(pn.get());
82     cc.setUsage(CredentialCriteria::TLS_CREDENTIAL);
83
84     if (!x509trust->validate(chain.front(), chain, *(policy.getMetadataProvider()), &cc)) {
85         log.error("unable to verify certificate chain with supplied trust engine");
86         return;
87     }
88     
89     log.debug("client certificate verified against message issuer");
90     policy.setSecure(true);
91 }