rearranging the commands into their own directory
[gssweb.git] / json_gssapi / src / commands / GSSCreateSecContextCommand.cpp
1 /*
2  * Copyright (c) 2014 <copyright holder> <email>
3  * 
4  * For license details, see the LICENSE file in the root of this project.
5  * 
6  */
7
8 #include "GSSCreateSecContextCommand.h"
9 #include "GSSException.h"
10 #include <gssapi.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 typedef OM_uint32 (*init_sec_context)(
15     OM_uint32 *,        /* minor_status */
16     gss_cred_id_t,      /* claimant_cred_handle */
17     gss_ctx_id_t *,     /* context_handle */
18     gss_name_t,         /* target_name */
19     gss_OID,            /* mech_type (used to be const) */
20     OM_uint32,          /* req_flags */
21     OM_uint32,          /* time_req */
22     gss_channel_bindings_t,     /* input_chan_bindings */
23     gss_buffer_t,       /* input_token */
24     gss_OID *,          /* actual_mech_type */
25     gss_buffer_t,       /* output_token */
26     OM_uint32 *,        /* ret_flags */
27     OM_uint32 *         /* time_req */
28 );
29
30 /* Helper function - import the OID from a string */
31 static gss_OID str_to_oid(const char *mech_type_str = NULL)
32 {
33   /* Variables */
34   gss_buffer_desc gssbuffOID;
35   gss_OID gssoidTargetOID;
36   OM_uint32 major;
37   OM_uint32 minor;
38   
39   /* Error checking */
40   if (mech_type_str == NULL ||
41       *mech_type_str == 0)
42     return NULL;
43   
44   /* Setup */
45   /* Main */
46   gssbuffOID.value = (void *)(mech_type_str);
47   gssbuffOID.length = strlen(mech_type_str);
48   major = gss_str_to_oid(&minor, 
49                          &gssbuffOID, 
50                          &gssoidTargetOID);
51   if (major != GSS_S_COMPLETE)
52     throw GSSException("Error converting string to OID", major, minor);
53
54   /* Cleanup */
55   
56   /* Return */
57   return gssoidTargetOID;
58 }
59
60 void
61 GSSCreateSecContextCommand::execute()
62 {
63   /* Variables */
64   init_sec_context fn = (init_sec_context)function;
65   
66   /* Error checking */
67   
68   /* Setup */
69   if (output_token.length > 0)
70     retVal = gss_release_buffer(&minor_status, &output_token);
71   
72   /* Main */
73   
74   retVal = fn(
75     &minor_status,
76     GSS_C_NO_CREDENTIAL,
77     &context_handle,
78     target_name,
79     mech_type,
80     req_flags,
81     time_req,
82     GSS_C_NO_CHANNEL_BINDINGS,
83     &input_token,
84     &actual_mech_type,
85     &output_token,
86     &ret_flags,
87     &time_rec);
88   
89   /* Cleanup */
90   
91   /* Return */
92 }
93
94 const char* GSSCreateSecContextCommand::getTargetDisplayName()
95 {
96   /* Variables */
97   gss_buffer_desc output_name;
98   gss_OID output_type;
99   OM_uint32 major, minor;
100   const char *ret;
101   
102   /* error checking */
103   
104   /* Setup */
105   
106   /* Main */
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;
110   else
111     ret = NULL;
112   
113   /* cleanup */
114   
115   /* return */
116   return( ret );
117 }
118
119 const char* GSSCreateSecContextCommand::getActualMechType()
120 {
121   return(this->oidToStr(this->actual_mech_type));
122 }
123
124 const char* GSSCreateSecContextCommand::getMechType()
125 {
126   return(this->oidToStr(this->mech_type));
127 }
128
129 const char* GSSCreateSecContextCommand::oidToStr(gss_OID oid)
130 {
131   gss_buffer_desc output;
132   OM_uint32 major, minor;
133   const char *retVal;
134
135   /* error checking */
136   if (oid == NULL)
137     return NULL;
138
139   /* Setup */  
140   
141   /* main */
142   major = gss_oid_to_str(&minor, oid, &output);
143   if (major == GSS_S_COMPLETE)
144     retVal = (const char *)output.value;
145   else
146     retVal = NULL;
147   
148   /* cleanup */
149   
150   /* return */
151   return( retVal );
152 }
153
154 bool GSSCreateSecContextCommand::loadParameters(JSONObject *params)
155 {
156   /* Variables */
157   OM_uint32 major, minor;
158   gss_buffer_desc gssbuffTargetName;
159   const char *buffer;
160   
161   /* Error checking */
162   
163   /* Setup */
164   // Should I zeroOut?
165   
166   /* Main processing */
167   // Easy stuff(*params)
168   this->time_req = (OM_uint32)( (*params)["arguments"]["time_req"].integer() );
169   this->req_flags = (OM_uint32)( (*params)["arguments"]["req_flags"].integer() );
170   
171   // context_handle
172   //   -- just treat the value passed in as correct.
173   context_handle = (gss_ctx_id_t)( (*params)["arguments"]["context_handle"].integer() );
174   
175   // target_name
176   buffer = (*params)["arguments"]["target_name"].string();
177   if (buffer != NULL && *buffer != 0)
178   {
179     gssbuffTargetName.value = (void *)buffer;
180     gssbuffTargetName.length = strlen( buffer );
181     
182     major = gss_import_name(&minor, 
183                             &gssbuffTargetName, 
184                             GSS_C_NO_OID, 
185                             &target_name);
186     if (major != GSS_S_COMPLETE)
187       throw GSSException("Error importing target_name", major, minor);
188   }
189   
190   // mech_type  
191   mech_type = str_to_oid( (*params)["arguments"]["mech_type"].string() );
192   
193   // input_token
194   buffer = (*params)["arguments"]["input_token"].string();
195   if (buffer != NULL && *buffer != 0)
196   {
197     this->input_token.value = (void *)buffer;
198     this->input_token.length = strlen(buffer);
199   }
200   
201   /* Cleanup */
202   
203   
204   /* Return */
205   return true;
206 }
207
208 bool GSSCreateSecContextCommand::zeroOut(bool initialized)
209 {
210   /* Error checking */
211   /* Variables */
212   OM_uint32 minor;
213   gss_buffer_desc output;
214   
215   /* Setup */
216   /* Main */
217
218   // Free up existing memory if it's been set.  
219   if (initialized)
220   {
221     if (this->context_handle != NULL)
222       gss_delete_sec_context(&minor, &(this->context_handle), &output);
223     
224     if (this->target_name != NULL)
225       gss_release_name(&minor, &(this->target_name));
226       
227     if (mech_type != NULL)
228       gss_release_oid(&minor, &(this->mech_type));
229     
230     if (this->actual_mech_type != NULL)
231       gss_release_oid(&minor, &(this->actual_mech_type));
232     
233     if (this->output_token.length > 0)
234       gss_release_buffer(&minor, &output_token);
235     
236     if (this->input_token.length > 0)
237       gss_release_buffer(&minor, &input_token);
238   }
239
240   // Now set things to reasonable defaults
241   this->retVal = 0;
242   this->minor_status = 0;
243   this->req_flags = 0;
244   this->time_req = 0;
245   this->ret_flags = 0;
246   this->time_rec = 0;
247
248   this->context_handle = GSS_C_NO_CONTEXT;
249   this->target_name = GSS_C_NO_NAME;
250   mech_type = str_to_oid( "{ 1 2 840 113554 1 2 1 4 }" );
251   this->input_token.length = 0;
252   this->input_token.value = NULL;
253   this->actual_mech_type = GSS_C_NO_OID;
254   this->output_token.length = 0;
255   this->output_token.value = NULL;
256
257   /* Cleanup */
258   /* Return */
259   return(true);
260 }
261
262 JSONObject *GSSCreateSecContextCommand::toJSON()
263 {
264   /* Variables */
265   JSONObject *ret = new JSONObject();
266   JSONObject *values = new JSONObject();
267   
268   /* Error checking */
269   
270   /* Setup */
271   
272   /* Main */
273   values->set("major_status", this->retVal);
274   values->set("minor_status", this->minor_status);
275   values->set("context_handle", (json_int_t)0);
276   values->set("actual_mech_type", this->getActualMechType());
277   values->set("output_token", (const char *)this->output_token.value);
278   values->set("ret_flags", this->ret_flags);
279   values->set("time_rec", this->time_rec);
280   ret->set("command", "gss_init_sec_context");
281   ret->set("return_values", *values);
282   
283   /* Cleanup */
284   
285   /* Return */
286   return(ret);
287 }
288
289 GSSCreateSecContextCommand::GSSCreateSecContextCommand(
290   JSONObject *params, 
291   void *fn) : GSSCommand(params)
292 {
293   zeroOut(false);
294   loadParameters(params);
295   function = fn;
296 }
297
298 GSSCreateSecContextCommand::GSSCreateSecContextCommand(void *fn)
299 {
300   zeroOut(false);
301   function = fn;
302 }
303