8e2d742612eecea9c4f415677ca11402dbec0e91
[gssweb.git] / json_gssapi / src / commands / GSSUnwrap.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 "GSSUnwrap.h"
9 #include <cache/GSSContextCache.h>
10
11 GSSUnwrap::GSSUnwrap ( JSONObject* params, gss_unwrap_type fn )
12 {
13   function = fn;
14   loadParameters(params);
15 }
16
17 bool GSSUnwrap::loadParameters(JSONObject *params)
18 {
19   /* Variables */
20   std::string sInputMessage;
21   
22   /* Error checking */
23   /* Setup */
24   // Should I zeroOut?
25   
26   /* Main processing */
27   
28   /*****************
29    * input_message *
30    *****************/
31   if ( ! params->get("arguments").get("input_message").isNull() )
32   {
33     sInputMessage = params->get("arguments").get("input_message").string();
34     this->inputMessage.setValue(sInputMessage);
35   }
36   
37   /******************
38    * context_handle *
39    ******************/
40   if ( ! params->get("arguments").get("context_handle").isNull() )
41   {
42     sInputMessage = params->get("arguments").get("context_handle").string();
43     GSSContext ctx = GSSContextCache::instance()->retrieve(sInputMessage);
44     this->context = ctx.getContext();
45   }
46   
47   /* Cleanup */
48   /* Return */
49   return true;
50 }
51
52
53 void GSSUnwrap::execute()
54 {
55   /* Variables */
56   gss_buffer_desc output_buf;
57   
58   retVal = function(
59     &(this->minor_status),
60     this->context,
61     this->inputMessage.toGss(),
62     &output_buf,
63     &(this->conf_state),
64     &(this->qop_state)
65   );
66   
67   this->outputMessage.setValue( (char *)output_buf.value, output_buf.length );
68 }
69
70 JSONObject* GSSUnwrap::toJSON()
71 {
72   /* Variables */
73   const char *conf_state;
74   JSONObject *ret = new JSONObject();
75   JSONObject *values = new JSONObject();
76   
77   /* Error checking */
78   
79   /* Setup */
80   
81   /* Main */
82   // Return Values
83   // Easy stuff
84   values->set("major_status", this->retVal);
85   values->set("minor_status", this->minor_status);
86   
87   values->set("qop_state", this->qop_state);
88   
89   conf_state = (this->conf_state) ? "TRUE" : "FALSE";
90   values->set("conf_state", conf_state);
91   
92   values->set(
93     "output_message",
94     this->outputMessage.toString().c_str()
95   );
96   
97   // Put it all together.
98   ret->set("command", "gss_wrap");
99   ret->set("return_values", *values);
100   
101   /* Cleanup */
102   
103   /* Return */
104   return(ret);
105 }