GSSAcquireCred works.
[gssweb.git] / json_gssapi / src / commands / GSSAcquireCred.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 "GSSAcquireCred.h"
36 #include "GSSException.h"
37 #include <cache/GSSCredentialCache.h>
38 #include <cache/GSSNameCache.h>
39 #include <datamodel/GSSDisplayStatus.h>
40
41 #include <stdexcept>
42
43 GSSAcquireCred::GSSAcquireCred(gss_acq_cred_type fn) : function(fn)
44 {
45   desired_name = GSS_C_NO_NAME;
46 }
47
48 GSSAcquireCred::GSSAcquireCred ( const GSSAcquireCred& other )
49 {
50
51 }
52
53 GSSAcquireCred::~GSSAcquireCred()
54 {
55
56 }
57
58 GSSAcquireCred::GSSAcquireCred ( 
59     JSONObject *params, 
60     gss_acq_cred_type fn
61 )
62 {
63   /* Variables */
64   /* Error checking */
65   /* Setup */
66   /* Main */
67   loadParameters(params);
68   function = fn;
69   values = new JSONObject();
70
71   /* Cleanup */
72   /* Return */
73 }
74
75
76 bool GSSAcquireCred::loadParameters(JSONObject *params)
77 {
78   /* Variables */
79   std::string sCredUsage;
80   size_t nDesiredMechs;
81   
82   /* Error checking */
83   /* Setup */
84   // Should I zeroOut?
85   
86   /* Main processing */
87   // Easy stuff(*params)
88   this->time_req = (OM_uint32 )(*params)["time_req"].integer();
89
90   /**************
91    * cred_usage *
92    **************/
93   if ( ! params->get("cred_usage").isNull() )
94   {
95     if (params->get("cred_usage").isString())
96     {
97       sCredUsage = params->get("cred_usage").string();
98       if (sCredUsage == "GSS_C_BOTH")
99         this->cred_usage = GSS_C_BOTH;
100       else if (sCredUsage == "GSS_C_INITIATE")
101         this->cred_usage = GSS_C_INITIATE;
102       else if (sCredUsage == "GSS_C_ACCEPT")
103         this->cred_usage = GSS_C_ACCEPT;
104       else
105         throw std::invalid_argument( std::string("Invalid cred_usage type given: ") + sCredUsage );
106     } else if (params->get("cred_usage").isInteger())
107       this->cred_usage = (gss_cred_usage_t)( params->get("cred_usage").integer() );
108     else
109       throw std::invalid_argument( "Unrecognized argument type for cred_usage." );
110       }
111   
112   /*****************
113    * desired_mechs *
114    *****************/
115   if ( ! params->get("desired_mechs").isNull() )
116   {
117     if ( params->get("desired_mechs").isArray() )
118     {
119       for (nDesiredMechs = 0; 
120           nDesiredMechs < params->get("desired_mechs").size();
121           nDesiredMechs++)
122       {
123         std::string mechStr = params->get("desired_mechs")[nDesiredMechs].string();
124         desiredMechs.addOID( GSSOID(mechStr).toGss() );
125       }
126     } else
127       throw std::invalid_argument("Unrecognized desired_mechs array.");
128   } else {
129     // Use OID for eap-aes128 by default
130     desiredMechs.addOID( GSSOID((char *)"{ 1 3 6 1 5 5 15 1 1 17 }") );
131   }
132
133   /****************
134    * desired_name *
135    ****************/
136   if ( ! params->get("desired_name").isNull() )
137   {
138     std::string key = params->get("desired_name").string();
139     this->desired_name = GSSNameCache::instance()->retrieve(key);
140   }
141
142   
143   /* Cleanup */
144   /* Return */
145   return true;
146 }
147
148
149
150 void GSSAcquireCred::execute()
151 {
152   /* Variables */
153   gss_cred_id_t output_cred_handle;
154   gss_OID_set   actual_mechs;
155   JSONObject    errors;
156   std::string   key;
157   
158   /* Error checking */
159   /* Setup */
160   /* Main */
161   this->retVal = function(
162     &this->minor_status,
163     this->desired_name.toGss(),
164     this->time_req,
165     this->desiredMechs.toGss(),
166     this->cred_usage,
167     &output_cred_handle,
168     &actual_mechs,
169     &this->time_rec
170   );
171
172   GSSDisplayStatus ds(retVal, minor_status, NULL);
173   errors.set("major_status_message", ds.getMajorMessage().c_str());
174   errors.set("minor_status_message", ds.getMinorMessage().c_str());
175   values->set("errors", errors);
176   
177   this->cred.setValue(output_cred_handle);
178   key = GSSCredentialCache::instance()->store(this->cred);
179   this->cred.setKey(key);
180   this->actualMechs = actual_mechs;
181   
182   /* Cleanup */
183   /* Return */
184 }
185
186 /* Desired JSON output:
187  * 
188  * {
189  *     "major_status": 0,
190  *     "minor_status": 0,
191  *     "cred_handle": "###########",
192  *     "actual_mechs": [
193  *       "{ 1 2 3 4 }",
194  *       "{ 5 6 7 8 }"
195  *     ],
196  *     "time_rec": 0
197  * }
198  */
199 JSONObject *GSSAcquireCred::toJSON()
200 {
201   /* Variables */
202   JSONObject *temp;
203   
204   /* Error checking */
205   
206   /* Setup */
207   
208   /* Main */
209   // Return Values
210   // Easy stuff
211   values->set("major_status", this->retVal);
212   values->set("minor_status", this->minor_status);
213   values->set("time_rec", (int)this->time_rec );
214   
215   values->set("output_cred_handle", this->cred.getKey().c_str() );
216   
217   // Objects that generate their own JSONObject
218   temp = this->actualMechs.toJSONValue();
219   values->set("actual_mechs", *temp);
220   
221   /* Cleanup */
222   
223   /* Return */
224   return(values);
225 }