2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
11 #include "git2/transport.h"
17 #include "repository.h"
23 /* For IInternetSecurityManager zone check */
27 #define WIDEN2(s) L ## s
28 #define WIDEN(s) WIDEN2(s)
30 #define MAX_CONTENT_TYPE_LEN 100
31 #define WINHTTP_OPTION_PEERDIST_EXTENSION_STATE 109
32 #define CACHED_POST_BODY_BUF_SIZE 4096
33 #define UUID_LENGTH_CCH 32
34 #define TIMEOUT_INFINITE -1
35 #define DEFAULT_CONNECT_TIMEOUT 60000
36 #ifndef WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH
37 #define WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH 0
40 static const char *prefix_https
= "https://";
41 static const char *upload_pack_service
= "upload-pack";
42 static const char *upload_pack_ls_service_url
= "/info/refs?service=git-upload-pack";
43 static const char *upload_pack_service_url
= "/git-upload-pack";
44 static const char *receive_pack_service
= "receive-pack";
45 static const char *receive_pack_ls_service_url
= "/info/refs?service=git-receive-pack";
46 static const char *receive_pack_service_url
= "/git-receive-pack";
47 static const wchar_t *get_verb
= L
"GET";
48 static const wchar_t *post_verb
= L
"POST";
49 static const wchar_t *pragma_nocache
= L
"Pragma: no-cache";
50 static const wchar_t *transfer_encoding
= L
"Transfer-Encoding: chunked";
51 static const int no_check_cert_flags
= SECURITY_FLAG_IGNORE_CERT_CN_INVALID
|
52 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
|
53 SECURITY_FLAG_IGNORE_UNKNOWN_CA
;
55 #if defined(__MINGW32__)
56 static const CLSID CLSID_InternetSecurityManager_mingw
=
57 { 0x7B8A2D94, 0x0AC9, 0x11D1,
58 { 0x89, 0x6C, 0x00, 0xC0, 0x4F, 0xB6, 0xBF, 0xC4 } };
59 static const IID IID_IInternetSecurityManager_mingw
=
60 { 0x79EAC9EE, 0xBAF9, 0x11CE,
61 { 0x8C, 0x82, 0x00, 0xAA, 0x00, 0x4B, 0xA9, 0x0B } };
63 # define CLSID_InternetSecurityManager CLSID_InternetSecurityManager_mingw
64 # define IID_IInternetSecurityManager IID_IInternetSecurityManager_mingw
67 #define OWNING_SUBTRANSPORT(s) ((winhttp_subtransport *)(s)->parent.subtransport)
70 GIT_WINHTTP_AUTH_BASIC
= 1,
71 GIT_WINHTTP_AUTH_NTLM
= 2,
72 GIT_WINHTTP_AUTH_NEGOTIATE
= 4,
73 GIT_WINHTTP_AUTH_DIGEST
= 8,
74 } winhttp_authmechanism_t
;
77 git_smart_subtransport_stream parent
;
79 const char *service_url
;
84 unsigned chunk_buffer_len
;
87 unsigned sent_request
: 1,
88 received_response
: 1,
93 git_smart_subtransport parent
;
94 transport_smart
*owner
;
95 gitno_connection_data connection_data
;
96 gitno_connection_data proxy_connection_data
;
102 HINTERNET connection
;
103 } winhttp_subtransport
;
105 static int _apply_userpass_credential(HINTERNET request
, DWORD target
, DWORD scheme
, git_cred
*cred
)
107 git_cred_userpass_plaintext
*c
= (git_cred_userpass_plaintext
*)cred
;
108 wchar_t *user
, *pass
;
109 int user_len
= 0, pass_len
= 0, error
= 0;
111 if ((error
= user_len
= git__utf8_to_16_alloc(&user
, c
->username
)) < 0)
114 if ((error
= pass_len
= git__utf8_to_16_alloc(&pass
, c
->password
)) < 0)
117 if (!WinHttpSetCredentials(request
, target
, scheme
, user
, pass
, NULL
)) {
118 giterr_set(GITERR_OS
, "failed to set credentials");
124 git__memzero(user
, user_len
* sizeof(wchar_t));
127 git__memzero(pass
, pass_len
* sizeof(wchar_t));
135 static int apply_userpass_credential_proxy(HINTERNET request
, git_cred
*cred
, int mechanisms
)
137 if (GIT_WINHTTP_AUTH_DIGEST
& mechanisms
) {
138 return _apply_userpass_credential(request
, WINHTTP_AUTH_TARGET_PROXY
,
139 WINHTTP_AUTH_SCHEME_DIGEST
, cred
);
142 return _apply_userpass_credential(request
, WINHTTP_AUTH_TARGET_PROXY
,
143 WINHTTP_AUTH_SCHEME_BASIC
, cred
);
146 static int apply_userpass_credential(HINTERNET request
, int mechanisms
, git_cred
*cred
)
150 if ((mechanisms
& GIT_WINHTTP_AUTH_NTLM
) ||
151 (mechanisms
& GIT_WINHTTP_AUTH_NEGOTIATE
)) {
152 native_scheme
= WINHTTP_AUTH_SCHEME_NTLM
;
153 } else if (mechanisms
& GIT_WINHTTP_AUTH_BASIC
) {
154 native_scheme
= WINHTTP_AUTH_SCHEME_BASIC
;
156 giterr_set(GITERR_NET
, "invalid authentication scheme");
160 return _apply_userpass_credential(request
, WINHTTP_AUTH_TARGET_SERVER
,
161 native_scheme
, cred
);
164 static int apply_default_credentials(HINTERNET request
, int mechanisms
)
166 /* Either the caller explicitly requested that default credentials be passed,
167 * or our fallback credential callback was invoked and checked that the target
168 * URI was in the appropriate Internet Explorer security zone. By setting this
169 * flag, we guarantee that the credentials are delivered by WinHTTP. The default
170 * is "medium" which applies to the intranet and sounds like it would correspond
171 * to Internet Explorer security zones, but in fact does not. */
172 DWORD data
= WINHTTP_AUTOLOGON_SECURITY_LEVEL_LOW
;
174 if ((mechanisms
& GIT_WINHTTP_AUTH_NTLM
) == 0 &&
175 (mechanisms
& GIT_WINHTTP_AUTH_NEGOTIATE
) == 0) {
176 giterr_set(GITERR_NET
, "invalid authentication scheme");
180 if (!WinHttpSetOption(request
, WINHTTP_OPTION_AUTOLOGON_POLICY
, &data
, sizeof(DWORD
)))
186 static int fallback_cred_acquire_cb(
189 const char *username_from_url
,
190 unsigned int allowed_types
,
195 GIT_UNUSED(username_from_url
);
198 /* If the target URI supports integrated Windows authentication
199 * as an authentication mechanism */
200 if (GIT_CREDTYPE_DEFAULT
& allowed_types
) {
203 /* Convert URL to wide characters */
204 if (git__utf8_to_16_alloc(&wide_url
, url
) < 0) {
205 giterr_set(GITERR_OS
, "failed to convert string to wide form");
209 if (SUCCEEDED(CoInitializeEx(NULL
, COINIT_MULTITHREADED
))) {
210 IInternetSecurityManager
* pISM
;
212 /* And if the target URI is in the My Computer, Intranet, or Trusted zones */
213 if (SUCCEEDED(CoCreateInstance(&CLSID_InternetSecurityManager
, NULL
,
214 CLSCTX_ALL
, &IID_IInternetSecurityManager
, (void **)&pISM
))) {
217 if (SUCCEEDED(pISM
->lpVtbl
->MapUrlToZone(pISM
, wide_url
, &dwZone
, 0)) &&
218 (URLZONE_LOCAL_MACHINE
== dwZone
||
219 URLZONE_INTRANET
== dwZone
||
220 URLZONE_TRUSTED
== dwZone
)) {
221 git_cred
*existing
= *cred
;
224 existing
->free(existing
);
226 /* Then use default Windows credentials to authenticate this request */
227 error
= git_cred_default_new(cred
);
230 pISM
->lpVtbl
->Release(pISM
);
242 static int certificate_check(winhttp_stream
*s
, int valid
)
245 winhttp_subtransport
*t
= OWNING_SUBTRANSPORT(s
);
246 PCERT_CONTEXT cert_ctx
;
247 DWORD cert_ctx_size
= sizeof(cert_ctx
);
250 /* If there is no override, we should fail if WinHTTP doesn't think it's fine */
251 if (t
->owner
->certificate_check_cb
== NULL
&& !valid
) {
253 giterr_set(GITERR_NET
, "unknown certificate check failure");
255 return GIT_ECERTIFICATE
;
258 if (t
->owner
->certificate_check_cb
== NULL
|| !t
->connection_data
.use_ssl
)
261 if (!WinHttpQueryOption(s
->request
, WINHTTP_OPTION_SERVER_CERT_CONTEXT
, &cert_ctx
, &cert_ctx_size
)) {
262 giterr_set(GITERR_OS
, "failed to get server certificate");
267 cert
.parent
.cert_type
= GIT_CERT_X509
;
268 cert
.data
= cert_ctx
->pbCertEncoded
;
269 cert
.len
= cert_ctx
->cbCertEncoded
;
270 error
= t
->owner
->certificate_check_cb((git_cert
*) &cert
, valid
, t
->connection_data
.host
, t
->owner
->cred_acquire_payload
);
271 CertFreeCertificateContext(cert_ctx
);
273 if (error
< 0 && !giterr_last())
274 giterr_set(GITERR_NET
, "user cancelled certificate check");
279 static void winhttp_stream_close(winhttp_stream
*s
)
281 if (s
->chunk_buffer
) {
282 git__free(s
->chunk_buffer
);
283 s
->chunk_buffer
= NULL
;
287 CloseHandle(s
->post_body
);
291 if (s
->request_uri
) {
292 git__free(s
->request_uri
);
293 s
->request_uri
= NULL
;
297 WinHttpCloseHandle(s
->request
);
305 * Extract the url and password from a URL. The outputs are pointers
308 static int userpass_from_url(wchar_t **user
, int *user_len
,
309 wchar_t **pass
, int *pass_len
,
310 const wchar_t *url
, int url_len
)
312 URL_COMPONENTS components
= { 0 };
314 components
.dwStructSize
= sizeof(components
);
315 /* These tell WinHttpCrackUrl that we're interested in the fields */
316 components
.dwUserNameLength
= 1;
317 components
.dwPasswordLength
= 1;
319 if (!WinHttpCrackUrl(url
, url_len
, 0, &components
)) {
320 giterr_set(GITERR_OS
, "failed to extract user/pass from url");
324 *user
= components
.lpszUserName
;
325 *user_len
= components
.dwUserNameLength
;
326 *pass
= components
.lpszPassword
;
327 *pass_len
= components
.dwPasswordLength
;
332 #define SCHEME_HTTP "http://"
333 #define SCHEME_HTTPS "https://"
335 static int winhttp_stream_connect(winhttp_stream
*s
)
337 winhttp_subtransport
*t
= OWNING_SUBTRANSPORT(s
);
338 git_buf buf
= GIT_BUF_INIT
;
339 char *proxy_url
= NULL
;
340 wchar_t ct
[MAX_CONTENT_TYPE_LEN
];
341 LPCWSTR types
[] = { L
"*/*", NULL
};
342 BOOL peerdist
= FALSE
;
344 unsigned long disable_redirects
= WINHTTP_DISABLE_REDIRECTS
;
345 int default_timeout
= TIMEOUT_INFINITE
;
346 int default_connect_timeout
= DEFAULT_CONNECT_TIMEOUT
;
348 const git_proxy_options
*proxy_opts
;
351 git_buf_printf(&buf
, "%s%s", t
->connection_data
.path
, s
->service_url
);
353 if (git_buf_oom(&buf
))
356 /* Convert URL to wide characters */
357 if (git__utf8_to_16_alloc(&s
->request_uri
, git_buf_cstr(&buf
)) < 0) {
358 giterr_set(GITERR_OS
, "failed to convert string to wide form");
362 /* Establish request */
363 s
->request
= WinHttpOpenRequest(
370 t
->connection_data
.use_ssl
? WINHTTP_FLAG_SECURE
: 0);
373 giterr_set(GITERR_OS
, "failed to open request");
377 if (!WinHttpSetTimeouts(s
->request
, default_timeout
, default_connect_timeout
, default_timeout
, default_timeout
)) {
378 giterr_set(GITERR_OS
, "failed to set timeouts for WinHTTP");
382 proxy_opts
= &t
->owner
->proxy
;
383 if (proxy_opts
->type
== GIT_PROXY_AUTO
) {
384 /* Set proxy if necessary */
385 if (git_remote__get_http_proxy(t
->owner
->owner
, !!t
->connection_data
.use_ssl
, &proxy_url
) < 0)
388 else if (proxy_opts
->type
== GIT_PROXY_SPECIFIED
) {
389 proxy_url
= git__strdup(proxy_opts
->url
);
390 GITERR_CHECK_ALLOC(proxy_url
);
394 git_buf processed_url
= GIT_BUF_INIT
;
395 WINHTTP_PROXY_INFO proxy_info
;
398 if (!git__prefixcmp(proxy_url
, SCHEME_HTTP
)) {
399 t
->proxy_connection_data
.use_ssl
= false;
400 } else if (!git__prefixcmp(proxy_url
, SCHEME_HTTPS
)) {
401 t
->proxy_connection_data
.use_ssl
= true;
403 giterr_set(GITERR_NET
, "invalid URL: '%s'", proxy_url
);
407 gitno_connection_data_free_ptrs(&t
->proxy_connection_data
);
409 if ((error
= gitno_extract_url_parts(&t
->proxy_connection_data
.host
, &t
->proxy_connection_data
.port
, NULL
,
410 &t
->proxy_connection_data
.user
, &t
->proxy_connection_data
.pass
, proxy_url
, NULL
)) < 0)
413 if (t
->proxy_connection_data
.user
&& t
->proxy_connection_data
.pass
) {
415 t
->proxy_cred
->free(t
->proxy_cred
);
418 if ((error
= git_cred_userpass_plaintext_new(&t
->proxy_cred
, t
->proxy_connection_data
.user
, t
->proxy_connection_data
.pass
)) < 0)
422 if (t
->proxy_connection_data
.use_ssl
)
423 git_buf_PUTS(&processed_url
, SCHEME_HTTPS
);
425 git_buf_PUTS(&processed_url
, SCHEME_HTTP
);
427 git_buf_puts(&processed_url
, t
->proxy_connection_data
.host
);
428 if (t
->proxy_connection_data
.port
)
429 git_buf_printf(&processed_url
, ":%s", t
->proxy_connection_data
.port
);
431 if (git_buf_oom(&processed_url
)) {
437 /* Convert URL to wide characters */
438 error
= git__utf8_to_16_alloc(&proxy_wide
, processed_url
.ptr
);
439 git_buf_free(&processed_url
);
443 proxy_info
.dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
444 proxy_info
.lpszProxy
= proxy_wide
;
445 proxy_info
.lpszProxyBypass
= NULL
;
447 if (!WinHttpSetOption(s
->request
,
448 WINHTTP_OPTION_PROXY
,
450 sizeof(WINHTTP_PROXY_INFO
))) {
451 giterr_set(GITERR_OS
, "failed to set proxy");
452 git__free(proxy_wide
);
456 git__free(proxy_wide
);
459 if (t
->proxy_cred
->credtype
== GIT_CREDTYPE_USERPASS_PLAINTEXT
) {
460 if ((error
= apply_userpass_credential_proxy(s
->request
, t
->proxy_cred
, t
->auth_mechanisms
)) < 0)
467 /* Disable WinHTTP redirects so we can handle them manually. Why, you ask?
468 * http://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/b2ff8879-ab9f-4218-8f09-16d25dff87ae
470 if (!WinHttpSetOption(s
->request
,
471 WINHTTP_OPTION_DISABLE_FEATURE
,
473 sizeof(disable_redirects
))) {
474 giterr_set(GITERR_OS
, "failed to disable redirects");
478 /* Strip unwanted headers (X-P2P-PeerDist, X-P2P-PeerDistEx) that WinHTTP
479 * adds itself. This option may not be supported by the underlying
480 * platform, so we do not error-check it */
481 WinHttpSetOption(s
->request
,
482 WINHTTP_OPTION_PEERDIST_EXTENSION_STATE
,
486 /* Send Pragma: no-cache header */
487 if (!WinHttpAddRequestHeaders(s
->request
, pragma_nocache
, (ULONG
) -1L, WINHTTP_ADDREQ_FLAG_ADD
)) {
488 giterr_set(GITERR_OS
, "failed to add a header to the request");
492 if (post_verb
== s
->verb
) {
493 /* Send Content-Type and Accept headers -- only necessary on a POST */
495 if (git_buf_printf(&buf
,
496 "Content-Type: application/x-git-%s-request",
500 if (git__utf8_to_16(ct
, MAX_CONTENT_TYPE_LEN
, git_buf_cstr(&buf
)) < 0) {
501 giterr_set(GITERR_OS
, "failed to convert content-type to wide characters");
505 if (!WinHttpAddRequestHeaders(s
->request
, ct
, (ULONG
)-1L,
506 WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
)) {
507 giterr_set(GITERR_OS
, "failed to add a header to the request");
512 if (git_buf_printf(&buf
,
513 "Accept: application/x-git-%s-result",
517 if (git__utf8_to_16(ct
, MAX_CONTENT_TYPE_LEN
, git_buf_cstr(&buf
)) < 0) {
518 giterr_set(GITERR_OS
, "failed to convert accept header to wide characters");
522 if (!WinHttpAddRequestHeaders(s
->request
, ct
, (ULONG
)-1L,
523 WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
)) {
524 giterr_set(GITERR_OS
, "failed to add a header to the request");
529 for (i
= 0; i
< t
->owner
->custom_headers
.count
; i
++) {
530 if (t
->owner
->custom_headers
.strings
[i
]) {
532 git_buf_puts(&buf
, t
->owner
->custom_headers
.strings
[i
]);
533 if (git__utf8_to_16(ct
, MAX_CONTENT_TYPE_LEN
, git_buf_cstr(&buf
)) < 0) {
534 giterr_set(GITERR_OS
, "failed to convert custom header to wide characters");
538 if (!WinHttpAddRequestHeaders(s
->request
, ct
, (ULONG
)-1L,
539 WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
)) {
540 giterr_set(GITERR_OS
, "failed to add a header to the request");
546 /* If requested, disable certificate validation */
547 if (t
->connection_data
.use_ssl
) {
550 if (t
->owner
->parent
.read_flags(&t
->owner
->parent
, &flags
) < 0)
554 /* If we have a credential on the subtransport, apply it to the request */
556 t
->cred
->credtype
== GIT_CREDTYPE_USERPASS_PLAINTEXT
&&
557 apply_userpass_credential(s
->request
, t
->auth_mechanisms
, t
->cred
) < 0)
560 t
->cred
->credtype
== GIT_CREDTYPE_DEFAULT
&&
561 apply_default_credentials(s
->request
, t
->auth_mechanisms
) < 0)
564 /* If no other credentials have been applied and the URL has username and
565 * password, use those */
566 if (!t
->cred
&& t
->connection_data
.user
&& t
->connection_data
.pass
) {
568 git_cred_userpass_plaintext_new(&t
->url_cred
, t
->connection_data
.user
, t
->connection_data
.pass
) < 0)
570 if (apply_userpass_credential(s
->request
, GIT_WINHTTP_AUTH_BASIC
, t
->url_cred
) < 0)
574 /* We've done everything up to calling WinHttpSendRequest. */
580 winhttp_stream_close(s
);
582 git__free(proxy_url
);
587 static int parse_unauthorized_response(
590 int *allowed_mechanisms
)
592 DWORD supported
, first
, target
;
595 *allowed_mechanisms
= 0;
597 /* WinHttpQueryHeaders() must be called before WinHttpQueryAuthSchemes().
598 * We can assume this was already done, since we know we are unauthorized.
600 if (!WinHttpQueryAuthSchemes(request
, &supported
, &first
, &target
)) {
601 giterr_set(GITERR_OS
, "failed to parse supported auth schemes");
605 if (WINHTTP_AUTH_SCHEME_NTLM
& supported
) {
606 *allowed_types
|= GIT_CREDTYPE_USERPASS_PLAINTEXT
;
607 *allowed_types
|= GIT_CREDTYPE_DEFAULT
;
608 *allowed_mechanisms
= GIT_WINHTTP_AUTH_NEGOTIATE
;
611 if (WINHTTP_AUTH_SCHEME_NEGOTIATE
& supported
) {
612 *allowed_types
|= GIT_CREDTYPE_DEFAULT
;
613 *allowed_mechanisms
= GIT_WINHTTP_AUTH_NEGOTIATE
;
616 if (WINHTTP_AUTH_SCHEME_BASIC
& supported
) {
617 *allowed_types
|= GIT_CREDTYPE_USERPASS_PLAINTEXT
;
618 *allowed_mechanisms
|= GIT_WINHTTP_AUTH_BASIC
;
621 if (WINHTTP_AUTH_SCHEME_DIGEST
& supported
) {
622 *allowed_types
|= GIT_CREDTYPE_USERPASS_PLAINTEXT
;
623 *allowed_mechanisms
|= GIT_WINHTTP_AUTH_DIGEST
;
629 static int write_chunk(HINTERNET request
, const char *buffer
, size_t len
)
632 git_buf buf
= GIT_BUF_INIT
;
635 git_buf_printf(&buf
, "%X\r\n", len
);
637 if (git_buf_oom(&buf
))
640 if (!WinHttpWriteData(request
,
641 git_buf_cstr(&buf
), (DWORD
)git_buf_len(&buf
),
644 giterr_set(GITERR_OS
, "failed to write chunk header");
651 if (!WinHttpWriteData(request
,
654 giterr_set(GITERR_OS
, "failed to write chunk");
659 if (!WinHttpWriteData(request
,
662 giterr_set(GITERR_OS
, "failed to write chunk footer");
669 static int winhttp_close_connection(winhttp_subtransport
*t
)
674 if (!WinHttpCloseHandle(t
->connection
)) {
675 giterr_set(GITERR_OS
, "unable to close connection");
679 t
->connection
= NULL
;
683 if (!WinHttpCloseHandle(t
->session
)) {
684 giterr_set(GITERR_OS
, "unable to close session");
694 static int user_agent(git_buf
*ua
)
696 const char *custom
= git_libgit2__user_agent();
699 git_buf_PUTS(ua
, "git/1.0 (");
702 git_buf_puts(ua
, custom
);
704 git_buf_PUTS(ua
, "libgit2 " LIBGIT2_VERSION
);
706 return git_buf_putc(ua
, ')');
709 static void CALLBACK
winhttp_status(
710 HINTERNET connection
,
718 if (code
!= WINHTTP_CALLBACK_STATUS_SECURE_FAILURE
)
721 status
= *((DWORD
*)info
);
723 if ((status
& WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID
))
724 giterr_set(GITERR_NET
, "SSL certificate issued for different common name");
725 else if ((status
& WINHTTP_CALLBACK_STATUS_FLAG_CERT_DATE_INVALID
))
726 giterr_set(GITERR_NET
, "SSL certificate has expired");
727 else if ((status
& WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CA
))
728 giterr_set(GITERR_NET
, "SSL certificate signed by unknown CA");
729 else if ((status
& WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CERT
))
730 giterr_set(GITERR_NET
, "SSL certificate is invalid");
731 else if ((status
& WINHTTP_CALLBACK_STATUS_FLAG_CERT_REV_FAILED
))
732 giterr_set(GITERR_NET
, "certificate revocation check failed");
733 else if ((status
& WINHTTP_CALLBACK_STATUS_FLAG_CERT_REVOKED
))
734 giterr_set(GITERR_NET
, "SSL certificate was revoked");
735 else if ((status
& WINHTTP_CALLBACK_STATUS_FLAG_SECURITY_CHANNEL_ERROR
))
736 giterr_set(GITERR_NET
, "security libraries could not be loaded");
738 giterr_set(GITERR_NET
, "unknown security error %d", status
);
741 static int winhttp_connect(
742 winhttp_subtransport
*t
)
747 git_buf ua
= GIT_BUF_INIT
;
749 int default_timeout
= TIMEOUT_INFINITE
;
750 int default_connect_timeout
= DEFAULT_CONNECT_TIMEOUT
;
753 t
->connection
= NULL
;
756 if (git__strtol32(&port
, t
->connection_data
.port
, NULL
, 10) < 0)
760 if (git__utf8_to_16_alloc(&wide_host
, t
->connection_data
.host
) < 0) {
761 giterr_set(GITERR_OS
, "unable to convert host to wide characters");
765 if ((error
= user_agent(&ua
)) < 0) {
766 git__free(wide_host
);
770 if (git__utf8_to_16_alloc(&wide_ua
, git_buf_cstr(&ua
)) < 0) {
771 giterr_set(GITERR_OS
, "unable to convert host to wide characters");
772 git__free(wide_host
);
779 /* Establish session */
780 t
->session
= WinHttpOpen(
782 WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
,
783 WINHTTP_NO_PROXY_NAME
,
784 WINHTTP_NO_PROXY_BYPASS
,
788 giterr_set(GITERR_OS
, "failed to init WinHTTP");
792 if (!WinHttpSetTimeouts(t
->session
, default_timeout
, default_connect_timeout
, default_timeout
, default_timeout
)) {
793 giterr_set(GITERR_OS
, "failed to set timeouts for WinHTTP");
798 /* Establish connection */
799 t
->connection
= WinHttpConnect(
802 (INTERNET_PORT
) port
,
805 if (!t
->connection
) {
806 giterr_set(GITERR_OS
, "failed to connect to host");
810 if (WinHttpSetStatusCallback(t
->connection
, winhttp_status
, WINHTTP_CALLBACK_FLAG_SECURE_FAILURE
, 0) == WINHTTP_INVALID_STATUS_CALLBACK
) {
811 giterr_set(GITERR_OS
, "failed to set status callback");
819 winhttp_close_connection(t
);
821 git__free(wide_host
);
827 static int do_send_request(winhttp_stream
*s
, size_t len
, int ignore_length
)
830 if (!WinHttpSendRequest(s
->request
,
831 WINHTTP_NO_ADDITIONAL_HEADERS
, 0,
832 WINHTTP_NO_REQUEST_DATA
, 0,
833 WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH
, 0)) {
837 if (!WinHttpSendRequest(s
->request
,
838 WINHTTP_NO_ADDITIONAL_HEADERS
, 0,
839 WINHTTP_NO_REQUEST_DATA
, 0,
848 static int send_request(winhttp_stream
*s
, size_t len
, int ignore_length
)
850 int request_failed
= 0, cert_valid
= 1, error
= 0;
854 if ((error
= do_send_request(s
, len
, ignore_length
)) < 0) {
855 if (GetLastError() != ERROR_WINHTTP_SECURE_FAILURE
) {
856 giterr_set(GITERR_OS
, "failed to send request");
865 if ((error
= certificate_check(s
, cert_valid
)) < 0) {
867 giterr_set(GITERR_OS
, "user cancelled certificate check");
872 /* if neither the request nor the certificate check returned errors, we're done */
876 ignore_flags
= no_check_cert_flags
;
878 if (!WinHttpSetOption(s
->request
, WINHTTP_OPTION_SECURITY_FLAGS
, &ignore_flags
, sizeof(ignore_flags
))) {
879 giterr_set(GITERR_OS
, "failed to set security options");
883 if ((error
= do_send_request(s
, len
, ignore_length
)) < 0)
884 giterr_set(GITERR_OS
, "failed to send request");
889 static int winhttp_stream_read(
890 git_smart_subtransport_stream
*stream
,
895 winhttp_stream
*s
= (winhttp_stream
*)stream
;
896 winhttp_subtransport
*t
= OWNING_SUBTRANSPORT(s
);
898 char replay_count
= 0;
902 /* Enforce a reasonable cap on the number of replays */
903 if (++replay_count
>= 7) {
904 giterr_set(GITERR_NET
, "too many redirects or authentication replays");
908 /* Connect if necessary */
909 if (!s
->request
&& winhttp_stream_connect(s
) < 0)
912 if (!s
->received_response
) {
913 DWORD status_code
, status_code_length
, content_type_length
, bytes_written
;
914 char expected_content_type_8
[MAX_CONTENT_TYPE_LEN
];
915 wchar_t expected_content_type
[MAX_CONTENT_TYPE_LEN
], content_type
[MAX_CONTENT_TYPE_LEN
];
917 if (!s
->sent_request
) {
919 if ((error
= send_request(s
, s
->post_body_len
, 0)) < 0)
926 assert(s
->verb
== post_verb
);
928 /* Flush, if necessary */
929 if (s
->chunk_buffer_len
> 0 &&
930 write_chunk(s
->request
, s
->chunk_buffer
, s
->chunk_buffer_len
) < 0)
933 s
->chunk_buffer_len
= 0;
935 /* Write the final chunk. */
936 if (!WinHttpWriteData(s
->request
,
939 giterr_set(GITERR_OS
, "failed to write final chunk");
943 else if (s
->post_body
) {
945 DWORD len
= s
->post_body_len
, bytes_read
;
947 if (INVALID_SET_FILE_POINTER
== SetFilePointer(s
->post_body
,
949 NO_ERROR
!= GetLastError()) {
950 giterr_set(GITERR_OS
, "failed to reset file pointer");
954 buffer
= git__malloc(CACHED_POST_BODY_BUF_SIZE
);
959 if (!ReadFile(s
->post_body
, buffer
,
960 min(CACHED_POST_BODY_BUF_SIZE
, len
),
961 &bytes_read
, NULL
) ||
964 giterr_set(GITERR_OS
, "failed to read from temp file");
968 if (!WinHttpWriteData(s
->request
, buffer
,
969 bytes_read
, &bytes_written
)) {
971 giterr_set(GITERR_OS
, "failed to write data");
976 assert(bytes_read
== bytes_written
);
981 /* Eagerly close the temp file */
982 CloseHandle(s
->post_body
);
986 if (!WinHttpReceiveResponse(s
->request
, 0)) {
987 giterr_set(GITERR_OS
, "failed to receive response");
991 /* Verify that we got a 200 back */
992 status_code_length
= sizeof(status_code
);
994 if (!WinHttpQueryHeaders(s
->request
,
995 WINHTTP_QUERY_STATUS_CODE
| WINHTTP_QUERY_FLAG_NUMBER
,
996 WINHTTP_HEADER_NAME_BY_INDEX
,
997 &status_code
, &status_code_length
,
998 WINHTTP_NO_HEADER_INDEX
)) {
999 giterr_set(GITERR_OS
, "failed to retrieve status code");
1003 /* The implementation of WinHTTP prior to Windows 7 will not
1004 * redirect to an identical URI. Some Git hosters use self-redirects
1005 * as part of their DoS mitigation strategy. Check first to see if we
1006 * have a redirect status code, and that we haven't already streamed
1007 * a post body. (We can't replay a streamed POST.) */
1009 (HTTP_STATUS_MOVED
== status_code
||
1010 HTTP_STATUS_REDIRECT
== status_code
||
1011 (HTTP_STATUS_REDIRECT_METHOD
== status_code
&&
1012 get_verb
== s
->verb
) ||
1013 HTTP_STATUS_REDIRECT_KEEP_VERB
== status_code
)) {
1015 /* Check for Windows 7. This workaround is only necessary on
1016 * Windows Vista and earlier. Windows 7 is version 6.1. */
1018 DWORD location_length
;
1021 /* OK, fetch the Location header from the redirect. */
1022 if (WinHttpQueryHeaders(s
->request
,
1023 WINHTTP_QUERY_LOCATION
,
1024 WINHTTP_HEADER_NAME_BY_INDEX
,
1025 WINHTTP_NO_OUTPUT_BUFFER
,
1027 WINHTTP_NO_HEADER_INDEX
) ||
1028 GetLastError() != ERROR_INSUFFICIENT_BUFFER
) {
1029 giterr_set(GITERR_OS
, "failed to read Location header");
1033 location
= git__malloc(location_length
);
1034 GITERR_CHECK_ALLOC(location
);
1036 if (!WinHttpQueryHeaders(s
->request
,
1037 WINHTTP_QUERY_LOCATION
,
1038 WINHTTP_HEADER_NAME_BY_INDEX
,
1041 WINHTTP_NO_HEADER_INDEX
)) {
1042 giterr_set(GITERR_OS
, "failed to read Location header");
1043 git__free(location
);
1047 /* Convert the Location header to UTF-8 */
1048 if (git__utf16_to_8_alloc(&location8
, location
) < 0) {
1049 giterr_set(GITERR_OS
, "failed to convert Location header to UTF-8");
1050 git__free(location
);
1054 git__free(location
);
1056 /* Replay the request */
1057 winhttp_stream_close(s
);
1059 if (!git__prefixcmp_icase(location8
, prefix_https
)) {
1060 /* Upgrade to secure connection; disconnect and start over */
1061 if (gitno_connection_data_from_url(&t
->connection_data
, location8
, s
->service_url
) < 0) {
1062 git__free(location8
);
1066 winhttp_close_connection(t
);
1068 if (winhttp_connect(t
) < 0)
1072 git__free(location8
);
1076 /* Handle proxy authentication failures */
1077 if (status_code
== HTTP_STATUS_PROXY_AUTH_REQ
) {
1080 if (parse_unauthorized_response(s
->request
, &allowed_types
, &t
->auth_mechanisms
) < 0)
1083 /* TODO: extract the username from the url, no payload? */
1084 if (t
->owner
->proxy
.credentials
) {
1086 cred_error
= t
->owner
->proxy
.credentials(&t
->proxy_cred
, t
->owner
->proxy
.url
, NULL
, allowed_types
, NULL
);
1092 winhttp_stream_close(s
);
1096 /* Handle authentication failures */
1097 if (HTTP_STATUS_DENIED
== status_code
&& get_verb
== s
->verb
) {
1100 if (parse_unauthorized_response(s
->request
, &allowed_types
, &t
->auth_mechanisms
) < 0)
1103 if (allowed_types
) {
1106 git_cred_free(t
->cred
);
1108 /* Start with the user-supplied credential callback, if present */
1109 if (t
->owner
->cred_acquire_cb
) {
1110 cred_error
= t
->owner
->cred_acquire_cb(&t
->cred
, t
->owner
->url
,
1111 t
->connection_data
.user
, allowed_types
, t
->owner
->cred_acquire_payload
);
1113 /* Treat GIT_PASSTHROUGH as though git_cred_acquire_cb isn't set */
1114 if (cred_error
== GIT_PASSTHROUGH
)
1116 else if (cred_error
< 0)
1120 /* Invoke the fallback credentials acquisition callback if necessary */
1121 if (cred_error
> 0) {
1122 cred_error
= fallback_cred_acquire_cb(&t
->cred
, t
->owner
->url
,
1123 t
->connection_data
.user
, allowed_types
, NULL
);
1132 winhttp_stream_close(s
);
1134 /* Successfully acquired a credential */
1140 if (HTTP_STATUS_OK
!= status_code
) {
1141 giterr_set(GITERR_NET
, "request failed with status code: %d", status_code
);
1145 /* Verify that we got the correct content-type back */
1146 if (post_verb
== s
->verb
)
1147 p_snprintf(expected_content_type_8
, MAX_CONTENT_TYPE_LEN
, "application/x-git-%s-result", s
->service
);
1149 p_snprintf(expected_content_type_8
, MAX_CONTENT_TYPE_LEN
, "application/x-git-%s-advertisement", s
->service
);
1151 if (git__utf8_to_16(expected_content_type
, MAX_CONTENT_TYPE_LEN
, expected_content_type_8
) < 0) {
1152 giterr_set(GITERR_OS
, "failed to convert expected content-type to wide characters");
1156 content_type_length
= sizeof(content_type
);
1158 if (!WinHttpQueryHeaders(s
->request
,
1159 WINHTTP_QUERY_CONTENT_TYPE
,
1160 WINHTTP_HEADER_NAME_BY_INDEX
,
1161 &content_type
, &content_type_length
,
1162 WINHTTP_NO_HEADER_INDEX
)) {
1163 giterr_set(GITERR_OS
, "failed to retrieve response content-type");
1167 if (wcscmp(expected_content_type
, content_type
)) {
1168 giterr_set(GITERR_NET
, "received unexpected content-type");
1172 s
->received_response
= 1;
1175 if (!WinHttpReadData(s
->request
,
1180 giterr_set(GITERR_OS
, "failed to read data");
1184 *bytes_read
= dw_bytes_read
;
1189 static int winhttp_stream_write_single(
1190 git_smart_subtransport_stream
*stream
,
1194 winhttp_stream
*s
= (winhttp_stream
*)stream
;
1195 DWORD bytes_written
;
1198 if (!s
->request
&& winhttp_stream_connect(s
) < 0)
1201 /* This implementation of write permits only a single call. */
1202 if (s
->sent_request
) {
1203 giterr_set(GITERR_NET
, "subtransport configured for only one write");
1207 if ((error
= send_request(s
, len
, 0)) < 0)
1210 s
->sent_request
= 1;
1212 if (!WinHttpWriteData(s
->request
,
1216 giterr_set(GITERR_OS
, "failed to write data");
1220 assert((DWORD
)len
== bytes_written
);
1225 static int put_uuid_string(LPWSTR buffer
, size_t buffer_len_cch
)
1228 RPC_STATUS status
= UuidCreate(&uuid
);
1231 if (RPC_S_OK
!= status
&&
1232 RPC_S_UUID_LOCAL_ONLY
!= status
&&
1233 RPC_S_UUID_NO_ADDRESS
!= status
) {
1234 giterr_set(GITERR_NET
, "unable to generate name for temp file");
1238 if (buffer_len_cch
< UUID_LENGTH_CCH
+ 1) {
1239 giterr_set(GITERR_NET
, "buffer too small for name of temp file");
1243 #if !defined(__MINGW32__) || defined(MINGW_HAS_SECURE_API)
1244 result
= swprintf_s(buffer
, buffer_len_cch
,
1246 result
= wsprintfW(buffer
,
1248 L
"%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x",
1249 uuid
.Data1
, uuid
.Data2
, uuid
.Data3
,
1250 uuid
.Data4
[0], uuid
.Data4
[1], uuid
.Data4
[2], uuid
.Data4
[3],
1251 uuid
.Data4
[4], uuid
.Data4
[5], uuid
.Data4
[6], uuid
.Data4
[7]);
1253 if (result
< UUID_LENGTH_CCH
) {
1254 giterr_set(GITERR_OS
, "unable to generate name for temp file");
1261 static int get_temp_file(LPWSTR buffer
, DWORD buffer_len_cch
)
1265 if (!GetTempPathW(buffer_len_cch
, buffer
)) {
1266 giterr_set(GITERR_OS
, "failed to get temp path");
1270 len
= wcslen(buffer
);
1272 if (buffer
[len
- 1] != '\\' && len
< buffer_len_cch
)
1273 buffer
[len
++] = '\\';
1275 if (put_uuid_string(&buffer
[len
], (size_t)buffer_len_cch
- len
) < 0)
1281 static int winhttp_stream_write_buffered(
1282 git_smart_subtransport_stream
*stream
,
1286 winhttp_stream
*s
= (winhttp_stream
*)stream
;
1287 DWORD bytes_written
;
1289 if (!s
->request
&& winhttp_stream_connect(s
) < 0)
1292 /* Buffer the payload, using a temporary file so we delegate
1293 * memory management of the data to the operating system. */
1294 if (!s
->post_body
) {
1295 wchar_t temp_path
[MAX_PATH
+ 1];
1297 if (get_temp_file(temp_path
, MAX_PATH
+ 1) < 0)
1300 s
->post_body
= CreateFileW(temp_path
,
1301 GENERIC_READ
| GENERIC_WRITE
,
1302 FILE_SHARE_DELETE
, NULL
,
1304 FILE_ATTRIBUTE_TEMPORARY
| FILE_FLAG_DELETE_ON_CLOSE
| FILE_FLAG_SEQUENTIAL_SCAN
,
1307 if (INVALID_HANDLE_VALUE
== s
->post_body
) {
1308 s
->post_body
= NULL
;
1309 giterr_set(GITERR_OS
, "failed to create temporary file");
1314 if (!WriteFile(s
->post_body
, buffer
, (DWORD
)len
, &bytes_written
, NULL
)) {
1315 giterr_set(GITERR_OS
, "failed to write to temporary file");
1319 assert((DWORD
)len
== bytes_written
);
1321 s
->post_body_len
+= bytes_written
;
1326 static int winhttp_stream_write_chunked(
1327 git_smart_subtransport_stream
*stream
,
1331 winhttp_stream
*s
= (winhttp_stream
*)stream
;
1334 if (!s
->request
&& winhttp_stream_connect(s
) < 0)
1337 if (!s
->sent_request
) {
1338 /* Send Transfer-Encoding: chunked header */
1339 if (!WinHttpAddRequestHeaders(s
->request
,
1340 transfer_encoding
, (ULONG
) -1L,
1341 WINHTTP_ADDREQ_FLAG_ADD
)) {
1342 giterr_set(GITERR_OS
, "failed to add a header to the request");
1346 if ((error
= send_request(s
, 0, 1)) < 0)
1349 s
->sent_request
= 1;
1352 if (len
> CACHED_POST_BODY_BUF_SIZE
) {
1353 /* Flush, if necessary */
1354 if (s
->chunk_buffer_len
> 0) {
1355 if (write_chunk(s
->request
, s
->chunk_buffer
, s
->chunk_buffer_len
) < 0)
1358 s
->chunk_buffer_len
= 0;
1361 /* Write chunk directly */
1362 if (write_chunk(s
->request
, buffer
, len
) < 0)
1366 /* Append as much to the buffer as we can */
1367 int count
= (int)min(CACHED_POST_BODY_BUF_SIZE
- s
->chunk_buffer_len
, len
);
1369 if (!s
->chunk_buffer
)
1370 s
->chunk_buffer
= git__malloc(CACHED_POST_BODY_BUF_SIZE
);
1372 memcpy(s
->chunk_buffer
+ s
->chunk_buffer_len
, buffer
, count
);
1373 s
->chunk_buffer_len
+= count
;
1377 /* Is the buffer full? If so, then flush */
1378 if (CACHED_POST_BODY_BUF_SIZE
== s
->chunk_buffer_len
) {
1379 if (write_chunk(s
->request
, s
->chunk_buffer
, s
->chunk_buffer_len
) < 0)
1382 s
->chunk_buffer_len
= 0;
1384 /* Is there any remaining data from the source? */
1386 memcpy(s
->chunk_buffer
, buffer
, len
);
1387 s
->chunk_buffer_len
= (unsigned int)len
;
1395 static void winhttp_stream_free(git_smart_subtransport_stream
*stream
)
1397 winhttp_stream
*s
= (winhttp_stream
*)stream
;
1399 winhttp_stream_close(s
);
1403 static int winhttp_stream_alloc(winhttp_subtransport
*t
, winhttp_stream
**stream
)
1410 s
= git__calloc(1, sizeof(winhttp_stream
));
1411 GITERR_CHECK_ALLOC(s
);
1413 s
->parent
.subtransport
= &t
->parent
;
1414 s
->parent
.read
= winhttp_stream_read
;
1415 s
->parent
.write
= winhttp_stream_write_single
;
1416 s
->parent
.free
= winhttp_stream_free
;
1423 static int winhttp_uploadpack_ls(
1424 winhttp_subtransport
*t
,
1429 s
->service
= upload_pack_service
;
1430 s
->service_url
= upload_pack_ls_service_url
;
1436 static int winhttp_uploadpack(
1437 winhttp_subtransport
*t
,
1442 s
->service
= upload_pack_service
;
1443 s
->service_url
= upload_pack_service_url
;
1444 s
->verb
= post_verb
;
1449 static int winhttp_receivepack_ls(
1450 winhttp_subtransport
*t
,
1455 s
->service
= receive_pack_service
;
1456 s
->service_url
= receive_pack_ls_service_url
;
1462 static int winhttp_receivepack(
1463 winhttp_subtransport
*t
,
1468 /* WinHTTP only supports Transfer-Encoding: chunked
1469 * on Windows Vista (NT 6.0) and higher. */
1470 s
->chunked
= git_has_win32_version(6, 0, 0);
1473 s
->parent
.write
= winhttp_stream_write_chunked
;
1475 s
->parent
.write
= winhttp_stream_write_buffered
;
1477 s
->service
= receive_pack_service
;
1478 s
->service_url
= receive_pack_service_url
;
1479 s
->verb
= post_verb
;
1484 static int winhttp_action(
1485 git_smart_subtransport_stream
**stream
,
1486 git_smart_subtransport
*subtransport
,
1488 git_smart_service_t action
)
1490 winhttp_subtransport
*t
= (winhttp_subtransport
*)subtransport
;
1495 if ((ret
= gitno_connection_data_from_url(&t
->connection_data
, url
, NULL
)) < 0 ||
1496 (ret
= winhttp_connect(t
)) < 0)
1499 if (winhttp_stream_alloc(t
, &s
) < 0)
1507 case GIT_SERVICE_UPLOADPACK_LS
:
1508 ret
= winhttp_uploadpack_ls(t
, s
);
1511 case GIT_SERVICE_UPLOADPACK
:
1512 ret
= winhttp_uploadpack(t
, s
);
1515 case GIT_SERVICE_RECEIVEPACK_LS
:
1516 ret
= winhttp_receivepack_ls(t
, s
);
1519 case GIT_SERVICE_RECEIVEPACK
:
1520 ret
= winhttp_receivepack(t
, s
);
1528 *stream
= &s
->parent
;
1533 static int winhttp_close(git_smart_subtransport
*subtransport
)
1535 winhttp_subtransport
*t
= (winhttp_subtransport
*)subtransport
;
1537 gitno_connection_data_free_ptrs(&t
->connection_data
);
1538 memset(&t
->connection_data
, 0x0, sizeof(gitno_connection_data
));
1539 gitno_connection_data_free_ptrs(&t
->proxy_connection_data
);
1540 memset(&t
->proxy_connection_data
, 0x0, sizeof(gitno_connection_data
));
1543 t
->cred
->free(t
->cred
);
1547 if (t
->proxy_cred
) {
1548 t
->proxy_cred
->free(t
->proxy_cred
);
1549 t
->proxy_cred
= NULL
;
1553 t
->url_cred
->free(t
->url_cred
);
1557 return winhttp_close_connection(t
);
1560 static void winhttp_free(git_smart_subtransport
*subtransport
)
1562 winhttp_subtransport
*t
= (winhttp_subtransport
*)subtransport
;
1564 winhttp_close(subtransport
);
1569 int git_smart_subtransport_http(git_smart_subtransport
**out
, git_transport
*owner
, void *param
)
1571 winhttp_subtransport
*t
;
1578 t
= git__calloc(1, sizeof(winhttp_subtransport
));
1579 GITERR_CHECK_ALLOC(t
);
1581 t
->owner
= (transport_smart
*)owner
;
1582 t
->parent
.action
= winhttp_action
;
1583 t
->parent
.close
= winhttp_close
;
1584 t
->parent
.free
= winhttp_free
;
1586 *out
= (git_smart_subtransport
*) t
;
1590 #endif /* GIT_WINHTTP */