Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*.class
*.jar
*.dll
!src/test/resources/**/*.dll
*.obj
*.lib
*.exp
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Changes to prior versions can be found on the [GitHub release page](https://gith

## Unreleased

No changes yet.
### Added
* Update mechanism `InstallerUpdateMechanism`, which downloads, verifies and runs the EXE or MSI installer matching the current installation. Self-updates are only performed if the registry value `HKLM\SOFTWARE\Skymatic GmbH\Cryptomator\InstallType` is `EXE` or `MSI`, and the expected signer of the installer is configured via the JVM properties `cryptomator.integrationsWin.updateDurableIdentityEku` and `cryptomator.integrationsWin.updateRootCaThumbprint`.


## [1.6.2](https://github.com/cryptomator/integrations-win/releases/1.6.2) - 2026-09-29
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ This project uses the following JVM properties:
* `cryptomator.integrationsWin.windowsHelloKeyId` - Identifier for the Windows Hello keypair
* `cryptomator.integrationsWin.windowsHelloKeychainPaths` - Locations of the file-based windowsHello keychain
* `cryptomator.integrationsWin.keychainPaths` - List of file paths, which are checked for data encrypted with the Windows data protection api
* `cryptomator.integrationsWin.updateDurableIdentityEku` - OID of an extended key usage, which the code signing certificate of a downloaded installer must contain. With Azure Artifact Signing, this is the durable identity of the signing account, which stays the same across certificate renewals (e.g. `1.3.6.1.4.1.311.97.986645502.209118791.449271898.355477593` for Skymatic GmbH). Required by `InstallerUpdateMechanism`.
* `cryptomator.integrationsWin.updateRootCaThumbprint` - SHA-1 thumbprint (40 hex characters, without spaces) of the root certificate, at which the certificate chain of a downloaded installer's signature must end (e.g. `F40042E2E5F7E8EF8189FED15519AECE42C3BFA2` for Microsoft Identity Verification Root Certificate Authority 2020). Required by `InstallerUpdateMechanism`.

`InstallerUpdateMechanism` only updates if both of the `update…` properties are set.

## Building

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import org.cryptomator.integrations.quickaccess.QuickAccessService;
import org.cryptomator.integrations.revealpath.RevealPathService;
import org.cryptomator.integrations.uiappearance.UiAppearanceProvider;
import org.cryptomator.integrations.update.UpdateMechanism;
import org.cryptomator.windows.autostart.WindowsAutoStart;
import org.cryptomator.windows.keychain.WindowsHelloKeychainAccess;
import org.cryptomator.windows.keychain.WindowsProtectedKeychainAccess;
import org.cryptomator.windows.quickaccess.ExplorerQuickAccessService;
import org.cryptomator.windows.revealpath.ExplorerRevealPathService;
import org.cryptomator.windows.uiappearance.WinUiAppearanceProvider;
import org.cryptomator.windows.update.InstallerUpdateMechanism;

module org.cryptomator.integrations.win {
requires org.cryptomator.integrations.api;
Expand All @@ -24,5 +26,6 @@
provides UiAppearanceProvider with WinUiAppearanceProvider;
provides RevealPathService with ExplorerRevealPathService;
provides QuickAccessService with ExplorerQuickAccessService;
provides UpdateMechanism with InstallerUpdateMechanism;

}
26 changes: 21 additions & 5 deletions src/main/java/org/cryptomator/windows/common/RegistryKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,23 @@ public class RegistryKey implements AutoCloseable {
* @throws RegistryValueException if winreg.h:RegGetValueW returns a result != ERROR_SUCCESS
*/
public String getStringValue(String name, boolean isExpandable) throws RegistryValueException {
return getStringValue(null, name, isExpandable);
}

/**
* Gets a REG_SZ or REG_EXPAND_SZ value of a subkey of this registry key, only requiring read access to the subkey.
* <p>
* The size of the data is restricted to at most {@value MAX_DATA_SIZE}. If the the value exceeds the size, a runtime exception is thrown.
*
* @param subkey name/path of the subkey containing the value or {@code null} to read the value of this key
* @param name name of the value
* @param isExpandable flag indicating if the value is of type REG_EXPAND_SZ
* @return the data of the value
* @throws RegistryValueException if winreg.h:RegGetValueW returns a result != ERROR_SUCCESS
*/
public String getStringValue(String subkey, String name, boolean isExpandable) throws RegistryValueException {
try (var arena = Arena.ofConfined()) {
var data = getValue(arena, name, isExpandable ? RRF_RT_REG_EXPAND_SZ() : RRF_RT_REG_SZ());
var data = getValue(arena, subkey, name, isExpandable ? RRF_RT_REG_EXPAND_SZ() : RRF_RT_REG_SZ());
return data.getString(0, StandardCharsets.UTF_16LE);
}
}
Expand All @@ -62,12 +77,13 @@ public String getStringValue(String name, boolean isExpandable) throws RegistryV
*/
public int getDwordValue(String name) throws RegistryValueException {
try (var arena = Arena.ofConfined()) {
var data = getValue(arena, name, RRF_RT_REG_DWORD());
var data = getValue(arena, null, name, RRF_RT_REG_DWORD());
return data.get(ValueLayout.JAVA_INT, 0);
}
}

private MemorySegment getValue(Arena arena, String name, int dwFlags) throws RegistryValueException {
private MemorySegment getValue(Arena arena, String subkey, String name, int dwFlags) throws RegistryValueException {
var lpSubKey = subkey == null ? NULL : arena.allocateFrom(subkey, StandardCharsets.UTF_16LE);
var lpValueName = arena.allocateFrom(name, StandardCharsets.UTF_16LE);
var lpDataSize = arena.allocateFrom(ValueLayout.JAVA_INT, 0);

Expand All @@ -82,14 +98,14 @@ private MemorySegment getValue(Arena arena, String name, int dwFlags) throws Reg
lpData = arena.allocate(bufferSize);
lpDataSize.set(ValueLayout.JAVA_INT, 0, bufferSize);

result = Winreg_h.RegGetValueW(handle, NULL, lpValueName, dwFlags, NULL, lpData, lpDataSize);
result = Winreg_h.RegGetValueW(handle, lpSubKey, lpValueName, dwFlags, NULL, lpData, lpDataSize);

} while (result == ERROR_MORE_DATA());

if (result == ERROR_SUCCESS()) {
return lpData;
} else {
throw new RegistryValueException("winreg_h:RegGetValue", path, name, result);
throw new RegistryValueException("winreg_h:RegGetValue", subkey == null ? path : path + "\\" + subkey, name, result);
}
}

Expand Down
238 changes: 238 additions & 0 deletions src/main/java/org/cryptomator/windows/update/Authenticode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
package org.cryptomator.windows.update;

import java.io.ByteArrayInputStream;
import java.lang.foreign.Arena;
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.Linker;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.StructLayout;
import java.lang.foreign.SymbolLookup;
import java.lang.invoke.MethodHandle;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HexFormat;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static java.lang.foreign.MemoryLayout.PathElement.groupElement;
import static java.lang.foreign.MemorySegment.NULL;
import static java.lang.foreign.ValueLayout.ADDRESS;
import static java.lang.foreign.ValueLayout.JAVA_BYTE;
import static java.lang.foreign.ValueLayout.JAVA_INT;
import static java.lang.foreign.ValueLayout.JAVA_SHORT;

/**
* Verifies Authenticode signatures of files using <a href="https://learn.microsoft.com/en-us/windows/win32/api/wintrust/nf-wintrust-winverifytrust">WinVerifyTrust</a>.
*/
final class Authenticode {

// Hand-written bindings, see WinTrust.h, SoftPub.h and wincrypt.h. The DLL is loaded by absolute path to prevent DLL search order hijacking.
private static final Path SYSTEM32 = Path.of(Optional.ofNullable(System.getenv("SystemRoot")).orElse("C:\\Windows"), "System32");
private static final Linker LINKER = Linker.nativeLinker();
private static final SymbolLookup WINTRUST = SymbolLookup.libraryLookup(SYSTEM32.resolve("wintrust.dll"), Arena.global());

private static final MethodHandle WIN_VERIFY_TRUST = downcall(WINTRUST, "WinVerifyTrust", FunctionDescriptor.of(JAVA_INT, ADDRESS, ADDRESS, ADDRESS));
private static final MethodHandle WT_HELPER_PROV_DATA_FROM_STATE_DATA = downcall(WINTRUST, "WTHelperProvDataFromStateData", FunctionDescriptor.of(ADDRESS, ADDRESS));
private static final MethodHandle WT_HELPER_GET_PROV_SIGNER_FROM_CHAIN = downcall(WINTRUST, "WTHelperGetProvSignerFromChain", FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_INT, JAVA_INT, JAVA_INT));
private static final MethodHandle WT_HELPER_GET_PROV_CERT_FROM_CHAIN = downcall(WINTRUST, "WTHelperGetProvCertFromChain", FunctionDescriptor.of(ADDRESS, ADDRESS, JAVA_INT));

private static final int WTD_UI_NONE = 2;
private static final int WTD_REVOKE_WHOLECHAIN = 0x1;
private static final int WTD_CHOICE_FILE = 1;
private static final int WTD_STATEACTION_VERIFY = 0x1;
private static final int WTD_STATEACTION_CLOSE = 0x2;
private static final int WTD_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT = 0x80;

private static final StructLayout GUID = MemoryLayout.structLayout(
JAVA_INT.withName("Data1"),
JAVA_SHORT.withName("Data2"),
JAVA_SHORT.withName("Data3"),
MemoryLayout.sequenceLayout(8, JAVA_BYTE).withName("Data4"));

private static final StructLayout WINTRUST_FILE_INFO = MemoryLayout.structLayout(
JAVA_INT.withName("cbStruct"),
MemoryLayout.paddingLayout(4),
ADDRESS.withName("pcwszFilePath"),
ADDRESS.withName("hFile"),
ADDRESS.withName("pgKnownSubject"));

private static final StructLayout WINTRUST_DATA = MemoryLayout.structLayout(
JAVA_INT.withName("cbStruct"),
MemoryLayout.paddingLayout(4),
ADDRESS.withName("pPolicyCallbackData"),
ADDRESS.withName("pSIPClientData"),
JAVA_INT.withName("dwUIChoice"),
JAVA_INT.withName("fdwRevocationChecks"),
JAVA_INT.withName("dwUnionChoice"),
MemoryLayout.paddingLayout(4),
ADDRESS.withName("pFile"),
JAVA_INT.withName("dwStateAction"),
MemoryLayout.paddingLayout(4),
ADDRESS.withName("hWVTStateData"),
ADDRESS.withName("pwszURLReference"),
JAVA_INT.withName("dwProvFlags"),
JAVA_INT.withName("dwUIContext"),
ADDRESS.withName("pSignatureSettings"));

// only the leading fields are declared
private static final StructLayout CRYPT_PROVIDER_SGNR = MemoryLayout.structLayout(
JAVA_INT.withName("cbStruct"),
JAVA_INT.withName("sftVerifyAsOfLow"), // FILETIME, 4-byte aligned
JAVA_INT.withName("sftVerifyAsOfHigh"),
JAVA_INT.withName("csCertChain"),
ADDRESS.withName("pasCertChain"));

// only the leading fields are declared
private static final StructLayout CRYPT_PROVIDER_CERT = MemoryLayout.structLayout(
JAVA_INT.withName("cbStruct"),
MemoryLayout.paddingLayout(4),
ADDRESS.withName("pCert"));

private static final StructLayout CERT_CONTEXT = MemoryLayout.structLayout(
JAVA_INT.withName("dwCertEncodingType"),
MemoryLayout.paddingLayout(4),
ADDRESS.withName("pbCertEncoded"),
JAVA_INT.withName("cbCertEncoded"),
MemoryLayout.paddingLayout(4),
ADDRESS.withName("pCertInfo"),
ADDRESS.withName("hCertStore"));

private Authenticode() {
}

/**
* Verifies the embedded Authenticode signature of a file and checks that it was created by the expected signer.
*
* @param file the signed file, e.g. an .exe or .msi
* @param signerEku OID of an extended key usage, which the signing certificate must contain, e.g. the durable identity of an Azure Artifact Signing account
* @param rootThumbprint SHA-1 thumbprint (hex) of the root certificate, at which the certificate chain must end
* @return the signing certificate
* @throws SignatureException if the file is not signed, the signature is not trusted or it was created by a different signer
*/
static X509Certificate verify(Path file, String signerEku, String rootThumbprint) throws SignatureException {
var chain = getVerifiedCertificateChain(file);
var signerCert = chain.getFirst();
var rootCert = chain.getLast();
try {
var ekus = Objects.requireNonNullElse(signerCert.getExtendedKeyUsage(), List.<String>of());
if (!ekus.contains(signerEku)) {
throw new SignatureException("Signing certificate %s of %s lacks extended key usage %s".formatted(signerCert.getSubjectX500Principal(), file, signerEku));
}
var actualRootThumbprint = HexFormat.of().formatHex(MessageDigest.getInstance("SHA-1").digest(rootCert.getEncoded()));
if (!actualRootThumbprint.equalsIgnoreCase(rootThumbprint)) {
throw new SignatureException("Certificate chain of %s ends at unexpected root %s (thumbprint %s)".formatted(file, rootCert.getSubjectX500Principal(), actualRootThumbprint));
}
} catch (CertificateException e) {
throw new SignatureException("Failed to read certificate of " + file, e);
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("Every Java platform is required to support SHA-1", e);
}
return signerCert;
}

/**
* Verifies the embedded Authenticode signature of a file, including the certificate chain and its revocation status.
*
* @param file the signed file, e.g. an .exe or .msi
* @return the certificate chain of the primary signature, starting with the signing certificate and ending with the trusted root certificate
* @throws SignatureException if the file is not signed or the signature is not trusted
*/
static List<X509Certificate> getVerifiedCertificateChain(Path file) throws SignatureException {
try (var arena = Arena.ofConfined()) {
var fileInfo = arena.allocate(WINTRUST_FILE_INFO);
setInt(fileInfo, WINTRUST_FILE_INFO, "cbStruct", (int) WINTRUST_FILE_INFO.byteSize());
setAddress(fileInfo, WINTRUST_FILE_INFO, "pcwszFilePath", arena.allocateFrom(file.toString(), StandardCharsets.UTF_16LE));

var data = arena.allocate(WINTRUST_DATA);
setInt(data, WINTRUST_DATA, "cbStruct", (int) WINTRUST_DATA.byteSize());
setInt(data, WINTRUST_DATA, "dwUIChoice", WTD_UI_NONE);
setInt(data, WINTRUST_DATA, "fdwRevocationChecks", WTD_REVOKE_WHOLECHAIN);
setInt(data, WINTRUST_DATA, "dwUnionChoice", WTD_CHOICE_FILE);
setAddress(data, WINTRUST_DATA, "pFile", fileInfo);
setInt(data, WINTRUST_DATA, "dwStateAction", WTD_STATEACTION_VERIFY);
setInt(data, WINTRUST_DATA, "dwProvFlags", WTD_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT);

var actionId = allocateGenericVerifyV2(arena);
int result = winVerifyTrust(actionId, data);
try {
if (result != 0) {
throw new SignatureException("WinVerifyTrust failed with error 0x%08X for %s".formatted(result, file));
}
var provData = call(WT_HELPER_PROV_DATA_FROM_STATE_DATA, data.get(ADDRESS, offset(WINTRUST_DATA, "hWVTStateData")));
var signer = provData.equals(NULL) ? NULL : call(WT_HELPER_GET_PROV_SIGNER_FROM_CHAIN, provData, 0, 0, 0);
int chainLength = signer.equals(NULL) ? 0 : signer.reinterpret(CRYPT_PROVIDER_SGNR.byteSize()).get(JAVA_INT, offset(CRYPT_PROVIDER_SGNR, "csCertChain"));
if (chainLength == 0) {
throw new SignatureException("No signer certificate found for " + file);
}
var certFactory = CertificateFactory.getInstance("X.509");
var chain = new ArrayList<X509Certificate>(chainLength);
for (int i = 0; i < chainLength; i++) {
var cert = call(WT_HELPER_GET_PROV_CERT_FROM_CHAIN, signer, i).reinterpret(CRYPT_PROVIDER_CERT.byteSize());
var certContext = cert.get(ADDRESS, offset(CRYPT_PROVIDER_CERT, "pCert")).reinterpret(CERT_CONTEXT.byteSize());
var encoded = certContext.get(ADDRESS, offset(CERT_CONTEXT, "pbCertEncoded")).reinterpret(certContext.get(JAVA_INT, offset(CERT_CONTEXT, "cbCertEncoded")));
chain.add((X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(encoded.toArray(JAVA_BYTE))));
}
return chain;
} catch (CertificateException e) {
throw new SignatureException("Failed to read certificate chain of " + file, e);
} finally {
// release state data allocated by WTD_STATEACTION_VERIFY
setInt(data, WINTRUST_DATA, "dwStateAction", WTD_STATEACTION_CLOSE);
winVerifyTrust(actionId, data);
}
}
}

// {00AAC56B-CD44-11d0-8CC2-00C04FC295EE}
private static MemorySegment allocateGenericVerifyV2(Arena arena) {
var guid = arena.allocate(GUID);
guid.set(JAVA_INT, offset(GUID, "Data1"), 0x00AAC56B);
guid.set(JAVA_SHORT, offset(GUID, "Data2"), (short) 0xCD44);
guid.set(JAVA_SHORT, offset(GUID, "Data3"), (short) 0x11D0);
guid.asSlice(offset(GUID, "Data4")).copyFrom(MemorySegment.ofArray(new byte[]{(byte) 0x8C, (byte) 0xC2, 0x00, (byte) 0xC0, 0x4F, (byte) 0xC2, (byte) 0x95, (byte) 0xEE}));
return guid;
}

private static int winVerifyTrust(MemorySegment actionId, MemorySegment data) {
try {
return (int) WIN_VERIFY_TRUST.invokeExact(NULL, actionId, data);
} catch (Throwable e) {
throw new AssertionError("should not reach here", e);
}
}

private static MemorySegment call(MethodHandle handle, Object... args) {
try {
return (MemorySegment) handle.invokeWithArguments(args);
} catch (Throwable e) {
throw new AssertionError("should not reach here", e);
}
}

private static MethodHandle downcall(SymbolLookup lookup, String name, FunctionDescriptor descriptor) {
var symbol = lookup.find(name).orElseThrow(() -> new UnsatisfiedLinkError("unresolved symbol: " + name));
return LINKER.downcallHandle(symbol, descriptor);
}

private static long offset(StructLayout layout, String field) {
return layout.byteOffset(groupElement(field));
}

private static void setInt(MemorySegment segment, StructLayout layout, String field, int value) {
segment.set(JAVA_INT, offset(layout, field), value);
}

private static void setAddress(MemorySegment segment, StructLayout layout, String field, MemorySegment value) {
segment.set(ADDRESS, offset(layout, field), value);
}

}
Loading
Loading