Raw signature trust support, Redirect binding, "simple" signing rule.
[shibboleth/cpp-opensaml.git] / saml / binding / impl / SimpleSigningRule.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  * SimpleSigningRule.cpp
19  * 
20  * Blob-oriented signature checking SecurityPolicyRule
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "RootObject.h"
26 #include "binding/HTTPRequest.h"
27 #include "binding/SimpleSigningRule.h"
28 #include "saml2/core/Protocols.h"
29 #include "saml2/metadata/Metadata.h"
30 #include "saml2/metadata/MetadataProvider.h"
31 #include "security/TrustEngine.h"
32
33 #include <log4cpp/Category.hh>
34 #include <xmltooling/util/NDC.h>
35 #include <xmltooling/util/ReplayCache.h>
36 #include <xsec/enc/XSECCryptoException.hpp>
37 #include <xsec/enc/XSECCryptoProvider.hpp>
38 #include <xsec/framework/XSECException.hpp>
39
40 using namespace opensaml::saml2md;
41 using namespace opensaml;
42 using namespace xmltooling;
43 using namespace log4cpp;
44 using namespace std;
45
46 namespace opensaml {
47     SecurityPolicyRule* SAML_DLLLOCAL SimpleSigningRuleFactory(const DOMElement* const & e)
48     {
49         return new SimpleSigningRule(e);
50     }
51
52     // Appends a raw parameter=value pair to the string.
53     static bool appendParameter(string& s, const char* data, const char* name)
54     {
55         const char* start = strstr(data,name);
56         if (!start)
57             return false;
58         if (!s.empty())
59             s += '&';
60         const char* end = strchr(start,'&');
61         if (end)
62             s.append(start, end-start);
63         else
64             s.append(start);
65         return true;
66     }
67 };
68
69
70 pair<saml2::Issuer*,const saml2md::RoleDescriptor*> SimpleSigningRule::evaluate(
71     const GenericRequest& request,
72     const XMLObject& message,
73     const MetadataProvider* metadataProvider,
74     const QName* role,
75     const opensaml::TrustEngine* trustEngine
76     ) const
77 {
78     Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.SimpleSigning");
79     log.debug("evaluating simple signing policy");
80     
81     pair<saml2::Issuer*,const RoleDescriptor*> ret = pair<saml2::Issuer*,const RoleDescriptor*>(NULL,NULL);  
82     
83     if (!metadataProvider || !role || !trustEngine) {
84         log.debug("ignoring message, no metadata supplied");
85         return ret;
86     }
87     
88     const char* signature = request.getParameter("Signature");
89     if (!signature) {
90         log.debug("ignoring unsigned message");
91         return ret;
92     }
93     
94     const char* sigAlgorithm = request.getParameter("SigAlg");
95     if (!sigAlgorithm) {
96         log.error("SigAlg parameter not found, no way to verify the signature");
97         return ret;
98     }
99
100     try {
101         log.debug("extracting issuer from message");
102         pair<saml2::Issuer*,const XMLCh*> issuerInfo = getIssuerAndProtocol(message);
103         
104         auto_ptr<saml2::Issuer> issuer(issuerInfo.first);
105         if (!issuerInfo.first || !issuerInfo.second ||
106                 (issuer->getFormat() && !XMLString::equals(issuer->getFormat(), saml2::NameIDType::ENTITY))) {
107             log.warn("issuer identity not estabished, or was not an entityID");
108             return ret;
109         }
110         
111         log.debug("searching metadata for message issuer...");
112         const EntityDescriptor* entity = metadataProvider->getEntityDescriptor(issuer->getName());
113         if (!entity) {
114             auto_ptr_char temp(issuer->getName());
115             log.warn("no metadata found, can't establish identity of issuer (%s)", temp.get());
116             return ret;
117         }
118
119         log.debug("matched assertion issuer against metadata, searching for applicable role...");
120         const RoleDescriptor* roledesc=entity->getRoleDescriptor(*role, issuerInfo.second);
121         if (!roledesc) {
122             log.warn("unable to find compatible role (%s) in metadata", role->toString().c_str());
123             return ret;
124         }
125
126         // We have to construct a string containing the signature input by accessing the
127         // request directly. We can't use the decoded parameters because we need the raw
128         // data and URL-encoding isn't canonical.
129         string input;
130         const HTTPRequest& httpRequest = dynamic_cast<const HTTPRequest&>(request);
131         const char* raw =
132             (!strcmp(httpRequest.getMethod(), "GET")) ? httpRequest.getQueryString() : httpRequest.getRequestBody();
133         if (!appendParameter(input, raw, "SAMLRequest="))
134             appendParameter(input, raw, "SAMLResponse=");
135         appendParameter(input, raw, "RelayState=");
136         appendParameter(input, raw, "SigAlg=");
137
138         auto_ptr_XMLCh alg(sigAlgorithm);
139         if (!trustEngine->validate(alg.get(), signature, NULL, input.c_str(), input.length(), *roledesc, metadataProvider->getKeyResolver())) {
140             log.error("unable to verify signature on message with supplied trust engine");
141             return ret;
142         }
143
144         if (log.isDebugEnabled()) {
145             auto_ptr_char iname(entity->getEntityID());
146             log.debug("message from (%s), signature verified", iname.get());
147         }
148         
149         ret.first = issuer.release();
150         ret.second = roledesc;
151     }
152     catch (bad_cast&) {
153         // Just trap it.
154         log.warn("caught a bad_cast while extracting issuer");
155     }
156     return ret;
157 }
158
159 pair<saml2::Issuer*,const XMLCh*> SimpleSigningRule::getIssuerAndProtocol(const XMLObject& message) const
160 {
161     // We just let any bad casts throw here.
162
163     // Shortcuts some of the casting.
164     const XMLCh* ns = message.getElementQName().getNamespaceURI();
165     if (ns) {
166         if (XMLString::equals(ns, samlconstants::SAML20P_NS) || XMLString::equals(ns, samlconstants::SAML20_NS)) {
167             // 2.0 namespace should be castable to a specialized 2.0 root.
168             const saml2::RootObject& root = dynamic_cast<const saml2::RootObject&>(message);
169             saml2::Issuer* issuer = root.getIssuer();
170             if (issuer && issuer->getName()) {
171                 return make_pair(issuer->cloneIssuer(), samlconstants::SAML20P_NS);
172             }
173             
174             // No issuer in the message, so we have to try the Response approach. 
175             const vector<saml2::Assertion*>& assertions = dynamic_cast<const saml2p::Response&>(message).getAssertions();
176             if (!assertions.empty()) {
177                 issuer = assertions.front()->getIssuer();
178                 if (issuer && issuer->getName())
179                     return make_pair(issuer->cloneIssuer(), samlconstants::SAML20P_NS);
180             }
181         }
182     }
183     return pair<saml2::Issuer*,const XMLCh*>(NULL,NULL);
184 }