2 * Copyright 2001-2007 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SimpleSigningRule.cpp
20 * Blob-oriented signature checking SecurityPolicyRule
24 #include "exceptions.h"
25 #include "binding/HTTPRequest.h"
26 #include "binding/SecurityPolicyRule.h"
27 #include "saml2/core/Assertions.h"
28 #include "saml2/metadata/Metadata.h"
29 #include "saml2/metadata/MetadataProvider.h"
31 #include <log4cpp/Category.hh>
32 #include <xercesc/util/Base64.hpp>
34 using namespace opensaml::saml2md;
35 using namespace opensaml;
36 using namespace xmltooling;
37 using namespace log4cpp;
40 using xmlsignature::KeyInfo;
41 using xmlsignature::SignatureException;
44 class SAML_DLLLOCAL SimpleSigningRule : public SecurityPolicyRule
47 SimpleSigningRule(const DOMElement* e);
48 virtual ~SimpleSigningRule() {}
50 void evaluate(const xmltooling::XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
53 // Appends a raw parameter=value pair to the string.
54 static bool appendParameter(string& s, const char* data, const char* name);
59 SecurityPolicyRule* SAML_DLLLOCAL SimpleSigningRuleFactory(const DOMElement* const & e)
61 return new SimpleSigningRule(e);
64 static const XMLCh errorsFatal[] = UNICODE_LITERAL_11(e,r,r,o,r,s,F,a,t,a,l);
67 bool SimpleSigningRule::appendParameter(string& s, const char* data, const char* name)
69 const char* start = strstr(data,name);
74 const char* end = strchr(start,'&');
76 s.append(start, end-start);
82 SimpleSigningRule::SimpleSigningRule(const DOMElement* e) : m_errorsFatal(false)
85 const XMLCh* flag = e->getAttributeNS(NULL, errorsFatal);
86 m_errorsFatal = (flag && (*flag==chLatin_t || *flag==chDigit_1));
90 void SimpleSigningRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
92 Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.SimpleSigning");
94 if (!policy.getIssuerMetadata()) {
95 log.debug("ignoring message, no issuer metadata supplied");
98 else if (!policy.getTrustEngine()) {
99 log.debug("ignoring message, no TrustEngine supplied");
103 const HTTPRequest* httpRequest = dynamic_cast<const HTTPRequest*>(request);
104 if (!request || !httpRequest)
107 const char* signature = request->getParameter("Signature");
111 const char* sigAlgorithm = request->getParameter("SigAlg");
113 log.error("SigAlg parameter not found, no way to verify the signature");
119 if (!strcmp(httpRequest->getMethod(), "GET")) {
120 // We have to construct a string containing the signature input by accessing the
121 // request directly. We can't use the decoded parameters because we need the raw
122 // data and URL-encoding isn't canonical.
124 // NOTE: SimpleSign for GET means Redirect binding, which means we verify over the
125 // base64-encoded message directly.
127 pch = httpRequest->getQueryString();
128 if (!appendParameter(input, pch, "SAMLRequest="))
129 appendParameter(input, pch, "SAMLResponse=");
130 appendParameter(input, pch, "RelayState=");
131 appendParameter(input, pch, "SigAlg=");
134 // With POST, the input string is concatenated from the decoded form controls.
135 // GET should be this way too, but I messed up the spec, sorry.
137 // NOTE: SimpleSign for POST means POST binding, which means we verify over the
138 // base64-decoded XML. This sucks, because we have to decode the base64 directly.
139 // Serializing the XMLObject doesn't guarantee the signature will verify (this is
140 // why XMLSignature exists, and why this isn't really "simpler").
143 pch = httpRequest->getParameter("SAMLRequest");
145 XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(pch),&x);
147 log.warn("unable to decode base64 in POST binding message");
150 input = string("SAMLRequest=") + reinterpret_cast<const char*>(decoded);
151 XMLString::release(&decoded);
154 pch = httpRequest->getParameter("SAMLResponse");
155 XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(pch),&x);
157 log.warn("unable to decode base64 in POST binding message");
160 input = string("SAMLResponse=") + reinterpret_cast<const char*>(decoded);
161 XMLString::release(&decoded);
164 pch = httpRequest->getParameter("RelayState");
166 input = input + "&RelayState=" + pch;
167 input = input + "&SigAlg=" + sigAlgorithm;
170 // Check for KeyInfo, but defensively (we might be able to run without it).
171 KeyInfo* keyInfo=NULL;
172 pch = request->getParameter("KeyInfo");
175 istringstream kstrm(pch);
176 DOMDocument* doc = XMLToolingConfig::getConfig().getParser().parse(kstrm);
177 XercesJanitor<DOMDocument> janitor(doc);
178 XMLObject* kxml = XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true);
180 if (!(keyInfo=dynamic_cast<KeyInfo*>(kxml)))
183 catch (XMLToolingException& ex) {
184 log.warn("Failed to load KeyInfo from message: %s", ex.what());
188 auto_ptr<KeyInfo> kjanitor(keyInfo);
189 auto_ptr_XMLCh alg(sigAlgorithm);
191 if (!policy.getTrustEngine()->validate(
192 alg.get(), signature, keyInfo, input.c_str(), input.length(),
193 *(policy.getIssuerMetadata()), policy.getMetadataProvider()->getKeyResolver()
195 log.error("unable to verify message signature with supplied trust engine");
197 throw SignatureException("Message was signed, but signature could not be verified.");
201 log.debug("signature verified against message issuer");
202 policy.setSecure(true);