2 * Copyright 2001-2006 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 "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"
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>
40 using namespace opensaml::saml2md;
41 using namespace opensaml;
42 using namespace xmltooling;
43 using namespace log4cpp;
46 using xmlsignature::KeyInfo;
49 SecurityPolicyRule* SAML_DLLLOCAL SimpleSigningRuleFactory(const DOMElement* const & e)
51 return new SimpleSigningRule(e);
54 // Appends a raw parameter=value pair to the string.
55 static bool appendParameter(string& s, const char* data, const char* name)
57 const char* start = strstr(data,name);
62 const char* end = strchr(start,'&');
64 s.append(start, end-start);
72 pair<saml2::Issuer*,const saml2md::RoleDescriptor*> SimpleSigningRule::evaluate(
73 const GenericRequest& request,
74 const XMLObject& message,
75 const MetadataProvider* metadataProvider,
77 const opensaml::TrustEngine* trustEngine,
78 const MessageExtractor& extractor
81 Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.SimpleSigning");
82 log.debug("evaluating simple signing policy");
84 pair<saml2::Issuer*,const RoleDescriptor*> ret = pair<saml2::Issuer*,const RoleDescriptor*>(NULL,NULL);
86 if (!metadataProvider || !role || !trustEngine) {
87 log.debug("ignoring message, no metadata supplied");
91 const char* signature = request.getParameter("Signature");
93 log.debug("ignoring unsigned message");
97 const char* sigAlgorithm = request.getParameter("SigAlg");
99 log.error("SigAlg parameter not found, no way to verify the signature");
104 log.debug("extracting issuer from message");
105 pair<saml2::Issuer*,const XMLCh*> issuerInfo = extractor.getIssuerAndProtocol(message);
107 auto_ptr<saml2::Issuer> issuer(issuerInfo.first);
108 if (!issuerInfo.first || !issuerInfo.second ||
109 (issuer->getFormat() && !XMLString::equals(issuer->getFormat(), saml2::NameIDType::ENTITY))) {
110 log.warn("issuer identity not estabished, or was not an entityID");
114 log.debug("searching metadata for message issuer...");
115 const EntityDescriptor* entity = metadataProvider->getEntityDescriptor(issuer->getName());
117 auto_ptr_char temp(issuer->getName());
118 log.warn("no metadata found, can't establish identity of issuer (%s)", temp.get());
122 log.debug("matched assertion issuer against metadata, searching for applicable role...");
123 const RoleDescriptor* roledesc=entity->getRoleDescriptor(*role, issuerInfo.second);
125 log.warn("unable to find compatible role (%s) in metadata", role->toString().c_str());
131 const HTTPRequest& httpRequest = dynamic_cast<const HTTPRequest&>(request);
132 if (!strcmp(httpRequest.getMethod(), "GET")) {
133 // We have to construct a string containing the signature input by accessing the
134 // request directly. We can't use the decoded parameters because we need the raw
135 // data and URL-encoding isn't canonical.
136 pch = httpRequest.getQueryString();
137 if (!appendParameter(input, pch, "SAMLRequest="))
138 appendParameter(input, pch, "SAMLResponse=");
139 appendParameter(input, pch, "RelayState=");
140 appendParameter(input, pch, "SigAlg=");
143 // With POST, the input string is concatenated from the decoded form controls.
144 // GET should be this way too, but I messed up the spec, sorry.
145 pch = httpRequest.getParameter("SAMLRequest");
147 input = string("SAMLRequest=") + pch;
149 pch = httpRequest.getParameter("SAMLResponse");
150 input = string("SAMLResponse=") + pch;
152 pch = httpRequest.getParameter("RelayState");
154 input = input + "&RelayState=" + pch;
155 input = input + "&SigAlg=" + sigAlgorithm;
158 // Check for KeyInfo, but defensively (we might be able to run without it).
159 KeyInfo* keyInfo=NULL;
160 pch = request.getParameter("KeyInfo");
163 istringstream kstrm(pch);
164 DOMDocument* doc = XMLToolingConfig::getConfig().getParser().parse(kstrm);
165 XercesJanitor<DOMDocument> janitor(doc);
166 XMLObject* kxml = XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true);
168 if (!(keyInfo=dynamic_cast<KeyInfo*>(kxml)))
171 catch (XMLToolingException& ex) {
172 log.warn("Failed to load KeyInfo from message: %s", ex.what());
176 auto_ptr<KeyInfo> kjanitor(keyInfo);
177 auto_ptr_XMLCh alg(sigAlgorithm);
179 if (!trustEngine->validate(alg.get(), signature, keyInfo, input.c_str(), input.length(), *roledesc, metadataProvider->getKeyResolver())) {
180 log.error("unable to verify signature on message with supplied trust engine");
184 if (log.isDebugEnabled()) {
185 auto_ptr_char iname(entity->getEntityID());
186 log.debug("message from (%s), signature verified", iname.get());
189 ret.first = issuer.release();
190 ret.second = roledesc;
194 log.warn("caught a bad_cast while extracting issuer");