digest_sspi.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 2014 - 2016, Steve Holme, <steve_holme@hotmail.com>.
00009  * Copyright (C) 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
00010  *
00011  * This software is licensed as described in the file COPYING, which
00012  * you should have received as part of this distribution. The terms
00013  * are also available at https://curl.haxx.se/docs/copyright.html.
00014  *
00015  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00016  * copies of the Software, and permit persons to whom the Software is
00017  * furnished to do so, under the terms of the COPYING file.
00018  *
00019  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00020  * KIND, either express or implied.
00021  *
00022  * RFC2831 DIGEST-MD5 authentication
00023  *
00024  ***************************************************************************/
00025 
00026 #include "curl_setup.h"
00027 
00028 #if defined(USE_WINDOWS_SSPI) && !defined(CURL_DISABLE_CRYPTO_AUTH)
00029 
00030 #include <curl/curl.h>
00031 
00032 #include "vauth/vauth.h"
00033 #include "vauth/digest.h"
00034 #include "urldata.h"
00035 #include "curl_base64.h"
00036 #include "warnless.h"
00037 #include "curl_multibyte.h"
00038 #include "sendf.h"
00039 #include "strdup.h"
00040 #include "strcase.h"
00041 
00042 /* The last #include files should be: */
00043 #include "curl_memory.h"
00044 #include "memdebug.h"
00045 
00046 /*
00047 * Curl_auth_is_digest_supported()
00048 *
00049 * This is used to evaluate if DIGEST is supported.
00050 *
00051 * Parameters: None
00052 *
00053 * Returns TRUE if DIGEST is supported by Windows SSPI.
00054 */
00055 bool Curl_auth_is_digest_supported(void)
00056 {
00057   PSecPkgInfo SecurityPackage;
00058   SECURITY_STATUS status;
00059 
00060   /* Query the security package for Digest */
00061   status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *) TEXT(SP_NAME_DIGEST),
00062                                               &SecurityPackage);
00063 
00064   return (status == SEC_E_OK ? TRUE : FALSE);
00065 }
00066 
00067 /*
00068  * Curl_auth_create_digest_md5_message()
00069  *
00070  * This is used to generate an already encoded DIGEST-MD5 response message
00071  * ready for sending to the recipient.
00072  *
00073  * Parameters:
00074  *
00075  * data    [in]     - The session handle.
00076  * chlg64  [in]     - The base64 encoded challenge message.
00077  * userp   [in]     - The user name in the format User or Domain\User.
00078  * passdwp [in]     - The user's password.
00079  * service [in]     - The service type such as http, smtp, pop or imap.
00080  * outptr  [in/out] - The address where a pointer to newly allocated memory
00081  *                    holding the result will be stored upon completion.
00082  * outlen  [out]    - The length of the output message.
00083  *
00084  * Returns CURLE_OK on success.
00085  */
00086 CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data,
00087                                              const char *chlg64,
00088                                              const char *userp,
00089                                              const char *passwdp,
00090                                              const char *service,
00091                                              char **outptr, size_t *outlen)
00092 {
00093   CURLcode result = CURLE_OK;
00094   TCHAR *spn = NULL;
00095   size_t chlglen = 0;
00096   size_t token_max = 0;
00097   unsigned char *input_token = NULL;
00098   unsigned char *output_token = NULL;
00099   CredHandle credentials;
00100   CtxtHandle context;
00101   PSecPkgInfo SecurityPackage;
00102   SEC_WINNT_AUTH_IDENTITY identity;
00103   SEC_WINNT_AUTH_IDENTITY *p_identity;
00104   SecBuffer chlg_buf;
00105   SecBuffer resp_buf;
00106   SecBufferDesc chlg_desc;
00107   SecBufferDesc resp_desc;
00108   SECURITY_STATUS status;
00109   unsigned long attrs;
00110   TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
00111 
00112   /* Decode the base-64 encoded challenge message */
00113   if(strlen(chlg64) && *chlg64 != '=') {
00114     result = Curl_base64_decode(chlg64, &input_token, &chlglen);
00115     if(result)
00116       return result;
00117   }
00118 
00119   /* Ensure we have a valid challenge message */
00120   if(!input_token) {
00121     infof(data, "DIGEST-MD5 handshake failure (empty challenge message)\n");
00122 
00123     return CURLE_BAD_CONTENT_ENCODING;
00124   }
00125 
00126   /* Query the security package for DigestSSP */
00127   status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *) TEXT(SP_NAME_DIGEST),
00128                                               &SecurityPackage);
00129   if(status != SEC_E_OK) {
00130     free(input_token);
00131 
00132     return CURLE_NOT_BUILT_IN;
00133   }
00134 
00135   token_max = SecurityPackage->cbMaxToken;
00136 
00137   /* Release the package buffer as it is not required anymore */
00138   s_pSecFn->FreeContextBuffer(SecurityPackage);
00139 
00140   /* Allocate our response buffer */
00141   output_token = malloc(token_max);
00142   if(!output_token) {
00143     free(input_token);
00144 
00145     return CURLE_OUT_OF_MEMORY;
00146   }
00147 
00148   /* Generate our SPN */
00149   spn = Curl_auth_build_spn(service, data->easy_conn->host.name, NULL);
00150   if(!spn) {
00151     free(output_token);
00152     free(input_token);
00153 
00154     return CURLE_OUT_OF_MEMORY;
00155   }
00156 
00157   if(userp && *userp) {
00158     /* Populate our identity structure */
00159     result = Curl_create_sspi_identity(userp, passwdp, &identity);
00160     if(result) {
00161       free(spn);
00162       free(output_token);
00163       free(input_token);
00164 
00165       return result;
00166     }
00167 
00168     /* Allow proper cleanup of the identity structure */
00169     p_identity = &identity;
00170   }
00171   else
00172     /* Use the current Windows user */
00173     p_identity = NULL;
00174 
00175   /* Acquire our credentials handle */
00176   status = s_pSecFn->AcquireCredentialsHandle(NULL,
00177                                               (TCHAR *) TEXT(SP_NAME_DIGEST),
00178                                               SECPKG_CRED_OUTBOUND, NULL,
00179                                               p_identity, NULL, NULL,
00180                                               &credentials, &expiry);
00181 
00182   if(status != SEC_E_OK) {
00183     Curl_sspi_free_identity(p_identity);
00184     free(spn);
00185     free(output_token);
00186     free(input_token);
00187 
00188     return CURLE_LOGIN_DENIED;
00189   }
00190 
00191   /* Setup the challenge "input" security buffer */
00192   chlg_desc.ulVersion = SECBUFFER_VERSION;
00193   chlg_desc.cBuffers  = 1;
00194   chlg_desc.pBuffers  = &chlg_buf;
00195   chlg_buf.BufferType = SECBUFFER_TOKEN;
00196   chlg_buf.pvBuffer   = input_token;
00197   chlg_buf.cbBuffer   = curlx_uztoul(chlglen);
00198 
00199   /* Setup the response "output" security buffer */
00200   resp_desc.ulVersion = SECBUFFER_VERSION;
00201   resp_desc.cBuffers  = 1;
00202   resp_desc.pBuffers  = &resp_buf;
00203   resp_buf.BufferType = SECBUFFER_TOKEN;
00204   resp_buf.pvBuffer   = output_token;
00205   resp_buf.cbBuffer   = curlx_uztoul(token_max);
00206 
00207   /* Generate our response message */
00208   status = s_pSecFn->InitializeSecurityContext(&credentials, NULL, spn,
00209                                                0, 0, 0, &chlg_desc, 0,
00210                                                &context, &resp_desc, &attrs,
00211                                                &expiry);
00212 
00213   if(status == SEC_I_COMPLETE_NEEDED ||
00214      status == SEC_I_COMPLETE_AND_CONTINUE)
00215     s_pSecFn->CompleteAuthToken(&credentials, &resp_desc);
00216   else if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED) {
00217     s_pSecFn->FreeCredentialsHandle(&credentials);
00218     Curl_sspi_free_identity(p_identity);
00219     free(spn);
00220     free(output_token);
00221     free(input_token);
00222 
00223     return CURLE_RECV_ERROR;
00224   }
00225 
00226   /* Base64 encode the response */
00227   result = Curl_base64_encode(data, (char *) output_token, resp_buf.cbBuffer,
00228                               outptr, outlen);
00229 
00230   /* Free our handles */
00231   s_pSecFn->DeleteSecurityContext(&context);
00232   s_pSecFn->FreeCredentialsHandle(&credentials);
00233 
00234   /* Free the identity structure */
00235   Curl_sspi_free_identity(p_identity);
00236 
00237   /* Free the SPN */
00238   free(spn);
00239 
00240   /* Free the response buffer */
00241   free(output_token);
00242 
00243   /* Free the decoded challenge message */
00244   free(input_token);
00245 
00246   return result;
00247 }
00248 
00249 /*
00250  * Curl_override_sspi_http_realm()
00251  *
00252  * This is used to populate the domain in a SSPI identity structure
00253  * The realm is extracted from the challenge message and used as the
00254  * domain if it is not already explicitly set.
00255  *
00256  * Parameters:
00257  *
00258  * chlg     [in]     - The challenge message.
00259  * identity [in/out] - The identity structure.
00260  *
00261  * Returns CURLE_OK on success.
00262  */
00263 CURLcode Curl_override_sspi_http_realm(const char *chlg,
00264                                        SEC_WINNT_AUTH_IDENTITY *identity)
00265 {
00266   xcharp_u domain, dup_domain;
00267 
00268   /* If domain is blank or unset, check challenge message for realm */
00269   if(!identity->Domain || !identity->DomainLength) {
00270     for(;;) {
00271       char value[DIGEST_MAX_VALUE_LENGTH];
00272       char content[DIGEST_MAX_CONTENT_LENGTH];
00273 
00274       /* Pass all additional spaces here */
00275       while(*chlg && ISSPACE(*chlg))
00276         chlg++;
00277 
00278       /* Extract a value=content pair */
00279       if(Curl_auth_digest_get_pair(chlg, value, content, &chlg)) {
00280         if(strcasecompare(value, "realm")) {
00281 
00282           /* Setup identity's domain and length */
00283           domain.tchar_ptr = Curl_convert_UTF8_to_tchar((char *) content);
00284           if(!domain.tchar_ptr)
00285             return CURLE_OUT_OF_MEMORY;
00286 
00287           dup_domain.tchar_ptr = _tcsdup(domain.tchar_ptr);
00288           if(!dup_domain.tchar_ptr) {
00289             Curl_unicodefree(domain.tchar_ptr);
00290             return CURLE_OUT_OF_MEMORY;
00291           }
00292 
00293           free(identity->Domain);
00294           identity->Domain = dup_domain.tbyte_ptr;
00295           identity->DomainLength = curlx_uztoul(_tcslen(dup_domain.tchar_ptr));
00296           dup_domain.tchar_ptr = NULL;
00297 
00298           Curl_unicodefree(domain.tchar_ptr);
00299         }
00300         else {
00301           /* Unknown specifier, ignore it! */
00302         }
00303       }
00304       else
00305         break; /* We're done here */
00306 
00307       /* Pass all additional spaces here */
00308       while(*chlg && ISSPACE(*chlg))
00309         chlg++;
00310 
00311       /* Allow the list to be comma-separated */
00312       if(',' == *chlg)
00313         chlg++;
00314     }
00315   }
00316 
00317   return CURLE_OK;
00318 }
00319 
00320 /*
00321  * Curl_auth_decode_digest_http_message()
00322  *
00323  * This is used to decode a HTTP DIGEST challenge message into the seperate
00324  * attributes.
00325  *
00326  * Parameters:
00327  *
00328  * chlg    [in]     - The challenge message.
00329  * digest  [in/out] - The digest data struct being used and modified.
00330  *
00331  * Returns CURLE_OK on success.
00332  */
00333 CURLcode Curl_auth_decode_digest_http_message(const char *chlg,
00334                                               struct digestdata *digest)
00335 {
00336   size_t chlglen = strlen(chlg);
00337 
00338   /* We had an input token before and we got another one now. This means we
00339      provided bad credentials in the previous request. */
00340   if(digest->input_token)
00341     return CURLE_BAD_CONTENT_ENCODING;
00342 
00343   /* Simply store the challenge for use later */
00344   digest->input_token = (BYTE *) Curl_memdup(chlg, chlglen);
00345   if(!digest->input_token)
00346     return CURLE_OUT_OF_MEMORY;
00347 
00348   digest->input_token_len = chlglen;
00349 
00350   return CURLE_OK;
00351 }
00352 
00353 /*
00354  * Curl_auth_create_digest_http_message()
00355  *
00356  * This is used to generate a HTTP DIGEST response message ready for sending
00357  * to the recipient.
00358  *
00359  * Parameters:
00360  *
00361  * data    [in]     - The session handle.
00362  * userp   [in]     - The user name in the format User or Domain\User.
00363  * passdwp [in]     - The user's password.
00364  * request [in]     - The HTTP request.
00365  * uripath [in]     - The path of the HTTP uri.
00366  * digest  [in/out] - The digest data struct being used and modified.
00367  * outptr  [in/out] - The address where a pointer to newly allocated memory
00368  *                    holding the result will be stored upon completion.
00369  * outlen  [out]    - The length of the output message.
00370  *
00371  * Returns CURLE_OK on success.
00372  */
00373 CURLcode Curl_auth_create_digest_http_message(struct Curl_easy *data,
00374                                               const char *userp,
00375                                               const char *passwdp,
00376                                               const unsigned char *request,
00377                                               const unsigned char *uripath,
00378                                               struct digestdata *digest,
00379                                               char **outptr, size_t *outlen)
00380 {
00381   size_t token_max;
00382   CredHandle credentials;
00383   CtxtHandle context;
00384   char *resp;
00385   BYTE *output_token;
00386   PSecPkgInfo SecurityPackage;
00387   SEC_WINNT_AUTH_IDENTITY identity;
00388   SEC_WINNT_AUTH_IDENTITY *p_identity;
00389   SecBuffer chlg_buf[3];
00390   SecBuffer resp_buf;
00391   SecBufferDesc chlg_desc;
00392   SecBufferDesc resp_desc;
00393   SECURITY_STATUS status;
00394   unsigned long attrs;
00395   TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
00396   TCHAR *spn;
00397 
00398   (void) data;
00399 
00400   /* Query the security package for DigestSSP */
00401   status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *) TEXT(SP_NAME_DIGEST),
00402                                               &SecurityPackage);
00403   if(status != SEC_E_OK)
00404     return CURLE_NOT_BUILT_IN;
00405 
00406   token_max = SecurityPackage->cbMaxToken;
00407 
00408   /* Release the package buffer as it is not required anymore */
00409   s_pSecFn->FreeContextBuffer(SecurityPackage);
00410 
00411   if(userp && *userp) {
00412     /* Populate our identity structure */
00413     if(Curl_create_sspi_identity(userp, passwdp, &identity))
00414       return CURLE_OUT_OF_MEMORY;
00415 
00416     /* Populate our identity domain */
00417     if(Curl_override_sspi_http_realm((const char *) digest->input_token,
00418                                      &identity))
00419       return CURLE_OUT_OF_MEMORY;
00420 
00421     /* Allow proper cleanup of the identity structure */
00422     p_identity = &identity;
00423   }
00424   else
00425     /* Use the current Windows user */
00426     p_identity = NULL;
00427 
00428   /* Acquire our credentials handle */
00429   status = s_pSecFn->AcquireCredentialsHandle(NULL,
00430                                               (TCHAR *) TEXT(SP_NAME_DIGEST),
00431                                               SECPKG_CRED_OUTBOUND, NULL,
00432                                               p_identity, NULL, NULL,
00433                                               &credentials, &expiry);
00434   if(status != SEC_E_OK) {
00435     Curl_sspi_free_identity(p_identity);
00436 
00437     return CURLE_LOGIN_DENIED;
00438   }
00439 
00440   /* Allocate the output buffer according to the max token size as indicated
00441      by the security package */
00442   output_token = malloc(token_max);
00443   if(!output_token) {
00444     s_pSecFn->FreeCredentialsHandle(&credentials);
00445 
00446     Curl_sspi_free_identity(p_identity);
00447 
00448     return CURLE_OUT_OF_MEMORY;
00449   }
00450 
00451   /* Setup the challenge "input" security buffer if present */
00452   chlg_desc.ulVersion    = SECBUFFER_VERSION;
00453   chlg_desc.cBuffers     = 3;
00454   chlg_desc.pBuffers     = chlg_buf;
00455   chlg_buf[0].BufferType = SECBUFFER_TOKEN;
00456   chlg_buf[0].pvBuffer   = digest->input_token;
00457   chlg_buf[0].cbBuffer   = curlx_uztoul(digest->input_token_len);
00458   chlg_buf[1].BufferType = SECBUFFER_PKG_PARAMS;
00459   chlg_buf[1].pvBuffer   = (void *) request;
00460   chlg_buf[1].cbBuffer   = curlx_uztoul(strlen((const char *) request));
00461   chlg_buf[2].BufferType = SECBUFFER_PKG_PARAMS;
00462   chlg_buf[2].pvBuffer   = NULL;
00463   chlg_buf[2].cbBuffer   = 0;
00464 
00465   /* Setup the response "output" security buffer */
00466   resp_desc.ulVersion = SECBUFFER_VERSION;
00467   resp_desc.cBuffers  = 1;
00468   resp_desc.pBuffers  = &resp_buf;
00469   resp_buf.BufferType = SECBUFFER_TOKEN;
00470   resp_buf.pvBuffer   = output_token;
00471   resp_buf.cbBuffer   = curlx_uztoul(token_max);
00472 
00473   spn = Curl_convert_UTF8_to_tchar((char *) uripath);
00474   if(!spn) {
00475     s_pSecFn->FreeCredentialsHandle(&credentials);
00476 
00477     Curl_sspi_free_identity(p_identity);
00478     free(output_token);
00479 
00480     return CURLE_OUT_OF_MEMORY;
00481   }
00482 
00483   /* Generate our reponse message */
00484   status = s_pSecFn->InitializeSecurityContext(&credentials, NULL,
00485                                                spn,
00486                                                ISC_REQ_USE_HTTP_STYLE, 0, 0,
00487                                                &chlg_desc, 0, &context,
00488                                                &resp_desc, &attrs, &expiry);
00489   Curl_unicodefree(spn);
00490 
00491   if(status == SEC_I_COMPLETE_NEEDED ||
00492      status == SEC_I_COMPLETE_AND_CONTINUE)
00493     s_pSecFn->CompleteAuthToken(&credentials, &resp_desc);
00494   else if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED) {
00495     s_pSecFn->FreeCredentialsHandle(&credentials);
00496 
00497     Curl_sspi_free_identity(p_identity);
00498     free(output_token);
00499 
00500     return CURLE_OUT_OF_MEMORY;
00501   }
00502 
00503   resp = malloc(resp_buf.cbBuffer + 1);
00504   if(!resp) {
00505     s_pSecFn->DeleteSecurityContext(&context);
00506     s_pSecFn->FreeCredentialsHandle(&credentials);
00507 
00508     Curl_sspi_free_identity(p_identity);
00509     free(output_token);
00510 
00511     return CURLE_OUT_OF_MEMORY;
00512   }
00513 
00514   /* Copy the generated reponse */
00515   memcpy(resp, resp_buf.pvBuffer, resp_buf.cbBuffer);
00516   resp[resp_buf.cbBuffer] = 0x00;
00517 
00518   /* Return the response */
00519   *outptr = resp;
00520   *outlen = resp_buf.cbBuffer;
00521 
00522   /* Free our handles */
00523   s_pSecFn->DeleteSecurityContext(&context);
00524   s_pSecFn->FreeCredentialsHandle(&credentials);
00525 
00526   /* Free the identity structure */
00527   Curl_sspi_free_identity(p_identity);
00528 
00529   /* Free the response buffer */
00530   free(output_token);
00531 
00532   return CURLE_OK;
00533 }
00534 
00535 /*
00536  * Curl_auth_digest_cleanup()
00537  *
00538  * This is used to clean up the digest specific data.
00539  *
00540  * Parameters:
00541  *
00542  * digest    [in/out] - The digest data struct being cleaned up.
00543  *
00544  */
00545 void Curl_auth_digest_cleanup(struct digestdata *digest)
00546 {
00547   /* Free the input token */
00548   Curl_safefree(digest->input_token);
00549 
00550   /* Reset any variables */
00551   digest->input_token_len = 0;
00552 }
00553 
00554 #endif /* USE_WINDOWS_SSPI && !CURL_DISABLE_CRYPTO_AUTH */


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