7fdbe2a71b4f5ceab1af57cbcd60a686e52644d3
[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  * {"method": "gss_get_mic",
22  *  "arguments":
23  *  {
24  *       "context_handle": "#######",
25  *       "qop_req": "GSS_C_QOP_DEFAULT",
26  *       "input_message": "mary had a little lamb"
27  *  }
28  * }
29  * 
30  */
31 bool GSSGetMic::loadParameters ( JSONObject* params )
32 {
33   std::string sQopReq, sConfReq, sInputMessage;
34   
35   /* Error checking */
36   /* Setup */
37   // Should I zeroOut?
38   
39   /* Main processing */
40   
41   /***********
42    * QOP_REQ *
43    ***********/
44   if ( ! params->get("arguments").get("qop_req").isNull() )
45   {
46     if (params->get("arguments").get("qop_req").isString())
47     {
48       sQopReq = params->get("arguments").get("qop_req").string();
49       if (sQopReq == "GSS_C_QOP_DEFAULT")
50         this->qop_req = GSS_C_QOP_DEFAULT;
51       else
52         throw std::invalid_argument( std::string("Invalid QOP type given: ") + sQopReq );
53     } else if (params->get("arguments").get("qop_req").isInteger())
54       this->qop_req = (gss_cred_usage_t)( params->get("arguments").get("qop_req").integer() );
55     else
56       throw std::invalid_argument( "Unrecognized argument type for qop_req." );
57   }
58   
59   /*****************
60    * input_message *
61    *****************/
62   if ( ! params->get("arguments").get("input_message").isNull() )
63   {
64     sInputMessage = params->get("arguments").get("input_message").string();
65     this->inputMessage.setValue(sInputMessage);
66   }
67   
68   /******************
69    * context_handle *
70    ******************/
71   if ( ! params->get("arguments").get("context_handle").isNull() )
72   {
73     sInputMessage = params->get("arguments").get("context_handle").string();
74     GSSContext ctx = GSSContextCache::instance()->retrieve(sInputMessage);
75     this->context = ctx.getContext();
76   }
77   
78   /* Cleanup */
79   /* Return */
80   return true;
81 }
82
83 void GSSGetMic::execute()
84 {
85     gss_buffer_desc output;
86     
87     this->retVal =  this->function(
88       &(this->minor_status),
89       this->context,
90       this->qop_req,
91       this->inputMessage.toGss(),
92       &output
93     );
94     
95     this->outputToken.setValue( (char *)(output.value), output.length );
96 }
97
98 JSONObject* GSSGetMic::toJSON()
99 {
100   /* Variables */
101   JSONObject *ret = new JSONObject();
102   JSONObject *values = new JSONObject();
103   
104   /* Error checking */
105   
106   /* Setup */
107   
108   /* Main */
109   // Return Values
110   // Easy stuff
111   values->set("major_status", this->retVal);
112   values->set("minor_status", this->minor_status);
113   
114   values->set(
115     "output_token",
116     this->outputToken.toString().c_str()
117   );
118   
119   // Put it all together.
120   ret->set("command", "gss_get_mic");
121   ret->set("return_values", *values);
122   
123   /* Cleanup */
124   
125   /* Return */
126   return(ret);
127 }