Adding GSS Pseudo Random
[gssweb.git] / json_gssapi / src / 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
12 GSSPseudoRandom::GSSPseudoRandom(JSONObject *params, 
13                                                gss_pseudo_random_type fn) : GSSCommand(params)
14 {
15   /* Variables */
16   /* Error checking */
17   /* Setup */
18   /* Main */
19   loadParameters(params);
20   function = fn;
21   /* Cleanup */
22   /* Return */
23   
24 }
25
26 /* JSON input
27  * 
28  * {"method":    "gss_pseudo_random",
29  *  "arguments": 
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("arguments").get("prf_key").isNull() )
51   {
52     if (params->get("arguments").get("prf_key").isInteger())
53       this->key = params->get("arguments").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("arguments").get("desired_output_len").isNull() )
63   {
64     if (params->get("arguments").get("desired_output_len").isInteger())
65       this->desiredOutputLength = params->get("arguments").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("arguments").get("prf_in").isNull() )
75   {
76     if (params->get("arguments").get("prf_in").isString())
77     {
78       sInputMessage = params->get("arguments").get("prf_in").string();
79       this->inputMessage.setValue(sInputMessage);
80     }
81   }
82   
83   /* Cleanup */
84   /* Return */
85   return true;
86 }
87
88
89 void GSSPseudoRandom::execute()
90 {
91   /* Variables */
92   gss_buffer_desc output_buf;
93   OM_uint32 minor = 0;
94   
95   retVal = function(
96     &minor,
97     this->context,
98     this->key,
99     this->inputMessage.toGss(),
100     this->desiredOutputLength,
101     &output_buf
102   );
103   
104   this->minor_status = minor;
105   this->outputMessage.setValue( (char *)output_buf.value, output_buf.length );
106 }
107
108 /* Desired JSON output:
109  * 
110  * {
111  *  "command":       "gss_pseudo_random",
112  *  "return_values": 
113  *  {
114  *      "major_status": 0,
115  *      "minor_status": 0,
116  *      "random_bytes": "asdf"
117  *  }
118  * }
119  */
120 JSONObject* GSSPseudoRandom::toJSON()
121 {
122   /* Variables */
123   JSONObject *ret = new JSONObject();
124   JSONObject *values = new JSONObject();
125   
126   /* Error checking */
127   
128   /* Setup */
129   
130   /* Main */
131   // Return Values
132   // Easy stuff
133   values->set("major_status", this->retVal);
134   values->set("minor_status", this->minor_status);
135   
136   values->set(
137     "random_bytes",
138     this->outputMessage.toString().c_str()
139   );
140   
141   // Put it all together.
142   ret->set("command", "gss_pseudo_random");
143   ret->set("return_values", *values);
144   
145   /* Cleanup */
146   
147   /* Return */
148   return(ret);
149 }
150