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.
34 // MRW -- Add proper copyright boilerplate to all files
36 #include "GSSInitSecContext.h"
37 #include "GSSException.h"
38 #include <cache/GSSContextCache.h>
39 #include <cache/GSSNameCache.h>
40 #include <datamodel/GSSName.h>
46 typedef OM_uint32 (*init_sec_context)(
47 OM_uint32 *, /* minor_status */
48 gss_cred_id_t, /* claimant_cred_handle */
49 gss_ctx_id_t *, /* context_handle */
50 gss_name_t, /* target_name */
51 gss_OID, /* mech_type (used to be const) */
52 OM_uint32, /* req_flags */
53 OM_uint32, /* time_req */
54 gss_channel_bindings_t, /* input_chan_bindings */
55 gss_buffer_t, /* input_token */
56 gss_OID *, /* actual_mech_type */
57 gss_buffer_t, /* output_token */
58 OM_uint32 *, /* ret_flags */
59 OM_uint32 * /* time_req */
63 GSSInitSecContext::execute()
66 init_sec_context fn = (init_sec_context)function;
67 gss_OID actual_mech_type;
72 if (output_token.length > 0)
73 retVal = gss_release_buffer(&minor_status, &output_token);
76 // MRW -- fix so that this uses all of the vars from the object
85 GSS_C_NO_CHANNEL_BINDINGS,
92 if ( GSS_ERROR(this->retVal) )
94 // MRW -- steal code from import name
96 errMsg += "Cannot init_sec_context: ";
97 throw GSSException(errMsg.c_str(), this->retVal, this->minor_status, mechType.toGss());
100 actualMechType.setValue(actual_mech_type);
102 context.setContext(context_handle, true);
103 contextKey = GSSContextCache::instance()->store(context);
111 const char* GSSInitSecContext::getTargetDisplayName()
114 gss_buffer_desc output_name;
116 OM_uint32 major, minor;
124 major = gss_display_name(&minor, target_name, &output_name, &output_type);
125 if (major == GSS_S_COMPLETE)
126 ret = (const char *)output_name.value;
137 bool GSSInitSecContext::loadParameters(JSONObject *params)
143 if ( params->isNull() )
148 /* Main processing */
149 // MRW -- finish parsing all of the variables
150 // claimant_cred_handle
153 if ( ! params->get("context_handle").isNull() )
155 this->context_handle = GSS_C_NO_CONTEXT;
156 if (params->get("context_handle").isString())
158 key = params->get("context_handle").string();
159 context = GSSContextCache::instance()->retrieve( key.c_str() );
160 this->context_handle = context.getContext();
162 if (GSS_C_NO_CONTEXT == this->context_handle)
163 throw std::invalid_argument( "Could not find the context_handle." );
167 if ( ! params->get("target_name").isNull() )
169 this->target_name = GSS_C_NO_NAME;
170 if (params->get("target_name").isString())
172 key = params->get("target_name").string();
174 targetName = GSSNameCache::instance()->retrieve(key);
176 this->target_name = targetName.toGss();
178 if (GSS_C_NO_NAME == this->target_name)
179 throw std::invalid_argument( "Could not find the target_name" );
183 if ( ! params->get("mech_type").isNull() )
186 if (params->get("mech_type").isString())
188 key = params->get("mech_type").string();
189 mechType.setValue(key);
191 if (GSS_C_NO_OID == this->mechType.toGss() )
192 throw std::invalid_argument( std::string() + "Could not create a mech_type OID from '" + key + "'");
196 if ( !params->get("req_flags").isNull() )
197 this->req_flags = params->get("req_flags").integer();
200 if ( !params->get("time_req").isNull() )
201 this->time_req = params->get("time_req").integer();
203 // input_chennel_bindings
206 if ( ! params->get("input_token").isNull() )
208 key = params->get("input_token").string();
209 this->input_token.value = (void *)key.c_str();
210 this->input_token.length = key.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.length > 0)
242 gss_release_buffer(&minor, &input_token);
245 // Now set things to reasonable defaults
247 this->minor_status = 0;
253 this->context_handle = GSS_C_NO_CONTEXT;
254 this->target_name = GSS_C_NO_NAME;
255 this->mechType.setValue( (char *)"{ 1 3 6 1 5 5 15 1 1 18 }" );
256 this->input_token.length = 0;
257 this->input_token.value = NULL;
258 this->output_token.length = 0;
259 this->output_token.value = NULL;
266 JSONObject *GSSInitSecContext::toJSON()
269 // MRW -- values should be scoped to the class, so execute can set error values?
270 JSONObject *values = new JSONObject();
277 values->set("major_status", this->retVal);
278 values->set("minor_status", this->minor_status);
279 values->set("context_handle", this->contextKey.c_str());
280 values->set("actual_mech_type", this->getActualMechType().toString().c_str());
281 values->set("output_token", (const char *)this->output_token.value);
282 values->set("ret_flags", this->ret_flags);
283 values->set("time_rec", this->time_rec);
284 // MRW -- modify for new error handling
292 GSSInitSecContext::GSSInitSecContext(
294 void *fn) : GSSCommand(params)
297 loadParameters(params);
301 GSSInitSecContext::GSSInitSecContext(void *fn)