]> git.proxmox.com Git - libgit2.git/blobdiff - src/transports/winhttp.c
New upstream version 1.3.0+dfsg.1
[libgit2.git] / src / transports / winhttp.c
index 8e2bdd44fe0300fad3c22dea6ffa3e14f3050e9c..f4801a4516b6384d5231297acc2ea85187be3375 100644 (file)
@@ -5,6 +5,8 @@
  * a Linking Exception. For full terms see the included COPYING file.
  */
 
+#include "common.h"
+
 #ifdef GIT_WINHTTP
 
 #include "git2.h"
@@ -15,6 +17,8 @@
 #include "smart.h"
 #include "remote.h"
 #include "repository.h"
+#include "http.h"
+#include "git2/sys/credential.h"
 
 #include <wincrypt.h>
 #include <winhttp.h>
 #define WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH 0
 #endif
 
+#ifndef WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1
+# define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 0x00000200
+#endif
+
+#ifndef WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2
+# define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2 0x00000800
+#endif
+
+#ifndef WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3
+# define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3 0x00002000
+#endif
+
+#ifndef WINHTTP_NO_CLIENT_CERT_CONTEXT
+# define WINHTTP_NO_CLIENT_CERT_CONTEXT NULL
+#endif
+
+#ifndef HTTP_STATUS_PERMANENT_REDIRECT
+# define HTTP_STATUS_PERMANENT_REDIRECT 308
+#endif
+
+#ifndef DWORD_MAX
+# define DWORD_MAX 0xffffffff
+#endif
+
+bool git_http__expect_continue = false;
+
 static const char *prefix_https = "https://";
 static const char *upload_pack_service = "upload-pack";
 static const char *upload_pack_ls_service_url = "/info/refs?service=git-upload-pack";
@@ -52,17 +82,24 @@ static const int no_check_cert_flags = SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
        SECURITY_FLAG_IGNORE_UNKNOWN_CA;
 
 #if defined(__MINGW32__)
