2 * Copyright (c) 2014, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "GSSInitSecContext.h"
36 #include "GSSException.h"
37 #include <cache/GSSContextCache.h>
38 #include <cache/GSSCredentialCache.h>
39 #include <cache/GSSNameCache.h>
40 #include <datamodel/GSSDisplayStatus.h>
41 #include <datamodel/GSSName.h>
47 #include "utils/util_base64.h"
50 GSSInitSecContext::execute()
53 gss_OID actual_mech_type;
59 if (output_token.length > 0)
60 retVal = gss_release_buffer(&minor_status, &output_token);
71 GSS_C_NO_CHANNEL_BINDINGS,
78 actualMechType.setValue(actual_mech_type);
79 context.setContext(context_handle, true);
80 contextKey = GSSContextCache::instance()->store(context);
85 GSSDisplayStatus ds(retVal, minor_status, mechType.toGss());
86 errors.set("major_status_message", ds.getMajorMessage().c_str());
87 errors.set("minor_status_message", ds.getMinorMessage().c_str());
88 values->set("errors", errors);
94 const char* GSSInitSecContext::getTargetDisplayName()
97 gss_buffer_desc output_name;
99 OM_uint32 major, minor;
107 major = gss_display_name(&minor, target_name, &output_name, &output_type);
108 if (major == GSS_S_COMPLETE)
109 ret = (const char *)output_name.value;
119 bool GSSInitSecContext::loadParameters(JSONObject *params)
126 if ( params->isNull() )
131 /* Main processing */
132 // claimant_cred_handle
133 if (!(params->get("claimant_cred_handle").isNull() ||
134 (params->get("claimant_cred_handle").isString() &&
135 std::string("") == params->get("claimant_cred_handle").string())))
137 std::string key = params->get("claimant_cred_handle").string();
138 this->claimantCred = GSSCredentialCache::instance()->retrieve(key);
142 if (!(params->get("context_handle").isNull() ||
143 (params->get("context_handle").isString() &&
144 std::string("") == params->get("context_handle").string())))
146 this->context_handle = GSS_C_NO_CONTEXT;
147 if (params->get("context_handle").isString())
149 key = params->get("context_handle").string();
150 context = GSSContextCache::instance()->retrieve( key.c_str() );
151 this->context_handle = context.getContext();
153 if (GSS_C_NO_CONTEXT == this->context_handle)
154 throw std::invalid_argument( "Could not find the context_handle." );
158 if (! ( params->get("target_name").isNull() ||
159 (params->get("target_name").isString() &&
160 std::string("") == params->get("target_name").string())
164 this->target_name = GSS_C_NO_NAME;
165 if (params->get("target_name").isString())
167 key = params->get("target_name").string();
169 targetName = GSSNameCache::instance()->retrieve(key);
171 this->target_name = targetName.toGss();
173 if (GSS_C_NO_NAME == this->target_name)
174 throw std::invalid_argument( "Could not find the target_name" );
178 if (! ( params->get("mech_type").isNull() ||
180 params->get("mech_type").isString() &&
181 std::string("") == params->get("mech_type").string()
187 if (params->get("mech_type").isString())
189 key = params->get("mech_type").string();
190 mechType.setValue(key);
192 if (GSS_C_NO_OID == this->mechType.toGss() )
193 throw std::invalid_argument( std::string() + "Could not create a mech_type OID from '" + key + "'");
197 if (!params->get("req_flags").isNull() )
198 this->req_flags = (OM_uint32 )params->get("req_flags").integer();
201 if (!params->get("time_req").isNull() )
202 this->time_req = (OM_uint32 )params->get("time_req").integer();
205 if (! (params->get("input_token").isNull() ||
206 (params->get("input_token").isString() &&
207 std::string("") == params->get("input_token").string())))
209 token = params->get("input_token").string();
210 input_token.value = base64Decode(token.c_str(), &input_token.length);
219 bool GSSInitSecContext::zeroOut(bool initialized)
224 gss_buffer_desc output;
229 // Free up existing memory if it's been set.
232 if (this->context_handle != NULL)
233 gss_delete_sec_context(&minor, &(this->context_handle), &output);
235 if (this->target_name != NULL)
236 gss_release_name(&minor, &(this->target_name));
238 if (this->output_token.length > 0)
239 gss_release_buffer(&minor, &output_token);
241 if (this->input_token.value) {
242 base64Free(input_token.value);
243 input_token.value = NULL;
244 input_token.length = 0;
248 // Now set things to reasonable defaults
250 this->minor_status = 0;
256 this->claimantCred = GSS_C_NO_CREDENTIAL;
257 this->context_handle = GSS_C_NO_CONTEXT;
258 this->target_name = GSS_C_NO_NAME;
259 this->mechType.setValue( (char *)"{ 1 3 6 1 5 5 15 1 1 18 }" );
260 this->input_token.length = 0;
261 this->input_token.value = NULL;
262 this->output_token.length = 0;
263 this->output_token.value = NULL;
270 JSONObject *GSSInitSecContext::toJSON()
273 std::string output_str;
278 base64EncodeStr(output_token.value, output_token.length, output_str);
281 values->set("major_status", this->retVal);
282 values->set("minor_status", this->minor_status);
284 if ( !GSS_ERROR(this->retVal) )
286 values->set("context_handle", this->contextKey.c_str());
287 values->set("actual_mech_type", this->getActualMechType().toString().c_str());
288 values->set("output_token", output_str.c_str());
289 values->set("ret_flags", this->ret_flags);
290 values->set("time_rec", this->time_rec);
299 GSSInitSecContext::GSSInitSecContext(
301 init_sec_context_type fn)
304 loadParameters(params);
307 values = new JSONObject();
310 GSSInitSecContext::GSSInitSecContext(init_sec_context_type fn)