1 /*
2  * Copyright (C) the libgit2 contributors. All rights reserved.
3  *
4  * This file is part of libgit2, distributed under the GNU GPL v2 with
5  * a Linking Exception. For full terms see the included COPYING file.
6  */
7 
8 #include "common.h"
9 
10 #ifdef GIT_WINHTTP
11 
12 #include "git2.h"
13 #include "git2/transport.h"
14 #include "buffer.h"
15 #include "posix.h"
16 #include "netops.h"
17 #include "smart.h"
18 #include "remote.h"
19 #include "repository.h"
20 #include "http.h"
21 #include "git2/sys/credential.h"
22 
23 #include <wincrypt.h>
24 #include <winhttp.h>
25 
26 /* For IInternetSecurityManager zone check */
27 #include <objbase.h>
28 #include <urlmon.h>
29 
30 #define WIDEN2(s) L ## s
31 #define WIDEN(s) WIDEN2(s)
32 
33 #define MAX_CONTENT_TYPE_LEN	100
34 #define WINHTTP_OPTION_PEERDIST_EXTENSION_STATE	109
35 #define CACHED_POST_BODY_BUF_SIZE	4096
36 #define UUID_LENGTH_CCH	32
37 #define TIMEOUT_INFINITE -1
38 #define DEFAULT_CONNECT_TIMEOUT 60000
39 #ifndef WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH
40 #define WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH 0
41 #endif
42 
43 #ifndef WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1
44 # define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 0x00000200
45 #endif
46 
47 #ifndef WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2
48 # define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2 0x00000800
49 #endif
50 
51 #ifndef WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3
52 # define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3 0x00002000
53 #endif
54 
55 #ifndef WINHTTP_NO_CLIENT_CERT_CONTEXT
56 # define WINHTTP_NO_CLIENT_CERT_CONTEXT NULL
57 #endif
58 
59 #ifndef HTTP_STATUS_PERMANENT_REDIRECT
60 # define HTTP_STATUS_PERMANENT_REDIRECT 308
61 #endif
62 
63 #ifndef DWORD_MAX
64 # define DWORD_MAX 0xffffffff
65 #endif
66 
67 bool git_http__expect_continue = false;
68 
69 static const char *prefix_https = "https://";
70 static const char *upload_pack_service = "upload-pack";
71 static const char *upload_pack_ls_service_url = "/info/refs?service=git-upload-pack";
72 static const char *upload_pack_service_url = "/git-upload-pack";
73 static const char *receive_pack_service = "receive-pack";
74 static const char *receive_pack_ls_service_url = "/info/refs?service=git-receive-pack";
75 static const char *receive_pack_service_url = "/git-receive-pack";
76 static const wchar_t *get_verb = L"GET";
77 static const wchar_t *post_verb = L"POST";
78 static const wchar_t *pragma_nocache = L"Pragma: no-cache";
79 static const wchar_t *transfer_encoding = L"Transfer-Encoding: chunked";
80 static const int no_check_cert_flags = SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
81 	SECURITY_FLAG_IGNORE_CERT_DATE_INVALID |
82 	SECURITY_FLAG_IGNORE_UNKNOWN_CA;
83 
84 #if defined(__MINGW32__)
85 static const CLSID CLSID_InternetSecurityManager_mingw =
86 	{ 0x7B8A2D94, 0x0AC9, 0x11D1,
87 	{ 0x89, 0x6C, 0x00, 0xC0, 0x4F, 0xB6, 0xBF, 0xC4 } };
88 static const IID IID_IInternetSecurityManager_mingw =
89 	{ 0x79EAC9EE, 0xBAF9, 0x11CE,
90 	{ 0x8C, 0x82, 0x00, 0xAA, 0x00, 0x4B, 0xA9, 0x0B } };
91 
92 # define CLSID_InternetSecurityManager CLSID_InternetSecurityManager_mingw
93 # define IID_IInternetSecurityManager IID_IInternetSecurityManager_mingw
94 #endif
95 
96 #define OWNING_SUBTRANSPORT(s) ((winhttp_subtransport *)(s)->parent.subtransport)
97 
98 typedef enum {
99 	GIT_WINHTTP_AUTH_BASIC = 1,
100 	GIT_WINHTTP_AUTH_NTLM = 2,
101 	GIT_WINHTTP_AUTH_NEGOTIATE = 4,
102 	GIT_WINHTTP_AUTH_DIGEST = 8,
103 } winhttp_authmechanism_t;
104 
105 typedef struct {
106 	git_smart_subtransport_stream parent;
107 	const char *service;
108 	const char *service_url;
109 	const wchar_t *verb;
110 	HINTERNET request;
111 	wchar_t *request_uri;
112 	char *chunk_buffer;
113 	unsigned chunk_buffer_len;
114 	HANDLE post_body;
115 	DWORD post_body_len;
116 	unsigned sent_request : 1,
117 		received_response : 1,
118 		chunked : 1,
119 		status_sending_request_reached: 1;
120 } winhttp_stream;
121 
122 typedef struct {
123 	git_net_url url;
124 	git_credential *cred;
125 	int auth_mechanisms;
126 	bool url_cred_presented;
127 } winhttp_server;
128 
129 typedef struct {
130 	git_smart_subtransport parent;
131 	transport_smart *owner;
132 
133 	winhttp_server server;
134 	winhttp_server proxy;
135 
136 	HINTERNET session;
137 	HINTERNET connection;
138 } winhttp_subtransport;
139 
apply_userpass_credentials(HINTERNET request,DWORD target,int mechanisms,git_credential * cred)140 static int apply_userpass_credentials(HINTERNET request, DWORD target, int mechanisms, git_credential *cred)
141 {
142 	git_credential_userpass_plaintext *c = (git_credential_userpass_plaintext *)cred;
143 	wchar_t *user = NULL, *pass = NULL;
144 	int user_len = 0, pass_len = 0, error = 0;
145 	DWORD native_scheme;
146 
147 	if (mechanisms & GIT_WINHTTP_AUTH_NEGOTIATE) {
148 		native_scheme = WINHTTP_AUTH_SCHEME_NEGOTIATE;
149 	} else if (mechanisms & GIT_WINHTTP_AUTH_NTLM) {
150 		native_scheme = WINHTTP_AUTH_SCHEME_NTLM;
151 	} else if (mechanisms & GIT_WINHTTP_AUTH_DIGEST) {
152 		native_scheme = WINHTTP_AUTH_SCHEME_DIGEST;
153 	} else if (mechanisms & GIT_WINHTTP_AUTH_BASIC) {
154 		native_scheme = WINHTTP_AUTH_SCHEME_BASIC;
155 	} else {
156 		git_error_set(GIT_ERROR_HTTP, "invalid authentication scheme");
157 		error = GIT_EAUTH;
158 		goto done;
159 	}
160 
161 	if ((error = user_len = git__utf8_to_16_alloc(&user, c->username)) < 0)
162 		goto done;
163 
164 	if ((error = pass_len = git__utf8_to_16_alloc(&pass, c->password)) < 0)
165 		goto done;
166 
167 	if (!WinHttpSetCredentials(request, target, native_scheme, user, pass, NULL)) {
168 		git_error_set(GIT_ERROR_OS, "failed to set credentials");
169 		error = -1;
170 	}
171 
172 done:
173 	if (user_len > 0)
174 		git__memzero(user, user_len * sizeof(wchar_t));
175 
176 	if (pass_len > 0)
177 		git__memzero(pass, pass_len * sizeof(wchar_t));
178 
179 	git__free(user);
180 	git__free(pass);
181 
182 	return error;
183 }
184 
apply_default_credentials(HINTERNET request,DWORD target,int mechanisms)185 static int apply_default_credentials(HINTERNET request, DWORD target, int mechanisms)
186 {
187 	DWORD autologon_level = WINHTTP_AUTOLOGON_SECURITY_LEVEL_LOW;
188 	DWORD native_scheme = 0;
189 
190 	if ((mechanisms & GIT_WINHTTP_AUTH_NEGOTIATE) != 0) {
191 		native_scheme = WINHTTP_AUTH_SCHEME_NEGOTIATE;
192 	} else if ((mechanisms & GIT_WINHTTP_AUTH_NTLM) != 0) {
193 		native_scheme = WINHTTP_AUTH_SCHEME_NTLM;
194 	} else {
195 		git_error_set(GIT_ERROR_HTTP, "invalid authentication scheme");
196 		return GIT_EAUTH;
197 	}
198 
199 	/*
200 	 * Autologon policy must be "low" to use default creds.
201 	 * This is safe as the user has explicitly requested it.
202 	 */
203 	if (!WinHttpSetOption(request, WINHTTP_OPTION_AUTOLOGON_POLICY, &autologon_level, sizeof(DWORD))) {
204 		git_error_set(GIT_ERROR_OS, "could not configure logon policy");
205 		return -1;
206 	}
207 
208 	if (!WinHttpSetCredentials(request, target, native_scheme, NULL, NULL, NULL)) {
209 		git_error_set(GIT_ERROR_OS, "could not configure credentials");
210 		return -1;
211 	}
212 
213 	return 0;
214 }
215 
acquire_url_cred(git_credential ** cred,unsigned int allowed_types,const char * username,const char * password)216 static int acquire_url_cred(
217 	git_credential **cred,
218 	unsigned int allowed_types,
219 	const char *username,
220 	const char *password)
221 {
222 	if (allowed_types & GIT_CREDENTIAL_USERPASS_PLAINTEXT)
223 		return git_credential_userpass_plaintext_new(cred, username, password);
224 
225 	if ((allowed_types & GIT_CREDENTIAL_DEFAULT) && *username == '\0' && *password == '\0')
226 		return git_credential_default_new(cred);
227 
228 	return 1;
229 }
230 
acquire_fallback_cred(git_credential ** cred,const char * url,unsigned int allowed_types)231 static int acquire_fallback_cred(
232 	git_credential **cred,
233 	const char *url,
234 	unsigned int allowed_types)
235 {
236 	int error = 1;
237 
238 	/* If the target URI supports integrated Windows authentication
239 	 * as an authentication mechanism */
240 	if (GIT_CREDENTIAL_DEFAULT & allowed_types) {
241 		wchar_t *wide_url;
242 		HRESULT hCoInitResult;
243 
244 		/* Convert URL to wide characters */
245 		if (git__utf8_to_16_alloc(&wide_url, url) < 0) {
246 			git_error_set(GIT_ERROR_OS, "failed to convert string to wide form");
247 			return -1;
248 		}
249 
250 		hCoInitResult = CoInitializeEx(NULL, COINIT_MULTITHREADED);
251 
252 		if (SUCCEEDED(hCoInitResult) || hCoInitResult == RPC_E_CHANGED_MODE) {
253 			IInternetSecurityManager* pISM;
254 
255 			/* And if the target URI is in the My Computer, Intranet, or Trusted zones */
256 			if (SUCCEEDED(CoCreateInstance(&CLSID_InternetSecurityManager, NULL,
257 				CLSCTX_ALL, &IID_IInternetSecurityManager, (void **)&pISM))) {
258 				DWORD dwZone;
259 
260 				if (SUCCEEDED(pISM->lpVtbl->MapUrlToZone(pISM, wide_url, &dwZone, 0)) &&
261 					(URLZONE_LOCAL_MACHINE == dwZone ||
262 					URLZONE_INTRANET == dwZone ||
263 					URLZONE_TRUSTED == dwZone)) {
264 					git_credential *existing = *cred;
265 
266 					if (existing)
267 						existing->free(existing);
268 
269 					/* Then use default Windows credentials to authenticate this request */
270 					error = git_credential_default_new(cred);
271 				}
272 
273 				pISM->lpVtbl->Release(pISM);
274 			}
275 
276 			/* Only uninitialize if the call to CoInitializeEx was successful. */
277 			if (SUCCEEDED(hCoInitResult))
278 				CoUninitialize();
279 		}
280 
281 		git__free(wide_url);
282 	}
283 
284 	return error;
285 }
286 
certificate_check(winhttp_stream * s,int valid)287 static int certificate_check(winhttp_stream *s, int valid)
288 {
289 	int error;
290 	winhttp_subtransport *t = OWNING_SUBTRANSPORT(s);
291 	PCERT_CONTEXT cert_ctx;
292 	DWORD cert_ctx_size = sizeof(cert_ctx);
293 	git_cert_x509 cert;
294 
295 	/* If there is no override, we should fail if WinHTTP doesn't think it's fine */
296 	if (t->owner->certificate_check_cb == NULL && !valid) {
297 		if (!git_error_last())
298 			git_error_set(GIT_ERROR_HTTP, "unknown certificate check failure");
299 
300 		return GIT_ECERTIFICATE;
301 	}
302 
303 	if (t->owner->certificate_check_cb == NULL || git__strcmp(t->server.url.scheme, "https") != 0)
304 		return 0;
305 
306 	if (!WinHttpQueryOption(s->request, WINHTTP_OPTION_SERVER_CERT_CONTEXT, &cert_ctx, &cert_ctx_size)) {
307 		git_error_set(GIT_ERROR_OS, "failed to get server certificate");
308 		return -1;
309 	}
310 
311 	git_error_clear();
312 	cert.parent.cert_type = GIT_CERT_X509;
313 	cert.data = cert_ctx->pbCertEncoded;
314 	cert.len = cert_ctx->cbCertEncoded;
315 	error = t->owner->certificate_check_cb((git_cert *) &cert, valid, t->server.url.host, t->owner->message_cb_payload);
316 	CertFreeCertificateContext(cert_ctx);
317 
318 	if (error == GIT_PASSTHROUGH)
319 		error = valid ? 0 : GIT_ECERTIFICATE;
320 
321 	if (error < 0 && !git_error_last())
322 		git_error_set(GIT_ERROR_HTTP, "user cancelled certificate check");
323 
324 	return error;
325 }
326 
winhttp_stream_close(winhttp_stream * s)327 static void winhttp_stream_close(winhttp_stream *s)
328 {
329 	if (s->chunk_buffer) {
330 		git__free(s->chunk_buffer);
331 		s->chunk_buffer = NULL;
332 	}
333 
334 	if (s->post_body) {
335 		CloseHandle(s->post_body);
336 		s->post_body = NULL;
337 	}
338 
339 	if (s->request_uri) {
340 		git__free(s->request_uri);
341 		s->request_uri = NULL;
342 	}
343 
344 	if (s->request) {
345 		WinHttpCloseHandle(s->request);
346 		s->request = NULL;
347 	}
348 
349 	s->sent_request = 0;
350 }
351 
apply_credentials(HINTERNET request,git_net_url * url,int target,git_credential * creds,int mechanisms)352 static int apply_credentials(
353 	HINTERNET request,
354 	git_net_url *url,
355 	int target,
356 	git_credential *creds,
357 	int mechanisms)
358 {
359 	int error = 0;
360 
361 	GIT_UNUSED(url);
362 
363 	/* If we have creds, just apply them */
364 	if (creds && creds->credtype == GIT_CREDENTIAL_USERPASS_PLAINTEXT)
365 		error = apply_userpass_credentials(request, target, mechanisms, creds);
366 	else if (creds && creds->credtype == GIT_CREDENTIAL_DEFAULT)
367 		error = apply_default_credentials(request, target, mechanisms);
368 
369 	return error;
370 }
371 
winhttp_stream_connect(winhttp_stream * s)372 static int winhttp_stream_connect(winhttp_stream *s)
373 {
374 	winhttp_subtransport *t = OWNING_SUBTRANSPORT(s);
375 	git_buf buf = GIT_BUF_INIT;
376 	char *proxy_url = NULL;
377 	wchar_t ct[MAX_CONTENT_TYPE_LEN];
378 	LPCWSTR types[] = { L"*/*", NULL };
379 	BOOL peerdist = FALSE;
380 	int error = -1;
381 	unsigned long disable_redirects = WINHTTP_DISABLE_REDIRECTS;
382 	int default_timeout = TIMEOUT_INFINITE;
383 	int default_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
384 	DWORD autologon_policy = WINHTTP_AUTOLOGON_SECURITY_LEVEL_HIGH;
385 
386 	const char *service_url = s->service_url;
387 	size_t i;
388 	const git_proxy_options *proxy_opts;
389 
390 	/* If path already ends in /, remove the leading slash from service_url */
391 	if ((git__suffixcmp(t->server.url.path, "/") == 0) && (git__prefixcmp(service_url, "/") == 0))
392 		service_url++;
393 	/* Prepare URL */
394 	git_buf_printf(&buf, "%s%s", t->server.url.path, service_url);
395 
396 	if (git_buf_oom(&buf))
397 		return -1;
398 
399 	/* Convert URL to wide characters */
400 	if (git__utf8_to_16_alloc(&s->request_uri, git_buf_cstr(&buf)) < 0) {
401 		git_error_set(GIT_ERROR_OS, "failed to convert string to wide form");
402 		goto on_error;
403 	}
404 
405 	/* Establish request */
406 	s->request = WinHttpOpenRequest(
407 			t->connection,
408 			s->verb,
409 			s->request_uri,
410 			NULL,
411 			WINHTTP_NO_REFERER,
412 			types,
413 			git__strcmp(t->server.url.scheme, "https") == 0 ? WINHTTP_FLAG_SECURE : 0);
414 
415 	if (!s->request) {
416 		git_error_set(GIT_ERROR_OS, "failed to open request");
417 		goto on_error;
418 	}
419 
420 	/* Never attempt default credentials; we'll provide them explicitly. */
421 	if (!WinHttpSetOption(s->request, WINHTTP_OPTION_AUTOLOGON_POLICY, &autologon_policy, sizeof(DWORD)))
422 		return -1;
423 
424 	if (!WinHttpSetTimeouts(s->request, default_timeout, default_connect_timeout, default_timeout, default_timeout)) {
425 		git_error_set(GIT_ERROR_OS, "failed to set timeouts for WinHTTP");
426 		goto on_error;
427 	}
428 
429 	proxy_opts = &t->owner->proxy;
430 	if (proxy_opts->type == GIT_PROXY_AUTO) {
431 		/* Set proxy if necessary */
432 		if (git_remote__http_proxy(&proxy_url, t->owner->owner, &t->server.url) < 0)
433 			goto on_error;
434 	}
435 	else if (proxy_opts->type == GIT_PROXY_SPECIFIED) {
436 		proxy_url = git__strdup(proxy_opts->url);
437 		GIT_ERROR_CHECK_ALLOC(proxy_url);
438 	}
439 
440 	if (proxy_url) {
441 		git_buf processed_url = GIT_BUF_INIT;
442 		WINHTTP_PROXY_INFO proxy_info;
443 		wchar_t *proxy_wide;
444 
445 		git_net_url_dispose(&t->proxy.url);
446 
447 		if ((error = git_net_url_parse(&t->proxy.url, proxy_url)) < 0)
448 			goto on_error;
449 
450 		if (strcmp(t->proxy.url.scheme, "http") != 0 && strcmp(t->proxy.url.scheme, "https") != 0) {
451 			git_error_set(GIT_ERROR_HTTP, "invalid URL: '%s'", proxy_url);
452 			error = -1;
453 			goto on_error;
454 		}
455 
456 		git_buf_puts(&processed_url, t->proxy.url.scheme);
457 		git_buf_PUTS(&processed_url, "://");
458 
459 		if (git_net_url_is_ipv6(&t->proxy.url))
460 			git_buf_putc(&processed_url, '[');
461 
462 		git_buf_puts(&processed_url, t->proxy.url.host);
463 
464 		if (git_net_url_is_ipv6(&t->proxy.url))
465 			git_buf_putc(&processed_url, ']');
466 
467 		if (!git_net_url_is_default_port(&t->proxy.url))
468 			git_buf_printf(&processed_url, ":%s", t->proxy.url.port);
469 
470 		if (git_buf_oom(&processed_url)) {
471 			error = -1;
472 			goto on_error;
473 		}
474 
475 		/* Convert URL to wide characters */
476 		error = git__utf8_to_16_alloc(&proxy_wide, processed_url.ptr);
477 		git_buf_dispose(&processed_url);
478 		if (error < 0)
479 			goto on_error;
480 
481 		proxy_info.dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
482 		proxy_info.lpszProxy = proxy_wide;
483 		proxy_info.lpszProxyBypass = NULL;
484 
485 		if (!WinHttpSetOption(s->request,
486 			WINHTTP_OPTION_PROXY,
487 			&proxy_info,
488 			sizeof(WINHTTP_PROXY_INFO))) {
489 			git_error_set(GIT_ERROR_OS, "failed to set proxy");
490 			git__free(proxy_wide);
491 			goto on_error;
492 		}
493 
494 		git__free(proxy_wide);
495 
496 		if ((error = apply_credentials(s->request, &t->proxy.url, WINHTTP_AUTH_TARGET_PROXY, t->proxy.cred, t->proxy.auth_mechanisms)) < 0)
497 			goto on_error;
498 	}
499 
500 	/* Disable WinHTTP redirects so we can handle them manually. Why, you ask?
501 	 * http://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/b2ff8879-ab9f-4218-8f09-16d25dff87ae
502 	 */
503 	if (!WinHttpSetOption(s->request,
504 		WINHTTP_OPTION_DISABLE_FEATURE,
505 		&disable_redirects,
506 		sizeof(disable_redirects))) {
507 			git_error_set(GIT_ERROR_OS, "failed to disable redirects");
508 			error = -1;
509 			goto on_error;
510 	}
511 
512 	/* Strip unwanted headers (X-P2P-PeerDist, X-P2P-PeerDistEx) that WinHTTP
513 	 * adds itself. This option may not be supported by the underlying
514 	 * platform, so we do not error-check it */
515 	WinHttpSetOption(s->request,
516 		WINHTTP_OPTION_PEERDIST_EXTENSION_STATE,
517 		&peerdist,
518 		sizeof(peerdist));
519 
520 	/* Send Pragma: no-cache header */
521 	if (!WinHttpAddRequestHeaders(s->request, pragma_nocache, (ULONG) -1L, WINHTTP_ADDREQ_FLAG_ADD)) {
522 		git_error_set(GIT_ERROR_OS, "failed to add a header to the request");
523 		goto on_error;
524 	}
525 
526 	if (post_verb == s->verb) {
527 		/* Send Content-Type and Accept headers -- only necessary on a POST */
528 		git_buf_clear(&buf);
529 		if (git_buf_printf(&buf,
530 			"Content-Type: application/x-git-%s-request",
531 			s->service) < 0)
532 			goto on_error;
533 
534 		if (git__utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_buf_cstr(&buf)) < 0) {
535 			git_error_set(GIT_ERROR_OS, "failed to convert content-type to wide characters");
536 			goto on_error;
537 		}
538 
539 		if (!WinHttpAddRequestHeaders(s->request, ct, (ULONG)-1L,
540 			WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE)) {
541 			git_error_set(GIT_ERROR_OS, "failed to add a header to the request");
542 			goto on_error;
543 		}
544 
545 		git_buf_clear(&buf);
546 		if (git_buf_printf(&buf,
547 			"Accept: application/x-git-%s-result",
548 			s->service) < 0)
549 			goto on_error;
550 
551 		if (git__utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_buf_cstr(&buf)) < 0) {
552 			git_error_set(GIT_ERROR_OS, "failed to convert accept header to wide characters");
553 			goto on_error;
554 		}
555 
556 		if (!WinHttpAddRequestHeaders(s->request, ct, (ULONG)-1L,
557 			WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE)) {
558 			git_error_set(GIT_ERROR_OS, "failed to add a header to the request");
559 			goto on_error;
560 		}
561 	}
562 
563 	for (i = 0; i < t->owner->custom_headers.count; i++) {
564 		if (t->owner->custom_headers.strings[i]) {
565 			git_buf_clear(&buf);
566 			git_buf_puts(&buf, t->owner->custom_headers.strings[i]);
567 			if (git__utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_buf_cstr(&buf)) < 0) {
568 				git_error_set(GIT_ERROR_OS, "failed to convert custom header to wide characters");
569 				goto on_error;
570 			}
571 
572 			if (!WinHttpAddRequestHeaders(s->request, ct, (ULONG)-1L,
573 				WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE)) {
574 				git_error_set(GIT_ERROR_OS, "failed to add a header to the request");
575 				goto on_error;
576 			}
577 		}
578 	}
579 
580 	/* If requested, disable certificate validation */
581 	if (strcmp(t->server.url.scheme, "https") == 0) {
582 		int flags;
583 
584 		if (t->owner->parent.read_flags(&t->owner->parent, &flags) < 0)
585 			goto on_error;
586 	}
587 
588 	if ((error = apply_credentials(s->request, &t->server.url, WINHTTP_AUTH_TARGET_SERVER, t->server.cred, t->server.auth_mechanisms)) < 0)
589 		goto on_error;
590 
591 	/* We've done everything up to calling WinHttpSendRequest. */
592 
593 	error = 0;
594 
595 on_error:
596 	if (error < 0)
597 		winhttp_stream_close(s);
598 
599 	git__free(proxy_url);
600 	git_buf_dispose(&buf);
601 	return error;
602 }
603 
parse_unauthorized_response(int * allowed_types,int * allowed_mechanisms,HINTERNET request)604 static int parse_unauthorized_response(
605 	int *allowed_types,
606 	int *allowed_mechanisms,
607 	HINTERNET request)
608 {
609 	DWORD supported, first, target;
610 
611 	*allowed_types = 0;
612 	*allowed_mechanisms = 0;
613 
614 	/* WinHttpQueryHeaders() must be called before WinHttpQueryAuthSchemes().
615 	 * We can assume this was already done, since we know we are unauthorized.
616 	 */
617 	if (!WinHttpQueryAuthSchemes(request, &supported, &first, &target)) {
618 		git_error_set(GIT_ERROR_OS, "failed to parse supported auth schemes");
619 		return GIT_EAUTH;
620 	}
621 
622 	if (WINHTTP_AUTH_SCHEME_NTLM & supported) {
623 		*allowed_types |= GIT_CREDENTIAL_USERPASS_PLAINTEXT;
624 		*allowed_types |= GIT_CREDENTIAL_DEFAULT;
625 		*allowed_mechanisms |= GIT_WINHTTP_AUTH_NTLM;
626 	}
627 
628 	if (WINHTTP_AUTH_SCHEME_NEGOTIATE & supported) {
629 		*allowed_types |= GIT_CREDENTIAL_DEFAULT;
630 		*allowed_mechanisms |= GIT_WINHTTP_AUTH_NEGOTIATE;
631 	}
632 
633 	if (WINHTTP_AUTH_SCHEME_BASIC & supported) {
634 		*allowed_types |= GIT_CREDENTIAL_USERPASS_PLAINTEXT;
635 		*allowed_mechanisms |= GIT_WINHTTP_AUTH_BASIC;
636 	}
637 
638 	if (WINHTTP_AUTH_SCHEME_DIGEST & supported) {
639 		*allowed_types |= GIT_CREDENTIAL_USERPASS_PLAINTEXT;
640 		*allowed_mechanisms |= GIT_WINHTTP_AUTH_DIGEST;
641 	}
642 
643 	return 0;
644 }
645 
write_chunk(HINTERNET request,const char * buffer,size_t len)646 static int write_chunk(HINTERNET request, const char *buffer, size_t len)
647 {
648 	DWORD bytes_written;
649 	git_buf buf = GIT_BUF_INIT;
650 
651 	/* Chunk header */
652 	git_buf_printf(&buf, "%"PRIXZ"\r\n", len);
653 
654 	if (git_buf_oom(&buf))
655 		return -1;
656 
657 	if (!WinHttpWriteData(request,
658 		git_buf_cstr(&buf),	(DWORD)git_buf_len(&buf),
659 		&bytes_written)) {
660 		git_buf_dispose(&buf);
661 		git_error_set(GIT_ERROR_OS, "failed to write chunk header");
662 		return -1;
663 	}
664 
665 	git_buf_dispose(&buf);
666 
667 	/* Chunk body */
668 	if (!WinHttpWriteData(request,
669 		buffer, (DWORD)len,
670 		&bytes_written)) {
671 		git_error_set(GIT_ERROR_OS, "failed to write chunk");
672 		return -1;
673 	}
674 
675 	/* Chunk footer */
676 	if (!WinHttpWriteData(request,
677 		"\r\n", 2,
678 		&bytes_written)) {
679 		git_error_set(GIT_ERROR_OS, "failed to write chunk footer");
680 		return -1;
681 	}
682 
683 	return 0;
684 }
685 
winhttp_close_connection(winhttp_subtransport * t)686 static int winhttp_close_connection(winhttp_subtransport *t)
687 {
688 	int ret = 0;
689 
690 	if (t->connection) {
691 		if (!WinHttpCloseHandle(t->connection)) {
692 			git_error_set(GIT_ERROR_OS, "unable to close connection");
693 			ret = -1;
694 		}
695 
696 		t->connection = NULL;
697 	}
698 
699 	if (t->session) {
700 		if (!WinHttpCloseHandle(t->session)) {
701 			git_error_set(GIT_ERROR_OS, "unable to close session");
702 			ret = -1;
703 		}
704 
705 		t->session = NULL;
706 	}
707 
708 	return ret;
709 }
710 
winhttp_status(HINTERNET connection,DWORD_PTR ctx,DWORD code,LPVOID info,DWORD info_len)711 static void CALLBACK winhttp_status(
712 	HINTERNET connection,
713 	DWORD_PTR ctx,
714 	DWORD code,
715 	LPVOID info,
716 	DWORD info_len)
717 {
718 	DWORD status;
719 
720 	GIT_UNUSED(connection);
721 	GIT_UNUSED(info_len);
722 
723 	switch (code) {
724 		case WINHTTP_CALLBACK_STATUS_SECURE_FAILURE:
725 			status = *((DWORD *)info);
726 
727 			if ((status & WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID))
728 				git_error_set(GIT_ERROR_HTTP, "SSL certificate issued for different common name");
729 			else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_CERT_DATE_INVALID))
730 				git_error_set(GIT_ERROR_HTTP, "SSL certificate has expired");
731 			else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CA))
732 				git_error_set(GIT_ERROR_HTTP, "SSL certificate signed by unknown CA");
733 			else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CERT))
734 				git_error_set(GIT_ERROR_HTTP, "SSL certificate is invalid");
735 			else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_CERT_REV_FAILED))
736 				git_error_set(GIT_ERROR_HTTP, "certificate revocation check failed");
737 			else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_CERT_REVOKED))
738 				git_error_set(GIT_ERROR_HTTP, "SSL certificate was revoked");
739 			else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_SECURITY_CHANNEL_ERROR))
740 				git_error_set(GIT_ERROR_HTTP, "security libraries could not be loaded");
741 			else
742 				git_error_set(GIT_ERROR_HTTP, "unknown security error %lu", status);
743 
744 			break;
745 
746 		case WINHTTP_CALLBACK_STATUS_SENDING_REQUEST:
747 			((winhttp_stream *) ctx)->status_sending_request_reached = 1;
748 
749 			break;
750 	}
751 }
752 
winhttp_connect(winhttp_subtransport * t)753 static int winhttp_connect(
754 	winhttp_subtransport *t)
755 {
756 	wchar_t *wide_host = NULL;
757 	int32_t port;
758 	wchar_t *wide_ua = NULL;
759 	git_buf ipv6 = GIT_BUF_INIT, ua = GIT_BUF_INIT;
760 	const char *host;
761 	int error = -1;
762 	int default_timeout = TIMEOUT_INFINITE;
763 	int default_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
764 	DWORD protocols =
765 		WINHTTP_FLAG_SECURE_PROTOCOL_TLS1 |
766 		WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 |
767 		WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2 |
768 		WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3;
769 
770 	t->session = NULL;
771 	t->connection = NULL;
772 
773 	/* Prepare port */
774 	if (git__strntol32(&port, t->server.url.port,
775 			   strlen(t->server.url.port), NULL, 10) < 0)
776 		goto on_error;
777 
778 	/* IPv6? Add braces around the host. */
779 	if (git_net_url_is_ipv6(&t->server.url)) {
780 		if (git_buf_printf(&ipv6, "[%s]", t->server.url.host) < 0)
781 			goto on_error;
782 
783 		host = ipv6.ptr;
784 	} else {
785 		host = t->server.url.host;
786 	}
787 
788 	/* Prepare host */
789 	if (git__utf8_to_16_alloc(&wide_host, host) < 0) {
790 		git_error_set(GIT_ERROR_OS, "unable to convert host to wide characters");
791 		goto on_error;
792 	}
793 
794 
795 	if (git_http__user_agent(&ua) < 0)
796 		goto on_error;
797 
798 	if (git__utf8_to_16_alloc(&wide_ua, git_buf_cstr(&ua)) < 0) {
799 		git_error_set(GIT_ERROR_OS, "unable to convert host to wide characters");
800 		goto on_error;
801 	}
802 
803 	/* Establish session */
804 	t->session = WinHttpOpen(
805 		wide_ua,
806 		WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
807 		WINHTTP_NO_PROXY_NAME,
808 		WINHTTP_NO_PROXY_BYPASS,
809 		0);
810 
811 	if (!t->session) {
812 		git_error_set(GIT_ERROR_OS, "failed to init WinHTTP");
813 		goto on_error;
814 	}
815 
816 	/*
817 	 * Do a best-effort attempt to enable TLS 1.3 and 1.2 but allow this to
818 	 * fail; if TLS 1.2 or 1.3 support is not available for some reason,
819 	 * ignore the failure (it will keep the default protocols).
820 	 */
821 	if (WinHttpSetOption(t->session,
822 		WINHTTP_OPTION_SECURE_PROTOCOLS,
823 		&protocols,
824 		sizeof(protocols)) == FALSE) {
825 		protocols &= ~WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3;
826 		WinHttpSetOption(t->session,
827 			WINHTTP_OPTION_SECURE_PROTOCOLS,
828 			&protocols,
829 			sizeof(protocols));
830 	}
831 
832 	if (!WinHttpSetTimeouts(t->session, default_timeout, default_connect_timeout, default_timeout, default_timeout)) {
833 		git_error_set(GIT_ERROR_OS, "failed to set timeouts for WinHTTP");
834 		goto on_error;
835 	}
836 
837 
838 	/* Establish connection */
839 	t->connection = WinHttpConnect(
840 		t->session,
841 		wide_host,
842 		(INTERNET_PORT) port,
843 		0);
844 
845 	if (!t->connection) {
846 		git_error_set(GIT_ERROR_OS, "failed to connect to host");
847 		goto on_error;
848 	}
849 
850 	if (WinHttpSetStatusCallback(
851 			t->connection,
852 			winhttp_status,
853 			WINHTTP_CALLBACK_FLAG_SECURE_FAILURE | WINHTTP_CALLBACK_FLAG_SEND_REQUEST,
854 			0
855 		) == WINHTTP_INVALID_STATUS_CALLBACK) {
856 		git_error_set(GIT_ERROR_OS, "failed to set status callback");
857 		goto on_error;
858 	}
859 
860 	error = 0;
861 
862 on_error:
863 	if (error < 0)
864 		winhttp_close_connection(t);
865 
866 	git_buf_dispose(&ua);
867 	git_buf_dispose(&ipv6);
868 	git__free(wide_host);
869 	git__free(wide_ua);
870 
871 	return error;
872 }
873 
do_send_request(winhttp_stream * s,size_t len,bool chunked)874 static int do_send_request(winhttp_stream *s, size_t len, bool chunked)
875 {
876 	int attempts;
877 	bool success;
878 
879 	if (len > DWORD_MAX) {
880 		SetLastError(ERROR_NOT_ENOUGH_MEMORY);
881 		return -1;
882 	}
883 
884 	for (attempts = 0; attempts < 5; attempts++) {
885 		if (chunked) {
886 			success = WinHttpSendRequest(s->request,
887 				WINHTTP_NO_ADDITIONAL_HEADERS, 0,
888 				WINHTTP_NO_REQUEST_DATA, 0,
889 				WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH, (DWORD_PTR)s);
890 		} else {
891 			success = WinHttpSendRequest(s->request,
892 				WINHTTP_NO_ADDITIONAL_HEADERS, 0,
893 				WINHTTP_NO_REQUEST_DATA, 0,
894 				(DWORD)len, (DWORD_PTR)s);
895 		}
896 
897 		if (success || GetLastError() != (DWORD)SEC_E_BUFFER_TOO_SMALL)
898 			break;
899 	}
900 
901 	return success ? 0 : -1;
902 }
903 
send_request(winhttp_stream * s,size_t len,bool chunked)904 static int send_request(winhttp_stream *s, size_t len, bool chunked)
905 {
906 	int request_failed = 1, error, attempts = 0;
907 	DWORD ignore_flags, send_request_error;
908 
909 	git_error_clear();
910 
911 	while (request_failed && attempts++ < 3) {
912 		int cert_valid = 1;
913 		int client_cert_requested = 0;
914 		request_failed = 0;
915 		if ((error = do_send_request(s, len, chunked)) < 0) {
916 			send_request_error = GetLastError();
917 			request_failed = 1;
918 			switch (send_request_error) {
919 				case ERROR_WINHTTP_SECURE_FAILURE:
920 					cert_valid = 0;
921 					break;
922 				case ERROR_WINHTTP_CLIENT_AUTH_CERT_NEEDED:
923 					client_cert_requested = 1;
924 					break;
925 				default:
926 					git_error_set(GIT_ERROR_OS, "failed to send request");
927 					return -1;
928 			}
929 		}
930 
931 		/*
932 		 * Only check the certificate if we were able to reach the sending request phase, or
933 		 * received a secure failure error. Otherwise, the server certificate won't be available
934 		 * since the request wasn't able to complete (e.g. proxy auth required)
935 		 */
936 		if (!cert_valid ||
937 			(!request_failed && s->status_sending_request_reached)) {
938 			git_error_clear();
939 			if ((error = certificate_check(s, cert_valid)) < 0) {
940 				if (!git_error_last())
941 					git_error_set(GIT_ERROR_OS, "user cancelled certificate check");
942 
943 				return error;
944 			}
945 		}
946 
947 		/* if neither the request nor the certificate check returned errors, we're done */
948 		if (!request_failed)
949 			return 0;
950 
951 		if (!cert_valid) {
952 			ignore_flags = no_check_cert_flags;
953 			if (!WinHttpSetOption(s->request, WINHTTP_OPTION_SECURITY_FLAGS, &ignore_flags, sizeof(ignore_flags))) {
954 				git_error_set(GIT_ERROR_OS, "failed to set security options");
955 				return -1;
956 			}
957 		}
958 
959 		if (client_cert_requested) {
960 			/*
961 			 * Client certificates are not supported, explicitly tell the server that
962 			 * (it's possible a client certificate was requested but is not required)
963 			 */
964 			if (!WinHttpSetOption(s->request, WINHTTP_OPTION_CLIENT_CERT_CONTEXT, WINHTTP_NO_CLIENT_CERT_CONTEXT, 0)) {
965 				git_error_set(GIT_ERROR_OS, "failed to set client cert context");
966 				return -1;
967 			}
968 		}
969 	}
970 
971 	return error;
972 }
973 
acquire_credentials(HINTERNET request,winhttp_server * server,const char * url_str,git_credential_acquire_cb cred_cb,void * cred_cb_payload)974 static int acquire_credentials(
975 	HINTERNET request,
976 	winhttp_server *server,
977 	const char *url_str,
978 	git_credential_acquire_cb cred_cb,
979 	void *cred_cb_payload)
980 {
981 	int allowed_types;
982 	int error = 1;
983 
984 	if (parse_unauthorized_response(&allowed_types, &server->auth_mechanisms, request) < 0)
985 		return -1;
986 
987 	if (allowed_types) {
988 		git_credential_free(server->cred);
989 		server->cred = NULL;
990 
991 		/* Start with URL-specified credentials, if there were any. */
992 		if (!server->url_cred_presented && server->url.username && server->url.password) {
993 			error = acquire_url_cred(&server->cred, allowed_types, server->url.username, server->url.password);
994 			server->url_cred_presented = 1;
995 
996 			if (error < 0)
997 				return error;
998 		}
999 
1000 		/* Next use the user-defined callback, if there is one. */
1001 		if (error > 0 && cred_cb) {
1002 			error = cred_cb(&server->cred, url_str, server->url.username, allowed_types, cred_cb_payload);
1003 
1004 			/* Treat GIT_PASSTHROUGH as though git_credential_acquire_cb isn't set */
1005 			if (error == GIT_PASSTHROUGH)
1006 				error = 1;
1007 			else if (error < 0)
1008 				return error;
1009 		}
1010 
1011 		/* Finally, invoke the fallback default credential lookup. */
1012 		if (error > 0) {
1013 			error = acquire_fallback_cred(&server->cred, url_str, allowed_types);
1014 
1015 			if (error < 0)
1016 				return error;
1017 		}
1018 	}
1019 
1020 	/*
1021 	 * No error occurred but we could not find appropriate credentials.
1022 	 * This behaves like a pass-through.
1023 	 */
1024 	return error;
1025 }
1026 
winhttp_stream_read(git_smart_subtransport_stream * stream,char * buffer,size_t buf_size,size_t * bytes_read)1027 static int winhttp_stream_read(
1028 	git_smart_subtransport_stream *stream,
1029 	char *buffer,
1030 	size_t buf_size,
1031 	size_t *bytes_read)
1032 {
1033 	winhttp_stream *s = (winhttp_stream *)stream;
1034 	winhttp_subtransport *t = OWNING_SUBTRANSPORT(s);
1035 	DWORD dw_bytes_read;
1036 	char replay_count = 0;
1037 	int error;
1038 
1039 replay:
1040 	/* Enforce a reasonable cap on the number of replays */
1041 	if (replay_count++ >= GIT_HTTP_REPLAY_MAX) {
1042 		git_error_set(GIT_ERROR_HTTP, "too many redirects or authentication replays");
1043 		return GIT_ERROR; /* not GIT_EAUTH because the exact cause is not clear */
1044 	}
1045 
1046 	/* Connect if necessary */
1047 	if (!s->request && winhttp_stream_connect(s) < 0)
1048 		return -1;
1049 
1050 	if (!s->received_response) {
1051 		DWORD status_code, status_code_length, content_type_length, bytes_written;
1052 		char expected_content_type_8[MAX_CONTENT_TYPE_LEN];
1053 		wchar_t expected_content_type[MAX_CONTENT_TYPE_LEN], content_type[MAX_CONTENT_TYPE_LEN];
1054 
1055 		if (!s->sent_request) {
1056 
1057 			if ((error = send_request(s, s->post_body_len, false)) < 0)
1058 				return error;
1059 
1060 			s->sent_request = 1;
1061 		}
1062 
1063 		if (s->chunked) {
1064 			GIT_ASSERT(s->verb == post_verb);
1065 
1066 			/* Flush, if necessary */
1067 			if (s->chunk_buffer_len > 0 &&
1068 				write_chunk(s->request, s->chunk_buffer, s->chunk_buffer_len) < 0)
1069 				return -1;
1070 
1071 			s->chunk_buffer_len = 0;
1072 
1073 			/* Write the final chunk. */
1074 			if (!WinHttpWriteData(s->request,
1075 				"0\r\n\r\n", 5,
1076 				&bytes_written)) {
1077 				git_error_set(GIT_ERROR_OS, "failed to write final chunk");
1078 				return -1;
1079 			}
1080 		}
1081 		else if (s->post_body) {
1082 			char *buffer;
1083 			DWORD len = s->post_body_len, bytes_read;
1084 
1085 			if (INVALID_SET_FILE_POINTER == SetFilePointer(s->post_body,
1086 					0, 0, FILE_BEGIN) &&
1087 				NO_ERROR != GetLastError()) {
1088 				git_error_set(GIT_ERROR_OS, "failed to reset file pointer");
1089 				return -1;
1090 			}
1091 
1092 			buffer = git__malloc(CACHED_POST_BODY_BUF_SIZE);
1093 			GIT_ERROR_CHECK_ALLOC(buffer);
1094 
1095 			while (len > 0) {
1096 				DWORD bytes_written;
1097 
1098 				if (!ReadFile(s->post_body, buffer,
1099 					min(CACHED_POST_BODY_BUF_SIZE, len),
1100 					&bytes_read, NULL) ||
1101 					!bytes_read) {
1102 					git__free(buffer);
1103 					git_error_set(GIT_ERROR_OS, "failed to read from temp file");
1104 					return -1;
1105 				}
1106 
1107 				if (!WinHttpWriteData(s->request, buffer,
1108 					bytes_read, &bytes_written)) {
1109 					git__free(buffer);
1110 					git_error_set(GIT_ERROR_OS, "failed to write data");
1111 					return -1;
1112 				}
1113 
1114 				len -= bytes_read;
1115 				GIT_ASSERT(bytes_read == bytes_written);
1116 			}
1117 
1118 			git__free(buffer);
1119 
1120 			/* Eagerly close the temp file */
1121 			CloseHandle(s->post_body);
1122 			s->post_body = NULL;
1123 		}
1124 
1125 		if (!WinHttpReceiveResponse(s->request, 0)) {
1126 			git_error_set(GIT_ERROR_OS, "failed to receive response");
1127 			return -1;
1128 		}
1129 
1130 		/* Verify that we got a 200 back */
1131 		status_code_length = sizeof(status_code);
1132 
1133 		if (!WinHttpQueryHeaders(s->request,
1134 			WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
1135 			WINHTTP_HEADER_NAME_BY_INDEX,
1136 			&status_code, &status_code_length,
1137 			WINHTTP_NO_HEADER_INDEX)) {
1138 				git_error_set(GIT_ERROR_OS, "failed to retrieve status code");
1139 				return -1;
1140 		}
1141 
1142 		/* The implementation of WinHTTP prior to Windows 7 will not
1143 		 * redirect to an identical URI. Some Git hosters use self-redirects
1144 		 * as part of their DoS mitigation strategy. Check first to see if we
1145 		 * have a redirect status code, and that we haven't already streamed
1146 		 * a post body. (We can't replay a streamed POST.) */
1147 		if (!s->chunked &&
1148 			(HTTP_STATUS_MOVED == status_code ||
1149 			 HTTP_STATUS_REDIRECT == status_code ||
1150 			 (HTTP_STATUS_REDIRECT_METHOD == status_code &&
1151 			  get_verb == s->verb) ||
1152 			 HTTP_STATUS_REDIRECT_KEEP_VERB == status_code ||
1153 			 HTTP_STATUS_PERMANENT_REDIRECT == status_code)) {
1154 
1155 			/* Check for Windows 7. This workaround is only necessary on
1156 			 * Windows Vista and earlier. Windows 7 is version 6.1. */
1157 			wchar_t *location;
1158 			DWORD location_length;
1159 			char *location8;
1160 
1161 			/* OK, fetch the Location header from the redirect. */
1162 			if (WinHttpQueryHeaders(s->request,
1163 				WINHTTP_QUERY_LOCATION,
1164 				WINHTTP_HEADER_NAME_BY_INDEX,
1165 				WINHTTP_NO_OUTPUT_BUFFER,
1166 				&location_length,
1167 				WINHTTP_NO_HEADER_INDEX) ||
1168 				GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
1169 				git_error_set(GIT_ERROR_OS, "failed to read Location header");
1170 				return -1;
1171 			}
1172 
1173 			location = git__malloc(location_length);
1174 			GIT_ERROR_CHECK_ALLOC(location);
1175 
1176 			if (!WinHttpQueryHeaders(s->request,
1177 				WINHTTP_QUERY_LOCATION,
1178 				WINHTTP_HEADER_NAME_BY_INDEX,
1179 				location,
1180 				&location_length,
1181 				WINHTTP_NO_HEADER_INDEX)) {
1182 				git_error_set(GIT_ERROR_OS, "failed to read Location header");
1183 				git__free(location);
1184 				return -1;
1185 			}
1186 
1187 			/* Convert the Location header to UTF-8 */
1188 			if (git__utf16_to_8_alloc(&location8, location) < 0) {
1189 				git_error_set(GIT_ERROR_OS, "failed to convert Location header to UTF-8");
1190 				git__free(location);
1191 				return -1;
1192 			}
1193 
1194 			git__free(location);
1195 
1196 			/* Replay the request */
1197 			winhttp_stream_close(s);
1198 
1199 			if (!git__prefixcmp_icase(location8, prefix_https)) {
1200 				/* Upgrade to secure connection; disconnect and start over */
1201 				if (git_net_url_apply_redirect(&t->server.url, location8, s->service_url) < 0) {
1202 					git__free(location8);
1203 					return -1;
1204 				}
1205 
1206 				winhttp_close_connection(t);
1207 
1208 				if (winhttp_connect(t) < 0)
1209 					return -1;
1210 			}
1211 
1212 			git__free(location8);
1213 			goto replay;
1214 		}
1215 
1216 		/* Handle authentication failures */
1217 		if (status_code == HTTP_STATUS_DENIED) {
1218 			int error = acquire_credentials(s->request,
1219 				&t->server,
1220 				t->owner->url,
1221 				t->owner->cred_acquire_cb,
1222 				t->owner->cred_acquire_payload);
1223 
1224 			if (error < 0) {
1225 				return error;
1226 			} else if (!error) {
1227 				GIT_ASSERT(t->server.cred);
1228 				winhttp_stream_close(s);
1229 				goto replay;
1230 			}
1231 		} else if (status_code == HTTP_STATUS_PROXY_AUTH_REQ) {
1232 			int error = acquire_credentials(s->request,
1233 				&t->proxy,
1234 				t->owner->proxy.url,
1235 				t->owner->proxy.credentials,
1236 				t->owner->proxy.payload);
1237 
1238 			if (error < 0) {
1239 				return error;
1240 			} else if (!error) {
1241 				GIT_ASSERT(t->proxy.cred);
1242 				winhttp_stream_close(s);
1243 				goto replay;
1244 			}
1245 		}
1246 
1247 		if (HTTP_STATUS_OK != status_code) {
1248 			git_error_set(GIT_ERROR_HTTP, "request failed with status code: %lu", status_code);
1249 			return -1;
1250 		}
1251 
1252 		/* Verify that we got the correct content-type back */
1253 		if (post_verb == s->verb)
1254 			p_snprintf(expected_content_type_8, MAX_CONTENT_TYPE_LEN, "application/x-git-%s-result", s->service);
1255 		else
1256 			p_snprintf(expected_content_type_8, MAX_CONTENT_TYPE_LEN, "application/x-git-%s-advertisement", s->service);
1257 
1258 		if (git__utf8_to_16(expected_content_type, MAX_CONTENT_TYPE_LEN, expected_content_type_8) < 0) {
1259 			git_error_set(GIT_ERROR_OS, "failed to convert expected content-type to wide characters");
1260 			return -1;
1261 		}
1262 
1263 		content_type_length = sizeof(content_type);
1264 
1265 		if (!WinHttpQueryHeaders(s->request,
1266 			WINHTTP_QUERY_CONTENT_TYPE,
1267 			WINHTTP_HEADER_NAME_BY_INDEX,
1268 			&content_type, &content_type_length,
1269 			WINHTTP_NO_HEADER_INDEX)) {
1270 				git_error_set(GIT_ERROR_OS, "failed to retrieve response content-type");
1271 				return -1;
1272 		}
1273 
1274 		if (wcscmp(expected_content_type, content_type)) {
1275 			git_error_set(GIT_ERROR_HTTP, "received unexpected content-type");
1276 			return -1;
1277 		}
1278 
1279 		s->received_response = 1;
1280 	}
1281 
1282 	if (!WinHttpReadData(s->request,
1283 		(LPVOID)buffer,
1284 		(DWORD)buf_size,
1285 		&dw_bytes_read))
1286 	{
1287 		git_error_set(GIT_ERROR_OS, "failed to read data");
1288 		return -1;
1289 	}
1290 
1291 	*bytes_read = dw_bytes_read;
1292 
1293 	return 0;
1294 }
1295 
winhttp_stream_write_single(git_smart_subtransport_stream * stream,const char * buffer,size_t len)1296 static int winhttp_stream_write_single(
1297 	git_smart_subtransport_stream *stream,
1298 	const char *buffer,
1299 	size_t len)
1300 {
1301 	winhttp_stream *s = (winhttp_stream *)stream;
1302 	DWORD bytes_written;
1303 	int error;
1304 
1305 	if (!s->request && winhttp_stream_connect(s) < 0)
1306 		return -1;
1307 
1308 	/* This implementation of write permits only a single call. */
1309 	if (s->sent_request) {
1310 		git_error_set(GIT_ERROR_HTTP, "subtransport configured for only one write");
1311 		return -1;
1312 	}
1313 
1314 	if ((error = send_request(s, len, false)) < 0)
1315 		return error;
1316 
1317 	s->sent_request = 1;
1318 
1319 	if (!WinHttpWriteData(s->request,
1320 			(LPCVOID)buffer,
1321 			(DWORD)len,
1322 			&bytes_written)) {
1323 		git_error_set(GIT_ERROR_OS, "failed to write data");
1324 		return -1;
1325 	}
1326 
1327 	GIT_ASSERT((DWORD)len == bytes_written);
1328 
1329 	return 0;
1330 }
1331 
put_uuid_string(LPWSTR buffer,size_t buffer_len_cch)1332 static int put_uuid_string(LPWSTR buffer, size_t buffer_len_cch)
1333 {
1334 	UUID uuid;
1335 	RPC_STATUS status = UuidCreate(&uuid);
1336 	int result;
1337 
1338 	if (RPC_S_OK != status &&
1339 		RPC_S_UUID_LOCAL_ONLY != status &&
1340 		RPC_S_UUID_NO_ADDRESS != status) {
1341 		git_error_set(GIT_ERROR_HTTP, "unable to generate name for temp file");
1342 		return -1;
1343 	}
1344 
1345 	if (buffer_len_cch < UUID_LENGTH_CCH + 1) {
1346 		git_error_set(GIT_ERROR_HTTP, "buffer too small for name of temp file");
1347 		return -1;
1348 	}
1349 
1350 #if !defined(__MINGW32__) || defined(MINGW_HAS_SECURE_API)
1351 	result = swprintf_s(buffer, buffer_len_cch,
1352 #else
1353 	result = wsprintfW(buffer,
1354 #endif
1355 		L"%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x",
1356 		uuid.Data1, uuid.Data2, uuid.Data3,
1357 		uuid.Data4[0], uuid.Data4[1], uuid.Data4[2], uuid.Data4[3],
1358 		uuid.Data4[4], uuid.Data4[5], uuid.Data4[6], uuid.Data4[7]);
1359 
1360 	if (result < UUID_LENGTH_CCH) {
1361 		git_error_set(GIT_ERROR_OS, "unable to generate name for temp file");
1362 		return -1;
1363 	}
1364 
1365 	return 0;
1366 }
1367 
get_temp_file(LPWSTR buffer,DWORD buffer_len_cch)1368 static int get_temp_file(LPWSTR buffer, DWORD buffer_len_cch)
1369 {
1370 	size_t len;
1371 
1372 	if (!GetTempPathW(buffer_len_cch, buffer)) {
1373 		git_error_set(GIT_ERROR_OS, "failed to get temp path");
1374 		return -1;
1375 	}
1376 
1377 	len = wcslen(buffer);
1378 
1379 	if (buffer[len - 1] != '\\' && len < buffer_len_cch)
1380 		buffer[len++] = '\\';
1381 
1382 	if (put_uuid_string(&buffer[len], (size_t)buffer_len_cch - len) < 0)
1383 		return -1;
1384 
1385 	return 0;
1386 }
1387 
winhttp_stream_write_buffered(git_smart_subtransport_stream * stream,const char * buffer,size_t len)1388 static int winhttp_stream_write_buffered(
1389 	git_smart_subtransport_stream *stream,
1390 	const char *buffer,
1391 	size_t len)
1392 {
1393 	winhttp_stream *s = (winhttp_stream *)stream;
1394 	DWORD bytes_written;
1395 
1396 	if (!s->request && winhttp_stream_connect(s) < 0)
1397 		return -1;
1398 
1399 	/* Buffer the payload, using a temporary file so we delegate
1400 	 * memory management of the data to the operating system. */
1401 	if (!s->post_body) {
1402 		wchar_t temp_path[MAX_PATH + 1];
1403 
1404 		if (get_temp_file(temp_path, MAX_PATH + 1) < 0)
1405 			return -1;
1406 
1407 		s->post_body = CreateFileW(temp_path,
1408 			GENERIC_READ | GENERIC_WRITE,
1409 			FILE_SHARE_DELETE, NULL,
1410 			CREATE_NEW,
1411 			FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_SEQUENTIAL_SCAN,
1412 			NULL);
1413 
1414 		if (INVALID_HANDLE_VALUE == s->post_body) {
1415 			s->post_body = NULL;
1416 			git_error_set(GIT_ERROR_OS, "failed to create temporary file");
1417 			return -1;
1418 		}
1419 	}
1420 
1421 	if (!WriteFile(s->post_body, buffer, (DWORD)len, &bytes_written, NULL)) {
1422 		git_error_set(GIT_ERROR_OS, "failed to write to temporary file");
1423 		return -1;
1424 	}
1425 
1426 	GIT_ASSERT((DWORD)len == bytes_written);
1427 
1428 	s->post_body_len += bytes_written;
1429 
1430 	return 0;
1431 }
1432 
winhttp_stream_write_chunked(git_smart_subtransport_stream * stream,const char * buffer,size_t len)1433 static int winhttp_stream_write_chunked(
1434 	git_smart_subtransport_stream *stream,
1435 	const char *buffer,
1436 	size_t len)
1437 {
1438 	winhttp_stream *s = (winhttp_stream *)stream;
1439 	int error;
1440 
1441 	if (!s->request && winhttp_stream_connect(s) < 0)
1442 		return -1;
1443 
1444 	if (!s->sent_request) {
1445 		/* Send Transfer-Encoding: chunked header */
1446 		if (!WinHttpAddRequestHeaders(s->request,
1447 			transfer_encoding, (ULONG) -1L,
1448 			WINHTTP_ADDREQ_FLAG_ADD)) {
1449 			git_error_set(GIT_ERROR_OS, "failed to add a header to the request");
1450 			return -1;
1451 		}
1452 
1453 		if ((error = send_request(s, 0, true)) < 0)
1454 			return error;
1455 
1456 		s->sent_request = 1;
1457 	}
1458 
1459 	if (len > CACHED_POST_BODY_BUF_SIZE) {
1460 		/* Flush, if necessary */
1461 		if (s->chunk_buffer_len > 0) {
1462 			if (write_chunk(s->request, s->chunk_buffer, s->chunk_buffer_len) < 0)
1463 				return -1;
1464 
1465 			s->chunk_buffer_len = 0;
1466 		}
1467 
1468 		/* Write chunk directly */
1469 		if (write_chunk(s->request, buffer, len) < 0)
1470 			return -1;
1471 	}
1472 	else {
1473 		/* Append as much to the buffer as we can */
1474 		int count = (int)min(CACHED_POST_BODY_BUF_SIZE - s->chunk_buffer_len, len);
1475 
1476 		if (!s->chunk_buffer) {
1477 			s->chunk_buffer = git__malloc(CACHED_POST_BODY_BUF_SIZE);
1478 			GIT_ERROR_CHECK_ALLOC(s->chunk_buffer);
1479 		}
1480 
1481 		memcpy(s->chunk_buffer + s->chunk_buffer_len, buffer, count);
1482 		s->chunk_buffer_len += count;
1483 		buffer += count;
1484 		len -= count;
1485 
1486 		/* Is the buffer full? If so, then flush */
1487 		if (CACHED_POST_BODY_BUF_SIZE == s->chunk_buffer_len) {
1488 			if (write_chunk(s->request, s->chunk_buffer, s->chunk_buffer_len) < 0)
1489 				return -1;
1490 
1491 			s->chunk_buffer_len = 0;
1492 
1493 			/* Is there any remaining data from the source? */
1494 			if (len > 0) {
1495 				memcpy(s->chunk_buffer, buffer, len);
1496 				s->chunk_buffer_len = (unsigned int)len;
1497 			}
1498 		}
1499 	}
1500 
1501 	return 0;
1502 }
1503 
winhttp_stream_free(git_smart_subtransport_stream * stream)1504 static void winhttp_stream_free(git_smart_subtransport_stream *stream)
1505 {
1506 	winhttp_stream *s = (winhttp_stream *)stream;
1507 
1508 	winhttp_stream_close(s);
1509 	git__free(s);
1510 }
1511 
winhttp_stream_alloc(winhttp_subtransport * t,winhttp_stream ** stream)1512 static int winhttp_stream_alloc(winhttp_subtransport *t, winhttp_stream **stream)
1513 {
1514 	winhttp_stream *s;
1515 
1516 	if (!stream)
1517 		return -1;
1518 
1519 	s = git__calloc(1, sizeof(winhttp_stream));
1520 	GIT_ERROR_CHECK_ALLOC(s);
1521 
1522 	s->parent.subtransport = &t->parent;
1523 	s->parent.read = winhttp_stream_read;
1524 	s->parent.write = winhttp_stream_write_single;
1525 	s->parent.free = winhttp_stream_free;
1526 
1527 	*stream = s;
1528 
1529 	return 0;
1530 }
1531 
winhttp_uploadpack_ls(winhttp_subtransport * t,winhttp_stream * s)1532 static int winhttp_uploadpack_ls(
1533 	winhttp_subtransport *t,
1534 	winhttp_stream *s)
1535 {
1536 	GIT_UNUSED(t);
1537 
1538 	s->service = upload_pack_service;
1539 	s->service_url = upload_pack_ls_service_url;
1540 	s->verb = get_verb;
1541 
1542 	return 0;
1543 }
1544 
winhttp_uploadpack(winhttp_subtransport * t,winhttp_stream * s)1545 static int winhttp_uploadpack(
1546 	winhttp_subtransport *t,
1547 	winhttp_stream *s)
1548 {
1549 	GIT_UNUSED(t);
1550 
1551 	s->service = upload_pack_service;
1552 	s->service_url = upload_pack_service_url;
1553 	s->verb = post_verb;
1554 
1555 	return 0;
1556 }
1557 
winhttp_receivepack_ls(winhttp_subtransport * t,winhttp_stream * s)1558 static int winhttp_receivepack_ls(
1559 	winhttp_subtransport *t,
1560 	winhttp_stream *s)
1561 {
1562 	GIT_UNUSED(t);
1563 
1564 	s->service = receive_pack_service;
1565 	s->service_url = receive_pack_ls_service_url;
1566 	s->verb = get_verb;
1567 
1568 	return 0;
1569 }
1570 
winhttp_receivepack(winhttp_subtransport * t,winhttp_stream * s)1571 static int winhttp_receivepack(
1572 	winhttp_subtransport *t,
1573 	winhttp_stream *s)
1574 {
1575 	GIT_UNUSED(t);
1576 
1577 	/* WinHTTP only supports Transfer-Encoding: chunked
1578 	 * on Windows Vista (NT 6.0) and higher. */
1579 	s->chunked = git_has_win32_version(6, 0, 0);
1580 
1581 	if (s->chunked)
1582 		s->parent.write = winhttp_stream_write_chunked;
1583 	else
1584 		s->parent.write = winhttp_stream_write_buffered;
1585 
1586 	s->service = receive_pack_service;
1587 	s->service_url = receive_pack_service_url;
1588 	s->verb = post_verb;
1589 
1590 	return 0;
1591 }
1592 
winhttp_action(git_smart_subtransport_stream ** stream,git_smart_subtransport * subtransport,const char * url,git_smart_service_t action)1593 static int winhttp_action(
1594 	git_smart_subtransport_stream **stream,
1595 	git_smart_subtransport *subtransport,
1596 	const char *url,
1597 	git_smart_service_t action)
1598 {
1599 	winhttp_subtransport *t = (winhttp_subtransport *)subtransport;
1600 	winhttp_stream *s;
1601 	int ret = -1;
1602 
1603 	if (!t->connection)
1604 		if ((ret = git_net_url_parse(&t->server.url, url)) < 0 ||
1605 			 (ret = winhttp_connect(t)) < 0)
1606 			return ret;
1607 
1608 	if (winhttp_stream_alloc(t, &s) < 0)
1609 		return -1;
1610 
1611 	if (!stream)
1612 		return -1;
1613 
1614 	switch (action)
1615 	{
1616 		case GIT_SERVICE_UPLOADPACK_LS:
1617 			ret = winhttp_uploadpack_ls(t, s);
1618 			break;
1619 
1620 		case GIT_SERVICE_UPLOADPACK:
1621 			ret = winhttp_uploadpack(t, s);
1622 			break;
1623 
1624 		case GIT_SERVICE_RECEIVEPACK_LS:
1625 			ret = winhttp_receivepack_ls(t, s);
1626 			break;
1627 
1628 		case GIT_SERVICE_RECEIVEPACK:
1629 			ret = winhttp_receivepack(t, s);
1630 			break;
1631 
1632 		default:
1633 			GIT_ASSERT(0);
1634 	}
1635 
1636 	if (!ret)
1637 		*stream = &s->parent;
1638 
1639 	return ret;
1640 }
1641 
winhttp_close(git_smart_subtransport * subtransport)1642 static int winhttp_close(git_smart_subtransport *subtransport)
1643 {
1644 	winhttp_subtransport *t = (winhttp_subtransport *)subtransport;
1645 
1646 	git_net_url_dispose(&t->server.url);
1647 	git_net_url_dispose(&t->proxy.url);
1648 
1649 	if (t->server.cred) {
1650 		t->server.cred->free(t->server.cred);
1651 		t->server.cred = NULL;
1652 	}
1653 
1654 	if (t->proxy.cred) {
1655 		t->proxy.cred->free(t->proxy.cred);
1656 		t->proxy.cred = NULL;
1657 	}
1658 
1659 	return winhttp_close_connection(t);
1660 }
1661 
winhttp_free(git_smart_subtransport * subtransport)1662 static void winhttp_free(git_smart_subtransport *subtransport)
1663 {
1664 	winhttp_subtransport *t = (winhttp_subtransport *)subtransport;
1665 
1666 	winhttp_close(subtransport);
1667 
1668 	git__free(t);
1669 }
1670 
git_smart_subtransport_http(git_smart_subtransport ** out,git_transport * owner,void * param)1671 int git_smart_subtransport_http(git_smart_subtransport **out, git_transport *owner, void *param)
1672 {
1673 	winhttp_subtransport *t;
1674 
1675 	GIT_UNUSED(param);
1676 
1677 	if (!out)
1678 		return -1;
1679 
1680 	t = git__calloc(1, sizeof(winhttp_subtransport));
1681 	GIT_ERROR_CHECK_ALLOC(t);
1682 
1683 	t->owner = (transport_smart *)owner;
1684 	t->parent.action = winhttp_action;
1685 	t->parent.close = winhttp_close;
1686 	t->parent.free = winhttp_free;
1687 
1688 	*out = (git_smart_subtransport *) t;
1689 	return 0;
1690 }
1691 
1692 #endif /* GIT_WINHTTP */
1693