Home | History | Annotate | Download | only in lib
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 2009, 2011, Markus Moeller, <markus_moeller (at) compuserve.com>
      9  * Copyright (C) 2012 - 2016, Daniel Stenberg, <daniel (at) 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.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  ***************************************************************************/
     23 
     24 #include "curl_setup.h"
     25 
     26 #if defined(HAVE_GSSAPI) && !defined(CURL_DISABLE_PROXY)
     27 
     28 #include "curl_gssapi.h"
     29 #include "urldata.h"
     30 #include "sendf.h"
     31 #include "connect.h"
     32 #include "timeval.h"
     33 #include "socks.h"
     34 #include "warnless.h"
     35 
     36 /* The last 3 #include files should be in this order */
     37 #include "curl_printf.h"
     38 #include "curl_memory.h"
     39 #include "memdebug.h"
     40 
     41 static gss_ctx_id_t gss_context = GSS_C_NO_CONTEXT;
     42 
     43 /*
     44  * Helper GSS-API error functions.
     45  */
     46 static int check_gss_err(struct Curl_easy *data,
     47                          OM_uint32 major_status,
     48                          OM_uint32 minor_status,
     49                          const char* function)
     50 {
     51   if(GSS_ERROR(major_status)) {
     52     OM_uint32 maj_stat, min_stat;
     53     OM_uint32 msg_ctx = 0;
     54     gss_buffer_desc status_string;
     55     char buf[1024];
     56     size_t len;
     57 
     58     len = 0;
     59     msg_ctx = 0;
     60     while(!msg_ctx) {
     61       /* convert major status code (GSS-API error) to text */
     62       maj_stat = gss_display_status(&min_stat, major_status,
     63                                     GSS_C_GSS_CODE,
     64                                     GSS_C_NULL_OID,
     65                                     &msg_ctx, &status_string);
     66       if(maj_stat == GSS_S_COMPLETE) {
     67         if(sizeof(buf) > len + status_string.length + 1) {
     68           strcpy(buf+len, (char*) status_string.value);
     69           len += status_string.length;
     70         }
     71         gss_release_buffer(&min_stat, &status_string);
     72         break;
     73       }
     74       gss_release_buffer(&min_stat, &status_string);
     75     }
     76     if(sizeof(buf) > len + 3) {
     77       strcpy(buf+len, ".\n");
     78       len += 2;
     79     }
     80     msg_ctx = 0;
     81     while(!msg_ctx) {
     82       /* convert minor status code (underlying routine error) to text */
     83       maj_stat = gss_display_status(&min_stat, minor_status,
     84                                     GSS_C_MECH_CODE,
     85                                     GSS_C_NULL_OID,
     86                                     &msg_ctx, &status_string);
     87       if(maj_stat == GSS_S_COMPLETE) {
     88         if(sizeof(buf) > len + status_string.length)
     89           strcpy(buf+len, (char*) status_string.value);
     90         gss_release_buffer(&min_stat, &status_string);
     91         break;
     92       }
     93       gss_release_buffer(&min_stat, &status_string);
     94     }
     95     failf(data, "GSS-API error: %s failed:\n%s", function, buf);
     96     return 1;
     97   }
     98 
     99   return 0;
    100 }
    101 
    102 CURLcode Curl_SOCKS5_gssapi_negotiate(int sockindex,
    103                                       struct connectdata *conn)
    104 {
    105   struct Curl_easy *data = conn->data;
    106   curl_socket_t sock = conn->sock[sockindex];
    107   CURLcode code;
    108   ssize_t actualread;
    109   ssize_t written;
    110   int result;
    111   OM_uint32 gss_major_status, gss_minor_status, gss_status;
    112   OM_uint32 gss_ret_flags;
    113   int gss_conf_state, gss_enc;
    114   gss_buffer_desc  service = GSS_C_EMPTY_BUFFER;
    115   gss_buffer_desc  gss_send_token = GSS_C_EMPTY_BUFFER;
    116   gss_buffer_desc  gss_recv_token = GSS_C_EMPTY_BUFFER;
    117   gss_buffer_desc  gss_w_token = GSS_C_EMPTY_BUFFER;
    118   gss_buffer_desc* gss_token = GSS_C_NO_BUFFER;
    119   gss_name_t       server = GSS_C_NO_NAME;
    120   gss_name_t       gss_client_name = GSS_C_NO_NAME;
    121   unsigned short   us_length;
    122   char             *user=NULL;
    123   unsigned char socksreq[4]; /* room for GSS-API exchange header only */
    124   const char *serviceptr = data->set.str[STRING_PROXY_SERVICE_NAME] ?
    125                            data->set.str[STRING_PROXY_SERVICE_NAME] : "rcmd";
    126 
    127   /*   GSS-API request looks like
    128    * +----+------+-----+----------------+
    129    * |VER | MTYP | LEN |     TOKEN      |
    130    * +----+------+----------------------+
    131    * | 1  |  1   |  2  | up to 2^16 - 1 |
    132    * +----+------+-----+----------------+
    133    */
    134 
    135   /* prepare service name */
    136   if(strchr(serviceptr, '/')) {
    137     service.value = malloc(strlen(serviceptr));
    138     if(!service.value)
    139       return CURLE_OUT_OF_MEMORY;
    140     service.length = strlen(serviceptr);
    141     memcpy(service.value, serviceptr, service.length);
    142 
    143     gss_major_status = gss_import_name(&gss_minor_status, &service,
    144                                        (gss_OID) GSS_C_NULL_OID, &server);
    145   }
    146   else {
    147     service.value = malloc(strlen(serviceptr) +strlen(conn->proxy.name)+2);
    148     if(!service.value)
    149       return CURLE_OUT_OF_MEMORY;
    150     service.length = strlen(serviceptr) +strlen(conn->proxy.name)+1;
    151     snprintf(service.value, service.length+1, "%s@%s",
    152              serviceptr, conn->proxy.name);
    153 
    154     gss_major_status = gss_import_name(&gss_minor_status, &service,
    155                                        GSS_C_NT_HOSTBASED_SERVICE, &server);
    156   }
    157 
    158   gss_release_buffer(&gss_status, &service); /* clear allocated memory */
    159 
    160   if(check_gss_err(data, gss_major_status,
    161                    gss_minor_status, "gss_import_name()")) {
    162     failf(data, "Failed to create service name.");
    163     gss_release_name(&gss_status, &server);
    164     return CURLE_COULDNT_CONNECT;
    165   }
    166 
    167   /* As long as we need to keep sending some context info, and there's no  */
    168   /* errors, keep sending it...                                            */
    169   for(;;) {
    170     gss_major_status = Curl_gss_init_sec_context(data,
    171                                                  &gss_minor_status,
    172                                                  &gss_context,
    173                                                  server,
    174                                                  &Curl_krb5_mech_oid,
    175                                                  NULL,
    176                                                  gss_token,
    177                                                  &gss_send_token,
    178                                                  TRUE,
    179                                                  &gss_ret_flags);
    180 
    181     if(gss_token != GSS_C_NO_BUFFER)
    182       gss_release_buffer(&gss_status, &gss_recv_token);
    183     if(check_gss_err(data, gss_major_status,
    184                      gss_minor_status, "gss_init_sec_context")) {
    185       gss_release_name(&gss_status, &server);
    186       gss_release_buffer(&gss_status, &gss_recv_token);
    187       gss_release_buffer(&gss_status, &gss_send_token);
    188       gss_delete_sec_context(&gss_status, &gss_context, NULL);
    189       failf(data, "Failed to initial GSS-API token.");
    190       return CURLE_COULDNT_CONNECT;
    191     }
    192 
    193     if(gss_send_token.length != 0) {
    194       socksreq[0] = 1;    /* GSS-API subnegotiation version */
    195       socksreq[1] = 1;    /* authentication message type */
    196       us_length = htons((short)gss_send_token.length);
    197       memcpy(socksreq+2, &us_length, sizeof(short));
    198 
    199       code = Curl_write_plain(conn, sock, (char *)socksreq, 4, &written);
    200       if(code || (4 != written)) {
    201         failf(data, "Failed to send GSS-API authentication request.");
    202         gss_release_name(&gss_status, &server);
    203         gss_release_buffer(&gss_status, &gss_recv_token);
    204         gss_release_buffer(&gss_status, &gss_send_token);
    205         gss_delete_sec_context(&gss_status, &gss_context, NULL);
    206         return CURLE_COULDNT_CONNECT;
    207       }
    208 
    209       code = Curl_write_plain(conn, sock, (char *)gss_send_token.value,
    210                               gss_send_token.length, &written);
    211 
    212       if(code || ((ssize_t)gss_send_token.length != written)) {
    213         failf(data, "Failed to send GSS-API authentication token.");
    214         gss_release_name(&gss_status, &server);
    215         gss_release_buffer(&gss_status, &gss_recv_token);
    216         gss_release_buffer(&gss_status, &gss_send_token);
    217         gss_delete_sec_context(&gss_status, &gss_context, NULL);
    218         return CURLE_COULDNT_CONNECT;
    219       }
    220 
    221     }
    222 
    223     gss_release_buffer(&gss_status, &gss_send_token);
    224     gss_release_buffer(&gss_status, &gss_recv_token);
    225     if(gss_major_status != GSS_S_CONTINUE_NEEDED) break;
    226 
    227     /* analyse response */
    228 
    229     /*   GSS-API response looks like
    230      * +----+------+-----+----------------+
    231      * |VER | MTYP | LEN |     TOKEN      |
    232      * +----+------+----------------------+
    233      * | 1  |  1   |  2  | up to 2^16 - 1 |
    234      * +----+------+-----+----------------+
    235      */
    236 
    237     result=Curl_blockread_all(conn, sock, (char *)socksreq, 4, &actualread);
    238     if(result || (actualread != 4)) {
    239       failf(data, "Failed to receive GSS-API authentication response.");
    240       gss_release_name(&gss_status, &server);
    241       gss_delete_sec_context(&gss_status, &gss_context, NULL);
    242       return CURLE_COULDNT_CONNECT;
    243     }
    244 
    245     /* ignore the first (VER) byte */
    246     if(socksreq[1] == 255) { /* status / message type */
    247       failf(data, "User was rejected by the SOCKS5 server (%d %d).",
    248             socksreq[0], socksreq[1]);
    249       gss_release_name(&gss_status, &server);
    250       gss_delete_sec_context(&gss_status, &gss_context, NULL);
    251       return CURLE_COULDNT_CONNECT;
    252     }
    253 
    254     if(socksreq[1] != 1) { /* status / messgae type */
    255       failf(data, "Invalid GSS-API authentication response type (%d %d).",
    256             socksreq[0], socksreq[1]);
    257       gss_release_name(&gss_status, &server);
    258       gss_delete_sec_context(&gss_status, &gss_context, NULL);
    259       return CURLE_COULDNT_CONNECT;
    260     }
    261 
    262     memcpy(&us_length, socksreq+2, sizeof(short));
    263     us_length = ntohs(us_length);
    264 
    265     gss_recv_token.length=us_length;
    266     gss_recv_token.value=malloc(us_length);
    267     if(!gss_recv_token.value) {
    268       failf(data,
    269             "Could not allocate memory for GSS-API authentication "
    270             "response token.");
    271       gss_release_name(&gss_status, &server);
    272       gss_delete_sec_context(&gss_status, &gss_context, NULL);
    273       return CURLE_OUT_OF_MEMORY;
    274     }
    275 
    276     result=Curl_blockread_all(conn, sock, (char *)gss_recv_token.value,
    277                               gss_recv_token.length, &actualread);
    278 
    279     if(result || (actualread != us_length)) {
    280       failf(data, "Failed to receive GSS-API authentication token.");
    281       gss_release_name(&gss_status, &server);
    282       gss_release_buffer(&gss_status, &gss_recv_token);
    283       gss_delete_sec_context(&gss_status, &gss_context, NULL);
    284       return CURLE_COULDNT_CONNECT;
    285     }
    286 
    287     gss_token = &gss_recv_token;
    288   }
    289 
    290   gss_release_name(&gss_status, &server);
    291 
    292   /* Everything is good so far, user was authenticated! */
    293   gss_major_status = gss_inquire_context (&gss_minor_status, gss_context,
    294                                           &gss_client_name, NULL, NULL, NULL,
    295                                           NULL, NULL, NULL);
    296   if(check_gss_err(data, gss_major_status,
    297                    gss_minor_status, "gss_inquire_context")) {
    298     gss_delete_sec_context(&gss_status, &gss_context, NULL);
    299     gss_release_name(&gss_status, &gss_client_name);
    300     failf(data, "Failed to determine user name.");
    301     return CURLE_COULDNT_CONNECT;
    302   }
    303   gss_major_status = gss_display_name(&gss_minor_status, gss_client_name,
    304                                       &gss_send_token, NULL);
    305   if(check_gss_err(data, gss_major_status,
    306                    gss_minor_status, "gss_display_name")) {
    307     gss_delete_sec_context(&gss_status, &gss_context, NULL);
    308     gss_release_name(&gss_status, &gss_client_name);
    309     gss_release_buffer(&gss_status, &gss_send_token);
    310     failf(data, "Failed to determine user name.");
    311     return CURLE_COULDNT_CONNECT;
    312   }
    313   user=malloc(gss_send_token.length+1);
    314   if(!user) {
    315     gss_delete_sec_context(&gss_status, &gss_context, NULL);
    316     gss_release_name(&gss_status, &gss_client_name);
    317     gss_release_buffer(&gss_status, &gss_send_token);
    318     return CURLE_OUT_OF_MEMORY;
    319   }
    320 
    321   memcpy(user, gss_send_token.value, gss_send_token.length);
    322   user[gss_send_token.length] = '\0';
    323   gss_release_name(&gss_status, &gss_client_name);
    324   gss_release_buffer(&gss_status, &gss_send_token);
    325   infof(data, "SOCKS5 server authencticated user %s with GSS-API.\n",user);
    326   free(user);
    327   user=NULL;
    328 
    329   /* Do encryption */
    330   socksreq[0] = 1;    /* GSS-API subnegotiation version */
    331   socksreq[1] = 2;    /* encryption message type */
    332 
    333   gss_enc = 0; /* no data protection */
    334   /* do confidentiality protection if supported */
    335   if(gss_ret_flags & GSS_C_CONF_FLAG)
    336     gss_enc = 2;
    337   /* else do integrity protection */
    338   else if(gss_ret_flags & GSS_C_INTEG_FLAG)
    339     gss_enc = 1;
    340 
    341   infof(data, "SOCKS5 server supports GSS-API %s data protection.\n",
    342         (gss_enc==0)?"no":((gss_enc==1)?"integrity":"confidentiality"));
    343   /* force for the moment to no data protection */
    344   gss_enc = 0;
    345   /*
    346    * Sending the encryption type in clear seems wrong. It should be
    347    * protected with gss_seal()/gss_wrap(). See RFC1961 extract below
    348    * The NEC reference implementations on which this is based is
    349    * therefore at fault
    350    *
    351    *  +------+------+------+.......................+
    352    *  + ver  | mtyp | len  |   token               |
    353    *  +------+------+------+.......................+
    354    *  + 0x01 | 0x02 | 0x02 | up to 2^16 - 1 octets |
    355    *  +------+------+------+.......................+
    356    *
    357    *   Where:
    358    *
    359    *  - "ver" is the protocol version number, here 1 to represent the
    360    *    first version of the SOCKS/GSS-API protocol
    361    *
    362    *  - "mtyp" is the message type, here 2 to represent a protection
    363    *    -level negotiation message
    364    *
    365    *  - "len" is the length of the "token" field in octets
    366    *
    367    *  - "token" is the GSS-API encapsulated protection level
    368    *
    369    * The token is produced by encapsulating an octet containing the
    370    * required protection level using gss_seal()/gss_wrap() with conf_req
    371    * set to FALSE.  The token is verified using gss_unseal()/
    372    * gss_unwrap().
    373    *
    374    */
    375   if(data->set.socks5_gssapi_nec) {
    376     us_length = htons((short)1);
    377     memcpy(socksreq+2, &us_length, sizeof(short));
    378   }
    379   else {
    380     gss_send_token.length = 1;
    381     gss_send_token.value = malloc(1);
    382     if(!gss_send_token.value) {
    383       gss_delete_sec_context(&gss_status, &gss_context, NULL);
    384       return CURLE_OUT_OF_MEMORY;
    385     }
    386     memcpy(gss_send_token.value, &gss_enc, 1);
    387 
    388     gss_major_status = gss_wrap(&gss_minor_status, gss_context, 0,
    389                                 GSS_C_QOP_DEFAULT, &gss_send_token,
    390                                 &gss_conf_state, &gss_w_token);
    391 
    392     if(check_gss_err(data, gss_major_status, gss_minor_status, "gss_wrap")) {
    393       gss_release_buffer(&gss_status, &gss_send_token);
    394       gss_release_buffer(&gss_status, &gss_w_token);
    395       gss_delete_sec_context(&gss_status, &gss_context, NULL);
    396       failf(data, "Failed to wrap GSS-API encryption value into token.");
    397       return CURLE_COULDNT_CONNECT;
    398     }
    399     gss_release_buffer(&gss_status, &gss_send_token);
    400 
    401     us_length = htons((short)gss_w_token.length);
    402     memcpy(socksreq+2, &us_length, sizeof(short));
    403   }
    404 
    405   code = Curl_write_plain(conn, sock, (char *)socksreq, 4, &written);
    406   if(code  || (4 != written)) {
    407     failf(data, "Failed to send GSS-API encryption request.");
    408     gss_release_buffer(&gss_status, &gss_w_token);
    409     gss_delete_sec_context(&gss_status, &gss_context, NULL);
    410     return CURLE_COULDNT_CONNECT;
    411   }
    412 
    413   if(data->set.socks5_gssapi_nec) {
    414     memcpy(socksreq, &gss_enc, 1);
    415     code = Curl_write_plain(conn, sock, socksreq, 1, &written);
    416     if(code || ( 1 != written)) {
    417       failf(data, "Failed to send GSS-API encryption type.");
    418       gss_delete_sec_context(&gss_status, &gss_context, NULL);
    419       return CURLE_COULDNT_CONNECT;
    420     }
    421   }
    422   else {
    423     code = Curl_write_plain(conn, sock, (char *)gss_w_token.value,
    424                             gss_w_token.length, &written);
    425     if(code || ((ssize_t)gss_w_token.length != written)) {
    426       failf(data, "Failed to send GSS-API encryption type.");
    427       gss_release_buffer(&gss_status, &gss_w_token);
    428       gss_delete_sec_context(&gss_status, &gss_context, NULL);
    429       return CURLE_COULDNT_CONNECT;
    430     }
    431     gss_release_buffer(&gss_status, &gss_w_token);
    432   }
    433 
    434   result=Curl_blockread_all(conn, sock, (char *)socksreq, 4, &actualread);
    435   if(result || (actualread != 4)) {
    436     failf(data, "Failed to receive GSS-API encryption response.");
    437     gss_delete_sec_context(&gss_status, &gss_context, NULL);
    438     return CURLE_COULDNT_CONNECT;
    439   }
    440 
    441   /* ignore the first (VER) byte */
    442   if(socksreq[1] == 255) { /* status / message type */
    443     failf(data, "User was rejected by the SOCKS5 server (%d %d).",
    444           socksreq[0], socksreq[1]);
    445     gss_delete_sec_context(&gss_status, &gss_context, NULL);
    446     return CURLE_COULDNT_CONNECT;
    447   }
    448 
    449   if(socksreq[1] != 2) { /* status / messgae type */
    450     failf(data, "Invalid GSS-API encryption response type (%d %d).",
    451           socksreq[0], socksreq[1]);
    452     gss_delete_sec_context(&gss_status, &gss_context, NULL);
    453     return CURLE_COULDNT_CONNECT;
    454   }
    455 
    456   memcpy(&us_length, socksreq+2, sizeof(short));
    457   us_length = ntohs(us_length);
    458 
    459   gss_recv_token.length= us_length;
    460   gss_recv_token.value=malloc(gss_recv_token.length);
    461   if(!gss_recv_token.value) {
    462     gss_delete_sec_context(&gss_status, &gss_context, NULL);
    463     return CURLE_OUT_OF_MEMORY;
    464   }
    465   result=Curl_blockread_all(conn, sock, (char *)gss_recv_token.value,
    466                             gss_recv_token.length, &actualread);
    467 
    468   if(result || (actualread != us_length)) {
    469     failf(data, "Failed to receive GSS-API encryptrion type.");
    470     gss_release_buffer(&gss_status, &gss_recv_token);
    471     gss_delete_sec_context(&gss_status, &gss_context, NULL);
    472     return CURLE_COULDNT_CONNECT;
    473   }
    474 
    475   if(!data->set.socks5_gssapi_nec) {
    476     gss_major_status = gss_unwrap(&gss_minor_status, gss_context,
    477                                   &gss_recv_token, &gss_w_token,
    478                                   0, GSS_C_QOP_DEFAULT);
    479 
    480     if(check_gss_err(data, gss_major_status, gss_minor_status, "gss_unwrap")) {
    481       gss_release_buffer(&gss_status, &gss_recv_token);
    482       gss_release_buffer(&gss_status, &gss_w_token);
    483       gss_delete_sec_context(&gss_status, &gss_context, NULL);
    484       failf(data, "Failed to unwrap GSS-API encryption value into token.");
    485       return CURLE_COULDNT_CONNECT;
    486     }
    487     gss_release_buffer(&gss_status, &gss_recv_token);
    488 
    489     if(gss_w_token.length != 1) {
    490       failf(data, "Invalid GSS-API encryption response length (%d).",
    491             gss_w_token.length);
    492       gss_release_buffer(&gss_status, &gss_w_token);
    493       gss_delete_sec_context(&gss_status, &gss_context, NULL);
    494       return CURLE_COULDNT_CONNECT;
    495     }
    496 
    497     memcpy(socksreq, gss_w_token.value, gss_w_token.length);
    498     gss_release_buffer(&gss_status, &gss_w_token);
    499   }
    500   else {
    501     if(gss_recv_token.length != 1) {
    502       failf(data, "Invalid GSS-API encryption response length (%d).",
    503             gss_recv_token.length);
    504       gss_release_buffer(&gss_status, &gss_recv_token);
    505       gss_delete_sec_context(&gss_status, &gss_context, NULL);
    506       return CURLE_COULDNT_CONNECT;
    507     }
    508 
    509     memcpy(socksreq, gss_recv_token.value, gss_recv_token.length);
    510     gss_release_buffer(&gss_status, &gss_recv_token);
    511   }
    512 
    513   infof(data, "SOCKS5 access with%s protection granted.\n",
    514         (socksreq[0]==0)?"out GSS-API data":
    515         ((socksreq[0]==1)?" GSS-API integrity":" GSS-API confidentiality"));
    516 
    517   conn->socks5_gssapi_enctype = socksreq[0];
    518   if(socksreq[0] == 0)
    519     gss_delete_sec_context(&gss_status, &gss_context, NULL);
    520 
    521   return CURLE_OK;
    522 }
    523 
    524 #endif /* HAVE_GSSAPI && !CURL_DISABLE_PROXY */
    525