GSSWrap command completed.
authorMark Donnelly <mark@painless-security.com>
Tue, 13 May 2014 19:03:08 +0000 (15:03 -0400)
committerMark Donnelly <mark@painless-security.com>
Tue, 13 May 2014 19:03:08 +0000 (15:03 -0400)
json_gssapi/CMakeLists.txt
json_gssapi/src/GSSUnwrap.cpp [new file with mode: 0644]
json_gssapi/src/GSSUnwrap.h [new file with mode: 0644]
json_gssapi/test/CMakeLists.txt
json_gssapi/test/GSSUnwrapTest.cpp [new file with mode: 0644]
json_gssapi/test/GSSUnwrapTest.h [new file with mode: 0644]
json_gssapi/test/command_mocks/MockUnwrap.cpp [new file with mode: 0644]
json_gssapi/test/command_mocks/MockUnwrap.h [new file with mode: 0644]

index c013e81..2ac73d0 100644 (file)
@@ -12,7 +12,8 @@ add_executable(json_gssapi src/datamodel/GSSName.cpp
                            src/GSSCommand.cpp 
                            src/GSSImportName.cpp
                            src/GSSAcquireCred.cpp 
-                           src/GSSWrap.cpp 
+                           src/GSSWrap.cpp  
+                           src/GSSUnwrap.cpp
                            src/util_json.cpp 
                            main.cpp)
 target_link_libraries(json_gssapi gssapi_krb5 jansson)
