6438b8bcec90e44941fc5087a019b40377bd4c17
[shibboleth/cpp-opensaml.git] / saml / binding / impl / ClientCertAuthRule.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * ClientCertAuthRule.cpp
23  * 
24  * TLS client authentication SecurityPolicyRule.
25  */
26
27 #include "internal.h"
28 #include "exceptions.h"
29 #include "binding/SecurityPolicy.h"
30 #include "binding/SecurityPolicyRule.h"
31 #include "saml2/metadata/Metadata.h"
32 #include "saml2/metadata/MetadataCredentialCriteria.h"
33 #include "saml2/metadata/MetadataProvider.h"
34
35 #include <xmltooling/logging.h>
36 #include <xmltooling/io/GenericRequest.h>
37 #include <xmltooling/security/Credential.h>
38 #include <xmltooling/security/X509TrustEngine.h>
39 #include <xmltooling/util/ReplayCache.h>
40
41 using namespace opensaml::saml2md;
42 using namespace opensaml;
43 using namespace xmltooling::logging;
44 using namespace xmltooling;
45 using namespace std;
46
47 namespace opensaml {
48     class SAML_DLLLOCAL ClientCertAuthRule : public SecurityPolicyRule
49     {
50     public:
51         ClientCertAuthRule(const DOMElement* e);
52         virtual ~ClientCertAuthRule() {}
53         
54         const char* getType() const {
55             return CLIENTCERTAUTH_POLICY_RULE;
56         }
57         bool evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
58
59     private:
60         bool m_errorFatal;
61     };
62
63     SecurityPolicyRule* SAML_DLLLOCAL ClientCertAuthRuleFactory(const DOMElement* const & e)
64     {
65         return new ClientCertAuthRule(e);
66     }
67
68     static const XMLCh errorFatal[] = UNICODE_LITERAL_10(e,r,r,o,r,F,a,t,a,l);
69 };
70
71 ClientCertAuthRule::ClientCertAuthRule(const DOMElement* e) : m_errorFatal(XMLHelper::getAttrBool(e, false, errorFatal))
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 }