-const CLSID CLSID_InternetSecurityManager = { 0x7B8A2D94, 0x0AC9, 0x11D1,
+static const CLSID CLSID_InternetSecurityManager_mingw =
+       { 0x7B8A2D94, 0x0AC9, 0x11D1,
        { 0x89, 0x6C, 0x00, 0xC0, 0x4F, 0xB6, 0xBF, 0xC4 } };
-const IID IID_IInternetSecurityManager = { 0x79EAC9EE, 0xBAF9, 0x11CE,
+static const IID IID_IInternetSecurityManager_mingw =
+       { 0x79EAC9EE, 0xBAF9, 0x11CE,
        { 0x8C, 0x82, 0x00, 0xAA, 0x00, 0x4B, 0xA9, 0x0B } };
+
+# define CLSID_InternetSecurityManager CLSID_InternetSecurityManager_mingw
+# define IID_IInternetSecurityManager IID_IInternetSecurityManager_mingw
 #endif
 
 #define OWNING_SUBTRANSPORT(s) ((winhttp_subtransport *)(s)->parent.subtransport)
 
 typedef enum {
        GIT_WINHTTP_AUTH_BASIC = 1,
-       GIT_WINHTTP_AUTH_NEGOTIATE = 2,
+       GIT_WINHTTP_AUTH_NTLM = 2,
+       GIT_WINHTTP_AUTH_NEGOTIATE = 4,
+       GIT_WINHTTP_AUTH_DIGEST = 8,
 } winhttp_authmechanism_t;
 
 typedef struct {
@@ -78,104 +115,142 @@ typedef struct {
        DWORD post_body_len;
        unsigned sent_request : 1,
                received_response : 1,
-               chunked : 1;
+               chunked : 1,
+               status_sending_request_reached: 1;
 } winhttp_stream;
 
+typedef struct {
+       git_net_url url;
+       git_credential *cred;
+       int auth_mechanisms;
+       bool url_cred_presented;
+} winhttp_server;
+
 typedef struct {
        git_smart_subtransport parent;
        transport_smart *owner;
-       gitno_connection_data connection_data;
-       git_cred *cred;
-       git_cred *url_cred;
-       int auth_mechanism;
+
+       winhttp_server server;
+       winhttp_server proxy;
+
        HINTERNET session;
        HINTERNET connection;
 } winhttp_subtransport;
 
-static int apply_basic_credential(HINTERNET request, git_cred *cred)
+static int apply_userpass_credentials(HINTERNET request, DWORD target, int mechanisms, git_credential *cred)
 {
-       git_cred_userpass_plaintext *c = (git_cred_userpass_plaintext *)cred;
-       git_buf buf = GIT_BUF_INIT, raw = GIT_BUF_INIT;
-       wchar_t *wide = NULL;
-       int error = -1, wide_len;
-
-       git_buf_printf(&raw, "%s:%s", c->username, c->password);
+       git_credential_userpass_plaintext *c = (git_credential_userpass_plaintext *)cred;
+       wchar_t *user = NULL, *pass = NULL;
+       int user_len = 0, pass_len = 0, error = 0;
+       DWORD native_scheme;
+
+       if (mechanisms & GIT_WINHTTP_AUTH_NEGOTIATE) {
+               native_scheme = WINHTTP_AUTH_SCHEME_NEGOTIATE;
+       } else if (mechanisms & GIT_WINHTTP_AUTH_NTLM) {
+               native_scheme = WINHTTP_AUTH_SCHEME_NTLM;
+       } else if (mechanisms & GIT_WINHTTP_AUTH_DIGEST) {
+               native_scheme = WINHTTP_AUTH_SCHEME_DIGEST;
+       } else if (mechanisms & GIT_WINHTTP_AUTH_BASIC) {
+               native_scheme = WINHTTP_AUTH_SCHEME_BASIC;
+       } else {
+               git_error_set(GIT_ERROR_HTTP, "invalid authentication scheme");
+               error = GIT_EAUTH;
+               goto done;
+       }
 
-       if (git_buf_oom(&raw) ||
-               git_buf_puts(&buf, "Authorization: Basic ") < 0 ||
-               git_buf_encode_base64(&buf, git_buf_cstr(&raw), raw.size) < 0)
-               goto on_error;
+       if ((error = user_len = git__utf8_to_16_alloc(&user, c->username)) < 0)
+               goto done;
 
-       if ((wide_len = git__utf8_to_16_alloc(&wide, git_buf_cstr(&buf))) < 0) {
-               giterr_set(GITERR_OS, "Failed to convert string to wide form");
-               goto on_error;
-       }
+       if ((error = pass_len = git__utf8_to_16_alloc(&pass, c->password)) < 0)
+               goto done;
 
-       if (!WinHttpAddRequestHeaders(request, wide, (ULONG) -1L, WINHTTP_ADDREQ_FLAG_ADD)) {
-               giterr_set(GITERR_OS, "Failed to add a header to the request");
-               goto on_error;
+       if (!WinHttpSetCredentials(request, target, native_scheme, user, pass, NULL)) {
+               git_error_set(GIT_ERROR_OS, "failed to set credentials");
+               error = -1;
        }
 
-       error = 0;
-
-on_error:
-       /* We were dealing with plaintext passwords, so clean up after ourselves a bit. */
-       if (wide)
-               memset(wide, 0x0, wide_len * sizeof(wchar_t));
+done:
+       if (user_len > 0)
+               git__memzero(user, user_len * sizeof(wchar_t));
 
-       if (buf.size)
-               memset(buf.ptr, 0x0, buf.size);
+       if (pass_len > 0)
+               git__memzero(pass, pass_len * sizeof(wchar_t));
 
-       if (raw.size)
-               memset(raw.ptr, 0x0, raw.size);
+       git__free(user);
+       git__free(pass);
 
-       git__free(wide);
-       git_buf_free(&buf);
-       git_buf_free(&raw);
        return error;
 }
 
-static int apply_default_credentials(HINTERNET request)
+static int apply_default_credentials(HINTERNET request, DWORD target, int mechanisms)
 {
-       /* Either the caller explicitly requested that default credentials be passed,
-        * or our fallback credential callback was invoked and checked that the target
-        * URI was in the appropriate Internet Explorer security zone. By setting this
-        * flag, we guarantee that the credentials are delivered by WinHTTP. The default
-        * is "medium" which applies to the intranet and sounds like it would correspond
-        * to Internet Explorer security zones, but in fact does not. */
-       DWORD data = WINHTTP_AUTOLOGON_SECURITY_LEVEL_LOW;
-
-       if (!WinHttpSetOption(request, WINHTTP_OPTION_AUTOLOGON_POLICY, &data, sizeof(DWORD)))
+       DWORD autologon_level = WINHTTP_AUTOLOGON_SECURITY_LEVEL_LOW;
+       DWORD native_scheme = 0;
+
+       if ((mechanisms & GIT_WINHTTP_AUTH_NEGOTIATE) != 0) {
+               native_scheme = WINHTTP_AUTH_SCHEME_NEGOTIATE;
+       } else if ((mechanisms & GIT_WINHTTP_AUTH_NTLM) != 0) {
+               native_scheme = WINHTTP_AUTH_SCHEME_NTLM;
+       } else {
+               git_error_set(GIT_ERROR_HTTP, "invalid authentication scheme");
+               return GIT_EAUTH;
+       }
+
+       /*
+        * Autologon policy must be "low" to use default creds.
+        * This is safe as the user has explicitly requested it.
+        */
+       if (!WinHttpSetOption(request, WINHTTP_OPTION_AUTOLOGON_POLICY, &autologon_level, sizeof(DWORD))) {
+               git_error_set(GIT_ERROR_OS, "could not configure logon policy");
+               return -1;
+       }
+
+       if (!WinHttpSetCredentials(request, target, native_scheme, NULL, NULL, NULL)) {
+               git_error_set(GIT_ERROR_OS, "could not configure credentials");
                return -1;
+       }
 
        return 0;
 }
 
-static int fallback_cred_acquire_cb(
-       git_cred **cred,
-       const char *url,
-       const char *username_from_url,
+static int acquire_url_cred(
+       git_credential **cred,
        unsigned int allowed_types,
-       void *payload)
+       const char *username,
+       const char *password)
 {
-       int error = 1;
+       if (allowed_types & GIT_CREDENTIAL_USERPASS_PLAINTEXT)
+               return git_credential_userpass_plaintext_new(cred, username, password);
+
+       if ((allowed_types & GIT_CREDENTIAL_DEFAULT) && *username == '\0' && *password == '\0')
+               return git_credential_default_new(cred);
+
+       return 1;
+}
 
-       GIT_UNUSED(username_from_url);
-       GIT_UNUSED(payload);
+static int acquire_fallback_cred(
+       git_credential **cred,
+       const char *url,
+       unsigned int allowed_types)
+{
+       int error = 1;
 
        /* If the target URI supports integrated Windows authentication
         * as an authentication mechanism */
-       if (GIT_CREDTYPE_DEFAULT & allowed_types) {
+       if (GIT_CREDENTIAL_DEFAULT & allowed_types) {
                wchar_t *wide_url;
+               HRESULT hCoInitResult;
 
                /* Convert URL to wide characters */
                if (git__utf8_to_16_alloc(&wide_url, url) < 0) {
-                       giterr_set(GITERR_OS, "Failed to convert string to wide form");
+                       git_error_set(GIT_ERROR_OS, "failed to convert string to wide form");
                        return -1;
                }
 
-               if (SUCCEEDED(CoInitializeEx(NULL, COINIT_MULTITHREADED))) {
-                       IInternetSecurityManager* pISM;
+               hCoInitResult = CoInitializeEx(NULL, COINIT_MULTITHREADED);
+
+               if (SUCCEEDED(hCoInitResult) || hCoInitResult == RPC_E_CHANGED_MODE) {
+                       IInternetSecurityManager *pISM;
 
                        /* And if the target URI is in the My Computer, Intranet, or Trusted zones */
                        if (SUCCEEDED(CoCreateInstance(&CLSID_InternetSecurityManager, NULL,
@@ -186,19 +261,21 @@ static int fallback_cred_acquire_cb(
                                        (URLZONE_LOCAL_MACHINE == dwZone ||
                                        URLZONE_INTRANET == dwZone ||
                                        URLZONE_TRUSTED == dwZone)) {
-                                       git_cred *existing = *cred;
+                                       git_credential *existing = *cred;
 
                                        if (existing)
                                                existing->free(existing);
 
                                        /* Then use default Windows credentials to authenticate this request */
-                                       error = git_cred_default_new(cred);
+                                       error = git_credential_default_new(cred);
                                }
 
                                pISM->lpVtbl->Release(pISM);
                        }
 
-                       CoUninitialize();
+                       /* Only uninitialize if the call to CoInitializeEx was successful. */
+                       if (SUCCEEDED(hCoInitResult))
+                               CoUninitialize();
                }
 
                git__free(wide_url);
@@ -216,26 +293,33 @@ static int certificate_check(winhttp_stream *s, int valid)
        git_cert_x509 cert;
 
        /* If there is no override, we should fail if WinHTTP doesn't think it's fine */
-       if (t->owner->certificate_check_cb == NULL && !valid)
+       if (t->owner->certificate_check_cb == NULL && !valid) {
+               if (!git_error_last())
+                       git_error_set(GIT_ERROR_HTTP, "unknown certificate check failure");
+
                return GIT_ECERTIFICATE;
+       }
 
-       if (t->owner->certificate_check_cb == NULL || !t->connection_data.use_ssl)
+       if (t->owner->certificate_check_cb == NULL || git__strcmp(t->server.url.scheme, "https") != 0)
                return 0;
 
        if (!WinHttpQueryOption(s->request, WINHTTP_OPTION_SERVER_CERT_CONTEXT, &cert_ctx, &cert_ctx_size)) {
-               giterr_set(GITERR_OS, "failed to get server certificate");
+               git_error_set(GIT_ERROR_OS, "failed to get server certificate");
                return -1;
        }
 
-       giterr_clear();
+       git_error_clear();
        cert.parent.cert_type = GIT_CERT_X509;
        cert.data = cert_ctx->pbCertEncoded;
        cert.len = cert_ctx->cbCertEncoded;
-       error = t->owner->certificate_check_cb((git_cert *) &cert, valid, t->connection_data.host, t->owner->cred_acquire_payload);
+       error = t->owner->certificate_check_cb((git_cert *) &cert, valid, t->server.url.host, t->owner->message_cb_payload);
        CertFreeCertificateContext(cert_ctx);
 
-       if (error < 0 && !giterr_last())
-               giterr_set(GITERR_NET, "user cancelled certificate check");
+       if (error == GIT_PASSTHROUGH)
+               error = valid ? 0 : GIT_ECERTIFICATE;
+
+       if (error < 0 && !git_error_last())
+               git_error_set(GIT_ERROR_HTTP, "user cancelled certificate check");
 
        return error;
 }
@@ -265,6 +349,26 @@ static void winhttp_stream_close(winhttp_stream *s)
        s->sent_request = 0;
 }
 
+static int apply_credentials(
+       HINTERNET request,
+       git_net_url *url,
+       int target,
+       git_credential *creds,
+       int mechanisms)
+{
+       int error = 0;
+
+       GIT_UNUSED(url);
+
+       /* If we have creds, just apply them */
+       if (creds && creds->credtype == GIT_CREDENTIAL_USERPASS_PLAINTEXT)
+               error = apply_userpass_credentials(request, target, mechanisms, creds);
+       else if (creds && creds->credtype == GIT_CREDENTIAL_DEFAULT)
+               error = apply_default_credentials(request, target, mechanisms);
+
+       return error;
+}
+
 static int winhttp_stream_connect(winhttp_stream *s)
 {
        winhttp_subtransport *t = OWNING_SUBTRANSPORT(s);
@@ -277,17 +381,24 @@ static int winhttp_stream_connect(winhttp_stream *s)
        unsigned long disable_redirects = WINHTTP_DISABLE_REDIRECTS;
        int default_timeout = TIMEOUT_INFINITE;
        int default_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
-       int i;
+       DWORD autologon_policy = WINHTTP_AUTOLOGON_SECURITY_LEVEL_HIGH;
+
+       const char *service_url = s->service_url;
+       size_t i;
+       const git_proxy_options *proxy_opts;
 
+       /* If path already ends in /, remove the leading slash from service_url */
+       if ((git__suffixcmp(t->server.url.path, "/") == 0) && (git__prefixcmp(service_url, "/") == 0))
+               service_url++;
        /* Prepare URL */
-       git_buf_printf(&buf, "%s%s", t->connection_data.path, s->service_url);
+       git_buf_printf(&buf, "%s%s", t->server.url.path, service_url);
 
        if (git_buf_oom(&buf))
                return -1;
 
        /* Convert URL to wide characters */
        if (git__utf8_to_16_alloc(&s->request_uri, git_buf_cstr(&buf)) < 0) {
-               giterr_set(GITERR_OS, "Failed to convert string to wide form");
+               git_error_set(GIT_ERROR_OS, "failed to convert string to wide form");
                goto on_error;
        }
 
@@ -299,38 +410,73 @@ static int winhttp_stream_connect(winhttp_stream *s)
                        NULL,
                        WINHTTP_NO_REFERER,
                        types,
-                       t->connection_data.use_ssl ? WINHTTP_FLAG_SECURE : 0);
+                       git__strcmp(t->server.url.scheme, "https") == 0 ? WINHTTP_FLAG_SECURE : 0);
 
        if (!s->request) {
-               giterr_set(GITERR_OS, "Failed to open request");
+               git_error_set(GIT_ERROR_OS, "failed to open request");
                goto on_error;
        }
 
+       /* Never attempt default credentials; we'll provide them explicitly. */
+       if (!WinHttpSetOption(s->request, WINHTTP_OPTION_AUTOLOGON_POLICY, &autologon_policy, sizeof(DWORD)))
+               return -1;
+
        if (!WinHttpSetTimeouts(s->request, default_timeout, default_connect_timeout, default_timeout, default_timeout)) {
-               giterr_set(GITERR_OS, "Failed to set timeouts for WinHTTP");
+               git_error_set(GIT_ERROR_OS, "failed to set timeouts for WinHTTP");
                goto on_error;
        }
 
-       /* Set proxy if necessary */
-       if (git_remote__get_http_proxy(t->owner->owner, !!t->connection_data.use_ssl, &proxy_url) < 0)
-               goto on_error;
+       proxy_opts = &t->owner->proxy;
+       if (proxy_opts->type == GIT_PROXY_AUTO) {
+               /* Set proxy if necessary */
+               if (git_remote__http_proxy(&proxy_url, t->owner->owner, &t->server.url) < 0)
+                       goto on_error;
+       }
+       else if (proxy_opts->type == GIT_PROXY_SPECIFIED) {
+               proxy_url = git__strdup(proxy_opts->url);
+               GIT_ERROR_CHECK_ALLOC(proxy_url);
+       }
 
        if (proxy_url) {
+               git_buf processed_url = GIT_BUF_INIT;
                WINHTTP_PROXY_INFO proxy_info;
                wchar_t *proxy_wide;
 
-               /* Convert URL to wide characters */
-               int proxy_wide_len = git__utf8_to_16_alloc(&proxy_wide, proxy_url);
+               git_net_url_dispose(&t->proxy.url);
+
+               if ((error = git_net_url_parse(&t->proxy.url, proxy_url)) < 0)
+                       goto on_error;
+
+               if (strcmp(t->proxy.url.scheme, "http") != 0 && strcmp(t->proxy.url.scheme, "https") != 0) {
+                       git_error_set(GIT_ERROR_HTTP, "invalid URL: '%s'", proxy_url);
+                       error = -1;
+                       goto on_error;
+               }
+
+               git_buf_puts(&processed_url, t->proxy.url.scheme);
+               git_buf_PUTS(&processed_url, "://");
 
-               if (proxy_wide_len < 0) {
-                       giterr_set(GITERR_OS, "Failed to convert string to wide form");
+               if (git_net_url_is_ipv6(&t->proxy.url))
+                       git_buf_putc(&processed_url, '[');
+
+               git_buf_puts(&processed_url, t->proxy.url.host);
+
+               if (git_net_url_is_ipv6(&t->proxy.url))
+                       git_buf_putc(&processed_url, ']');
+
+               if (!git_net_url_is_default_port(&t->proxy.url))
+                       git_buf_printf(&processed_url, ":%s", t->proxy.url.port);
+
+               if (git_buf_oom(&processed_url)) {
+                       error = -1;
                        goto on_error;
                }
 
-               /* Strip any trailing forward slash on the proxy URL;
-                * WinHTTP doesn't like it if one is present */
-               if (proxy_wide_len > 1 && L'/' == proxy_wide[proxy_wide_len - 2])
-                       proxy_wide[proxy_wide_len - 2] = L'\0';
+               /* Convert URL to wide characters */
+               error = git__utf8_to_16_alloc(&proxy_wide, processed_url.ptr);
+               git_buf_dispose(&processed_url);
+               if (error < 0)
+                       goto on_error;
 
                proxy_info.dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
                proxy_info.lpszProxy = proxy_wide;
@@ -340,12 +486,15 @@ static int winhttp_stream_connect(winhttp_stream *s)
                        WINHTTP_OPTION_PROXY,
                        &proxy_info,
                        sizeof(WINHTTP_PROXY_INFO))) {
-                       giterr_set(GITERR_OS, "Failed to set proxy");
+                       git_error_set(GIT_ERROR_OS, "failed to set proxy");
                        git__free(proxy_wide);
                        goto on_error;
                }
 
                git__free(proxy_wide);
+
+               if ((error = apply_credentials(s->request, &t->proxy.url, WINHTTP_AUTH_TARGET_PROXY, t->proxy.cred, t->proxy.auth_mechanisms)) < 0)
+                       goto on_error;
        }
 
        /* Disable WinHTTP redirects so we can handle them manually. Why, you ask?
@@ -355,7 +504,8 @@ static int winhttp_stream_connect(winhttp_stream *s)
                WINHTTP_OPTION_DISABLE_FEATURE,
                &disable_redirects,
                sizeof(disable_redirects))) {
-                       giterr_set(GITERR_OS, "Failed to disable redirects");
+                       git_error_set(GIT_ERROR_OS, "failed to disable redirects");
+                       error = -1;
                        goto on_error;
        }
 
@@ -369,7 +519,7 @@ static int winhttp_stream_connect(winhttp_stream *s)
 
        /* Send Pragma: no-cache header */
        if (!WinHttpAddRequestHeaders(s->request, pragma_nocache, (ULONG) -1L, WINHTTP_ADDREQ_FLAG_ADD)) {
-               giterr_set(GITERR_OS, "Failed to add a header to the request");
+               git_error_set(GIT_ERROR_OS, "failed to add a header to the request");
                goto on_error;
        }
 
@@ -382,13 +532,13 @@ static int winhttp_stream_connect(winhttp_stream *s)
                        goto on_error;
 
                if (git__utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_buf_cstr(&buf)) < 0) {
-                       giterr_set(GITERR_OS, "Failed to convert content-type to wide characters");
+                       git_error_set(GIT_ERROR_OS, "failed to convert content-type to wide characters");
                        goto on_error;
                }
 
                if (!WinHttpAddRequestHeaders(s->request, ct, (ULONG)-1L,
                        WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE)) {
-                       giterr_set(GITERR_OS, "Failed to add a header to the request");
+                       git_error_set(GIT_ERROR_OS, "failed to add a header to the request");
                        goto on_error;
                }
 
@@ -399,64 +549,45 @@ static int winhttp_stream_connect(winhttp_stream *s)
                        goto on_error;
 
                if (git__utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_buf_cstr(&buf)) < 0) {
-                       giterr_set(GITERR_OS, "Failed to convert accept header to wide characters");
+                       git_error_set(GIT_ERROR_OS, "failed to convert accept header to wide characters");
                        goto on_error;
                }
 
                if (!WinHttpAddRequestHeaders(s->request, ct, (ULONG)-1L,
                        WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE)) {
-                       giterr_set(GITERR_OS, "Failed to add a header to the request");
+                       git_error_set(GIT_ERROR_OS, "failed to add a header to the request");
                        goto on_error;
                }
        }
 
-       if (t->owner->custom_headers) {
-               for (i = 0; i < t->owner->custom_headers->count; i++) {
+       for (i = 0; i < t->owner->custom_headers.count; i++) {
+               if (t->owner->custom_headers.strings[i]) {
                        git_buf_clear(&buf);
-                       git_buf_puts(&buf, t->owner->custom_headers->strings[i]);
+                       git_buf_puts(&buf, t->owner->custom_headers.strings[i]);
                        if (git__utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_buf_cstr(&buf)) < 0) {
-                               giterr_set(GITERR_OS, "Failed to convert custom header to wide characters");
+                               git_error_set(GIT_ERROR_OS, "failed to convert custom header to wide characters");
                                goto on_error;
                        }
 
                        if (!WinHttpAddRequestHeaders(s->request, ct, (ULONG)-1L,
                                WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE)) {
-                               giterr_set(GITERR_OS, "Failed to add a header to the request");
+                               git_error_set(GIT_ERROR_OS, "failed to add a header to the request");
                                goto on_error;
                        }
                }
        }
 
        /* If requested, disable certificate validation */
-       if (t->connection_data.use_ssl) {
+       if (strcmp(t->server.url.scheme, "https") == 0) {
                int flags;
 
                if (t->owner->parent.read_flags(&t->owner->parent, &flags) < 0)
                        goto on_error;
        }
 
-       /* If we have a credential on the subtransport, apply it to the request */
-       if (t->cred &&
-               t->cred->credtype == GIT_CREDTYPE_USERPASS_PLAINTEXT &&
-               t->auth_mechanism == GIT_WINHTTP_AUTH_BASIC &&
-               apply_basic_credential(s->request, t->cred) < 0)
-               goto on_error;
-       else if (t->cred &&
-               t->cred->credtype == GIT_CREDTYPE_DEFAULT &&
-               t->auth_mechanism == GIT_WINHTTP_AUTH_NEGOTIATE &&
-               apply_default_credentials(s->request) < 0)
+       if ((error = apply_credentials(s->request, &t->server.url, WINHTTP_AUTH_TARGET_SERVER, t->server.cred, t->server.auth_mechanisms)) < 0)
                goto on_error;
 
-       /* If no other credentials have been applied and the URL has username and
-        * password, use those */
-       if (!t->cred && t->connection_data.user && t->connection_data.pass) {
-               if (!t->url_cred &&
-                       git_cred_userpass_plaintext_new(&t->url_cred, t->connection_data.user, t->connection_data.pass) < 0)
-                       goto on_error;
-               if (apply_basic_credential(s->request, t->url_cred) < 0)
-                       goto on_error;
-       }
-
        /* We've done everything up to calling WinHttpSendRequest. */
 
        error = 0;
@@ -466,37 +597,47 @@ on_error:
                winhttp_stream_close(s);
 
        git__free(proxy_url);
-       git_buf_free(&buf);
+       git_buf_dispose(&buf);
        return error;
 }
 
 static int parse_unauthorized_response(
-       HINTERNET request,
        int *allowed_types,
-       int *auth_mechanism)
+       int *allowed_mechanisms,
+       HINTERNET request)
 {
        DWORD supported, first, target;
 
        *allowed_types = 0;
-       *auth_mechanism = 0;
+       *allowed_mechanisms = 0;
 
-       /* WinHttpQueryHeaders() must be called before WinHttpQueryAuthSchemes(). 
-        * We can assume this was already done, since we know we are unauthorized. 
+       /* WinHttpQueryHeaders() must be called before WinHttpQueryAuthSchemes().
+        * We can assume this was already done, since we know we are unauthorized.
         */
        if (!WinHttpQueryAuthSchemes(request, &supported, &first, &target)) {
-               giterr_set(GITERR_OS, "Failed to parse supported auth schemes"); 
-               return -1;
+               git_error_set(GIT_ERROR_OS, "failed to parse supported auth schemes");
+               return GIT_EAUTH;
+       }
+
+       if (WINHTTP_AUTH_SCHEME_NTLM & supported) {
+               *allowed_types |= GIT_CREDENTIAL_USERPASS_PLAINTEXT;
+               *allowed_types |= GIT_CREDENTIAL_DEFAULT;
+               *allowed_mechanisms |= GIT_WINHTTP_AUTH_NTLM;
+       }
+
+       if (WINHTTP_AUTH_SCHEME_NEGOTIATE & supported) {
+               *allowed_types |= GIT_CREDENTIAL_DEFAULT;
+               *allowed_mechanisms |= GIT_WINHTTP_AUTH_NEGOTIATE;
        }
 
        if (WINHTTP_AUTH_SCHEME_BASIC & supported) {
-               *allowed_types |= GIT_CREDTYPE_USERPASS_PLAINTEXT;
-               *auth_mechanism = GIT_WINHTTP_AUTH_BASIC;
+               *allowed_types |= GIT_CREDENTIAL_USERPASS_PLAINTEXT;
+               *allowed_mechanisms |= GIT_WINHTTP_AUTH_BASIC;
        }
 
-       if ((WINHTTP_AUTH_SCHEME_NTLM & supported) ||
-               (WINHTTP_AUTH_SCHEME_NEGOTIATE & supported)) {
-               *allowed_types |= GIT_CREDTYPE_DEFAULT;
-               *auth_mechanism = GIT_WINHTTP_AUTH_NEGOTIATE;
+       if (WINHTTP_AUTH_SCHEME_DIGEST & supported) {
+               *allowed_types |= GIT_CREDENTIAL_USERPASS_PLAINTEXT;
+               *allowed_mechanisms |= GIT_WINHTTP_AUTH_DIGEST;
        }
 
        return 0;
@@ -508,7 +649,7 @@ static int write_chunk(HINTERNET request, const char *buffer, size_t len)
        git_buf buf = GIT_BUF_INIT;
 
        /* Chunk header */
-       git_buf_printf(&buf, "%X\r\n", len);
+       git_buf_printf(&buf, "%"PRIXZ"\r\n", len);
 
        if (git_buf_oom(&buf))
                return -1;
@@ -516,18 +657,18 @@ static int write_chunk(HINTERNET request, const char *buffer, size_t len)
        if (!WinHttpWriteData(request,
                git_buf_cstr(&buf),     (DWORD)git_buf_len(&buf),
                &bytes_written)) {
-               git_buf_free(&buf);
-               giterr_set(GITERR_OS, "Failed to write chunk header");
+               git_buf_dispose(&buf);
+               git_error_set(GIT_ERROR_OS, "failed to write chunk header");
                return -1;
        }
 
-       git_buf_free(&buf);
+       git_buf_dispose(&buf);
 
        /* Chunk body */
        if (!WinHttpWriteData(request,
                buffer, (DWORD)len,
                &bytes_written)) {
-               giterr_set(GITERR_OS, "Failed to write chunk");
+               git_error_set(GIT_ERROR_OS, "failed to write chunk");
                return -1;
        }
 
@@ -535,7 +676,7 @@ static int write_chunk(HINTERNET request, const char *buffer, size_t len)
        if (!WinHttpWriteData(request,
                "\r\n", 2,
                &bytes_written)) {
-               giterr_set(GITERR_OS, "Failed to write chunk footer");
+               git_error_set(GIT_ERROR_OS, "failed to write chunk footer");
                return -1;
        }
 
@@ -548,7 +689,7 @@ static int winhttp_close_connection(winhttp_subtransport *t)
 
        if (t->connection) {
                if (!WinHttpCloseHandle(t->connection)) {
-                       giterr_set(GITERR_OS, "Unable to close connection");
+                       git_error_set(GIT_ERROR_OS, "unable to close connection");
                        ret = -1;
                }
 
@@ -557,7 +698,7 @@ static int winhttp_close_connection(winhttp_subtransport *t)
 
        if (t->session) {
                if (!WinHttpCloseHandle(t->session)) {
-                       giterr_set(GITERR_OS, "Unable to close session");
+                       git_error_set(GIT_ERROR_OS, "unable to close session");
                        ret = -1;
                }
 
@@ -567,48 +708,133 @@ static int winhttp_close_connection(winhttp_subtransport *t)
        return ret;
 }
 
+static void CALLBACK winhttp_status(
+       HINTERNET connection,
+       DWORD_PTR ctx,
+       DWORD code,
+       LPVOID info,
+       DWORD info_len)
+{
+       DWORD status;
+
+       GIT_UNUSED(connection);
+       GIT_UNUSED(info_len);
+
+       switch (code) {
+               case WINHTTP_CALLBACK_STATUS_SECURE_FAILURE:
+                       status = *((DWORD *)info);
+
+                       if ((status & WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID))
+                               git_error_set(GIT_ERROR_HTTP, "SSL certificate issued for different common name");
+                       else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_CERT_DATE_INVALID))
+                               git_error_set(GIT_ERROR_HTTP, "SSL certificate has expired");
+                       else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CA))
+                               git_error_set(GIT_ERROR_HTTP, "SSL certificate signed by unknown CA");
+                       else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CERT))
+                               git_error_set(GIT_ERROR_HTTP, "SSL certificate is invalid");
+                       else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_CERT_REV_FAILED))
+                               git_error_set(GIT_ERROR_HTTP, "certificate revocation check failed");
+                       else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_CERT_REVOKED))
+                               git_error_set(GIT_ERROR_HTTP, "SSL certificate was revoked");
+                       else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_SECURITY_CHANNEL_ERROR))
+                               git_error_set(GIT_ERROR_HTTP, "security libraries could not be loaded");
+                       else
+                               git_error_set(GIT_ERROR_HTTP, "unknown security error %lu", status);
+
+                       break;
+
+               case WINHTTP_CALLBACK_STATUS_SENDING_REQUEST:
+                       ((winhttp_stream *) ctx)->status_sending_request_reached = 1;
+
+                       break;
+       }
+}
+
 static int winhttp_connect(
        winhttp_subtransport *t)
 {
-       wchar_t *ua = L"git/1.0 (libgit2 " WIDEN(LIBGIT2_VERSION) L")";
-       wchar_t *wide_host;
+       wchar_t *wide_host = NULL;
        int32_t port;
+       wchar_t *wide_ua = NULL;
+       git_buf ipv6 = GIT_BUF_INIT, ua = GIT_BUF_INIT;
+       const char *host;
        int error = -1;
        int default_timeout = TIMEOUT_INFINITE;
        int default_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
+       DWORD protocols =
+               WINHTTP_FLAG_SECURE_PROTOCOL_TLS1 |
+               WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 |
+               WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2 |
+               WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3;
 
        t->session = NULL;
        t->connection = NULL;
 
        /* Prepare port */
-       if (git__strtol32(&port, t->connection_data.port, NULL, 10) < 0)
-               return -1;
+       if (git__strntol32(&port, t->server.url.port,
+                          strlen(t->server.url.port), NULL, 10) < 0)
+               goto on_error;
+
+       /* IPv6? Add braces around the host. */
+       if (git_net_url_is_ipv6(&t->server.url)) {
+               if (git_buf_printf(&ipv6, "[%s]", t->server.url.host) < 0)
+                       goto on_error;
+
+               host = ipv6.ptr;
+       } else {
+               host = t->server.url.host;
+       }
 
        /* Prepare host */
-       if (git__utf8_to_16_alloc(&wide_host, t->connection_data.host) < 0) {
-               giterr_set(GITERR_OS, "Unable to convert host to wide characters");
-               return -1;
+       if (git__utf8_to_16_alloc(&wide_host, host) < 0) {
+               git_error_set(GIT_ERROR_OS, "unable to convert host to wide characters");
+               goto on_error;
+       }
+
+
+       if (git_http__user_agent(&ua) < 0)
+               goto on_error;
+
+       if (git__utf8_to_16_alloc(&wide_ua, git_buf_cstr(&ua)) < 0) {
+               git_error_set(GIT_ERROR_OS, "unable to convert host to wide characters");
+               goto on_error;
        }
 
        /* Establish session */
        t->session = WinHttpOpen(
-               ua,
+               wide_ua,
                WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
                WINHTTP_NO_PROXY_NAME,
                WINHTTP_NO_PROXY_BYPASS,
                0);
 
        if (!t->session) {
-               giterr_set(GITERR_OS, "Failed to init WinHTTP");
+               git_error_set(GIT_ERROR_OS, "failed to init WinHTTP");
                goto on_error;
        }
 
+       /*
+        * Do a best-effort attempt to enable TLS 1.3 and 1.2 but allow this to
+        * fail; if TLS 1.2 or 1.3 support is not available for some reason,
+        * ignore the failure (it will keep the default protocols).
+        */
+       if (WinHttpSetOption(t->session,
+               WINHTTP_OPTION_SECURE_PROTOCOLS,
+               &protocols,
+               sizeof(protocols)) == FALSE) {
+               protocols &= ~WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3;
+               WinHttpSetOption(t->session,
+                       WINHTTP_OPTION_SECURE_PROTOCOLS,
+                       &protocols,
+                       sizeof(protocols));
+       }
+
        if (!WinHttpSetTimeouts(t->session, default_timeout, default_connect_timeout, default_timeout, default_timeout)) {
-               giterr_set(GITERR_OS, "Failed to set timeouts for WinHTTP");
+               git_error_set(GIT_ERROR_OS, "failed to set timeouts for WinHTTP");
                goto on_error;
        }
 
-       
+
        /* Establish connection */
        t->connection = WinHttpConnect(
                t->session,
@@ -617,7 +843,17 @@ static int winhttp_connect(
                0);
 
        if (!t->connection) {
-               giterr_set(GITERR_OS, "Failed to connect to host");
+               git_error_set(GIT_ERROR_OS, "failed to connect to host");
+               goto on_error;
+       }
+
+       if (WinHttpSetStatusCallback(
+                       t->connection,
+                       winhttp_status,
+                       WINHTTP_CALLBACK_FLAG_SECURE_FAILURE | WINHTTP_CALLBACK_FLAG_SEND_REQUEST,
+                       0
+               ) == WINHTTP_INVALID_STATUS_CALLBACK) {
+               git_error_set(GIT_ERROR_OS, "failed to set status callback");
                goto on_error;
        }
 
@@ -627,71 +863,164 @@ on_error:
        if (error < 0)
                winhttp_close_connection(t);
 
+       git_buf_dispose(&ua);
+       git_buf_dispose(&ipv6);
        git__free(wide_host);
+       git__free(wide_ua);
 
        return error;
 }
 
-static int do_send_request(winhttp_stream *s, size_t len, int ignore_length)
+static int do_send_request(winhttp_stream *s, size_t len, bool chunked)
 {
-       if (ignore_length) {
-               if (!WinHttpSendRequest(s->request,
-                       WINHTTP_NO_ADDITIONAL_HEADERS, 0,
-                       WINHTTP_NO_REQUEST_DATA, 0,
-                       WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH, 0)) {
-                       return -1;
-               }
-       } else {
-               if (!WinHttpSendRequest(s->request,
-                       WINHTTP_NO_ADDITIONAL_HEADERS, 0,
-                       WINHTTP_NO_REQUEST_DATA, 0,
-                       len, 0)) {
-                       return -1;
+       int attempts;
+       bool success;
+
+       if (len > DWORD_MAX) {
+               SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+               return -1;
+       }
+
+       for (attempts = 0; attempts < 5; attempts++) {
+               if (chunked) {
+                       success = WinHttpSendRequest(s->request,
+                               WINHTTP_NO_ADDITIONAL_HEADERS, 0,
+                               WINHTTP_NO_REQUEST_DATA, 0,
+                               WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH, (DWORD_PTR)s);
+               } else {
+                       success = WinHttpSendRequest(s->request,
+                               WINHTTP_NO_ADDITIONAL_HEADERS, 0,
+                               WINHTTP_NO_REQUEST_DATA, 0,
+                               (DWORD)len, (DWORD_PTR)s);
                }
+
+               if (success || GetLastError() != (DWORD)SEC_E_BUFFER_TOO_SMALL)
+                       break;
        }
 
-       return 0;
+       return success ? 0 : -1;
 }
 
-static int send_request(winhttp_stream *s, size_t len, int ignore_length)
+static int send_request(winhttp_stream *s, size_t len, bool chunked)
 {
-       int request_failed = 0, cert_valid = 1, error = 0;
-       DWORD ignore_flags;
+       int request_failed = 1, error, attempts = 0;
+       DWORD ignore_flags, send_request_error;
+
+       git_error_clear();
+
+       while (request_failed && attempts++ < 3) {
+               int cert_valid = 1;
+               int client_cert_requested = 0;
+               request_failed = 0;
+               if ((error = do_send_request(s, len, chunked)) < 0) {
+                       send_request_error = GetLastError();
+                       request_failed = 1;
+                       switch (send_request_error) {
+                               case ERROR_WINHTTP_SECURE_FAILURE:
+                                       cert_valid = 0;
+                                       break;
+                               case ERROR_WINHTTP_CLIENT_AUTH_CERT_NEEDED:
+                                       client_cert_requested = 1;
+                                       break;
+                               default:
+                                       git_error_set(GIT_ERROR_OS, "failed to send request");
+                                       return -1;
+                       }
+               }
 
-       if ((error = do_send_request(s, len, ignore_length)) < 0)
-               request_failed = 1;
+               /*
+                * Only check the certificate if we were able to reach the sending request phase, or
+                * received a secure failure error. Otherwise, the server certificate won't be available
+                * since the request wasn't able to complete (e.g. proxy auth required)
+                */
+               if (!cert_valid ||
+                       (!request_failed && s->status_sending_request_reached)) {
+                       git_error_clear();
+                       if ((error = certificate_check(s, cert_valid)) < 0) {
+                               if (!git_error_last())
+                                       git_error_set(GIT_ERROR_OS, "user cancelled certificate check");
 
-       if (request_failed) {
-               if (GetLastError() != ERROR_WINHTTP_SECURE_FAILURE) {
-                       giterr_set(GITERR_OS, "failed to send request");
-                       return -1;
-               } else {
-                       cert_valid = 0;
+                               return error;
+                       }
                }
-       }
 
-       giterr_clear();
-       if ((error = certificate_check(s, cert_valid)) < 0) {
-               if (!giterr_last())
-                       giterr_set(GITERR_OS, "user cancelled certificate check");
+               /* if neither the request nor the certificate check returned errors, we're done */
+               if (!request_failed)
+                       return 0;
 
-               return error;
+               if (!cert_valid) {
+                       ignore_flags = no_check_cert_flags;
+                       if (!WinHttpSetOption(s->request, WINHTTP_OPTION_SECURITY_FLAGS, &ignore_flags, sizeof(ignore_flags))) {
+                               git_error_set(GIT_ERROR_OS, "failed to set security options");
+                               return -1;
+                       }
+               }
+
+               if (client_cert_requested) {
+                       /*
+                        * Client certificates are not supported, explicitly tell the server that
+                        * (it's possible a client certificate was requested but is not required)
+                        */
+                       if (!WinHttpSetOption(s->request, WINHTTP_OPTION_CLIENT_CERT_CONTEXT, WINHTTP_NO_CLIENT_CERT_CONTEXT, 0)) {
+                               git_error_set(GIT_ERROR_OS, "failed to set client cert context");
+                               return -1;
+                       }
+               }
        }
 
-       /* if neither the request nor the certificate check returned errors, we're done */
-       if (!request_failed)
-               return 0;
+       return error;
+}
+
+static int acquire_credentials(
+       HINTERNET request,
+       winhttp_server *server,
+       const char *url_str,
+       git_credential_acquire_cb cred_cb,
+       void *cred_cb_payload)
+{
+       int allowed_types;
+       int error = 1;
 
-       ignore_flags = no_check_cert_flags;
-       
-       if (!WinHttpSetOption(s->request, WINHTTP_OPTION_SECURITY_FLAGS, &ignore_flags, sizeof(ignore_flags))) {
-               giterr_set(GITERR_OS, "failed to set security options");
+       if (parse_unauthorized_response(&allowed_types, &server->auth_mechanisms, request) < 0)
                return -1;
-       }
 
-       if ((error = do_send_request(s, len, ignore_length)) < 0)
-               giterr_set(GITERR_OS, "failed to send request");
+       if (allowed_types) {
+               git_credential_free(server->cred);
+               server->cred = NULL;
+
+               /* Start with URL-specified credentials, if there were any. */
+               if (!server->url_cred_presented && server->url.username && server->url.password) {
+                       error = acquire_url_cred(&server->cred, allowed_types, server->url.username, server->url.password);
+                       server->url_cred_presented = 1;
+
+                       if (error < 0)
+                               return error;
+               }
+
+               /* Next use the user-defined callback, if there is one. */
+               if (error > 0 && cred_cb) {
+                       error = cred_cb(&server->cred, url_str, server->url.username, allowed_types, cred_cb_payload);
+
+                       /* Treat GIT_PASSTHROUGH as though git_credential_acquire_cb isn't set */
+                       if (error == GIT_PASSTHROUGH)
+                               error = 1;
+                       else if (error < 0)
+                               return error;
+               }
+
+               /* Finally, invoke the fallback default credential lookup. */
+               if (error > 0) {
+                       error = acquire_fallback_cred(&server->cred, url_str, allowed_types);
 
+                       if (error < 0)
+                               return error;
+               }
+       }
+
+       /*
+        * No error occurred but we could not find appropriate credentials.
+        * This behaves like a pass-through.
+        */
        return error;
 }
 
@@ -709,9 +1038,9 @@ static int winhttp_stream_read(
 
 replay:
        /* Enforce a reasonable cap on the number of replays */
-       if (++replay_count >= 7) {
-               giterr_set(GITERR_NET, "Too many redirects or authentication replays");
-               return -1;
+       if (replay_count++ >= GIT_HTTP_REPLAY_MAX) {
+               git_error_set(GIT_ERROR_HTTP, "too many redirects or authentication replays");
+               return GIT_ERROR; /* not GIT_EAUTH because the exact cause is not clear */
        }
 
        /* Connect if necessary */
@@ -725,14 +1054,14 @@ replay:
 
                if (!s->sent_request) {
 
-                       if ((error = send_request(s, s->post_body_len, 0)) < 0)
+                       if ((error = send_request(s, s->post_body_len, false)) < 0)
                                return error;
 
                        s->sent_request = 1;
                }
 
                if (s->chunked) {
-                       assert(s->verb == post_verb);
+                       GIT_ASSERT(s->verb == post_verb);
 
                        /* Flush, if necessary */
                        if (s->chunk_buffer_len > 0 &&
@@ -745,7 +1074,7 @@ replay:
                        if (!WinHttpWriteData(s->request,
                                "0\r\n\r\n", 5,
                                &bytes_written)) {
-                               giterr_set(GITERR_OS, "Failed to write final chunk");
+                               git_error_set(GIT_ERROR_OS, "failed to write final chunk");
                                return -1;
                        }
                }
@@ -756,11 +1085,12 @@ replay:
                        if (INVALID_SET_FILE_POINTER == SetFilePointer(s->post_body,
                                        0, 0, FILE_BEGIN) &&
                                NO_ERROR != GetLastError()) {
-                               giterr_set(GITERR_OS, "Failed to reset file pointer");
+                               git_error_set(GIT_ERROR_OS, "failed to reset file pointer");
                                return -1;
                        }
 
                        buffer = git__malloc(CACHED_POST_BODY_BUF_SIZE);
+                       GIT_ERROR_CHECK_ALLOC(buffer);
 
                        while (len > 0) {
                                DWORD bytes_written;
@@ -770,19 +1100,19 @@ replay:
                                        &bytes_read, NULL) ||
                                        !bytes_read) {
                                        git__free(buffer);
-                                       giterr_set(GITERR_OS, "Failed to read from temp file");
+                                       git_error_set(GIT_ERROR_OS, "failed to read from temp file");
                                        return -1;
                                }
 
                                if (!WinHttpWriteData(s->request, buffer,
                                        bytes_read, &bytes_written)) {
                                        git__free(buffer);
-                                       giterr_set(GITERR_OS, "Failed to write data");
+                                       git_error_set(GIT_ERROR_OS, "failed to write data");
                                        return -1;
                                }
 
                                len -= bytes_read;
-                               assert(bytes_read == bytes_written);
+                               GIT_ASSERT(bytes_read == bytes_written);
                        }
 
                        git__free(buffer);
@@ -793,7 +1123,7 @@ replay:
                }
 
                if (!WinHttpReceiveResponse(s->request, 0)) {
-                       giterr_set(GITERR_OS, "Failed to receive response");
+                       git_error_set(GIT_ERROR_OS, "failed to receive response");
                        return -1;
                }
 
@@ -805,7 +1135,7 @@ replay:
                        WINHTTP_HEADER_NAME_BY_INDEX,
                        &status_code, &status_code_length,
                        WINHTTP_NO_HEADER_INDEX)) {
-                               giterr_set(GITERR_OS, "Failed to retrieve status code");
+                               git_error_set(GIT_ERROR_OS, "failed to retrieve status code");
                                return -1;
                }
 
@@ -819,7 +1149,8 @@ replay:
                         HTTP_STATUS_REDIRECT == status_code ||
                         (HTTP_STATUS_REDIRECT_METHOD == status_code &&
                          get_verb == s->verb) ||
-                        HTTP_STATUS_REDIRECT_KEEP_VERB == status_code)) {
+                        HTTP_STATUS_REDIRECT_KEEP_VERB == status_code ||
+                        HTTP_STATUS_PERMANENT_REDIRECT == status_code)) {
 
                        /* Check for Windows 7. This workaround is only necessary on
                         * Windows Vista and earlier. Windows 7 is version 6.1. */
@@ -835,12 +1166,12 @@ replay:
                                &location_length,
                                WINHTTP_NO_HEADER_INDEX) ||
                                GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
-                               giterr_set(GITERR_OS, "Failed to read Location header");
+                               git_error_set(GIT_ERROR_OS, "failed to read Location header");
                                return -1;
                        }
 
                        location = git__malloc(location_length);
