Skip to content

Commit 203494e

Browse files
committed
fetcher,winhttp: force TLS 1.2 on Win 8.0 and 7
On ancient Windows, we must opt-in to using TLS 1.2. Otherwise it only allows for TLS 1.0. And of course there's no TLS 1.3 support there at all. Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent ff64ab1 commit 203494e

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

installer/fetcher/fetcher.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ static DWORD __stdcall download_thread(void *param)
114114
if (!session)
115115
goto out;
116116
WinHttpSetOption(session, WINHTTP_OPTION_ENABLE_HTTP_PROTOCOL, &enable_http2, sizeof(enable_http2)); // Don't check return value, in case of old Windows
117+
if (is_win8dotzero_or_below()) {
118+
DWORD enable_tls12 = WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2;
119+
if (!WinHttpSetOption(session, WINHTTP_OPTION_SECURE_PROTOCOLS, &enable_tls12, sizeof(enable_tls12)))
120+
goto out;
121+
}
122+
117123
connection = WinHttpConnect(session, L(server), port, 0);
118124
if (!connection)
119125
goto out;

installer/fetcher/systeminfo.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,10 @@ bool is_win7(void)
6565
RtlGetNtVersionNumbers(&maj, &min, &build);
6666
return maj == 6 && min == 1;
6767
}
68+
69+
bool is_win8dotzero_or_below(void)
70+
{
71+
DWORD maj, min, build;
72+
RtlGetNtVersionNumbers(&maj, &min, &build);
73+
return maj == 6 && min <= 2;
74+
}

installer/fetcher/systeminfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
const char *architecture(void);
1212
const char *useragent(void);
1313
bool is_win7(void);
14+
bool is_win8dotzero_or_below(void);
1415

1516
#endif

updater/winhttp/syscall_windows.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,16 @@ const (
256256
_INTERNET_SCHEME_FTP = 3
257257
_INTERNET_SCHEME_SOCKS = 4
258258

259+
_WINHTTP_FLAG_SECURE_PROTOCOL_SSL2 = 0x00000008
260+
_WINHTTP_FLAG_SECURE_PROTOCOL_SSL3 = 0x00000020
261+
_WINHTTP_FLAG_SECURE_PROTOCOL_TLS1 = 0x00000080
262+
_WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 = 0x00000200
263+
_WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2 = 0x00000800
264+
_WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3 = 0x00002000
265+
_WINHTTP_FLAG_SECURE_PROTOCOL_ALL = _WINHTTP_FLAG_SECURE_PROTOCOL_SSL2 | _WINHTTP_FLAG_SECURE_PROTOCOL_SSL3 | _WINHTTP_FLAG_SECURE_PROTOCOL_TLS1
266+
267+
_WINHTTP_PROTOCOL_FLAG_HTTP2 = 0x1
268+
259269
_WINHTTP_ERROR_BASE = 12000
260270
_ERROR_WINHTTP_OUT_OF_HANDLES = Error(12000 + 1)
261271
_ERROR_WINHTTP_TIMEOUT = Error(12000 + 2)

updater/winhttp/winhttp.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ func isWin7() bool {
4848
return maj < 6 || (maj == 6 && min <= 1)
4949
}
5050

51+
func isWin8DotZeroOrBelow() bool {
52+
maj, min, _ := windows.RtlGetNtVersionNumbers()
53+
return maj < 6 || (maj == 6 && min <= 2)
54+
}
55+
5156
func NewSession(userAgent string) (session *Session, err error) {
5257
session = new(Session)
5358
defer convertError(&err)
@@ -69,9 +74,17 @@ func NewSession(userAgent string) (session *Session, err error) {
6974
if err != nil {
7075
return
7176
}
72-
var enableHttp2 uint32 = 1
77+
var enableHttp2 uint32 = _WINHTTP_PROTOCOL_FLAG_HTTP2
7378
_ = winHttpSetOption(session.handle, _WINHTTP_OPTION_ENABLE_HTTP_PROTOCOL, unsafe.Pointer(&enableHttp2), uint32(unsafe.Sizeof(enableHttp2))) // Don't check return value, in case of old Windows
7479

80+
if isWin8DotZeroOrBelow() {
81+
var enableTLS12 uint32 = _WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2
82+
err = winHttpSetOption(session.handle, _WINHTTP_OPTION_SECURE_PROTOCOLS, unsafe.Pointer(&enableTLS12), uint32(unsafe.Sizeof(enableTLS12)))
83+
if err != nil {
84+
return
85+
}
86+
}
87+
7588
runtime.SetFinalizer(session, func(session *Session) {
7689
session.Close()
7790
})

0 commit comments

Comments
 (0)