Allow message-only policy rules, basic SAML SOAP client.
[shibboleth/cpp-opensaml.git] / saml / binding / impl / ClientCertAuthRule.cpp
1 /*
2  *  Copyright 2001-2006 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  * XML Signature checking SecurityPolicyRule
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "binding/ClientCertAuthRule.h"
26 #include "saml2/metadata/Metadata.h"
27 #include "saml2/metadata/MetadataProvider.h"
28
29 #include <xmltooling/security/X509TrustEngine.h>
30 #include <xmltooling/util/NDC.h>
31 #include <xmltooling/util/ReplayCache.h>
32 #include <log4cpp/Category.hh>
33
34 using namespace opensaml::saml2md;
35 using namespace opensaml;
36 using namespace xmltooling;
37 using namespace log4cpp;
38 using namespace std;
39
40 namespace opensaml {
41     SecurityPolicyRule* SAML_DLLLOCAL ClientCertAuthRuleFactory(const DOMElement* const & e)
42     {
43         return new ClientCertAuthRule(e);
44     }
45 };
46
47 pair<saml2::Issuer*,const RoleDescriptor*> ClientCertAuthRule::evaluate(
48     const XMLObject& message,
49     const GenericRequest* request,
50     const MetadataProvider* metadataProvider,
51     const QName* role,
52     const TrustEngine* trustEngine
53     ) const
54 {
55     Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.ClientCertAuth");
56     log.debug("evaluating client certificate authentication policy");
57     
58     pair<saml2::Issuer*,const RoleDescriptor*> ret = pair<saml2::Issuer*,const RoleDescriptor*>(NULL,NULL);
59     if (!request) {
60         log.debug("ignoring message, no protocol request available");
61         return ret;
62     }  
63     
64     const X509TrustEngine* x509trust;
65     if (!metadataProvider || !role || !(x509trust=dynamic_cast<const X509TrustEngine*>(trustEngine))) {
66         log.debug("ignoring message, no metadata or X509TrustEngine supplied");
67         return ret;
68     }
69     
70     const std::vector<XSECCryptoX509*>& chain = request->getClientCertificates();
71     if (chain.empty()) {
72         log.debug("ignoring message, no client certificates in request");
73         return ret;
74     }
75     
76     try {
77         log.debug("extracting issuer from message");
78         pair<saml2::Issuer*,const XMLCh*> issuerInfo = getIssuerAndProtocol(message);
79         
80         auto_ptr<saml2::Issuer> issuer(issuerInfo.first);
81         if (!issuerInfo.first || !issuerInfo.second ||
82                 (issuer->getFormat() && !XMLString::equals(issuer->getFormat(), saml2::NameIDType::ENTITY))) {
83             log.warn("issuer identity not estabished, or was not an entityID");
84             return ret;
85         }
86         
87         log.debug("searching metadata for message issuer...");
88         const EntityDescriptor* entity = metadataProvider->getEntityDescriptor(issuer->getName());
89         if (!entity) {
90             auto_ptr_char temp(issuer->getName());
91             log.warn("no metadata found, can't establish identity of issuer (%s)", temp.get());
92             return ret;
93         }
94
95         log.debug("matched message issuer against metadata, searching for applicable role...");
96         const RoleDescriptor* roledesc=entity->getRoleDescriptor(*role, issuerInfo.second);
97         if (!roledesc) {
98             log.warn("unable to find compatible role (%s) in metadata", role->toString().c_str());
99             return ret;
100         }
101
102         if (!x509trust->validate(chain.front(), chain, *roledesc, true, metadataProvider->getKeyResolver())) {
103             log.error("unable to verify certificate chain with supplied trust engine");
104             return ret;
105         }
106
107         if (log.isDebugEnabled()) {
108             auto_ptr_char iname(entity->getEntityID());
109             log.debug("message from (%s), signature verified", iname.get());
110         }
111         
112         ret.first = issuer.release();
113         ret.second = roledesc;
114     }
115     catch (bad_cast&) {
116         // Just trap it.
117         log.warn("caught a bad_cast while extracting issuer");
118     }
119     return ret;
120 }
121
122 pair<saml2::Issuer*,const XMLCh*> ClientCertAuthRule::getIssuerAndProtocol(const XMLObject& message) const
123 {
124     // We just let any bad casts throw here.
125
126     // Shortcuts some of the casting.
127     const XMLCh* ns = message.getElementQName().getNamespaceURI();
128     if (ns) {
129         if (XMLString::equals(ns, samlconstants::SAML20P_NS)) {
130             // 2.0 namespace should be castable to a specialized 2.0 root.
131             const saml2::RootObject& root = dynamic_cast<const saml2::RootObject&>(message);
132             saml2::Issuer* issuer = root.getIssuer();
133             if (issuer && issuer->getName())
134                 return make_pair(issuer->cloneIssuer(), samlconstants::SAML20P_NS);
135         }
136     }
137     return pair<saml2::Issuer*,const XMLCh*>(NULL,NULL);
138 }