Skip to content

Open-source Xbox-inspired desktop launcher for Xodus on Linux

TypeScript
29.4 KB
Raw Blame
--- a/dlls/xgameruntime/xthreading.c
+++ b/dlls/xgameruntime/xthreading.c
@@ -234,6 +234,9 @@
     BOOL completed;
     HRESULT status;
     LONG cleaned;
+    XTaskQueueHandle retained_queue;
+    BOOL completion_finished;
+    BOOL provider_cleanup_finished;
 };
 
 static inline struct async_state *async_state( XAsyncBlock *asyncBlock )
@@ -241,6 +244,21 @@
     return (struct async_state *)asyncBlock->internal[0];
 }
 
+/* Like the native GDK and mixutin's queue-lifetime fix, Begin owns a queue
+ * reference until provider cleanup and the completion callback both finish. */
+static void async_release_queue( struct async_state *state )
+{
+    XTaskQueueHandle queue = NULL;
+    EnterCriticalSection( &state->cs );
+    if (state->provider_cleanup_finished && !state->active_provider && state->completion_finished)
+    {
+        queue = state->retained_queue;
+        state->retained_queue = NULL;
+    }
+    LeaveCriticalSection( &state->cs );
+    if (queue) IUnknown_Release( &queue->IUnknown_iface );
+}
+
 static void async_cleanup_state( struct async_state *state, XAsyncProvider *provider )
 {
     BOOL cleanup = FALSE;
@@ -253,7 +271,13 @@
     }
     LeaveCriticalSection( &state->cs );
     if (cleanup)
+    {
         provider( XAsyncOp_Cleanup, &state->data );
+        EnterCriticalSection( &state->cs );
+        state->provider_cleanup_finished = TRUE;
+        LeaveCriticalSection( &state->cs );
+    }
+    async_release_queue( state );
 }
 
 static HRESULT async_invoke_provider( struct async_state *state, XAsyncOp op )
@@ -276,7 +300,14 @@
         cleanup = TRUE;
     }
     LeaveCriticalSection( &state->cs );
-    if (cleanup) state->provider( XAsyncOp_Cleanup, &state->data );
+    if (cleanup)
+    {
+        state->provider( XAsyncOp_Cleanup, &state->data );
+        EnterCriticalSection( &state->cs );
+        state->provider_cleanup_finished = TRUE;
+        LeaveCriticalSection( &state->cs );
+    }
+    async_release_queue( state );
     return hr;
 }
 
@@ -287,18 +318,34 @@
 
 static HRESULT WINAPI x_threading_XAsyncGetStatus( IXThreadingImpl *iface, XAsyncBlock *asyncBlock, BOOLEAN wait )
 {
+    struct async_state *state;
+    HRESULT result;
     TRACE( "iface %p, asyncBlock %p, wait %d.\n", iface, asyncBlock, wait );
-    if (!asyncBlock->internal[3]) return E_INVALIDARG;
-    if (WaitForSingleObject( asyncBlock->internal[3], wait ? INFINITE : 0 ) == WAIT_TIMEOUT) return E_PENDING;
-    TRACE( "asyncBlock %p -> status %#lx.\n", asyncBlock, async_state( asyncBlock )->status );
-    return async_state( asyncBlock )->status;
+    if (!asyncBlock || !asyncBlock->internal[0] || !asyncBlock->internal[3]) return E_INVALIDARG;
+    state = async_state( asyncBlock );
+    /* Polling reports the work result. Waiting also waits for the completion
+     * callback to return, so the caller can observe its final side effects. */
+    if (wait && WaitForSingleObject( state->provider_block.internal[3], INFINITE ) == WAIT_FAILED)
+        return HRESULT_FROM_WIN32( GetLastError() );
+    EnterCriticalSection( &state->cs );
+    result = state->status;
+    LeaveCriticalSection( &state->cs );
+    TRACE( "asyncBlock %p -> status %#lx.\n", asyncBlock, result );
+    return result;
 }
 
 static HRESULT WINAPI x_threading_XAsyncGetResultSize( IXThreadingImpl *iface, XAsyncBlock *asyncBlock, SIZE_T *bufferSize )
 {
+    struct async_state *state;
+    HRESULT result;
     TRACE( "iface %p, asyncBlock %p, bufferSize %p.\n", iface, asyncBlock, bufferSize );
-    *bufferSize = ((XAsyncProviderData *)asyncBlock->internal[0])->bufferSize;
-    return S_OK;
+    if (!asyncBlock || !asyncBlock->internal[0] || !bufferSize) return E_INVALIDARG;
+    state = async_state( asyncBlock );
+    EnterCriticalSection( &state->cs );
+    *bufferSize = state->data.bufferSize;
+    result = state->status;
+    LeaveCriticalSection( &state->cs );
+    return result;
 }
 
 static void WINAPI x_threading_XAsyncCancel( IXThreadingImpl *iface, XAsyncBlock *asyncBlock )