-                       GITERR_CHECK_ALLOC(location);
+                       GIT_ERROR_CHECK_ALLOC(location);
 
                        if (!WinHttpQueryHeaders(s->request,
                                WINHTTP_QUERY_LOCATION,
@@ -848,14 +1179,14 @@ replay:
                                location,
                                &location_length,
                                WINHTTP_NO_HEADER_INDEX)) {
-                               giterr_set(GITERR_OS, "Failed to read Location header");
+                               git_error_set(GIT_ERROR_OS, "failed to read Location header");
                                git__free(location);
                                return -1;
                        }
 
                        /* Convert the Location header to UTF-8 */
                        if (git__utf16_to_8_alloc(&location8, location) < 0) {
-                               giterr_set(GITERR_OS, "Failed to convert Location header to UTF-8");
+                               git_error_set(GIT_ERROR_OS, "failed to convert Location header to UTF-8");
                                git__free(location);
                                return -1;
                        }
@@ -867,7 +1198,7 @@ replay:
 
                        if (!git__prefixcmp_icase(location8, prefix_https)) {
                                /* Upgrade to secure connection; disconnect and start over */
-                               if (gitno_connection_data_from_url(&t->connection_data, location8, s->service_url) < 0) {
+                               if (git_net_url_apply_redirect(&t->server.url, location8, s->service_url) < 0) {
                                        git__free(location8);
                                        return -1;
                                }
@@ -883,47 +1214,38 @@ replay:
                }
 
                /* Handle authentication failures */
-               if (HTTP_STATUS_DENIED == status_code && get_verb == s->verb) {
-                       int allowed_types;
-
-                       if (parse_unauthorized_response(s->request, &allowed_types, &t->auth_mechanism) < 0)
-                               return -1;
-
-                       if (allowed_types &&
-                               (!t->cred || 0 == (t->cred->credtype & allowed_types))) {
-                               int cred_error = 1;
-
-                               /* Start with the user-supplied credential callback, if present */
-                               if (t->owner->cred_acquire_cb) {
-                                       cred_error = t->owner->cred_acquire_cb(&t->cred, t->owner->url,
-                                               t->connection_data.user, allowed_types, t->owner->cred_acquire_payload);
-
-                                       if (cred_error < 0)
-                                               return cred_error;
-                               }
-
-                               /* Invoke the fallback credentials acquisition callback if necessary */
-                               if (cred_error > 0) {
-                                       cred_error = fallback_cred_acquire_cb(&t->cred, t->owner->url,
-                                               t->connection_data.user, allowed_types, NULL);
-
-                                       if (cred_error < 0)
-                                               return cred_error;
-                               }
-
-                               if (!cred_error) {
-                                       assert(t->cred);
-
-                                       winhttp_stream_close(s);
-
-                                       /* Successfully acquired a credential */
-                                       goto replay;
-                               }
+               if (status_code == HTTP_STATUS_DENIED) {
+                       int error = acquire_credentials(s->request,
+                               &t->server,
+                               t->owner->url,
+                               t->owner->cred_acquire_cb,
+                               t->owner->cred_acquire_payload);
+
+                       if (error < 0) {
+                               return error;
+                       } else if (!error) {
+                               GIT_ASSERT(t->server.cred);
+                               winhttp_stream_close(s);
+                               goto replay;
+                       }
+               } else if (status_code == HTTP_STATUS_PROXY_AUTH_REQ) {
+                       int error = acquire_credentials(s->request,
+                               &t->proxy,
+                               t->owner->proxy.url,
+                               t->owner->proxy.credentials,
+                               t->owner->proxy.payload);
+
+                       if (error < 0) {
+                               return error;
+                       } else if (!error) {
+                               GIT_ASSERT(t->proxy.cred);
+                               winhttp_stream_close(s);
+                               goto replay;
                        }
                }
 
                if (HTTP_STATUS_OK != status_code) {
-                       giterr_set(GITERR_NET, "Request failed with status code: %d", status_code);
+                       git_error_set(GIT_ERROR_HTTP, "request failed with status code: %lu", status_code);
                        return -1;
                }
 
@@ -934,7 +1256,7 @@ replay:
                        p_snprintf(expected_content_type_8, MAX_CONTENT_TYPE_LEN, "application/x-git-%s-advertisement", s->service);
 
                if (git__utf8_to_16(expected_content_type, MAX_CONTENT_TYPE_LEN, expected_content_type_8) < 0) {
-                       giterr_set(GITERR_OS, "Failed to convert expected content-type to wide characters");
+                       git_error_set(GIT_ERROR_OS, "failed to convert expected content-type to wide characters");
                        return -1;
                }
 
@@ -945,12 +1267,12 @@ replay:
                        WINHTTP_HEADER_NAME_BY_INDEX,
                        &content_type, &content_type_length,
                        WINHTTP_NO_HEADER_INDEX)) {
-                               giterr_set(GITERR_OS, "Failed to retrieve response content-type");
+                               git_error_set(GIT_ERROR_OS, "failed to retrieve response content-type");
                                return -1;
                }
 
                if (wcscmp(expected_content_type, content_type)) {
-                       giterr_set(GITERR_NET, "Received unexpected content-type");
+                       git_error_set(GIT_ERROR_HTTP, "received unexpected content-type");
                        return -1;
                }
 
@@ -962,7 +1284,7 @@ replay:
                (DWORD)buf_size,
                &dw_bytes_read))
        {
-               giterr_set(GITERR_OS, "Failed to read data");
+               git_error_set(GIT_ERROR_OS, "failed to read data");
                return -1;
        }
 
