Remove calls to GSSCommand constructor from derived classes
[gssweb.git] / json_gssapi / src / commands / GSSPseudoRandom.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 <stdexcept>
36
37 #include "GSSPseudoRandom.h"
38 #include <cache/GSSContextCache.h>
39 #include <datamodel/GSSContext.h>
40
41 GSSPseudoRandom::GSSPseudoRandom(JSONObject *params, 
42                                                gss_pseudo_random_type fn)
43 {
44   /* Variables */
45   /* Error checking */
46   /* Setup */
47   /* Main */
48   loadParameters(params);
49   function = fn;
50   /* Cleanup */
51   /* Return */
52   
53 }
54
55 /* JSON input
56  * 
57  *   {
58  *     "context_handle":     "########",
59  *     "prf_key":            ###,
60  *     "prf_in":             "la la la input message",
61  *     "desired_output_len": ####
62  *   }
63  * 
64  */
65 bool GSSPseudoRandom::loadParameters ( JSONObject* params )
66 {
67   /* Variables */
68   std::string sInputMessage;
69   
70   /* Error checking */
71   /* Setup */
72   /* Main */
73   
74   /***********
75    * prf_key *
76    ***********/
77   if ( ! params->get("prf_key").isNull() )
78   {
79     if (params->get("prf_key").isInteger())
80       this->key = params->get("prf_key").integer();
81     else
82       throw std::invalid_argument( "Unrecognized argument type for prf_key." );
83   }  
84
85   
86   /**********************
87    * desired_output_len *
88    **********************/
89   if ( ! params->get("desired_output_len").isNull() )
90   {
91     if (params->get("desired_output_len").isInteger())
92       this->desiredOutputLength = params->get("desired_output_len").integer();
93     else
94       throw std::invalid_argument( "Unrecognized argument type for desired_output_len." );
95   }  
96
97   
98   /**********
99    * prf_in *
100    **********/
101   if ( ! params->get("prf_in").isNull() )
102   {
103     if (params->get("prf_in").isString())
104     {
105       sInputMessage = params->get("prf_in").string();
106       this->inputMessage.setValue(sInputMessage);
107     }
108   }
109
110   
111   /******************
112    * context_handle *
113    ******************/
114   if ( ! params->get("context_handle").isNull() )
115   {
116     if (params->get("context_handle").isString())
117     {
118       std::string contextKey = params->get("context_handle").string();
119       GSSContext ctx = GSSContextCache::instance()->retrieve(contextKey);
120       this->context = ctx.getContext();
121     }
122   }
123   
124   /* Cleanup */
125   /* Return */
126   return true;
127 }
128
129
130 void GSSPseudoRandom::execute()
131 {
132   /* Variables */
133   gss_buffer_desc output_buf;
134   OM_uint32 minor = 0;
135   
136   retVal = function(
137     &minor,
138     this->context,
139     this->key,
140     this->inputMessage.toGss(),
141     this->desiredOutputLength,
142     &output_buf
143   );
144   
145   this->minor_status = minor;
146   this->outputMessage.setValue( (char *)output_buf.value, output_buf.length );
147 }
148
149 /* Desired JSON output:
150  * 
151  * {
152  *  "command":       "gss_pseudo_random",
153  *  "return_values": 
154  *  {
155  *      "major_status": 0,
156  *      "minor_status": 0,
157  *      "random_bytes": "asdf"
158  *  }
159  * }
160  */
161 JSONObject* GSSPseudoRandom::toJSON()
162 {
163   /* Variables */
164   JSONObject *values = new JSONObject();
165   
166   /* Error checking */
167   
168   /* Setup */
169   
170   /* Main */
171   // Return Values
172   // Easy stuff
173   values->set("major_status", this->retVal);
174   values->set("minor_status", this->minor_status);
175   
176   values->set(
177     "random_bytes",
178     this->outputMessage.toString().c_str()
179   );
180   
181   /* Cleanup */
182   
183   /* Return */
184   return(values);
185 }
186