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.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 * ClientCertAuthRule.cpp
24 * TLS client authentication SecurityPolicyRule.
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"
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>
41 using namespace opensaml::saml2md;
42 using namespace opensaml;
43 using namespace xmltooling::logging;
44 using namespace xmltooling;
48 class SAML_DLLLOCAL ClientCertAuthRule : public SecurityPolicyRule
51 ClientCertAuthRule(const DOMElement* e);
52 virtual ~ClientCertAuthRule() {}
54 const char* getType() const {
55 return CLIENTCERTAUTH_POLICY_RULE;
57 bool evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
63 SecurityPolicyRule* SAML_DLLLOCAL ClientCertAuthRuleFactory(const DOMElement* const & e)
65 return new ClientCertAuthRule(e);
68 static const XMLCh errorFatal[] = UNICODE_LITERAL_10(e,r,r,o,r,F,a,t,a,l);
71 ClientCertAuthRule::ClientCertAuthRule(const DOMElement* e) : m_errorFatal(XMLHelper::getAttrBool(e, false, errorFatal))
75 bool ClientCertAuthRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
77 Category& log=Category::getInstance(SAML_LOGCAT ".SecurityPolicyRule.ClientCertAuth");
82 if (!policy.getIssuerMetadata()) {
83 log.debug("ignoring message, no issuer metadata supplied");
87 const X509TrustEngine* x509trust;
88 if (!(x509trust=dynamic_cast<const X509TrustEngine*>(policy.getTrustEngine()))) {
89 log.debug("ignoring message, no X509TrustEngine supplied");
93 const vector<XSECCryptoX509*>& chain = request->getClientCertificates();
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);
103 if (!x509trust->validate(chain.front(), chain, *(policy.getMetadataProvider()), &cc)) {
105 throw SecurityPolicyException("Client certificate supplied, but could not be verified.");
106 log.error("unable to verify certificate chain with supplied trust engine");
110 log.debug("client certificate verified against message issuer");
111 policy.setAuthenticated(true);