Harden Apple Watch snapshot synchronization

This commit is contained in:
2026-08-17 18:06:09 +02:00
parent 5c88d3ace0
commit 2a59803b6c
8 changed files with 372 additions and 64 deletions

View File

@@ -10,6 +10,8 @@ use data_encoding::HEXLOWER;
#[cfg(feature = "full")]
use data_encoding::HEXLOWER_PERMISSIVE;
#[cfg(feature = "full")]
use rand::{RngCore as _, rngs::OsRng};
#[cfg(feature = "full")]
use serde::{Deserialize, Serialize};
use sha2::{Digest as _, Sha256};
@@ -221,30 +223,17 @@ struct AcceptedSnapshot {
impl WatchSnapshotReceiver {
pub fn apply(&mut self, bytes: SecretBytes) -> Result<WatchSnapshotApply, WatchSnapshotError> {
let snapshot = match decode_snapshot(bytes.expose()) {
Ok(snapshot) => snapshot,
Err(error) => {
self.current = None;
return Err(error);
}
};
let mut pairing_changed = false;
// A valid revocation can only remove secrets, so it must survive a sender reset.
if !snapshot.is_revocation()
&& let Some(accepted) = self.accepted
{
if snapshot.revision < accepted.revision {
let snapshot = decode_snapshot(bytes.expose())?;
if let Some(accepted) = self.accepted {
if snapshot.pairing != accepted.pairing || snapshot.revision < accepted.revision {
return Ok(WatchSnapshotApply::Stale);
} else if snapshot.revision == accepted.revision {
if snapshot.pairing != accepted.pairing || snapshot.digest != accepted.digest {
self.current = None;
if snapshot.digest != accepted.digest {
return Err(WatchSnapshotError::RevisionConflict);
}
if self.current.is_some() {
return Ok(WatchSnapshotApply::Duplicate);
}
} else if accepted.pairing != snapshot.pairing {
pairing_changed = true;
}
}
let accepted = AcceptedSnapshot {
@@ -252,6 +241,37 @@ impl WatchSnapshotReceiver {
revision: snapshot.revision,
digest: snapshot.digest,
};
let result = if snapshot.is_revocation() {
WatchSnapshotApply::Revoked
} else {
WatchSnapshotApply::Replaced
};
self.accepted = Some(accepted);
self.current = Some(snapshot);
Ok(result)
}
pub fn replace_authoritative(
&mut self,
bytes: SecretBytes,
) -> Result<WatchSnapshotApply, WatchSnapshotError> {
let snapshot = decode_snapshot(bytes.expose())?;
let pairing_changed = self
.accepted
.is_some_and(|accepted| accepted.pairing != snapshot.pairing);
if let Some(accepted) = self.accepted
&& !pairing_changed
{
if snapshot.revision < accepted.revision {
return Ok(WatchSnapshotApply::Stale);
}
if accepted.revision == snapshot.revision
&& accepted.digest == snapshot.digest
&& self.current.is_some()
{
return Ok(WatchSnapshotApply::Duplicate);
}
}
let result = if snapshot.is_revocation() {
WatchSnapshotApply::Revoked
} else if pairing_changed {
@@ -259,7 +279,11 @@ impl WatchSnapshotReceiver {
} else {
WatchSnapshotApply::Replaced
};
self.accepted = Some(accepted);
self.accepted = Some(AcceptedSnapshot {
pairing: snapshot.pairing,
revision: snapshot.revision,
digest: snapshot.digest,
});
self.current = Some(snapshot);
Ok(result)
}
@@ -432,14 +456,36 @@ impl WatchRuntime {
let apply = match self.receiver.apply(SecretBytes::new(bytes)) {
Ok(apply) => apply,
Err(error) => {
self.presentation_state = WatchPresentationState::Error;
self.sync_failed();
return Err(error);
}
};
Ok(self.finish_apply(apply))
}
pub fn apply_authoritative_snapshot(
&mut self,
bytes: Vec<u8>,
) -> Result<WatchSnapshotUpdate, WatchSnapshotError> {
let apply = match self.receiver.replace_authoritative(SecretBytes::new(bytes)) {
Ok(apply) => apply,
Err(error) => {
self.sync_failed();
return Err(error);
}
};
Ok(self.finish_apply(apply))
}
fn finish_apply(&mut self, apply: WatchSnapshotApply) -> WatchSnapshotUpdate {
self.protected_data_available = true;
self.presentation_state = match apply {
WatchSnapshotApply::Revoked => WatchPresentationState::Empty,
WatchSnapshotApply::Stale => WatchPresentationState::Stale,
WatchSnapshotApply::Stale => match self.receiver.current() {
Some(snapshot) if snapshot.is_revocation() => WatchPresentationState::Empty,
Some(_) => WatchPresentationState::Ready,
None => WatchPresentationState::Stale,
},
WatchSnapshotApply::Replaced
| WatchSnapshotApply::Duplicate
| WatchSnapshotApply::PairingChanged => WatchPresentationState::Ready,
@@ -454,7 +500,7 @@ impl WatchRuntime {
}
};
let current = self.receiver.current();
Ok(WatchSnapshotUpdate {
WatchSnapshotUpdate {
apply,
persistence,
revision: current.map(WatchSnapshot::revision),
@@ -462,7 +508,7 @@ impl WatchRuntime {
.map(|snapshot| u32::try_from(snapshot.entries().len()).unwrap_or(u32::MAX))
.unwrap_or(0),
receipt: self.receiver.current_receipt().unwrap_or_default(),
})
}
}
pub fn records_at(
@@ -573,8 +619,11 @@ impl WatchRuntime {
}
pub fn sync_failed(&mut self) {
self.receiver.clear_secrets();
self.presentation_state = WatchPresentationState::Error;
self.presentation_state = if self.receiver.current().is_some() {
WatchPresentationState::Stale
} else {
WatchPresentationState::Error
};
}
pub fn protected_data_unavailable(&mut self) {
@@ -620,6 +669,8 @@ impl WatchSnapshotSender {
) -> Result<(), WatchSnapshotError> {
if unpaired {
self.journal.pairing = None;
self.journal.generation = None;
self.journal.revision = 0;
self.journal.digest = None;
self.journal.snapshot_digest = None;
self.journal.delivered_revision = None;
@@ -646,29 +697,44 @@ impl WatchSnapshotSender {
if platform_pairing_identity.trim().is_empty() {
return Err(WatchSnapshotError::InvalidPairing);
}
let pairing = digest(platform_pairing_identity.as_bytes());
let platform_pairing = digest(platform_pairing_identity.as_bytes());
let content = encode_entries(&entries)?;
let content_digest = digest(&content);
let pairing_text = HEXLOWER.encode(&pairing);
let pairing_text = HEXLOWER.encode(&platform_pairing);
let digest_text = HEXLOWER.encode(&content_digest);
if self.journal.pairing.as_deref() != Some(&pairing_text)
|| self.journal.digest.as_deref() != Some(&digest_text)
|| self.journal.generation.is_none()
{
let mut generation = [0_u8; 32];
OsRng.fill_bytes(&mut generation);
self.journal.pairing = Some(pairing_text);
self.journal.generation = Some(HEXLOWER.encode(&generation));
self.journal.revision = 0;
self.journal.digest = None;
self.journal.snapshot_digest = None;
self.journal.delivered_revision = None;
self.journal.current_revision = None;
}
if self.journal.digest.as_deref() != Some(&digest_text) {
self.journal.revision = self
.journal
.revision
.checked_add(1)
.ok_or(WatchSnapshotError::RevisionOverflow)?;
self.journal.pairing = Some(pairing_text);
self.journal.digest = Some(digest_text);
self.journal.delivered_revision = None;
self.journal.current_revision = None;
save_journal(&self.path, &self.journal)?;
}
if self.journal.revision == 0 {
self.journal.revision = 1;
save_journal(&self.path, &self.journal)?;
}
let generation = decode_hash(
self.journal
.generation
.as_deref()
.ok_or(WatchSnapshotError::InvalidJournal)?,
)?;
let pairing = snapshot_pairing(platform_pairing, generation);
let snapshot = encode_snapshot(pairing, self.journal.revision, &content)?;
let snapshot_digest = digest(snapshot.expose());
self.journal.snapshot_digest = Some(HEXLOWER.encode(&snapshot_digest));
@@ -709,12 +775,19 @@ impl WatchSnapshotSender {
receipt: &[u8],
) -> Result<MobileWatchSnapshotStatus, WatchSnapshotError> {
let receipt = decode_receipt(receipt)?;
let pairing = decode_hash(
let platform_pairing = decode_hash(
self.journal
.pairing
.as_deref()
.ok_or(WatchSnapshotError::NoPendingSnapshot)?,
)?;
let generation = decode_hash(
self.journal
.generation
.as_deref()
.ok_or(WatchSnapshotError::NoPendingSnapshot)?,
)?;
let pairing = snapshot_pairing(platform_pairing, generation);
if receipt.pairing != pairing || receipt.revision != self.journal.revision {
return Ok(self.status());
}
@@ -762,6 +835,8 @@ struct SenderJournal {
#[serde(default)]
pairing: Option<String>,
#[serde(default)]
generation: Option<String>,
#[serde(default)]
revision: u64,
#[serde(default)]
digest: Option<String>,
@@ -779,6 +854,7 @@ impl Default for SenderJournal {
Self {
version: JOURNAL_VERSION,
pairing: None,
generation: None,
revision: 0,
digest: None,
snapshot_digest: None,
@@ -1085,6 +1161,15 @@ fn set_private_permissions(_temporary: &TempFile<'_>) -> Result<(), WatchSnapsho
fn digest(bytes: &[u8]) -> [u8; 32] {
Sha256::digest(bytes).into()
}
#[cfg(feature = "full")]
fn snapshot_pairing(platform_pairing: [u8; 32], generation: [u8; 32]) -> [u8; 32] {
let mut hasher = Sha256::new();
hasher.update(b"IronStorage Watch snapshot generation\0");
hasher.update(platform_pairing);
hasher.update(generation);
hasher.finalize().into()
}
#[cfg(feature = "full")]
fn decode_hash(text: &str) -> Result<[u8; 32], WatchSnapshotError> {
let bytes = HEXLOWER_PERMISSIVE