Return results from policy rules.
[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 "binding/HTTPRequest.h"
26 #include "binding/SimpleSigningRule.h"
27 #include "saml2/core/Assertions.h"
28 #include "saml2/metadata/Metadata.h"
29 #include "saml2/metadata/MetadataProvider.h"
30
31 #include <log4cpp/Category.hh>
32
33 using namespace opensaml::saml2md;
34 using namespace opensaml;
35 using namespace xmltooling;
36 using namespace log4cpp;
37 using namespace std;
38
39 using xmlsignature::KeyInfo;
40
41 namespace opensaml {
42     SecurityPolicyRule* SAML_DLLLOCAL SimpleSigningRuleFactory(const DOMElement* const & e)
43     {
44         return new SimpleSigningRule(e);
45     }
46
47     // Appends a raw parameter=value pair to the string.
48     static bool appendParameter(string& s, const char* data, const char* name)
49     {
50         const char* start = strstr(data,name);
51         if (!start)
52             return false;
53         if (!s.empty())
54             s += '&';
55         const char* end = strchr(start,'&');
56         if (end)
57             s.append(start, end-start);
58         else
59             s.append(start);
60         return true;
61     }
62 };
63
64
65 bool SimpleSigningRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
66 {
67     Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.SimpleSigning");
68     log.debug("evaluating simple signing policy");
69     
70     if (!policy.getIssuerMetadata()) {
71         log.debug("ignoring message, no issuer metadata supplied");
72         return false;
73     }
74     else if (!policy.getTrustEngine()) {
75         log.debug("ignoring message, no TrustEngine supplied");
76         return false;
77     }
78
79     const HTTPRequest* httpRequest = dynamic_cast<const HTTPRequest*>(request);
80     if (!request || !httpRequest) {
81         log.debug("ignoring message, no HTTP protocol request available");
82         return false;
83     }
84
85     const char* signature = request->getParameter("Signature");
86     if (!signature) {
87         log.debug("ignoring unsigned message");
88         return false;
89     }
90     
91     const char* sigAlgorithm = request->getParameter("SigAlg");
92     if (!sigAlgorithm) {
93         log.error("SigAlg parameter not found, no way to verify the signature");
94         return false;
95     }
96
97     string input;
98     const char* pch;
99     if (!strcmp(httpRequest->getMethod(), "GET")) {
100         // We have to construct a string containing the signature input by accessing the
101         // request directly. We can't use the decoded parameters because we need the raw
102         // data and URL-encoding isn't canonical.
103         pch = httpRequest->getQueryString();
104         if (!appendParameter(input, pch, "SAMLRequest="))
105             appendParameter(input, pch, "SAMLResponse=");
106         appendParameter(input, pch, "RelayState=");
107         appendParameter(input, pch, "SigAlg=");
108     }
109     else {
110         // With POST, the input string is concatenated from the decoded form controls.
111         // GET should be this way too, but I messed up the spec, sorry.
112         pch = httpRequest->getParameter("SAMLRequest");
113         if (pch)
114             input = string("SAMLRequest=") + pch;
115         else {
116             pch = httpRequest->getParameter("SAMLResponse");
117             input = string("SAMLResponse=") + pch;
118         }
119         pch = httpRequest->getParameter("RelayState");
120         if (pch)
121             input = input + "&RelayState=" + pch;
122         input = input + "&SigAlg=" + sigAlgorithm;
123     }
124
125     // Check for KeyInfo, but defensively (we might be able to run without it).
126     KeyInfo* keyInfo=NULL;
127     pch = request->getParameter("KeyInfo");
128     if (pch) {
129         try {
130             istringstream kstrm(pch);
131             DOMDocument* doc = XMLToolingConfig::getConfig().getParser().parse(kstrm);
132             XercesJanitor<DOMDocument> janitor(doc);
133             XMLObject* kxml = XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true);
134             janitor.release();
135             if (!(keyInfo=dynamic_cast<KeyInfo*>(kxml)))
136                 delete kxml;
137         }
138         catch (XMLToolingException& ex) {
139             log.warn("Failed to load KeyInfo from message: %s", ex.what());
140         }
141     }
142     
143     auto_ptr<KeyInfo> kjanitor(keyInfo);
144     auto_ptr_XMLCh alg(sigAlgorithm);
145     
146     if (!policy.getTrustEngine()->validate(
147             alg.get(), signature, keyInfo, input.c_str(), input.length(),
148             *(policy.getIssuerMetadata()), policy.getMetadataProvider()->getKeyResolver()
149             )) {
150         log.error("unable to verify message signature with supplied trust engine");
151         return false;
152     }
153
154     log.debug("signature verified against message issuer");
155     return true;
156 }