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