http_digest.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 
00023 #include "curl_setup.h"
00024 
00025 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_CRYPTO_AUTH)
00026 
00027 #include "urldata.h"
00028 #include "strcase.h"
00029 #include "vauth/vauth.h"
00030 #include "http_digest.h"
00031 /* The last 3 #include files should be in this order */
00032 #include "curl_printf.h"
00033 #include "curl_memory.h"
00034 #include "memdebug.h"
00035 
00036 /* Test example headers:
00037 
00038 WWW-Authenticate: Digest realm="testrealm", nonce="1053604598"
00039 Proxy-Authenticate: Digest realm="testrealm", nonce="1053604598"
00040 
00041 */
00042 
00043 CURLcode Curl_input_digest(struct connectdata *conn,
00044                            bool proxy,
00045                            const char *header) /* rest of the *-authenticate:
00046                                                   header */
00047 {
00048   struct Curl_easy *data = conn->data;
00049 
00050   /* Point to the correct struct with this */
00051   struct digestdata *digest;
00052 
00053   if(proxy) {
00054     digest = &data->state.proxydigest;
00055   }
00056   else {
00057     digest = &data->state.digest;
00058   }
00059 
00060   if(!checkprefix("Digest", header))
00061     return CURLE_BAD_CONTENT_ENCODING;
00062 
00063   header += strlen("Digest");
00064   while(*header && ISSPACE(*header))
00065     header++;
00066 
00067   return Curl_auth_decode_digest_http_message(header, digest);
00068 }
00069 
00070 CURLcode Curl_output_digest(struct connectdata *conn,
00071                             bool proxy,
00072                             const unsigned char *request,
00073                             const unsigned char *uripath)
00074 {
00075   CURLcode result;
00076   struct Curl_easy *data = conn->data;
00077   unsigned char *path = NULL;
00078   char *tmp = NULL;
00079   char *response;
00080   size_t len;
00081   bool have_chlg;
00082 
00083   /* Point to the address of the pointer that holds the string to send to the
00084      server, which is for a plain host or for a HTTP proxy */
00085   char **allocuserpwd;
00086 
00087   /* Point to the name and password for this */
00088   const char *userp;
00089   const char *passwdp;
00090 
00091   /* Point to the correct struct with this */
00092   struct digestdata *digest;
00093   struct auth *authp;
00094 
00095   if(proxy) {
00096     digest = &data->state.proxydigest;
00097     allocuserpwd = &conn->allocptr.proxyuserpwd;
00098     userp = conn->http_proxy.user;
00099     passwdp = conn->http_proxy.passwd;
00100     authp = &data->state.authproxy;
00101   }
00102   else {
00103     digest = &data->state.digest;
00104     allocuserpwd = &conn->allocptr.userpwd;
00105     userp = conn->user;
00106     passwdp = conn->passwd;
00107     authp = &data->state.authhost;
00108   }
00109 
00110   Curl_safefree(*allocuserpwd);
00111 
00112   /* not set means empty */
00113   if(!userp)
00114     userp = "";
00115 
00116   if(!passwdp)
00117     passwdp = "";
00118 
00119 #if defined(USE_WINDOWS_SSPI)
00120   have_chlg = digest->input_token ? TRUE : FALSE;
00121 #else
00122   have_chlg = digest->nonce ? TRUE : FALSE;
00123 #endif
00124 
00125   if(!have_chlg) {
00126     authp->done = FALSE;
00127     return CURLE_OK;
00128   }
00129 
00130   /* So IE browsers < v7 cut off the URI part at the query part when they
00131      evaluate the MD5 and some (IIS?) servers work with them so we may need to
00132      do the Digest IE-style. Note that the different ways cause different MD5
00133      sums to get sent.
00134 
00135      Apache servers can be set to do the Digest IE-style automatically using
00136      the BrowserMatch feature:
00137      https://httpd.apache.org/docs/2.2/mod/mod_auth_digest.html#msie
00138 
00139      Further details on Digest implementation differences:
00140      http://www.fngtps.com/2006/09/http-authentication
00141   */
00142 
00143   if(authp->iestyle) {
00144     tmp = strchr((char *)uripath, '?');
00145     if(tmp) {
00146       size_t urilen = tmp - (char *)uripath;
00147       path = (unsigned char *) aprintf("%.*s", urilen, uripath);
00148     }
00149   }
00150   if(!tmp)
00151     path = (unsigned char *) strdup((char *) uripath);
00152 
00153   if(!path)
00154     return CURLE_OUT_OF_MEMORY;
00155 
00156   result = Curl_auth_create_digest_http_message(data, userp, passwdp, request,
00157                                                 path, digest, &response, &len);
00158   free(path);
00159   if(result)
00160     return result;
00161 
00162   *allocuserpwd = aprintf("%sAuthorization: Digest %s\r\n",
00163                           proxy ? "Proxy-" : "",
00164                           response);
00165   free(response);
00166   if(!*allocuserpwd)
00167     return CURLE_OUT_OF_MEMORY;
00168 
00169   authp->done = TRUE;
00170 
00171   return CURLE_OK;
00172 }
00173 
00174 void Curl_digest_cleanup(struct Curl_easy *data)
00175 {
00176   Curl_auth_digest_cleanup(&data->state.digest);
00177   Curl_auth_digest_cleanup(&data->state.proxydigest);
00178 }
00179 
00180 #endif


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:04