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