@@ -985,11 +1307,11 @@ static int winhttp_stream_write_single(
 
        /* This implementation of write permits only a single call. */
        if (s->sent_request) {
-               giterr_set(GITERR_NET, "Subtransport configured for only one write");
+               git_error_set(GIT_ERROR_HTTP, "subtransport configured for only one write");
                return -1;
        }
 
-       if ((error = send_request(s, len, 0)) < 0)
+       if ((error = send_request(s, len, false)) < 0)
                return error;
 
        s->sent_request = 1;
@@ -998,11 +1320,11 @@ static int winhttp_stream_write_single(
                        (LPCVOID)buffer,
                        (DWORD)len,
                        &bytes_written)) {
-               giterr_set(GITERR_OS, "Failed to write data");
+               git_error_set(GIT_ERROR_OS, "failed to write data");
                return -1;
        }
 
-       assert((DWORD)len == bytes_written);
+       GIT_ASSERT((DWORD)len == bytes_written);
 
        return 0;
 }
@@ -1016,12 +1338,12 @@ static int put_uuid_string(LPWSTR buffer, size_t buffer_len_cch)
        if (RPC_S_OK != status &&
                RPC_S_UUID_LOCAL_ONLY != status &&
                RPC_S_UUID_NO_ADDRESS != status) {
-               giterr_set(GITERR_NET, "Unable to generate name for temp file");
+               git_error_set(GIT_ERROR_HTTP, "unable to generate name for temp file");
                return -1;
        }
 
        if (buffer_len_cch < UUID_LENGTH_CCH + 1) {
-               giterr_set(GITERR_NET, "Buffer too small for name of temp file");
+               git_error_set(GIT_ERROR_HTTP, "buffer too small for name of temp file");
                return -1;
        }
 