@@ -347,12 +394,19 @@
 
     if (!asyncBlock || !provider) return E_INVALIDARG;
     queue = asyncBlock->queue;
-    if (!queue && !IXThreadingImpl_XTaskQueueGetCurrentProcessTaskQueue( iface, &queue )) return E_INVALIDARG;
-    if (!(state = calloc( 1, sizeof(*state) ))) return E_OUTOFMEMORY;
+    if (queue) IUnknown_AddRef( &queue->IUnknown_iface );
+    else if (!IXThreadingImpl_XTaskQueueGetCurrentProcessTaskQueue( iface, &queue )) return E_INVALIDARG;
+    if (!(state = calloc( 1, sizeof(*state) )))
+    {
+        IUnknown_Release( &queue->IUnknown_iface );
+        return E_OUTOFMEMORY;
+    }
     if (!(event = CreateEventA( NULL, TRUE, FALSE, NULL )))
     {
+        hr = HRESULT_FROM_WIN32( GetLastError() );
         free( state );
-        return HRESULT_FROM_WIN32( GetLastError() );
+        IUnknown_Release( &queue->IUnknown_iface );
+        return hr;
     }
     InitializeCriticalSection( &state->cs );
     state->status = E_PENDING;
@@ -361,6 +415,7 @@
     state->callback = asyncBlock->callback;
     state->provider_block = *asyncBlock;
     state->provider_block.queue = queue;
+    state->retained_queue = queue;
     data = &state->data;
     asyncBlock->internal[0] = data;
     asyncBlock->internal[1] = provider;
@@ -409,14 +464,18 @@
 static void CALLBACK async_completion_callback( struct async_state *state, BOOLEAN canceled )
 {
     if (canceled) async_invoke_provider( state, XAsyncOp_Cancel );
-    SetEvent( state->provider_block.internal[3] );
     if (!canceled) state->callback( state->user_block );
+    EnterCriticalSection( &state->cs );
+    state->completion_finished = TRUE;
+    LeaveCriticalSection( &state->cs );
+    SetEvent( state->provider_block.internal[3] );
     /* The application may delete or reuse XAsyncBlock inside its completion
      * callback. Only the runtime-owned state may be touched after it returns.
      * GetResult may already have cleaned the provider; cleaned makes this
      * idempotent without dereferencing the caller's freed block. */
     if (canceled || FAILED(state->status) || !state->data.bufferSize)
         async_cleanup_state( state, state->provider );
+    async_release_queue( state );
 }
 
 static void WINAPI x_threading_XAsyncComplete( IXThreadingImpl *iface, XAsyncBlock *asyncBlock, HRESULT result, SIZE_T requiredBufferSize )
@@ -437,20 +496,37 @@
     if (state->callback &&
         SUCCEEDED(IXThreadingImpl_XTaskQueueSubmitCallback( iface, state->provider_block.queue, XTaskQueuePort_Completion, state, (XTaskQueueCallback *)async_completion_callback )))
         return;
+    EnterCriticalSection( &state->cs );
+    state->completion_finished = TRUE;
+    LeaveCriticalSection( &state->cs );
     SetEvent( state->provider_block.internal[3] );
     if (FAILED(result) || !requiredBufferSize) async_cleanup_state( state, state->provider );
