tizen 2.4 release
[external/curl.git] / lib / curl_sasl_gssapi.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 2014 - 2015, Steve Holme, <steve_holme@hotmail.com>.
9  * Copyright (C) 2015, 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 http://curl.haxx.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 "curl_sasl.h"
33 #include "urldata.h"
34 #include "curl_base64.h"
35 #include "curl_gssapi.h"
36 #include "curl_memory.h"
37 #include "sendf.h"
38
39 #define _MPRINTF_REPLACE /* use our functions only */
40 #include <curl/mprintf.h>
41
42 /* The last #include file should be: */
43 #include "memdebug.h"
44
45 /*
46 * Curl_sasl_build_gssapi_spn()
47 *
48 * This is used to build a SPN string in the format service@host.
49 *
50 * Parameters:
51 *
52 * serivce  [in] - The service type such as www, smtp, pop or imap.
53 * host     [in] - The host name or realm.
54 *
55 * Returns a pointer to the newly allocated SPN.
56 */
57 static char *Curl_sasl_build_gssapi_spn(const char *service, const char *host)
58 {
59   /* Generate and return our SPN */
60   return aprintf("%s@%s", service, host);
61 }
62
63 /*
64  * Curl_sasl_create_gssapi_user_message()
65  *
66  * This is used to generate an already encoded GSSAPI (Kerberos V5) user token
67  * message ready for sending to the recipient.
68  *
69  * Parameters:
70  *
71  * data        [in]     - The session handle.
72  * userp       [in]     - The user name.
73  * passdwp     [in]     - The user's password.
74  * service     [in]     - The service type such as www, smtp, pop or imap.
75  * mutual_auth [in]     - Flag specifing whether or not mutual authentication
76  *                        is enabled.
77  * chlg64      [in]     - Pointer to the optional base64 encoded challenge
78  *                        message.
79  * krb5        [in/out] - The gssapi data struct being used and modified.
80  * outptr      [in/out] - The address where a pointer to newly allocated memory
81  *                        holding the result will be stored upon completion.
82  * outlen      [out]    - The length of the output message.
83  *
84  * Returns CURLE_OK on success.
85  */
86 CURLcode Curl_sasl_create_gssapi_user_message(struct SessionHandle *data,
87                                               const char *userp,
88                                               const char *passwdp,
89                                               const char *service,
90                                               const bool mutual_auth,
91                                               const char *chlg64,
92                                               struct kerberos5data *krb5,
93                                               char **outptr, size_t *outlen)
94 {
95   CURLcode result = CURLE_OK;
96   size_t chlglen = 0;
97   unsigned char *chlg = NULL;
98   OM_uint32 gss_status;
99   OM_uint32 gss_major_status;
100   OM_uint32 gss_minor_status;
101   gss_buffer_desc spn_token = GSS_C_EMPTY_BUFFER;
102   gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
103   gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
104
105   (void) userp;
106   (void) passwdp;
107
108   if(krb5->context == GSS_C_NO_CONTEXT) {
109     /* Generate our SPN */
110     char *spn = Curl_sasl_build_gssapi_spn(service,
111                                            data->easy_conn->host.name);
112     if(!spn)
113       return CURLE_OUT_OF_MEMORY;
114
115     /* Populate the SPN structure */
116     spn_token.value = spn;
117     spn_token.length = strlen(spn);
118
119     /* Import the SPN */
120     gss_major_status = gss_import_name(&gss_minor_status, &spn_token,
121                                        GSS_C_NT_HOSTBASED_SERVICE, &krb5->spn);
122     if(GSS_ERROR(gss_major_status)) {
123       Curl_gss_log_error(data, gss_minor_status, "gss_import_name() failed: ");
124
125       return CURLE_OUT_OF_MEMORY;
126     }
127   }
128   else {
129     /* Decode the base-64 encoded challenge message */
130     if(strlen(chlg64) && *chlg64 != '=') {
131       result = Curl_base64_decode(chlg64, &chlg, &chlglen);
132       if(result)
133         return result;
134     }
135
136     /* Ensure we have a valid challenge message */
137     if(!chlg) {
138       infof(data, "GSSAPI handshake failure (empty challenge message)\n");
139
140       return CURLE_BAD_CONTENT_ENCODING;
141     }
142
143     /* Setup the challenge "input" security buffer */
144     input_token.value = chlg;
145     input_token.length = chlglen;
146   }
147
148   gss_major_status = Curl_gss_init_sec_context(data,
149                                                &gss_minor_status,
150                                                &krb5->context,
151                                                krb5->spn,
152                                                &Curl_krb5_mech_oid,
153                                                GSS_C_NO_CHANNEL_BINDINGS,
154                                                &input_token,
155                                                &output_token,
156                                                mutual_auth,
157                                                NULL);
158
159   Curl_safefree(input_token.value);
160
161   if(GSS_ERROR(gss_major_status)) {
162     if(output_token.value)
163       gss_release_buffer(&gss_status, &output_token);
164
165     Curl_gss_log_error(data, gss_minor_status,
166                        "gss_init_sec_context() failed: ");
167
168     return CURLE_RECV_ERROR;
169   }
170
171   if(output_token.value && output_token.length) {
172     /* Base64 encode the response */
173     result = Curl_base64_encode(data, (char *) output_token.value,
174                                 output_token.length, outptr, outlen);
175
176     gss_release_buffer(&gss_status, &output_token);
177   }
178
179   return result;
180 }
181
182 /*
183  * Curl_sasl_create_gssapi_security_message()
184  *
185  * This is used to generate an already encoded GSSAPI (Kerberos V5) security
186  * token message ready for sending to the recipient.
187  *
188  * Parameters:
189  *
190  * data    [in]     - The session handle.
191  * chlg64  [in]     - Pointer to the optional base64 encoded challenge message.
192  * krb5    [in/out] - The gssapi data struct being used and modified.
193  * outptr  [in/out] - The address where a pointer to newly allocated memory
194  *                    holding the result will be stored upon completion.
195  * outlen  [out]    - The length of the output message.
196  *
197  * Returns CURLE_OK on success.
198  */
199 CURLcode Curl_sasl_create_gssapi_security_message(struct SessionHandle *data,
200                                                   const char *chlg64,
201                                                   struct kerberos5data *krb5,
202                                                   char **outptr,
203                                                   size_t *outlen)
204 {
205   CURLcode result = CURLE_OK;
206   size_t chlglen = 0;
207   size_t messagelen = 0;
208   unsigned char *chlg = NULL;
209   unsigned char *message = NULL;
210   OM_uint32 gss_status;
211   OM_uint32 gss_major_status;
212   OM_uint32 gss_minor_status;
213   gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
214   gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
215   unsigned int indata = 0;
216   unsigned int outdata = 0;
217   gss_qop_t qop = GSS_C_QOP_DEFAULT;
218   unsigned int sec_layer = 0;
219   unsigned int max_size = 0;
220   gss_name_t username = GSS_C_NO_NAME;
221   gss_buffer_desc username_token;
222
223   /* Decode the base-64 encoded input message */
224   if(strlen(chlg64) && *chlg64 != '=') {
225     result = Curl_base64_decode(chlg64, &chlg, &chlglen);
226     if(result)
227       return result;
228   }
229
230   /* Ensure we have a valid challenge message */
231   if(!chlg) {
232     infof(data, "GSSAPI handshake failure (empty security message)\n");
233
234     return CURLE_BAD_CONTENT_ENCODING;
235   }
236
237   /* Get the fully qualified username back from the context */
238   gss_major_status = gss_inquire_context(&gss_minor_status, krb5->context,
239                                          &username, NULL, NULL, NULL, NULL,
240                                          NULL, NULL);
241   if(GSS_ERROR(gss_major_status)) {
242     Curl_gss_log_error(data, gss_minor_status,
243                        "gss_inquire_context() failed: ");
244
245     Curl_safefree(chlg);
246
247     return CURLE_OUT_OF_MEMORY;
248   }
249
250   /* Convert the username from internal format to a displayable token */
251   gss_major_status = gss_display_name(&gss_minor_status, username,
252                                       &username_token, NULL);
253   if(GSS_ERROR(gss_major_status)) {
254     Curl_gss_log_error(data, gss_minor_status, "gss_display_name() failed: ");
255
256     Curl_safefree(chlg);
257
258     return CURLE_OUT_OF_MEMORY;
259   }
260
261   /* Setup the challenge "input" security buffer */
262   input_token.value = chlg;
263   input_token.length = chlglen;
264
265   /* Decrypt the inbound challenge and obtain the qop */
266   gss_major_status = gss_unwrap(&gss_minor_status, krb5->context, &input_token,
267                                 &output_token, NULL, &qop);
268   if(GSS_ERROR(gss_major_status)) {
269     Curl_gss_log_error(data, gss_minor_status, "gss_unwrap() failed: ");
270
271     gss_release_buffer(&gss_status, &username_token);
272     Curl_safefree(chlg);
273
274     return CURLE_BAD_CONTENT_ENCODING;
275   }
276
277   /* Not 4 octets long so fail as per RFC4752 Section 3.1 */
278   if(output_token.length != 4) {
279     infof(data, "GSSAPI handshake failure (invalid security data)\n");
280
281     gss_release_buffer(&gss_status, &username_token);
282     Curl_safefree(chlg);
283
284     return CURLE_BAD_CONTENT_ENCODING;
285   }
286
287   /* Copy the data out and free the challenge as it is not required anymore */
288   memcpy(&indata, output_token.value, 4);
289   gss_release_buffer(&gss_status, &output_token);
290   Curl_safefree(chlg);
291
292   /* Extract the security layer */
293   sec_layer = indata & 0x000000FF;
294   if(!(sec_layer & GSSAUTH_P_NONE)) {
295     infof(data, "GSSAPI handshake failure (invalid security layer)\n");
296
297     gss_release_buffer(&gss_status, &username_token);
298
299     return CURLE_BAD_CONTENT_ENCODING;
300   }
301
302   /* Extract the maximum message size the server can receive */
303   max_size = ntohl(indata & 0xFFFFFF00);
304   if(max_size > 0) {
305     /* The server has told us it supports a maximum receive buffer, however, as
306        we don't require one unless we are encrypting data, we tell the server
307        our receive buffer is zero. */
308     max_size = 0;
309   }
310
311   /* Allocate our message */
312   messagelen = sizeof(outdata) + username_token.length + 1;
313   message = malloc(messagelen);
314   if(!message) {
315     gss_release_buffer(&gss_status, &username_token);
316
317     return CURLE_OUT_OF_MEMORY;
318   }
319
320   /* Populate the message with the security layer, client supported receive
321      message size and authorization identity including the 0x00 based
322      terminator. Note: Dispite RFC4752 Section 3.1 stating "The authorization
323      identity is not terminated with the zero-valued (%x00) octet." it seems
324      necessary to include it. */
325   outdata = htonl(max_size) | sec_layer;
326   memcpy(message, &outdata, sizeof(outdata));
327   memcpy(message + sizeof(outdata), username_token.value,
328          username_token.length);
329   message[messagelen - 1] = '\0';
330
331   /* Free the username token as it is not required anymore */
332   gss_release_buffer(&gss_status, &username_token);
333
334   /* Setup the "authentication data" security buffer */
335   input_token.value = message;
336   input_token.length = messagelen;
337
338   /* Encrypt the data */
339   gss_major_status = gss_wrap(&gss_minor_status, krb5->context, 0,
340                               GSS_C_QOP_DEFAULT, &input_token, NULL,
341                               &output_token);
342   if(GSS_ERROR(gss_major_status)) {
343     Curl_gss_log_error(data, gss_minor_status, "gss_wrap() failed: ");
344
345     Curl_safefree(message);
346
347     return CURLE_OUT_OF_MEMORY;
348   }
349
350   /* Base64 encode the response */
351   result = Curl_base64_encode(data, (char *) output_token.value,
352                               output_token.length, outptr, outlen);
353
354   /* Free the output buffer */
355   gss_release_buffer(&gss_status, &output_token);
356
357   /* Free the message buffer */
358   Curl_safefree(message);
359
360   return result;
361 }
362
363 /*
364  * Curl_sasl_gssapi_cleanup()
365  *
366  * This is used to clean up the gssapi specific data.
367  *
368  * Parameters:
369  *
370  * krb5     [in/out] - The kerberos 5 data struct being cleaned up.
371  *
372  */
373 void Curl_sasl_gssapi_cleanup(struct kerberos5data *krb5)
374 {
375   OM_uint32 minor_status;
376
377   /* Free our security context */
378   if(krb5->context != GSS_C_NO_CONTEXT) {
379     gss_delete_sec_context(&minor_status, &krb5->context, GSS_C_NO_BUFFER);
380     krb5->context = GSS_C_NO_CONTEXT;
381   }
382
383   /* Free the SPN */
384   if(krb5->spn != GSS_C_NO_NAME) {
385     gss_release_name(&minor_status, &krb5->spn);
386     krb5->spn = GSS_C_NO_NAME;
387   }
388 }
389
390 #endif /* HAVE_GSSAPI && USE_KERBEROS5 */