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