diff --git a/json_gssapi/src/GSSUnwrap.cpp b/json_gssapi/src/GSSUnwrap.cpp
new file mode 100644 (file)
index 0000000..ab8d158
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2014 <copyright holder> <email>
+ *
+ * For license details, see the LICENSE file in the root of this project.
+ *
+ */
+
+#include "GSSUnwrap.h"
+
+GSSUnwrap::GSSUnwrap ( JSONObject* params, gss_unwrap_type fn )
+{
+  function = fn;
+  loadParameters(params);
+}
+
+bool GSSUnwrap::loadParameters(JSONObject *params)
+{
+  /* Variables */
+  std::string sInputMessage;
+  
+  /* Error checking */
+  /* Setup */
+  // Should I zeroOut?
+  
+  /* Main processing */
+  
+  /*****************
+   * input_message *
+   *****************/
+  if ( ! params->get("arguments").get("input_message").isNull() )
+  {
+    sInputMessage = params->get("arguments").get("input_message").string();
+    this->inputMessage.setValue(sInputMessage);
+  }
+  
+  /* Cleanup */
+  /* Return */
+  return true;
+}
+
+
+void GSSUnwrap::execute()
+{
+  /* Variables */
+  gss_buffer_desc output_buf;
+  
+  retVal = function(
+    &(this->minor_status),
+    this->context,
+    this->inputMessage.toGss(),
+    &output_buf,
+    &(this->conf_state),
+    &(this->qop_state)
+  );
+  
+  this->outputMessage.setValue( (char *)output_buf.value, output_buf.length );
+}
+
+JSONObject* GSSUnwrap::toJSON()
+{
+  /* Variables */
+  const char *conf_state, *qop_state;
+  JSONObject *ret = new JSONObject();
+  JSONObject *values = new JSONObject();
+  
+  /* Error checking */
+  
+  /* Setup */
+  
+  /* Main */
+  // Return Values
+  // Easy stuff
+  values->set("major_status", this->retVal);
+  values->set("minor_status", this->minor_status);
+  
+  values->set("qop_state", this->qop_state);
+  
+  conf_state = (this->conf_state) ? "TRUE" : "FALSE";
+  values->set("conf_state", conf_state);
+  
+  values->set(
+    "output_message",
+    this->outputMessage.toString().c_str()
+  );
+  
+  // Put it all together.
+  ret->set("command", "gss_wrap");
+  ret->set("return_values", *values);
+  
+  /* Cleanup */
+  
+  /* Return */
+  return(ret);
+}
diff --git a/json_gssapi/src/GSSUnwrap.h b/json_gssapi/src/GSSUnwrap.h
new file mode 100644 (file)
index 0000000..ac29117
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2014 <copyright holder> <email>
+ *
+ * For license details, see the LICENSE file in the root of this project.
+ *
+ */
+
+#ifndef GSSUNWRAP_H
+#define GSSUNWRAP_H
+
+#include "GSSCommand.h"
+#include "datamodel/GSSBuffer.h"
+#include <gssapi/gssapi.h>
+
+typedef OM_uint32 (*gss_unwrap_type) (
+    OM_uint32 *,        /* minor_status */
+    gss_ctx_id_t,       /* context_handle */
+    gss_buffer_t,       /* input_message_buffer */
+    gss_buffer_t,       /* output_message_buffer */
+    int *,              /* conf_state */
+    gss_qop_t *);       /* qop_state */
+
+
+class GSSUnwrap :  GSSCommand
+{
+public:
+    GSSUnwrap( gss_unwrap_type fn = &gss_unwrap ) : function(fn) {};
+    GSSUnwrap( JSONObject *params, gss_unwrap_type fn = &gss_unwrap);
+    
+    gss_unwrap_type getGSSFunction() { return(function); }
+    void setContextHandle ( gss_ctx_id_t context_handle ) { context = context_handle; }
+    void setInputMessage ( GSSBuffer* input ) { inputMessage.setValue(input->toString()); };
+    
+    gss_qop_t getQopState() const { return(qop_state); }
+    int getConfState() const { return(conf_state); }
+    GSSBuffer getInputMessage() const { return(inputMessage); }
+    GSSBuffer getOutputMessage() const { return(outputMessage); }
+
+    virtual void execute();  
+    virtual JSONObject* toJSON();
+    
+private:
+    gss_unwrap_type function;
+    OM_uint32 retVal;
+    OM_uint32 minor_status;
+    gss_ctx_id_t context;
+    gss_qop_t qop_state;
+    int conf_state;
+    
+    GSSBuffer inputMessage;
+    GSSBuffer outputMessage;
+    
+    bool loadParameters(JSONObject *params);
+};
+
+#endif // GSSUNWRAP_H
index b2f2ecf..ee516c0 100644 (file)
@@ -2,9 +2,11 @@ include_directories(${CMAKE_SOURCE_DIR}/src)
 
 add_executable(test GSSExceptionTest.cpp 
                     GSSAcquireCredTest.cpp
-                    GSSWrapTest.cpp
+                    GSSWrapTest.cpp 
+                    GSSUnwrapTest.cpp
                     command_mocks/InitSecContextMock.cpp
-                    command_mocks/MockAcquireCred.cpp 
+                    command_mocks/MockAcquireCred.cpp  
+                    command_mocks/MockUnwrap.cpp
                     command_mocks/MockWrap.cpp 
                     GSSCreateSecContextTest.cpp 
                     GSSImportNameTest.cpp
@@ -15,6 +17,7 @@ add_executable(test GSSExceptionTest.cpp
                     ../src/GSSImportName.cpp
                     ../src/GSSException.cpp
                     ../src/GSSAcquireCred.cpp
+                    ../src/GSSUnwrap.cpp
                     ../src/GSSWrap.cpp
                     ../src/datamodel/GSSBuffer.cpp
                     ../src/datamodel/GSSCredential.cpp
diff --git a/json_gssapi/test/GSSUnwrapTest.cpp b/json_gssapi/test/GSSUnwrapTest.cpp
new file mode 100644 (file)
index 0000000..d807b52
--- /dev/null
@@ -0,0 +1,226 @@
+/*
+ * Copyright (c) 2014 <copyright holder> <email>
+ *
+ * For license details, see the LICENSE file in the root of this project.
+ *
+ */
+
+#include "GSSUnwrapTest.h"
+#include "command_mocks/MockUnwrap.h"
+#include "GSSUnwrap.h"
+#include <gssapi/gssapi.h>
+
+CPPUNIT_TEST_SUITE_REGISTRATION( GSSUnwrapTest );
+
+/* 
+ * a mock of the gss_import_name call
+ * 
+ * Basically, just copy the arguments over to/from the
+ * MockUnwrap global object
+ */
+
+static OM_uint32 KRB5_CALLCONV
+mock_unwrap(
+    OM_uint32    *minor_status,
+    gss_ctx_id_t  context_handle,
+    gss_buffer_t  input_message_buffer,
+    gss_buffer_t  output_message_buffer,
+    int          *conf_state,
+    gss_qop_t    *qop_state)
+{
+  /* Error checking */
+  /* Variables */
+  std::string buffer;
+  
+  /* Setup */
+  buffer = MockUnwrap::outputMessageBuffer.toString();
+  
+  /* Main */
+  // Copy our input from the appropriate parameters to MockUnwrap
+  MockUnwrap::context_handle = context_handle;
+  MockUnwrap::inputMessageBuffer.setValue(input_message_buffer);
+  
+  
+  // copy our output to the appropriate parameters
+  *minor_status = MockUnwrap::minor_status;
+  *conf_state = MockUnwrap::conf_state;
+  *qop_state = MockUnwrap::qop_state;
+  output_message_buffer->length = buffer.length();
+  output_message_buffer->value  = (void *)buffer.c_str();
+  
+  /* Cleanup */
+  /* return */
+  return MockUnwrap::retVal;
+}
+
+void GSSUnwrapTest::setUp()
+{
+  CppUnit::TestFixture::setUp();
+  MockUnwrap::reset();
+}
+
+void GSSUnwrapTest::testConstructor()
+{
+  /* Variables */
+  GSSUnwrap cmd = GSSUnwrap();
+  
+  /* Error checking */
+  /* Setup */
+  /* Main */
+  CPPUNIT_ASSERT_EQUAL_MESSAGE(
+    "The GSSUnwrap object has the wrong GSS function",
+    (void *)&gss_unwrap,
+    (void *)cmd.getGSSFunction()
+  );
+  
+  /* Cleanup */
+  /* Return */
+}
+
+void GSSUnwrapTest::testEmptyCall()
+{
+  /* Variables */
+  GSSUnwrap cmd = GSSUnwrap(&mock_unwrap);
+  GSSBuffer input((char *)"Input message");
+  gss_ctx_id_t desiredContext = (gss_ctx_id_t)rand();
+  
+  /* Error checking */
+  /* Setup */
+  cmd.setContextHandle(desiredContext);
+  cmd.setInputMessage(&input);
+  
+  MockUnwrap::minor_status = 0;
+  MockUnwrap::retVal = 0;
+  MockUnwrap::conf_state = rand();
+  MockUnwrap::qop_state = GSS_C_QOP_DEFAULT;
+  MockUnwrap::outputMessageBuffer.setValue((char *)"Output message");
+  
+  
+  /* Main */
+  cmd.execute();
+  CPPUNIT_ASSERT_EQUAL_MESSAGE(
+    "The requested GSS context handle is not correct",
+    desiredContext,
+    MockUnwrap::context_handle
+  );
+  
+  CPPUNIT_ASSERT_EQUAL_MESSAGE(
+    "The input message was incorrect.",
+    input.toString(),
+    MockUnwrap::inputMessageBuffer.toString()
+  );
+  
+  
+  
+  CPPUNIT_ASSERT_EQUAL_MESSAGE(
+    "The conf_state flag was incorrect.",
+    MockUnwrap::qop_state,
+    cmd.getQopState()
+  );
+  
+  CPPUNIT_ASSERT_EQUAL_MESSAGE(
+    "The conf_state flag was incorrect.",
+    MockUnwrap::conf_state,
+    cmd.getConfState()
+  );
+  
+  CPPUNIT_ASSERT_EQUAL_MESSAGE(
+    "The input message was incorrect.",
+    cmd.getOutputMessage().toString(),
+    MockUnwrap::outputMessageBuffer.toString()
+  );
+  
+  
+  /* Cleanup */
+  /* Return */
+}
+
+void GSSUnwrapTest::testConstructorWithJSONObject()
+{
+  /* Variables */
+  const char* input = "{\"method\": \"gss_wrap\", \
+    \"arguments\": \
+    { \
+         \"context_handle\": \"#######\", \
+         \"input_message\": \"mary had a little lamb\" \
+    }\
+  }";
+  json_error_t jsonErr;
+  JSONObject json = JSONObject::load(input, 0, &jsonErr);
+  
+  GSSUnwrap cmd = GSSUnwrap(&json, &mock_unwrap);
+  
+  /* Error checking */
+  /* Setup */
+  /* Main */
+  
+  CPPUNIT_ASSERT_EQUAL_MESSAGE(
+    "GSSUnwrap did not parse the input message argument correctly",
+    std::string("mary had a little lamb"),
+    cmd.getInputMessage().toString()
+  );
+  
+  
+  
+  /* Cleanup */
+  /* Return */
+}
+
+
+void GSSUnwrapTest::testJSONMarshal()
+{
+  /* Variables */
+  std::string output("dns@google.com");
+  int confState = 1;
+  gss_qop_t qopState = GSS_C_QOP_DEFAULT;
+  JSONObject *result;
+  GSSUnwrap cmd = GSSUnwrap(&mock_unwrap);
+  
+  /* Error checking */
+  /* Setup */
+  MockUnwrap::minor_status = 0;
+  MockUnwrap::retVal = 0;
+  MockUnwrap::outputMessageBuffer.setValue(output);
+  MockUnwrap::conf_state = confState;
+  MockUnwrap::qop_state = qopState;
+  
+  /* Main */
+  cmd.execute();
+  result = cmd.toJSON();
+//   std::cout << "\nGSSUnwrap JSON: \n" << result->dump() << "\n";
+  
+  CPPUNIT_ASSERT_EQUAL_MESSAGE(
+    "The command name is incorrect",
+    std::string("gss_wrap"),
+    std::string( (*result)["command"].string() )
+  );
+  
+  
+  CPPUNIT_ASSERT_EQUAL_MESSAGE(
+    "The return value was reported incorrectly",
+    (int)MockUnwrap::retVal,
+    (int)( (*result)["return_values"]["major_status"].integer() )
+  );
+  
+  CPPUNIT_ASSERT_EQUAL_MESSAGE(
+    "The minor_status value was reported incorrectly",
+    (int)MockUnwrap::minor_status,
+    (int)( (*result)["return_values"]["minor_status"].integer() )
+  );
+  
+  CPPUNIT_ASSERT_EQUAL_MESSAGE(
+    "The output message was reported incorrectly",
+    MockUnwrap::outputMessageBuffer.toString(),
+    std::string( (*result)["return_values"]["output_message"].string() )
+  );
+  
+  CPPUNIT_ASSERT_EQUAL_MESSAGE(
+    "The minor_status value was reported incorrectly",
+    (int)qopState,
+    (int)( (*result)["return_values"]["qop_state"].integer() )
+  );
+  
+  
+  /* Cleanup */
+  /* Return */
+}
diff --git a/json_gssapi/test/GSSUnwrapTest.h b/json_gssapi/test/GSSUnwrapTest.h
new file mode 100644 (file)
index 0000000..173793c
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2014 <copyright holder> <email>
+ *
+ * For license details, see the LICENSE file in the root of this project.
+ *
+ */
+
+#ifndef GSSUNWRAPTEST_H
+#define GSSUNWRAPTEST_H
+
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+class GSSUnwrapTest : public CppUnit::TestFixture
+{
+  CPPUNIT_TEST_SUITE( GSSUnwrapTest );
+  CPPUNIT_TEST( testConstructor );
+  CPPUNIT_TEST( testConstructorWithJSONObject );
+  CPPUNIT_TEST( testEmptyCall );
+  CPPUNIT_TEST( testJSONMarshal );
+  CPPUNIT_TEST_SUITE_END();
+  
+public:
+  void setUp();
+  
+  void testConstructor();
+  void testConstructorWithJSONObject();
+  void testEmptyCall();
+  void testJSONMarshal();
+
+};
+
+#endif // GSSWRAPTEST_H
diff --git a/json_gssapi/test/command_mocks/MockUnwrap.cpp b/json_gssapi/test/command_mocks/MockUnwrap.cpp
new file mode 100644 (file)
index 0000000..8c37d39
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2014 <copyright holder> <email>
+ *
+ * For license details, see the LICENSE file in the root of this project.
+ *
+ */
+
+#include "MockUnwrap.h"
+
+
+OM_uint32    MockUnwrap::retVal;
+OM_uint32    MockUnwrap::minor_status;
+gss_ctx_id_t MockUnwrap::context_handle;
+GSSBuffer    MockUnwrap::inputMessageBuffer;
+GSSBuffer    MockUnwrap::outputMessageBuffer;
+int          MockUnwrap::conf_state;
+gss_qop_t    MockUnwrap::qop_state;
+
+
+void MockUnwrap::reset()
+{
+  retVal = 0;
+  minor_status = 0;
+  inputMessageBuffer.setValue(std::string());
+  outputMessageBuffer.setValue(std::string());
+  conf_state = 0;
+  qop_state= 0;
+}
diff --git a/json_gssapi/test/command_mocks/MockUnwrap.h b/json_gssapi/test/command_mocks/MockUnwrap.h
new file mode 100644 (file)
index 0000000..38ec004
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2014 <copyright holder> <email>
+ *
+ * For license details, see the LICENSE file in the root of this project.
+ *
+ */
+
+#ifndef MOCKUNWRAP_H
+#define MOCKUNWRAP_H
+
+#include "datamodel/GSSBuffer.h"
+#include <gssapi/gssapi.h>
+
+class MockUnwrap
+{
+public:
+  static OM_uint32     retVal;
+  static OM_uint32     minor_status;
+  static gss_ctx_id_t  context_handle;
+  static GSSBuffer     inputMessageBuffer;
+  static GSSBuffer     outputMessageBuffer;
+  static int           conf_state;
+  static gss_qop_t     qop_state;
+  
+  static void reset();
+
+};
+
+#endif // MOCKUNWRAP_H