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