Return an error when GSSInitSecContext is called with an unacceptable mechanism.
[gssweb.git] / json_gssapi / src / commands / GSSInitSecContext.cpp
index 0f312b9..8b71d81 100644 (file)
@@ -1,42 +1,57 @@
 /*
- * Copyright (c) 2014 <copyright holder> <email>
- * 
- * For license details, see the LICENSE file in the root of this project.
- * 
+ * Copyright (c) 2014, JANET(UK)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of JANET(UK) nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
  */
 
 #include "GSSInitSecContext.h"
 #include "GSSException.h"
 #include <cache/GSSContextCache.h>
+#include <cache/GSSCredentialCache.h>
 #include <cache/GSSNameCache.h>
+#include <datamodel/GSSDisplayStatus.h>
 #include <datamodel/GSSName.h>
 #include <gssapi.h>
 #include <stdexcept>
 #include <stdlib.h>
 #include <string.h>
 
-typedef OM_uint32 (*init_sec_context)(
-    OM_uint32 *,        /* minor_status */
-    gss_cred_id_t,      /* claimant_cred_handle */
-    gss_ctx_id_t *,     /* context_handle */
-    gss_name_t,         /* target_name */
-    gss_OID,            /* mech_type (used to be const) */
-    OM_uint32,          /* req_flags */
-    OM_uint32,          /* time_req */
-    gss_channel_bindings_t,     /* input_chan_bindings */
-    gss_buffer_t,       /* input_token */
-    gss_OID *,          /* actual_mech_type */
-    gss_buffer_t,       /* output_token */
-    OM_uint32 *,        /* ret_flags */
-    OM_uint32 *         /* time_req */
-);
+#include "utils/util_base64.h"
 
 void
 GSSInitSecContext::execute()
 {
   /* Variables */
-  init_sec_context fn = (init_sec_context)function;
   gss_OID actual_mech_type;
+  JSONObject errors;
   
   /* Error checking */
   
@@ -45,9 +60,9 @@ GSSInitSecContext::execute()
     retVal = gss_release_buffer(&minor_status, &output_token);
 
   /* Main */
-  retVal = fn(
+  retVal = function(
     &minor_status,
-    GSS_C_NO_CREDENTIAL,
+    claimantCred.toGss(),
     &context_handle,
     targetName.toGss(),
     mechType.toGss(),
@@ -59,21 +74,20 @@ GSSInitSecContext::execute()
     &output_token,
     &ret_flags,
     &time_rec);
-  
-  if ( GSS_ERROR(this->retVal) )
-  {
-    std::string errMsg;
-    errMsg += "Cannot init_sec_context: ";
-    throw GSSException(errMsg.c_str(), this->retVal, this->minor_status, mechType.toGss());
-  }
-  
+
   actualMechType.setValue(actual_mech_type);
-  
   context.setContext(context_handle, true);
   contextKey = GSSContextCache::instance()->store(context);
   
   /* Cleanup */
   
+  // Handle errors
+  GSSDisplayStatus ds(retVal, minor_status, mechType.toGss());
+  errors.set("major_status_message", ds.getMajorMessage().c_str());
+  errors.set("minor_status_message", ds.getMinorMessage().c_str());
+  values->set("errors", errors);
+
+
   /* Return */
 }
 
@@ -106,24 +120,28 @@ bool GSSInitSecContext::loadParameters(JSONObject *params)
 {
   /* Variables */
   std::string key;
+  std::string token;
   
   /* Error checking */
   if ( params->isNull() )
     return true;
   
   /* Setup */
-  // Should I zeroOut?
   
   /* Main processing */
-  // Easy stuff(*params)
-  if ( !params->get("time_req").isNull() )
-    this->time_req = params->get("time_req").integer();
-  
-  if ( !params->get("req_flags").isNull() )
-    this->req_flags = params->get("req_flags").integer();
-  
+  // claimant_cred_handle
+  if (!(params->get("claimant_cred_handle").isNull() ||
+       (params->get("claimant_cred_handle").isString() &&
+        std::string("") == params->get("claimant_cred_handle").string())))
+  {
+    std::string key = params->get("claimant_cred_handle").string();
+    this->claimantCred = GSSCredentialCache::instance()->retrieve(key);
+  }
+
   // context_handle
-  if ( ! params->get("context_handle").isNull() )
+  if (!(params->get("context_handle").isNull() ||
+       (params->get("context_handle").isString() &&
+        std::string("") == params->get("context_handle").string())))
   {
     this->context_handle = GSS_C_NO_CONTEXT;
     if (params->get("context_handle").isString())
@@ -137,7 +155,11 @@ bool GSSInitSecContext::loadParameters(JSONObject *params)
   }
   
   // target_name
-  if ( ! params->get("target_name").isNull() )
+  if (! (  params->get("target_name").isNull() ||
+          (params->get("target_name").isString() &&
+           std::string("") == params->get("target_name").string())
+       )
+     )
   {
     this->target_name = GSS_C_NO_NAME;
     if (params->get("target_name").isString())
@@ -153,7 +175,13 @@ bool GSSInitSecContext::loadParameters(JSONObject *params)
   }
   
   // mech_type  
-  if ( ! params->get("mech_type").isNull() )
+  if (! ( params->get("mech_type").isNull() ||
+         (
+           params->get("mech_type").isString() &&
+           std::string("") == params->get("mech_type").string()
+         )
+       )
+     )
   {
     key.clear();
     if (params->get("mech_type").isString())
@@ -163,19 +191,30 @@ bool GSSInitSecContext::loadParameters(JSONObject *params)
     }
     if (GSS_C_NO_OID == this->mechType.toGss() )
       throw std::invalid_argument( std::string() + "Could not create a mech_type OID from '" + key + "'");
+    if ( !(this->mechType.isGssEapMech()) )
+      throw std::invalid_argument( std::string() +
+                                  "'" + key + "' must be 1.3.6.1.5.5.15.1.1.*");
   }
   
+  // req_flags
+  if (!params->get("req_flags").isNull() )
+    this->req_flags = (OM_uint32 )params->get("req_flags").integer();
+
+  // time_req
+  if (!params->get("time_req").isNull() )
+    this->time_req = (OM_uint32 )params->get("time_req").integer();
+
   // input_token
-  if ( ! params->get("input_token").isNull() )
+  if (! (params->get("input_token").isNull() ||
+        (params->get("input_token").isString() &&
+         std::string("") == params->get("input_token").string())))
   {
-    key = params->get("input_token").string();
-    this->input_token.value = (void *)key.c_str();
-    this->input_token.length = key.length();
+    token = params->get("input_token").string();
+    input_token.value = base64Decode(token.c_str(), &input_token.length);
   }
 
   /* Cleanup */
   
-  
   /* Return */
   return true;
 }
