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