+    async_release_queue( state );
 }
 
 static HRESULT WINAPI x_threading_XAsyncGetResult( IXThreadingImpl *iface, XAsyncBlock *asyncBlock, const void *identity, SIZE_T bufferSize, void *buffer, SIZE_T *bufferUsed )
 {
+    struct async_state *state;
+    SIZE_T required;
     HRESULT hr;
     TRACE( "iface %p asyncBlock %p, identity %p, bufferSize %Iu, buffer %p, bufferUsed %p.\n", iface, asyncBlock, identity, bufferSize, buffer, bufferUsed );
-    if (WaitForSingleObject( asyncBlock->internal[3], 0 ) == WAIT_TIMEOUT) return E_PENDING;
-    if (FAILED(hr = async_state( asyncBlock )->status)) return hr;
-    if (bufferSize < async_state( asyncBlock )->data.bufferSize) return HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER );
-    async_state( asyncBlock )->data.buffer = buffer;
-    hr = async_invoke_provider( async_state( asyncBlock ), XAsyncOp_GetResult );
-    if (bufferUsed) *bufferUsed = SUCCEEDED(hr) ? async_state( asyncBlock )->data.bufferSize : 0;
+    if (FAILED(hr = x_threading_XAsyncGetStatus( iface, asyncBlock, FALSE ))) return hr;
+    state = async_state( asyncBlock );
+    required = state->data.bufferSize;
+    if (!required)
+    {
+        if (bufferUsed) *bufferUsed = 0;
+        return S_OK;
+    }
+    if (identity != asyncBlock->internal[2] || !buffer) return E_INVALIDARG;
+    if (bufferSize < required) return HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER );
+    /* GDK supplies the caller's capacity to GetResult, not the minimum
+     * payload size reported at Complete. Keep bufferUsed at that minimum. */
+    state->data.bufferSize = bufferSize;
+    state->data.buffer = buffer;
+    hr = async_invoke_provider( state, XAsyncOp_GetResult );
+    state->data.bufferSize = required;
+    if (bufferUsed) *bufferUsed = SUCCEEDED(hr) ? required : 0;
     async_cleanup( asyncBlock );
     return hr;
 }
