Message passing with cookies (app_tag, gssweb_bg_tag, etc.)
[gssweb.git] / json_gssapi / src / commands / GSSGetMic.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 "GSSGetMic.h"
9 #include <cache/GSSContextCache.h>
10
11 #include <stdexcept>
12 #include <mit-krb5/gssapi/gssapi.h>
13
14 GSSGetMic::GSSGetMic ( JSONObject* params, gss_get_mic_type fn )
15 {
16     this->function = fn;
17     loadParameters(params);
18 }
19
20 /*
21  *  {
22  *       "context_handle": "#######",
23  *       "qop_req": "GSS_C_QOP_DEFAULT",
24  *       "input_message": "mary had a little lamb"
25  *  }
26  * 
27  */
28 bool GSSGetMic::loadParameters ( JSONObject* params )
29 {
30   std::string sQopReq, sConfReq, sInputMessage;
31   
32   /* Error checking */
33   /* Setup */
34   // Should I zeroOut?
35   
36   /* Main processing */
37   
38   /***********
39    * QOP_REQ *
40    ***********/
41   if ( ! params->get("qop_req").isNull() )
42   {
43     if (params->get("qop_req").isString())
44     {
45       sQopReq = params->get("qop_req").string();
46       if (sQopReq == "GSS_C_QOP_DEFAULT")
47         this->qop_req = GSS_C_QOP_DEFAULT;
48       else
49         throw std::invalid_argument( std::string("Invalid QOP type given: ") + sQopReq );
50     } else if (params->get("qop_req").isInteger())
51       this->qop_req = (gss_cred_usage_t)( params->get("qop_req").integer() );
52     else
53       throw std::invalid_argument( "Unrecognized argument type for qop_req." );
54   }
55   
56   /*****************
57    * input_message *
58    *****************/
59   if ( ! params->get("input_message").isNull() )
60   {
61     sInputMessage = params->get("input_message").string();
62     this->inputMessage.setValue(sInputMessage);
63   }
64   
65   /******************
66    * context_handle *
67    ******************/
68   if ( ! params->get("context_handle").isNull() )
69   {
70     sInputMessage = params->get("context_handle").string();
71     GSSContext ctx = GSSContextCache::instance()->retrieve(sInputMessage);
72     this->context = ctx.getContext();
73   }
74   
75   /* Cleanup */
76   /* Return */
77   return true;
78 }
79
80 void GSSGetMic::execute()
81 {
82     gss_buffer_desc output;
83     
84     this->retVal =  this->function(
85       &(this->minor_status),
86       this->context,
87       this->qop_req,
88       this->inputMessage.toGss(),
89       &output
90     );
91     
92     this->outputToken.setValue( (char *)(output.value), output.length );
93 }
94
95 JSONObject* GSSGetMic::toJSON()
96 {
97   /* Variables */
98   JSONObject *values = new JSONObject();
99   
100   /* Error checking */
101   
102   /* Setup */
103   
104   /* Main */
105   // Return Values
106   // Easy stuff
107   values->set("major_status", this->retVal);
108   values->set("minor_status", this->minor_status);
109   
110   values->set(
111     "output_token",
112     this->outputToken.toString().c_str()
113   );
114   
115   /* Cleanup */
116   
117   /* Return */
118   return(values);
119 }