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:
1 parent
c0031c00da
commit
1445d546a1
6 files changed
+108
-20
No files matched your search
@@ -26,6 +26,10 @@ public struct BulkConfig: Sendable, Equatable {
|
||||
public var port: UInt16
|
||||
/// The shared secret both cipher streams are derived from.
|
||||
public var securityKey: String
|
||||
/// `MacCipherScheme`, the same value the control channel runs on: a peer
|
||||
/// that speaks one scheme on 15101 speaks it on 15100 too
|
||||
/// (`docs/protocol.md` §2.5).
|
||||
public var cipherScheme: CipherScheme
|
||||
/// Where a received file is streamed while it is incomplete. The commit is
|
||||
/// a rename inside this same directory, so it must be the download folder.
|
||||
public var stagingDirectory: URL
|
||||
@@ -36,12 +40,14 @@ public struct BulkConfig: Sendable, Equatable {
|
||||
/// A configuration with the Windows limits.
|
||||
public init(
|
||||
localName: String, localID: UInt32, port: UInt16, securityKey: String,
|
||||
stagingDirectory: URL, maxFileSize: Int64 = ClipboardCodec.maxFileSize
|
||||
cipherScheme: CipherScheme = .legacy, stagingDirectory: URL,
|
||||
maxFileSize: Int64 = ClipboardCodec.maxFileSize
|
||||
) {
|
||||
self.localName = localName
|
||||
self.localID = localID
|
||||
self.port = port
|
||||
self.securityKey = securityKey
|
||||
self.cipherScheme = cipherScheme
|
||||
self.stagingDirectory = stagingDirectory
|
||||
self.maxFileSize = maxFileSize
|
||||
}
|
||||
|
||||
@@ -88,8 +88,8 @@ final class BulkConnection: @unchecked Sendable {
|
||||
self.onEvent = onEvent
|
||||
self.peerName = peerName ?? ""
|
||||
self.postAction = postAction
|
||||
writer = try CipherWriter(securityKey: config.securityKey)
|
||||
reader = CipherReader(securityKey: config.securityKey)
|
||||
writer = try CipherWriter(securityKey: config.securityKey, scheme: config.cipherScheme)
|
||||
reader = CipherReader(securityKey: config.securityKey, scheme: config.cipherScheme)
|
||||
}
|
||||
|
||||
// MARK: - Lifecycle
|
||||
@@ -120,7 +120,11 @@ final class BulkConnection: @unchecked Sendable {
|
||||
started = true
|
||||
armDeadline()
|
||||
// `ShakeHand` (`Clipboard.cs:983-987`): the cipher preamble, then one
|
||||
// unframed 64-byte package, before anything is read.
|
||||
// unframed 64-byte package, before anything is read. There is no
|
||||
// scheme-mismatch diagnostic here, and none is needed: a bulk transfer
|
||||
// only starts with a peer whose control connection already framed under
|
||||
// the configured scheme (`BulkSink.authorise`), so the mismatch has
|
||||
// already been reported by then (`docs/protocol.md` §2.5).
|
||||
send(writer.header, encrypted: false)
|
||||
send(handshakePacket().bytes, encrypted: true)
|
||||
receive()
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,9 +35,10 @@ final class PeerConnection: @unchecked Sendable {
|
||||
/// exactly 32 bytes of plaintext and a short read means here what it means
|
||||
/// there. `Session` does the 32-then-32 reassembly of a big packet.
|
||||
static let readChunk = packageSize
|
||||
/// Salt, IV and the sacrificial block the peer writes first
|
||||
/// (`docs/protocol.md` §2.3).
|
||||
static let preambleSize = 48
|
||||
/// Raw bytes the scheme-mismatch diagnostic needs before it can replay the
|
||||
/// peer's opening under the other scheme: the longest preamble either
|
||||
/// scheme has (`.modern`'s 48) plus one packet (`docs/protocol.md` §2.5).
|
||||
static let diagnosticBytes = CipherScheme.modern.preambleSize + packageSize
|
||||
/// Framed packets the outbox holds before the connection is closed
|
||||
/// (contracts C3; Windows instead sizes the socket buffer at 320 000
|
||||
/// bytes, `SocketStuff.cs:1283-1284`).
|
||||
@@ -63,6 +64,13 @@ final class PeerConnection: @unchecked Sendable {
|
||||
private let queue: DispatchQueue
|
||||
private let onEvent: (PeerConnection, Event) -> Void
|
||||
|
||||
/// Kept for the scheme-mismatch diagnostic only, which needs to redo the
|
||||
/// peer's opening bytes under the other scheme and re-check their framing.
|
||||
private let securityKey: String
|
||||
private let cipherScheme: CipherScheme
|
||||
private let magic: UInt32
|
||||
private var probe: SchemeProbe? = SchemeProbe()
|
||||
|
||||
private var outbox: [Data] = []
|
||||
private var sending = false
|
||||
private var sendDeadline: DispatchWorkItem?
|
||||
@@ -93,13 +101,16 @@ final class PeerConnection: @unchecked Sendable {
|
||||
self.queue = queue
|
||||
self.onEvent = onEvent
|
||||
self.peerName = peerNameHint
|
||||
writer = try CipherWriter(securityKey: config.securityKey)
|
||||
reader = CipherReader(securityKey: config.securityKey)
|
||||
securityKey = config.securityKey
|
||||
cipherScheme = config.cipherScheme
|
||||
magic = MagicHash.magicNumber(for: config.securityKey)
|
||||
writer = try CipherWriter(securityKey: config.securityKey, scheme: config.cipherScheme)
|
||||
reader = CipherReader(securityKey: config.securityKey, scheme: config.cipherScheme)
|
||||
session = Session(
|
||||
config: SessionConfig(
|
||||
localName: config.localName,
|
||||
localID: config.localID,
|
||||
magic: MagicHash.magicNumber(for: config.securityKey),
|
||||
magic: magic,
|
||||
blockScreenSaver: config.blockScreenSaver,
|
||||
peerNameHint: peerNameHint),
|
||||
sequencer: sequencer,
|
||||
@@ -155,7 +166,9 @@ final class PeerConnection: @unchecked Sendable {
|
||||
started = true
|
||||
let now = monotonicNow()
|
||||
lastReceive = now
|
||||
enqueue(writer.header) // 48 bytes in the clear, before anything else
|
||||
// The scheme's preamble, before anything else: 48 bytes under
|
||||
// `.modern` (salt, IV, sacrificial block), 16 under `.legacy`.
|
||||
enqueue(writer.header)
|
||||
session.start(now: now)
|
||||
pump()
|
||||
readPreamble()
|
||||
@@ -163,12 +176,13 @@ final class PeerConnection: @unchecked Sendable {
|
||||
|
||||
// MARK: - Reading
|
||||
|
||||
/// The peer's 48-byte preamble, read in one piece so every read after it
|
||||
/// lands on a packet boundary (`docs/protocol.md` §2.3). It yields no
|
||||
/// plaintext: `CipherReader` eats the salt and IV and discards the
|
||||
/// The peer's preamble, read in one piece so every read after it lands on a
|
||||
/// packet boundary (`docs/protocol.md` §2.3, §2.5): 48 bytes under
|
||||
/// `.modern`, 16 under `.legacy`. It yields no plaintext — `CipherReader`
|
||||
/// eats the salt and IV, if the scheme has them, and discards the
|
||||
/// sacrificial block.
|
||||
private func readPreamble() {
|
||||
receiveExactly(PeerConnection.preambleSize) { [weak self] bytes in
|
||||
receiveExactly(cipherScheme.preambleSize) { [weak self] bytes in
|
||||
guard let self else { return }
|
||||
self.consume(bytes)
|
||||
self.readPacket()
|
||||
@@ -217,13 +231,68 @@ final class PeerConnection: @unchecked Sendable {
|
||||
private func consume(_ ciphertext: Data) {
|
||||
guard !ciphertext.isEmpty else { return }
|
||||
do {
|
||||
session.feed(try reader.read(ciphertext))
|
||||
let plaintext = try reader.read(ciphertext)
|
||||
if noteForDiagnostic(ciphertext: ciphertext, plaintext: plaintext) {
|
||||
closing = .invalidKey(.cipherSchemeMismatch)
|
||||
report(.cipherSchemeMismatch)
|
||||
close(.invalidKey(.cipherSchemeMismatch))
|
||||
return
|
||||
}
|
||||
session.feed(plaintext)
|
||||
} catch {
|
||||
session.noteReceiveError(bytesReceived: 0)
|
||||
}
|
||||
pump()
|
||||
}
|
||||
|
||||
// MARK: - The scheme-mismatch diagnostic (`docs/protocol.md` §2.5)
|
||||
|
||||
/// The peer's opening bytes, kept only until one packet has been framed.
|
||||
///
|
||||
/// There is no negotiation in this protocol and no MAC, so a peer running
|
||||
/// the other vintage of PowerToys looks exactly like a peer with the wrong
|
||||
/// key: garbage plaintext, failed framing. Replaying the same bytes under
|
||||
/// the other scheme is the only way to tell the two apart, and it is worth
|
||||
/// one AES setup on a connection that was going to fail anyway.
|
||||
private struct SchemeProbe {
|
||||
var raw = Data()
|
||||
var plaintext = Data()
|
||||
}
|
||||
|
||||
/// Feeds the probe and answers whether the peer is speaking the other
|
||||
/// scheme. Nothing here acts on the bytes: a `true` only closes the
|
||||
/// connection with a reason the menu can explain (CLAUDE.md invariant 2).
|
||||
private func noteForDiagnostic(ciphertext: Data, plaintext: Data) -> Bool {
|
||||
guard probe != nil else { return false }
|
||||
probe?.raw.append(ciphertext)
|
||||
probe?.plaintext.append(plaintext)
|
||||
guard let probe, probe.plaintext.count >= packageSize else { return false }
|
||||
|
||||
// One packet that frames is all it takes to prove the scheme is right.
|
||||
guard framingFails(probe.plaintext) else {
|
||||
self.probe = nil
|
||||
return false
|
||||
}
|
||||
// Wait for enough raw bytes to cover the other scheme's preamble too;
|
||||
// meanwhile the garbage keeps spending the session's untrusted budget,
|
||||
// exactly as it would without this diagnostic.
|
||||
guard probe.raw.count >= PeerConnection.diagnosticBytes else { return false }
|
||||
self.probe = nil
|
||||
return framesUnderOtherScheme(probe.raw)
|
||||
}
|
||||
|
||||
private func framingFails(_ plaintext: Data) -> Bool {
|
||||
Framing.open(Data(plaintext.prefix(packageSize)), magic: magic).type == .invalid
|
||||
}
|
||||
|
||||
private func framesUnderOtherScheme(_ raw: Data) -> Bool {
|
||||
let other = CipherReader(securityKey: securityKey, scheme: cipherScheme.other)
|
||||
guard let plaintext = try? other.read(raw), plaintext.count >= packageSize else {
|
||||
return false
|
||||
}
|
||||
return !framingFails(plaintext)
|
||||
}
|
||||
|
||||
// MARK: - Writing
|
||||
|
||||
/// Queues an application packet the app has already sequenced (contracts
|
||||
|
||||
@@ -405,7 +405,8 @@ extension Engine {
|
||||
func bulkConfig() -> BulkConfig {
|
||||
BulkConfig(
|
||||
localName: switcher.localName, localID: switcher.localID, port: settings.tcpPort,
|
||||
securityKey: settings.securityKey, stagingDirectory: ReceivedFile.destinationURL())
|
||||
securityKey: settings.securityKey, cipherScheme: settings.macCipherScheme,
|
||||
stagingDirectory: ReceivedFile.destinationURL())
|
||||
}
|
||||
|
||||
/// Re-applies the settings; called from `applyConfiguration()`.
|
||||
|
||||
@@ -259,7 +259,8 @@ extension Engine {
|
||||
}
|
||||
return ChannelConfig(
|
||||
localName: switcher.localName, localID: switcher.localID, port: settings.tcpPort,
|
||||
securityKey: settings.securityKey, matrixNames: peers, name2IP: settings.name2IP,
|
||||
securityKey: settings.securityKey, cipherScheme: settings.macCipherScheme,
|
||||
matrixNames: peers, name2IP: settings.name2IP,
|
||||
sameSubnetOnly: settings.sameSubnetOnly,
|
||||
validateRemoteIP: settings.validateRemoteMachineIP,
|
||||
blockScreenSaver: settings.blockScreenSaverOnOtherMachines)
|
||||
|
||||
Reference in new issue
Block a user