c633679b5e87b44604321389f5cdbd6790932bc1
[gssweb.git] / json_gssapi / src / GSSWrap.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 "GSSWrap.h"
9 #include <stdexcept>
10
11 /*
12  *     gss_wrap_type function;
13     int conf_req;
14     gss_ctx_id_t context;
15     gss_qop_t qop_req;
16     GSSBuffer inputMessage;
17     GSSBuffer outputMessage;
18     
19   
20     OM_uint32 retVal;
21     OM_uint32 minor_status;
22     int conf_state;
23  */
24
25 bool GSSWrap::loadParameters(JSONObject *params)
26 {
27   /* Variables */
28   std::string sQopReq, sConfReq, sInputMessage;
29   
30   /* Error checking */
31   /* Setup */
32   // Should I zeroOut?
33   
34   /* Main processing */
35   
36   /************
37    * CONF_REQ *
38    ************/
39   if ( ! params->get("arguments").get("conf_req").isNull() )
40   {
41     if (params->get("arguments").get("conf_req").isString())
42     {
43       sConfReq = params->get("arguments").get("conf_req").string();
44       if (sConfReq == "TRUE")
45         this->conf_req = 1;
46       else if (sConfReq == "FALSE")
47         this->conf_req = 0;
48       else
49         throw std::invalid_argument( std::string("Invalid QOP type given: ") + sConfReq );
50     } else if (params->get("arguments").get("conf_req").isInteger())
51       this->conf_req = (gss_cred_usage_t)( params->get("arguments").get("conf_req").integer() );
52     else
53       throw std::invalid_argument( "Unrecognized argument type for conf_req." );
54   }  
55   
56   /***********
57    * QOP_REQ *
58    ***********/
59   if ( ! params->get("arguments").get("qop_req").isNull() )
60   {
61     if (params->get("arguments").get("qop_req").isString())
62     {
63       sQopReq = params->get("arguments").get("qop_req").string();
64       if (sQopReq == "GSS_C_QOP_DEFAULT")
65         this->qop_req = GSS_C_QOP_DEFAULT;
66       else
67         throw std::invalid_argument( std::string("Invalid QOP type given: ") + sQopReq );
68     } else if (params->get("arguments").get("qop_req").isInteger())
69       this->qop_req = (gss_cred_usage_t)( params->get("arguments").get("qop_req").integer() );
70     else
71       throw std::invalid_argument( "Unrecognized argument type for qop_req." );
72   }
73   
74   /*****************
75    * input_message *
76    *****************/
77   if ( ! params->get("arguments").get("input_message").isNull() )
78   {
79     sInputMessage = params->get("arguments").get("input_message").string();
80     this->inputMessage.setValue(sInputMessage);
81   }
82   
83   /* Cleanup */
84   /* Return */
85   return true;
86 }
87
88 GSSWrap::GSSWrap ( JSONObject* params, gss_wrap_type fn )
89 {
90   function = fn;
91   loadParameters(params);
92 }
93
94
95 GSSWrap::~GSSWrap()
96 {
97
98 }
99
100 void GSSWrap::execute()
101 {
102   /* Variables */
103   gss_buffer_desc output_buf;
104   
105   retVal = function(
106     &(this->minor_status),
107     this->context,
108     this->conf_req,
109     this->qop_req,
110     this->inputMessage.toGss(),
111     &(this->conf_state),
112     &output_buf
113   );
114   
115   this->outputMessage.setValue( (char *)output_buf.value, output_buf.length );
116 }
117
118 /* Desired JSON output:
119  * 
120  * {
121  *  "command":       "gss_wrap",
122  *  "return_values": 
123  *  {
124  *      "major_status":   0,
125  *      "minor_status":   0,
126  *      "conf_state":     "TRUE",
127  *      "output_message": "asdf"
128  *  }
129  * }
130  */
131 JSONObject *GSSWrap::toJSON()
132 {
133   /* Variables */
134   const char *conf_state;
135   JSONObject *ret = new JSONObject();
136   JSONObject *values = new JSONObject();
137   
138   /* Error checking */
139   
140   /* Setup */
141   
142   /* Main */
143   // Return Values
144   // Easy stuff
145   values->set("major_status", this->retVal);
146   values->set("minor_status", this->minor_status);
147   
148   conf_state = (this->conf_state) ? "TRUE" : "FALSE";
149   values->set("conf_state", conf_state);
150   
151   values->set(
152     "output_message",
153     this->outputMessage.toString().c_str()
154   );
155   
156   // Put it all together.
157   ret->set("command", "gss_wrap");
158   ret->set("return_values", *values);
159   
160   /* Cleanup */
161   
162   /* Return */
163   return(ret);
164 }
165