00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00039 #include "curl_memory.h"
00040 #include "memdebug.h"
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 bool Curl_auth_is_spnego_supported(void)
00052 {
00053 PSecPkgInfo SecurityPackage;
00054 SECURITY_STATUS status;
00055
00056
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
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
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;
00100
00101 #if defined(CURL_DISABLE_VERBOSE_STRINGS)
00102 (void) data;
00103 #endif
00104
00105 if(nego->context && nego->status == SEC_E_OK) {
00106
00107
00108
00109 Curl_auth_spnego_cleanup(nego);
00110 return CURLE_LOGIN_DENIED;
00111 }
00112
00113 if(!nego->spn) {
00114
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
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
00131 s_pSecFn->FreeContextBuffer(SecurityPackage);
00132
00133
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
00141 if(user && *user) {
00142
00143 result = Curl_create_sspi_identity(user, password, &nego->identity);
00144 if(result)
00145 return result;
00146
00147
00148 nego->p_identity = &nego->identity;
00149 }
00150 else
00151
00152 nego->p_identity = NULL;
00153
00154
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
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
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
00181 if(*chlg64 != '=') {
00182 result = Curl_base64_decode(chlg64, &chlg, &chlglen);
00183 if(result)
00184 return result;
00185 }
00186
00187
00188 if(!chlg) {
00189 infof(data, "SPNEGO handshake failure (empty challenge message)\n");
00190
00191 return CURLE_BAD_CONTENT_ENCODING;
00192 }
00193
00194
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
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
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
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
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
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
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
00284
00285
00286
00287
00288
00289
00290
00291
00292 void Curl_auth_spnego_cleanup(struct negotiatedata *nego)
00293 {
00294
00295 if(nego->context) {
00296 s_pSecFn->DeleteSecurityContext(nego->context);
00297 free(nego->context);
00298 nego->context = NULL;
00299 }
00300
00301
00302 if(nego->credentials) {
00303 s_pSecFn->FreeCredentialsHandle(nego->credentials);
00304 free(nego->credentials);
00305 nego->credentials = NULL;
00306 }
00307
00308
00309 Curl_sspi_free_identity(nego->p_identity);
00310 nego->p_identity = NULL;
00311
00312
00313 Curl_safefree(nego->spn);
00314 Curl_safefree(nego->output_token);
00315
00316
00317 nego->status = 0;
00318 nego->token_max = 0;
00319 }
00320
00321 #endif