@@ -550,6 +626,7 @@
 {
     XTaskQueueHandle queue = entry->queue;
     XTaskQueuePortHandle port = entry->port;
+    TRACE( "executing queue %p port %p callback %p context %p.\n", queue, port, entry->callback, entry->callbackContext );
     if (entry->termination)
     {
         TRACE( "queue %p drained; notifying on completion port.\n", queue );
@@ -733,6 +810,7 @@
 {
     XTaskQueueHandle queue;
     HRESULT hr;
+    TRACE( "iface %p, workMode %d, completionMode %d, out %p.\n", iface, workMode, completionMode, out );
     if (!out) return E_POINTER;
     *out = NULL;
     if (FAILED(hr = create_queue( &queue ))) return hr;
@@ -752,6 +830,7 @@
 {
     XTaskQueueHandle queue;
     HRESULT hr;
+    TRACE( "iface %p, workPort %p, completionPort %p, out %p.\n", iface, workPort, completionPort, out );
     if (!out) return E_POINTER;
     *out = NULL;
     if (!workPort || !completionPort) return E_INVALIDARG;
@@ -769,6 +848,7 @@
 
 static HRESULT WINAPI x_threading_XTaskQueueGetPort( IXThreadingImpl *iface, XTaskQueueHandle queue, XTaskQueuePort port, XTaskQueuePortHandle *out )
 {
+    TRACE( "iface %p, queue %p, port %d, out %p.\n", iface, queue, port, out );
     if (!queue || !out) return E_POINTER;
     switch (port)
     {
@@ -780,6 +860,7 @@
 
 static HRESULT WINAPI x_threading_XTaskQueueDuplicateHandle( IXThreadingImpl *iface, XTaskQueueHandle queue, XTaskQueueHandle *out )
 {
+    TRACE( "iface %p, queue %p, out %p.\n", iface, queue, out );
     if (!queue || !out) return E_POINTER;
     IUnknown_AddRef( &queue->IUnknown_iface );
     *out = queue;
@@ -791,6 +872,8 @@
     XTaskQueuePortHandle port;
     ULONGLONG start = GetTickCount64();
     BOOL acquired = FALSE, result = FALSE;
+    static LONG logged;
+    if (InterlockedIncrement( &logged ) <= 50) TRACE( "queue %p, kind %d, timeoutInMs %u, vtbl %p.\n", queue, kind, timeoutInMs, queue ? *(void ***)queue : NULL );
     if (!queue)
     {
         if (!IXThreadingImpl_XTaskQueueGetCurrentProcessTaskQueue( iface, &queue )) return FALSE;
@@ -826,11 +909,13 @@
 
 static void WINAPI x_threading_XTaskQueueCloseHandle( IXThreadingImpl *iface, XTaskQueueHandle queue )
 {
+    TRACE( "iface %p, queue %p.\n", iface, queue );
     if (queue) IUnknown_Release( &queue->IUnknown_iface );
 }
 
 static HRESULT WINAPI x_threading_XTaskQueueSubmitCallback( IXThreadingImpl *iface, XTaskQueueHandle queue, XTaskQueuePort port, void *context, XTaskQueueCallback *callback )
 {
+    TRACE( "queue %p, port %d, context %p, callback %p, vtbl %p.\n", queue, port, context, callback, queue ? *(void ***)queue : NULL );
     return IXThreadingImpl_XTaskQueueSubmitDelayedCallback( iface, queue, port, 0, context, callback );
 }
 
@@ -839,6 +924,8 @@
     struct task_context *entry;
     XTaskQueuePortHandle port;
     HRESULT hr;
+    TRACE( "queue %p, kind %d, delayMs %u, context %p, callback %p, workMode %d compMode %d.\n", queue, kind, delayMs, context, callback,
+           queue ? queue->work->mode : -1, queue ? queue->completion->mode : -1 );
     if (!queue || !callback) return E_POINTER;
     if (kind != XTaskQueuePort_Work && kind != XTaskQueuePort_Completion) return E_INVALIDARG;
     if (!(entry = calloc( 1, sizeof(*entry) ))) return E_OUTOFMEMORY;
--- a/dlls/xgameruntime/xnetworking.c
+++ b/dlls/xgameruntime/xnetworking.c
@@ -20,6 +20,8 @@
  */
 
 #include "private.h"
+#include "winhttp.h"
+#include "wincrypt.h"
 
 WINE_DEFAULT_DEBUG_CHANNEL(gdkc);
 
@@ -148,46 +150,200 @@
     return TRUE;
 }
 
+static const char security_information_identity[] = "XNetworkingSecurityInformation";
+
+static HRESULT WINAPI security_information_provider( XAsyncOp op, const XAsyncProviderData *data )
+{
+    XNetworkingSecurityInformation *information;
+    switch (op)
+    {
+        case XAsyncOp_Begin:
+            return IXThreadingImpl_XAsyncSchedule( x_threading_impl, data->async, 0 );
+        case XAsyncOp_DoWork:
+            IXThreadingImpl_XAsyncComplete( x_threading_impl, data->async, S_OK, sizeof(*information) );
+            break;
+        case XAsyncOp_GetResult:
+            information = data->buffer;
+            memset( information, 0, sizeof(*information) );
+            /* Default HTTPS policy used by the community implementations.
+             * WinHTTP still validates the server chain and hostname. No
+             * title-specific pin policy has been loaded by this runtime. */
+            information->enabledHttpSecurityProtocolFlags = WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2 |
+                                                           WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3;
+            break;
+        default: break;
+    }
+    return S_OK;
+}
+
+static HRESULT begin_security_information( const WCHAR *url, XAsyncBlock *async )
+{
+    URL_COMPONENTS components = {sizeof(components)};
+    WCHAR *https_url = NULL;
+    SIZE_T length;
+    BOOL valid;
+    if (!url || !async) return E_INVALIDARG;
+    /* WSS uses the same TLS endpoint/certificate policy as HTTPS. WinHTTP's
+     * URL parser does not recognize the websocket scheme; normalize only
+     * for parsing the policy query, not for issuing a request. */
+    length = wcslen(url);
+    if (length >= 6 && CompareStringOrdinal(url,6,L"wss://",6,TRUE) == CSTR_EQUAL)
+    {
+        if (length > (SIZE_T)-1 / sizeof(*https_url) - 3) return E_INVALIDARG;
+        if (!(https_url = malloc((length + 3) * sizeof(*https_url)))) return E_OUTOFMEMORY;
+        memcpy(https_url,L"https://",8 * sizeof(*https_url));
+        memcpy(https_url + 8,url + 6,(length - 5) * sizeof(*https_url));
+        url = https_url;
+    }
+    components.dwHostNameLength = 1;
+    valid = WinHttpCrackUrl(url,0,0,&components) &&
+            components.nScheme == INTERNET_SCHEME_HTTPS && components.dwHostNameLength;
+    free(https_url);
+    if (!valid)
+    {
+        return E_INVALIDARG;
+    }
+    return IXThreadingImpl_XAsyncBegin( x_threading_impl, async, NULL,
+                                      security_information_identity, "XNetworkingSecurityInformation",
+                                      security_information_provider );
+}
+
 static HRESULT WINAPI x_networking_XNetworkingQuerySecurityInformationForUrlAsync( IXNetworkingImpl2 *iface, const char *url, XAsyncBlock *asyncBlock )
 {
-    FIXME( "iface %p, url %p, asyncBlock %p stub!\n", iface, url, asyncBlock );
-    return E_NOTIMPL;
+    WCHAR *wide;
+    int length;
+    HRESULT hr;
+    TRACE( "iface %p, url %p, asyncBlock %p.\n", iface, url, asyncBlock );
+    if (!url || !(length = MultiByteToWideChar( CP_UTF8, MB_ERR_INVALID_CHARS, url, -1, NULL, 0 ))) return E_INVALIDARG;
+    if (!(wide = malloc( length * sizeof(*wide) ))) return E_OUTOFMEMORY;
+    MultiByteToWideChar( CP_UTF8, MB_ERR_INVALID_CHARS, url, -1, wide, length );
+    hr = begin_security_information( wide, asyncBlock );
+    free( wide );
+    return hr;
 }
 
 static HRESULT WINAPI x_networking_XNetworkingQuerySecurityInformationForUrlAsyncResultSize( IXNetworkingImpl2 *iface, XAsyncBlock *asyncBlock, SIZE_T *securityInformationBufferByteCount )
 {
-    FIXME( "iface %p, asyncBlock %p, securityInformationBufferByteCount %p stub!\n", iface, asyncBlock, securityInformationBufferByteCount );
-    return E_NOTIMPL;
+    return IXThreadingImpl_XAsyncGetResultSize( x_threading_impl, asyncBlock, securityInformationBufferByteCount );
 }
 
 static HRESULT WINAPI x_networking_XNetworkingQuerySecurityInformationForUrlAsyncResult( IXNetworkingImpl2 *iface, XAsyncBlock *asyncBlock, SIZE_T securityInformationBufferByteCount, SIZE_T *securityInformationBufferByteCountUsed, UINT8 *securityInformationBuffer, XNetworkingSecurityInformation **securityInformation )
 {
-    FIXME( "iface %p, asyncBlock %p, securityInformationBufferByteCount %Iu, securityInformationBufferByteCountUsed %p, securityInformationBuffer %p, securityInformation %p stub!\n", iface, asyncBlock, securityInformationBufferByteCount, securityInformationBufferByteCountUsed, securityInformationBuffer, securityInformation );
-    return E_NOTIMPL;
+    HRESULT hr;
+    if (!securityInformation) return E_POINTER;
+    *securityInformation = NULL;
+    hr = IXThreadingImpl_XAsyncGetResult( x_threading_impl, asyncBlock, security_information_identity,
+                                        securityInformationBufferByteCount, securityInformationBuffer,
+                                        securityInformationBufferByteCountUsed );
+    if (SUCCEEDED(hr)) *securityInformation = (XNetworkingSecurityInformation *)securityInformationBuffer;
+    return hr;
 }
 
 static HRESULT WINAPI x_networking_XNetworkingQuerySecurityInformationForUrlUtf16Async( IXNetworkingImpl2 *iface, const WCHAR *url, XAsyncBlock *asyncBlock )
 {
-    FIXME( "iface %p, url %p, asyncBlock %p stub!\n", iface, url, asyncBlock );
-    return E_NOTIMPL;
+    TRACE( "iface %p, url %p, asyncBlock %p.\n", iface, url, asyncBlock );
+    return begin_security_information( url, asyncBlock );
 }
 
 static HRESULT WINAPI x_networking_XNetworkingQuerySecurityInformationForUrlUtf16AsyncResultSize( IXNetworkingImpl2 *iface, XAsyncBlock *asyncBlock, SIZE_T *securityInformationBufferByteCount )
 {
-    FIXME( "iface %p, asyncBlock %p, securityInformationBufferByteCount %p stub!\n", iface, asyncBlock, securityInformationBufferByteCount );
-    return E_NOTIMPL;
+    return x_networking_XNetworkingQuerySecurityInformationForUrlAsyncResultSize( iface, asyncBlock, securityInformationBufferByteCount );
 }
 
 static HRESULT WINAPI x_networking_XNetworkingQuerySecurityInformationForUrlUtf16AsyncResult( IXNetworkingImpl2 *iface, XAsyncBlock *asyncBlock, SIZE_T securityInformationBufferByteCount, SIZE_T *securityInformationBufferByteCountUsed, UINT8 *securityInformationBuffer, XNetworkingSecurityInformation **securityInformation )
 {
-    FIXME( "iface %p, asyncBlock %p, securityInformationBufferByteCount %Iu, securityInformationBufferByteCountUsed %p, securityInformationBuffer %p, securityInformation %p stub!\n", iface, asyncBlock, securityInformationBufferByteCount, securityInformationBufferByteCountUsed, securityInformationBuffer, securityInformation );
-    return E_NOTIMPL;
+    return x_networking_XNetworkingQuerySecurityInformationForUrlAsyncResult( iface, asyncBlock,
+        securityInformationBufferByteCount, securityInformationBufferByteCountUsed,
+        securityInformationBuffer, securityInformation );
+}
+
+static BOOL security_pin_matches( PCCERT_CHAIN_CONTEXT chain, const XNetworkingThumbprint *pin )
+{
+    ALG_ID algorithm;
+    DWORD length;
+    BYTE digest[32];
+    if (!pin->thumbprintBuffer || pin->thumbprintType < XNetworkingThumbprintType_Leaf ||
+        pin->thumbprintType > XNetworkingThumbprintType_Root) return FALSE;
+    if (pin->thumbprintBufferByteCount == 20) algorithm = CALG_SHA1;
+    else if (pin->thumbprintBufferByteCount == 32) algorithm = CALG_SHA_256;
+    else return FALSE;
+    for (DWORD i = 0; i < chain->cChain; ++i)
+    {
+        PCERT_SIMPLE_CHAIN simple = chain->rgpChain[i];
+        DWORD index;
+        PCCERT_CONTEXT certificate;
+        if (!simple->cElement) continue;
+        if (pin->thumbprintType == XNetworkingThumbprintType_Leaf) index = 0;
+        else if (pin->thumbprintType == XNetworkingThumbprintType_Root) index = simple->cElement - 1;
+        else
+        {
+            if (simple->cElement < 2) continue;
+            index = 1;
+        }
+        certificate = simple->rgpElement[index]->pCertContext;
+        length = sizeof(digest);
+        if (CryptHashCertificate( 0, algorithm, 0, certificate->pbCertEncoded,
+                                  certificate->cbCertEncoded, digest, &length ) &&
+            length == pin->thumbprintBufferByteCount && !memcmp( digest, pin->thumbprintBuffer, length )) return TRUE;
+    }
+    return FALSE;
 }
 
 static HRESULT WINAPI x_networking_XNetworkingVerifyServerCertificate( IXNetworkingImpl2 *iface, void *requestHandle, const XNetworkingSecurityInformation *securityInformation )
 {
-    FIXME( "iface %p, requestHandle %p, securityInformation %p stub!\n", iface, requestHandle, securityInformation );
-    return E_NOTIMPL;
+    PCCERT_CONTEXT certificate = NULL;
+    PCCERT_CHAIN_CONTEXT chain = NULL;
+    CERT_CHAIN_PARA chain_parameters = {sizeof(chain_parameters)};
+    SSL_EXTRA_CERT_CHAIN_POLICY_PARA ssl = {{sizeof(ssl)}, AUTHTYPE_SERVER, 0, NULL};
+    CERT_CHAIN_POLICY_PARA parameters = {sizeof(parameters), 0, &ssl};
+    CERT_CHAIN_POLICY_STATUS status = {sizeof(status)};
+    URL_COMPONENTS components = {sizeof(components)};
+    DWORD flags, length = sizeof(flags);
+    WCHAR *url = NULL, *host = NULL;
+    BOOL match = FALSE;
+    HRESULT hr = HRESULT_FROM_WIN32( ERROR_WINHTTP_SECURE_FAILURE );
+    TRACE( "iface %p, requestHandle %p, securityInformation %p.\n", iface, requestHandle, securityInformation );
+    if (!requestHandle || !securityInformation || securityInformation->thumbprintCount > 128 ||
+        (securityInformation->thumbprintCount && !securityInformation->thumbprints)) return E_INVALIDARG;
+    /* Never approve an explicitly weakened WinHTTP connection. */
+    if (!WinHttpQueryOption( requestHandle, WINHTTP_OPTION_SECURITY_FLAGS, &flags, &length )) goto win32_error;
+    if (flags & (SECURITY_FLAG_IGNORE_UNKNOWN_CA | SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
+                 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID | SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE)) goto done;
+    length = sizeof(certificate);
+    if (!WinHttpQueryOption( requestHandle, WINHTTP_OPTION_SERVER_CERT_CONTEXT, &certificate, &length )) goto win32_error;
+    length = 0;
+    if (WinHttpQueryOption( requestHandle, WINHTTP_OPTION_URL, NULL, &length ) ||
+        GetLastError() != ERROR_INSUFFICIENT_BUFFER || length > 65536 || !length) goto win32_error;
+    if (!(url = malloc( length ))) { hr = E_OUTOFMEMORY; goto done; }
+    if (!WinHttpQueryOption( requestHandle, WINHTTP_OPTION_URL, url, &length )) goto win32_error;
+    components.dwHostNameLength = (DWORD)-1;
+    if (!WinHttpCrackUrl( url, 0, 0, &components )) goto win32_error;
+    if (components.nScheme != INTERNET_SCHEME_HTTPS || !components.dwHostNameLength) goto done;
+    if (!(host = malloc( (components.dwHostNameLength + 1) * sizeof(*host) ))) { hr = E_OUTOFMEMORY; goto done; }
+    memcpy( host, components.lpszHostName, components.dwHostNameLength * sizeof(*host) );
+    host[components.dwHostNameLength] = 0;
+    ssl.pwszServerName = host;
+    if (!CertGetCertificateChain( NULL, certificate, NULL, certificate->hCertStore, &chain_parameters,
+                                  0, NULL, &chain )) goto win32_error;
+    if (!CertVerifyCertificateChainPolicy( CERT_CHAIN_POLICY_SSL, chain, &parameters, &status )) goto win32_error;
+    if (status.dwError) { hr = status.dwError; goto done; }
+    if (securityInformation->thumbprintCount)
+    {
+        for (SIZE_T i = 0; i < securityInformation->thumbprintCount; ++i)
+            if (security_pin_matches( chain, &securityInformation->thumbprints[i] )) { match = TRUE; break; }
+        if (!match) goto done;
+    }
+    hr = S_OK;
+    goto done;
+win32_error:
+    hr = HRESULT_FROM_WIN32( GetLastError() );
+    if (SUCCEEDED(hr)) hr = HRESULT_FROM_WIN32( ERROR_WINHTTP_SECURE_FAILURE );
+done:
+    if (chain) CertFreeCertificateChain( chain );
+    if (certificate) CertFreeCertificateContext( certificate );
+    free( host );
+    free( url );
+    return hr;
 }
 
 static HRESULT WINAPI x_networking_XNetworkingGetConnectivityHint( IXNetworkingImpl2 *iface, XNetworkingConnectivityHint *connectivityHint )
@@ -204,18 +360,117 @@
     return S_OK;
 }
 
+struct connectivity_registration
+{
+    struct connectivity_registration *next;
+    LONG ref;
+    UINT64 token;
+    XTaskQueueHandle queue;
+    void *context;
+    XNetworkingConnectivityHintChangedCallback *callback;
+    BOOL removed, running;
+    DWORD callback_thread;
+};
+
+static SRWLOCK connectivity_lock = SRWLOCK_INIT;
+static CONDITION_VARIABLE connectivity_changed = CONDITION_VARIABLE_INIT;
+static struct connectivity_registration *connectivity_registrations;
+static UINT64 connectivity_next_token;
+
+static void connectivity_release( struct connectivity_registration *registration )
+{
+    if (!InterlockedDecrement( &registration->ref ))
+    {
+        IXThreadingImpl_XTaskQueueCloseHandle( x_threading_impl, registration->queue );
+        free( registration );
+    }
+}
+
+static void CALLBACK connectivity_initial_callback( void *context, BOOLEAN canceled )
+{
+    struct connectivity_registration *registration = context;
+    XNetworkingConnectivityHint hint;
+    BOOL deliver;
+
+    AcquireSRWLockExclusive( &connectivity_lock );
+    deliver = !canceled && !registration->removed;
+    if (deliver)
+    {
+        registration->running = TRUE;
+        registration->callback_thread = GetCurrentThreadId();
+    }
+    ReleaseSRWLockExclusive( &connectivity_lock );
+    if (deliver)
+    {
+        x_networking_XNetworkingGetConnectivityHint( NULL, &hint );
+        registration->callback( registration->context, &hint );
+        AcquireSRWLockExclusive( &connectivity_lock );
+        registration->running = FALSE;
+        WakeAllConditionVariable( &connectivity_changed );
+        ReleaseSRWLockExclusive( &connectivity_lock );
+    }
+    connectivity_release( registration );
+}
+
 static HRESULT WINAPI x_networking_XNetworkingRegisterConnectivityHintChanged( IXNetworkingImpl2 *iface, XTaskQueueHandle queue, void *context, XNetworkingConnectivityHintChangedCallback *callback, XTaskQueueRegistrationToken *token )
 {
-    static LONG64 next_token;
+    struct connectivity_registration *registration;
+    HRESULT hr;
+
     TRACE( "iface %p, queue %p, context %p, callback %p, token %p.\n", iface, queue, context, callback, token );
     if (!callback || !token) return E_POINTER;
-    token->token = InterlockedIncrement64( &next_token );
-    return S_OK;
+    token->token = 0;
+    if (!(registration = calloc( 1, sizeof(*registration) ))) return E_OUTOFMEMORY;
+    if (queue)
+        hr = IXThreadingImpl_XTaskQueueDuplicateHandle( x_threading_impl, queue, &registration->queue );
+    else
+        hr = IXThreadingImpl_XTaskQueueGetCurrentProcessTaskQueue( x_threading_impl, &registration->queue ) ? S_OK : E_FAIL;
+    if (FAILED(hr)) { free( registration ); return hr; }
+    registration->context = context;
+    registration->callback = callback;
+    /* Local call, registration list and queued notification each own a ref.
+     * An Immediate completion may unregister itself before Submit returns. */
+    registration->ref = 3;
+    AcquireSRWLockExclusive( &connectivity_lock );
+    registration->token = token->token = ++connectivity_next_token;
+    registration->next = connectivity_registrations;
+    connectivity_registrations = registration;
+    ReleaseSRWLockExclusive( &connectivity_lock );
+    /* GDK guarantees an initial notification on the completion port. Without
+     * it middleware can wait indefinitely before making its first request. */
+    hr = IXThreadingImpl_XTaskQueueSubmitCallback( x_threading_impl, registration->queue,
+            XTaskQueuePort_Completion, registration, connectivity_initial_callback );
+    if (FAILED(hr))
+    {
+        struct connectivity_registration **cursor;
+        BOOL removed = FALSE;
+        AcquireSRWLockExclusive( &connectivity_lock );
+        for (cursor = &connectivity_registrations; *cursor && *cursor != registration; cursor = &(*cursor)->next);
+        if (*cursor) { *cursor = registration->next; registration->removed = TRUE; removed = TRUE; }
+        ReleaseSRWLockExclusive( &connectivity_lock );
+        if (removed) connectivity_release( registration ); /* list */
+        connectivity_release( registration ); /* submission failed */
+        token->token = 0;
+    }
+    connectivity_release( registration ); /* local */
+    return hr;
 }
 
 static BOOLEAN WINAPI x_networking_XNetworkingUnregisterConnectivityHintChanged( IXNetworkingImpl2 *iface, XTaskQueueRegistrationToken token, BOOLEAN wait )
 {
+    struct connectivity_registration **cursor, *registration;
     TRACE( "iface %p, token %p, wait %d.\n", iface, &token, wait );
+    AcquireSRWLockExclusive( &connectivity_lock );
+    for (cursor = &connectivity_registrations; *cursor && (*cursor)->token != token.token; cursor = &(*cursor)->next);
+    if (!(registration = *cursor)) { ReleaseSRWLockExclusive( &connectivity_lock ); return FALSE; }
+    *cursor = registration->next;
+    registration->removed = TRUE;
+    /* Queued notifications are suppressed, so a manual queue need not be
+     * pumped to unregister. Wait only for a callback already in progress. */
+    while (wait && registration->running && registration->callback_thread != GetCurrentThreadId())
+        SleepConditionVariableSRW( &connectivity_changed, &connectivity_lock, INFINITE, 0 );
+    ReleaseSRWLockExclusive( &connectivity_lock );
+    connectivity_release( registration );
     return TRUE;
 }
 
--- a/dlls/xgameruntime/Makefile.in
+++ b/dlls/xgameruntime/Makefile.in
@@ -1,6 +1,6 @@
 MODULE    = xgameruntime.dll
 EXTRAINCL = $(XML2_PE_CFLAGS)
-IMPORTS   = bcrypt combase shlwapi winhttp wininet ws2_32 $(XML2_PE_LIBS)
+IMPORTS   = bcrypt combase crypt32 shlwapi winhttp wininet ws2_32 $(XML2_PE_LIBS)
 
 SOURCES = \
 	main.c \