• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 2014 - 2019, Steve Holme, <steve_holme@hotmail.com>.
9  * Copyright (C) 2015 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
10  *
11  * This software is licensed as described in the file COPYING, which
12  * you should have received as part of this distribution. The terms
13  * are also available at https://curl.se/docs/copyright.html.
14  *
15  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
16  * copies of the Software, and permit persons to whom the Software is
17  * furnished to do so, under the terms of the COPYING file.
18  *
19  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20  * KIND, either express or implied.
21  *
22  * RFC4752 The Kerberos V5 ("GSSAPI") SASL Mechanism
23  *
24  ***************************************************************************/
25 
26 #include "curl_setup.h"
27 
28 #if defined(HAVE_GSSAPI) && defined(USE_KERBEROS5)
29 
30 #include <curl/curl.h>
31 
32 #include "vauth/vauth.h"
33 #include "curl_sasl.h"
34 #include "urldata.h"
35 #include "curl_gssapi.h"
36 #include "sendf.h"
37 #include "curl_printf.h"
38 
39 /* The last #include files should be: */
40 #include "curl_memory.h"
41 #include "memdebug.h"
42 
43 /*
44  * Curl_auth_is_gssapi_supported()
45  *
46  * This is used to evaluate if GSSAPI (Kerberos V5) is supported.
47  *
48  * Parameters: None
49  *
50  * Returns TRUE if Kerberos V5 is supported by the GSS-API library.
51  */
Curl_auth_is_gssapi_supported(void)52 bool Curl_auth_is_gssapi_supported(void)
53 {
54   return TRUE;
55 }
56 
57 /*
58  * Curl_auth_create_gssapi_user_message()
59  *
60  * This is used to generate an already encoded GSSAPI (Kerberos V5) user token
61  * message ready for sending to the recipient.
62  *
63  * Parameters:
64  *
65  * data        [in]     - The session handle.
66  * userp       [in]     - The user name.
67  * passwdp     [in]     - The user's password.
68  * service     [in]     - The service type such as http, smtp, pop or imap.
69  * host        [in[     - The host name.
70  * mutual_auth [in]     - Flag specifying whether or not mutual authentication
71  *                        is enabled.
72  * chlg        [in]     - Optional challenge message.
73  * krb5        [in/out] - The Kerberos 5 data struct being used and modified.
74  * out         [out]    - The result storage.
75  *
76  * Returns CURLE_OK on success.
77  */
Curl_auth_create_gssapi_user_message(struct Curl_easy * data,const char * userp,const char * passwdp,const char * service,const char * host,const bool mutual_auth,const struct bufref * chlg,struct kerberos5data * krb5,struct bufref * out)78 CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
79                                               const char *userp,
80                                               const char *passwdp,
81                                               const char *service,
82                                               const char *host,
83                                               const bool mutual_auth,
84                                               const struct bufref *chlg,
85                                               struct kerberos5data *krb5,
86                                               struct bufref *out)
87 {
88   CURLcode result = CURLE_OK;
89   OM_uint32 major_status;
90   OM_uint32 minor_status;
91   OM_uint32 unused_status;
92   gss_buffer_desc spn_token = GSS_C_EMPTY_BUFFER;
93   gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
94   gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
95 
96   (void) userp;
97   (void) passwdp;
98 
99   if(!krb5->spn) {
100     /* Generate our SPN */
101     char *spn = Curl_auth_build_spn(service, NULL, host);
102     if(!spn)
103       return CURLE_OUT_OF_MEMORY;
104 
105     /* Populate the SPN structure */
106     spn_token.value = spn;
107     spn_token.length = strlen(spn);
108 
109     /* Import the SPN */
110     major_status = gss_import_name(&minor_status, &spn_token,
111                                    GSS_C_NT_HOSTBASED_SERVICE, &krb5->spn);
112     if(GSS_ERROR(major_status)) {
113       Curl_gss_log_error(data, "gss_import_name() failed: ",
114                          major_status, minor_status);
115 
116       free(spn);
117 
118       return CURLE_AUTH_ERROR;
119     }
120 
121     free(spn);
122   }
123 
124   if(chlg) {
125     if(!Curl_bufref_len(chlg)) {
126       infof(data, "GSSAPI handshake failure (empty challenge message)");
127       return CURLE_BAD_CONTENT_ENCODING;
128     }
129     input_token.value = (void *) Curl_bufref_ptr(chlg);
130     input_token.length = Curl_bufref_len(chlg);
131   }
132 
133   major_status = Curl_gss_init_sec_context(data,
134                                            &minor_status,
135                                            &krb5->context,
136                                            krb5->spn,
137                                            &Curl_krb5_mech_oid,
138                                            GSS_C_NO_CHANNEL_BINDINGS,
139                                            &input_token,
140                                            &output_token,
141                                            mutual_auth,
142                                            NULL);
143 
144   if(GSS_ERROR(major_status)) {
145     if(output_token.value)
146       gss_release_buffer(&unused_status, &output_token);
147 
148     Curl_gss_log_error(data, "gss_init_sec_context() failed: ",
149                        major_status, minor_status);
150 
151     return CURLE_AUTH_ERROR;
152   }
153 
154   if(output_token.value && output_token.length) {
155     result = Curl_bufref_memdup(out, output_token.value, output_token.length);
156     gss_release_buffer(&unused_status, &output_token);
157   }
158   else
159     Curl_bufref_set(out, mutual_auth? "": NULL, 0, NULL);
160 
161   return result;
162 }
163 
164 /*
165  * Curl_auth_create_gssapi_security_message()
166  *
167  * This is used to generate an already encoded GSSAPI (Kerberos V5) security
168  * token message ready for sending to the recipient.
169  *
170  * Parameters:
171  *
172  * data    [in]     - The session handle.
173  * chlg    [in]     - Optional challenge message.
174  * krb5    [in/out] - The Kerberos 5 data struct being used and modified.
175  * out     [out]    - The result storage.
176  *
177  * Returns CURLE_OK on success.
178  */
Curl_auth_create_gssapi_security_message(struct Curl_easy * data,const struct bufref * chlg,struct kerberos5data * krb5,struct bufref * out)179 CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
180                                                   const struct bufref *chlg,
181                                                   struct kerberos5data *krb5,
182                                                   struct bufref *out)
183 {
184   CURLcode result = CURLE_OK;
185   size_t messagelen = 0;
186   unsigned char *message = NULL;
187   OM_uint32 major_status;
188   OM_uint32 minor_status;
189   OM_uint32 unused_status;
190   gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
191   gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
192   unsigned int indata = 0;
193   unsigned int outdata = 0;
194   gss_qop_t qop = GSS_C_QOP_DEFAULT;
195   unsigned int sec_layer = 0;
196   unsigned int max_size = 0;
197   gss_name_t username = GSS_C_NO_NAME;
198   gss_buffer_desc username_token;
199 
200   /* Ensure we have a valid challenge message */
201   if(!Curl_bufref_len(chlg)) {
202     infof(data, "GSSAPI handshake failure (empty security message)");
203     return CURLE_BAD_CONTENT_ENCODING;
204   }
205 
206   /* Get the fully qualified username back from the context */
207   major_status = gss_inquire_context(&minor_status, krb5->context,
208                                      &username, NULL, NULL, NULL, NULL,
209                                      NULL, NULL);
210   if(GSS_ERROR(major_status)) {
211     Curl_gss_log_error(data, "gss_inquire_context() failed: ",
212                        major_status, minor_status);
213     return CURLE_AUTH_ERROR;
214   }
215 
216   /* Convert the username from internal format to a displayable token */
217   major_status = gss_display_name(&minor_status, username,
218                                   &username_token, NULL);
219   if(GSS_ERROR(major_status)) {
220     Curl_gss_log_error(data, "gss_display_name() failed: ",
221                        major_status, minor_status);
222     return CURLE_AUTH_ERROR;
223   }
224 
225   /* Setup the challenge "input" security buffer */
226   input_token.value = (void *) Curl_bufref_ptr(chlg);
227   input_token.length = Curl_bufref_len(chlg);
228 
229   /* Decrypt the inbound challenge and obtain the qop */
230   major_status = gss_unwrap(&minor_status, krb5->context, &input_token,
231                             &output_token, NULL, &qop);
232   if(GSS_ERROR(major_status)) {
233     Curl_gss_log_error(data, "gss_unwrap() failed: ",
234                        major_status, minor_status);
235     gss_release_buffer(&unused_status, &username_token);
236     return CURLE_BAD_CONTENT_ENCODING;
237   }
238 
239   /* Not 4 octets long so fail as per RFC4752 Section 3.1 */
240   if(output_token.length != 4) {
241     infof(data, "GSSAPI handshake failure (invalid security data)");
242     gss_release_buffer(&unused_status, &username_token);
243     return CURLE_BAD_CONTENT_ENCODING;
244   }
245 
246   /* Copy the data out and free the challenge as it is not required anymore */
247   memcpy(&indata, output_token.value, 4);
248   gss_release_buffer(&unused_status, &output_token);
249 
250   /* Extract the security layer */
251   sec_layer = indata & 0x000000FF;
252   if(!(sec_layer & GSSAUTH_P_NONE)) {
253     infof(data, "GSSAPI handshake failure (invalid security layer)");
254 
255     gss_release_buffer(&unused_status, &username_token);
256     return CURLE_BAD_CONTENT_ENCODING;
257   }
258 
259   /* Extract the maximum message size the server can receive */
260   max_size = ntohl(indata & 0xFFFFFF00);
261   if(max_size > 0) {
262     /* The server has told us it supports a maximum receive buffer, however, as
263        we don't require one unless we are encrypting data, we tell the server
264        our receive buffer is zero. */
265     max_size = 0;
266   }
267 
268   /* Allocate our message */
269   messagelen = sizeof(outdata) + username_token.length + 1;
270   message = malloc(messagelen);
271   if(!message) {
272     gss_release_buffer(&unused_status, &username_token);
273     return CURLE_OUT_OF_MEMORY;
274   }
275 
276   /* Populate the message with the security layer, client supported receive
277      message size and authorization identity including the 0x00 based
278      terminator. Note: Despite RFC4752 Section 3.1 stating "The authorization
279      identity is not terminated with the zero-valued (%x00) octet." it seems
280      necessary to include it. */
281   outdata = htonl(max_size) | sec_layer;
282   memcpy(message, &outdata, sizeof(outdata));
283   memcpy(message + sizeof(outdata), username_token.value,
284          username_token.length);
285   message[messagelen - 1] = '\0';
286 
287   /* Free the username token as it is not required anymore */
288   gss_release_buffer(&unused_status, &username_token);
289 
290   /* Setup the "authentication data" security buffer */
291   input_token.value = message;
292   input_token.length = messagelen;
293 
294   /* Encrypt the data */
295   major_status = gss_wrap(&minor_status, krb5->context, 0,
296                           GSS_C_QOP_DEFAULT, &input_token, NULL,
297                           &output_token);
298   if(GSS_ERROR(major_status)) {
299     Curl_gss_log_error(data, "gss_wrap() failed: ",
300                        major_status, minor_status);
301     free(message);
302     return CURLE_AUTH_ERROR;
303   }
304 
305   /* Return the response. */
306   result = Curl_bufref_memdup(out, output_token.value, output_token.length);
307   /* Free the output buffer */
308   gss_release_buffer(&unused_status, &output_token);
309 
310   /* Free the message buffer */
311   free(message);
312 
313   return result;
314 }
315 
316 /*
317  * Curl_auth_cleanup_gssapi()
318  *
319  * This is used to clean up the GSSAPI (Kerberos V5) specific data.
320  *
321  * Parameters:
322  *
323  * krb5     [in/out] - The Kerberos 5 data struct being cleaned up.
324  *
325  */
Curl_auth_cleanup_gssapi(struct kerberos5data * krb5)326 void Curl_auth_cleanup_gssapi(struct kerberos5data *krb5)
327 {
328   OM_uint32 minor_status;
329 
330   /* Free our security context */
331   if(krb5->context != GSS_C_NO_CONTEXT) {
332     gss_delete_sec_context(&minor_status, &krb5->context, GSS_C_NO_BUFFER);
333     krb5->context = GSS_C_NO_CONTEXT;
334   }
335 
336   /* Free the SPN */
337   if(krb5->spn != GSS_C_NO_NAME) {
338     gss_release_name(&minor_status, &krb5->spn);
339     krb5->spn = GSS_C_NO_NAME;
340   }
341 }
342 
343 #endif /* HAVE_GSSAPI && USE_KERBEROS5 */
344