2 * Copyright 2001-2009 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/SecurityPolicyRule.h"
26 #include "saml2/core/Assertions.h"
27 #include "saml2/metadata/Metadata.h"
28 #include "saml2/metadata/MetadataCredentialCriteria.h"
29 #include "saml2/metadata/MetadataProvider.h"
31 #include <xercesc/util/Base64.hpp>
32 #include <xmltooling/logging.h>
33 #include <xmltooling/io/HTTPRequest.h>
34 #include <xmltooling/security/SignatureTrustEngine.h>
36 using namespace opensaml::saml2md;
37 using namespace opensaml;
38 using namespace xmltooling::logging;
39 using namespace xmltooling;
42 using xmlsignature::KeyInfo;
43 using xmlsignature::SignatureException;
46 class SAML_DLLLOCAL SimpleSigningRule : public SecurityPolicyRule
49 SimpleSigningRule(const DOMElement* e);
50 virtual ~SimpleSigningRule() {}
52 const char* getType() const {
53 return SIMPLESIGNING_POLICY_RULE;
55 bool evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
58 // Appends a raw parameter=value pair to the string.
59 static bool appendParameter(string& s, const char* data, const char* name);
64 SecurityPolicyRule* SAML_DLLLOCAL SimpleSigningRuleFactory(const DOMElement* const & e)
66 return new SimpleSigningRule(e);
69 static const XMLCh errorFatal[] = UNICODE_LITERAL_10(e,r,r,o,r,F,a,t,a,l);
72 bool SimpleSigningRule::appendParameter(string& s, const char* data, const char* name)
74 const char* start = strstr(data,name);
79 const char* end = strchr(start,'&');
81 s.append(start, end-start);
87 SimpleSigningRule::SimpleSigningRule(const DOMElement* e) : m_errorFatal(false)
90 const XMLCh* flag = e->getAttributeNS(NULL, errorFatal);
91 m_errorFatal = (flag && (*flag==chLatin_t || *flag==chDigit_1));
95 bool SimpleSigningRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
97 Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.SimpleSigning");
99 if (!policy.getIssuerMetadata()) {
100 log.debug("ignoring message, no issuer metadata supplied");
104 const SignatureTrustEngine* sigtrust;
105 if (!(sigtrust=dynamic_cast<const SignatureTrustEngine*>(policy.getTrustEngine()))) {
106 log.debug("ignoring message, no SignatureTrustEngine supplied");
110 const HTTPRequest* httpRequest = dynamic_cast<const HTTPRequest*>(request);
111 if (!request || !httpRequest)
114 const char* signature = request->getParameter("Signature");
118 const char* sigAlgorithm = request->getParameter("SigAlg");
120 log.error("SigAlg parameter not found, no way to verify the signature");
126 if (!strcmp(httpRequest->getMethod(), "GET")) {
127 // We have to construct a string containing the signature input by accessing the
128 // request directly. We can't use the decoded parameters because we need the raw
129 // data and URL-encoding isn't canonical.
131 // NOTE: SimpleSign for GET means Redirect binding, which means we verify over the
132 // base64-encoded message directly.
134 pch = httpRequest->getQueryString();
135 if (!appendParameter(input, pch, "SAMLRequest="))
136 appendParameter(input, pch, "SAMLResponse=");
137 appendParameter(input, pch, "RelayState=");
138 appendParameter(input, pch, "SigAlg=");
141 // With POST, the input string is concatenated from the decoded form controls.
142 // GET should be this way too, but I messed up the spec, sorry.
144 // NOTE: SimpleSign for POST means POST binding, which means we verify over the
145 // base64-decoded XML. This sucks, because we have to decode the base64 directly.
146 // Serializing the XMLObject doesn't guarantee the signature will verify (this is
147 // why XMLSignature exists, and why this isn't really "simpler").
150 pch = httpRequest->getParameter("SAMLRequest");
152 XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(pch),&x);
154 log.warn("unable to decode base64 in POST binding message");
157 input = string("SAMLRequest=") + reinterpret_cast<const char*>(decoded);
158 #ifdef OPENSAML_XERCESC_HAS_XMLBYTE_RELEASE
159 XMLString::release(&decoded);
161 XMLString::release((char**)&decoded);
165 pch = httpRequest->getParameter("SAMLResponse");
166 XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(pch),&x);
168 log.warn("unable to decode base64 in POST binding message");
171 input = string("SAMLResponse=") + reinterpret_cast<const char*>(decoded);
172 #ifdef OPENSAML_XERCESC_HAS_XMLBYTE_RELEASE
173 XMLString::release(&decoded);
175 XMLString::release((char**)&decoded);
179 pch = httpRequest->getParameter("RelayState");
181 input = input + "&RelayState=" + pch;
182 input = input + "&SigAlg=" + sigAlgorithm;
185 // Check for KeyInfo, but defensively (we might be able to run without it).
186 KeyInfo* keyInfo=NULL;
187 pch = request->getParameter("KeyInfo");
190 XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(pch),&x);
193 istringstream kstrm((char*)decoded);
194 DOMDocument* doc = XMLToolingConfig::getConfig().getParser().parse(kstrm);
195 XercesJanitor<DOMDocument> janitor(doc);
196 XMLObject* kxml = XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true);
198 if (!(keyInfo=dynamic_cast<KeyInfo*>(kxml)))
201 catch (XMLToolingException& ex) {
202 log.warn("Failed to load KeyInfo from message: %s", ex.what());
204 #ifdef OPENSAML_XERCESC_HAS_XMLBYTE_RELEASE
205 XMLString::release(&decoded);
207 XMLString::release((char**)&decoded);
211 log.warn("Failed to load KeyInfo from message: Unable to decode base64-encoded KeyInfo.");
215 auto_ptr<KeyInfo> kjanitor(keyInfo);
216 auto_ptr_XMLCh alg(sigAlgorithm);
218 // Set up criteria object.
219 MetadataCredentialCriteria cc(*(policy.getIssuerMetadata()));
220 cc.setXMLAlgorithm(alg.get());
222 if (!sigtrust->validate(alg.get(), signature, keyInfo, input.c_str(), input.length(), *(policy.getMetadataProvider()), &cc)) {
223 log.error("unable to verify message signature with supplied trust engine");
225 throw SecurityPolicyException("Message was signed, but signature could not be verified.");
229 log.debug("signature verified against message issuer");
230 policy.setAuthenticated(true);