2 * Copyright (c) 2014 <copyright holder> <email>
4 * For license details, see the LICENSE file in the root of this project.
8 #include "GSSCreateSecContextCommand.h"
9 #include "GSSException.h"
10 #include <cache/GSSContextCache.h>
11 #include <cache/GSSNameCache.h>
12 #include <datamodel/GSSName.h>
18 typedef OM_uint32 (*init_sec_context)(
19 OM_uint32 *, /* minor_status */
20 gss_cred_id_t, /* claimant_cred_handle */
21 gss_ctx_id_t *, /* context_handle */
22 gss_name_t, /* target_name */
23 gss_OID, /* mech_type (used to be const) */
24 OM_uint32, /* req_flags */
25 OM_uint32, /* time_req */
26 gss_channel_bindings_t, /* input_chan_bindings */
27 gss_buffer_t, /* input_token */
28 gss_OID *, /* actual_mech_type */
29 gss_buffer_t, /* output_token */
30 OM_uint32 *, /* ret_flags */
31 OM_uint32 * /* time_req */
35 GSSCreateSecContextCommand::execute()
38 init_sec_context fn = (init_sec_context)function;
39 gss_OID actual_mech_type;
44 if (output_token.length > 0)
45 retVal = gss_release_buffer(&minor_status, &output_token);
56 GSS_C_NO_CHANNEL_BINDINGS,
63 if ( GSS_ERROR(this->retVal) )
66 errMsg += "Cannot init_sec_context: ";
67 throw GSSException(errMsg.c_str(), this->retVal, this->minor_status, mechType.toGss());
70 actualMechType.setValue(actual_mech_type);
72 context.setContext(context_handle, true);
73 contextKey = GSSContextCache::instance()->store(context);
80 const char* GSSCreateSecContextCommand::getTargetDisplayName()
83 gss_buffer_desc output_name;
85 OM_uint32 major, minor;
93 major = gss_display_name(&minor, target_name, &output_name, &output_type);
94 if (major == GSS_S_COMPLETE)
95 ret = (const char *)output_name.value;
105 bool GSSCreateSecContextCommand::loadParameters(JSONObject *params)
111 if ( params->get("arguments").isNull() )
117 /* Main processing */
118 // Easy stuff(*params)
119 if ( !params->get("arguments").get("time_req").isNull() )
120 this->time_req = params->get("arguments").get("time_req").integer();
122 if ( !params->get("arguments").get("req_flags").isNull() )
123 this->req_flags = params->get("arguments").get("req_flags").integer();
126 if ( ! params->get("arguments").get("context_handle").isNull() )
128 this->context_handle = GSS_C_NO_CONTEXT;
129 if (params->get("arguments").get("context_handle").isString())
131 key = params->get("arguments").get("context_handle").string();
132 context = GSSContextCache::instance()->retrieve( key.c_str() );
133 this->context_handle = context.getContext();
135 if (GSS_C_NO_CONTEXT == this->context_handle)
136 throw std::invalid_argument( "Could not find the context_handle." );
140 if ( ! params->get("arguments").get("target_name").isNull() )
142 this->target_name = GSS_C_NO_NAME;
143 if (params->get("arguments").get("target_name").isString())
145 key = params->get("arguments").get("target_name").string();
147 targetName = GSSNameCache::instance()->retrieve(key);
149 this->target_name = targetName.toGss();
151 if (GSS_C_NO_NAME == this->target_name)
152 throw std::invalid_argument( "Could not find the target_name" );
156 if ( ! params->get("arguments").get("mech_type").isNull() )
159 if (params->get("arguments").get("mech_type").isString())
161 key = params->get("arguments").get("mech_type").string();
162 mechType.setValue(key);
164 if (GSS_C_NO_OID == this->mechType.toGss() )
165 throw std::invalid_argument( std::string() + "Could not create a mech_type OID from '" + key + "'");
169 if ( ! params->get("arguments").get("input_token").isNull() )
171 key = params->get("arguments").get("input_token").string();
172 this->input_token.value = (void *)key.c_str();
173 this->input_token.length = key.length();
183 bool GSSCreateSecContextCommand::zeroOut(bool initialized)
188 gss_buffer_desc output;
193 // Free up existing memory if it's been set.
196 if (this->context_handle != NULL)
197 gss_delete_sec_context(&minor, &(this->context_handle), &output);
199 if (this->target_name != NULL)
200 gss_release_name(&minor, &(this->target_name));
202 if (this->output_token.length > 0)
203 gss_release_buffer(&minor, &output_token);
205 if (this->input_token.length > 0)
206 gss_release_buffer(&minor, &input_token);
209 // Now set things to reasonable defaults
211 this->minor_status = 0;
217 this->context_handle = GSS_C_NO_CONTEXT;
218 this->target_name = GSS_C_NO_NAME;
219 this->mechType.setValue( (char *)"{ 1 3 6 1 5 5 15 1 1 18 }" );
220 this->input_token.length = 0;
221 this->input_token.value = NULL;
222 this->output_token.length = 0;
223 this->output_token.value = NULL;
230 JSONObject *GSSCreateSecContextCommand::toJSON()
233 JSONObject *ret = new JSONObject();
234 JSONObject *values = new JSONObject();
241 values->set("major_status", this->retVal);
242 values->set("minor_status", this->minor_status);
243 values->set("context_handle", this->contextKey.c_str());
244 values->set("actual_mech_type", this->getActualMechType().toString().c_str());
245 values->set("output_token", (const char *)this->output_token.value);
246 values->set("ret_flags", this->ret_flags);
247 values->set("time_rec", this->time_rec);
248 ret->set("command", "gss_init_sec_context");
249 ret->set("return_values", *values);
257 GSSCreateSecContextCommand::GSSCreateSecContextCommand(
259 void *fn) : GSSCommand(params)
262 loadParameters(params);
266 GSSCreateSecContextCommand::GSSCreateSecContextCommand(void *fn)