907153cab2fbca8c184331ed47ee276e8ffcec8a
[trust_router.git] / gsscon / gsscon_passive.c
1 /*
2  * Copyright (c) 2012, 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  * This code was adapted from the MIT Kerberos Consortium's
34  * GSS example code, which was distributed under the following
35  * license:
36  *
37  * Copyright 2004-2006 Massachusetts Institute of Technology.
38  * All Rights Reserved.
39  *
40  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
41  * distribute this software and its documentation for any purpose and
42  * without fee is hereby granted, provided that the above copyright
43  * notice appear in all copies and that both that copyright notice and
44  * this permission notice appear in supporting documentation, and that
45  * the name of M.I.T. not be used in advertising or publicity pertaining
46  * to distribution of the software without specific, written prior
47  * permission.  Furthermore if you modify this software you must label
48  * your software as modified software and not distribute it in such a
49  * fashion that it might be confused with the original M.I.T. software.
50  * M.I.T. makes no representations about the suitability of
51  * this software for any purpose.  It is provided "as is" without express
52  * or implied warranty.
53  */
54
55 #include <gsscon.h>
56
57 const char *gServiceName = NULL;
58
59 int gsscon_passive_authenticate (int           inSocket, 
60                                  gss_ctx_id_t *outGSSContext,
61                                  client_cb_fn clientCb,
62                                  void *clientCbData)
63 {
64     int err = 0;
65     OM_uint32 majorStatus;
66     OM_uint32 minorStatus = 0;
67     gss_ctx_id_t gssContext = GSS_C_NO_CONTEXT;
68     gss_name_t clientName = GSS_C_NO_NAME, serviceName = GSS_C_NO_NAME;
69     gss_cred_id_t acceptorCredentials = NULL;
70     gss_buffer_desc clientDisplayName = {0, NULL};
71     gss_buffer_desc nameBuffer = {0, "trustidentity"};
72     
73     char *inputTokenBuffer = NULL;
74     size_t inputTokenBufferLength = 0;
75     gss_buffer_desc inputToken;  /* buffer received from the server */
76     
77
78     nameBuffer.length = strlen(nameBuffer.value);
79     if (inSocket <  0 ) { err = EINVAL; }
80     if (!outGSSContext) { err = EINVAL; }
81
82     if (!err)
83       majorStatus = gss_import_name (&minorStatus, &nameBuffer, (gss_OID) GSS_KRB5_NT_PRINCIPAL_NAME, &serviceName); 
84     if (majorStatus != GSS_S_COMPLETE) {
85         gsscon_print_gss_errors ("gss_import_name(inServiceName)", majorStatus, minorStatus);
86         err = minorStatus ? minorStatus : majorStatus; 
87       }
88
89     if (!err) {
90       majorStatus = gss_acquire_cred ( &minorStatus, serviceName,
91                                        GSS_C_INDEFINITE, GSS_C_NO_OID_SET,
92                                        GSS_C_ACCEPT, &acceptorCredentials,
93                                        NULL /*mechs out*/, NULL /*time out*/);
94       if (majorStatus != GSS_S_COMPLETE) { 
95         gsscon_print_gss_errors ("gss_acquire_cred", majorStatus, minorStatus);
96         err = minorStatus ? minorStatus : majorStatus; 
97       }
98     }
99
100     /* 
101      * The main authentication loop:
102      *
103      * GSS is a multimechanism API.  The number of packet exchanges required to  
104      * authenticatevaries between mechanisms.  As a result, we need to loop reading 
105      * input tokens from the client, calling gss_accept_sec_context on the input 
106      * tokens and send the resulting output tokens back to the client until we 
107      * get GSS_S_COMPLETE or an error.
108      *
109      * When we are done, save the client principal so we can make authorization 
110      * checks.
111      */
112     
113     majorStatus = GSS_S_CONTINUE_NEEDED;
114     while (!err && (majorStatus != GSS_S_COMPLETE)) {
115         /* Clean up old input buffer */
116         if (inputTokenBuffer != NULL) {
117             free (inputTokenBuffer);
118             inputTokenBuffer = NULL;  /* don't double-free */
119         }
120         
121         err = gsscon_read_token (inSocket, &inputTokenBuffer, &inputTokenBufferLength);
122         
123         if (!err) {
124             /* Set up input buffers for the next run through the loop */
125             inputToken.value = inputTokenBuffer;
126             inputToken.length = inputTokenBufferLength;
127         }
128         
129         if (!err) {
130             /* buffer to send to the server */
131             gss_buffer_desc outputToken = { 0, NULL }; 
132             
133             /*
134              * accept_sec_context does the actual work of taking the client's 
135              * request and generating an appropriate reply.              */
136             majorStatus = gss_accept_sec_context (&minorStatus, 
137                                                   &gssContext, 
138                                                   acceptorCredentials,
139                                                   &inputToken, 
140                                                   GSS_C_NO_CHANNEL_BINDINGS, 
141                                                   &clientName,
142                                                   NULL /* actual_mech_type */,
143                                                   &outputToken, 
144                                                   NULL /* req_flags */, 
145                                                   NULL /* time_rec */, 
146                                                   NULL /* delegated_cred_handle */);
147             
148             if ((outputToken.length > 0) && (outputToken.value != NULL)) {
149                 /* Send the output token to the client (even on error) */
150                 err = gsscon_write_token (inSocket, outputToken.value, outputToken.length);
151                 
152                 /* free the output token */
153                 gss_release_buffer (&minorStatus, &outputToken);
154             }
155         }
156         
157         if ((majorStatus != GSS_S_COMPLETE) && (majorStatus != GSS_S_CONTINUE_NEEDED)) {
158             gsscon_print_gss_errors ("gss_accept_sec_context", majorStatus, minorStatus);
159             err = minorStatus ? minorStatus : majorStatus; 
160         }            
161     }
162
163     if (!err) {
164       majorStatus = gss_display_name(&minorStatus, clientName, &clientDisplayName, NULL);
165       if (GSS_ERROR(majorStatus)) {
166         gsscon_print_gss_errors("gss_display_name", majorStatus, minorStatus);
167         err = EINVAL;
168       }
169       if (!err)
170         err = clientCb(clientName, &clientDisplayName, clientCbData);
171     }
172
173     if (!err) { 
174         *outGSSContext = gssContext;
175         gssContext = NULL;
176     } else {
177         gsscon_print_error (err, "Authenticate failed");
178     }
179     
180     if (inputTokenBuffer) { free (inputTokenBuffer); }
181     if (gssContext != GSS_C_NO_CONTEXT) { 
182         gss_delete_sec_context (&minorStatus, &gssContext, GSS_C_NO_BUFFER); }
183 if (clientName != GSS_C_NO_NAME)
184   gss_release_name(&minorStatus, &clientName);
185 if (clientDisplayName.value != NULL)
186   gss_release_buffer(&minorStatus, &clientDisplayName);
187  gss_release_name( &minorStatus, &serviceName);
188  gss_release_cred( &minorStatus, &acceptorCredentials);
189         
190     return err;
191 }
192
193 /* --------------------------------------------------------------------------- */
194
195 static int ServicePrincipalIsValidForService (const char *inServicePrincipal)
196 {
197     int err = 0;
198     krb5_context context = NULL;
199     krb5_principal principal = NULL;
200     
201     if (!inServicePrincipal) { err = EINVAL; }
202     
203     if (!err) {
204         err = krb5_init_context (&context);
205     }
206     
207     if (!err) {
208         err = krb5_parse_name (context, inServicePrincipal, &principal);
209     }
210     
211     if (!err) {
212         /* 
213          * Here is where we check to see if the service principal the client 
214          * used is valid.  Typically we would just check that the first component 
215          * is the name of the service provided by the server.  This check exists
216          * to make sure the server is using the correct key in its keytab since
217          * we passed GSS_C_NO_CREDENTIAL into gss_accept_sec_context().
218          */
219         if (gServiceName && strcmp (gServiceName, 
220                                     krb5_princ_name (context, principal)->data) != 0) {
221             err = KRB5KRB_AP_WRONG_PRINC;
222         }
223     }
224     
225     if (principal) { krb5_free_principal (context, principal); }
226     if (context  ) { krb5_free_context (context); }
227     
228     return err;
229 }
230
231
232 /* --------------------------------------------------------------------------- */
233
234 static int ClientPrincipalIsAuthorizedForService (const char *inClientPrincipal)
235 {
236     int err = 0;
237         /* 
238          * Here is where the server checks to see if the client principal should 
239          * be allowed to use your service. Typically it should check both the name 
240          * and the realm, since with cross-realm shared keys, a user at another 
241          * realm may be trying to contact your service.  
242          */
243         err = 0;
244
245     
246     
247     return err;
248 }
249
250 /* --------------------------------------------------------------------------- */
251
252 int gsscon_authorize (gss_ctx_id_t  inContext, 
253                       int          *outAuthorized, 
254                       int          *outAuthorizationError)
255 {
256     int err = 0;
257     OM_uint32 majorStatus;
258     OM_uint32 minorStatus = 0;
259     gss_name_t clientName = NULL;
260     gss_name_t serviceName = NULL;
261     char *clientPrincipal = NULL;
262     char *servicePrincipal = NULL;
263
264     if (!inContext            ) { err = EINVAL; }
265     if (!outAuthorized        ) { err = EINVAL; }
266     if (!outAuthorizationError) { err = EINVAL; }
267     
268     if (!err) {
269         /* Get the client and service principals used to authenticate */
270         majorStatus = gss_inquire_context (&minorStatus, 
271                                            inContext, 
272                                            &clientName, 
273                                            &serviceName, 
274                                            NULL, NULL, NULL, NULL, NULL);
275         if (majorStatus != GSS_S_COMPLETE) { 
276             err = minorStatus ? minorStatus : majorStatus; 
277         }
278     }
279     
280     if (!err) {
281         /* Pull the client principal string out of the gss name */
282         gss_buffer_desc nameToken;
283         
284         majorStatus = gss_display_name (&minorStatus, 
285                                         clientName, 
286                                         &nameToken, 
287                                         NULL);
288         if (majorStatus != GSS_S_COMPLETE) { 
289             err = minorStatus ? minorStatus : majorStatus; 
290         }
291         
292         if (!err) {
293             clientPrincipal = malloc (nameToken.length + 1);
294             if (clientPrincipal == NULL) { err = ENOMEM; }
295         }
296         
297         if (!err) {
298             memcpy (clientPrincipal, nameToken.value, nameToken.length);
299             clientPrincipal[nameToken.length] = '\0';
300         }        
301
302         if (nameToken.value) { gss_release_buffer (&minorStatus, &nameToken); }
303     }
304     
305         if (!err) {
306     //    /* Pull the service principal string out of the gss name */
307     //    gss_buffer_desc nameToken;
308     //    
309     //    majorStatus = gss_display_name (&minorStatus, 
310     //                                    serviceName, 
311     //                                    &nameToken, 
312     //                                    NULL);
313     //    if (majorStatus != GSS_S_COMPLETE) { 
314     //        err = minorStatus ? minorStatus : majorStatus; 
315     //    }
316     //    
317     //    if (!err) {
318     //        servic7ePrincipal = malloc (nameToken.length + 1);
319     //        if (servicePrincipal == NULL) { err = ENOMEM; }
320     //    }
321     //    
322     //    if (!err) {
323     //        memcpy (servicePrincipal, nameToken.value, nameToken.length);
324     //        servicePrincipal[nameToken.length] = '\0';
325     //    }        
326
327     //    if (nameToken.value) { gss_release_buffer (&minorStatus, &nameToken); }
328     // }
329     
330 //    if (!err) {
331 //        int authorizationErr = ServicePrincipalIsValidForService (servicePr// incipal);
332 //        
333 //        if (!authorizationErr) {
334
335           int authorizationErr = 0;
336           authorizationErr = ClientPrincipalIsAuthorizedForService (clientPrincipal);
337
338 //        }
339         
340 //        printf ("'%s' is%s authorized for service '%s'\n", 
341 //                clientPrincipal, authorizationErr ? " NOT" : "", servicePrincipal);            
342 //        
343           *outAuthorized = !authorizationErr;
344           *outAuthorizationError = authorizationErr;
345         }
346     
347     if (serviceName     ) { gss_release_name (&minorStatus, &serviceName); }
348     if (clientName      ) { gss_release_name (&minorStatus, &clientName); }
349     if (clientPrincipal ) { free (clientPrincipal); }
350     if (servicePrincipal) { free (servicePrincipal); }
351
352     return err; 
353 }
354
355