Files
sepot/app/lib/models/attachment.dart
T
Julius Joska e3a2378c34 fix(app): accept incoming calls, restore from recovery phrase, duress session isolation, message loss
Calls: `call` was missing from the known content types, so every offer was
acknowledged and dropped; the call screen column now fills the width (the
avatar jumped left after hang-up); a ring started from a push ends after
12 s when no offer arrives; STUN fallback when no TURN credentials.
Registration: X-Sepot-Identity-Proof lets a new device reclaim an existing
identity; retry backs off from 30 s to 10 min and exposes a conflict flag.
Prekeys: PrekeysLow triggers a batch of 50 one-time prekeys via POST
/v1/keys; the signed prekey rotates weekly, the Kyber prekey monthly.
Messaging: crypto operations run serially (concurrent encrypts derived the
same message key); stop() waits for in-flight deliveries (auto-lock between
decrypt and persist lost messages); disappearing-message cleanup no longer
overwrites fresh messages; a push wake-up reconnects stale connections; the
outbox carries polls and mentions; length limits on wire input.
Storage: a duress session never exposes the real identity or phrase, does
not start networking and cannot touch the keystore; deleting data in a
duress session leaves the real account alone; PIN guessing is throttled
(5 misses, then 30 s doubling up to 15 min); the duress blob shares the salt
so unlock time does not reveal which PIN was entered; iOS keychain items are
this-device only; biometrics are biometric-only; screen security defaults
to on and is applied after unlock; tapjacking filter in MainActivity.
Tests: content types, shared-salt unwrap, duress isolation.
2026-10-09 14:02:33 +02:00

156 lines
5.1 KiB
Dart

import 'dart:convert';
import 'dart:typed_data';
import 'package:sepot/core/constants/app_constants.dart';
/// Stav přenosu přílohy — čistě lokální (na drát nikdy nejde).
enum AttachmentTransferState {
/// Přijatá zpráva, blob se ještě nezačal stahovat.
pending,
downloading,
/// Blob leží (šifrovaný) v lokálním AttachmentStore.
ready,
/// Download/ověření selhalo — UI nabídne opakování.
failed,
}
/// Metadata přílohy. Klíč/nonce/digest dorazily E2E kanálem (relay je
/// nevidí); v lokální DB žijí uvnitř SQLCipher, takže at-rest jsou
/// chráněné stejně jako zbytek zpráv.
///
/// Wire tvar (pole `att` v content JSON, krátké klíče kvůli obálce):
/// { bid, k, n, d, s, mime, name?, w?, h?, th? }
class AttachmentMeta {
const AttachmentMeta({
required this.blobId,
required this.keyB64,
required this.nonceB64,
required this.digestB64,
required this.size,
required this.mime,
this.name,
this.width,
this.height,
this.thumbB64,
this.durationMs,
this.state = AttachmentTransferState.pending,
});
final String blobId; // 32 hex znaků (id na relayi i lokálně)
final String keyB64; // 32 B per-file AES klíč
final String nonceB64; // 12 B GCM nonce
final String digestB64; // SHA-256 šifrovaného blobu
final int size; // velikost PLAINTEXTU v bajtech
final String mime;
final String? name; // původní název souboru (dokumenty)
final int? width; // rozměry obrázku
final int? height;
final String? thumbB64; // JPEG náhled — jede UVNITŘ E2E plaintextu
final int? durationMs; // délka hlasové zprávy / audia
final AttachmentTransferState state;
bool get isImage => mime.startsWith('image/');
bool get isAudio => mime.startsWith('audio/');
Uint8List get key => base64Decode(keyB64);
Uint8List get nonce => base64Decode(nonceB64);
Uint8List get digest => base64Decode(digestB64);
Uint8List? get thumbnail =>
thumbB64 == null ? null : base64Decode(thumbB64!);
AttachmentMeta copyWith({AttachmentTransferState? state}) {
return AttachmentMeta(
blobId: blobId,
keyB64: keyB64,
nonceB64: nonceB64,
digestB64: digestB64,
size: size,
mime: mime,
name: name,
width: width,
height: height,
thumbB64: thumbB64,
durationMs: durationMs,
state: state ?? this.state,
);
}
/// Do content JSON zprávy (bez lokálního stavu).
Map<String, dynamic> toWire() => {
'bid': blobId,
'k': keyB64,
'n': nonceB64,
'd': digestB64,
's': size,
'mime': mime,
if (name != null) 'name': name,
if (width != null) 'w': width,
if (height != null) 'h': height,
if (thumbB64 != null) 'th': thumbB64,
if (durationMs != null) 'dur': durationMs,
};
/// Z content JSON příchozí zprávy. Remote input — tvary se validují
/// (blob_id formát hlídá AttachmentStore, tady rozsahy a typy).
factory AttachmentMeta.fromWire(Map<String, dynamic> wire) {
final size = wire['s'] as int;
if (size <= 0 || size > AppConstants.maxAttachmentBytes) {
throw const FormatException('implausible attachment size');
}
// Krypto pole MUSÍ být validní base64 správné délky (AES-256-GCM klíč,
// 96bitová nonce, SHA-256 digest) — jinak by vadná meta prošla a
// selhala až při otevření přílohy, kdy je obálka dávno ACKnutá.
if (base64Decode(wire['k'] as String).length != 32 ||
base64Decode(wire['n'] as String).length != 12 ||
base64Decode(wire['d'] as String).length != 32) {
throw const FormatException('invalid attachment key material');
}
final name = wire['name'] as String?;
final mime = wire['mime'] as String;
if ((name != null && name.length > 255) || mime.length > 100) {
throw const FormatException('attachment name or mime too long');
}
// Vadný náhled zprávu nezabíjí — UI ukáže placeholder a ostrá fotka
// se stáhne z blobu. base64Decode v build() by shodil celou bublinu.
var thumb = wire['th'] as String?;
if (thumb != null) {
try {
base64Decode(thumb);
} on FormatException {
thumb = null;
}
}
return AttachmentMeta(
blobId: wire['bid'] as String,
keyB64: wire['k'] as String,
nonceB64: wire['n'] as String,
digestB64: wire['d'] as String,
size: size,
mime: mime,
name: name,
width: wire['w'] as int?,
height: wire['h'] as int?,
thumbB64: thumb,
durationMs: wire['dur'] as int?,
state: AttachmentTransferState.pending,
);
}
/// Lokální perzistence (SQLCipher) — wire tvar + stav.
Map<String, dynamic> toJson() => {
...toWire(),
'state': state.name,
};
factory AttachmentMeta.fromJson(Map<String, dynamic> json) {
final base = AttachmentMeta.fromWire(json);
final stateName = json['state'] as String?;
return base.copyWith(
state: AttachmentTransferState.values.asNameMap()[stateName] ??
AttachmentTransferState.pending,
);
}
}