polish P1: carry the cipher scheme through the channels, diagnose a mismatch

ChannelConfig and BulkConfig carry MacCipherScheme; a change to it reopens
every connection the way a key change does. PeerConnection reads the
scheme's preamble length (16 legacy, 48 modern) instead of a hard-coded 48.

The diagnostic: the peer's opening bytes are kept until one packet has
framed. If the first packet fails framing under the configured scheme and
the same raw bytes frame under the other one, the connection closes with
.invalidKey(.cipherSchemeMismatch) and reports status
.cipherSchemeMismatch, so the menu names the setting to change rather than
blaming the security key. Costs one AES setup on a connection that was
going to fail anyway, and nothing at all on success.

The bulk channel needs no diagnostic: a transfer only starts with a peer
whose control connection already framed.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
lucioandClaude Opus 5 committed 2026-09-16 21:08:19 -07:00
1 parent c0031c00da
commit 1445d546a1
6 files changed
+108 -20

No files matched your search

+9 -2
View File
@@ -26,6 +26,10 @@ public struct ChannelConfig: Sendable, Equatable {
public var port: UInt16
/// The shared security key. Everything on the wire is derived from it.
public var securityKey: String
/// `MacCipherScheme`: which of the two PowerToys stream layouts the peers
/// speak (`docs/protocol.md` §2.5). Not negotiated — both vintages of
/// Windows write their preamble first and never adapt.
public var cipherScheme: CipherScheme
/// The four matrix slot names; every non-local, non-empty one gets an
/// outbound client (`SocketStuff.cs:797-837`).
public var matrixNames: [String]
@@ -52,6 +56,7 @@ public struct ChannelConfig: Sendable, Equatable {
localID: UInt32,
port: UInt16 = 15100,
securityKey: String,
cipherScheme: CipherScheme = .legacy,
matrixNames: [String] = [],
name2IP: String = "",
sameSubnetOnly: Bool = false,
@@ -62,6 +67,7 @@ public struct ChannelConfig: Sendable, Equatable {
self.localID = localID
self.port = port
self.securityKey = securityKey
self.cipherScheme = cipherScheme
self.matrixNames = matrixNames
self.name2IP = name2IP
self.sameSubnetOnly = sameSubnetOnly
@@ -74,10 +80,11 @@ public struct ChannelConfig: Sendable, Equatable {
/// The key changes the cipher stream and the magic number, and the port
/// changes both listener and clients, so either one means a full reopen —
/// Windows writes the same rule as "a key change reopens all sockets"
/// (`docs/protocol.md` §2.4, `Setting.cs:479-511`).
/// (`docs/protocol.md` §2.4, `Setting.cs:479-511`). The cipher scheme is
/// the same kind of change: it rewrites every stream from its first byte.
func needsFullReopen(comparedTo other: ChannelConfig) -> Bool {
securityKey != other.securityKey || port != other.port || localName != other.localName
|| localID != other.localID
|| localID != other.localID || cipherScheme != other.cipherScheme
}
}