spnego_sspi.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  * RFC4178 Simple and Protected GSS-API Negotiation Mechanism
00022  *
00023  ***************************************************************************/
00024 
00025 #include "curl_setup.h"
00026 
00027 #if defined(USE_WINDOWS_SSPI) && defined(USE_SPNEGO)
00028 
00029 #include <curl/curl.h>
00030 
00031 #include "vauth/vauth.h"
00032 #include "urldata.h"
00033 #include "curl_base64.h"
00034 #include "warnless.h"
00035 #include "curl_multibyte.h"
00036 #include "sendf.h"
00037 
00038 /* The last #include files should be: */
00039 #include "curl_memory.h"
00040 #include "memdebug.h"
00041 
00042 /*
00043  * Curl_auth_is_spnego_supported()
00044  *
00045  * This is used to evaluate if SPNEGO (Negotiate) is supported.
00046  *
00047  * Parameters: None
00048  *
00049  * Returns TRUE if Negotiate is supported by Windows SSPI.
00050  */
00051 bool Curl_auth_is_spnego_supported(void)
00052 {
00053   PSecPkgInfo SecurityPackage;
00054   SECURITY_STATUS status;
00055 
00056   /* Query the security package for Negotiate */
00057   status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *)
00058                                               TEXT(SP_NAME_NEGOTIATE),
00059                                               &SecurityPackage);
00060 
00061   return (status == SEC_E_OK ? TRUE : FALSE);
00062 }
00063 
00064 /*
00065  * Curl_auth_decode_spnego_message()
00066  *
00067  * This is used to decode an already encoded SPNEGO (Negotiate) challenge
00068  * message.
00069  *
00070  * Parameters:
00071  *
00072  * data        [in]     - The session handle.
00073  * userp       [in]     - The user name in the format User or Domain\User.
00074  * passdwp     [in]     - The user's password.
00075  * service     [in]     - The service type such as http, smtp, pop or imap.
00076  * host        [in]     - The host name.
00077  * chlg64      [in]     - The optional base64 encoded challenge message.
00078  * nego        [in/out] - The Negotiate data struct being used and modified.
00079  *
00080  * Returns CURLE_OK on success.
00081  */
00082 CURLcode Curl_auth_decode_spnego_message(struct Curl_easy *data,
00083                                          const char *user,
00084                                          const char *password,
00085                                          const char *service,
00086                                          const char *host,
00087                                          const char *chlg64,
00088                                          struct negotiatedata *nego)
00089 {
00090   CURLcode result = CURLE_OK;
00091   size_t chlglen = 0;
00092   unsigned char *chlg = NULL;
00093   PSecPkgInfo SecurityPackage;
00094   SecBuffer chlg_buf;
00095   SecBuffer resp_buf;
00096   SecBufferDesc chlg_desc;
00097   SecBufferDesc resp_desc;
00098   unsigned long attrs;
00099   TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
00100 
00101 #if defined(CURL_DISABLE_VERBOSE_STRINGS)
00102   (void) data;
00103 #endif
00104 
00105   if(nego->context && nego->status == SEC_E_OK) {
00106     /* We finished successfully our part of authentication, but server
00107      * rejected it (since we're again here). Exit with an error since we
00108      * can't invent anything better */
00109     Curl_auth_spnego_cleanup(nego);
00110     return CURLE_LOGIN_DENIED;
00111   }
00112 
00113   if(!nego->spn) {
00114     /* Generate our SPN */
00115     nego->spn = Curl_auth_build_spn(service, host, NULL);
00116     if(!nego->spn)
00117       return CURLE_OUT_OF_MEMORY;
00118   }
00119 
00120   if(!nego->output_token) {
00121     /* Query the security package for Negotiate */
00122     nego->status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *)
00123                                                       TEXT(SP_NAME_NEGOTIATE),
00124                                                       &SecurityPackage);
00125     if(nego->status != SEC_E_OK)
00126       return CURLE_NOT_BUILT_IN;
00127 
00128     nego->token_max = SecurityPackage->cbMaxToken;
00129 
00130     /* Release the package buffer as it is not required anymore */
00131     s_pSecFn->FreeContextBuffer(SecurityPackage);
00132 
00133     /* Allocate our output buffer */
00134     nego->output_token = malloc(nego->token_max);
00135     if(!nego->output_token)
00136       return CURLE_OUT_OF_MEMORY;
00137  }
00138 
00139   if(!nego->credentials) {
00140     /* Do we have credientials to use or are we using single sign-on? */
00141     if(user && *user) {
00142       /* Populate our identity structure */
00143       result = Curl_create_sspi_identity(user, password, &nego->identity);
00144       if(result)
00145         return result;
00146 
00147       /* Allow proper cleanup of the identity structure */
00148       nego->p_identity = &nego->identity;
00149     }
00150     else
00151       /* Use the current Windows user */
00152       nego->p_identity = NULL;
00153 
00154     /* Allocate our credentials handle */
00155     nego->credentials = malloc(sizeof(CredHandle));
00156     if(!nego->credentials)
00157       return CURLE_OUT_OF_MEMORY;
00158 
00159     memset(nego->credentials, 0, sizeof(CredHandle));
00160 
00161     /* Acquire our credentials handle */
00162     nego->status =
00163       s_pSecFn->AcquireCredentialsHandle(NULL,
00164                                          (TCHAR *)TEXT(SP_NAME_NEGOTIATE),
00165                                          SECPKG_CRED_OUTBOUND, NULL,
00166                                          nego->p_identity, NULL, NULL,
00167                                          nego->credentials, &expiry);
00168     if(nego->status != SEC_E_OK)
00169       return CURLE_LOGIN_DENIED;
00170 
00171     /* Allocate our new context handle */
00172     nego->context = malloc(sizeof(CtxtHandle));
00173     if(!nego->context)
00174       return CURLE_OUT_OF_MEMORY;
00175 
00176     memset(nego->context, 0, sizeof(CtxtHandle));
00177   }
00178 
00179   if(chlg64 && *chlg64) {
00180     /* Decode the base-64 encoded challenge message */
00181     if(*chlg64 != '=') {
00182       result = Curl_base64_decode(chlg64, &chlg, &chlglen);
00183       if(result)
00184         return result;
00185     }
00186 
00187     /* Ensure we have a valid challenge message */
00188     if(!chlg) {
00189       infof(data, "SPNEGO handshake failure (empty challenge message)\n");
00190 
00191       return CURLE_BAD_CONTENT_ENCODING;
00192     }
00193 
00194     /* Setup the challenge "input" security buffer */
00195     chlg_desc.ulVersion = SECBUFFER_VERSION;
00196     chlg_desc.cBuffers  = 1;
00197     chlg_desc.pBuffers  = &chlg_buf;
00198     chlg_buf.BufferType = SECBUFFER_TOKEN;
00199     chlg_buf.pvBuffer   = chlg;
00200     chlg_buf.cbBuffer   = curlx_uztoul(chlglen);
00201   }
00202 
00203   /* Setup the response "output" security buffer */
00204   resp_desc.ulVersion = SECBUFFER_VERSION;
00205   resp_desc.cBuffers  = 1;
00206   resp_desc.pBuffers  = &resp_buf;
00207   resp_buf.BufferType = SECBUFFER_TOKEN;
00208   resp_buf.pvBuffer   = nego->output_token;
00209   resp_buf.cbBuffer   = curlx_uztoul(nego->token_max);
00210 
00211   /* Generate our challenge-response message */
00212   nego->status = s_pSecFn->InitializeSecurityContext(nego->credentials,
00213                                                      chlg ? nego->context :
00214                                                             NULL,
00215                                                      nego->spn,
00216                                                      ISC_REQ_CONFIDENTIALITY,
00217                                                      0, SECURITY_NATIVE_DREP,
00218                                                      chlg ? &chlg_desc : NULL,
00219                                                      0, nego->context,
00220                                                      &resp_desc, &attrs,
00221                                                      &expiry);
00222 
00223   /* Free the decoded challenge as it is not required anymore */
00224   free(chlg);
00225 
00226   if(GSS_ERROR(nego->status)) {
00227     return CURLE_OUT_OF_MEMORY;
00228   }
00229 
00230   if(nego->status == SEC_I_COMPLETE_NEEDED ||
00231      nego->status == SEC_I_COMPLETE_AND_CONTINUE) {
00232     nego->status = s_pSecFn->CompleteAuthToken(nego->context, &resp_desc);
00233     if(GSS_ERROR(nego->status)) {
00234       return CURLE_RECV_ERROR;
00235     }
00236   }
00237 
00238   nego->output_token_length = resp_buf.cbBuffer;
00239 
00240   return result;
00241 }
00242 
00243 /*
00244  * Curl_auth_create_spnego_message()
00245  *
00246  * This is used to generate an already encoded SPNEGO (Negotiate) response
00247  * message ready for sending to the recipient.
00248  *
00249  * Parameters:
00250  *
00251  * data        [in]     - The session handle.
00252  * nego        [in/out] - The Negotiate data struct being used and modified.
00253  * outptr      [in/out] - The address where a pointer to newly allocated memory
00254  *                        holding the result will be stored upon completion.
00255  * outlen      [out]    - The length of the output message.
00256  *
00257  * Returns CURLE_OK on success.
00258  */
00259 CURLcode Curl_auth_create_spnego_message(struct Curl_easy *data,
00260                                          struct negotiatedata *nego,
00261                                          char **outptr, size_t *outlen)
00262 {
00263   CURLcode result;
00264 
00265   /* Base64 encode the already generated response */
00266   result = Curl_base64_encode(data,
00267                               (const char *) nego->output_token,
00268                               nego->output_token_length,
00269                               outptr, outlen);
00270 
00271   if(result)
00272     return result;
00273 
00274   if(!*outptr || !*outlen) {
00275     free(*outptr);
00276     return CURLE_REMOTE_ACCESS_DENIED;
00277   }
00278 
00279   return CURLE_OK;
00280 }
00281 
00282 /*
00283  * Curl_auth_spnego_cleanup()
00284  *
00285  * This is used to clean up the SPNEGO (Negotiate) specific data.
00286  *
00287  * Parameters:
00288  *
00289  * nego     [in/out] - The Negotiate data struct being cleaned up.
00290  *
00291  */
00292 void Curl_auth_spnego_cleanup(struct negotiatedata *nego)
00293 {
00294   /* Free our security context */
00295   if(nego->context) {
00296     s_pSecFn->DeleteSecurityContext(nego->context);
00297     free(nego->context);
00298     nego->context = NULL;
00299   }
00300 
00301   /* Free our credentials handle */
00302   if(nego->credentials) {
00303     s_pSecFn->FreeCredentialsHandle(nego->credentials);
00304     free(nego->credentials);
00305     nego->credentials = NULL;
00306   }
00307 
00308   /* Free our identity */
00309   Curl_sspi_free_identity(nego->p_identity);
00310   nego->p_identity = NULL;
00311 
00312   /* Free the SPN and output token */
00313   Curl_safefree(nego->spn);
00314   Curl_safefree(nego->output_token);
00315 
00316   /* Reset any variables */
00317   nego->status = 0;
00318   nego->token_max = 0;
00319 }
00320 
00321 #endif /* USE_WINDOWS_SSPI && USE_SPNEGO */


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