Add Janet copyright to GSS command files.
[gssweb.git] / json_gssapi / src / commands / GSSWrap.cpp
1 /*
2  * Copyright (c) 2014, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35 #include "GSSWrap.h"
36 #include <cache/GSSContextCache.h>
37 #include <stdexcept>
38
39 /*
40  *     gss_wrap_type function;
41     int conf_req;
42     gss_ctx_id_t context;
43     gss_qop_t qop_req;
44     GSSBuffer inputMessage;
45     GSSBuffer outputMessage;
46     
47   
48     OM_uint32 retVal;
49     OM_uint32 minor_status;
50     int conf_state;
51  */
52
53 bool GSSWrap::loadParameters(JSONObject *params)
54 {
55   /* Variables */
56   std::string sQopReq, sConfReq, sInputMessage;
57   
58   /* Error checking */
59   /* Setup */
60   // Should I zeroOut?
61   
62   /* Main processing */
63   
64   /************
65    * CONF_REQ *
66    ************/
67   if ( ! params->get("conf_req").isNull() )
68   {
69     if (params->get("conf_req").isString())
70     {
71       sConfReq = params->get("conf_req").string();
72       if (sConfReq == "TRUE")
73         this->conf_req = 1;
74       else if (sConfReq == "FALSE")
75         this->conf_req = 0;
76       else
77         throw std::invalid_argument( std::string("Invalid QOP type given: ") + sConfReq );
78     } else if (params->get("conf_req").isInteger())
79       this->conf_req = (gss_cred_usage_t)( params->get("conf_req").integer() );
80     else
81       throw std::invalid_argument( "Unrecognized argument type for conf_req." );
82   }  
83   
84   /***********
85    * QOP_REQ *
86    ***********/
87   if ( ! params->get("qop_req").isNull() )
88   {
89     if (params->get("qop_req").isString())
90     {
91       sQopReq = params->get("qop_req").string();
92       if (sQopReq == "GSS_C_QOP_DEFAULT")
93         this->qop_req = GSS_C_QOP_DEFAULT;
94       else
95         throw std::invalid_argument( std::string("Invalid QOP type given: ") + sQopReq );
96     } else if (params->get("qop_req").isInteger())
97       this->qop_req = (gss_cred_usage_t)( params->get("qop_req").integer() );
98     else
99       throw std::invalid_argument( "Unrecognized argument type for qop_req." );
100   }
101   
102   /*****************
103    * input_message *
104    *****************/
105   if ( ! params->get("input_message").isNull() )
106   {
107     sInputMessage = params->get("input_message").string();
108     this->inputMessage.setValue(sInputMessage);
109   }
110   
111   /***********
112    * context *
113    ***********/
114   if ( ! params->get("context_handle").isNull() )
115   {
116     std::string contextKey = params->get("context_handle").string();
117     GSSContext ctx = GSSContextCache::instance()->retrieve(contextKey);
118     this->context = ctx.getContext();
119   }
120   
121   /* Cleanup */
122   /* Return */
123   return true;
124 }
125
126 GSSWrap::GSSWrap ( JSONObject* params, gss_wrap_type fn )
127 {
128   function = fn;
129   loadParameters(params);
130 }
131
132
133 GSSWrap::~GSSWrap()
134 {
135
136 }
137
138 void GSSWrap::execute()
139 {
140   /* Variables */
141   gss_buffer_desc output_buf;
142   
143   retVal = function(
144     &(this->minor_status),
145     this->context,
146     this->conf_req,
147     this->qop_req,
148     this->inputMessage.toGss(),
149     &(this->conf_state),
150     &output_buf
151   );
152   
153   this->outputMessage.setValue( (char *)output_buf.value, output_buf.length );
154 }
155
156 /* Desired JSON output:
157  * 
158  *  {
159  *      "major_status":   0,
160  *      "minor_status":   0,
161  *      "conf_state":     "TRUE",
162  *      "output_message": "asdf"
163  *  }
164  * 
165  */
166 JSONObject *GSSWrap::toJSON()
167 {
168   /* Variables */
169   const char *conf_state;
170   JSONObject *values = new JSONObject();
171   
172   /* Error checking */
173   
174   /* Setup */
175   
176   /* Main */
177   // Return Values
178   // Easy stuff
179   values->set("major_status", this->retVal);
180   values->set("minor_status", this->minor_status);
181   
182   conf_state = (this->conf_state) ? "TRUE" : "FALSE";
183   values->set("conf_state", conf_state);
184   
185   values->set(
186     "output_message",
187     this->outputMessage.toString().c_str()
188   );
189   
190   /* Cleanup */
191   
192   /* Return */
193   return(values);
194 }
195