Convert from NULL macro to nullptr, remove unused zlib code.
[shibboleth/cpp-opensaml.git] / saml / binding / impl / ClientCertAuthRule.cpp
1 /*
2  *  Copyright 2001-2010 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/SecurityPolicy.h"
26 #include "binding/SecurityPolicyRule.h"
27 #include "saml2/metadata/Metadata.h"
28 #include "saml2/metadata/MetadataCredentialCriteria.h"
29 #include "saml2/metadata/MetadataProvider.h"
30
31 #include <xmltooling/logging.h>
32 #include <xmltooling/io/GenericRequest.h>
33 #include <xmltooling/security/Credential.h>
34 #include <xmltooling/security/X509TrustEngine.h>
35 #include <xmltooling/util/ReplayCache.h>
36
37 using namespace opensaml::saml2md;
38 using namespace opensaml;
39 using namespace xmltooling::logging;
40 using namespace xmltooling;
41 using namespace std;
42
43 namespace opensaml {
44     class SAML_DLLLOCAL ClientCertAuthRule : public SecurityPolicyRule
45     {
46     public:
47         ClientCertAuthRule(const DOMElement* e);
48         virtual ~ClientCertAuthRule() {}
49         
50         const char* getType() const {
51             return CLIENTCERTAUTH_POLICY_RULE;
52         }
53         bool evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
54
55     private:
56         bool m_errorFatal;
57     };
58
59     SecurityPolicyRule* SAML_DLLLOCAL ClientCertAuthRuleFactory(const DOMElement* const & e)
60     {
61         return new ClientCertAuthRule(e);
62     }
63
64     static const XMLCh errorFatal[] = UNICODE_LITERAL_10(e,r,r,o,r,F,a,t,a,l);
65 };
66
67 ClientCertAuthRule::ClientCertAuthRule(const DOMElement* e) : m_errorFatal(false)
68 {
69     if (e) {
70         const XMLCh* flag = e->getAttributeNS(nullptr, errorFatal);
71         m_errorFatal = (flag && (*flag==chLatin_t || *flag==chDigit_1)); 
72     }
73 }
74
75 bool ClientCertAuthRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
76 {
77     Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.ClientCertAuth");
78     
79     if (!request)
80         return false;
81     
82     if (!policy.getIssuerMetadata()) {
83         log.debug("ignoring message, no issuer metadata supplied");
84         return false;
85     }
86
87     const X509TrustEngine* x509trust;
88     if (!(x509trust=dynamic_cast<const X509TrustEngine*>(policy.getTrustEngine()))) {
89         log.debug("ignoring message, no X509TrustEngine supplied");
90         return false;
91     }
92     
93     const vector<XSECCryptoX509*>& chain = request->getClientCertificates();
94     if (chain.empty())
95         return false;
96     
97     // Set up criteria object, including peer name to enforce cert name checking.
98     MetadataCredentialCriteria cc(*(policy.getIssuerMetadata()));
99     auto_ptr_char pn(policy.getIssuer()->getName());
100     cc.setPeerName(pn.get());
101     cc.setUsage(Credential::TLS_CREDENTIAL);
102
103     if (!x509trust->validate(chain.front(), chain, *(policy.getMetadataProvider()), &cc)) {
104         if (m_errorFatal)
105             throw SecurityPolicyException("Client certificate supplied, but could not be verified.");
106         log.error("unable to verify certificate chain with supplied trust engine");
107         return false;
108     }
109     
110     log.debug("client certificate verified against message issuer");
111     policy.setAuthenticated(true);
112     return true;
113 }