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