take mit-krb5 out of include path
[gssweb.git] / json_gssapi / src / commands / GSSGetMic.cpp
1 /*
2  * Copyright (c) 2014, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35 #include "GSSGetMic.h"
36 #include <cache/GSSContextCache.h>
37
38 #include <stdexcept>
39 #include <gssapi/gssapi.h>
40
41 GSSGetMic::GSSGetMic ( JSONObject* params, gss_get_mic_type fn )
42 {
43     this->function = fn;
44     loadParameters(params);
45 }
46
47 /*
48  *  {
49  *       "context_handle": "#######",
50  *       "qop_req": "GSS_C_QOP_DEFAULT",
51  *       "input_message": "mary had a little lamb"
52  *  }
53  * 
54  */
55 bool GSSGetMic::loadParameters ( JSONObject* params )
56 {
57   std::string sQopReq, sConfReq, sInputMessage;
58   
59   /* Error checking */
60   /* Setup */
61   // Should I zeroOut?
62   
63   /* Main processing */
64   
65   /***********
66    * QOP_REQ *
67    ***********/
68   if ( ! params->get("qop_req").isNull() )
69   {
70     if (params->get("qop_req").isString())
71     {
72       sQopReq = params->get("qop_req").string();
73       if (sQopReq == "GSS_C_QOP_DEFAULT")
74         this->qop_req = GSS_C_QOP_DEFAULT;
75       else
76         throw std::invalid_argument( std::string("Invalid QOP type given: ") + sQopReq );
77     } else if (params->get("qop_req").isInteger())
78       this->qop_req = (gss_cred_usage_t)( params->get("qop_req").integer() );
79     else
80       throw std::invalid_argument( "Unrecognized argument type for qop_req." );
81   }
82   
83   /*****************
84    * input_message *
85    *****************/
86   if ( ! params->get("input_message").isNull() )
87   {
88     sInputMessage = params->get("input_message").string();
89     this->inputMessage.setValue(sInputMessage);
90   }
91   
92   /******************
93    * context_handle *
94    ******************/
95   if ( ! params->get("context_handle").isNull() )
96   {
97     sInputMessage = params->get("context_handle").string();
98     GSSContext ctx = GSSContextCache::instance()->retrieve(sInputMessage);
99     this->context = ctx.getContext();
100   }
101   
102   /* Cleanup */
103   /* Return */
104   return true;
105 }
106
107 void GSSGetMic::execute()
108 {
109     gss_buffer_desc output;
110     
111     this->retVal =  this->function(
112       &(this->minor_status),
113       this->context,
114       this->qop_req,
115       this->inputMessage.toGss(),
116       &output
117     );
118     
119     this->outputToken.setValue( (char *)(output.value), output.length );
120 }
121
122 JSONObject* GSSGetMic::toJSON()
123 {
124   /* Variables */
125   JSONObject *values = new JSONObject();
126   
127   /* Error checking */
128   
129   /* Setup */
130   
131   /* Main */
132   // Return Values
133   // Easy stuff
134   values->set("major_status", this->retVal);
135   values->set("minor_status", this->minor_status);
136   
137   values->set(
138     "output_token",
139     this->outputToken.toString().c_str()
140   );
141   
142   /* Cleanup */
143   
144   /* Return */
145   return(values);
146 }