@@ -202,8 +241,11 @@ bool GSSInitSecContext::zeroOut(bool initialized)
     if (this->output_token.length > 0)
       gss_release_buffer(&minor, &output_token);
     
-    if (this->input_token.length > 0)
-      gss_release_buffer(&minor, &input_token);
+    if (this->input_token.value) {
+      base64Free(input_token.value);
+      input_token.value = NULL;
+      input_token.length = 0;
+    }
   }
 
   // Now set things to reasonable defaults
@@ -214,9 +256,10 @@ bool GSSInitSecContext::zeroOut(bool initialized)
   this->ret_flags = 0;
   this->time_rec = 0;
 
+  this->claimantCred = GSS_C_NO_CREDENTIAL;
   this->context_handle = GSS_C_NO_CONTEXT;
   this->target_name = GSS_C_NO_NAME;
-  this->mechType.setValue( (char *)"{ 1 3 6 1 5 5 15 1 1 18 }" );
+  this->mechType.setValue( (char *)"{ 1 3 6 1 5 5 15 1 1 17 }" );
   this->input_token.length = 0;
   this->input_token.value = NULL;
   this->output_token.length = 0;
@@ -230,21 +273,26 @@ bool GSSInitSecContext::zeroOut(bool initialized)
 JSONObject *GSSInitSecContext::toJSON()
 {
   /* Variables */
-  JSONObject *values = new JSONObject();
+  std::string output_str;
   
   /* Error checking */
   
   /* Setup */
+  base64EncodeStr(output_token.value, output_token.length, output_str);
   
   /* Main */
   values->set("major_status", this->retVal);
   values->set("minor_status", this->minor_status);
-  values->set("context_handle", this->contextKey.c_str());
-  values->set("actual_mech_type", this->getActualMechType().toString().c_str());
-  values->set("output_token", (const char *)this->output_token.value);
-  values->set("ret_flags", this->ret_flags);
-  values->set("time_rec", this->time_rec);
-  
+
+  if ( !GSS_ERROR(this->retVal) )
+  {
+    values->set("context_handle", this->contextKey.c_str());
+    values->set("actual_mech_type", this->getActualMechType().toString().c_str());
+    values->set("output_token", output_str.c_str());
+    values->set("ret_flags", this->ret_flags);
+    values->set("time_rec", this->time_rec);
+  }
+
   /* Cleanup */
   
   /* Return */
@@ -253,14 +301,16 @@ JSONObject *GSSInitSecContext::toJSON()
 
 GSSInitSecContext::GSSInitSecContext(
   JSONObject *params, 
-  void *fn) : GSSCommand(params)
+  init_sec_context_type fn)
 {
   zeroOut(false);
   loadParameters(params);
   function = fn;
+
+  values = new JSONObject();
 }
 
-GSSInitSecContext::GSSInitSecContext(void *fn)
+GSSInitSecContext::GSSInitSecContext(init_sec_context_type fn)
 {
   zeroOut(false);
   function = fn;