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