@@ -1036,7 +1358,7 @@ static int put_uuid_string(LPWSTR buffer, size_t buffer_len_cch)
                uuid.Data4[4], uuid.Data4[5], uuid.Data4[6], uuid.Data4[7]);
 
        if (result < UUID_LENGTH_CCH) {
-               giterr_set(GITERR_OS, "Unable to generate name for temp file");
+               git_error_set(GIT_ERROR_OS, "unable to generate name for temp file");
                return -1;
        }
 
@@ -1048,7 +1370,7 @@ static int get_temp_file(LPWSTR buffer, DWORD buffer_len_cch)
        size_t len;
 
        if (!GetTempPathW(buffer_len_cch, buffer)) {
-               giterr_set(GITERR_OS, "Failed to get temp path");
+               git_error_set(GIT_ERROR_OS, "failed to get temp path");
                return -1;
        }
 
@@ -1091,17 +1413,17 @@ static int winhttp_stream_write_buffered(
 
                if (INVALID_HANDLE_VALUE == s->post_body) {
                        s->post_body = NULL;
-                       giterr_set(GITERR_OS, "Failed to create temporary file");
+                       git_error_set(GIT_ERROR_OS, "failed to create temporary file");
                        return -1;
                }
        }
 
        if (!WriteFile(s->post_body, buffer, (DWORD)len, &bytes_written, NULL)) {
-               giterr_set(GITERR_OS, "Failed to write to temporary file");
+               git_error_set(GIT_ERROR_OS, "failed to write to temporary file");
                return -1;
        }
 
