Skip to content

Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit

9.7 KB
Raw Blame
diff --git a/dlls/crypt32/object.c b/dlls/crypt32/object.c
index 8a4a161b..f7833b68 100644
--- a/dlls/crypt32/object.c
+++ b/dlls/crypt32/object.c
@@ -767,7 +767,7 @@
     return ret;
 }
 
-BOOL WINAPI CryptQueryObject(DWORD dwObjectType, const void *pvObject,
+static BOOL crypt_query_object(DWORD dwObjectType, const void *pvObject,
  DWORD dwExpectedContentTypeFlags, DWORD dwExpectedFormatTypeFlags,
  DWORD dwFlags, DWORD *pdwMsgAndCertEncodingType, DWORD *pdwContentType,
  DWORD *pdwFormatType, HCERTSTORE *phCertStore, HCRYPTMSG *phMsg,
@@ -861,6 +861,58 @@
         SetLastError(CRYPT_E_NO_MATCH);
     TRACE("returning %d\n", ret);
     return ret;
+}
+
+/* NotProton: Apple's MetalFX DLSS bridge (nvngx-on-metalfx, linked as system32 nvngx.dll)
+ * carries no NVIDIA signature, and the NGX loader inside NVIDIA Streamline refuses an
+ * unsigned NGX core, so Streamline games never offer DLSS. When the run script sets
+ * NOTPROTON_NGX_SIGNER (NVAPI=1 on D3DMetal), a signature check of that one file, by
+ * path, is answered from an NVIDIA-signed Streamline module that ships with the game. */
+static const WCHAR *notproton_ngx_signer( const WCHAR *path, WCHAR *buf, DWORD len )
+{
+    static const WCHAR * const signed_modules[] = { L"sl.interposer.dll", L"sl.common.dll", L"nvngx_dlss.dll" };
+    WCHAR sysdir[MAX_PATH];
+    const WCHAR *base;
+    WCHAR *slash;
+    DWORD i, sysdir_len;
+
+    if (!path || !GetEnvironmentVariableW( L"NOTPROTON_NGX_SIGNER", NULL, 0 )) return path;
+    base = wcsrchr( path, '\\' );
+    if (!base) return path;
+    base++;
+    if (wcsicmp( base, L"nvngx.dll" ) && wcsicmp( base, L"_nvngx.dll" )) return path;
+    /* Only the system directory's copy, not any folder that happens to be called system32. */
+    sysdir_len = GetSystemDirectoryW( sysdir, ARRAY_SIZE(sysdir) );
+    if (!sysdir_len || sysdir_len >= ARRAY_SIZE(sysdir)) return path;
+    if ((DWORD)(base - 1 - path) != sysdir_len || wcsnicmp( path, sysdir, sysdir_len )) return path;
+    if (!GetModuleFileNameW( NULL, buf, len ) || !(slash = wcsrchr( buf, '\\' ))) return path;
+    for (i = 0; i < ARRAY_SIZE(signed_modules); i++)
+    {
+        if (wcslen( signed_modules[i] ) + 1 > len - (DWORD)(slash + 1 - buf)) continue;
+        wcscpy( slash + 1, signed_modules[i] );
+        if (GetFileAttributesW( buf ) == INVALID_FILE_ATTRIBUTES) continue;
+        FIXME( "NotProton: checking the signature of %s against %s\n", debugstr_w(path), debugstr_w(buf) );
+        return buf;
+    }
+    return path;
+}
+
+/***********************************************************************
+ *		CryptQueryObject (CRYPT32.@)
+ */
+BOOL WINAPI CryptQueryObject(DWORD dwObjectType, const void *pvObject,
+ DWORD dwExpectedContentTypeFlags, DWORD dwExpectedFormatTypeFlags,
+ DWORD dwFlags, DWORD *pdwMsgAndCertEncodingType, DWORD *pdwContentType,
+ DWORD *pdwFormatType, HCERTSTORE *phCertStore, HCRYPTMSG *phMsg,
+ const void **ppvContext)
+{
+    WCHAR ngx_signer[MAX_PATH];
+
+    if (dwObjectType == CERT_QUERY_OBJECT_FILE)
+        pvObject = notproton_ngx_signer(pvObject, ngx_signer, ARRAY_SIZE(ngx_signer));
+    return crypt_query_object(dwObjectType, pvObject, dwExpectedContentTypeFlags,
+     dwExpectedFormatTypeFlags, dwFlags, pdwMsgAndCertEncodingType, pdwContentType,
+     pdwFormatType, phCertStore, phMsg, ppvContext);
 }
 
 static BOOL WINAPI CRYPT_FormatHexString(DWORD dwCertEncodingType,
diff --git a/dlls/wintrust/wintrust_main.c b/dlls/wintrust/wintrust_main.c
index a9b1587e..5cc951b6 100644
--- a/dlls/wintrust/wintrust_main.c
+++ b/dlls/wintrust/wintrust_main.c
@@ -654,6 +654,104 @@
  * NOTES
  *   Trust providers can be found at:
  *   HKLM\SOFTWARE\Microsoft\Cryptography\Providers\Trust\
+ */
+/* NotProton: Apple's MetalFX DLSS bridge (nvngx-on-metalfx, linked as system32 nvngx.dll)
+ * carries no NVIDIA signature, and the NGX loader inside NVIDIA Streamline refuses an
+ * unsigned NGX core, so Streamline games never offer DLSS. When the run script sets
+ * NOTPROTON_NGX_SIGNER (NVAPI=1 on D3DMetal), a signature check of that one file, by
+ * path, is answered from an NVIDIA-signed Streamline module that ships with the game. */
+static const WCHAR *notproton_ngx_signer( const WCHAR *path, WCHAR *buf, DWORD len )
+{
+    static const WCHAR * const signed_modules[] = { L"sl.interposer.dll", L"sl.common.dll", L"nvngx_dlss.dll" };
+    WCHAR sysdir[MAX_PATH];
+    const WCHAR *base;
+    WCHAR *slash;
+    DWORD i, sysdir_len;
+
+    if (!path || !GetEnvironmentVariableW( L"NOTPROTON_NGX_SIGNER", NULL, 0 )) return path;
+    base = wcsrchr( path, '\\' );
+    if (!base) return path;
+    base++;
+    if (wcsicmp( base, L"nvngx.dll" ) && wcsicmp( base, L"_nvngx.dll" )) return path;
+    /* Only the system directory's copy, not any folder that happens to be called system32. */
+    sysdir_len = GetSystemDirectoryW( sysdir, ARRAY_SIZE(sysdir) );
+    if (!sysdir_len || sysdir_len >= ARRAY_SIZE(sysdir)) return path;
+    if ((DWORD)(base - 1 - path) != sysdir_len || wcsnicmp( path, sysdir, sysdir_len )) return path;
+    if (!GetModuleFileNameW( NULL, buf, len ) || !(slash = wcsrchr( buf, '\\' ))) return path;
+    for (i = 0; i < ARRAY_SIZE(signed_modules); i++)
+    {
+        if (wcslen( signed_modules[i] ) + 1 > len - (DWORD)(slash + 1 - buf)) continue;
+        wcscpy( slash + 1, signed_modules[i] );
+        if (GetFileAttributesW( buf ) == INVALID_FILE_ATTRIBUTES) continue;
+        FIXME( "NotProton: checking the signature of %s against %s\n", debugstr_w(path), debugstr_w(buf) );
+        return buf;
+    }
+    return path;
+}
+
+/* A redirected check runs on a copy of the caller's WINTRUST_DATA and WINTRUST_FILE_INFO,
+ * so the caller's structs are never written (they may be read-only) except hWVTStateData,
+ * the state handle WinVerifyTrust hands back. The copy is on the heap because with
+ * WTD_STATEACTION_VERIFY the provider state keeps pointing at it until the
+ * WTD_STATEACTION_CLOSE call; live copies are tracked so that call can free them. */
+struct ngx_trust_copy
+{
+    WINTRUST_DATA data;
+    WINTRUST_FILE_INFO file;
+    WCHAR path[MAX_PATH];
+};
+
+static SRWLOCK ngx_copies_lock = SRWLOCK_INIT;
+static struct ngx_trust_copy *ngx_copies[16];
+
+static BOOL ngx_copy_track( struct ngx_trust_copy *copy )
+{
+    BOOL tracked = FALSE;
+    DWORD i;
+
+    AcquireSRWLockExclusive( &ngx_copies_lock );
+    for (i = 0; i < ARRAY_SIZE(ngx_copies) && !tracked; i++)
+        if (!ngx_copies[i]) ngx_copies[i] = copy, tracked = TRUE;
+    ReleaseSRWLockExclusive( &ngx_copies_lock );
+    return tracked;
+}
+
+/* Removes and returns the tracked copy whose WINTRUST_DATA is data, or NULL. */
+static struct ngx_trust_copy *ngx_copy_untrack( const WINTRUST_DATA *data )
+{
+    struct ngx_trust_copy *found = NULL;
+    DWORD i;
+
+    if (!data) return NULL;
+    AcquireSRWLockExclusive( &ngx_copies_lock );
+    for (i = 0; i < ARRAY_SIZE(ngx_copies) && !found; i++)
+        if (ngx_copies[i] && &ngx_copies[i]->data == data) found = ngx_copies[i], ngx_copies[i] = NULL;
+    ReleaseSRWLockExclusive( &ngx_copies_lock );
+    return found;
+}
+
+/* The copy to verify instead of data, or NULL when data's file is not redirected. */
+static struct ngx_trust_copy *ngx_trust_copy( const WINTRUST_DATA *data )
+{
+    struct ngx_trust_copy *copy;
+    WCHAR signer[MAX_PATH];
+    const WCHAR *path;
+
+    if (!data || data->dwUnionChoice != WTD_CHOICE_FILE || !data->pFile || data->pFile->hFile)
+        return NULL;
+    path = notproton_ngx_signer( data->pFile->pcwszFilePath, signer, ARRAY_SIZE(signer) );
+    if (path == data->pFile->pcwszFilePath) return NULL;
+    if (!(copy = calloc( 1, sizeof(*copy) ))) return NULL;
+    memcpy( &copy->data, data, min( data->cbStruct, sizeof(copy->data) ) );
+    memcpy( &copy->file, data->pFile, min( data->pFile->cbStruct, sizeof(copy->file) ) );
+    lstrcpynW( copy->path, path, ARRAY_SIZE(copy->path) );
+    copy->file.pcwszFilePath = copy->path;
+    copy->data.pFile = &copy->file;
+    return copy;
+}
+
+/***********************************************************************
+ *		WinVerifyTrust (WINTRUST.@) with the NotProton NGX signer redirect
  */
 LONG WINAPI WinVerifyTrust( HWND hwnd, GUID *ActionID, LPVOID ActionData )
 {
@@ -667,8 +765,22 @@
     LONG err = ERROR_SUCCESS;
     WINTRUST_DATA *actionData = ActionData;
 
+    WINTRUST_DATA *callerData = ActionData;
+    struct ngx_trust_copy *ngx_copy, *ngx_closed = NULL;
+    CRYPT_PROVIDER_DATA *ngx_state;
+
     TRACE("(%p, %s, %p)\n", hwnd, debugstr_guid(ActionID), ActionData);
     dump_wintrust_data(ActionData);
+
+    if ((ngx_copy = ngx_trust_copy( callerData )))
+    {
+        ActionData = actionData = &ngx_copy->data;
+        /* A CLOSE frees the copy its VERIFY left in the provider state. */
+        if (WVT_ISINSTRUCT(WINTRUST_DATA, actionData->cbStruct, dwStateAction) &&
+            actionData->dwStateAction == WTD_STATEACTION_CLOSE &&
+            (ngx_state = actionData->hWVTStateData))
+            ngx_closed = ngx_copy_untrack( ngx_state->pWintrustData );
+    }
 
     /* Support for known old-style callers: */
     if (IsEqualGUID(ActionID, &published_software))
@@ -709,6 +821,20 @@
         }
     }
 
+    if (ngx_copy)
+    {
+        if (callerData->hWVTStateData != ngx_copy->data.hWVTStateData)
+            callerData->hWVTStateData = ngx_copy->data.hWVTStateData;
+        /* The provider state of a VERIFY points at the copy until its CLOSE. */
+        if (!ngx_copy->data.hWVTStateData || !ngx_copy_track( ngx_copy ))
+        {
+            if (ngx_copy->data.hWVTStateData)
+                FIXME( "NotProton: too many open NGX signer checks, the copy is leaked\n" );
+            else
+                free( ngx_copy );
+        }
+    }
+    free( ngx_closed );
     if (err) SetLastError(err);
     TRACE("returning %08lx\n", err);
     return err;