123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625 |
- #include "curl_setup.h"
- #if defined(USE_WINDOWS_SSPI) && !defined(CURL_DISABLE_PROXY)
- #include "urldata.h"
- #include "sendf.h"
- #include "cfilters.h"
- #include "connect.h"
- #include "strerror.h"
- #include "timeval.h"
- #include "socks.h"
- #include "curl_sspi.h"
- #include "curl_multibyte.h"
- #include "warnless.h"
- #include "strdup.h"
- #include "curl_printf.h"
- #include "curl_memory.h"
- #include "memdebug.h"
- static int check_sspi_err(struct Curl_easy *data,
- SECURITY_STATUS status,
- const char *function)
- {
- if(status != SEC_E_OK &&
- status != SEC_I_COMPLETE_AND_CONTINUE &&
- status != SEC_I_COMPLETE_NEEDED &&
- status != SEC_I_CONTINUE_NEEDED) {
- char buffer[STRERROR_LEN];
- failf(data, "SSPI error: %s failed: %s", function,
- Curl_sspi_strerror(status, buffer, sizeof(buffer)));
- return 1;
- }
- return 0;
- }
- CURLcode Curl_SOCKS5_gssapi_negotiate(struct Curl_cfilter *cf,
- struct Curl_easy *data)
- {
- struct connectdata *conn = cf->conn;
- curl_socket_t sock = conn->sock[cf->sockindex];
- CURLcode code;
- ssize_t actualread;
- ssize_t written;
- int result;
-
- SECURITY_STATUS status;
- unsigned long sspi_ret_flags = 0;
- unsigned char gss_enc;
- SecBuffer sspi_send_token, sspi_recv_token, sspi_w_token[3];
- SecBufferDesc input_desc, output_desc, wrap_desc;
- SecPkgContext_Sizes sspi_sizes;
- CredHandle cred_handle;
- CtxtHandle sspi_context;
- PCtxtHandle context_handle = NULL;
- SecPkgCredentials_Names names;
- TimeStamp expiry;
- char *service_name = NULL;
- unsigned short us_length;
- unsigned long qop;
- unsigned char socksreq[4];
- const char *service = data->set.str[STRING_PROXY_SERVICE_NAME] ?
- data->set.str[STRING_PROXY_SERVICE_NAME] : "rcmd";
- const size_t service_length = strlen(service);
-
-
- if(strchr(service, '/')) {
- service_name = strdup(service);
- if(!service_name)
- return CURLE_OUT_OF_MEMORY;
- }
- else {
- service_name = malloc(service_length +
- strlen(conn->socks_proxy.host.name) + 2);
- if(!service_name)
- return CURLE_OUT_OF_MEMORY;
- msnprintf(service_name, service_length +
- strlen(conn->socks_proxy.host.name) + 2, "%s/%s",
- service, conn->socks_proxy.host.name);
- }
- input_desc.cBuffers = 1;
- input_desc.pBuffers = &sspi_recv_token;
- input_desc.ulVersion = SECBUFFER_VERSION;
- sspi_recv_token.BufferType = SECBUFFER_TOKEN;
- sspi_recv_token.cbBuffer = 0;
- sspi_recv_token.pvBuffer = NULL;
- output_desc.cBuffers = 1;
- output_desc.pBuffers = &sspi_send_token;
- output_desc.ulVersion = SECBUFFER_VERSION;
- sspi_send_token.BufferType = SECBUFFER_TOKEN;
- sspi_send_token.cbBuffer = 0;
- sspi_send_token.pvBuffer = NULL;
- wrap_desc.cBuffers = 3;
- wrap_desc.pBuffers = sspi_w_token;
- wrap_desc.ulVersion = SECBUFFER_VERSION;
- cred_handle.dwLower = 0;
- cred_handle.dwUpper = 0;
- status = Curl_pSecFn->AcquireCredentialsHandle(NULL,
- (TCHAR *) TEXT("Kerberos"),
- SECPKG_CRED_OUTBOUND,
- NULL,
- NULL,
- NULL,
- NULL,
- &cred_handle,
- &expiry);
- if(check_sspi_err(data, status, "AcquireCredentialsHandle")) {
- failf(data, "Failed to acquire credentials.");
- free(service_name);
- Curl_pSecFn->FreeCredentialsHandle(&cred_handle);
- return CURLE_COULDNT_CONNECT;
- }
- (void)curlx_nonblock(sock, FALSE);
-
-
- for(;;) {
- TCHAR *sname;
- sname = curlx_convert_UTF8_to_tchar(service_name);
- if(!sname)
- return CURLE_OUT_OF_MEMORY;
- status = Curl_pSecFn->InitializeSecurityContext(&cred_handle,
- context_handle,
- sname,
- ISC_REQ_MUTUAL_AUTH |
- ISC_REQ_ALLOCATE_MEMORY |
- ISC_REQ_CONFIDENTIALITY |
- ISC_REQ_REPLAY_DETECT,
- 0,
- SECURITY_NATIVE_DREP,
- &input_desc,
- 0,
- &sspi_context,
- &output_desc,
- &sspi_ret_flags,
- &expiry);
- curlx_unicodefree(sname);
- if(sspi_recv_token.pvBuffer) {
- Curl_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
- sspi_recv_token.pvBuffer = NULL;
- sspi_recv_token.cbBuffer = 0;
- }
- if(check_sspi_err(data, status, "InitializeSecurityContext")) {
- free(service_name);
- Curl_pSecFn->FreeCredentialsHandle(&cred_handle);
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- if(sspi_recv_token.pvBuffer)
- Curl_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
- failf(data, "Failed to initialise security context.");
- return CURLE_COULDNT_CONNECT;
- }
- if(sspi_send_token.cbBuffer) {
- socksreq[0] = 1;
- socksreq[1] = 1;
- us_length = htons((unsigned short)sspi_send_token.cbBuffer);
- memcpy(socksreq + 2, &us_length, sizeof(short));
- written = Curl_conn_cf_send(cf->next, data, (char *)socksreq, 4, FALSE,
- &code);
- if(code || (4 != written)) {
- failf(data, "Failed to send SSPI authentication request.");
- free(service_name);
- if(sspi_send_token.pvBuffer)
- Curl_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
- if(sspi_recv_token.pvBuffer)
- Curl_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
- Curl_pSecFn->FreeCredentialsHandle(&cred_handle);
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- return CURLE_COULDNT_CONNECT;
- }
- written = Curl_conn_cf_send(cf->next, data,
- (char *)sspi_send_token.pvBuffer,
- sspi_send_token.cbBuffer, FALSE, &code);
- if(code || (sspi_send_token.cbBuffer != (size_t)written)) {
- failf(data, "Failed to send SSPI authentication token.");
- free(service_name);
- if(sspi_send_token.pvBuffer)
- Curl_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
- if(sspi_recv_token.pvBuffer)
- Curl_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
- Curl_pSecFn->FreeCredentialsHandle(&cred_handle);
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- return CURLE_COULDNT_CONNECT;
- }
- }
- if(sspi_send_token.pvBuffer) {
- Curl_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
- sspi_send_token.pvBuffer = NULL;
- }
- sspi_send_token.cbBuffer = 0;
- if(sspi_recv_token.pvBuffer) {
- Curl_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
- sspi_recv_token.pvBuffer = NULL;
- }
- sspi_recv_token.cbBuffer = 0;
- if(status != SEC_I_CONTINUE_NEEDED)
- break;
-
-
- result = Curl_blockread_all(cf, data, (char *)socksreq, 4, &actualread);
- if(result || (actualread != 4)) {
- failf(data, "Failed to receive SSPI authentication response.");
- free(service_name);
- Curl_pSecFn->FreeCredentialsHandle(&cred_handle);
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- return CURLE_COULDNT_CONNECT;
- }
-
- if(socksreq[1] == 255) {
- failf(data, "User was rejected by the SOCKS5 server (%u %u).",
- (unsigned int)socksreq[0], (unsigned int)socksreq[1]);
- free(service_name);
- Curl_pSecFn->FreeCredentialsHandle(&cred_handle);
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- return CURLE_COULDNT_CONNECT;
- }
- if(socksreq[1] != 1) {
- failf(data, "Invalid SSPI authentication response type (%u %u).",
- (unsigned int)socksreq[0], (unsigned int)socksreq[1]);
- free(service_name);
- Curl_pSecFn->FreeCredentialsHandle(&cred_handle);
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- return CURLE_COULDNT_CONNECT;
- }
- memcpy(&us_length, socksreq + 2, sizeof(short));
- us_length = ntohs(us_length);
- sspi_recv_token.cbBuffer = us_length;
- sspi_recv_token.pvBuffer = malloc(us_length);
- if(!sspi_recv_token.pvBuffer) {
- free(service_name);
- Curl_pSecFn->FreeCredentialsHandle(&cred_handle);
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- return CURLE_OUT_OF_MEMORY;
- }
- result = Curl_blockread_all(cf, data, (char *)sspi_recv_token.pvBuffer,
- sspi_recv_token.cbBuffer, &actualread);
- if(result || (actualread != us_length)) {
- failf(data, "Failed to receive SSPI authentication token.");
- free(service_name);
- if(sspi_recv_token.pvBuffer)
- Curl_pSecFn->FreeContextBuffer(sspi_recv_token.pvBuffer);
- Curl_pSecFn->FreeCredentialsHandle(&cred_handle);
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- return CURLE_COULDNT_CONNECT;
- }
- context_handle = &sspi_context;
- }
- free(service_name);
-
- status = Curl_pSecFn->QueryCredentialsAttributes(&cred_handle,
- SECPKG_CRED_ATTR_NAMES,
- &names);
- Curl_pSecFn->FreeCredentialsHandle(&cred_handle);
- if(check_sspi_err(data, status, "QueryCredentialAttributes")) {
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- Curl_pSecFn->FreeContextBuffer(names.sUserName);
- failf(data, "Failed to determine username.");
- return CURLE_COULDNT_CONNECT;
- }
- else {
- #ifndef CURL_DISABLE_VERBOSE_STRINGS
- char *user_utf8 = curlx_convert_tchar_to_UTF8(names.sUserName);
- infof(data, "SOCKS5 server authenticated user %s with GSS-API.",
- (user_utf8 ? user_utf8 : "(unknown)"));
- curlx_unicodefree(user_utf8);
- #endif
- Curl_pSecFn->FreeContextBuffer(names.sUserName);
- }
-
- socksreq[0] = 1;
- socksreq[1] = 2;
- gss_enc = 0;
-
- if(sspi_ret_flags & ISC_REQ_CONFIDENTIALITY)
- gss_enc = 2;
-
- else if(sspi_ret_flags & ISC_REQ_INTEGRITY)
- gss_enc = 1;
- infof(data, "SOCKS5 server supports GSS-API %s data protection.",
- (gss_enc == 0) ? "no" :
- ((gss_enc == 1) ? "integrity":"confidentiality") );
-
- gss_enc = 0;
-
- if(data->set.socks5_gssapi_nec) {
- us_length = htons((unsigned short)1);
- memcpy(socksreq + 2, &us_length, sizeof(short));
- }
- else {
- status = Curl_pSecFn->QueryContextAttributes(&sspi_context,
- SECPKG_ATTR_SIZES,
- &sspi_sizes);
- if(check_sspi_err(data, status, "QueryContextAttributes")) {
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- failf(data, "Failed to query security context attributes.");
- return CURLE_COULDNT_CONNECT;
- }
- sspi_w_token[0].cbBuffer = sspi_sizes.cbSecurityTrailer;
- sspi_w_token[0].BufferType = SECBUFFER_TOKEN;
- sspi_w_token[0].pvBuffer = malloc(sspi_sizes.cbSecurityTrailer);
- if(!sspi_w_token[0].pvBuffer) {
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- return CURLE_OUT_OF_MEMORY;
- }
- sspi_w_token[1].cbBuffer = 1;
- sspi_w_token[1].pvBuffer = malloc(1);
- if(!sspi_w_token[1].pvBuffer) {
- Curl_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- return CURLE_OUT_OF_MEMORY;
- }
- memcpy(sspi_w_token[1].pvBuffer, &gss_enc, 1);
- sspi_w_token[2].BufferType = SECBUFFER_PADDING;
- sspi_w_token[2].cbBuffer = sspi_sizes.cbBlockSize;
- sspi_w_token[2].pvBuffer = malloc(sspi_sizes.cbBlockSize);
- if(!sspi_w_token[2].pvBuffer) {
- Curl_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
- Curl_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- return CURLE_OUT_OF_MEMORY;
- }
- status = Curl_pSecFn->EncryptMessage(&sspi_context,
- KERB_WRAP_NO_ENCRYPT,
- &wrap_desc,
- 0);
- if(check_sspi_err(data, status, "EncryptMessage")) {
- Curl_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
- Curl_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
- Curl_pSecFn->FreeContextBuffer(sspi_w_token[2].pvBuffer);
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- failf(data, "Failed to query security context attributes.");
- return CURLE_COULDNT_CONNECT;
- }
- sspi_send_token.cbBuffer = sspi_w_token[0].cbBuffer
- + sspi_w_token[1].cbBuffer
- + sspi_w_token[2].cbBuffer;
- sspi_send_token.pvBuffer = malloc(sspi_send_token.cbBuffer);
- if(!sspi_send_token.pvBuffer) {
- Curl_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
- Curl_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
- Curl_pSecFn->FreeContextBuffer(sspi_w_token[2].pvBuffer);
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- return CURLE_OUT_OF_MEMORY;
- }
- memcpy(sspi_send_token.pvBuffer, sspi_w_token[0].pvBuffer,
- sspi_w_token[0].cbBuffer);
- memcpy((PUCHAR) sspi_send_token.pvBuffer +(int)sspi_w_token[0].cbBuffer,
- sspi_w_token[1].pvBuffer, sspi_w_token[1].cbBuffer);
- memcpy((PUCHAR) sspi_send_token.pvBuffer
- + sspi_w_token[0].cbBuffer
- + sspi_w_token[1].cbBuffer,
- sspi_w_token[2].pvBuffer, sspi_w_token[2].cbBuffer);
- Curl_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
- sspi_w_token[0].pvBuffer = NULL;
- sspi_w_token[0].cbBuffer = 0;
- Curl_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
- sspi_w_token[1].pvBuffer = NULL;
- sspi_w_token[1].cbBuffer = 0;
- Curl_pSecFn->FreeContextBuffer(sspi_w_token[2].pvBuffer);
- sspi_w_token[2].pvBuffer = NULL;
- sspi_w_token[2].cbBuffer = 0;
- us_length = htons((unsigned short)sspi_send_token.cbBuffer);
- memcpy(socksreq + 2, &us_length, sizeof(short));
- }
- written = Curl_conn_cf_send(cf->next, data, (char *)socksreq, 4, FALSE,
- &code);
- if(code || (4 != written)) {
- failf(data, "Failed to send SSPI encryption request.");
- if(sspi_send_token.pvBuffer)
- Curl_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- return CURLE_COULDNT_CONNECT;
- }
- if(data->set.socks5_gssapi_nec) {
- memcpy(socksreq, &gss_enc, 1);
- written = Curl_conn_cf_send(cf->next, data, (char *)socksreq, 1, FALSE,
- &code);
- if(code || (1 != written)) {
- failf(data, "Failed to send SSPI encryption type.");
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- return CURLE_COULDNT_CONNECT;
- }
- }
- else {
- written = Curl_conn_cf_send(cf->next, data,
- (char *)sspi_send_token.pvBuffer,
- sspi_send_token.cbBuffer, FALSE, &code);
- if(code || (sspi_send_token.cbBuffer != (size_t)written)) {
- failf(data, "Failed to send SSPI encryption type.");
- if(sspi_send_token.pvBuffer)
- Curl_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- return CURLE_COULDNT_CONNECT;
- }
- if(sspi_send_token.pvBuffer)
- Curl_pSecFn->FreeContextBuffer(sspi_send_token.pvBuffer);
- }
- result = Curl_blockread_all(cf, data, (char *)socksreq, 4, &actualread);
- if(result || (actualread != 4)) {
- failf(data, "Failed to receive SSPI encryption response.");
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- return CURLE_COULDNT_CONNECT;
- }
-
- if(socksreq[1] == 255) {
- failf(data, "User was rejected by the SOCKS5 server (%u %u).",
- (unsigned int)socksreq[0], (unsigned int)socksreq[1]);
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- return CURLE_COULDNT_CONNECT;
- }
- if(socksreq[1] != 2) {
- failf(data, "Invalid SSPI encryption response type (%u %u).",
- (unsigned int)socksreq[0], (unsigned int)socksreq[1]);
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- return CURLE_COULDNT_CONNECT;
- }
- memcpy(&us_length, socksreq + 2, sizeof(short));
- us_length = ntohs(us_length);
- sspi_w_token[0].cbBuffer = us_length;
- sspi_w_token[0].pvBuffer = malloc(us_length);
- if(!sspi_w_token[0].pvBuffer) {
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- return CURLE_OUT_OF_MEMORY;
- }
- result = Curl_blockread_all(cf, data, (char *)sspi_w_token[0].pvBuffer,
- sspi_w_token[0].cbBuffer, &actualread);
- if(result || (actualread != us_length)) {
- failf(data, "Failed to receive SSPI encryption type.");
- Curl_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- return CURLE_COULDNT_CONNECT;
- }
- if(!data->set.socks5_gssapi_nec) {
- wrap_desc.cBuffers = 2;
- sspi_w_token[0].BufferType = SECBUFFER_STREAM;
- sspi_w_token[1].BufferType = SECBUFFER_DATA;
- sspi_w_token[1].cbBuffer = 0;
- sspi_w_token[1].pvBuffer = NULL;
- status = Curl_pSecFn->DecryptMessage(&sspi_context,
- &wrap_desc,
- 0,
- &qop);
- if(check_sspi_err(data, status, "DecryptMessage")) {
- if(sspi_w_token[0].pvBuffer)
- Curl_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
- if(sspi_w_token[1].pvBuffer)
- Curl_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- failf(data, "Failed to query security context attributes.");
- return CURLE_COULDNT_CONNECT;
- }
- if(sspi_w_token[1].cbBuffer != 1) {
- failf(data, "Invalid SSPI encryption response length (%lu).",
- (unsigned long)sspi_w_token[1].cbBuffer);
- if(sspi_w_token[0].pvBuffer)
- Curl_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
- if(sspi_w_token[1].pvBuffer)
- Curl_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- return CURLE_COULDNT_CONNECT;
- }
- memcpy(socksreq, sspi_w_token[1].pvBuffer, sspi_w_token[1].cbBuffer);
- Curl_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
- Curl_pSecFn->FreeContextBuffer(sspi_w_token[1].pvBuffer);
- }
- else {
- if(sspi_w_token[0].cbBuffer != 1) {
- failf(data, "Invalid SSPI encryption response length (%lu).",
- (unsigned long)sspi_w_token[0].cbBuffer);
- Curl_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
- Curl_pSecFn->DeleteSecurityContext(&sspi_context);
- return CURLE_COULDNT_CONNECT;
- }
- memcpy(socksreq, sspi_w_token[0].pvBuffer, sspi_w_token[0].cbBuffer);
- Curl_pSecFn->FreeContextBuffer(sspi_w_token[0].pvBuffer);
- }
- (void)curlx_nonblock(sock, TRUE);
- infof(data, "SOCKS5 access with%s protection granted.",
- (socksreq[0] == 0) ? "out GSS-API data":
- ((socksreq[0] == 1) ? " GSS-API integrity" :
- " GSS-API confidentiality"));
-
- return CURLE_OK;
- }
- #endif
|