-       assert((DWORD)len == bytes_written);
+       GIT_ASSERT((DWORD)len == bytes_written);
 
        s->post_body_len += bytes_written;
 
@@ -1124,11 +1446,11 @@ static int winhttp_stream_write_chunked(
                if (!WinHttpAddRequestHeaders(s->request,
                        transfer_encoding, (ULONG) -1L,
                        WINHTTP_ADDREQ_FLAG_ADD)) {
-                       giterr_set(GITERR_OS, "Failed to add a header to the request");
+                       git_error_set(GIT_ERROR_OS, "failed to add a header to the request");
                        return -1;
                }
 
-               if ((error = send_request(s, 0, 1)) < 0)
+               if ((error = send_request(s, 0, true)) < 0)
                        return error;
 
                s->sent_request = 1;
@@ -1151,8 +1473,10 @@ static int winhttp_stream_write_chunked(
                /* Append as much to the buffer as we can */
                int count = (int)min(CACHED_POST_BODY_BUF_SIZE - s->chunk_buffer_len, len);
 
-               if (!s->chunk_buffer)
+               if (!s->chunk_buffer) {
                        s->chunk_buffer = git__malloc(CACHED_POST_BODY_BUF_SIZE);
+                       GIT_ERROR_CHECK_ALLOC(s->chunk_buffer);
+               }
 
                memcpy(s->chunk_buffer + s->chunk_buffer_len, buffer, count);
                s->chunk_buffer_len += count;
@@ -1193,7 +1517,7 @@ static int winhttp_stream_alloc(winhttp_subtransport *t, winhttp_stream **stream
                return -1;
 
        s = git__calloc(1, sizeof(winhttp_stream));
-       GITERR_CHECK_ALLOC(s);
+       GIT_ERROR_CHECK_ALLOC(s);
 
        s->parent.subtransport = &t->parent;
        s->parent.read = winhttp_stream_read;
@@ -1277,7 +1601,7 @@ static int winhttp_action(
        int ret = -1;
 
        if (!t->connection)
-               if ((ret = gitno_connection_data_from_url(&t->connection_data, url, NULL)) < 0 ||
+               if ((ret = git_net_url_parse(&t->server.url, url)) < 0 ||
                         (ret = winhttp_connect(t)) < 0)
                        return ret;
 
@@ -1306,7 +1630,7 @@ static int winhttp_action(
                        break;
 
                default:
-                       assert(0);
+                       GIT_ASSERT(0);
        }
 
        if (!ret)
@@ -1319,17 +1643,17 @@ static int winhttp_close(git_smart_subtransport *subtransport)
 {
        winhttp_subtransport *t = (winhttp_subtransport *)subtransport;
 
-       gitno_connection_data_free_ptrs(&t->connection_data);
-       memset(&t->connection_data, 0x0, sizeof(gitno_connection_data));
+       git_net_url_dispose(&t->server.url);
+       git_net_url_dispose(&t->proxy.url);
 
-       if (t->cred) {
-               t->cred->free(t->cred);
-               t->cred = NULL;
+       if (t->server.cred) {
+               t->server.cred->free(t->server.cred);
+               t->server.cred = NULL;
        }
 
-       if (t->url_cred) {
-               t->url_cred->free(t->url_cred);
-               t->url_cred = NULL;
+       if (t->proxy.cred) {
+               t->proxy.cred->free(t->proxy.cred);
+               t->proxy.cred = NULL;
        }
 
        return winhttp_close_connection(t);
@@ -1354,7 +1678,7 @@ int git_smart_subtransport_http(git_smart_subtransport **out, git_transport *own
                return -1;
 
        t = git__calloc(1, sizeof(winhttp_subtransport));
-       GITERR_CHECK_ALLOC(t);
+       GIT_ERROR_CHECK_ALLOC(t);
 
        t->owner = (transport_smart *)owner;
        t->parent.action = winhttp_action;