diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e0245ac --- /dev/null +++ b/.dockerignore @@ -0,0 +1,10 @@ +.git +.github +target +tests +docs +*.toml +!Cargo.toml +*.log +*.sqlite* +deployment-* diff --git a/.github/workflows/native.yml b/.github/workflows/native.yml new file mode 100644 index 0000000..42e1597 --- /dev/null +++ b/.github/workflows/native.yml @@ -0,0 +1,55 @@ +name: Signal service native checks +on: + push: + pull_request: +permissions: + contents: read +concurrency: + group: signal-service-${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true +jobs: + native: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + toolchain: ['1.95.0', stable] + services: + postgres: + image: postgres:16.11-alpine + env: + POSTGRES_DB: signal_node_tests + POSTGRES_USER: signal_node_tests + POSTGRES_HOST_AUTH_METHOD: trust + ports: + - 5432:5432 + options: >- + --health-cmd "pg_isready -U signal_node_tests -d signal_node_tests" + --health-interval 5s --health-timeout 5s --health-retries 10 + env: + SIGNAL_NODE_TEST_POSTGRES_URL: postgres://signal_node_tests@127.0.0.1:5432/signal_node_tests + steps: + - uses: actions/checkout@v4 + with: + persist-credentials: false + - uses: dtolnay/rust-toolchain@stable + with: + toolchain: ${{ matrix.toolchain }} + components: rustfmt, clippy + - uses: Swatinem/rust-cache@v2 + with: + key: signal-service-${{ matrix.toolchain }} + - run: cargo +${{ matrix.toolchain }} fmt --all --check + - run: cargo +${{ matrix.toolchain }} clippy --workspace --all-targets --locked -- -D warnings + - run: cargo +${{ matrix.toolchain }} test --workspace --all-targets --locked + - run: cargo +${{ matrix.toolchain }} test --workspace --doc --locked + - run: cargo +${{ matrix.toolchain }} build --release --locked + - run: ./target/release/signal-node --version + container: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + persist-credentials: false + - run: docker build --tag signal-node:ci . + - run: docker run --rm --read-only --cap-drop ALL --security-opt no-new-privileges signal-node:ci --version diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..68f982c --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,3867 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aead" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0" +dependencies = [ + "crypto-common 0.1.7", + "generic-array", +] + +[[package]] +name = "aead" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1973cfbc1a2daf9cf550e74e1f088c28e7f7d8c1e1418fb6c9dc5184b7e84c99" +dependencies = [ + "crypto-common 0.2.2", + "inout 0.2.2", +] + +[[package]] +name = "aes" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" +dependencies = [ + "cfg-if", + "cipher 0.4.4", + "cpufeatures 0.2.17", +] + +[[package]] +name = "aes" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35f0f96ce78e38c3dc6d8948aa8163d06385be74000f3c7a95bf1eef35d3ea32" +dependencies = [ + "cipher 0.5.2", + "cpubits", + "cpufeatures 0.3.1", +] + +[[package]] +name = "aes-gcm" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "831010a0f742e1209b3bcea8fab6a8e149051ba6099432c8cb2cc117dec3ead1" +dependencies = [ + "aead 0.5.2", + "aes 0.8.4", + "cipher 0.4.4", + "ctr 0.9.2", + "ghash 0.5.1", + "subtle", +] + +[[package]] +name = "aes-gcm" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f2b8006a0c83f52b62ba44a97b58bf76fe2f70a329e588f67f89691d93d498f" +dependencies = [ + "aead 0.6.1", + "aes 0.9.3", + "cipher 0.5.2", + "ctr 0.10.1", + "ctutils", + "ghash 0.6.0", +] + +[[package]] +name = "aho-corasick" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba" +dependencies = [ + "memchr", +] + +[[package]] +name = "allocator-api2" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" + +[[package]] +name = "anyhow" +version = "1.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" + +[[package]] +name = "arrayvec" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fb67a6e08acf24fdeccbac2cb6ac4305825bd1f117462e0e6f2f193345ad56" + +[[package]] +name = "async-channel" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "924ed96dd52d1b75e9c1a3e6275715fd320f5f9439fb5a4a11fa51f4221158d2" +dependencies = [ + "concurrent-queue", + "event-listener-strategy", + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "async-lock" +version = "3.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "290f7f2596bd5b78a9fec8088ccd89180d7f9f55b94b0576823bbbdc72ee8311" +dependencies = [ + "event-listener", + "event-listener-strategy", + "pin-project-lite", +] + +[[package]] +name = "async-trait" +version = "0.1.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82f6aeea286b8eb4dd3431a1be1b59d290ace00f5bfd8e2a159bc2a05e2c1667" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.7", +] + +[[package]] +name = "atoi" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f28d99ec8bfea296261ca1af174f24225171fea9664ba9003cbebee704810528" +dependencies = [ + "num-traits", +] + +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + +[[package]] +name = "axum" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31b698c5f9a010f6573133b09e0de5408834d0c82f8d7475a89fc1867a71cd90" +dependencies = [ + "axum-core", + "bytes", + "form_urlencoded", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-util", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "serde_core", + "serde_json", + "serde_path_to_error", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tower", + "tower-layer", + "tower-service", +] + +[[package]] +name = "axum-core" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c78f31d7b1291f7ee735c1c6780ccde7785daae9a9206026862dab7d8792d1" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "http-body-util", + "mime", + "pin-project-lite", + "sync_wrapper", + "tower-layer", + "tower-service", +] + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "base64" +version = "0.23.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac07cdecf99051d9a5238b80f35af32cdeba5b336e55d957b318b50137e18da5" + +[[package]] +name = "base64ct" +version = "1.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06" + +[[package]] +name = "beef" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a8241f3ebb85c056b509d4327ad0358fbbba6ffb340bf388f26350aeda225b1" + +[[package]] +name = "bitflags" +version = "2.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" +dependencies = [ + "serde_core", +] + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "block-buffer" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2f6c7dbe95a6ed67ad9f18e57daf93a2f034c524b99fd2b76d18fdfeb6660aa" +dependencies = [ + "hybrid-array", +] + +[[package]] +name = "block-padding" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "710f1dd022ef4e93f8a438b4ba958de7f64308434fa6a87104481645cc30068b" +dependencies = [ + "hybrid-array", +] + +[[package]] +name = "bon" +version = "3.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "214f5df094ce551a10a30ffb9c70243d61f121a3d985a6495933e181dee6a7d2" +dependencies = [ + "bon-macros", +] + +[[package]] +name = "bon-macros" +version = "3.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2706da6c749998cc555d04184909608956a69ad3f8ab1a076fbc867b4a3c3ce5" +dependencies = [ + "darling", + "ident_case", + "prettyplease 0.3.0", + "proc-macro2", + "quote", + "syn 3.0.7", +] + +[[package]] +name = "borrow-or-share" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc0b364ead1874514c8c2855ab558056ebfeb775653e7ae45ff72f28f8f3166c" + +[[package]] +name = "buffa" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a92f2f5df67a9d5ccfc65237bfc954c56328aee40a04a9b92381ecba370be246" +dependencies = [ + "base64 0.22.1", + "bytes", + "foldhash", + "hashbrown 0.15.5", + "once_cell", + "rustversion", + "serde", + "serde_json", + "smoothutf8", + "thiserror", +] + +[[package]] +name = "buffa-build" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eee6b6b4a8d23f1f98a043f188f4d9d2ef8bee43191f42e93ac577f3c3b0f334" +dependencies = [ + "buffa", + "buffa-codegen", + "tempfile", +] + +[[package]] +name = "buffa-codegen" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb4bea10ef85ea3d06a20412c67cb612527feb25f33367eab850139441fb5dd9" +dependencies = [ + "buffa", + "buffa-descriptor", + "prettyplease 0.2.37", + "proc-macro2", + "quote", + "syn 2.0.119", + "thiserror", +] + +[[package]] +name = "buffa-descriptor" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b7dd79a6898e70dac8adae959c8ad2cae51ed96397935c91c996280cedcb55e" +dependencies = [ + "buffa", + "rustversion", + "serde", + "serde_json", +] + +[[package]] +name = "bumpalo" +version = "3.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "bytes" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" + +[[package]] +name = "castaway" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dec551ab6e7578819132c713a93c022a05d60159dc86e7a7050223577484c55a" +dependencies = [ + "rustversion", +] + +[[package]] +name = "cbc" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce2dc9ee5f88d11e0beb842c88b33c8a5cf0d1329c4b19494af42b07dbfe8896" +dependencies = [ + "cipher 0.5.2", +] + +[[package]] +name = "cc" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50a649af8a827553c29fb0cb4bd4a6f1a0dd695bd3232b9bc98bd9c8a3ffbb8b" +dependencies = [ + "find-msvc-tools", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" + +[[package]] +name = "cfg_aliases" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f079e83a288787bcd14a6aea84cee5c87a67c5a3e660c30f557a3d24761b3527" + +[[package]] +name = "chacha20" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "rand_core 0.10.1", +] + +[[package]] +name = "chrono" +version = "0.4.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327" +dependencies = [ + "num-traits", + "serde", +] + +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common 0.1.7", + "inout 0.1.4", +] + +[[package]] +name = "cipher" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8cf2a2c93cd704877c0858356ed03480ff301ee950b43f1cbe4573b088bfa6c" +dependencies = [ + "block-buffer 0.12.1", + "crypto-common 0.2.2", + "inout 0.2.2", +] + +[[package]] +name = "cmov" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c9ea0ac24bc397ab3c98583a3c9ba74fa56b09a4449bbe172b9b1ddb016027a" + +[[package]] +name = "compact_str" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79fcda08c33bb58b97008b2cdada6622500e949e060f5913361763121abd2416" +dependencies = [ + "castaway", + "cfg-if", + "itoa", + "serde", + "static_assertions", + "zmij", +] + +[[package]] +name = "concurrent-queue" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "const-oid" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" + +[[package]] +name = "cpubits" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15b85f9c39137c3a891689859392b1bd49812121d0d61c9caf00d46ed5ce06ae" + +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + +[[package]] +name = "cpufeatures" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566" +dependencies = [ + "libc", +] + +[[package]] +name = "crc" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5eb8a2a1cd12ab0d987a5d5e825195d372001a4094a0376319d5a0ad71c1ba0d" +dependencies = [ + "crc-catalog", +] + +[[package]] +name = "crc-catalog" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "217698eaf96b4a3f0bc4f3662aaa55bdf913cd54d7204591faa790070c6d0853" + +[[package]] +name = "crossbeam-queue" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03e8bd762f7479489c70ed6c768ddca99d7296857de437a68dcb2a94365b3fae" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6" + +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "rand_core 0.6.4", + "typenum", +] + +[[package]] +name = "crypto-common" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453" +dependencies = [ + "hybrid-array", +] + +[[package]] +name = "ctr" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0369ee1ad671834580515889b80f2ea915f23b8be8d0daa4bbaf2ac5c7590835" +dependencies = [ + "cipher 0.4.4", +] + +[[package]] +name = "ctr" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baaca1c4b237092596f64d571e9db6ce4109c4ef9742e27590f1709594461f21" +dependencies = [ + "cipher 0.5.2", +] + +[[package]] +name = "ctutils" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03bb0e1cc970d482d121d9a1744999169b69a07470b3d644a7894e53fcaf4574" +dependencies = [ + "cmov", +] + +[[package]] +name = "darling" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed17f5901b6630b993ca003def43f2f8ef4014fc13b047b57aad617ff32bc2ec" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6837e2cf7485aaae18f86181d2f0e9a7ed297a025e220aeabf63fdebd3a2ddff" +dependencies = [ + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn 3.0.7", +] + +[[package]] +name = "darling_macro" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ac7135c3ef02b2f7833bbeb1be5ba7f966dcde8a87c6b87f65a778d71a02785" +dependencies = [ + "darling_core", + "quote", + "syn 3.0.7", +] + +[[package]] +name = "data-encoding" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4583a4551df46e2792f82ceeac45e850d2e2d5debba0b91f102385cda5b11f06" + +[[package]] +name = "der" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7c1832837b905bbfb5101e07cc24c8deddf52f93225eee6ead5f4d63d53ddcb" +dependencies = [ + "const-oid", + "pem-rfc7468", + "zeroize", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer 0.10.4", + "const-oid", + "crypto-common 0.1.7", + "subtle", +] + +[[package]] +name = "digest" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2" +dependencies = [ + "block-buffer 0.12.1", + "crypto-common 0.2.2", + "ctutils", +] + +[[package]] +name = "displaydoc" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6232dd377dcc64799954cbd3a9bb882e9cdc1308ccd87b1c098f1fb2eaf82a8" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.7", +] + +[[package]] +name = "dotenvy" +version = "0.15.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" + +[[package]] +name = "either" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be" +dependencies = [ + "serde", +] + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "etcetera" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "136d1b5283a1ab77bd9257427ffd09d8667ced0570b6f938942bc7568ed5b943" +dependencies = [ + "cfg-if", + "home", + "windows-sys 0.48.0", +] + +[[package]] +name = "event-listener" +version = "5.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a23add41df1562121a9393cb065eab5146a1242410f23a644851e90cfd669d2" +dependencies = [ + "parking", + "pin-project-lite", +] + +[[package]] +name = "event-listener-strategy" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8be9f3dfaaffdae2972880079a491a1a8bb7cbed0b8dd7a347f668b4150a3b93" +dependencies = [ + "event-listener", + "pin-project-lite", +] + +[[package]] +name = "fastrand" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" + +[[package]] +name = "find-msvc-tools" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aedcfb3409746eddb02b9e19ebda1c3394f759a152e48ee875a0844d1b955484" + +[[package]] +name = "fixedbitset" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d674e81391d1e1ab681a28d99df07927c6d4aa5b027d7da16ba32d1d21ecd99" + +[[package]] +name = "fluent-uri" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc74ac4d8359ae70623506d512209619e5cf8f347124910440dbc221714b328e" +dependencies = [ + "borrow-or-share", + "ref-cast", + "serde", +] + +[[package]] +name = "flume" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da0e4dd2a88388a1f4ccc7c9ce104604dab68d9f408dc34cd45823d5a9069095" +dependencies = [ + "futures-core", + "futures-sink", + "spin", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + +[[package]] +name = "form_urlencoded" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "futures" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a31d2a3fbaaeb2af2368bbdd904aa8e812d3c04a1ee10d3171f52d556e5d0a3" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f9e3d69d39e4862ffed03ed071a76f9a13ba1d9109d355b0f0aa6b15e393c4" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" + +[[package]] +name = "futures-executor" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "031b47cf1a3c6cc8bc2fc76cd437f521619387907d469316e7c0bc278f1f5432" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-intrusive" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d930c203dd0b6ff06e0201a4a2fe9149b43c684fd4420555b26d21b1a02956f" +dependencies = [ + "futures-core", + "lock_api", + "parking_lot", +] + +[[package]] +name = "futures-io" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53c0fa8157de1303bfffdaa1cc2a673bfffb60102f76b0ef4441659124373fed" + +[[package]] +name = "futures-macro" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb9654ba8355388abeb8dcb4fc62f511300867002afc858860463bdd9fe0c44" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.7", +] + +[[package]] +name = "futures-sink" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1944426bf7d03f1d14f708785e4b33efd750b36d48a157b836b3efc15ede8e1d" + +[[package]] +name = "futures-task" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd417de3d1d015fc3bfd2b1ea46dfc7bab72ef86f1cc7cc9c78e728b34a6d1fd" + +[[package]] +name = "futures-util" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "slab", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "wasi", + "wasm-bindgen", +] + +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "libc", + "r-efi 5.3.0", + "wasip2", +] + +[[package]] +name = "getrandom" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "r-efi 6.0.0", + "rand_core 0.10.1", + "wasm-bindgen", +] + +[[package]] +name = "ghash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0d8a4362ccb29cb0b265253fb0a2728f592895ee6854fd9bc13f2ffda266ff1" +dependencies = [ + "opaque-debug", + "polyval 0.6.2", +] + +[[package]] +name = "ghash" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2eecf2d5dc9b66b732b97707a0210906b1d30523eb773193ab777c0c84b3e8d5" +dependencies = [ + "polyval 0.7.3", +] + +[[package]] +name = "hashbrown" +version = "0.15.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +dependencies = [ + "allocator-api2", + "equivalent", + "foldhash", + "serde", +] + +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" + +[[package]] +name = "hashlink" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7382cf6263419f2d8df38c55d7da83da5c18aef87fc7a7fc1fb1e344edfe14c1" +dependencies = [ + "hashbrown 0.15.5", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "hkdf" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b5f8eb2ad728638ea2c7d47a21db23b7b58a72ed6a38256b8a1849f15fbbdf7" +dependencies = [ + "hmac 0.12.1", +] + +[[package]] +name = "hkdf" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4aaa26c720c68b866f2c96ef5c1264b3e6f473fe5d4ce61cd44bbe913e553018" +dependencies = [ + "hmac 0.13.0", +] + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest 0.10.7", +] + +[[package]] +name = "hmac" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6303bc9732ae41b04cb554b844a762b4115a61bfaa81e3e83050991eeb56863f" +dependencies = [ + "digest 0.11.3", +] + +[[package]] +name = "home" +version = "0.5.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc627f471c528ff0c4a49e1d5e60450c8f6461dd6d10ba9dcd3a61d3dff7728d" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "http" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "918d3568bebf352712bc2ef3d46a8bcf1a75b373be6539de198e9105cbbf9ce0" +dependencies = [ + "bytes", + "itoa", +] + +[[package]] +name = "http-body" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca2a8f2913ee65f60facd6a5905613afaa448497a0230cc41ce022d93290bc2c" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "http-body-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23169fe34a5fbcdd3f3862e78fb9b6fccd5f02a6dc6f732547005d45631ce71c" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "hybrid-array" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27f864f10dfb56725ce5ce5472bc52252c8f93a4ab86327122cebf62c5f59a17" +dependencies = [ + "typenum", +] + +[[package]] +name = "hyper" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c3e324da4c95177d6291d4c8730197c0d1822f8a9766814a4a44fa5ab797c9c" +dependencies = [ + "atomic-waker", + "bytes", + "futures-core", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "smallvec", + "tokio", + "want", +] + +[[package]] +name = "hyper-rustls" +version = "0.27.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfa8e654703247911e29c23fbeaa261834bd9bb74efba2f9acddc37bfb127f53" +dependencies = [ + "http", + "hyper", + "hyper-util", + "rustls", + "tokio", + "tokio-rustls", + "tower-service", + "webpki-roots 1.0.9", +] + +[[package]] +name = "hyper-util" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddc03d96684f9226b8a787cdb71488417b53ab5ea8fdb1dac946cb9431cc8bff" +dependencies = [ + "base64 0.23.1", + "bytes", + "futures-channel", + "futures-util", + "http", + "http-body", + "httparse", + "hyper", + "ipnet", + "libc", + "percent-encoding", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", +] + +[[package]] +name = "icu_collections" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa68d21081c4a05d5a901a1c62add574c77048b6a1c67be3b50ce0b60d4ca513" +dependencies = [ + "displaydoc", + "potential_utf", + "utf8_iter", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locale_core" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d56e28588da92eee5c3201a6eff33fabdd49b62269c8938d4ff050ce4d900deb" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_normalizer" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12f9cf5f235641ed274641dd81c3f28d870e276763d0797aeeab72317b1c646f" +dependencies = [ + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1563da1ed3e0b3bf3d74c9b85917ac9c56464d2f57242270c09c9e752f8021a0" + +[[package]] +name = "icu_properties" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e7ca276ad3145661a65914e6daf131ca5120cd3dcee8f8f3214b8875184a148" +dependencies = [ + "displaydoc", + "icu_collections", + "icu_locale_core", + "icu_properties_data", + "icu_provider", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e590f038c1464a96894fd6d10127e90a8be4509f56ff7ecef851b15cee0b7caa" + +[[package]] +name = "icu_provider" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d27bbb9d3abbefac45d55f647c9de1d44aafcd1186eb91879afef17c396c3e73" +dependencies = [ + "displaydoc", + "icu_locale_core", + "writeable", + "yoke", + "zerofrom", + "zerotrie", + "zerovec", +] + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + +[[package]] +name = "idna" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb68373c0d6620ef8105e855e7745e18b0d00d3bdb07fb532e434244cdb9a714" +dependencies = [ + "icu_normalizer", + "icu_properties", +] + +[[package]] +name = "indexmap" +version = "2.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855" +dependencies = [ + "equivalent", + "hashbrown 0.17.1", +] + +[[package]] +name = "inout" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" +dependencies = [ + "generic-array", +] + +[[package]] +name = "inout" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4250ce6452e92010fdf7268ccc5d14faa80bb12fc741938534c58f16804e03c7" +dependencies = [ + "block-padding", + "hybrid-array", +] + +[[package]] +name = "ipnet" +version = "2.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "791930b43c0d5973160d90a8f3894509f2b273430f5c5c73b668636d0287c5c0" + +[[package]] +name = "itertools" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" + +[[package]] +name = "js-sys" +version = "0.3.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7883d941dae510fb2d978fc3fe018c71c9e2892fd38854de3e8b92c2e5ad9cc5" +dependencies = [ + "cfg-if", + "futures-util", + "wasm-bindgen", +] + +[[package]] +name = "lazy_static" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb" +dependencies = [ + "spin", +] + +[[package]] +name = "libc" +version = "0.2.190" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce5d3ddc6d3fa000eb1536d85e147bfe31aacaba692ed6a876f95cb7c855be78" + +[[package]] +name = "libm" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981" + +[[package]] +name = "libredox" +version = "0.1.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61ff90caf6077a803a240f62fdbe88645a890bbca49ef8174c3cb0404362171d" +dependencies = [ + "bitflags", + "libc", + "plain", + "redox_syscall 0.9.4", +] + +[[package]] +name = "libsqlite3-sys" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e99fb7a497b1e3339bc746195567ed8d3e24945ecd636e3619d20b9de9e9149" +dependencies = [ + "cc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "linux-raw-sys" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" + +[[package]] +name = "litemap" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d9d19d1d6efa0109d2f65ff4c85cddd50bd572e5a00127ab10987290bcefae" + +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6" + +[[package]] +name = "logos" +version = "0.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff472f899b4ec2d99161c51f60ff7075eeb3097069a36050d8037a6325eb8154" +dependencies = [ + "logos-derive 0.15.1", +] + +[[package]] +name = "logos" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb2c55a318a87600ea870ff8c2012148b44bf18b74fad48d0f835c38c7d07c5f" +dependencies = [ + "logos-derive 0.16.1", +] + +[[package]] +name = "logos-codegen" +version = "0.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "192a3a2b90b0c05b27a0b2c43eecdb7c415e29243acc3f89cc8247a5b693045c" +dependencies = [ + "beef", + "fnv", + "lazy_static", + "proc-macro2", + "quote", + "regex-syntax", + "rustc_version", + "syn 2.0.119", +] + +[[package]] +name = "logos-codegen" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58b3ffaa284e1350d017a57d04ada118c4583cf260c8fb01e0fe28a2e9cf8970" +dependencies = [ + "fnv", + "proc-macro2", + "quote", + "regex-automata", + "regex-syntax", + "syn 2.0.119", +] + +[[package]] +name = "logos-derive" +version = "0.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "605d9697bcd5ef3a42d38efc51541aa3d6a4a25f7ab6d1ed0da5ac632a26b470" +dependencies = [ + "logos-codegen 0.15.1", +] + +[[package]] +name = "logos-derive" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52d3a9855747c17eaf4383823f135220716ab49bea5fbea7dd42cc9a92f8aa31" +dependencies = [ + "logos-codegen 0.16.1", +] + +[[package]] +name = "lru-slab" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4050469837a6ff301cd14c1f8f24f88549e6d548f24f64e2148eb0f72cebc51f" + +[[package]] +name = "matchers" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9" +dependencies = [ + "regex-automata", +] + +[[package]] +name = "matchit" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3" + +[[package]] +name = "md-5" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf" +dependencies = [ + "cfg-if", + "digest 0.10.7", +] + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "miette" +version = "7.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f98efec8807c63c752b5bd61f862c165c115b0a35685bdcfd9238c7aeb592b7" +dependencies = [ + "cfg-if", + "miette-derive", + "unicode-width", +] + +[[package]] +name = "miette-derive" +version = "7.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db5b29714e950dbb20d5e6f74f9dcec4edbcc1067bb7f8ed198c097b8c1a818b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "mio" +version = "1.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1788edb87fdc09c7e26304471e2f5be8cdefb1b6930d6e3985fc02ff53bf86ee" +dependencies = [ + "libc", + "wasi", + "windows-sys 0.61.2", +] + +[[package]] +name = "multimap" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d87ecb2933e8aeadb3e3a02b828fed80a7528047e68b4f424523a0981a3a084" + +[[package]] +name = "nu-ansi-term" +version = "0.50.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "num-bigint-dig" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e661dda6640fad38e827a6d4a310ff4763082116fe217f279885c97f511bb0b7" +dependencies = [ + "lazy_static", + "libm", + "num-integer", + "num-iter", + "num-traits", + "rand 0.8.8", + "smallvec", + "zeroize", +] + +[[package]] +name = "num-integer" +version = "0.1.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ce2d95d4b3734dc35aa2f45e1aa22cd416814592a4f9d9205e11affd5b8e10b" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c92800bd69a1eac91786bcfe9da64a897eb72911b8dc3095decbd07429e8048b" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", + "libm", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "opaque-debug" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" + +[[package]] +name = "parking" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba" + +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall 0.5.18", + "smallvec", + "windows-link", +] + +[[package]] +name = "pem-rfc7468" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88b39c9bfcfc231068454382784bb460aae594343fb030d46e9f50a645418412" +dependencies = [ + "base64ct", +] + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "petgraph" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8701b58ea97060d5e5b155d383a69952a60943f0e6dfe30b04c287beb0b27455" +dependencies = [ + "fixedbitset", + "hashbrown 0.15.5", + "indexmap", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "pkcs1" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8ffb9f10fa047879315e6625af03c164b16962a5368d724ed16323b68ace47f" +dependencies = [ + "der", + "pkcs8", + "spki", +] + +[[package]] +name = "pkcs8" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7" +dependencies = [ + "der", + "spki", +] + +[[package]] +name = "pkg-config" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" + +[[package]] +name = "plain" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" + +[[package]] +name = "polyval" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d1fe60d06143b2430aa532c94cfe9e29783047f06c0d7fd359a9a51b729fa25" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "opaque-debug", + "universal-hash 0.5.1", +] + +[[package]] +name = "polyval" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0fa31d631f2b2cb2a544d0aa321ce847a94764d701ca2becc411138b93d49cd" +dependencies = [ + "cpubits", + "cpufeatures 0.3.1", + "universal-hash 0.6.1", +] + +[[package]] +name = "portable-atomic" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85" + +[[package]] +name = "potential_utf" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d83eb9bc6d8e5cf568e7a1101d60ee05e81ed50ea106026f3d18deeb046d7661" +dependencies = [ + "zerovec", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "prettyplease" +version = "0.2.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" +dependencies = [ + "proc-macro2", + "syn 2.0.119", +] + +[[package]] +name = "prettyplease" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bfe0f4c752e450fc2faf62654f1c134747922825d5b04ca717b8874f41a40c0" +dependencies = [ + "proc-macro2", + "syn 3.0.7", +] + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "prost" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528ac67416ff8646872a3c02cad9cc4ee5dc9f9540c9b10771855c95cb2e5ae1" +dependencies = [ + "bytes", + "prost-derive", +] + +[[package]] +name = "prost-build" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03da047801ff44bb6a4d407d4860c05fd70bb81714e6b2f3812603d5b145b042" +dependencies = [ + "heck", + "itertools", + "log", + "multimap", + "petgraph", + "prettyplease 0.2.37", + "prost", + "prost-types", + "regex", + "syn 2.0.119", + "tempfile", +] + +[[package]] +name = "prost-derive" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b570b25f7617e43d59005d0990ccb79e950a423952cea19671b7a876da390adf" +dependencies = [ + "anyhow", + "itertools", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "prost-reflect" +version = "0.16.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01b80ea363c31af2de2b92e3c07ed1156628f7838c4afb4df75ee78a37fedbd1" +dependencies = [ + "logos 0.16.1", + "miette", + "prost", + "prost-types", +] + +[[package]] +name = "prost-types" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f94967dc7688f3054c7fac87473ffae4cc4c3904800e2d9f5b857246d8963b0a" +dependencies = [ + "prost", +] + +[[package]] +name = "protox" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f25a07a73c6717f0b9bbbd685918f5df9815f7efba450b83d9c9dea41f0e3a1" +dependencies = [ + "bytes", + "miette", + "prost", + "prost-reflect", + "prost-types", + "protox-parse", + "thiserror", +] + +[[package]] +name = "protox-parse" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "072eee358134396a4643dff81cfff1c255c9fbd3fb296be14bdb6a26f9156366" +dependencies = [ + "logos 0.15.1", + "miette", + "prost-types", + "thiserror", +] + +[[package]] +name = "quinn" +version = "0.11.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4051e23e9185c255a7e33ef59cdbca87a22d359052eecd22fc6b901fb37d9d11" +dependencies = [ + "bytes", + "cfg_aliases", + "pin-project-lite", + "quinn-proto", + "quinn-udp", + "rustc-hash", + "rustls", + "socket2", + "thiserror", + "tokio", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-proto" +version = "0.11.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e750cca55fe4f0439a15d0bb529da9651e79993e8e72c61a899a36d462befbe" +dependencies = [ + "bytes", + "getrandom 0.4.3", + "lru-slab", + "rand 0.10.3", + "rand_pcg", + "ring", + "rustc-hash", + "rustls", + "rustls-pki-types", + "slab", + "thiserror", + "tinyvec", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-udp" +version = "0.5.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af66907df18639dcf4db56ca65490cabc4b27a97dbadd96f2926cca73298f016" +dependencies = [ + "cfg_aliases", + "libc", + "once_cell", + "socket2", + "tracing", + "windows-sys 0.61.2", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + +[[package]] +name = "rand" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e058c7de0b26af77780c769414d6257830bb240f3c38477dbc2c16e5f54d6d4c" +dependencies = [ + "libc", + "rand_chacha 0.3.1", + "rand_core 0.6.4", +] + +[[package]] +name = "rand" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9ef1d0d795eb7d84685bca4f72f3649f064e6641543d3a8c415898726a57b41" +dependencies = [ + "rand_chacha 0.9.0", + "rand_core 0.9.5", +] + +[[package]] +name = "rand" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65c9fb96cbc91e3478eaae79a69fcd3f1ae4ad052e471fe6732fff548984b4af" +dependencies = [ + "chacha20", + "getrandom 0.4.3", + "rand_core 0.10.1", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core 0.9.5", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.17", +] + +[[package]] +name = "rand_core" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" +dependencies = [ + "getrandom 0.3.4", +] + +[[package]] +name = "rand_core" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" + +[[package]] +name = "rand_pcg" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caa0f4137e1c0a72f4c651489402276c8e8e1cf081f3b0ba156d2cbeef09e86a" +dependencies = [ + "rand_core 0.10.1", +] + +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags", +] + +[[package]] +name = "redox_syscall" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "737970939a87c6fa31e7acad13307bccbb017a073b695b6089a2c484f929e20e" +dependencies = [ + "bitflags", +] + +[[package]] +name = "ref-cast" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e440fb4e4b4147295338efb76001ab9e4efc0e5839df2c47fc5ac2381d365c3" +dependencies = [ + "ref-cast-impl", +] + +[[package]] +name = "ref-cast-impl" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92ecd8964f8453721699a1ed72037b0db49ce2f5a5138486ee89bed6f67cdf3a" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.7", +] + +[[package]] +name = "regex" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" + +[[package]] +name = "reqwest" +version = "0.12.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eddd3ca559203180a307f12d114c268abf583f59b03cb906fd0b3ff8646c1147" +dependencies = [ + "base64 0.22.1", + "bytes", + "futures-core", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-rustls", + "hyper-util", + "js-sys", + "log", + "percent-encoding", + "pin-project-lite", + "quinn", + "rustls", + "rustls-pki-types", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tokio-rustls", + "tokio-util", + "tower", + "tower-http", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "wasm-streams", + "web-sys", + "webpki-roots 1.0.9", +] + +[[package]] +name = "ring" +version = "0.17.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" +dependencies = [ + "cc", + "cfg-if", + "getrandom 0.2.17", + "libc", + "untrusted", + "windows-sys 0.52.0", +] + +[[package]] +name = "rsa" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8573f03f5883dcaebdfcf4725caa1ecb9c15b2ef50c43a07b816e06799bb12d" +dependencies = [ + "const-oid", + "digest 0.10.7", + "num-bigint-dig", + "num-integer", + "num-traits", + "pkcs1", + "pkcs8", + "rand_core 0.6.4", + "signature", + "spki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustc-hash" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d" + +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver", +] + +[[package]] +name = "rustix" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "891efababe418670775f199f0d233d84843c227a0949a883ce15b37c78d6629d" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.61.2", +] + +[[package]] +name = "rustls" +version = "0.23.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d41d731c7d2f962d1ccc364cec258de3c0e93b38c2fb3ba97ac74513048d634" +dependencies = [ + "once_cell", + "ring", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-pki-types" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f4925028c7eb5d1fcdaf196971378ed9d2c1c4efc7dc5d011256f76c99c0a96" +dependencies = [ + "web-time", + "zeroize", +] + +[[package]] +name = "rustls-webpki" +version = "0.103.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3c3cf1d8b1e7d4927e2d154c3fcb02979afb9939629c62cd9048d4f07b60ac2" +dependencies = [ + "ring", + "rustls-pki-types", + "untrusted", +] + +[[package]] +name = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + +[[package]] +name = "ryu" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "semver" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" + +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde-big-array" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11fc7cc2c76d73e0f27ee52abbd64eec84d46f370c88371120433196934e4b7f" +dependencies = [ + "serde", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.7", +] + +[[package]] +name = "serde_json" +version = "1.0.152" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1741ab7a6cc54a03a89b5d563ed60075c277d9e3cfa73ad0c1f23f23974703c6" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "serde_path_to_error" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a9ff822e371bb5403e391ecd83e182e0e77ba7f6fe0160b795797109d1b457" +dependencies = [ + "itoa", + "serde", + "serde_core", +] + +[[package]] +name = "serde_spanned" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7523beb55eece201a2356bee0bbca0d1ab466c14c07703b2e0ee6d42cb0c2c" +dependencies = [ + "serde_core", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sha1" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a978451301f4db1d02937a4ab3ccce137717b81826e79b7d49ffe3244a13c3b8" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "digest 0.10.7", +] + +[[package]] +name = "sha1" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aacc4cc499359472b4abe1bf11d0b12e688af9a805fa5e3016f9a386dc2d0214" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "digest 0.11.3", +] + +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "digest 0.10.7", +] + +[[package]] +name = "sha2" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "446ba717509524cb3f22f17ecc096f10f4822d76ab5c0b9822c5f9c284e825f4" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "digest 0.11.3", +] + +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + +[[package]] +name = "signal-hook-registry" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +dependencies = [ + "errno", + "libc", +] + +[[package]] +name = "signal-node" +version = "0.1.0" +dependencies = [ + "aes-gcm 0.10.3", + "anyhow", + "async-trait", + "axum", + "base64 0.22.1", + "bytes", + "futures-util", + "hex", + "hmac 0.12.1", + "prost", + "prost-build", + "protox", + "rand 0.10.3", + "rand 0.8.8", + "reqwest", + "serde", + "serde_json", + "sha2 0.10.9", + "sqlx", + "subtle", + "thiserror", + "tokio", + "tokio-stream", + "tokio-tungstenite", + "toml", + "tracing", + "tracing-subscriber", + "wacore", + "wacore-binary", + "wacore-recordstore", + "wacore-signal-ops", + "waproto", +] + +[[package]] +name = "signature" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de" +dependencies = [ + "digest 0.10.7", + "rand_core 0.6.4", +] + +[[package]] +name = "simdutf8" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" + +[[package]] +name = "siphasher" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33f4fe9184a62d842c9ef383018f3306d8ba224fd9d836f56d7288308847c256" + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "smallvec" +version = "1.16.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b3dc8af474f516a851ff4bd12db780f948b9250ad37211e4eec0bccea54e01b" +dependencies = [ + "serde", +] + +[[package]] +name = "smoothutf8" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b4ec95892483d6d94284caccfddb9b77111a4dcac33ed25d624248def0fa5f1" +dependencies = [ + "simdutf8", +] + +[[package]] +name = "socket2" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "spin" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3763264f6b73151db08c50ff20d7d8a0b8796e021cdea7ceedad07b80155fa0e" +dependencies = [ + "lock_api", +] + +[[package]] +name = "spki" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d" +dependencies = [ + "base64ct", + "der", +] + +[[package]] +name = "sqlx" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fefb893899429669dcdd979aff487bd78f4064e5e7907e4269081e0ef7d97dc" +dependencies = [ + "sqlx-core", + "sqlx-macros", + "sqlx-mysql", + "sqlx-postgres", + "sqlx-sqlite", +] + +[[package]] +name = "sqlx-core" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee6798b1838b6a0f69c007c133b8df5866302197e404e8b6ee8ed3e3a5e68dc6" +dependencies = [ + "base64 0.22.1", + "bytes", + "crc", + "crossbeam-queue", + "either", + "event-listener", + "futures-core", + "futures-intrusive", + "futures-io", + "futures-util", + "hashbrown 0.15.5", + "hashlink", + "indexmap", + "log", + "memchr", + "once_cell", + "percent-encoding", + "rustls", + "serde", + "serde_json", + "sha2 0.10.9", + "smallvec", + "thiserror", + "tokio", + "tokio-stream", + "tracing", + "url", + "webpki-roots 0.26.11", +] + +[[package]] +name = "sqlx-macros" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2d452988ccaacfbf5e0bdbc348fb91d7c8af5bee192173ac3636b5fb6e6715d" +dependencies = [ + "proc-macro2", + "quote", + "sqlx-core", + "sqlx-macros-core", + "syn 2.0.119", +] + +[[package]] +name = "sqlx-macros-core" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19a9c1841124ac5a61741f96e1d9e2ec77424bf323962dd894bdb93f37d5219b" +dependencies = [ + "dotenvy", + "either", + "heck", + "hex", + "once_cell", + "proc-macro2", + "quote", + "serde", + "serde_json", + "sha2 0.10.9", + "sqlx-core", + "sqlx-postgres", + "sqlx-sqlite", + "syn 2.0.119", + "tokio", + "url", +] + +[[package]] +name = "sqlx-mysql" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa003f0038df784eb8fecbbac13affe3da23b45194bd57dba231c8f48199c526" +dependencies = [ + "atoi", + "base64 0.22.1", + "bitflags", + "byteorder", + "bytes", + "crc", + "digest 0.10.7", + "dotenvy", + "either", + "futures-channel", + "futures-core", + "futures-io", + "futures-util", + "generic-array", + "hex", + "hkdf 0.12.4", + "hmac 0.12.1", + "itoa", + "log", + "md-5", + "memchr", + "once_cell", + "percent-encoding", + "rand 0.8.8", + "rsa", + "sha1 0.10.7", + "sha2 0.10.9", + "smallvec", + "sqlx-core", + "stringprep", + "thiserror", + "tracing", + "whoami", +] + +[[package]] +name = "sqlx-postgres" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db58fcd5a53cf07c184b154801ff91347e4c30d17a3562a635ff028ad5deda46" +dependencies = [ + "atoi", + "base64 0.22.1", + "bitflags", + "byteorder", + "crc", + "dotenvy", + "etcetera", + "futures-channel", + "futures-core", + "futures-util", + "hex", + "hkdf 0.12.4", + "hmac 0.12.1", + "home", + "itoa", + "log", + "md-5", + "memchr", + "once_cell", + "rand 0.8.8", + "serde", + "serde_json", + "sha2 0.10.9", + "smallvec", + "sqlx-core", + "stringprep", + "thiserror", + "tracing", + "whoami", +] + +[[package]] +name = "sqlx-sqlite" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2d12fe70b2c1b4401038055f90f151b78208de1f9f89a7dbfd41587a10c3eea" +dependencies = [ + "atoi", + "flume", + "futures-channel", + "futures-core", + "futures-executor", + "futures-intrusive", + "futures-util", + "libsqlite3-sys", + "log", + "percent-encoding", + "serde", + "serde_urlencoded", + "sqlx-core", + "thiserror", + "tracing", + "url", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + +[[package]] +name = "stringprep" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b4df3d392d81bd458a8a621b8bffbd2302a12ffe288a9d931670948749463b1" +dependencies = [ + "unicode-bidi", + "unicode-normalization", + "unicode-properties", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "3.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d62a2e0561533f2ca2561d0cf27fd9fedb640a1bf2616ff5d5c80d99017faadc" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" +dependencies = [ + "futures-core", +] + +[[package]] +name = "synstructure" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "901704edd0dfe137f1987838ee4f259e4e063c31371bdb423f7ae38ec6f77f02" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.7", +] + +[[package]] +name = "tempfile" +version = "3.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" +dependencies = [ + "fastrand", + "getrandom 0.4.3", + "once_cell", + "rustix", + "windows-sys 0.61.2", +] + +[[package]] +name = "thiserror" +version = "2.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09e52cb86a36cede5cb101bf8908837b3e4c6e5e59fe7fd85c23fb56200d189e" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe5197923287db20a58125f0bc85c062f7f2c892de97b18c356f9efb14b28524" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.7", +] + +[[package]] +name = "thread_local" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad99c4c6d32803332c548b1af0540b357b3f5fc0be8f6c6bfe8b2e6ae784070" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "tinystr" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1e27c91459209c2986af3dcf603a5a74a4368754ce37414f59acc971167f643" +dependencies = [ + "displaydoc", + "zerovec", +] + +[[package]] +name = "tinyvec" +version = "1.13.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd3ca314f692efd6c868f8408f53fe444634a845f96c028b97d35f6a1f79f0ee" + +[[package]] +name = "tokio" +version = "1.53.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e95f91fcc7a621e8b030f6aa23c71fe9838ae2fb4d8118b75602a328f5144044" +dependencies = [ + "bytes", + "libc", + "mio", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys 0.61.2", +] + +[[package]] +name = "tokio-macros" +version = "2.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.7", +] + +[[package]] +name = "tokio-rustls" +version = "0.26.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9cc2678c2cdd569ef8215e2afd7954ada2ae20b4fdd2c5fe6139a3b02d105db" +dependencies = [ + "rustls", + "tokio", +] + +[[package]] +name = "tokio-stream" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3d06f0b082ba57c26b79407372e57cf2a1e28124f78e9479fe80322cf53420b" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", + "tokio-util", +] + +[[package]] +name = "tokio-tungstenite" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "489a59b6730eda1b0171fcfda8b121f4bee2b35cba8645ca35c5f7ba3eb736c1" +dependencies = [ + "futures-util", + "log", + "rustls", + "rustls-pki-types", + "tokio", + "tokio-rustls", + "tungstenite", + "webpki-roots 0.26.11", +] + +[[package]] +name = "tokio-util" +version = "0.7.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e464cf451ba96ebfc6f9b6542f17ee8b8956e33f1e40d9690624e59d7a7f8a4b" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "toml" +version = "0.9.12+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf92845e79fc2e2def6a5d828f0801e29a2f8acc037becc5ab08595c7d5e9863" +dependencies = [ + "indexmap", + "serde_core", + "serde_spanned", + "toml_datetime", + "toml_parser", + "toml_writer", + "winnow 0.7.15", +] + +[[package]] +name = "toml_datetime" +version = "0.7.5+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92e1cfed4a3038bc5a127e35a2d360f145e1f4b971b551a2ba5fd7aedf7e1347" +dependencies = [ + "serde_core", +] + +[[package]] +name = "toml_parser" +version = "1.1.5+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baa693a8032d7e1cada7d0041e96126df243179ff061456783ac7f12bda4744c" +dependencies = [ + "winnow 1.0.4", +] + +[[package]] +name = "toml_writer" +version = "1.1.3+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06bdbd8cfc056b8d2e2e85f29b56a3bdbecb527cef81eb39e3e7b98af4652770" + +[[package]] +name = "tower" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper", + "tokio", + "tower-layer", + "tower-service", +] + +[[package]] +name = "tower-http" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cfcf7e2740e6fc6d4d688b4ef00650406bb94adf4731e43c096c3a19fe40840" +dependencies = [ + "bitflags", + "bytes", + "futures-util", + "http", + "http-body", + "pin-project-lite", + "tower", + "tower-layer", + "tower-service", + "url", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "log", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-serde" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "704b1aeb7be0d0a84fc9828cae51dab5970fee5088f83d1dd7ee6f6246fc6ff1" +dependencies = [ + "serde", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex-automata", + "serde", + "serde_json", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", + "tracing-serde", +] + +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + +[[package]] +name = "tungstenite" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eadc29d668c91fcc564941132e17b28a7ceb2f3ebf0b9dae3e03fd7a6748eb0d" +dependencies = [ + "bytes", + "data-encoding", + "http", + "httparse", + "log", + "rand 0.9.5", + "rustls", + "rustls-pki-types", + "sha1 0.10.7", + "thiserror", + "utf-8", +] + +[[package]] +name = "typenum" +version = "1.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" + +[[package]] +name = "unicode-bidi" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c1cb5db39152898a79168971543b1cb5020dff7fe43c8dc468b0885f5e29df5" + +[[package]] +name = "unicode-ident" +version = "1.0.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d245f478577f809a851594d02313b640fb437e0bb33866753cff937863096954" + +[[package]] +name = "unicode-normalization" +version = "0.1.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fd4f6878c9cb28d874b009da9e8d183b5abc80117c40bbd187a1fde336be6e8" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "unicode-properties" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7df058c713841ad818f1dc5d3fd88063241cc61f49f5fbea4b951e8cf5a8d71d" + +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" + +[[package]] +name = "universal-hash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea" +dependencies = [ + "crypto-common 0.1.7", + "subtle", +] + +[[package]] +name = "universal-hash" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4987bdc12753382e0bec4a65c50738ffaabc998b9cdd1f952fb5f39b0048a96" +dependencies = [ + "crypto-common 0.2.2", + "ctutils", +] + +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "url" +version = "2.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + +[[package]] +name = "utf-8" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" + +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + +[[package]] +name = "valuable" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "wacore" +version = "0.7.0" +source = "git+https://github.com/oxidezap/whatsapp-rust?rev=2671430db6161c01c507587d0e3c90dcf153aa85#2671430db6161c01c507587d0e3c90dcf153aa85" +dependencies = [ + "aes 0.9.3", + "anyhow", + "async-channel", + "async-lock", + "async-trait", + "base64 0.23.1", + "bon", + "buffa", + "buffa-build", + "bytes", + "cbc", + "chrono", + "compact_str", + "ctr 0.10.1", + "event-listener", + "fluent-uri", + "futures", + "hashbrown 0.17.1", + "hex", + "hkdf 0.13.0", + "hmac 0.13.0", + "itoa", + "log", + "portable-atomic", + "rand 0.10.3", + "serde", + "serde-big-array", + "serde_json", + "sha1 0.11.0", + "sha2 0.11.0", + "smallvec", + "smoothutf8", + "subtle", + "thiserror", + "wacore-appstate", + "wacore-binary", + "wacore-derive", + "wacore-libsignal", + "wacore-noise", + "waproto", + "zeroize", +] + +[[package]] +name = "wacore-appstate" +version = "0.7.0" +source = "git+https://github.com/oxidezap/whatsapp-rust?rev=2671430db6161c01c507587d0e3c90dcf153aa85#2671430db6161c01c507587d0e3c90dcf153aa85" +dependencies = [ + "anyhow", + "buffa", + "hex", + "hkdf 0.13.0", + "hmac 0.13.0", + "log", + "serde", + "serde-big-array", + "serde_json", + "sha2 0.11.0", + "subtle", + "thiserror", + "wacore-binary", + "wacore-libsignal", + "waproto", +] + +[[package]] +name = "wacore-binary" +version = "0.7.0" +source = "git+https://github.com/oxidezap/whatsapp-rust?rev=2671430db6161c01c507587d0e3c90dcf153aa85#2671430db6161c01c507587d0e3c90dcf153aa85" +dependencies = [ + "bytes", + "compact_str", + "getrandom 0.4.3", + "itoa", + "serde", + "serde_json", + "siphasher", + "smallvec", + "smoothutf8", + "stable_deref_trait", + "yoke", + "zlib-rs", +] + +[[package]] +name = "wacore-derive" +version = "0.7.0" +source = "git+https://github.com/oxidezap/whatsapp-rust?rev=2671430db6161c01c507587d0e3c90dcf153aa85#2671430db6161c01c507587d0e3c90dcf153aa85" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.7", +] + +[[package]] +name = "wacore-libsignal" +version = "0.7.0" +source = "git+https://github.com/oxidezap/whatsapp-rust?rev=2671430db6161c01c507587d0e3c90dcf153aa85#2671430db6161c01c507587d0e3c90dcf153aa85" +dependencies = [ + "aes 0.9.3", + "aes-gcm 0.11.1", + "arrayvec", + "async-lock", + "async-trait", + "buffa", + "bytes", + "cbc", + "chrono", + "ctr 0.10.1", + "hex", + "hkdf 0.13.0", + "hmac 0.13.0", + "itoa", + "log", + "portable-atomic", + "rand 0.10.3", + "serde", + "sha2 0.11.0", + "subtle", + "thiserror", + "wacore-binary", + "waproto", + "zeroize", +] + +[[package]] +name = "wacore-noise" +version = "0.7.0" +source = "git+https://github.com/oxidezap/whatsapp-rust?rev=2671430db6161c01c507587d0e3c90dcf153aa85#2671430db6161c01c507587d0e3c90dcf153aa85" +dependencies = [ + "anyhow", + "buffa", + "bytes", + "hkdf 0.13.0", + "log", + "rand 0.10.3", + "sha2 0.11.0", + "thiserror", + "wacore-binary", + "wacore-libsignal", + "waproto", +] + +[[package]] +name = "wacore-recordstore" +version = "0.1.0" +dependencies = [ + "aes-gcm 0.11.1", + "async-lock", + "async-trait", + "bytes", + "futures", + "rand 0.10.3", + "thiserror", + "wacore", +] + +[[package]] +name = "wacore-signal-ops" +version = "0.1.0" +dependencies = [ + "anyhow", + "async-lock", + "async-trait", + "buffa", + "bytes", + "futures", + "hex", + "hmac 0.13.0", + "log", + "rand 0.10.3", + "serde", + "sha2 0.11.0", + "thiserror", + "tokio", + "wacore", + "wacore-binary", + "wacore-recordstore", + "waproto", +] + +[[package]] +name = "want" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec4cdd0dd910afe868b7ef477227d8d538b46b3075031afee8a9f2acb0a2ed0b" +dependencies = [ + "try-lock", +] + +[[package]] +name = "waproto" +version = "0.7.0" +source = "git+https://github.com/oxidezap/whatsapp-rust?rev=2671430db6161c01c507587d0e3c90dcf153aa85#2671430db6161c01c507587d0e3c90dcf153aa85" +dependencies = [ + "buffa", + "buffa-build", + "buffa-descriptor", + "bytes", + "heck", + "prettyplease 0.3.0", + "quote", + "serde", + "sha2 0.11.0", + "syn 3.0.7", +] + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "wasip2" +version = "1.0.4+wasi-0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b67efb37e106e55ce722a510d6b5f9c17f083e5fc79afc2badeb12cc313d9487" +dependencies = [ + "wit-bindgen", +] + +[[package]] +name = "wasite" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b" + +[[package]] +name = "wasm-bindgen" +version = "0.2.129" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9bb54f33acc68fd454578d9820b0bde1a1a3d17aa17bb7b6595806d02886d409" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3cbab34de2d982e9b48e18d216d04c4a6f641066ff19ffb699980f591ee3610e" +dependencies = [ + "js-sys", + "tokio", + "wasm-bindgen", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.129" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e29d0c35b16e224a7eeb5cd2d25e3e1968fbd65604117b44d3b789d00ee8535" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.129" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f501a8bc3719dba86ef8ae4728879c08001bea749eb1333ac5b91e040e2a6b7" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn 3.0.7", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.129" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23f0c9c52aa7cd7d77769a4cfe2a9adb1b331f489a41d912ce14513d5ab995c6" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "wasm-streams" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15053d8d85c7eccdbefef60f06769760a563c7f0a9d6902a13d35c7800b0ad65" +dependencies = [ + "futures-util", + "js-sys", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + +[[package]] +name = "web-sys" +version = "0.3.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88261b9deccee56594c11a3460c462c41f58d148598fe70ad77070126a68aba4" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "web-time" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "webpki-roots" +version = "0.26.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "521bc38abb08001b01866da9f51eb7c5d647a19260e00054a8c7fd5f9e57f7a9" +dependencies = [ + "webpki-roots 1.0.9", +] + +[[package]] +name = "webpki-roots" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dcd9d09a39985f5344844e66b0c530a33843579125f23e21e9f0f220850f22a" +dependencies = [ + "rustls-pki-types", +] + +[[package]] +name = "whoami" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d4a4db5077702ca3015d3d02d74974948aba2ad9e12ab7df718ee64ccd7e97d" +dependencies = [ + "libredox", + "wasite", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "winnow" +version = "0.7.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df79d97927682d2fd8adb29682d1140b343be4ac0f08fd68b7765d9c059d3945" + +[[package]] +name = "winnow" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b97319f7b8343df12cc98938e5c3eb436064524c8d2b4e30a1d3a36eecdf81" + +[[package]] +name = "wit-bindgen" +version = "0.57.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" + +[[package]] +name = "writeable" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc" + +[[package]] +name = "yoke" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "709fe23a0424b6a435d82152b1bd3fdfb0833487d5fa90d05d42762a9891fef5" +dependencies = [ + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec8ebde2db3681e8c9980cc27822030e68752690ddfa9473e739aeb4dbde6d71" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.7", + "synstructure", +] + +[[package]] +name = "zerocopy" +version = "0.8.62" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86502bf56ac7c77571a32e2647bb2a15894565e981fb2a48d7bde2d91c965a9d" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.62" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5457206954b06561e2608c7e19cf58b1926586d999c246eebe4502f7e2039d1a" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "zerofrom" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ec05a11813ea801ff6d75110ad09cd0824ddba17dfe17128ea0d5f68e6c5272" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f75b4683f6c7f45248d4d64056a24298c6281e0993356d7d1b4a1a962ef10d4a" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.7", + "synstructure", +] + +[[package]] +name = "zeroize" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13084392c5e4bc371903e2935a5eaeed24905a7511356b883835e18a78f6879" + +[[package]] +name = "zerotrie" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ea269c3bd32f0a32c321907a2ae912ba6f4649bb0fc764a15627e99a7095a3f" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", +] + +[[package]] +name = "zerovec" +version = "0.11.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb0464e17806c1d976d5cba29399c7f08e516e279e2ba493f63123b5fca67dd8" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.11.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34df6fc39dbd26ddc9c10e6a2984476e13acce22e64e4487636ef494369225da" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.7", +] + +[[package]] +name = "zlib-rs" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b268e58e7c693d7c271f93ffc4ba3b380412554231c85bf61ca7af91042a4112" + +[[package]] +name = "zmij" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..ce3fceb --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,89 @@ +[package] +name = "signal-node" +version = "0.1.0" +edition = "2024" +rust-version.workspace = true +license = "AGPL-3.0-only" +description = "Self-hosted Signal service for Polymorfa zero-knowledge connections" +repository = "https://github.com/polymorfa/signal-node" +publish = false + +[lib] +name = "signal_node" +path = "src/lib.rs" + +[[bin]] +name = "signal-node" +path = "src/main.rs" + +[dependencies] +# Signal core from oxidezap/whatsapp-rust, plus this service's own Signal +# operations and sealed record store, which build on its public API. +wacore = { workspace = true } +wacore-binary = { workspace = true } +wacore-recordstore = { path = "crates/recordstore" } +wacore-signal-ops = { path = "crates/signal-ops" } +waproto = { workspace = true } + +aes-gcm = "0.10" +anyhow = "1" +async-trait = "0.1" +axum = { version = "0.8", default-features = false, features = ["http1", "json", "tokio", "query"] } +base64 = "0.22" +bytes = "1" +futures-util = { version = "0.3", default-features = false, features = ["sink", "std"] } +hex = "0.4" +hmac = "0.12" +prost = "0.14" +rand = "0.8" +reqwest = { version = "0.12", default-features = false, features = ["rustls-tls", "json"] } +serde = { version = "1", features = ["derive"] } +serde_json = "1" +sha2 = "0.10" +sqlx = { version = "0.8", default-features = false, features = ["runtime-tokio", "tls-rustls", "postgres", "sqlite", "migrate", "macros"] } +subtle = "2.6" +thiserror = "2" +tokio = { version = "1", features = ["macros", "rt-multi-thread", "sync", "time", "signal", "net"] } +tokio-stream = { version = "0.1", features = ["sync"] } +tokio-tungstenite = { version = "0.27", default-features = false, features = ["connect", "rustls-tls-webpki-roots"] } +toml = "0.9" +tracing = "0.1" +tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] } + +[build-dependencies] +prost-build = "0.14" +protox = "0.9" + +[dev-dependencies] +tokio = { version = "1", features = ["test-util"] } +wacore = { workspace = true, features = ["test-util"] } +rand10 = { package = "rand", version = "0.10" } +wacore-recordstore = { path = "crates/recordstore", features = ["conformance"] } +reqwest = { version = "0.12", default-features = false, features = ["rustls-tls", "json", "stream"] } + +[workspace] +resolver = "3" +members = [".", "crates/recordstore", "crates/signal-ops"] + +[workspace.package] +rust-version = "1.95" + +[workspace.dependencies] +wacore = { git = "https://github.com/oxidezap/whatsapp-rust", rev = "2671430db6161c01c507587d0e3c90dcf153aa85" } +wacore-binary = { git = "https://github.com/oxidezap/whatsapp-rust", rev = "2671430db6161c01c507587d0e3c90dcf153aa85" } +waproto = { git = "https://github.com/oxidezap/whatsapp-rust", rev = "2671430db6161c01c507587d0e3c90dcf153aa85" } +aes-gcm = { version = "0.11.0", default-features = false, features = ["aes", "alloc"] } +anyhow = { version = "1.0", default-features = false } +async-lock = { version = "3", default-features = false } +async-trait = "0.1.91" +buffa = { version = "0.9.2", default-features = false, features = ["json"] } +bytes = { version = "1.12", default-features = false } +futures = { version = "0.3", default-features = false, features = ["alloc", "async-await"] } +hex = { version = "0.4", default-features = false, features = ["alloc"] } +hmac = { version = "0.13.0", default-features = false } +rand = "0.10" +sha2 = { version = "0.11.0", default-features = false } +thiserror = "2.0.19" +tokio = { version = "1.53.1", default-features = false } +log = "0.4" +serde = { version = "1.0", features = ["derive", "rc"] } diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a419eac --- /dev/null +++ b/Dockerfile @@ -0,0 +1,24 @@ +FROM rust:1.95.0-bookworm AS build +WORKDIR /source +COPY Cargo.toml Cargo.lock build.rs ./ +COPY proto ./proto +COPY migrations ./migrations +COPY src ./src +COPY crates ./crates +RUN cargo build --release --locked + +FROM debian:bookworm-slim AS runtime +RUN apt-get update && apt-get install --no-install-recommends -y ca-certificates \ + && rm -rf /var/lib/apt/lists/* \ + && groupadd --gid 10001 signal-node \ + && useradd --uid 10001 --gid 10001 --no-create-home --shell /usr/sbin/nologin signal-node \ + && mkdir -p /var/lib/signal-node /etc/signal-node /run/secrets \ + && chown 10001:10001 /var/lib/signal-node +COPY --from=build /source/target/release/signal-node /usr/local/bin/signal-node +COPY LICENSE /usr/share/licenses/signal-node/ +USER 10001:10001 +WORKDIR /var/lib/signal-node +EXPOSE 8787 +STOPSIGNAL SIGTERM +ENTRYPOINT ["/usr/local/bin/signal-node"] +CMD ["--config", "/etc/signal-node/signal-node.toml"] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..be3f7b2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,661 @@ + GNU AFFERO GENERAL PUBLIC LICENSE + Version 3, 19 November 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU Affero General Public License is a free, copyleft license for +software and other kinds of works, specifically designed to ensure +cooperation with the community in the case of network server software. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +our General Public Licenses are intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + Developers that use our General Public Licenses protect your rights +with two steps: (1) assert copyright on the software, and (2) offer +you this License which gives you legal permission to copy, distribute +and/or modify the software. + + A secondary benefit of defending all users' freedom is that +improvements made in alternate versions of the program, if they +receive widespread use, become available for other developers to +incorporate. Many developers of free software are heartened and +encouraged by the resulting cooperation. However, in the case of +software used on network servers, this result may fail to come about. +The GNU General Public License permits making a modified version and +letting the public access it on a server without ever releasing its +source code to the public. + + The GNU Affero General Public License is designed specifically to +ensure that, in such cases, the modified source code becomes available +to the community. It requires the operator of a network server to +provide the source code of the modified version running there to the +users of that server. Therefore, public use of a modified version, on +a publicly accessible server, gives the public access to the source +code of the modified version. + + An older license, called the Affero General Public License and +published by Affero, was designed to accomplish similar goals. This is +a different license, not a version of the Affero GPL, but Affero has +released a new version of the Affero GPL which permits relicensing under +this license. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU Affero General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Remote Network Interaction; Use with the GNU General Public License. + + Notwithstanding any other provision of this License, if you modify the +Program, your modified version must prominently offer all users +interacting with it remotely through a computer network (if your version +supports such interaction) an opportunity to receive the Corresponding +Source of your version by providing access to the Corresponding Source +from a network server at no charge, through some standard or customary +means of facilitating copying of software. This Corresponding Source +shall include the Corresponding Source for any work covered by version 3 +of the GNU General Public License that is incorporated pursuant to the +following paragraph. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the work with which it is combined will remain governed by version +3 of the GNU General Public License. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU Affero General Public License from time to time. Such new versions +will be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU Affero General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU Affero General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU Affero General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If your software can interact with users remotely through a computer +network, you should also make sure that it provides a way for users to +get its source. For example, if your program is a web application, its +interface could display a "Source" link that leads users to an archive +of the code. There are many ways you could offer source, and different +solutions will be better for different programs; see section 13 for the +specific requirements. + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU AGPL, see +. diff --git a/README.md b/README.md index c08c7f5..1508e8d 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,164 @@ # signal-node -Self-hosted Signal service for Polymorfa zero-knowledge sessions. It holds a -linked WhatsApp device's Signal keys, sessions and message plaintext, and -delivers messages to your application. Polymorfa keeps the WhatsApp -connection and never sees message content. +`signal-node` is the customer-operated Signal service for Polymorfa zero-knowledge connections. It owns the linked device's keys, decrypts incoming messages in the customer's process, and delivers them directly to the customer's server application. Its outbound WebSocket carries encrypted stanzas and protocol operations to Polymorfa. The application sends messages through this service's local API. -The runner contract is `proto/polymorfa/signal/v1/signal_service.proto`. +This repository is private. The implementation is under review and has not been published or accepted against a real WhatsApp account. Deployment also requires the corresponding Polymorfa runner/API support and connection eligibility. Starting this binary does not enable zero-knowledge connections or grant permissions. + +## Contracts and dependency status + +- Runner protocol: [`proto/polymorfa/signal/v1/signal_service.proto`](proto/polymorfa/signal/v1/signal_service.proto), version `1`, subprotocol `polymorfa.signal.v1`, originating at `b6d5f5f`. +- Application API: [`docs/app-api.openapi.json`](docs/app-api.openapi.json). JSON field names use snake_case. Event and message timestamps use Unix seconds; lease expiry uses Unix milliseconds internally. +- Signal libraries: `wacore`, `wacore-binary` and `waproto` use exact `oxidezap/whatsapp-rust` revision `2671430db6161c01c507587d0e3c90dcf153aa85`. `wacore-recordstore` and `wacore-signal-ops` are workspace crates under `crates/`. A successful local build does not establish published dependencies or live-provider acceptance. +- Rust MSRV: `1.95`. The checked-in toolchain selects `1.95.0`. + +The server connection uses a project credential in `Authorization`, never in a URL or browser. The local API uses a separate application bearer secret. It rejects every authenticated request with an `Origin` header, including `Origin: null`, and supplies no CORS permission. Browser/WASM remote transport is unavailable until Polymorfa provides a distinct, short-lived client grant backed by live authorization. Never put the project credential or application secret in a browser. + +## Durable public prekey uploads + +The additive runner operations `PreparePreKeyUpload` and `AcknowledgePreKeyUpload` retain protocol version `1`. Prepare accepts 1..812 and returns the entire surviving pending batch under one opaque token, including after a process or database reopen. A later request for 50 can return the outstanding 812 keys; it never truncates or tops up that batch. Only public keys, IDs and the token cross the runner protocol. Counter, private keys and pending metadata commit atomically in the customer store. Keep one `SignalOps` instance per held connection scope, as the service does. + +The runner acknowledges only after its upload succeeds, using that token and unique original IDs covering every surviving key. Consumed originals may remain in an in-flight ACK; they stay deleted. Flags, the exact replay receipt and pending retirement commit together. The most recent exact ACK can be repeated after a lost response or restart without changing a newer pending batch. A timeout does not establish whether WhatsApp accepted an upload. Fresh retry-receipt keys still use `GeneratePreKeys` and `MarkPreKeysUploaded`; the legacy mark rejects the entire request if it includes an original pending-batch member, including a consumed member. Uploaded counts describe surviving local flags, not WhatsApp's remaining pool. + +Upgrade the supporting service before a runner requires these operations. An older service rejects an unknown operation with `CODE_UNSUPPORTED`; the runner must fail without falling back to fresh generation. The matching HyperMeow/backend consumers remain separate unmerged dependencies. This library/service checkpoint does not establish live-provider retry acceptance or change application HTTP routes, consent, custody, eligibility or deployment availability. Rollback must retain the sealed device, private-key and batch records; do not run an older writer over active pending batches without verifying compatible batch-aware behavior. + +## Run + +Build the binary with Rust 1.95 or newer: + +```sh +cargo build --release --locked +./target/release/signal-node --version +./target/release/signal-node --config /etc/signal-node/signal-node.toml +``` + +Copy [`signal-node.example.toml`](signal-node.example.toml) to a private configuration directory. Set the actual runner URL and eligible connection IDs. Give every node a unique `node_id`. Place the project credential, application secret, master key and optional webhook secret in separately mounted secret files. Secret file paths resolve against the process working directory; use absolute paths in deployments. Keep the master key and its recovery copy outside the database. + +The operator can generate a random master key with `signal-node generate-key`, redirecting its output directly to a private secret file. The value is base64 of 32 random bytes. Do not print it in logs, paste it into a support ticket, or commit it. Application secrets require at least 16 visible ASCII characters without spaces; webhook secrets require at least 16 characters; provision independent random secrets. + +Environment variables override inline file settings. An inline override and a configured `*_file` for the same secret are rejected, so remove the file setting before switching sources. Supported overrides are `SIGNAL_NODE_ID`, `SIGNAL_NODE_POLYMORFA_URL`, `SIGNAL_NODE_POLYMORFA_CREDENTIAL`, `SIGNAL_NODE_CONNECTIONS` (comma-separated), `SIGNAL_NODE_STORAGE_DRIVER`, `SIGNAL_NODE_STORAGE_URL`, `SIGNAL_NODE_MASTER_KEY`, `SIGNAL_NODE_HTTP_LISTEN`, `SIGNAL_NODE_HTTP_SECRET`, `SIGNAL_NODE_WEBHOOK_URL`, `SIGNAL_NODE_WEBHOOK_SECRET`, `SIGNAL_NODE_HISTORY_DELIVER` and `SIGNAL_NODE_APP_STATE_SHARE_KEYS`. `SIGNAL_NODE_CONFIG` selects the file when `--config` is absent. `SIGNAL_NODE_LOG_FORMAT=json` selects structured logs; `SIGNAL_NODE_LOG` sets the log filter. Avoid verbose dependency logging in production. + +The runner URL requires `wss`; loopback `ws` is available for local testing. It cannot contain URL credentials, a query or a fragment. Webhooks require HTTPS, with loopback HTTP allowed for tests. The service does not follow webhook redirects or forward either bearer credential to the webhook. + +### Container + +```sh +docker build -t signal-node:local . +docker run --rm --read-only --cap-drop ALL --security-opt no-new-privileges \ + --mount type=bind,src="$PWD/deployment-config",dst=/etc/signal-node,readonly \ + --mount type=bind,src="$PWD/deployment-secrets",dst=/run/secrets,readonly \ + --mount type=bind,src="$PWD/deployment-state",dst=/var/lib/signal-node \ + -p 127.0.0.1:8787:8787 \ + signal-node:local --config /etc/signal-node/signal-node.toml +``` + +The image runs as UID/GID `10001`. Grant that identity read access to secret/config files and write access to the state directory. Use `http.listen = "0.0.0.0:8787"` inside the container, while binding the host port to loopback or a private network. The HTTP API carries plaintext and descriptors with media keys; use authenticated TLS termination if it crosses a host boundary. The default binary listener is `127.0.0.1:8787`. `/healthz` is public and returns only `{"status":"ok"}`. It reports process liveness, not upstream connectivity or connection readiness. Authenticated `/v1/connections` supplies local ownership and claim state. + +SIGINT/SIGTERM stop renewal and release held leases. HTTP shutdown waits for open requests and SSE streams, then aborts remaining tasks after a ten-second deadline. Unreleased leases expire after a forced stop; configure the supervisor to allow at least that shutdown period. + +## Application API + +All `/v1/*` requests require `Authorization: Bearer `. The secret authorizes every configured local connection; deploy separate services/secrets for applications that must not share that authority. Do not pass a Polymorfa project credential to these endpoints. + +| Method | Path | Result | +| --- | --- | --- | +| GET | `/v1/connections` | Node ID and locally held connections, each with `id`, `lease_generation`, `claimed`, `has_device_keys`, `state`, nullable `pn` and `lid` | +| POST | `/v1/connections/{id}/messages` | Send text or one encrypted-media descriptor; returns `message_id`, `server_timestamp`, `resent` | +| GET | `/v1/events?connection_id={id}` | SSE stream of decrypted `message` events and explicitly enabled `history.sync` descriptors | +| POST | `/v1/events/{id}/ack` | `204` for a pending event; `404` when no delivery awaits that ID | + +A text send has this body: + +```json +{"to":"15551234567@s.whatsapp.net","text":"Hello"} +``` + +Supply exactly one of `text` or `media`. A media send provides an already encrypted/uploaded descriptor; the service does not upload plaintext or fetch media. The OpenAPI schema defines image, video, audio and document descriptors, required hashes/key/path/size fields, and optional metadata. An unknown configured connection returns `404`; an unowned or unpaired connection returns `409`; disconnected/storage-unavailable operations return `503`; runner rate refusal returns `429`. Local pacing queues sends rather than rejecting them solely because the five-per-second window is full. Invalid input returns `400`. Error envelopes use `{"error":{"code":"...","message":"..."}}`. + +If a queued send loses its reply through timeout or connection loss, the service returns `504 send_unconfirmed` with `error.message_id`. This also applies when a participant-hash mismatch acknowledgment requires a resend and that resend fails, even before the second stanza is queued. The first stanza may already have reached some participants. The ordinary `503` response applies only before a send acknowledgment. Retain the ID and reconcile the outcome before sending again. Do not automatically retry: a new POST generates another message ID and can duplicate delivery. The returned ID is neither an idempotency key nor an application-API status lookup. + +A sender-key distribution write failure preserves the acknowledgment and permits the one same-ID mismatch resend. Without a mismatch, the confirmed send returns `200`. Fence loss retires the old connection and returns the confirmed result without a resend. + +Events contain a stable `id`, `type`, `connection_id`, routing metadata, `message_id`, `timestamp`, `is_from_me`, `redelivered`, `identity_changed` and `message_proto`. Optional fields include `sender_alt`, `text`, `media` and `history`. `message_proto` is a base64 WhatsApp message protobuf for ordinary messages. History events use an empty `message_proto` and expose only the history notification descriptor (including any inline payload already contained in that notification). Descriptor keys are sensitive application data. + +### Acknowledgment and replay + +A receive that produces an application event succeeds only after one application acknowledgment. With both channels enabled, the first successful webhook or SSE acknowledgment wins. + +- Webhook: any `2xx` response acknowledges the event. Persist/deduplicate the event before responding. Non-`2xx`, redirects, timeouts and connection failures do not acknowledge it. +- SSE: persist/deduplicate the event, then POST its `id` to the ACK route. Multiple subscribers receive the same event; one acknowledgment completes all pending receives for that ID. IDs are unique per connection: two connections that receive the same group message get different IDs. Keep authorization headers on the stream and ACK request. + +The webhook signs the exact request body bytes. Headers are `Signal-Node-Timestamp` (Unix seconds), `Signal-Node-Event-Id`, and `Signal-Node-Signature: v1=`. The signed bytes are `timestamp + "." + rawBody`. Verify the MAC in constant time and reject timestamps outside a bounded window, such as five minutes, before parsing or processing. The Rust helpers `delivery::verify` and `delivery::verify_recent` implement the MAC and timestamp checks. Deduplicate by event ID in durable application storage; a valid signature alone does not prevent replay. + +Only then does the service call `mark_delivered` on its sealed decrypt-buffer record. The runner receives success after that write succeeds. If application acknowledgment or the final write fails, the same ciphertext can replay from the buffer with `redelivered = true`. Application processing and this write cannot form one distributed transaction, so delivery is at least once. Provider redelivery behavior still requires real-account acceptance. + +An enabled own-device app-state key share is sealed separately before that delivery marker. Retrying the ciphertext after a lost runner reply, including after restart, returns the same share with `already_delivered = true` during the decrypt-buffer retention window. Every retry still requires current sharing consent and own-device provenance. This replay record contains only the key share, not application plaintext, and expires without extending its original retention window on retries. + +Protocol-only messages such as sender-key distributions and app-state shares do not produce application events; the Signal core processes them locally and marks them consumed without an application ACK. Disabled or foreign-device history notifications also produce no application event. The service never sends decrypted message content in the runner receive response. The service returns app-state key shares to the runner only after explicit operator opt-in and only from the account's own devices. Mixed protocol/text or media messages preserve ordinary application content; their application protobuf omits protocol fields, and history metadata is included only with consent. + +SSE uses one shared buffer of at most 1,024 events per connection filter. All subscribers with the same filter share that buffer. A lagging subscriber can miss an event, but it cannot acknowledge an event it never processes. There is no SSE cursor replay or `Last-Event-ID` recovery. Reconnect the stream and rely on runner ciphertext retry plus the sealed buffer for unacknowledged messages. With no webhook and no stream subscriber, receive fails immediately instead of silently dropping an application message. The default acknowledgment timeout is ten seconds. + +## App-state key sharing consent + +`app_state.share_keys` defaults to `false`. Set it to `true`, or set +`SIGNAL_NODE_APP_STATE_SHARE_KEYS=true`, only after accepting the metadata disclosure. +The environment override accepts `1`, `true`, `yes`, `0`, `false`, or `no`; +other values refuse startup. Keep the setting consistent across deployment replicas. +This consent is independent of `history.deliver`: neither setting enables the other. + +When enabled, the service forwards app-state keys received from the account's own +devices to the runner. Foreign-device shares remain excluded. These keys allow the +runner to decrypt synchronized metadata, including contact names, labels and +mute/archive/pin state. Message-plaintext custody does not imply metadata opacity; +Polymorfa can also see operational metadata such as blocklists. Disabling the +setting stops future key forwarding; it cannot recall keys or metadata already +shared. This operator setting grants no connection eligibility or hosted-history access. + +Shipped-client evidence is Cellar bundle `whatsapp-1049950004`, +`WAWebSyncdDecryptMutations.js:24-35` (key-based mutation decryption) and +`WAWebProtobufSyncAction.pb.js:459-508,1268-1304` (contact, label and chat-state +fields). This evidence does not establish live-provider acceptance or prove that +blocklists use those app-state keys. + +## History consent and content limits + +`history.deliver` defaults to `false`. Enabling it is an explicit deployment-level consent decision by the customer operator; it does not grant Polymorfa connection eligibility, hosted-history storage, or a backend entitlement. Even with consent, only notifications marked `is_from_me` are delivered. The service forwards descriptors and optional inline notification payloads. It never downloads, inflates, stores or MMS-deletes history chunks. The application SDK owns those actions and its own retention decision. + +Zero-knowledge connections do not use hosted message history or content-dependent Campaigns. Polymorfa still sees routing and operational metadata required for transport. The customer service and application see plaintext. A compromised customer host or application bearer can expose it. This service does not remove the need to protect the customer runtime, backups and application database. + +Polymorfa holds the WhatsApp connection, so this service learns recipients' devices and public keys from Polymorfa, much as a WhatsApp client learns them from WhatsApp's servers. It checks that each key bundle is well formed and signed by the identity key the bundle presents, but it cannot independently verify that the identity key belongs to the intended recipient. Polymorfa is trusted to relay device lists, prekey bundles and identity-change notices faithfully. A Polymorfa runner that substituted a key or added a device could read messages encrypted to that key. That affects first contact with a recipient, newly listed devices, and contacts whose stored identity it caused this service to forget. It does not expose this service's stored keys or other recipients' sessions. Independent key verification is not implemented. + +## Storage and key custody + +Use SQLite for a deployment on one host, or PostgreSQL for multiple hosts. SQLite uses WAL and `BEGIN IMMEDIATE` for atomic fence checks and write batches. Multiple processes can share one local file; do not place that file on a network filesystem for multi-host failover. PostgreSQL locks the lease row and commits each fence check with its write batch. Startup runs the checked-in driver migrations; the database identity needs schema/migration permissions. Use a dedicated customer-owned database and network/TLS policy. The `memory` driver loses state on restart and is for tests only. + +Each connection gets a random 256-bit data key. An exclusive, fenced insert stores it wrapped with AES-256-GCM under the operator's master key. The wrap authenticates the connection scope. Every Signal record then uses the connection key and authenticates a length-prefixed domain, scope, namespace and record key. Copying ciphertext to another slot fails authentication. Record keys, namespace tags, connection scopes and lease metadata remain readable. Sealing does not detect a rollback to an older valid value in the same slot. + +All nodes sharing a deployment need the same master key and database. A wrong key fails connection activation without resetting state. Back up the database and master key separately, retain a protected recovery copy, and test restoration in an isolated deployment that cannot compete with production claims. Losing the master key loses access to the sealed device state. Online master/data-key rotation and a sealed cross-mode handoff are not implemented; never overwrite an existing key to simulate rotation. Set an operational rotation plan before approaching AES-GCM's per-key nonce limits. + +The sealed decrypt buffer defaults to 14 days; sealed sent-message retry records default to seven days. The pruner runs hourly for locally held connections. Expired records in inactive/logged-out scopes persist until an owning node prunes them or the operator removes that scope. A retention window bounds replay/retry support, not delivery latency. Retention applies to customer-owned sealed protocol state and is distinct from optional hosted history. + +## Scale and failover + +Give each replica a unique stable node ID, the same connection list, the same master key, and access to the same PostgreSQL store. Each process uses a fresh random incarnation token for its database lease holder, so even a replacement with the same configured node ID cannot share the old fence. Every replica attempts free connections. The first successful lease holder claims that connection over its outbound connection. Polymorfa routes to its highest accepted generation. A closed transport releases its fences, and a new socket claims only after acquiring higher durable generations. A crash leaves the lease to expire; another node then takes a higher generation. The old generation cannot commit record writes after takeover. + +Defaults are a 30-second lease TTL and renewal every ten seconds. Keep renewal at most half the TTL. A node stops serving on loss, expiry, or a renewal failure close to expiry. A resumed process checks its deadline before requests and again after send pacing or application ACK. Generation-specific callbacks cannot evict a newer local holder. Keep host clocks synchronized; lease timestamps come from the service clock, so this implementation does not solve arbitrary clock skew. Store loss fails closed; it does not create a new in-memory key store. + +The service clamps the upstream send limit to at most five `SendStanza` operations per second per connection, including its own participant-hash resend. The runner independently enforces its transport admission limit. Lookups/prekey operations do not consume the service's SendStanza pacing window. The ceiling is not a throughput guarantee or a WhatsApp safety approval. + +After a participant-hash mismatch, the service refreshes routing and rebuilds once with the same message ID. WhatsApp Web excludes previously addressed devices; this implementation re-encrypts for all current devices. Real-provider acceptance of that broader resend is unresolved. Source evidence is Cellar bundle `whatsapp-1049950004`, `WAWebSendUserMsgJob.js:53-79` and `WAWebSendGroupSkmsgJob.js:194-213`. Protocol-message dispatch evidence is `WAWebHandleMsgProcess.js:201-228`. Those shipped-client observations do not prove current server behavior. + +## Verification and release gates + +```sh +cargo fmt --all --check +cargo clippy --workspace --all-targets --locked -- -D warnings +# Use a dedicated fixture database; do not point tests at production. +SIGNAL_NODE_TEST_POSTGRES_URL=postgres://fixture@127.0.0.1/signal_node_tests \ + cargo test --workspace --all-targets --locked +cargo test --workspace --doc --locked +cargo build --release --locked +``` + +Without the PostgreSQL variable, local tests explicitly report that PG conformance was not exercised. CI requires it. Tests exercise real SQLite and PostgreSQL store conformance; per-slot authentication; a fake Polymorfa WebSocket with Hello/Welcome/claims; actual two-device Signal encryption/decryption; signed webhook failure/ACK and SSE delivery; encrypted SendStanza; consent-off history; expiry, stale writes and crash replay; and the five-per-second ceiling. The transport peer is a fixture, not a real runner or WhatsApp account. + +Release still requires the pinned library producer to merge, backend/SDK/CLI/docs consumers to integrate on explicit revisions, real-account pairing/send/receive/media/history/group/retry acceptance, and the private AGPL/legal review plus owner publication approval. Repository visibility, package/image publication and production rollout controls remain unchanged by this implementation. The service adds no staff control authority; backend-owned eligibility and counts-only diagnostics stay in the Polymorfa control plane. diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..29a531a --- /dev/null +++ b/build.rs @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +//! Compiles the runner contract with a pure-Rust protobuf compiler, so +//! building needs no `protoc` on the host. + +fn main() -> Result<(), Box> { + let proto = "proto/polymorfa/signal/v1/signal_service.proto"; + println!("cargo:rerun-if-changed={proto}"); + let descriptors = protox::compile([proto], ["proto"])?; + prost_build::Config::new().compile_fds(descriptors)?; + Ok(()) +} diff --git a/crates/recordstore/Cargo.toml b/crates/recordstore/Cargo.toml new file mode 100644 index 0000000..002a8b1 --- /dev/null +++ b/crates/recordstore/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "wacore-recordstore" +version = "0.1.0" +edition = "2024" +rust-version.workspace = true +license = "MIT" +repository = "https://github.com/polymorfa/signal-node" +description = "Fenced, sealed record storage for wacore Signal state" +publish = false + + +[features] +# Reusable checks every RecordStore implementation must pass. Adapter crates +# enable it in their dev-dependencies and call `conformance::run_all`. +conformance = [] + +[dependencies] +aes-gcm = { workspace = true } +async-lock = { workspace = true } +async-trait = { workspace = true } +bytes = { workspace = true } +rand = { workspace = true } +thiserror = { workspace = true } +wacore = { workspace = true } + +[dev-dependencies] +futures = { workspace = true, features = ["executor"] } diff --git a/crates/recordstore/src/conformance.rs b/crates/recordstore/src/conformance.rs new file mode 100644 index 0000000..9f50866 --- /dev/null +++ b/crates/recordstore/src/conformance.rs @@ -0,0 +1,551 @@ +// SPDX-License-Identifier: MIT + +//! Behaviour every [`RecordStore`] + [`LeaseStore`] adapter must show. +//! +//! Adapter crates enable the `conformance` feature in dev-dependencies and run +//! [`run_all`] against a real database or emulator. Each check uses its own +//! scope with a random suffix, so the suite can share a database with other +//! tests and with earlier runs. + +use crate::record::{ + Fence, LeaseStore, Namespace, RecordStore, WriteOp, is_fence_lost, is_record_exists, +}; +use rand::Rng; + +const TTL: u64 = 10_000; + +/// Run every check. Panics with the failing check's name. +pub async fn run_all(store: &S) { + let run = format!("{:08x}", rand::rng().next_u32()); + leases_exclude_other_holders_until_expiry(store, &run).await; + same_holder_keeps_its_generation(store, &run).await; + renew_fails_after_takeover(store, &run).await; + release_advances_the_next_generation(store, &run).await; + writes_need_the_current_fence(store, &run).await; + a_stale_batch_applies_nothing(store, &run).await; + a_batch_applies_every_op(store, &run).await; + update_never_inserts(store, &run).await; + insert_is_exclusive_and_atomic(store, &run).await; + inserts_see_earlier_ops_in_the_same_batch(store, &run).await; + insert_then_delete_preserves_absence_and_batch_atomicity(store, &run).await; + scan_orders_filters_and_limits(store, &run).await; + scopes_and_namespaces_are_isolated(store, &run).await; + get_many_returns_only_existing_keys(store, &run).await; +} + +fn scope(run: &str, check: &str) -> String { + format!("conformance-{run}-{check}") +} + +pub async fn insert_then_delete_preserves_absence_and_batch_atomicity< + S: RecordStore + LeaseStore, +>( + store: &S, + run: &str, +) { + let scope = scope(run, "insert-then-delete"); + let fence = take(store, &scope, "holder", 1_000).await; + let ns = Namespace::PreKey; + let conditional_delete = |key: &str| { + vec![ + WriteOp::Insert { + ns, + key: key.into(), + value: b"sealed-placeholder".to_vec(), + }, + WriteOp::Delete { + ns, + key: key.into(), + }, + put(Namespace::Device, "other", b"whole-batch"), + ] + }; + store + .write(&fence, &conditional_delete("absent")) + .await + .unwrap(); + assert_eq!(store.get(&scope, ns, "absent").await.unwrap(), None); + assert_eq!( + store.get(&scope, Namespace::Device, "other").await.unwrap(), + Some(b"whole-batch".to_vec()) + ); + store + .write( + &fence, + &[ + put(ns, "present", b"independent"), + put(Namespace::Device, "other", b"original"), + ], + ) + .await + .unwrap(); + let error = store + .write(&fence, &conditional_delete("present")) + .await + .unwrap_err(); + assert!(is_record_exists(&error)); + assert_eq!( + store.get(&scope, ns, "present").await.unwrap(), + Some(b"independent".to_vec()) + ); + assert_eq!( + store.get(&scope, Namespace::Device, "other").await.unwrap(), + Some(b"original".to_vec()) + ); +} + +fn put(ns: Namespace, key: &str, value: &[u8]) -> WriteOp { + WriteOp::Put { + ns, + key: key.to_owned(), + value: value.to_vec(), + } +} + +async fn take(store: &S, scope: &str, holder: &str, now: u64) -> Fence { + store + .acquire(scope, holder, now, TTL) + .await + .expect("acquire") + .unwrap_or_else(|| panic!("{holder} could not acquire {scope}")) + .fence +} + +pub async fn leases_exclude_other_holders_until_expiry(store: &S, run: &str) { + let scope = scope(run, "exclusive"); + let first = take(store, &scope, "a", 1_000).await; + assert!( + store + .acquire(&scope, "b", 1_000 + TTL - 1, TTL) + .await + .unwrap() + .is_none(), + "leases_exclude_other_holders_until_expiry: b took a live lease" + ); + let second = take(store, &scope, "b", 1_000 + TTL).await; + assert!( + second.generation > first.generation, + "leases_exclude_other_holders_until_expiry: generation did not advance" + ); +} + +pub async fn same_holder_keeps_its_generation(store: &S, run: &str) { + let scope = scope(run, "same-holder"); + let first = take(store, &scope, "a", 1_000).await; + let again = take(store, &scope, "a", 2_000).await; + assert_eq!( + first, again, + "same_holder_keeps_its_generation: re-acquire changed the fence" + ); +} + +pub async fn renew_fails_after_takeover(store: &S, run: &str) { + let scope = scope(run, "renew"); + let lease = store + .acquire(&scope, "a", 1_000, TTL) + .await + .unwrap() + .unwrap(); + let renewed = store.renew(&lease, 2_000, TTL).await.unwrap(); + assert!( + renewed.is_some(), + "renew_fails_after_takeover: live renew failed" + ); + take(store, &scope, "b", 2_000 + TTL).await; + assert!( + store + .renew(&lease, 2_000 + TTL, TTL) + .await + .unwrap() + .is_none(), + "renew_fails_after_takeover: lost lease renewed" + ); +} + +pub async fn release_advances_the_next_generation( + store: &S, + run: &str, +) { + let scope = scope(run, "release"); + let lease = store + .acquire(&scope, "a", 1_000, TTL) + .await + .unwrap() + .unwrap(); + store.release(&lease).await.unwrap(); + let next = take(store, &scope, "b", 1_001).await; + assert!( + next.generation > lease.fence.generation, + "release_advances_the_next_generation: b reused a's generation" + ); + let stale = store + .write(&lease.fence, &[put(Namespace::Session, "k", b"v")]) + .await; + assert!( + stale.as_ref().is_err_and(is_fence_lost), + "release_advances_the_next_generation: released fence still wrote" + ); +} + +pub async fn writes_need_the_current_fence(store: &S, run: &str) { + let scope = scope(run, "fence"); + let never_leased = Fence { + scope: scope.as_str().into(), + generation: 1, + }; + let refused = store + .write(&never_leased, &[put(Namespace::Session, "k", b"v")]) + .await; + assert!( + refused.as_ref().is_err_and(is_fence_lost), + "writes_need_the_current_fence: wrote without any lease" + ); + let a = take(store, &scope, "a", 1_000).await; + store + .write(&a, &[put(Namespace::Session, "k", b"from-a")]) + .await + .expect("writes_need_the_current_fence: holder write failed"); + let b = take(store, &scope, "b", 1_000 + TTL).await; + let late = store + .write(&a, &[put(Namespace::Session, "k", b"late-a")]) + .await; + assert!( + late.as_ref().is_err_and(is_fence_lost), + "writes_need_the_current_fence: superseded holder wrote" + ); + store + .write(&b, &[put(Namespace::Session, "k", b"from-b")]) + .await + .unwrap(); + assert_eq!( + store + .get(&scope, Namespace::Session, "k") + .await + .unwrap() + .as_deref(), + Some(&b"from-b"[..]), + "writes_need_the_current_fence: wrong final value" + ); +} + +pub async fn a_stale_batch_applies_nothing(store: &S, run: &str) { + let scope = scope(run, "stale-batch"); + let a = take(store, &scope, "a", 1_000).await; + store + .write(&a, &[put(Namespace::Session, "keep", b"original")]) + .await + .unwrap(); + take(store, &scope, "b", 1_000 + TTL).await; + let stale = store + .write( + &a, + &[ + put(Namespace::Session, "new", b"x"), + put(Namespace::Session, "keep", b"overwritten"), + WriteOp::Delete { + ns: Namespace::Session, + key: "keep".into(), + }, + ], + ) + .await; + assert!( + stale.as_ref().is_err_and(is_fence_lost), + "a_stale_batch_applies_nothing: stale batch was not refused as fence lost" + ); + assert_eq!( + store + .get(&scope, Namespace::Session, "keep") + .await + .unwrap() + .as_deref(), + Some(&b"original"[..]), + "a_stale_batch_applies_nothing: stale batch changed a record" + ); + assert!( + store + .get(&scope, Namespace::Session, "new") + .await + .unwrap() + .is_none(), + "a_stale_batch_applies_nothing: stale batch inserted a record" + ); +} + +pub async fn a_batch_applies_every_op(store: &S, run: &str) { + let scope = scope(run, "batch"); + let fence = take(store, &scope, "a", 1_000).await; + store + .write( + &fence, + &[ + put(Namespace::PreKey, "1", b"one"), + put(Namespace::PreKey, "2", b"two"), + ], + ) + .await + .unwrap(); + store + .write( + &fence, + &[ + WriteOp::Delete { + ns: Namespace::PreKey, + key: "1".into(), + }, + WriteOp::Update { + ns: Namespace::PreKey, + key: "2".into(), + value: b"two-updated".to_vec(), + }, + put(Namespace::PreKey, "3", b"three"), + ], + ) + .await + .unwrap(); + assert!( + store + .get(&scope, Namespace::PreKey, "1") + .await + .unwrap() + .is_none() + ); + assert_eq!( + store + .get(&scope, Namespace::PreKey, "2") + .await + .unwrap() + .as_deref(), + Some(&b"two-updated"[..]), + "a_batch_applies_every_op: update missing" + ); + assert_eq!( + store + .get(&scope, Namespace::PreKey, "3") + .await + .unwrap() + .as_deref(), + Some(&b"three"[..]), + "a_batch_applies_every_op: put missing" + ); +} + +pub async fn update_never_inserts(store: &S, run: &str) { + let scope = scope(run, "update"); + let fence = take(store, &scope, "a", 1_000).await; + store + .write( + &fence, + &[WriteOp::Update { + ns: Namespace::PreKey, + key: "gone".into(), + value: b"x".to_vec(), + }], + ) + .await + .unwrap(); + assert!( + store + .get(&scope, Namespace::PreKey, "gone") + .await + .unwrap() + .is_none(), + "update_never_inserts: update resurrected a missing key" + ); +} + +pub async fn scan_orders_filters_and_limits(store: &S, run: &str) { + let scope = scope(run, "scan"); + let fence = take(store, &scope, "a", 1_000).await; + let keys = ["user:2@s", "user@s", "user:1@s", "other@s", "usera@s"]; + let ops: Vec<_> = keys + .iter() + .map(|k| put(Namespace::Session, k, b"v")) + .collect(); + store.write(&fence, &ops).await.unwrap(); + assert_eq!( + store + .scan_keys(&scope, Namespace::Session, "user:", None) + .await + .unwrap(), + vec!["user:1@s".to_owned(), "user:2@s".to_owned()], + "scan_orders_filters_and_limits: prefix scan" + ); + assert_eq!( + store + .scan_keys(&scope, Namespace::Session, "", Some(2)) + .await + .unwrap(), + vec!["other@s".to_owned(), "user:1@s".to_owned()], + "scan_orders_filters_and_limits: limited full scan" + ); +} + +pub async fn scopes_and_namespaces_are_isolated(store: &S, run: &str) { + let first = scope(run, "iso-1"); + let second = scope(run, "iso-1x"); + let a = take(store, &first, "a", 1_000).await; + let b = take(store, &second, "a", 1_000).await; + store + .write(&a, &[put(Namespace::Session, "k", b"first-session")]) + .await + .unwrap(); + store + .write(&b, &[put(Namespace::Session, "k", b"second-session")]) + .await + .unwrap(); + assert!( + store + .get(&first, Namespace::Identity, "k") + .await + .unwrap() + .is_none(), + "scopes_and_namespaces_are_isolated: namespace leaked" + ); + assert_eq!( + store + .get(&first, Namespace::Session, "k") + .await + .unwrap() + .as_deref(), + Some(&b"first-session"[..]), + "scopes_and_namespaces_are_isolated: scope leaked" + ); + assert_eq!( + store + .scan_keys(&first, Namespace::Session, "", None) + .await + .unwrap(), + vec!["k".to_owned()], + "scopes_and_namespaces_are_isolated: scan crossed into a sibling scope" + ); +} + +pub async fn get_many_returns_only_existing_keys( + store: &S, + run: &str, +) { + let scope = scope(run, "get-many"); + let fence = take(store, &scope, "a", 1_000).await; + store + .write( + &fence, + &[ + put(Namespace::SenderKey, "x", b"1"), + put(Namespace::SenderKey, "y", b"2"), + ], + ) + .await + .unwrap(); + let mut found = store + .get_many(&scope, Namespace::SenderKey, &["x", "missing", "y"]) + .await + .unwrap(); + found.sort(); + assert_eq!( + found, + vec![ + ("x".to_owned(), b"1".to_vec()), + ("y".to_owned(), b"2".to_vec()) + ], + "get_many_returns_only_existing_keys" + ); +} + +pub async fn insert_is_exclusive_and_atomic(store: &S, run: &str) { + let scope = scope(run, "insert"); + let fence = take(store, &scope, "a", 1_000).await; + let insert = |key: &str, value: &[u8]| WriteOp::Insert { + ns: Namespace::Device, + key: key.to_owned(), + value: value.to_vec(), + }; + store.write(&fence, &[insert("k", b"first")]).await.unwrap(); + let refused = store + .write( + &fence, + &[ + put(Namespace::Session, "side-effect", b"x"), + insert("k", b"second"), + ], + ) + .await; + assert!( + refused.as_ref().is_err_and(is_record_exists), + "insert_is_exclusive_and_atomic: second insert was not refused" + ); + assert_eq!( + store + .get(&scope, Namespace::Device, "k") + .await + .unwrap() + .as_deref(), + Some(&b"first"[..]), + "insert_is_exclusive_and_atomic: existing record changed" + ); + assert!( + store + .get(&scope, Namespace::Session, "side-effect") + .await + .unwrap() + .is_none(), + "insert_is_exclusive_and_atomic: refused batch applied another op" + ); +} + +pub async fn inserts_see_earlier_ops_in_the_same_batch( + store: &S, + run: &str, +) { + let scope = scope(run, "insert-in-batch"); + let fence = take(store, &scope, "a", 1_000).await; + let ns = Namespace::Device; + let insert = |key: &str, value: &[u8]| WriteOp::Insert { + ns, + key: key.to_owned(), + value: value.to_vec(), + }; + let refused_cases: [(&str, Vec); 2] = [ + ("dup", vec![insert("dup", b"a"), insert("dup", b"b")]), + ("put", vec![put(ns, "put", b"a"), insert("put", b"b")]), + ]; + for (key, mut ops) in refused_cases { + ops.push(put(Namespace::Session, "side-effect", b"x")); + let refused = store.write(&fence, &ops).await; + assert!( + refused.as_ref().is_err_and(is_record_exists), + "inserts_see_earlier_ops_in_the_same_batch: {key} batch was not refused" + ); + assert!( + store.get(&scope, ns, key).await.unwrap().is_none(), + "inserts_see_earlier_ops_in_the_same_batch: {key} refused batch wrote a record" + ); + assert!( + store + .get(&scope, Namespace::Session, "side-effect") + .await + .unwrap() + .is_none(), + "inserts_see_earlier_ops_in_the_same_batch: {key} refused batch applied another op" + ); + } + store + .write( + &fence, + &[ + insert("again", b"a"), + WriteOp::Delete { + ns, + key: "again".into(), + }, + insert("again", b"b"), + ], + ) + .await + .expect("inserts_see_earlier_ops_in_the_same_batch: insert after delete refused"); + assert_eq!( + store.get(&scope, ns, "again").await.unwrap().as_deref(), + Some(&b"b"[..]), + "inserts_see_earlier_ops_in_the_same_batch: insert after delete lost" + ); +} diff --git a/crates/recordstore/src/lib.rs b/crates/recordstore/src/lib.rs new file mode 100644 index 0000000..b20f23f --- /dev/null +++ b/crates/recordstore/src/lib.rs @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: MIT + +//! Fenced, sealed record storage for wacore Signal state. +//! +//! A database adapter implements two small traits, [`RecordStore`] and +//! [`LeaseStore`], over opaque byte records. [`RecordSignalStore`] builds the +//! full [`wacore::store::traits::SignalStore`] on top of them, so Signal +//! crypto keeps using unmodified wacore code. +//! +//! Two guarantees come from this layer rather than from each adapter: +//! +//! - **Fencing.** Every write carries the [`Fence`] of the lease that +//! authorised it. The adapter applies the batch only while that lease +//! generation is still current, in the same transaction. A node that lost +//! its lease can never overwrite state written by its successor. +//! - **Sealing.** [`RecordSignalStore`] seals every value with a [`Sealer`] +//! before it reaches the adapter. The authenticated data binds scope, +//! namespace and key, so a record cannot be moved to another slot. Record +//! keys (Signal addresses and prekey IDs) stay readable so adapters can +//! index and scan them. [`RecordStore::write`] itself stores bytes as given: +//! a caller that bypasses [`RecordSignalStore`] must seal sensitive values +//! itself. +//! +//! Sealing does not detect rollback: an adapter that serves an older sealed +//! value for the same slot passes authentication. + +mod memory; +mod record; +mod seal; +mod signal; + +#[cfg(any(test, feature = "conformance"))] +pub mod conformance; + +pub use memory::MemoryRecordStore; +pub use record::{ + Fence, FenceLost, Lease, LeaseStore, Namespace, RecordExists, RecordStore, WriteOp, + is_fence_lost, is_record_exists, +}; +pub use seal::{AesGcmSealer, SealError, Sealer}; +pub use signal::RecordSignalStore; diff --git a/crates/recordstore/src/memory.rs b/crates/recordstore/src/memory.rs new file mode 100644 index 0000000..cfae9d1 --- /dev/null +++ b/crates/recordstore/src/memory.rs @@ -0,0 +1,228 @@ +// SPDX-License-Identifier: MIT + +use crate::record::{ + Fence, FenceLost, Lease, LeaseStore, Namespace, RecordExists, RecordStore, WriteOp, +}; +use async_trait::async_trait; +use std::collections::{BTreeMap, HashMap}; +use std::sync::{Arc, Mutex, MutexGuard}; +use wacore::store::error::{Result, StoreError}; + +type Records = BTreeMap<(Arc, Namespace, String), Vec>; + +#[derive(Default)] +struct State { + records: Records, + leases: HashMap, LeaseRow>, +} + +#[derive(Clone)] +struct LeaseRow { + holder: Arc, + generation: u64, + expires_at_ms: u64, +} + +/// In-process [`RecordStore`] and [`LeaseStore`]. The reference behaviour for +/// the conformance suite, and the fallback where no database is configured. +#[derive(Default)] +pub struct MemoryRecordStore { + state: Mutex, +} + +impl MemoryRecordStore { + pub fn new() -> Self { + Self::default() + } + + fn lock(&self) -> Result> { + self.state + .lock() + .map_err(|_| StoreError::Validation("memory record store lock poisoned".into())) + } +} + +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +impl RecordStore for MemoryRecordStore { + async fn get(&self, scope: &str, ns: Namespace, key: &str) -> Result>> { + let state = self.lock()?; + Ok(state + .records + .get(&(Arc::from(scope), ns, key.to_owned())) + .cloned()) + } + + async fn scan_keys( + &self, + scope: &str, + ns: Namespace, + prefix: &str, + limit: Option, + ) -> Result> { + let state = self.lock()?; + let scope: Arc = Arc::from(scope); + let start = (scope.clone(), ns, prefix.to_owned()); + let keys = state + .records + .range(start..) + .map(|((s, n, k), _)| (s, n, k)) + .take_while(|(s, n, k)| **s == scope && **n == ns && k.starts_with(prefix)) + .map(|(_, _, k)| k.clone()); + Ok(match limit { + Some(limit) => keys.take(limit).collect(), + None => keys.collect(), + }) + } + + async fn write(&self, fence: &Fence, ops: &[WriteOp]) -> Result<()> { + let mut state = self.lock()?; + let current = state.leases.get(&fence.scope).map(|row| row.generation); + if current != Some(fence.generation) { + return Err(FenceLost { + scope: fence.scope.clone(), + generation: fence.generation, + } + .into_store_error()); + } + // Validation happens before any mutation, so the batch stays atomic. + // `present` tracks the batch's running effect on key presence, so an + // Insert sees earlier Put/Insert/Delete ops of the same batch. + let mut present: HashMap<(Namespace, &str), bool> = HashMap::new(); + for op in ops { + match op { + WriteOp::Insert { ns, key, .. } => { + let exists = match present.get(&(*ns, key.as_str())) { + Some(&known) => known, + None => { + state + .records + .contains_key(&(fence.scope.clone(), *ns, key.clone())) + } + }; + if exists { + return Err(RecordExists { + ns: *ns, + key: key.clone(), + } + .into_store_error()); + } + present.insert((*ns, key.as_str()), true); + } + WriteOp::Put { ns, key, .. } => { + present.insert((*ns, key.as_str()), true); + } + WriteOp::Delete { ns, key } => { + present.insert((*ns, key.as_str()), false); + } + // Update never changes presence. + WriteOp::Update { .. } => {} + } + } + for op in ops { + match op { + WriteOp::Put { ns, key, value } | WriteOp::Insert { ns, key, value } => { + state + .records + .insert((fence.scope.clone(), *ns, key.clone()), value.clone()); + } + WriteOp::Update { ns, key, value } => { + if let Some(slot) = + state + .records + .get_mut(&(fence.scope.clone(), *ns, key.clone())) + { + slot.clone_from(value); + } + } + WriteOp::Delete { ns, key } => { + state + .records + .remove(&(fence.scope.clone(), *ns, key.clone())); + } + } + } + Ok(()) + } +} + +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +impl LeaseStore for MemoryRecordStore { + async fn acquire( + &self, + scope: &str, + holder: &str, + now_ms: u64, + ttl_ms: u64, + ) -> Result> { + // An empty holder is how a released lease is marked. + if holder.is_empty() { + return Err(StoreError::Validation( + "lease holder must not be empty".into(), + )); + } + let mut state = self.lock()?; + let scope: Arc = Arc::from(scope); + let expires_at_ms = now_ms.saturating_add(ttl_ms); + let generation = match state.leases.get(&scope) { + Some(row) if &*row.holder == holder => row.generation, + Some(row) if row.expires_at_ms > now_ms => return Ok(None), + Some(row) => row.generation + 1, + None => 1, + }; + let holder: Arc = Arc::from(holder); + state.leases.insert( + scope.clone(), + LeaseRow { + holder: holder.clone(), + generation, + expires_at_ms, + }, + ); + Ok(Some(Lease { + fence: Fence { scope, generation }, + holder, + expires_at_ms, + })) + } + + async fn renew(&self, lease: &Lease, now_ms: u64, ttl_ms: u64) -> Result> { + let mut state = self.lock()?; + let Some(row) = state.leases.get_mut(&lease.fence.scope) else { + return Ok(None); + }; + if row.generation != lease.fence.generation || row.holder != lease.holder { + return Ok(None); + } + row.expires_at_ms = now_ms.saturating_add(ttl_ms); + Ok(Some(Lease { + expires_at_ms: row.expires_at_ms, + ..lease.clone() + })) + } + + async fn release(&self, lease: &Lease) -> Result<()> { + let mut state = self.lock()?; + if let Some(row) = state.leases.get_mut(&lease.fence.scope) + && row.generation == lease.fence.generation + && row.holder == lease.holder + { + // Expire it but keep the generation: the next holder must still + // advance past it, or a delayed write from this lease could land. + row.expires_at_ms = 0; + row.holder = Arc::from(""); + } + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn passes_the_conformance_suite() { + futures::executor::block_on(crate::conformance::run_all(&MemoryRecordStore::new())); + } +} diff --git a/crates/recordstore/src/record.rs b/crates/recordstore/src/record.rs new file mode 100644 index 0000000..2ff2c08 --- /dev/null +++ b/crates/recordstore/src/record.rs @@ -0,0 +1,224 @@ +// SPDX-License-Identifier: MIT + +use async_trait::async_trait; +use std::sync::Arc; +use wacore::store::error::{Result, StoreError}; + +/// Record families. The wire tag is persisted by adapters; never renumber. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub enum Namespace { + Identity, + Session, + PreKey, + SignedPreKey, + SenderKey, + /// The local device's own keys (identity, registration ID, ADV secret, + /// current signed prekey). Read and written whole by the Signal service. + Device, + /// Decrypted results kept until the app confirms delivery, so a + /// redelivered ciphertext returns the same result instead of failing. + DecryptBuffer, + /// Messages this device sent, kept so a retry receipt can re-encrypt them. + SentMessage, + /// Group member devices confirmed to hold this device's sender key. + SenderKeyDevices, + /// Pinned identity keys of peer accounts, the anchor that companion + /// devices' ADV signatures are verified against. + AccountIdentity, +} + +impl Namespace { + pub const ALL: [Namespace; 10] = [ + Namespace::Identity, + Namespace::Session, + Namespace::PreKey, + Namespace::SignedPreKey, + Namespace::SenderKey, + Namespace::Device, + Namespace::DecryptBuffer, + Namespace::SentMessage, + Namespace::SenderKeyDevices, + Namespace::AccountIdentity, + ]; + + /// Stable persisted name. + pub const fn tag(self) -> &'static str { + match self { + Namespace::Identity => "identity", + Namespace::Session => "session", + Namespace::PreKey => "prekey", + Namespace::SignedPreKey => "signed_prekey", + Namespace::SenderKey => "sender_key", + Namespace::Device => "device", + Namespace::DecryptBuffer => "decrypt_buffer", + Namespace::SentMessage => "sent_message", + Namespace::SenderKeyDevices => "sender_key_devices", + Namespace::AccountIdentity => "account_identity", + } + } +} + +/// Authority to write one scope. A scope is one linked device's Signal state. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Fence { + pub scope: Arc, + pub generation: u64, +} + +/// A held lease. `fence` stays constant across renewals by the same holder. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Lease { + pub fence: Fence, + pub holder: Arc, + pub expires_at_ms: u64, +} + +/// One change inside an atomic [`RecordStore::write`] batch. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum WriteOp { + /// Insert or replace. + Put { + ns: Namespace, + key: String, + value: Vec, + }, + /// Replace only if the key exists; a missing key is a no-op, never an + /// insert. Used where resurrecting a deleted record would be a bug. + Update { + ns: Namespace, + key: String, + value: Vec, + }, + /// Remove; a missing key is a no-op. + Delete { ns: Namespace, key: String }, + /// Insert only if the key is absent. If it exists, the whole batch fails + /// with [`RecordExists`] and nothing is applied. + Insert { + ns: Namespace, + key: String, + value: Vec, + }, +} + +/// The write was refused because `fence` no longer matches the scope's lease. +#[derive(Debug, Clone, thiserror::Error)] +#[error("lease for scope {scope} moved past generation {generation}")] +pub struct FenceLost { + pub scope: Arc, + pub generation: u64, +} + +impl FenceLost { + pub fn into_store_error(self) -> StoreError { + StoreError::Database(Box::new(self)) + } +} + +/// An [`WriteOp::Insert`] found its key already present; nothing was applied. +#[derive(Debug, Clone, thiserror::Error)] +#[error("{ns:?} record {key} already exists")] +pub struct RecordExists { + pub ns: Namespace, + pub key: String, +} + +impl RecordExists { + pub fn into_store_error(self) -> StoreError { + StoreError::Database(Box::new(self)) + } +} + +/// Whether `error` (or any error it wraps) is a [`RecordExists`]. +pub fn is_record_exists(error: &StoreError) -> bool { + let mut layer: Option<&(dyn std::error::Error + 'static)> = Some(error); + while let Some(current) = layer { + if current.is::() { + return true; + } + layer = current.source(); + } + false +} + +/// Whether `error` (or any error it wraps) is a [`FenceLost`]. A node that +/// sees this must stop serving the scope; retrying cannot succeed. +pub fn is_fence_lost(error: &StoreError) -> bool { + let mut layer: Option<&(dyn std::error::Error + 'static)> = Some(error); + while let Some(current) = layer { + if current.is::() { + return true; + } + layer = current.source(); + } + false +} + +/// Byte-level storage an adapter implements. Values are already sealed. +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +pub trait RecordStore: Send + Sync { + async fn get(&self, scope: &str, ns: Namespace, key: &str) -> Result>>; + + /// Returns only keys that exist, in any order. + async fn get_many( + &self, + scope: &str, + ns: Namespace, + keys: &[&str], + ) -> Result)>> { + let mut found = Vec::with_capacity(keys.len()); + for key in keys { + if let Some(value) = self.get(scope, ns, key).await? { + found.push(((*key).to_owned(), value)); + } + } + Ok(found) + } + + /// Keys starting with `prefix`, ascending by byte order, at most `limit`. + async fn scan_keys( + &self, + scope: &str, + ns: Namespace, + prefix: &str, + limit: Option, + ) -> Result>; + + /// Apply every op or none, and only while `fence.generation` is the + /// scope's current lease generation. Otherwise return + /// [`FenceLost::into_store_error`] and apply nothing. An `Insert` whose key + /// exists fails the batch with [`RecordExists::into_store_error`]. + /// An `Insert` followed by a `Delete` of the same key checks absence + /// atomically without retaining a record after a successful batch. + /// + /// The returned future may be dropped after the backend applied the batch. + /// A caller that did not see the result must re-read the affected records + /// before retrying, because a blind retry of an `Insert` fails with + /// [`RecordExists`]. + async fn write(&self, fence: &Fence, ops: &[WriteOp]) -> Result<()>; +} + +/// Per-scope single-writer leases. Time is supplied by the caller so the +/// contract holds on targets without a system clock. +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +pub trait LeaseStore: Send + Sync { + /// Take the scope if it is free, expired, or already held by `holder`. + /// A new holder gets a strictly higher generation; the same holder keeps + /// its generation. Returns `None` while another holder's lease is live. + async fn acquire( + &self, + scope: &str, + holder: &str, + now_ms: u64, + ttl_ms: u64, + ) -> Result>; + + /// Extend `lease`. Returns `None` if the scope moved to another + /// generation, in which case the caller lost it. + async fn renew(&self, lease: &Lease, now_ms: u64, ttl_ms: u64) -> Result>; + + /// Give the scope up early. Releasing a lease that was already lost is a + /// no-op. The generation is kept so a later holder still advances it. + async fn release(&self, lease: &Lease) -> Result<()>; +} diff --git a/crates/recordstore/src/seal.rs b/crates/recordstore/src/seal.rs new file mode 100644 index 0000000..a7d3781 --- /dev/null +++ b/crates/recordstore/src/seal.rs @@ -0,0 +1,161 @@ +// SPDX-License-Identifier: MIT + +use aes_gcm::{ + Aes256Gcm, KeyInit, + aead::{Aead, Payload}, +}; +use async_trait::async_trait; +use rand::Rng; + +/// Seals record values before they reach a [`crate::RecordStore`]. +/// +/// Implementations other than [`AesGcmSealer`] exist where the key must not +/// enter this process, for example a non-extractable WebCrypto key in a +/// browser. `aad` must be authenticated, not encrypted. +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +pub trait Sealer: Send + Sync { + async fn seal(&self, aad: &[u8], plaintext: &[u8]) -> Result, SealError>; + async fn open(&self, aad: &[u8], sealed: &[u8]) -> Result, SealError>; +} + +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum SealError { + /// Unknown format version: written by a newer release, or not sealed. + #[error("unsupported sealed record format {0}")] + UnsupportedFormat(u8), + #[error("sealed record is truncated")] + Truncated, + /// Wrong key, tampered bytes, or a record moved to another slot. + #[error("sealed record failed authentication")] + Authentication, + #[error("sealer backend failed: {0}")] + Backend(String), +} + +/// AES-256-GCM with a 256-bit data key held in memory. +/// +/// Layout, format 1: `[0x01][12-byte random nonce][ciphertext || 16-byte tag]`. +/// Nonces are random 96-bit values, so NIST SP 800-38D section 8.3 limits one +/// key to 2^32 encryptions in total. Every seal counts, including overwrites +/// and retries, not only distinct records. Callers must rotate the data key +/// before reaching that limit. +pub struct AesGcmSealer { + cipher: Aes256Gcm, +} + +const FORMAT_V1: u8 = 1; +const NONCE_LEN: usize = 12; +const TAG_LEN: usize = 16; + +impl AesGcmSealer { + pub fn new(data_key: &[u8; 32]) -> Self { + // A 32-byte slice always matches Aes256Gcm's key size. + let cipher = Aes256Gcm::new(data_key.into()); + Self { cipher } + } +} + +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +impl Sealer for AesGcmSealer { + async fn seal(&self, aad: &[u8], plaintext: &[u8]) -> Result, SealError> { + let mut nonce = [0u8; NONCE_LEN]; + rand::rng().fill_bytes(&mut nonce); + let ciphertext = self + .cipher + .encrypt( + &nonce.into(), + Payload { + msg: plaintext, + aad, + }, + ) + .map_err(|_| SealError::Backend("aes-gcm encryption failed".into()))?; + let mut out = Vec::with_capacity(1 + NONCE_LEN + ciphertext.len()); + out.push(FORMAT_V1); + out.extend_from_slice(&nonce); + out.extend_from_slice(&ciphertext); + Ok(out) + } + + async fn open(&self, aad: &[u8], sealed: &[u8]) -> Result, SealError> { + let (&format, rest) = sealed.split_first().ok_or(SealError::Truncated)?; + if format != FORMAT_V1 { + return Err(SealError::UnsupportedFormat(format)); + } + if rest.len() < NONCE_LEN + TAG_LEN { + return Err(SealError::Truncated); + } + let (nonce, ciphertext) = rest.split_at(NONCE_LEN); + let nonce: [u8; NONCE_LEN] = nonce.try_into().map_err(|_| SealError::Truncated)?; + self.cipher + .decrypt( + &nonce.into(), + Payload { + msg: ciphertext, + aad, + }, + ) + .map_err(|_| SealError::Authentication) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use futures::executor::block_on; + + fn sealer(byte: u8) -> AesGcmSealer { + AesGcmSealer::new(&[byte; 32]) + } + + #[test] + fn round_trips_and_uses_fresh_nonces() { + let s = sealer(7); + let a = block_on(s.seal(b"slot", b"value")).unwrap(); + let b = block_on(s.seal(b"slot", b"value")).unwrap(); + assert_ne!(a, b, "two seals of the same value must differ"); + assert_eq!(block_on(s.open(b"slot", &a)).unwrap(), b"value"); + } + + #[test] + fn a_record_moved_to_another_slot_fails() { + let s = sealer(7); + let sealed = block_on(s.seal(b"slot-a", b"value")).unwrap(); + assert_eq!( + block_on(s.open(b"slot-b", &sealed)), + Err(SealError::Authentication) + ); + } + + #[test] + fn a_different_key_fails() { + let sealed = block_on(sealer(7).seal(b"slot", b"value")).unwrap(); + assert_eq!( + block_on(sealer(8).open(b"slot", &sealed)), + Err(SealError::Authentication) + ); + } + + #[test] + fn tampering_and_truncation_are_rejected() { + let s = sealer(7); + let mut sealed = block_on(s.seal(b"slot", b"value")).unwrap(); + let last = sealed.len() - 1; + sealed[last] ^= 1; + assert_eq!( + block_on(s.open(b"slot", &sealed)), + Err(SealError::Authentication) + ); + assert_eq!(block_on(s.open(b"slot", &[])), Err(SealError::Truncated)); + assert_eq!( + block_on(s.open(b"slot", &[FORMAT_V1, 0, 0])), + Err(SealError::Truncated) + ); + assert_eq!( + block_on(s.open(b"slot", &[9; 40])), + Err(SealError::UnsupportedFormat(9)) + ); + } +} diff --git a/crates/recordstore/src/signal.rs b/crates/recordstore/src/signal.rs new file mode 100644 index 0000000..6f9fbc7 --- /dev/null +++ b/crates/recordstore/src/signal.rs @@ -0,0 +1,1331 @@ +// SPDX-License-Identifier: MIT + +use crate::record::{Fence, Namespace, RecordExists, RecordStore, WriteOp}; +use crate::seal::{SealError, Sealer}; +use async_lock::Mutex; +use async_trait::async_trait; +use bytes::Bytes; +use std::collections::BTreeMap; +use std::sync::Arc; +use wacore::store::error::{Result, StoreError}; +use wacore::store::traits::SignalStore; + +const AAD_DOMAIN: &[u8] = b"wacore-recordstore/v1"; +const DEVICE_KEY: &str = "local"; + +/// Prekey plaintext starts with this flag byte, then the libsignal record. +const PREKEY_NOT_UPLOADED: u8 = 0; +const PREKEY_UPLOADED: u8 = 1; + +/// Namespaces the Signal service reads and writes directly, outside the +/// [`SignalStore`] surface. +const AUX_NAMESPACES: [Namespace; 5] = [ + Namespace::Device, + Namespace::DecryptBuffer, + Namespace::SentMessage, + Namespace::SenderKeyDevices, + Namespace::AccountIdentity, +]; + +/// A write before sealing. +#[derive(Debug, Clone)] +enum PlainOp { + Put(Namespace, String, Vec), + Insert(Namespace, String, Vec), + Update(Namespace, String, Vec), + Delete(Namespace, String), +} + +/// An uncommitted change held by a staged store. +#[derive(Debug, Clone)] +enum Staged { + Put(Vec), + /// Applied only if the key is still absent when the batch commits. + Insert(Vec), + /// No final value, while preserving an earlier insert's absence check. + InsertDeleted, + /// Applied only if the key still exists when the batch commits. + Update(Vec), + Delete, +} + +type Overlay = BTreeMap<(Namespace, String), Staged>; + +/// [`SignalStore`] for one scope, sealed by `S` and fenced by the lease that +/// produced `fence`. Build a new one whenever the lease is re-acquired. +/// +/// [`Self::staged`] returns a view whose writes stay in memory until +/// [`Self::commit`] seals them and applies them as one fenced batch, so a +/// decrypt's session update, consumed-prekey delete and buffered result land +/// together or not at all. +pub struct RecordSignalStore { + records: Arc, + sealer: Arc, + fence: Fence, + overlay: Option>, +} + +impl RecordSignalStore { + pub fn new(records: Arc, sealer: Arc, fence: Fence) -> Self { + Self { + records, + sealer, + fence, + overlay: None, + } + } + + pub fn fence(&self) -> &Fence { + &self.fence + } + + /// The record backend shared by every store built over it. + pub fn records(&self) -> &Arc { + &self.records + } + + /// Count surviving prekeys marked uploaded, refusing a superseded fence. + /// Missing records consumed during this read are skipped. This is a + /// count of local state, not confirmation of the provider's current pool. + pub async fn uploaded_prekey_count(&self) -> Result { + self.records.write(&self.fence, &[]).await?; + let keys = self.scan(Namespace::PreKey, "", None).await?; + let mut count = 0u32; + for chunk in keys.chunks(128) { + let refs: Vec<_> = chunk.iter().map(String::as_str).collect(); + for (_, value) in self.load_many(Namespace::PreKey, &refs).await? { + match value.first() { + Some(&PREKEY_UPLOADED) => { + count = count + .checked_add(1) + .ok_or_else(|| StoreError::Validation("prekey count overflow".into()))? + } + Some(&PREKEY_NOT_UPLOADED) => {} + _ => { + return Err(StoreError::Validation( + "prekey record has no upload flag".into(), + )); + } + } + } + } + self.records.write(&self.fence, &[]).await?; + Ok(count) + } + + /// A view over the same scope whose writes are held until [`Self::commit`]. + /// Reads see the view's own pending writes first. + pub fn staged(&self) -> Self { + Self { + records: self.records.clone(), + sealer: self.sealer.clone(), + fence: self.fence.clone(), + overlay: Some(Mutex::new(Overlay::new())), + } + } + + /// Seal and apply every pending write of a staged view in one fenced + /// batch. Pending writes remain available after an error or cancellation. + /// Committing a view that is not staged is a no-op. + /// + /// If the future is dropped after the backend applied the batch, the + /// pending writes survive and a retry fails with `RecordExists` on its + /// inserts. Re-read the affected records to reconcile before retrying. + pub async fn commit(&self) -> Result<()> { + let mut pending = match &self.overlay { + Some(overlay) => overlay.lock().await, + None => return Ok(()), + }; + let mut ops = Vec::with_capacity(pending.len()); + // Keep the async guard until the durable write succeeds. Concurrent + // edits cannot disappear behind a successful commit or a failed retry. + for ((ns, key), change) in pending.iter() { + let ns = *ns; + let key = key.clone(); + let op = match change { + Staged::Put(plaintext) => WriteOp::Put { + value: self.seal(ns, &key, plaintext).await?, + ns, + key, + }, + Staged::Insert(plaintext) => WriteOp::Insert { + value: self.seal(ns, &key, plaintext).await?, + ns, + key, + }, + Staged::InsertDeleted => { + // The batch must still prove the slot was absent, even + // though this view's final state has no record there. + ops.push(WriteOp::Insert { + value: self.seal(ns, &key, &[]).await?, + ns, + key: key.clone(), + }); + WriteOp::Delete { ns, key } + } + Staged::Update(plaintext) => WriteOp::Update { + value: self.seal(ns, &key, plaintext).await?, + ns, + key, + }, + Staged::Delete => WriteOp::Delete { ns, key }, + }; + ops.push(op); + } + if ops.is_empty() { + return Ok(()); + } + self.records.write(&self.fence, &ops).await?; + pending.clear(); + Ok(()) + } + + fn scope(&self) -> &str { + &self.fence.scope + } + + /// Authenticated data binding a value to its slot. Every component is + /// length-prefixed so no two (scope, namespace, key) triples encode alike; + /// scopes and keys may contain any byte, including NUL. + fn aad(&self, ns: Namespace, key: &str) -> Vec { + let mut aad = + Vec::with_capacity(AAD_DOMAIN.len() + 12 + self.scope().len() + key.len() + 16); + for part in [ + AAD_DOMAIN, + self.scope().as_bytes(), + ns.tag().as_bytes(), + key.as_bytes(), + ] { + aad.extend_from_slice(&(part.len() as u32).to_be_bytes()); + aad.extend_from_slice(part); + } + aad + } + + async fn seal(&self, ns: Namespace, key: &str, plaintext: &[u8]) -> Result> { + self.sealer + .seal(&self.aad(ns, key), plaintext) + .await + .map_err(seal_error) + } + + async fn open(&self, ns: Namespace, key: &str, sealed: &[u8]) -> Result> { + self.sealer + .open(&self.aad(ns, key), sealed) + .await + .map_err(seal_error) + } + + /// The staged change for one key, if this view holds one. + async fn pending(&self, ns: Namespace, key: &str) -> Option { + match &self.overlay { + Some(overlay) => overlay.lock().await.get(&(ns, key.to_owned())).cloned(), + None => None, + } + } + + async fn load(&self, ns: Namespace, key: &str) -> Result>> { + match self.pending(ns, key).await { + Some(Staged::Put(v) | Staged::Update(v) | Staged::Insert(v)) => return Ok(Some(v)), + Some(Staged::Delete | Staged::InsertDeleted) => return Ok(None), + None => {} + } + match self.records.get(self.scope(), ns, key).await? { + Some(sealed) => Ok(Some(self.open(ns, key, &sealed).await?)), + None => Ok(None), + } + } + + async fn load_many(&self, ns: Namespace, keys: &[&str]) -> Result)>> { + let mut opened = Vec::with_capacity(keys.len()); + let mut from_store = Vec::with_capacity(keys.len()); + for key in keys { + match self.pending(ns, key).await { + Some(Staged::Put(v) | Staged::Update(v) | Staged::Insert(v)) => { + opened.push(((*key).to_owned(), v)) + } + Some(Staged::Delete | Staged::InsertDeleted) => {} + None => from_store.push(*key), + } + } + for (key, sealed) in self.records.get_many(self.scope(), ns, &from_store).await? { + let plaintext = self.open(ns, &key, &sealed).await?; + opened.push((key, plaintext)); + } + Ok(opened) + } + + /// Keys with `prefix`, ascending, including this view's pending changes. + async fn scan(&self, ns: Namespace, prefix: &str, limit: Option) -> Result> { + let Some(overlay) = &self.overlay else { + return self + .records + .scan_keys(self.scope(), ns, prefix, limit) + .await; + }; + let overlay = overlay.lock().await; + // Pending deletes can hide stored keys, so ask the backend for that + // many extra. Inserts only add keys, so they need no headroom. + let backend_limit = limit.map(|limit| { + let hidden = overlay + .iter() + .filter(|((pns, key), change)| { + *pns == ns + && key.starts_with(prefix) + && matches!(change, Staged::Delete | Staged::InsertDeleted) + }) + .count(); + limit.saturating_add(hidden) + }); + let mut keys: std::collections::BTreeSet = self + .records + .scan_keys(self.scope(), ns, prefix, backend_limit) + .await? + .into_iter() + .collect(); + for ((pns, key), change) in overlay.iter() { + if *pns != ns || !key.starts_with(prefix) { + continue; + } + match change { + Staged::Put(_) | Staged::Insert(_) => { + keys.insert(key.clone()); + } + Staged::Delete | Staged::InsertDeleted => { + keys.remove(key); + } + Staged::Update(_) => {} + } + } + let keys = keys.into_iter(); + Ok(match limit { + Some(limit) => keys.take(limit).collect(), + None => keys.collect(), + }) + } + + async fn apply(&self, ops: Vec) -> Result<()> { + if ops.is_empty() { + return Ok(()); + } + if let Some(overlay) = &self.overlay { + let mut overlay = overlay.lock().await; + // Validate the entire call before changing the view. A second + // exclusive insert must not replace an earlier staged private key. + { + let mut inserts = std::collections::BTreeSet::new(); + for op in &ops { + if let PlainOp::Insert(ns, key, _) = op + && (!inserts.insert((*ns, key)) + || overlay.get(&(*ns, key.clone())).is_some_and(|change| { + !matches!(change, Staged::Delete | Staged::InsertDeleted) + })) + { + return Err(RecordExists { + ns: *ns, + key: key.clone(), + } + .into_store_error()); + } + } + } + for op in ops { + let (slot, change) = match op { + PlainOp::Put(ns, key, v) => { + // A later write may replace the staged value, but it + // must retain the original absence check at commit. + let change = match overlay.get(&(ns, key.clone())) { + Some(Staged::Insert(_) | Staged::InsertDeleted) => Staged::Insert(v), + _ => Staged::Put(v), + }; + ((ns, key), change) + } + PlainOp::Insert(ns, key, v) => { + // Deleting a stored key and inserting it again in one + // view replaces it: the commit must not demand the + // key's absence, since the delete is never sent. + let change = match overlay.get(&(ns, key.clone())) { + Some(Staged::Delete) => Staged::Put(v), + _ => Staged::Insert(v), + }; + ((ns, key), change) + } + PlainOp::Update(ns, key, v) => { + // Updating a key this view already wrote keeps it a Put. + let staged = match overlay.get(&(ns, key.clone())) { + Some(Staged::Put(_)) => Staged::Put(v), + Some(Staged::Insert(_)) => Staged::Insert(v), + Some(Staged::Delete | Staged::InsertDeleted) => continue, + _ => Staged::Update(v), + }; + ((ns, key), staged) + } + PlainOp::Delete(ns, key) => { + let change = match overlay.get(&(ns, key.clone())) { + Some(Staged::Insert(_) | Staged::InsertDeleted) => { + Staged::InsertDeleted + } + _ => Staged::Delete, + }; + ((ns, key), change) + } + }; + overlay.insert(slot, change); + } + return Ok(()); + } + let mut sealed = Vec::with_capacity(ops.len()); + for op in ops { + sealed.push(match op { + PlainOp::Put(ns, key, v) => WriteOp::Put { + value: self.seal(ns, &key, &v).await?, + ns, + key, + }, + PlainOp::Insert(ns, key, v) => WriteOp::Insert { + value: self.seal(ns, &key, &v).await?, + ns, + key, + }, + PlainOp::Update(ns, key, v) => WriteOp::Update { + value: self.seal(ns, &key, &v).await?, + ns, + key, + }, + PlainOp::Delete(ns, key) => WriteOp::Delete { ns, key }, + }); + } + self.records.write(&self.fence, &sealed).await + } + + async fn put_one(&self, ns: Namespace, key: &str, plaintext: &[u8]) -> Result<()> { + self.apply(vec![PlainOp::Put(ns, key.to_owned(), plaintext.to_vec())]) + .await + } + + async fn delete_keys>(&self, ns: Namespace, keys: &[K]) -> Result<()> { + self.apply( + keys.iter() + .map(|key| PlainOp::Delete(ns, key.as_ref().to_owned())) + .collect(), + ) + .await + } + + fn check_aux(ns: Namespace) -> Result<()> { + if AUX_NAMESPACES.contains(&ns) { + Ok(()) + } else { + Err(StoreError::Validation(format!( + "{} records are only reachable through SignalStore", + ns.tag() + ))) + } + } + + /// Read a record in one of the service's own namespaces + /// ([`Namespace::Device`], [`Namespace::DecryptBuffer`], + /// [`Namespace::SentMessage`]). + pub async fn load_aux(&self, ns: Namespace, key: &str) -> Result>> { + Self::check_aux(ns)?; + self.load(ns, key).await + } + + /// Write a record in one of the service's own namespaces. + pub async fn put_aux(&self, ns: Namespace, key: &str, plaintext: &[u8]) -> Result<()> { + Self::check_aux(ns)?; + self.put_one(ns, key, plaintext).await + } + + /// Insert an auxiliary record exclusively, including in a staged batch. + pub async fn insert_aux(&self, ns: Namespace, key: &str, plaintext: &[u8]) -> Result<()> { + Self::check_aux(ns)?; + self.apply(vec![PlainOp::Insert( + ns, + key.to_owned(), + plaintext.to_vec(), + )]) + .await + } + + /// Reserve fresh prekey IDs without overwriting an existing private key. + pub async fn insert_prekeys_batch(&self, keys: &[(u32, Bytes)]) -> Result<()> { + // A repeated ID would replace the first staged insert, so commit would + // succeed with fewer private keys stored than the caller supplied. + let mut seen = std::collections::HashSet::with_capacity(keys.len()); + if let Some((id, _)) = keys.iter().find(|(id, _)| !seen.insert(*id)) { + return Err(StoreError::Validation(format!( + "prekey batch repeats id {id}" + ))); + } + self.apply( + keys.iter() + .map(|(id, record)| { + PlainOp::Insert( + Namespace::PreKey, + id_key(*id), + prekey_plaintext(record, false), + ) + }) + .collect(), + ) + .await + } + + /// Delete records in one of the service's own namespaces. + pub async fn delete_aux(&self, ns: Namespace, keys: &[&str]) -> Result<()> { + Self::check_aux(ns)?; + self.delete_keys(ns, keys).await + } + + /// Keys in one of the service's own namespaces, ascending. + pub async fn scan_aux( + &self, + ns: Namespace, + prefix: &str, + limit: Option, + ) -> Result> { + Self::check_aux(ns)?; + self.scan(ns, prefix, limit).await + } + + /// Peer addresses with a stored session or identity, ascending. Only the + /// keys are read, so callers can find every device of a user. + pub async fn scan_peer_addresses( + &self, + ns: Namespace, + prefix: &str, + limit: Option, + ) -> Result> { + if !matches!(ns, Namespace::Session | Namespace::Identity) { + return Err(StoreError::Validation(format!( + "{} records do not hold peer addresses", + ns.tag() + ))); + } + self.scan(ns, prefix, limit).await + } + + /// The local device's sealed key blob, if the scope has been initialised. + pub async fn load_device(&self) -> Result>> { + self.load(Namespace::Device, DEVICE_KEY).await + } + + /// Write the local device's first key blob. Fails with + /// [`crate::RecordExists`] if the scope already has one, so two concurrent + /// initialisers cannot both succeed. + pub async fn create_device(&self, blob: &[u8]) -> Result<()> { + self.apply(vec![PlainOp::Insert( + Namespace::Device, + DEVICE_KEY.to_owned(), + blob.to_vec(), + )]) + .await + } + + /// Replace the local device's key blob, fenced like every other write. + pub async fn save_device(&self, blob: &[u8]) -> Result<()> { + self.put_one(Namespace::Device, DEVICE_KEY, blob).await + } + + async fn any_key_with_prefix(&self, ns: Namespace, prefix: &str) -> Result { + Ok(!self.scan(ns, prefix, Some(1)).await?.is_empty()) + } +} + +fn seal_error(error: SealError) -> StoreError { + StoreError::Serialization(Box::new(error)) +} + +/// Zero-padded so byte order matches numeric order in every adapter. +fn id_key(id: u32) -> String { + format!("{id:010}") +} + +fn parse_id(key: &str) -> Result { + key.parse() + .map_err(|_| StoreError::Validation(format!("invalid record id key: {key}"))) +} + +fn prekey_plaintext(record: &[u8], uploaded: bool) -> Vec { + let mut plaintext = Vec::with_capacity(record.len() + 1); + plaintext.push(if uploaded { + PREKEY_UPLOADED + } else { + PREKEY_NOT_UPLOADED + }); + plaintext.extend_from_slice(record); + plaintext +} + +fn prekey_record(plaintext: Vec) -> Result { + match plaintext.first() { + Some(&PREKEY_NOT_UPLOADED | &PREKEY_UPLOADED) => Ok(Bytes::from(plaintext).slice(1..)), + _ => Err(StoreError::Validation( + "prekey record has no upload flag".into(), + )), + } +} + +fn identity_key(plaintext: Vec) -> Result<[u8; 32]> { + let len = plaintext.len(); + plaintext + .try_into() + .map_err(|_| StoreError::Validation(format!("Invalid identity key length: {len}"))) +} + +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +impl SignalStore for RecordSignalStore { + async fn put_identity(&self, address: &str, key: [u8; 32]) -> Result<()> { + self.put_one(Namespace::Identity, address, &key).await + } + + async fn put_identities_batch(&self, identities: &[(Arc, [u8; 32])]) -> Result<()> { + self.apply( + identities + .iter() + .map(|(a, k)| PlainOp::Put(Namespace::Identity, a.to_string(), k.to_vec())) + .collect(), + ) + .await + } + + async fn load_identity(&self, address: &str) -> Result> { + self.load(Namespace::Identity, address) + .await? + .map(identity_key) + .transpose() + } + + async fn delete_identity(&self, address: &str) -> Result<()> { + self.delete_keys(Namespace::Identity, &[address]).await + } + + async fn delete_identities_batch(&self, addresses: &[Arc]) -> Result<()> { + self.delete_keys(Namespace::Identity, addresses).await + } + + async fn get_session(&self, address: &str) -> Result> { + Ok(self + .load(Namespace::Session, address) + .await? + .map(Bytes::from)) + } + + async fn put_session(&self, address: &str, session: &[u8]) -> Result<()> { + self.put_one(Namespace::Session, address, session).await + } + + async fn put_sessions_batch(&self, sessions: &[(Arc, Bytes)]) -> Result<()> { + self.apply( + sessions + .iter() + .map(|(a, s)| PlainOp::Put(Namespace::Session, a.to_string(), s.to_vec())) + .collect(), + ) + .await + } + + async fn get_sessions_batch(&self, addresses: &[Arc]) -> Result, Bytes)>> { + let keys: Vec<&str> = addresses.iter().map(|a| &**a).collect(); + let found = self.load_many(Namespace::Session, &keys).await?; + Ok(found + .into_iter() + .map(|(key, value)| { + // Hand back the caller's own Arc where possible. + let address = addresses + .iter() + .find(|a| ***a == *key) + .cloned() + .unwrap_or_else(|| Arc::from(key)); + (address, Bytes::from(value)) + }) + .collect()) + } + + async fn delete_session(&self, address: &str) -> Result<()> { + self.delete_keys(Namespace::Session, &[address]).await + } + + async fn delete_sessions_batch(&self, addresses: &[Arc]) -> Result<()> { + self.delete_keys(Namespace::Session, addresses).await + } + + async fn has_session(&self, address: &str) -> Result { + match self.pending(Namespace::Session, address).await { + Some(Staged::Delete | Staged::InsertDeleted) => Ok(false), + Some(_) => Ok(true), + None => Ok(self + .records + .get(self.scope(), Namespace::Session, address) + .await? + .is_some()), + } + } + + async fn has_signal_state_for_user(&self, user: &str) -> Result { + // Addresses are `user@server` (device 0) or `user:device@server`. + let device_zero = format!("{user}@"); + let other_devices = format!("{user}:"); + for ns in [Namespace::Session, Namespace::Identity] { + if self.any_key_with_prefix(ns, &device_zero).await? + || self.any_key_with_prefix(ns, &other_devices).await? + { + return Ok(true); + } + } + Ok(false) + } + + async fn store_prekey(&self, id: u32, record: &[u8], uploaded: bool) -> Result<()> { + self.put_one( + Namespace::PreKey, + &id_key(id), + &prekey_plaintext(record, uploaded), + ) + .await + } + + async fn store_prekeys_batch(&self, keys: &[(u32, Bytes)], uploaded: bool) -> Result<()> { + self.apply( + keys.iter() + .map(|(id, r)| { + PlainOp::Put( + Namespace::PreKey, + id_key(*id), + prekey_plaintext(r, uploaded), + ) + }) + .collect(), + ) + .await + } + + async fn load_prekey(&self, id: u32) -> Result> { + self.load(Namespace::PreKey, &id_key(id)) + .await? + .map(prekey_record) + .transpose() + } + + async fn load_prekeys_batch(&self, ids: &[u32]) -> Result> { + let keys: Vec = ids.iter().map(|id| id_key(*id)).collect(); + let key_refs: Vec<&str> = keys.iter().map(String::as_str).collect(); + let mut result = Vec::with_capacity(ids.len()); + for (key, plaintext) in self.load_many(Namespace::PreKey, &key_refs).await? { + result.push((parse_id(&key)?, prekey_record(plaintext)?)); + } + Ok(result) + } + + async fn mark_prekeys_uploaded(&self, ids: &[u32]) -> Result<()> { + let keys: Vec = ids.iter().map(|id| id_key(*id)).collect(); + let key_refs: Vec<&str> = keys.iter().map(String::as_str).collect(); + let mut ops = Vec::with_capacity(ids.len()); + for (key, plaintext) in self.load_many(Namespace::PreKey, &key_refs).await? { + let record = prekey_record(plaintext)?; + // Update, not Put: a key consumed after the read above stays + // deleted instead of being resurrected by this write. + ops.push(PlainOp::Update( + Namespace::PreKey, + key, + prekey_plaintext(&record, true), + )); + } + self.apply(ops).await + } + + async fn remove_prekey(&self, id: u32) -> Result<()> { + self.delete_keys(Namespace::PreKey, &[id_key(id)]).await + } + + async fn remove_prekeys_batch(&self, ids: &[u32]) -> Result<()> { + let keys: Vec = ids.iter().map(|id| id_key(*id)).collect(); + self.delete_keys(Namespace::PreKey, &keys).await + } + + async fn get_max_prekey_id(&self) -> Result { + let keys = self.scan(Namespace::PreKey, "", None).await?; + keys.last().map_or(Ok(0), |key| parse_id(key)) + } + + async fn store_signed_prekey(&self, id: u32, record: &[u8]) -> Result<()> { + self.put_one(Namespace::SignedPreKey, &id_key(id), record) + .await + } + + async fn load_signed_prekey(&self, id: u32) -> Result>> { + self.load(Namespace::SignedPreKey, &id_key(id)).await + } + + async fn load_all_signed_prekeys(&self) -> Result)>> { + let keys = self.scan(Namespace::SignedPreKey, "", None).await?; + let key_refs: Vec<&str> = keys.iter().map(String::as_str).collect(); + let mut result = Vec::with_capacity(keys.len()); + for (key, plaintext) in self.load_many(Namespace::SignedPreKey, &key_refs).await? { + result.push((parse_id(&key)?, plaintext)); + } + result.sort_by_key(|(id, _)| *id); + Ok(result) + } + + async fn remove_signed_prekey(&self, id: u32) -> Result<()> { + self.delete_keys(Namespace::SignedPreKey, &[id_key(id)]) + .await + } + + async fn put_sender_key(&self, address: &str, record: &[u8]) -> Result<()> { + self.put_one(Namespace::SenderKey, address, record).await + } + + async fn put_sender_keys_batch(&self, sender_keys: &[(Arc, Bytes)]) -> Result<()> { + self.apply( + sender_keys + .iter() + .map(|(a, r)| PlainOp::Put(Namespace::SenderKey, a.to_string(), r.to_vec())) + .collect(), + ) + .await + } + + async fn get_sender_key(&self, address: &str) -> Result>> { + self.load(Namespace::SenderKey, address).await + } + + async fn delete_sender_key(&self, address: &str) -> Result<()> { + self.delete_keys(Namespace::SenderKey, &[address]).await + } + + async fn delete_sender_keys_batch(&self, addresses: &[Arc]) -> Result<()> { + self.delete_keys(Namespace::SenderKey, addresses).await + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::memory::MemoryRecordStore; + use crate::record::{LeaseStore, is_fence_lost}; + use crate::seal::AesGcmSealer; + use futures::executor::block_on; + + const TTL: u64 = 10_000; + + fn setup() -> ( + Arc, + RecordSignalStore, + ) { + let records = Arc::new(MemoryRecordStore::new()); + let lease = block_on(records.acquire("device-1", "node-a", 0, TTL)) + .unwrap() + .unwrap(); + let store = RecordSignalStore::new( + records.clone(), + Arc::new(AesGcmSealer::new(&[3; 32])), + lease.fence, + ); + (records, store) + } + + /// Delegates to a [`MemoryRecordStore`] and records each scan limit. + #[derive(Default)] + struct ScanSpy { + inner: MemoryRecordStore, + limits: std::sync::Mutex>>, + } + + #[cfg_attr(target_arch = "wasm32", async_trait(?Send))] + #[cfg_attr(not(target_arch = "wasm32"), async_trait)] + impl RecordStore for ScanSpy { + async fn get(&self, scope: &str, ns: Namespace, key: &str) -> Result>> { + self.inner.get(scope, ns, key).await + } + + async fn scan_keys( + &self, + scope: &str, + ns: Namespace, + prefix: &str, + limit: Option, + ) -> Result> { + self.limits.lock().unwrap().push(limit); + self.inner.scan_keys(scope, ns, prefix, limit).await + } + + async fn write(&self, fence: &Fence, ops: &[WriteOp]) -> Result<()> { + self.inner.write(fence, ops).await + } + } + + #[test] + fn a_limited_staged_scan_stays_bounded_and_skips_pending_deletes() { + let records = Arc::new(ScanSpy::default()); + let lease = block_on(records.inner.acquire("device-1", "node-a", 0, TTL)) + .unwrap() + .unwrap(); + let store = RecordSignalStore::new( + records.clone(), + Arc::new(AesGcmSealer::new(&[3; 32])), + lease.fence, + ); + for key in ["s1", "s2", "s3", "s4", "s5", "s6"] { + block_on(store.put_session(key, b"v")).unwrap(); + } + let staged = store.staged(); + block_on(staged.delete_session("s1")).unwrap(); + block_on(staged.delete_session("s2")).unwrap(); + block_on(staged.put_session("s25", b"v")).unwrap(); + let keys = block_on(staged.scan(Namespace::Session, "s", Some(3))).unwrap(); + assert_eq!(keys, ["s25", "s3", "s4"]); + assert_eq!(records.limits.lock().unwrap().as_slice(), [Some(5)]); + } + + #[test] + fn a_prekey_batch_with_a_repeated_id_is_refused_whole() { + let (_, store) = setup(); + let keys = [ + (7, Bytes::from_static(b"a")), + (8, Bytes::from_static(b"b")), + (7, Bytes::from_static(b"c")), + ]; + assert!(matches!( + block_on(store.insert_prekeys_batch(&keys)), + Err(StoreError::Validation(_)) + )); + assert_eq!(block_on(store.load_prekey(7)).unwrap(), None); + assert_eq!(block_on(store.load_prekey(8)).unwrap(), None); + } + + #[test] + fn repeated_staged_prekey_insert_refuses_the_whole_later_batch() { + let (_, store) = setup(); + let staged = store.staged(); + block_on(staged.insert_prekeys_batch(&[(7, Bytes::from_static(b"first"))])).unwrap(); + let error = block_on(staged.insert_prekeys_batch(&[ + (8, Bytes::from_static(b"must-not-stage")), + (7, Bytes::from_static(b"replacement")), + ])) + .unwrap_err(); + assert!(crate::is_record_exists(&error)); + assert_eq!( + block_on(staged.load_prekey(7)).unwrap(), + Some(Bytes::from_static(b"first")) + ); + assert_eq!(block_on(staged.load_prekey(8)).unwrap(), None); + block_on(staged.commit()).unwrap(); + assert_eq!( + block_on(store.load_prekey(7)).unwrap(), + Some(Bytes::from_static(b"first")) + ); + assert_eq!(block_on(store.load_prekey(8)).unwrap(), None); + } + + #[test] + fn a_later_staged_put_preserves_the_exclusive_insert_condition() { + let (_, store) = setup(); + let staged = store.staged(); + block_on(staged.insert_prekeys_batch(&[(7, Bytes::from_static(b"first"))])).unwrap(); + block_on(staged.store_prekey(7, b"updated", false)).unwrap(); + block_on(store.store_prekey(7, b"independent", false)).unwrap(); + let error = block_on(staged.commit()).unwrap_err(); + assert!(crate::is_record_exists(&error)); + let retry = block_on(staged.commit()).unwrap_err(); + assert!(crate::is_record_exists(&retry)); + assert_eq!( + block_on(staged.load_prekey(7)).unwrap(), + Some(Bytes::from_static(b"updated")) + ); + assert_eq!( + block_on(store.load_prekey(7)).unwrap(), + Some(Bytes::from_static(b"independent")) + ); + } + + #[test] + fn staged_deletes_and_later_writes_keep_the_original_insert_condition() { + for put_after_delete in [false, true] { + let (_, store) = setup(); + let staged = store.staged(); + block_on(staged.insert_prekeys_batch(&[(7, Bytes::from_static(b"first"))])).unwrap(); + block_on(staged.remove_prekey(7)).unwrap(); + assert_eq!(block_on(staged.load_prekey(7)).unwrap(), None); + if put_after_delete { + block_on(staged.store_prekey(7, b"later", false)).unwrap(); + } + block_on(staged.put_aux(Namespace::DecryptBuffer, "unrelated", b"must-not-commit")) + .unwrap(); + block_on(store.store_prekey(7, b"independent", false)).unwrap(); + for _ in 0..2 { + let error = block_on(staged.commit()).unwrap_err(); + assert!(crate::is_record_exists(&error)); + assert_eq!( + block_on(store.load_prekey(7)).unwrap(), + Some(Bytes::from_static(b"independent")) + ); + assert_eq!( + block_on(store.load_aux(Namespace::DecryptBuffer, "unrelated")).unwrap(), + None + ); + } + } + } + + #[test] + fn inserting_then_deleting_an_absent_key_commits_without_leaving_a_record() { + let (_, store) = setup(); + let staged = store.staged(); + block_on(staged.insert_prekeys_batch(&[(7, Bytes::from_static(b"first"))])).unwrap(); + block_on(staged.remove_prekey(7)).unwrap(); + block_on(staged.put_aux(Namespace::DecryptBuffer, "other", b"committed")).unwrap(); + block_on(staged.commit()).unwrap(); + assert_eq!(block_on(store.load_prekey(7)).unwrap(), None); + assert_eq!( + block_on(store.load_aux(Namespace::DecryptBuffer, "other")).unwrap(), + Some(b"committed".to_vec()) + ); + } + + struct PausedSealer { + paused: std::sync::atomic::AtomicBool, + inner: AesGcmSealer, + } + + #[cfg_attr(target_arch = "wasm32", async_trait(?Send))] + #[cfg_attr(not(target_arch = "wasm32"), async_trait)] + impl Sealer for PausedSealer { + async fn seal( + &self, + aad: &[u8], + plaintext: &[u8], + ) -> std::result::Result, SealError> { + if self.paused.load(std::sync::atomic::Ordering::SeqCst) { + futures::future::pending::<()>().await; + } + self.inner.seal(aad, plaintext).await + } + + async fn open(&self, aad: &[u8], sealed: &[u8]) -> std::result::Result, SealError> { + self.inner.open(aad, sealed).await + } + } + + #[test] + fn cancelling_a_commit_before_submission_keeps_its_pending_writes() { + use std::future::Future; + use std::task::{Context, Poll, Waker}; + let records = Arc::new(MemoryRecordStore::new()); + let lease = block_on(records.acquire("cancelled", "node", 0, TTL)) + .unwrap() + .unwrap(); + let sealer = Arc::new(PausedSealer { + paused: std::sync::atomic::AtomicBool::new(true), + inner: AesGcmSealer::new(&[3; 32]), + }); + let store = RecordSignalStore::new(records, sealer.clone(), lease.fence); + let staged = store.staged(); + block_on(staged.save_device(b"pending-private-state")).unwrap(); + let mut commit = Box::pin(staged.commit()); + assert!(matches!( + commit + .as_mut() + .poll(&mut Context::from_waker(Waker::noop())), + Poll::Pending + )); + drop(commit); + assert_eq!( + block_on(staged.load_device()).unwrap(), + Some(b"pending-private-state".to_vec()) + ); + assert_eq!(block_on(store.load_device()).unwrap(), None); + sealer + .paused + .store(false, std::sync::atomic::Ordering::SeqCst); + block_on(staged.commit()).unwrap(); + assert_eq!( + block_on(store.load_device()).unwrap(), + Some(b"pending-private-state".to_vec()) + ); + } + + #[test] + fn device_blob_round_trips_sealed_and_fenced() { + let (records, store) = setup(); + assert_eq!(block_on(store.load_device()).unwrap(), None); + block_on(store.save_device(b"identity-private")).unwrap(); + assert_eq!( + block_on(store.load_device()).unwrap().as_deref(), + Some(&b"identity-private"[..]) + ); + let raw = block_on(records.get("device-1", Namespace::Device, "local")) + .unwrap() + .unwrap(); + assert!(!raw.windows(16).any(|w| w == b"identity-private")); + } + + #[test] + fn staged_writes_stay_private_until_commit() { + let (records, store) = setup(); + block_on(store.put_session("keep@s.whatsapp.net", b"old")).unwrap(); + block_on(store.store_prekey(5, b"five", false)).unwrap(); + + let staged = store.staged(); + block_on(staged.put_session("keep@s.whatsapp.net", b"new")).unwrap(); + block_on(staged.put_session("fresh@s.whatsapp.net", b"s")).unwrap(); + block_on(staged.remove_prekey(5)).unwrap(); + block_on(staged.put_aux(Namespace::DecryptBuffer, "h1", b"result")).unwrap(); + + // The view reads its own writes. + assert_eq!( + block_on(staged.get_session("keep@s.whatsapp.net")) + .unwrap() + .as_deref(), + Some(&b"new"[..]) + ); + assert_eq!(block_on(staged.load_prekey(5)).unwrap(), None); + assert_eq!(block_on(staged.get_max_prekey_id()).unwrap(), 0); + assert!(block_on(staged.has_signal_state_for_user("fresh")).unwrap()); + + // Nothing reached the store yet. + assert_eq!( + block_on(store.get_session("keep@s.whatsapp.net")) + .unwrap() + .as_deref(), + Some(&b"old"[..]) + ); + assert!( + block_on(records.get("device-1", Namespace::DecryptBuffer, "h1")) + .unwrap() + .is_none() + ); + + block_on(staged.commit()).unwrap(); + assert_eq!( + block_on(store.get_session("keep@s.whatsapp.net")) + .unwrap() + .as_deref(), + Some(&b"new"[..]) + ); + assert_eq!(block_on(store.load_prekey(5)).unwrap(), None); + assert_eq!( + block_on(store.load_aux(Namespace::DecryptBuffer, "h1")) + .unwrap() + .as_deref(), + Some(&b"result"[..]) + ); + } + + #[test] + fn a_stale_staged_commit_applies_nothing() { + let (records, store) = setup(); + let staged = store.staged(); + block_on(staged.put_session("a@s.whatsapp.net", b"s")).unwrap(); + block_on(records.acquire("device-1", "node-b", TTL, TTL)) + .unwrap() + .unwrap(); + let err = block_on(staged.commit()).unwrap_err(); + assert!(is_fence_lost(&err)); + assert!( + block_on(records.get("device-1", Namespace::Session, "a@s.whatsapp.net")) + .unwrap() + .is_none() + ); + } + + #[test] + fn signal_namespaces_are_not_reachable_through_the_aux_api() { + let (_, store) = setup(); + assert!(block_on(store.put_aux(Namespace::Session, "x", b"v")).is_err()); + assert!(block_on(store.load_aux(Namespace::Identity, "x")).is_err()); + block_on(store.put_aux(Namespace::SentMessage, "chat\0id", b"m")).unwrap(); + assert_eq!( + block_on(store.scan_aux(Namespace::SentMessage, "chat", None)).unwrap(), + vec!["chat\0id".to_owned()] + ); + } + + #[test] + fn slots_with_shifted_separators_get_distinct_aad() { + let records = Arc::new(MemoryRecordStore::new()); + let sealer = Arc::new(AesGcmSealer::new(&[3; 32])); + let fence = |scope: &str| Fence { + scope: Arc::from(scope), + generation: 1, + }; + let a = RecordSignalStore::new(records.clone(), sealer.clone(), fence("a")); + let b = RecordSignalStore::new(records, sealer, fence("a\0session\0b")); + assert_ne!( + a.aad(Namespace::Session, "b\0session\0c"), + b.aad(Namespace::Session, "c") + ); + } + + #[test] + fn values_are_sealed_at_rest() { + let (records, store) = setup(); + block_on(store.put_session("alice@s.whatsapp.net", b"ratchet-state")).unwrap(); + let raw = block_on(records.get("device-1", Namespace::Session, "alice@s.whatsapp.net")) + .unwrap() + .unwrap(); + assert!( + !raw.windows(b"ratchet-state".len()) + .any(|w| w == b"ratchet-state"), + "plaintext reached the record store" + ); + assert_eq!( + block_on(store.get_session("alice@s.whatsapp.net")) + .unwrap() + .as_deref(), + Some(&b"ratchet-state"[..]) + ); + } + + #[test] + fn a_record_copied_to_another_address_fails_to_open() { + let (records, store) = setup(); + block_on(store.put_session("alice@s.whatsapp.net", b"alice-state")).unwrap(); + let raw = block_on(records.get("device-1", Namespace::Session, "alice@s.whatsapp.net")) + .unwrap() + .unwrap(); + block_on(records.write( + store.fence(), + &[WriteOp::Put { + ns: Namespace::Session, + key: "mallory@s.whatsapp.net".into(), + value: raw, + }], + )) + .unwrap(); + assert!(block_on(store.get_session("mallory@s.whatsapp.net")).is_err()); + } + + #[test] + fn a_superseded_node_cannot_write() { + let (records, stale) = setup(); + let takeover = block_on(records.acquire("device-1", "node-b", TTL, TTL)) + .unwrap() + .unwrap(); + let fresh = RecordSignalStore::new( + records.clone(), + Arc::new(AesGcmSealer::new(&[3; 32])), + takeover.fence, + ); + let err = block_on(stale.put_session("alice@s.whatsapp.net", b"old")).unwrap_err(); + assert!(is_fence_lost(&err)); + block_on(fresh.put_session("alice@s.whatsapp.net", b"new")).unwrap(); + assert_eq!( + block_on(fresh.get_session("alice@s.whatsapp.net")) + .unwrap() + .as_deref(), + Some(&b"new"[..]) + ); + } + + #[test] + fn identities_round_trip_and_reject_bad_lengths() { + let (_, store) = setup(); + block_on(store.put_identity("bob@s.whatsapp.net", [9; 32])).unwrap(); + assert_eq!( + block_on(store.load_identity("bob@s.whatsapp.net")).unwrap(), + Some([9; 32]) + ); + assert_eq!( + block_on(store.load_identity("nobody@s.whatsapp.net")).unwrap(), + None + ); + } + + #[test] + fn prekeys_keep_numeric_order_and_never_resurrect() { + let (_, store) = setup(); + let keys = vec![ + (9, Bytes::from_static(b"nine")), + (10, Bytes::from_static(b"ten")), + (100, Bytes::from_static(b"hundred")), + ]; + block_on(store.store_prekeys_batch(&keys, false)).unwrap(); + assert_eq!(block_on(store.uploaded_prekey_count()).unwrap(), 0); + assert_eq!(block_on(store.get_max_prekey_id()).unwrap(), 100); + + block_on(store.remove_prekey(10)).unwrap(); + block_on(store.mark_prekeys_uploaded(&[9, 10])).unwrap(); + assert_eq!(block_on(store.uploaded_prekey_count()).unwrap(), 1); + assert_eq!(block_on(store.load_prekey(10)).unwrap(), None); + assert_eq!( + block_on(store.load_prekey(9)).unwrap().as_deref(), + Some(&b"nine"[..]) + ); + + let mut loaded = block_on(store.load_prekeys_batch(&[9, 10, 100])).unwrap(); + loaded.sort_by_key(|(id, _)| *id); + assert_eq!( + loaded, + vec![ + (9, Bytes::from_static(b"nine")), + (100, Bytes::from_static(b"hundred")) + ] + ); + } + + #[test] + fn signed_prekeys_list_in_order() { + let (_, store) = setup(); + block_on(store.store_signed_prekey(12, b"twelve")).unwrap(); + block_on(store.store_signed_prekey(2, b"two")).unwrap(); + assert_eq!( + block_on(store.load_all_signed_prekeys()).unwrap(), + vec![(2, b"two".to_vec()), (12, b"twelve".to_vec())] + ); + block_on(store.remove_signed_prekey(2)).unwrap(); + assert_eq!(block_on(store.load_signed_prekey(2)).unwrap(), None); + } + + #[test] + fn deleting_and_reinserting_a_stored_key_in_one_view_replaces_it() { + let (_, store) = setup(); + block_on(store.put_aux(Namespace::SentMessage, "k", b"old")).unwrap(); + let staged = store.staged(); + block_on(staged.delete_aux(Namespace::SentMessage, &["k"])).unwrap(); + block_on(staged.insert_aux(Namespace::SentMessage, "k", b"new")).unwrap(); + block_on(staged.commit()).unwrap(); + assert_eq!( + block_on(store.load_aux(Namespace::SentMessage, "k")).unwrap(), + Some(b"new".to_vec()) + ); + } + + #[test] + fn signal_state_lookup_matches_whole_user_ids() { + let (_, store) = setup(); + block_on(store.put_session("123:4@s.whatsapp.net", b"s")).unwrap(); + assert!(block_on(store.has_signal_state_for_user("123")).unwrap()); + assert!(!block_on(store.has_signal_state_for_user("12")).unwrap()); + assert!(!block_on(store.has_signal_state_for_user("1234")).unwrap()); + } + + #[test] + fn batches_and_sender_keys_round_trip() { + let (_, store) = setup(); + let a: Arc = Arc::from("a@s.whatsapp.net"); + let b: Arc = Arc::from("b@s.whatsapp.net"); + block_on(store.put_sessions_batch(&[ + (a.clone(), Bytes::from_static(b"sa")), + (b.clone(), Bytes::from_static(b"sb")), + ])) + .unwrap(); + let mut found = block_on(store.get_sessions_batch(&[a.clone(), b.clone()])).unwrap(); + found.sort_by(|x, y| x.0.cmp(&y.0)); + assert_eq!(found.len(), 2); + block_on(store.delete_sessions_batch(std::slice::from_ref(&a))).unwrap(); + assert!(!block_on(store.has_session(&a)).unwrap()); + assert!(block_on(store.has_session(&b)).unwrap()); + + block_on(store.put_sender_key("group:me", b"sk")).unwrap(); + assert_eq!( + block_on(store.get_sender_key("group:me")).unwrap(), + Some(b"sk".to_vec()) + ); + block_on(store.delete_sender_key("group:me")).unwrap(); + assert_eq!(block_on(store.get_sender_key("group:me")).unwrap(), None); + } +} diff --git a/crates/signal-ops/Cargo.toml b/crates/signal-ops/Cargo.toml new file mode 100644 index 0000000..b562b73 --- /dev/null +++ b/crates/signal-ops/Cargo.toml @@ -0,0 +1,34 @@ +[package] +name = "wacore-signal-ops" +version = "0.1.0" +edition = "2024" +rust-version.workspace = true +license = "MIT" +repository = "https://github.com/polymorfa/signal-node" +description = "Signal operations for a WhatsApp linked device whose keys live outside the client" +publish = false + +[dependencies] +anyhow = { workspace = true } +async-lock = { workspace = true } +async-trait = { workspace = true } +bytes = { workspace = true } +hex = { workspace = true } +log = { workspace = true } +rand = { workspace = true } +serde = { workspace = true } +sha2 = { workspace = true } +thiserror = { workspace = true } +wacore = { workspace = true } +wacore-binary = { workspace = true } +wacore-recordstore = { path = "../recordstore" } +waproto = { workspace = true } + +[dev-dependencies] +anyhow = { workspace = true } +tokio = { workspace = true, features = ["macros", "rt", "rt-multi-thread", "sync", "time"] } +buffa = { workspace = true } +futures = { workspace = true, features = ["executor"] } +hmac = { workspace = true } +sha2 = { workspace = true } +wacore = { workspace = true, features = ["test-util"] } diff --git a/crates/signal-ops/src/device.rs b/crates/signal-ops/src/device.rs new file mode 100644 index 0000000..4d7ac3f --- /dev/null +++ b/crates/signal-ops/src/device.rs @@ -0,0 +1,282 @@ +// SPDX-License-Identifier: MIT + +use crate::error::OpsError; +use rand::{Rng, RngExt}; +use wacore::libsignal::protocol::{IdentityKeyPair, KeyPair, PrivateKey, PublicKey}; + +/// The local device's own keys. Private halves never leave the Signal store. +#[derive(Clone)] +pub struct DeviceKeys { + pub identity: KeyPair, + pub registration_id: u32, + pub adv_secret: [u8; 32], + pub signed_prekey_id: u32, + pub signed_prekey: KeyPair, + pub signed_prekey_signature: [u8; 64], + /// Next one-time prekey ID to hand out. WhatsApp IDs are 24-bit. + pub next_prekey_id: u32, + /// Encoded `ADVSignedDeviceIdentity` from pairing. Sent with prekey + /// messages so recipients can verify this device belongs to the account. + pub account: Option>, + /// This device's phone-number JID (`user:device@s.whatsapp.net`). + pub own_pn: Option, + /// This device's LID (`user:device@lid`). + pub own_lid: Option, +} + +/// Everything the WhatsApp client needs to register and pair the device. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct PublicIdentity { + pub registration_id: u32, + /// 32-byte Curve25519 public key, without the type byte. + pub identity_key: [u8; 32], + pub signed_prekey: PublicSignedPreKey, + /// Shared with the primary through the QR code; an HMAC key for pairing, + /// not a decryption key. + pub adv_secret: [u8; 32], +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct PublicSignedPreKey { + pub id: u32, + pub public_key: [u8; 32], + pub signature: [u8; 64], +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct PublicPreKey { + pub id: u32, + pub public_key: [u8; 32], +} + +const FORMAT_V1: u8 = 1; +const FORMAT_V2: u8 = 2; +/// Fixed part shared by both formats; format 2 appends the paired identity. +const FIXED_LEN: usize = 1 + 32 + 4 + 32 + 4 + 32 + 64 + 4; +/// One-time prekey IDs are 3 bytes on the wire. +pub(crate) const MAX_PREKEY_ID: u32 = 0x00FF_FFFF; + +fn public32(key: &PublicKey) -> [u8; 32] { + let mut out = [0u8; 32]; + out.copy_from_slice(key.public_key_bytes()); + out +} + +impl DeviceKeys { + /// Fresh keys, generated the way wacore's own `Device::new` does. + pub fn generate() -> Result { + let mut rng = rand::make_rng::(); + let identity_pair = IdentityKeyPair::generate(&mut rng); + let identity = KeyPair::new( + *identity_pair.public_key(), + identity_pair.private_key().clone(), + ); + let signed_prekey = KeyPair::generate(&mut rng); + let signed_prekey_signature = sign_prekey(&identity, &signed_prekey)?; + let mut adv_secret = [0u8; 32]; + rng.fill_bytes(&mut adv_secret); + Ok(Self { + identity, + registration_id: rng.random_range(1..=2_147_483_647), + adv_secret, + signed_prekey_id: 1, + signed_prekey, + signed_prekey_signature, + next_prekey_id: 1, + account: None, + own_pn: None, + own_lid: None, + }) + } + + pub fn identity_pair(&self) -> IdentityKeyPair { + self.identity.clone().into() + } + + pub fn public(&self) -> PublicIdentity { + PublicIdentity { + registration_id: self.registration_id, + identity_key: public32(&self.identity.public_key), + signed_prekey: PublicSignedPreKey { + id: self.signed_prekey_id, + public_key: public32(&self.signed_prekey.public_key), + signature: self.signed_prekey_signature, + }, + adv_secret: self.adv_secret, + } + } + + /// Layout, format 2: version, identity private, registration ID, ADV + /// secret, signed prekey ID, signed prekey private, signature, next prekey + /// ID, then three length-prefixed fields (u32 account, u16 own PN, u16 own + /// LID; length 0 means absent). Integers are big-endian. Public keys are + /// derived on load. Format 1 is the fixed part alone and still decodes. + /// Encode for storage. Refuses a field too long for its length prefix + /// (the account blob over `u32::MAX` bytes, a JID over `u16::MAX`), since + /// a truncated prefix would make the stored record undecodable. + pub fn encode(&self) -> Result, OpsError> { + let too_long = |what: &str| OpsError::InvalidInput(format!("device keys: {what} too long")); + let mut out = Vec::with_capacity(FIXED_LEN + 8); + out.push(FORMAT_V2); + out.extend_from_slice(self.identity.private_key.serialize()); + out.extend_from_slice(&self.registration_id.to_be_bytes()); + out.extend_from_slice(&self.adv_secret); + out.extend_from_slice(&self.signed_prekey_id.to_be_bytes()); + out.extend_from_slice(self.signed_prekey.private_key.serialize()); + out.extend_from_slice(&self.signed_prekey_signature); + out.extend_from_slice(&self.next_prekey_id.to_be_bytes()); + let account = self.account.as_deref().unwrap_or_default(); + let account_len = u32::try_from(account.len()).map_err(|_| too_long("account"))?; + out.extend_from_slice(&account_len.to_be_bytes()); + out.extend_from_slice(account); + for jid in [&self.own_pn, &self.own_lid] { + let jid = jid.as_deref().unwrap_or_default().as_bytes(); + let jid_len = u16::try_from(jid.len()).map_err(|_| too_long("JID"))?; + out.extend_from_slice(&jid_len.to_be_bytes()); + out.extend_from_slice(jid); + } + Ok(out) + } + + pub fn decode(bytes: &[u8]) -> Result { + let invalid = |what: &str| OpsError::InvalidInput(format!("device keys: {what}")); + let format = *bytes.first().ok_or_else(|| invalid("empty"))?; + match format { + FORMAT_V1 if bytes.len() == FIXED_LEN => {} + FORMAT_V2 if bytes.len() >= FIXED_LEN => {} + FORMAT_V1 | FORMAT_V2 => return Err(invalid("wrong length")), + _ => return Err(invalid("unsupported format")), + } + let mut at = 1; + let mut take = |len: usize| -> Result<&[u8], OpsError> { + let slice = bytes + .get(at..at + len) + .ok_or_else(|| invalid("truncated"))?; + at += len; + Ok(slice) + }; + let key_pair = |private: &[u8]| -> Result { + let private = PrivateKey::deserialize(private).map_err(|_| invalid("private key"))?; + let public = private.public_key().map_err(|_| invalid("public key"))?; + Ok(KeyPair::new(public, private)) + }; + let u32_at = |s: &[u8]| u32::from_be_bytes([s[0], s[1], s[2], s[3]]); + + let identity = key_pair(take(32)?)?; + let registration_id = u32_at(take(4)?); + let adv_secret: [u8; 32] = take(32)?.try_into().map_err(|_| invalid("adv secret"))?; + let signed_prekey_id = u32_at(take(4)?); + let signed_prekey = key_pair(take(32)?)?; + let signed_prekey_signature: [u8; 64] = + take(64)?.try_into().map_err(|_| invalid("signature"))?; + let next_prekey_id = u32_at(take(4)?); + let (mut account, mut own_pn, mut own_lid) = (None, None, None); + if format == FORMAT_V2 { + let len = u32_at(take(4)?) as usize; + account = Some(take(len)?.to_vec()).filter(|a| !a.is_empty()); + let mut jid = || -> Result, OpsError> { + let len_bytes = take(2)?; + let len = u16::from_be_bytes([len_bytes[0], len_bytes[1]]) as usize; + let raw = take(len)?; + let text = std::str::from_utf8(raw).map_err(|_| invalid("jid is not UTF-8"))?; + Ok(Some(text.to_owned()).filter(|j| !j.is_empty())) + }; + own_pn = jid()?; + own_lid = jid()?; + } + if at != bytes.len() { + return Err(invalid("trailing bytes")); + } + Ok(Self { + identity, + registration_id, + adv_secret, + signed_prekey_id, + signed_prekey, + signed_prekey_signature, + next_prekey_id, + account, + own_pn, + own_lid, + }) + } +} + +/// WhatsApp signs the serialized (type-prefixed) signed prekey public key. +pub(crate) fn sign_prekey(identity: &KeyPair, prekey: &KeyPair) -> Result<[u8; 64], OpsError> { + let mut rng = rand::make_rng::(); + let signature = identity + .private_key + .calculate_signature(&prekey.public_key.serialize(), &mut rng) + .map_err(|e| OpsError::InvalidInput(format!("signing the prekey failed: {e}")))?; + signature + .as_ref() + .try_into() + .map_err(|_| OpsError::InvalidInput("signature is not 64 bytes".into())) +} + +pub(crate) fn public_bytes(key: &PublicKey) -> [u8; 32] { + public32(key) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn keys_round_trip_through_the_encoding() { + let keys = DeviceKeys::generate().unwrap(); + let decoded = DeviceKeys::decode(&keys.encode().unwrap()).unwrap(); + assert_eq!(decoded.public(), keys.public()); + assert_eq!(decoded.next_prekey_id, keys.next_prekey_id); + assert_eq!( + decoded.identity.private_key.serialize(), + keys.identity.private_key.serialize() + ); + } + + #[test] + fn the_signed_prekey_signature_verifies_against_the_identity() { + let keys = DeviceKeys::generate().unwrap(); + assert!(keys.identity.public_key.verify_signature( + &keys.signed_prekey.public_key.serialize(), + &keys.signed_prekey_signature + )); + } + + #[test] + fn a_field_too_long_for_its_length_prefix_is_refused() { + let mut keys = DeviceKeys::generate().unwrap(); + keys.own_pn = Some("1".repeat(usize::from(u16::MAX) + 1)); + assert!(matches!(keys.encode(), Err(OpsError::InvalidInput(_)))); + keys.own_pn = Some("1".repeat(usize::from(u16::MAX))); + let decoded = DeviceKeys::decode(&keys.encode().unwrap()).unwrap(); + assert_eq!(decoded.own_pn, keys.own_pn); + } + + #[test] + fn the_paired_identity_round_trips_and_format_1_still_decodes() { + let mut keys = DeviceKeys::generate().unwrap(); + keys.account = Some(vec![1, 2, 3]); + keys.own_pn = Some("15550001111:4@s.whatsapp.net".into()); + keys.own_lid = Some("123456789:4@lid".into()); + let decoded = DeviceKeys::decode(&keys.encode().unwrap()).unwrap(); + assert_eq!(decoded.account, keys.account); + assert_eq!(decoded.own_pn, keys.own_pn); + assert_eq!(decoded.own_lid, keys.own_lid); + + let mut v1 = keys.encode().unwrap()[..FIXED_LEN].to_vec(); + v1[0] = FORMAT_V1; + let old = DeviceKeys::decode(&v1).unwrap(); + assert_eq!(old.public(), keys.public()); + assert_eq!(old.account, None); + } + + #[test] + fn corrupt_encodings_are_rejected() { + let mut bytes = DeviceKeys::generate().unwrap().encode().unwrap(); + assert!(DeviceKeys::decode(&bytes[..10]).is_err()); + bytes[0] = 9; + assert!(DeviceKeys::decode(&bytes).is_err()); + } +} diff --git a/crates/signal-ops/src/error.rs b/crates/signal-ops/src/error.rs new file mode 100644 index 0000000..5621210 --- /dev/null +++ b/crates/signal-ops/src/error.rs @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: MIT + +use wacore::libsignal::protocol::SignalProtocolError; +use wacore::store::error::StoreError; + +#[derive(Debug, thiserror::Error)] +#[error("a Signal operation was interrupted; acquire a new fenced instance")] +struct InterruptedOperation; + +pub(crate) fn interrupted_operation() -> OpsError { + StoreError::Database(Box::new(InterruptedOperation)).into() +} + +fn state_is_refused(error: &StoreError) -> bool { + wacore_recordstore::is_fence_lost(error) + || matches!(error, StoreError::Database(source) if source.is::()) +} + +#[derive(Debug, thiserror::Error)] +pub enum OpsError { + #[error("signal protocol error")] + Signal(#[from] SignalProtocolError), + #[error("store error")] + Store(#[from] StoreError), + /// The caller sent something this operation cannot accept. + #[error("invalid input: {0}")] + InvalidInput(String), + /// Pairing was refused. `code` and `text` are the values WhatsApp Web + /// answers the server with for the same failure. + #[error("pairing refused: {code} {text}")] + Pairing { code: u16, text: &'static str }, + /// Building an outbound stanza failed, including resolver (device list, + /// prekey fetch) failures reported by the WhatsApp client. + #[error("send failed: {0}")] + Send(String), + /// A peer's prekey bundle did not verify against its pinned account + /// identity; see [`crate::BundleRejection`]. + #[error("prekey bundle refused: {0}")] + BundleRefused(crate::trust::BundleRejection), + /// The scope has no device keys yet; call `SignalOps::create`. + #[error("device keys are not initialised for this scope")] + NotInitialised, +} + +impl OpsError { + /// The state must no longer be served: its lease moved, or cancellation + /// left a write with uncertain completion. Release the lease and open a + /// new instance at a higher fence before resuming operations. + pub fn is_fence_lost(&self) -> bool { + match self { + OpsError::Store(e) => state_is_refused(e), + OpsError::Signal(SignalProtocolError::BackendError(_, source)) => source + .downcast_ref::() + .is_some_and(state_is_refused), + _ => false, + } + } +} diff --git a/crates/signal-ops/src/guard.rs b/crates/signal-ops/src/guard.rs new file mode 100644 index 0000000..15ac4a0 --- /dev/null +++ b/crates/signal-ops/src/guard.rs @@ -0,0 +1,70 @@ +// SPDX-License-Identifier: MIT + +//! A cancelled state-changing operation retires its instance before unlocking. + +use crate::OpsError; +use std::future::Future; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; + +pub(crate) struct Held { + // Dropped after our Drop body, so no waiter can acquire these locks before + // a cancelled operation has made the instance unusable. + _guards: Vec>, + usable: Arc, + completed: bool, +} + +impl Held { + pub(crate) fn new(guards: Vec>, usable: Arc) -> Self { + Self { + _guards: guards, + usable, + completed: false, + } + } + + /// Run `operation` under these locks. A completed operation returns its + /// own result even if another operation retired the instance meanwhile: + /// its writes are committed under locks it still holds, and discarding a + /// decrypt's plaintext would lose a message whose keys are already gone. + /// Retirement stops later operations at lock acquisition instead. + pub(crate) async fn run( + mut self, + operation: impl Future>, + ) -> Result { + let result = operation.await; + self.completed = true; + result + } +} + +impl Drop for Held { + fn drop(&mut self) { + if !self.completed { + self.usable.store(false, Ordering::SeqCst); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_completed_operation_keeps_its_result_after_another_retires_the_instance() { + let usable = Arc::new(AtomicBool::new(true)); + let running = Held::new(Vec::new(), usable.clone()); + let cancelled = Held::new(Vec::new(), usable.clone()); + let result = futures::executor::block_on(running.run(async { + // Another operation is dropped before it completes. + drop(cancelled); + Ok::<_, OpsError>("committed plaintext") + })); + assert_eq!(result.ok(), Some("committed plaintext")); + assert!( + !usable.load(Ordering::SeqCst), + "the instance is still retired for later operations" + ); + } +} diff --git a/crates/signal-ops/src/inbound.rs b/crates/signal-ops/src/inbound.rs new file mode 100644 index 0000000..4580c22 --- /dev/null +++ b/crates/signal-ops/src/inbound.rs @@ -0,0 +1,418 @@ +// SPDX-License-Identifier: MIT + +//! Buffered, idempotent receive for the Signal service. +//! +//! A decrypt advances the ratchet, so the same ciphertext cannot be decrypted +//! twice. Each result is kept in the sealed decrypt buffer, written in the +//! same batch as the ratchet advance, until the app confirms delivery with +//! [`SignalOps::mark_delivered`]. A redelivered ciphertext returns the +//! buffered result; once delivered it returns [`Received::AlreadyDelivered`]. + +use crate::error::OpsError; +use crate::ops::{EncKind, ServiceStore, SignalOps, account_lock, chain_key, session_key}; +use sha2::{Digest, Sha256}; +use std::sync::Arc; +use wacore::libsignal::protocol::{ProtocolAddress, SignalProtocolError}; +use wacore::message_processing::{DecryptedMessageResult, process_decrypted_plaintext}; +use wacore_recordstore::Namespace; +use waproto::whatsapp as wa; + +const STATE_PENDING: u8 = 1; +const STATE_DELIVERED: u8 = 2; +const HEADER_LEN: usize = 1 + 1 + 1 + 8; + +/// The `` of a received payload. +#[derive(Debug, Clone, Copy, PartialEq, Eq, wacore::WireEnum)] +pub enum ReceiveKind { + #[wire = "pkmsg"] + PreKey, + #[wire = "msg"] + Message, + /// Group sender-key message for the request's chat. + #[wire = "skmsg"] + SenderKey, +} + +/// One `` payload from a `` stanza. +#[derive(Debug, Clone, Copy)] +pub struct ReceiveRequest<'a> { + /// The stanza's chat JID. For group traffic this is the group. + pub chat: &'a str, + /// The sending device's Signal address. + pub sender: &'a ProtocolAddress, + pub kind: ReceiveKind, + pub ciphertext: &'a [u8], + /// The `` padding version. + pub padding_version: u8, + /// The client's claim that the sender is one of this account's devices. + /// The service derives this from the sender and the stored own JIDs; a + /// claim the sender does not support is refused with `InvalidInput`. + pub is_from_me: bool, + /// The stanza's ``, which a prekey message from a + /// companion device carries. A prekey message whose sender does not + /// verify against its pinned account is refused before decryption. + pub device_identity: Option<&'a [u8]>, +} + +#[derive(Debug)] +pub enum Received { + Message(Box), + /// Delivered before; the client only needs to send its receipt again. + AlreadyDelivered { + receipt_key: String, + }, +} + +#[derive(Debug)] +pub struct ReceivedMessage { + /// Pass to [`SignalOps::mark_delivered`] once the app has the message. + pub receipt_key: String, + /// Unpadded content with own-device copies unwrapped, plus its sender-key + /// distribution and protocol parts. + pub content: DecryptedMessageResult, + pub identity_changed: bool, + /// Served from the buffer after an earlier decrypt was not acknowledged. + pub redelivered: bool, + /// The message carried a sender-key distribution that could not be + /// stored. The rest of the message is still delivered; later group + /// messages from this sender fail until a valid distribution arrives. + pub sender_key_rejected: bool, + /// An app-state key share, only when sent by one of this account's own + /// devices (WA Web's `isMeAccount` gate). The only message content this + /// crate returns to its caller. + pub app_state_key_share: Option, +} + +fn receipt_key(request: &ReceiveRequest<'_>) -> String { + let mut hasher = Sha256::new(); + for part in [ + request.kind.as_str().as_bytes(), + request.chat.as_bytes(), + request.sender.as_str().as_bytes(), + ] { + hasher.update(part); + hasher.update([0]); + } + hasher.update(request.ciphertext); + hex::encode(hasher.finalize()) +} + +fn now_ms() -> u64 { + u64::try_from(wacore::time::now_utc().timestamp_millis()).unwrap_or(0) +} + +/// Flag bits in a buffer entry's third byte. +const FLAG_IDENTITY_CHANGED: u8 = 1; +const FLAG_SENDER_KEY_REJECTED: u8 = 2; + +struct BufferEntry { + state: u8, + padding_version: u8, + identity_changed: bool, + /// Kept so a redelivery reports a rejected distribution the first + /// response may have lost. + sender_key_rejected: bool, + inserted_ms: u64, + padded_plaintext: Vec, +} + +impl BufferEntry { + fn encode(&self) -> Vec { + let mut out = Vec::with_capacity(HEADER_LEN + self.padded_plaintext.len()); + out.push(self.state); + out.push(self.padding_version); + let mut flags = 0; + if self.identity_changed { + flags |= FLAG_IDENTITY_CHANGED; + } + if self.sender_key_rejected { + flags |= FLAG_SENDER_KEY_REJECTED; + } + out.push(flags); + out.extend_from_slice(&self.inserted_ms.to_be_bytes()); + out.extend_from_slice(&self.padded_plaintext); + out + } + + fn decode(bytes: Vec) -> Result { + if bytes.len() < HEADER_LEN { + return Err(OpsError::InvalidInput( + "decrypt buffer entry is truncated".into(), + )); + } + let mut inserted = [0u8; 8]; + inserted.copy_from_slice(&bytes[3..HEADER_LEN]); + Ok(Self { + state: bytes[0], + padding_version: bytes[1], + identity_changed: bytes[2] & FLAG_IDENTITY_CHANGED != 0, + sender_key_rejected: bytes[2] & FLAG_SENDER_KEY_REJECTED != 0, + inserted_ms: u64::from_be_bytes(inserted), + padded_plaintext: bytes[HEADER_LEN..].to_vec(), + }) + } + + fn delivered(inserted_ms: u64) -> Self { + Self { + state: STATE_DELIVERED, + padding_version: 0, + identity_changed: false, + sender_key_rejected: false, + inserted_ms, + padded_plaintext: Vec::new(), + } + } +} + +fn is_storage_failure(error: &OpsError) -> bool { + matches!( + error, + OpsError::Store(_) | OpsError::Signal(SignalProtocolError::BackendError(..)) + ) +} + +fn classify( + padded: &[u8], + padding_version: u8, + is_from_me: bool, +) -> Result { + let mut content = process_decrypted_plaintext(padded, padding_version, is_from_me) + .map_err(|e| OpsError::InvalidInput(format!("plaintext did not decode: {e}")))?; + if !is_from_me { + strip_app_state_keys(&mut content); + } + Ok(content) +} + +/// App-state key shares and requests are only honoured from this account's +/// own devices (WA Web's `isMeAccount` gate). From anyone else they are +/// removed everywhere, so they cannot reach the app through the message. +fn strip_app_state_keys(content: &mut DecryptedMessageResult) { + if let Some(protocol) = content.message.protocol_message.as_option_mut() { + protocol.app_state_sync_key_share = Default::default(); + protocol.app_state_sync_key_request = Default::default(); + } + if let Some(info) = content.protocol_message.as_mut() { + info.app_state_sync_key_share = None; + } +} + +fn key_share( + content: &DecryptedMessageResult, + is_from_me: bool, +) -> Option { + if !is_from_me { + return None; + } + content + .protocol_message + .as_ref() + .and_then(|p| p.app_state_sync_key_share.clone()) +} + +impl SignalOps { + /// Decrypt, buffer and classify one received payload. Sender-key + /// distributions inside it are stored in the same batch as the decrypt. + pub async fn receive(&self, request: ReceiveRequest<'_>) -> Result { + let receipt_key = receipt_key(&request); + // Pairwise payloads can carry a sender-key distribution for this chat, + // so they also lock the sender's chain. + let chain = chain_key(request.chat, request.sender.as_str()); + let keys = match request.kind { + ReceiveKind::SenderKey => vec![chain], + // A prekey message can create a session, so it also takes the + // sender's account lock (see `account_lock`). + _ => vec![ + session_key(request.sender), + chain, + account_lock(&crate::trust::device_of(request.sender)?.user), + ], + }; + // `is_from_me` gates app-state key shares, so it is derived from the + // sender, this account's stored JIDs and its pairing; a client claim + // the sender does not support is refused rather than believed. Until + // pairing anchors the account, no sender counts as an own device. + let from_me = self + .is_anchored_own_device(&crate::trust::device_of(request.sender)?) + .await; + if request.is_from_me && !from_me { + return Err(OpsError::InvalidInput( + "is_from_me is set, but the sender is not one of this paired account's devices" + .into(), + )); + } + let held = self.lock_keys(keys).await?; + held.run(async { + if let Some(bytes) = self + .store() + .load_aux(Namespace::DecryptBuffer, &receipt_key) + .await? + { + let entry = BufferEntry::decode(bytes)?; + if entry.state == STATE_DELIVERED { + return Ok(Received::AlreadyDelivered { receipt_key }); + } + let content = classify(&entry.padded_plaintext, entry.padding_version, from_me)?; + return Ok(Received::Message(Box::new(ReceivedMessage { + app_state_key_share: key_share(&content, from_me), + receipt_key, + content, + identity_changed: entry.identity_changed, + redelivered: true, + sender_key_rejected: entry.sender_key_rejected, + }))); + } + + let staged = Arc::new(self.store().staged()); + let (padded, identity_changed) = match request.kind { + ReceiveKind::PreKey | ReceiveKind::Message => { + let kind = if request.kind == ReceiveKind::PreKey { + EncKind::PreKey + } else { + EncKind::Message + }; + let decrypted = self + .decrypt_into( + &staged, + request.sender, + kind, + request.ciphertext, + request.device_identity, + ) + .await?; + (decrypted.plaintext, decrypted.identity_changed) + } + ReceiveKind::SenderKey => ( + self.group_decrypt_into( + &staged, + request.chat, + request.sender, + request.ciphertext, + ) + .await?, + false, + ), + }; + + let inserted_ms = now_ms(); + let content = match classify(&padded, request.padding_version, from_me) { + Ok(content) => content, + Err(error) => { + // The ratchet already advanced; keep that, and record the + // ciphertext as handled so redelivery does not loop. + staged + .put_aux( + Namespace::DecryptBuffer, + &receipt_key, + &BufferEntry::delivered(inserted_ms).encode(), + ) + .await?; + staged.commit().await?; + return Err(error); + } + }; + + // A bad distribution must not block the message carrying it: failing + // here would discard the ratchet advance, and every redelivery would + // fail the same way. The client logs and continues the same way. + // A storage failure is different: return it uncommitted, so the + // redelivery decrypts again and retries installing the key. + let mut sender_key_rejected = false; + // WA Web installs a distribution under its own group ID + // (`WAWebHandleMsgProcess`: `createGroupSignalSession(author, + // groupId, key)`). Only this chat's chain is locked here, so a + // distribution naming another group is refused rather than stored + // under the wrong one. + if let Some(skdm) = &content.skdm + && skdm.axolotl_sender_key_distribution_message.is_some() + && skdm.group_id.as_deref() != Some(request.chat) + { + sender_key_rejected = true; + } else if let Some(skdm) = &content.skdm + && let Some(axolotl) = &skdm.axolotl_sender_key_distribution_message + && let Err(error) = self + .process_sender_key_distribution_into( + &staged, + request.chat, + request.sender, + axolotl, + ) + .await + { + if is_storage_failure(&error) { + return Err(error); + } + sender_key_rejected = true; + } + + let entry = BufferEntry { + state: STATE_PENDING, + padding_version: request.padding_version, + identity_changed, + sender_key_rejected, + inserted_ms, + padded_plaintext: padded, + }; + staged + .put_aux(Namespace::DecryptBuffer, &receipt_key, &entry.encode()) + .await?; + staged.commit().await?; + + Ok(Received::Message(Box::new(ReceivedMessage { + app_state_key_share: key_share(&content, from_me), + receipt_key, + content, + identity_changed, + redelivered: false, + sender_key_rejected, + }))) + }) + .await + } + + /// The app has the message: drop its plaintext and keep a marker so a + /// redelivered ciphertext is recognised. + pub async fn mark_delivered(&self, receipt_key: &str) -> Result<(), OpsError> { + let store = self.store(); + if let Some(bytes) = store + .load_aux(Namespace::DecryptBuffer, receipt_key) + .await? + { + let entry = BufferEntry::decode(bytes)?; + if entry.state != STATE_DELIVERED { + store + .put_aux( + Namespace::DecryptBuffer, + receipt_key, + &BufferEntry::delivered(entry.inserted_ms).encode(), + ) + .await?; + } + } + Ok(()) + } + + /// Delete delivered markers inserted before `cutoff_ms`. Returns how many + /// were removed. Results not yet confirmed with [`Self::mark_delivered`] + /// are kept whatever their age: their keys are already consumed, so the + /// buffer is the only copy of that plaintext. The app bounds them by + /// confirming delivery. + pub async fn prune_decrypt_buffer(&self, cutoff_ms: u64) -> Result { + let store = self.store(); + let mut expired = Vec::new(); + for key in store.scan_aux(Namespace::DecryptBuffer, "").await? { + // An entry that no longer decodes cannot answer a replay; delete it + // rather than let one corrupt entry stop every later prune. + if let Some(bytes) = store.load_aux(Namespace::DecryptBuffer, &key).await? + && BufferEntry::decode(bytes).map_or(true, |entry| { + entry.state == STATE_DELIVERED && entry.inserted_ms < cutoff_ms + }) + { + expired.push(key); + } + } + let refs: Vec<&str> = expired.iter().map(String::as_str).collect(); + store.delete_aux(Namespace::DecryptBuffer, &refs).await?; + Ok(expired.len()) + } +} diff --git a/crates/signal-ops/src/lib.rs b/crates/signal-ops/src/lib.rs new file mode 100644 index 0000000..fde2469 --- /dev/null +++ b/crates/signal-ops/src/lib.rs @@ -0,0 +1,69 @@ +// SPDX-License-Identifier: MIT + +//! Signal operations for a WhatsApp linked device whose private keys live +//! outside the WhatsApp client. +//! +//! The client (connection, receipts, retries, prekey upload, device lists) +//! runs elsewhere and calls these operations whenever it needs a private key: +//! decrypting and encrypting messages, sender keys, new prekeys, and the one +//! pairing signature. Everything here is pure Signal state over a +//! [`wacore::store::traits::SignalStore`]; nothing opens a socket. +//! +//! # Trust boundary +//! +//! Private keys, plaintext and Signal state stay with the caller of these +//! operations. The client never holds them, and a client that only relays +//! ciphertext learns nothing more than it would as a transport. +//! +//! Prekey bundles come from the client too, so none is used until it is +//! bound to the peer account (see [`SignalResolver`] and [`BundleRejection`]): +//! a primary device's identity is pinned on first use and a change needs +//! [`SignalOps::trust_account_identity`] from the application; a companion +//! must carry an ADV device identity that verifies against the pinned key. +//! This applies to sends, retries and [`SignalOps::establish_session`]. +//! +//! The client is still trusted for which of an account's verified devices +//! and which accounts a message goes to: device lists, PN/LID mappings and +//! group membership come from it, and a pin is taken on first use. Device +//! revocation is not checked against the account's signed key-index list. +//! Prune sent messages ([`SignalOps::prune_sent_messages`]) to bound what a +//! retry can reach. +//! +//! Message padding is the caller's concern: [`SignalOps::decrypt`] returns +//! libsignal's plaintext exactly as received and [`SignalOps::encrypt`] takes +//! already padded plaintext. +//! +//! Use [`SignalOps::prepare_prekey_upload`] for ordinary public-key uploads: +//! its sealed pending batch survives reopening, and retries do not allocate +//! more private keys. Acknowledge a successful server upload with +//! [`SignalOps::acknowledge_prekey_upload`]; the last exact acknowledgment is +//! replayable after a lost response. [`SignalOps::generate_prekeys`] remains +//! fresh generation for retry receipts; [`SignalOps::mark_prekeys_uploaded`] +//! marks only those fresh keys and rejects pending upload members atomically. +//! These operations use the scope's fenced store; +//! upload batch IDs remain reserved across 24-bit wrap even after consumption. +//! +//! Dropping a state-changing operation before completion retires that +//! `SignalOps` instance before its locks are released. Submitted backend writes +//! may still finish, so callers must release its lease and open a new instance +//! with a higher fence, not reopen at the same generation. [`SignalOps::is_usable`] +//! exposes this state; later mutations report [`OpsError::is_fence_lost`]. + +mod device; +mod error; +mod guard; +mod inbound; +mod ops; +mod outbound; +mod stores; +mod trust; + +pub use device::{DeviceKeys, PublicIdentity, PublicPreKey, PublicSignedPreKey}; +pub use error::OpsError; +pub use inbound::{ReceiveKind, ReceiveRequest, Received, ReceivedMessage}; +pub use ops::{ + AuthorityKey, Decrypted, EncKind, Encrypted, PairingSignature, PreKeyUploadBatch, RemoteBundle, + ServiceStore, SignalOps, +}; +pub use outbound::{PreparedGroupSend, PreparedSend, RetryRequest, RetryRoute}; +pub use trust::{BundleRejection, FetchedBundle, SignalResolver, VerifiablePrekeys}; diff --git a/crates/signal-ops/src/ops.rs b/crates/signal-ops/src/ops.rs new file mode 100644 index 0000000..252f2e1 --- /dev/null +++ b/crates/signal-ops/src/ops.rs @@ -0,0 +1,1077 @@ +// SPDX-License-Identifier: MIT + +use crate::device::{ + DeviceKeys, MAX_PREKEY_ID, PublicIdentity, PublicPreKey, PublicSignedPreKey, public_bytes, + sign_prekey, +}; +use crate::error::OpsError; +use crate::stores::{KeyedLocks, Stores}; +use async_lock::{Mutex, RwLock}; +use async_trait::async_trait; +use std::sync::Arc; +use wacore::libsignal::protocol::{ + CiphertextMessageType, DeviceId, IdentityKey, KeyPair, PreKeyBundle, PreKeySignalMessage, + ProtocolAddress, PublicKey, SenderKeyDistributionMessage, SignalMessage, UsePQRatchet, + create_sender_key_distribution_message, group_decrypt, group_encrypt, message_decrypt_prekey, + message_decrypt_signal, message_encrypt, process_prekey_bundle, + process_sender_key_distribution_message, +}; +use wacore::libsignal::store::record_helpers; +use wacore::libsignal::store::sender_key_name::SenderKeyName; +use wacore::pair::PairUtils; +use wacore::store::error::Result as StoreResult; +use wacore::store::traits::SignalStore; +use wacore::types::jid::JidExt; +use wacore_binary::{Jid, Server}; +use wacore_recordstore::{Fence, Namespace, RecordSignalStore, RecordStore, Sealer}; + +#[path = "prekeys.rs"] +mod prekeys; +pub use prekeys::PreKeyUploadBatch; + +/// Storage the Signal service needs beyond [`SignalStore`]: the device key +/// blob, its own records (decrypt buffer, sent messages), and staged views +/// whose writes commit as one batch. +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +pub trait ServiceStore: SignalStore + Sized + 'static { + async fn load_device_keys(&self) -> StoreResult>>; + async fn save_device_keys(&self, blob: &[u8]) -> StoreResult<()>; + /// Write the first device key blob; fails with `RecordExists` if one is + /// already stored. + async fn create_device_keys(&self, blob: &[u8]) -> StoreResult<()>; + /// A view whose writes are held until [`Self::commit`]. + fn staged(&self) -> Self; + /// Apply a staged view's writes as one fenced batch. + async fn commit(&self) -> StoreResult<()>; + async fn load_aux(&self, ns: Namespace, key: &str) -> StoreResult>>; + async fn put_aux(&self, ns: Namespace, key: &str, value: &[u8]) -> StoreResult<()>; + /// Exclusive inserts used to publish durable prekey uploads atomically. + async fn insert_aux(&self, ns: Namespace, key: &str, value: &[u8]) -> StoreResult<()>; + async fn insert_prekeys_batch(&self, keys: &[(u32, bytes::Bytes)]) -> StoreResult<()>; + async fn delete_aux(&self, ns: Namespace, keys: &[&str]) -> StoreResult<()>; + async fn scan_aux(&self, ns: Namespace, prefix: &str) -> StoreResult>; + /// Addresses with a stored session ([`Namespace::Session`]) or identity + /// ([`Namespace::Identity`]) starting with `prefix`. + async fn scan_peer_addresses(&self, ns: Namespace, prefix: &str) -> StoreResult>; + /// Identifies the backend and lease fence this store writes under. + /// Instances with equal keys in one process share one [`SignalOps`] + /// authority. + fn authority_key(&self) -> AuthorityKey; +} + +/// The record backend and lease fence behind a [`ServiceStore`]. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct AuthorityKey { + backend: usize, + scope: Arc, + generation: u64, +} + +impl AuthorityKey { + /// `backend` must be the shared handle that every store over the same + /// records holds; it stays alive while an instance using it is open. + pub fn new(backend: &Arc, fence: &Fence) -> Self { + Self { + backend: Arc::as_ptr(backend).cast::<()>() as usize, + scope: fence.scope.clone(), + generation: fence.generation, + } + } +} + +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +impl ServiceStore for RecordSignalStore { + fn authority_key(&self) -> AuthorityKey { + AuthorityKey::new(self.records(), RecordSignalStore::fence(self)) + } + + async fn load_device_keys(&self) -> StoreResult>> { + self.load_device().await + } + + async fn save_device_keys(&self, blob: &[u8]) -> StoreResult<()> { + self.save_device(blob).await + } + + async fn create_device_keys(&self, blob: &[u8]) -> StoreResult<()> { + self.create_device(blob).await + } + + fn staged(&self) -> Self { + RecordSignalStore::staged(self) + } + + async fn commit(&self) -> StoreResult<()> { + RecordSignalStore::commit(self).await + } + + async fn load_aux(&self, ns: Namespace, key: &str) -> StoreResult>> { + RecordSignalStore::load_aux(self, ns, key).await + } + + async fn put_aux(&self, ns: Namespace, key: &str, value: &[u8]) -> StoreResult<()> { + RecordSignalStore::put_aux(self, ns, key, value).await + } + + async fn insert_aux(&self, ns: Namespace, key: &str, value: &[u8]) -> StoreResult<()> { + RecordSignalStore::insert_aux(self, ns, key, value).await + } + + async fn insert_prekeys_batch(&self, keys: &[(u32, bytes::Bytes)]) -> StoreResult<()> { + RecordSignalStore::insert_prekeys_batch(self, keys).await + } + + async fn delete_aux(&self, ns: Namespace, keys: &[&str]) -> StoreResult<()> { + RecordSignalStore::delete_aux(self, ns, keys).await + } + + async fn scan_aux(&self, ns: Namespace, prefix: &str) -> StoreResult> { + RecordSignalStore::scan_aux(self, ns, prefix, None).await + } + + async fn scan_peer_addresses(&self, ns: Namespace, prefix: &str) -> StoreResult> { + RecordSignalStore::scan_peer_addresses(self, ns, prefix, None).await + } +} + +/// Pairwise ciphertext kinds, named as in the `` attribute. +#[derive(Debug, Clone, Copy, PartialEq, Eq, wacore::WireEnum)] +pub enum EncKind { + /// First message of a session, carries X3DH material. + #[wire = "pkmsg"] + PreKey, + /// A message on an established session. + #[wire = "msg"] + Message, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Decrypted { + /// Padded plaintext exactly as libsignal produced it. + pub plaintext: Vec, + /// The sender's identity key differs from the one stored before. + pub identity_changed: bool, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Encrypted { + pub kind: EncKind, + pub ciphertext: Vec, +} + +/// A peer device's prekey bundle, as fetched by the client from the server. +#[derive(Debug, Clone)] +pub struct RemoteBundle { + pub registration_id: u32, + pub device_id: u32, + pub identity_key: [u8; 32], + pub signed_prekey_id: u32, + pub signed_prekey: [u8; 32], + pub signed_prekey_signature: [u8; 64], + pub prekey: Option<(u32, [u8; 32])>, + /// The `` (`ADVSignedDeviceIdentity` bytes) the bundle + /// came with. Required for companion devices; see [`crate::SignalResolver`]. + pub device_identity: Option>, +} + +impl RemoteBundle { + /// The libsignal bundle, validating every key. + pub fn to_prekey_bundle(&self) -> Result { + let prekey = match self.prekey { + Some((id, key)) => Some((id.into(), public_key(&key, "prekey")?)), + None => None, + }; + Ok(PreKeyBundle::new( + self.registration_id, + DeviceId::from(self.device_id), + prekey, + self.signed_prekey_id.into(), + public_key(&self.signed_prekey, "signed prekey")?, + self.signed_prekey_signature, + IdentityKey::new(public_key(&self.identity_key, "identity key")?), + )?) + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct PairingSignature { + /// `ADVSignedDeviceIdentity` with the device signature added, ready for the + /// pair-success response. + pub signed_identity: Vec, + pub key_index: u32, +} + +use crate::guard::Held; + +/// Lock key for one peer device's session and identity. +pub(crate) fn session_key(address: &ProtocolAddress) -> String { + format!("s:{}", address.as_str()) +} + +/// The device of `user`'s address `address` (a stored session or identity +/// key) on `server`, or `None` when it belongs to someone else. Two forms +/// are stored: wacore's `{user}[:{device}]@{server}.0`, with WA Web's `c.us` +/// spelling for phone numbers, and the plain libsignal name +/// `{user}@{server}.{device}` that callers can pass to `establish_session` +/// or `decrypt`. A `:device` part wins over the suffix. +fn device_of(address: &str, user: &str, server: Server) -> Option { + let rest = address.strip_prefix(user)?; + let (named, rest) = match rest.strip_prefix(':') { + Some(tail) => { + let at = tail.find('@')?; + (Some(tail[..at].parse().ok()?), &tail[at..]) + } + None => (None, rest), + }; + let rest = rest.strip_prefix('@')?; + let spellings: &[&str] = match server { + Server::Pn => &["c.us", "s.whatsapp.net"], + other => &[other.as_str()], + }; + // The libsignal device suffix. Requiring digits keeps `hosted` from + // matching `hosted.lid` addresses. + let suffix = spellings + .iter() + .find_map(|spelling| rest.strip_prefix(spelling)?.strip_prefix('.'))?; + if suffix.is_empty() || !suffix.bytes().all(|b| b.is_ascii_digit()) { + return None; + } + Some(match named { + Some(device) => device, + None => suffix.parse().ok()?, + }) +} + +/// `server` plus its hosted counterpart, if it has one. +fn with_hosted(server: Server) -> Vec { + match server { + Server::Pn => vec![Server::Pn, Server::Hosted], + Server::Lid => vec![Server::Lid, Server::HostedLid], + other => vec![other], + } +} + +/// Lock key for one sender-key chain. +pub(crate) fn chain_key(group: &str, sender: &str) -> String { + format!("k:{group}\u{0}{sender}") +} + +/// Lock key for a group's sender-key distribution marks. +/// Per-account lock: every path that can create a session with a peer takes +/// it, and `forget_user` takes it before scanning, so a session set up before +/// a reset cannot commit after it. Keyed on the user part, covering the +/// account's PN, hosted and LID addresses of that number alike. `p:` sorts +/// after `g:`/`k:` and before `s:`, matching the order every caller already +/// acquires those in. +pub(crate) fn account_lock(user: &str) -> String { + format!("p:{user}") +} + +pub(crate) fn group_key(group: &str) -> String { + format!("g:{group}") +} + +/// Signal operations for one scope (one linked device). +/// +/// Every operation that changes Signal state first locks exactly the state it +/// can write: a peer's session, a sender-key chain, or a group's distribution +/// marks. Staged views commit whole batches, so two views writing the same +/// record would lose an update; disjoint views proceed in parallel. Locks are +/// taken in sorted order, so overlapping operations cannot deadlock. The +/// store's lease fencing protects against a second node. +pub struct SignalOps { + store: Arc, + authority: Arc, + /// Keeps this fence's registry entry alive, so a later open joins it. + _slot: Arc, +} + +/// Cached keys, locks and the usability flag for one lease fence. A second +/// instance opened under the same fence shares them instead of creating an +/// independent authority whose writes the fence would also accept. +struct Authority { + keys: RwLock>, + /// Held while device keys are read-modify-written. + device_write: Arc>, + usable: Arc, + locks: Arc, + sender_key_locks: Arc, +} + +/// One fence's authority, filled by the first open that reads its keys. +type AuthoritySlot = async_lock::OnceCell>; + +type AuthorityRegistry = + std::sync::Mutex>>; + +fn authorities() -> &'static AuthorityRegistry { + static AUTHORITIES: std::sync::OnceLock = std::sync::OnceLock::new(); + AUTHORITIES.get_or_init(Default::default) +} + +/// Return the live slot for `key`, or register an empty one. The slot is +/// registered before any device keys are read, so an open that overlaps a +/// live instance, or another open, shares one authority instead of building +/// a second one from a read taken before the other's writes. +fn authority_slot(key: AuthorityKey) -> Arc { + let mut map = authorities() + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + if let Some(live) = map.get(&key).and_then(std::sync::Weak::upgrade) { + return live; + } + map.retain(|_, weak| weak.strong_count() > 0); + let slot = Arc::new(AuthoritySlot::new()); + map.insert(key, Arc::downgrade(&slot)); + slot +} + +fn new_authority(keys: DeviceKeys) -> Arc { + Arc::new(Authority { + keys: RwLock::new(Arc::new(keys)), + device_write: Arc::new(Mutex::new(())), + usable: Arc::new(std::sync::atomic::AtomicBool::new(true)), + locks: Arc::new(KeyedLocks::default()), + sender_key_locks: Arc::new(KeyedLocks::default()), + }) +} + +fn rng() -> rand::rngs::StdRng { + rand::make_rng::() +} + +fn public_key(bytes: &[u8; 32], what: &str) -> Result { + PublicKey::from_djb_public_key_bytes(bytes) + .map_err(|_| OpsError::InvalidInput(format!("{what} is not a valid public key"))) +} + +impl SignalOps { + /// Open a scope that already has device keys. Opening a scope again under + /// the same fence while an instance is live shares that instance's keys, + /// locks and retirement state. + pub async fn open(store: Arc) -> Result { + let slot = authority_slot(store.authority_key()); + let loaded = std::sync::atomic::AtomicBool::new(false); + let authority = slot + .get_or_try_init(|| async { + let keys = Self::stored_keys(&store).await?; + loaded.store(true, std::sync::atomic::Ordering::SeqCst); + Ok::<_, OpsError>(new_authority(keys)) + }) + .await? + .clone(); + if !loaded.load(std::sync::atomic::Ordering::SeqCst) { + // Joining a live authority skips its key read. Read through this + // store anyway: one with a different sealer must fail here, not + // later reseal the shared keys under its own key. The read only + // checks; the authority keeps the keys it already holds. + let own = Self::stored_keys(&store).await?; + let shared = authority.keys.read().await.clone(); + if own.identity.public_key.public_key_bytes() + != shared.identity.public_key.public_key_bytes() + { + return Err(OpsError::InvalidInput( + "this store's device keys differ from the open instance's".into(), + )); + } + } + Ok(Self { + store, + authority, + _slot: slot, + }) + } + + /// Generate and persist device keys for a new scope. Refuses to replace + /// existing keys, which would silently unlink the device; the write is an + /// exclusive insert, so concurrent initialisers cannot both succeed. + pub async fn create(store: Arc) -> Result { + let exists = || OpsError::InvalidInput("scope already has device keys".into()); + let slot = authority_slot(store.authority_key()); + let created = std::sync::atomic::AtomicBool::new(false); + let authority = slot + .get_or_try_init(|| async { + let keys = DeviceKeys::generate()?; + match store.create_device_keys(&keys.encode()?).await { + Ok(()) => { + created.store(true, std::sync::atomic::Ordering::SeqCst); + Ok(new_authority(keys)) + } + Err(e) if wacore_recordstore::is_record_exists(&e) => Err(exists()), + Err(e) => Err(e.into()), + } + }) + .await? + .clone(); + // A live instance under this fence means the keys already exist. + if !created.load(std::sync::atomic::Ordering::SeqCst) { + return Err(exists()); + } + Ok(Self { + store, + authority, + _slot: slot, + }) + } + + async fn stores(&self) -> Stores { + self.stores_over(self.store.clone()).await + } + + pub(crate) async fn stores_over(&self, store: Arc) -> Stores { + Stores { + store, + keys: self.authority.keys.read().await.clone(), + sender_key_locks: self.authority.sender_key_locks.clone(), + } + } + + pub(crate) fn store(&self) -> &Arc { + &self.store + } + + /// Whether this instance can still mutate Signal state. Cancellation of a + /// locked operation retires it; release the lease and use a higher fence + /// before constructing its replacement. + pub fn is_usable(&self) -> bool { + self.authority + .usable + .load(std::sync::atomic::Ordering::SeqCst) + } + + fn require_usable(&self) -> Result<(), OpsError> { + if self.is_usable() { + Ok(()) + } else { + Err(crate::error::interrupted_operation()) + } + } + + pub(crate) async fn lock_device(&self) -> Result { + self.require_usable()?; + let held = self.authority.device_write.lock_arc().await; + self.require_usable()?; + Ok(Held::new(vec![held], self.authority.usable.clone())) + } + + /// Acquire sorted state locks, then check for a cancelled predecessor. + pub(crate) async fn lock_keys(&self, mut keys: Vec) -> Result { + self.require_usable()?; + keys.sort(); + keys.dedup(); + let mut held = Vec::with_capacity(keys.len()); + for key in keys { + held.push(self.authority.locks.get(&key).lock_arc().await); + } + self.require_usable()?; + Ok(Held::new(held, self.authority.usable.clone())) + } + + pub(crate) async fn keys(&self) -> Arc { + self.authority.keys.read().await.clone() + } + + /// Record this device's own JIDs once the client knows them (after pair + /// success). Sends need them to build own-device copies. + pub async fn set_own_jids(&self, pn: &str, lid: Option<&str>) -> Result<(), OpsError> { + let parse = |jid: &str| { + // The device-key record stores each JID behind a u16 length. + if u16::try_from(jid.len()).is_err() { + return Err(OpsError::InvalidInput(format!("{jid} is too long"))); + } + jid.parse::() + .map_err(|_| OpsError::InvalidInput(format!("{jid} is not a JID"))) + }; + if !parse(pn)?.is_pn() { + return Err(OpsError::InvalidInput(format!( + "{pn} is not a phone-number JID" + ))); + } + if let Some(lid) = lid + && !parse(lid)?.is_lid() + { + return Err(OpsError::InvalidInput(format!("{lid} is not a LID"))); + } + let held = self.lock_device().await?; + held.run(async { + let mut keys = self.durable_keys().await?; + keys.own_pn = Some(pn.to_owned()); + keys.own_lid = lid.map(str::to_owned); + self.replace_keys(keys).await + }) + .await + } + + async fn replace_keys(&self, keys: DeviceKeys) -> Result<(), OpsError> { + let encoded = keys.encode()?; + if let Err(error) = self.store.save_device_keys(&encoded).await { + self.refresh_keys().await; + return Err(error.into()); + } + *self.authority.keys.write().await = Arc::new(keys); + Ok(()) + } + + async fn stored_keys(store: &S) -> Result { + DeviceKeys::decode( + &store + .load_device_keys() + .await? + .ok_or(OpsError::NotInitialised)?, + ) + } + + /// The device keys as last committed. Read-modify-write starts here, not + /// from the cache: a write whose response was lost may still have + /// committed, leaving the cache behind the store, and writing the cached + /// blob back would rewind it (for example `next_prekey_id`). Call with + /// the device lock held. + pub(crate) async fn durable_keys(&self) -> Result { + Self::stored_keys(&self.store).await + } + + /// After a device-key write with an uncertain outcome, bring the cache in + /// line with the store. Best effort: on failure the cache stays as it + /// was, and writers still start from [`Self::durable_keys`]. + pub(crate) async fn refresh_keys(&self) { + if let Ok(keys) = self.durable_keys().await { + *self.authority.keys.write().await = Arc::new(keys); + } + } + + pub async fn public_identity(&self) -> PublicIdentity { + self.authority.keys.read().await.public() + } + + /// Generate fresh one-time prekeys for retry receipts. Unlike + /// `prepare_prekey_upload`, each call reserves new IDs. The counter and + /// private keys commit together; active upload IDs remain reserved. + pub async fn generate_prekeys( + &self, + runtime: &dyn wacore::runtime::Runtime, + count: u32, + ) -> Result, OpsError> { + self.generate_fresh_prekeys(runtime, count).await + } + + /// Mark fresh retry prekeys uploaded. Any original member of the pending + /// upload batch rejects the entire call; use `acknowledge_prekey_upload` + /// for that batch. Consumed keys stay gone. + pub async fn mark_prekeys_uploaded(&self, ids: &[u32]) -> Result<(), OpsError> { + self.mark_fresh_prekeys_uploaded(ids).await + } + + /// Replace the signed prekey. The old one stays loadable by ID so prekey + /// messages minted against it still decrypt. + pub async fn rotate_signed_prekey(&self) -> Result { + let held = self.lock_device().await?; + held.run(async { + let mut keys = self.durable_keys().await?; + let retired = record_helpers::new_signed_pre_key_record( + keys.signed_prekey_id, + &keys.signed_prekey, + keys.signed_prekey_signature, + wacore::time::now_utc(), + )?; + self.store + .store_signed_prekey( + keys.signed_prekey_id, + &waproto::codec::signed_pre_key_record_to_vec(&retired), + ) + .await?; + let pair = KeyPair::generate(&mut rng()); + keys.signed_prekey_signature = sign_prekey(&keys.identity, &pair)?; + keys.signed_prekey = pair; + keys.signed_prekey_id = if keys.signed_prekey_id >= MAX_PREKEY_ID { + 1 + } else { + keys.signed_prekey_id + 1 + }; + let public = keys.public().signed_prekey; + self.replace_keys(keys).await?; + Ok(public) + }) + .await + } + + /// Stored session and identity addresses of every device of `user` on + /// `server`, with their device IDs. + async fn device_addresses( + &self, + user: &str, + server: Server, + ) -> Result, OpsError> { + let mut found = Vec::new(); + for ns in [Namespace::Session, Namespace::Identity] { + for address in self.store.scan_peer_addresses(ns, user).await? { + if let Some(device) = device_of(&address, user, server) { + found.push((address, device)); + } + } + } + found.sort(); + found.dedup(); + Ok(found) + } + + /// Drop the stored identity and sessions of every device of `user`, after + /// the server reports that the user's identity changed. A phone-number or + /// LID user also covers its hosted devices, as the client does. The next + /// message either way starts a fresh session. Sender keys are left alone: + /// they are replaced when the peer distributes a new one. + pub async fn forget_user(&self, user: &Jid) -> Result<(), OpsError> { + let account = self.lock_keys(vec![account_lock(&user.user)]).await?; + account + .run(async { + // Scanned under the account lock, so no session setup for this + // user is still in flight. + let mut addresses = Vec::new(); + for server in with_hosted(user.server) { + for (address, _) in self.device_addresses(&user.user, server).await? { + addresses.push(address); + } + } + addresses.dedup(); + if addresses.is_empty() { + return Ok(()); + } + let held = self + .lock_keys(addresses.iter().map(|a| format!("s:{a}")).collect()) + .await?; + held.run(async { + let batch: Vec> = + addresses.iter().map(|a| Arc::from(a.as_str())).collect(); + let staged = self.store.staged(); + staged.delete_sessions_batch(&batch).await?; + staged.delete_identities_batch(&batch).await?; + staged.commit().await?; + Ok(()) + }) + .await + }) + .await + } + + /// Move a user's sessions and identities from phone-number addresses to + /// LID addresses once the client learns the mapping, for regular and + /// hosted devices. On a conflict the phone-number session wins (it is the + /// one the peer has been using) and an existing LID identity wins. Sender + /// keys are not moved; WA Web keeps them where they are. When a device is + /// stored under two address forms, the one wacore writes wins and the other + /// is dropped. Returns how many target addresses changed. + pub async fn migrate_pn_to_lid(&self, pn: &Jid, lid: &Jid) -> Result { + if !pn.is_pn() || !lid.is_lid() { + return Err(OpsError::InvalidInput( + "migrate_pn_to_lid needs a phone-number JID and a LID".into(), + )); + } + // Both accounts first, then the scan, so no session for either is + // being set up while this decides what to move. + let accounts = self + .lock_keys(vec![account_lock(&pn.user), account_lock(&lid.user)]) + .await?; + accounts + .run(async { + let mut moves = Vec::new(); + for (from, to) in [ + (Server::Pn, Server::Lid), + (Server::Hosted, Server::HostedLid), + ] { + for (address, device) in self.device_addresses(&pn.user, from).await? { + let target = Jid::new(lid.user.clone(), to).with_device(device); + // The form wacore itself writes for this device; a + // plain libsignal name for the same device is the + // fallback. + let canonical = Jid::new(pn.user.clone(), from) + .with_device(device) + .to_protocol_address(); + let fallback = address != canonical.as_str(); + moves.push(( + target.to_protocol_address().as_str().to_owned(), + fallback, + address, + )); + } + } + // Two stored forms can name one device. Group them by target, + // canonical form first, so one source decides the target. + moves.sort(); + let moves: Vec<(String, String)> = + moves.into_iter().map(|(to, _, from)| (from, to)).collect(); + let held = self + .lock_keys( + moves + .iter() + .flat_map(|(from, to)| [format!("s:{from}"), format!("s:{to}")]) + .collect(), + ) + .await?; + held.run(async { + let staged = self.store.staged(); + let mut moved = std::collections::HashSet::new(); + // Targets whose session this migration already wrote. A + // second source for the same device is dropped, not + // copied over it, so the session and identity that land + // at the target come from one source. + let mut decided = std::collections::HashSet::new(); + for (from, to) in &moves { + let first = !decided.contains(to); + if let Some(session) = staged.get_session(from).await? { + if first { + staged.put_session(to, &session).await?; + } + staged.delete_session(from).await?; + moved.insert(to.clone()); + } + if let Some(identity) = staged.load_identity(from).await? { + if first && staged.load_identity(to).await?.is_none() { + staged.put_identity(to, identity).await?; + } + staged.delete_identity(from).await?; + moved.insert(to.clone()); + } + if moved.contains(to) { + decided.insert(to.clone()); + } + } + let moved = moved.len(); + // The account's pin moves with it, even with no session to + // move. An existing LID pin wins: it was pinned or accepted + // for that address directly. + let (pn_account, lid_account) = + (pn.to_non_ad().to_string(), lid.to_non_ad().to_string()); + if let Some(pin) = staged + .load_aux(Namespace::AccountIdentity, &pn_account) + .await? + && staged + .load_aux(Namespace::AccountIdentity, &lid_account) + .await? + .is_none() + { + staged + .insert_aux(Namespace::AccountIdentity, &lid_account, &pin) + .await?; + } + staged.commit().await?; + Ok(moved) + }) + .await + }) + .await + } + + pub async fn has_session(&self, address: &ProtocolAddress) -> Result { + Ok(self.store.has_session(address.as_str()).await?) + } + + /// Run X3DH against a fetched bundle so [`Self::encrypt`] can produce a + /// `pkmsg` for that device. + pub async fn establish_session( + &self, + address: &ProtocolAddress, + bundle: &RemoteBundle, + ) -> Result<(), OpsError> { + let user = crate::trust::device_of(address)?.user; + let held = self + .lock_keys(vec![session_key(address), account_lock(&user)]) + .await?; + held.run(async { + let staged = Arc::new(self.store.staged()); + self.establish_into(&staged, address, bundle).await?; + staged.commit().await?; + Ok(()) + }) + .await + } + + /// X3DH into a staged view without committing it. The caller holds the + /// session lock and commits. + pub(crate) async fn establish_into( + &self, + staged: &Arc, + address: &ProtocolAddress, + bundle: &RemoteBundle, + ) -> Result<(), OpsError> { + let device = crate::trust::device_of(address)?; + let device_identity = bundle.device_identity.as_deref(); + let bundle = bundle.to_prekey_bundle()?; + self.verify_bundle(staged, &device, &bundle, device_identity, None) + .await? + .map_err(OpsError::BundleRefused)?; + let mut sessions = self.stores_over(staged.clone()).await; + let mut identities = sessions.clone(); + process_prekey_bundle( + address, + &mut sessions, + &mut identities, + &bundle, + &mut rng(), + UsePQRatchet::No, + ) + .await?; + Ok(()) + } + + /// Decrypt one pairwise `` payload. The session update and the + /// consumed prekey's removal commit together. + pub async fn decrypt( + &self, + sender: &ProtocolAddress, + kind: EncKind, + ciphertext: &[u8], + device_identity: Option<&[u8]>, + ) -> Result { + let user = crate::trust::device_of(sender)?.user; + let held = self + .lock_keys(vec![session_key(sender), account_lock(&user)]) + .await?; + held.run(async { + let staged = Arc::new(self.store.staged()); + let decrypted = self + .decrypt_into(&staged, sender, kind, ciphertext, device_identity) + .await?; + staged.commit().await?; + Ok(decrypted) + }) + .await + } + + /// Decrypt into a staged view without committing it. The caller holds the + /// address lock and commits. + /// `device_identity` is the `` sent with a prekey message + /// from a companion device; a prekey message would start a session, so its + /// sender's identity is verified first, as a fetched bundle is. + pub(crate) async fn decrypt_into( + &self, + staged: &Arc, + sender: &ProtocolAddress, + kind: EncKind, + ciphertext: &[u8], + device_identity: Option<&[u8]>, + ) -> Result { + if kind == EncKind::PreKey { + let identity = *PreKeySignalMessage::try_from(ciphertext)? + .identity_key() + .public_key() + .public_key_bytes(); + let device = crate::trust::device_of(sender)?; + self.verify_identity(staged, &device, identity, device_identity, None) + .await? + .map_err(OpsError::BundleRefused)?; + } + let mut sessions = self.stores_over(staged.clone()).await; + let mut identities = sessions.clone(); + // libsignal reports a replaced identity for a prekey message but not + // for an ordinary `msg` that commits a session with another identity, + // so compare the stored identity around the decrypt as well. + let before = + wacore::libsignal::protocol::IdentityKeyStore::get_identity(&identities, sender) + .await?; + let result = match kind { + EncKind::PreKey => { + let message = PreKeySignalMessage::try_from(ciphertext)?; + let mut prekeys = sessions.clone(); + let signed_prekeys = sessions.clone(); + message_decrypt_prekey( + &message, + sender, + &mut sessions, + &mut identities, + &mut prekeys, + &signed_prekeys, + &mut rng(), + UsePQRatchet::No, + ) + .await? + } + EncKind::Message => { + let message = SignalMessage::try_from(ciphertext)?; + message_decrypt_signal(&message, sender, &mut sessions, &mut identities, &mut rng()) + .await? + } + }; + // Staged with the promoted session, so the prekey is never gone while + // the session that consumed it is not durable. + if let Some(id) = result.consumed_prekey_id { + staged.remove_prekey(id.into()).await?; + } + let after = + wacore::libsignal::protocol::IdentityKeyStore::get_identity(&identities, sender) + .await?; + Ok(Decrypted { + plaintext: result.plaintext, + identity_changed: matches!( + result.identity_change, + wacore::libsignal::protocol::IdentityChange::ReplacedExisting + ) || before + .is_some_and(|before| after.is_some_and(|after| before != after)), + }) + } + + /// Encrypt padded plaintext for one device with an existing session. + pub async fn encrypt( + &self, + recipient: &ProtocolAddress, + plaintext: &[u8], + ) -> Result { + let held = self.lock_keys(vec![session_key(recipient)]).await?; + held.run(async { + let mut sessions = self.stores().await; + let mut identities = sessions.clone(); + let message = + message_encrypt(plaintext, recipient, &mut sessions, &mut identities).await?; + let kind = match message.message_type() { + CiphertextMessageType::PreKey => EncKind::PreKey, + CiphertextMessageType::Whisper => EncKind::Message, + other => { + return Err(OpsError::InvalidInput(format!( + "unexpected pairwise ciphertext type {other:?}" + ))); + } + }; + Ok(Encrypted { + kind, + ciphertext: message.serialize().to_vec(), + }) + }) + .await + } + + /// The sender-key distribution message for our own chain in `group`, + /// creating the chain if needed. Encrypt it pairwise to each member device. + pub async fn sender_key_distribution( + &self, + group: &str, + own_address: &ProtocolAddress, + ) -> Result, OpsError> { + let name = SenderKeyName::from_parts(group, own_address.as_str()); + let held = self + .lock_keys(vec![chain_key(group, own_address.as_str())]) + .await?; + held.run(async { + let mut store = self.stores().await; + let skdm = + create_sender_key_distribution_message(&name, &mut store, &mut rng()).await?; + Ok(skdm.serialized().to_vec()) + }) + .await + } + + /// Encrypt group plaintext (`skmsg`) on our own chain. + pub async fn group_encrypt( + &self, + group: &str, + own_address: &ProtocolAddress, + plaintext: &[u8], + ) -> Result, OpsError> { + let name = SenderKeyName::from_parts(group, own_address.as_str()); + let held = self + .lock_keys(vec![chain_key(group, own_address.as_str())]) + .await?; + held.run(async { + let mut store = self.stores().await; + let message = group_encrypt(&mut store, &name, plaintext, &mut rng()).await?; + Ok(message.serialized().to_vec()) + }) + .await + } + + /// Store a member's sender key received in a decrypted message. + pub async fn process_sender_key_distribution( + &self, + group: &str, + sender: &ProtocolAddress, + skdm: &[u8], + ) -> Result<(), OpsError> { + let name = SenderKeyName::from_parts(group, sender.as_str()); + let message = SenderKeyDistributionMessage::try_from(skdm)?; + let held = self + .lock_keys(vec![chain_key(group, sender.as_str())]) + .await?; + held.run(async { + let mut store = self.stores().await; + process_sender_key_distribution_message(&name, &message, &mut store).await?; + Ok(()) + }) + .await + } + + /// Decrypt a group `skmsg` into a staged view without committing it. + pub(crate) async fn group_decrypt_into( + &self, + staged: &Arc, + group: &str, + sender: &ProtocolAddress, + ciphertext: &[u8], + ) -> Result, OpsError> { + let name = SenderKeyName::from_parts(group, sender.as_str()); + let mut store = self.stores_over(staged.clone()).await; + Ok(group_decrypt(ciphertext, &mut store, &name).await?) + } + + /// Store a received sender key in a staged view without committing it. + pub(crate) async fn process_sender_key_distribution_into( + &self, + staged: &Arc, + group: &str, + sender: &ProtocolAddress, + skdm: &[u8], + ) -> Result<(), OpsError> { + let name = SenderKeyName::from_parts(group, sender.as_str()); + let message = SenderKeyDistributionMessage::try_from(skdm)?; + let mut store = self.stores_over(staged.clone()).await; + process_sender_key_distribution_message(&name, &message, &mut store).await?; + Ok(()) + } + + /// Decrypt a group `skmsg` from `sender`. + pub async fn group_decrypt( + &self, + group: &str, + sender: &ProtocolAddress, + ciphertext: &[u8], + ) -> Result, OpsError> { + let name = SenderKeyName::from_parts(group, sender.as_str()); + let held = self + .lock_keys(vec![chain_key(group, sender.as_str())]) + .await?; + held.run(async { + let mut store = self.stores().await; + Ok(group_decrypt(ciphertext, &mut store, &name).await?) + }) + .await + } + + /// Verify the primary's pair-success container and add our device + /// signature. Signs only the ADV device-identity message built here, never + /// caller-chosen bytes. + pub async fn sign_pairing( + &self, + device_identity_container: &[u8], + ) -> Result { + let keys = self.authority.keys.read().await.clone(); + let (signed_identity, key_index) = PairUtils::do_pair_crypto_with( + &keys.identity, + &keys.adv_secret, + device_identity_container, + ) + .map_err(|e| OpsError::Pairing { + code: e.code, + text: e.text, + })?; + // Kept for prekey messages, which carry the device identity. + let held = self.lock_device().await?; + held.run(async { + let mut updated = self.durable_keys().await?; + updated.account = Some(signed_identity.clone()); + self.replace_keys(updated).await?; + Ok(PairingSignature { + signed_identity, + key_index, + }) + }) + .await + } +} diff --git a/crates/signal-ops/src/outbound.rs b/crates/signal-ops/src/outbound.rs new file mode 100644 index 0000000..64aa796 --- /dev/null +++ b/crates/signal-ops/src/outbound.rs @@ -0,0 +1,1191 @@ +// SPDX-License-Identifier: MIT + +//! Outbound messages built inside the Signal service. +//! +//! The application hands the service a `waE2E.Message`; wacore's stanza +//! builder pads it, adds own-device copies and encrypts it per device. The +//! WhatsApp client only answers device-list and prekey queries through the +//! [`SendContextResolver`] and transmits the finished stanza, so it never +//! handles the plaintext. The prekey bundles it supplies are verified before +//! use; see the crate's trust boundary. Session changes and the sent-message record commit before +//! the stanza is returned, matching wacore's rule that Signal state is durable +//! before ciphertext is published. + +use crate::error::OpsError; +use crate::ops::RemoteBundle; +use crate::ops::{ServiceStore, SignalOps, account_lock, chain_key, group_key, session_key}; +use crate::trust::{SignalResolver, Verifying}; +use std::collections::HashSet; +use wacore::client::context::{GroupRoutingInfo, SendContextResolver}; +use wacore::libsignal::protocol::{UsePQRatchet, process_prekey_bundle}; +use wacore::libsignal::store::sender_key_name::SenderKeyName; +use wacore::runtime::Runtime; +use wacore::send::{ + DmSignalAddressing, DmStanzaRequest, GroupStanzaRequest, ResolvedDmDevices, + ResolvedGroupDevices, SenderKeyDistributionPolicy, SignalStores, prepare_dm_stanza, + prepare_group_stanza_resolved, retain_skdm_distribution_targets, +}; +use wacore::send::{PairwiseRetryDestination, PairwiseRetryRequest, prepare_pairwise_retry_stanza}; +use wacore::types::jid::JidExt as _; +use wacore::types::message::{AddressingMode, EditAttribute}; +use wacore_binary::{Jid, JidExt}; +use wacore_recordstore::Namespace; +use waproto::whatsapp as wa; + +/// A finished `` stanza for the client to transmit. +#[derive(Debug, Clone)] +pub struct PreparedSend { + /// The stanza, marshaled as the binary node format the socket writes. + pub stanza: Vec, + /// Devices that were addressed but got no ``. + pub unreached_devices: Vec, + /// Device-list hash of the addressed set, for comparing with the server + /// ack. + pub phash: Option, +} + +fn sent_key(chat: &Jid, message_id: &str) -> String { + format!("{chat}\u{0}{message_id}") +} + +/// Sent-message record: `[inserted ms, u64 BE][has_secret][secret (32 bytes, +/// when set)][audience count, u32 BE]([JID length, u16 BE][JID])*[message]`. +/// The audience is who the message went to: the chat user for a direct +/// message, the group's participants at send time otherwise. A retry is only +/// answered for that audience. +fn encode_sent(message: &[u8], secret: Option<&[u8; 32]>, audience: &[Jid]) -> Vec { + let inserted_ms = u64::try_from(wacore::time::now_utc().timestamp_millis()).unwrap_or(0); + let audience: Vec = audience.iter().map(Jid::to_string).collect(); + let mut out = Vec::with_capacity( + 8 + 1 + 32 + 4 + audience.iter().map(|jid| 2 + jid.len()).sum::() + message.len(), + ); + out.extend_from_slice(&inserted_ms.to_be_bytes()); + match secret { + Some(secret) => { + out.push(1); + out.extend_from_slice(secret); + } + None => out.push(0), + } + let audience: Vec<&String> = audience + .iter() + .filter(|jid| u16::try_from(jid.len()).is_ok()) + .collect(); + out.extend_from_slice( + &u32::try_from(audience.len()) + .unwrap_or(u32::MAX) + .to_be_bytes(), + ); + for jid in audience { + out.extend_from_slice(&(jid.len() as u16).to_be_bytes()); + out.extend_from_slice(jid.as_bytes()); + } + out.extend_from_slice(message); + out +} + +/// Borrowed fields of a persisted sent-message record. +struct SentRecord<'a> { + inserted_ms: u64, + secret: Option<&'a [u8]>, + audience: Vec<&'a str>, + body: &'a [u8], +} + +fn decode_sent(record: &[u8]) -> Result, OpsError> { + let corrupt = || OpsError::InvalidInput("sent-message record is corrupt".into()); + let (inserted, rest) = record.split_at_checked(8).ok_or_else(corrupt)?; + let inserted_ms = u64::from_be_bytes(inserted.try_into().map_err(|_| corrupt())?); + let (secret, mut rest) = match rest.first() { + Some(0) => (None, &rest[1..]), + Some(1) if rest.len() >= 33 => (Some(&rest[1..33]), &rest[33..]), + _ => return Err(corrupt()), + }; + let (count, tail) = rest.split_at_checked(4).ok_or_else(corrupt)?; + rest = tail; + let count = u32::from_be_bytes(count.try_into().map_err(|_| corrupt())?); + let mut audience = Vec::new(); + for _ in 0..count { + let (len, tail) = rest.split_at_checked(2).ok_or_else(corrupt)?; + let len = usize::from(u16::from_be_bytes(len.try_into().map_err(|_| corrupt())?)); + let (jid, tail) = tail.split_at_checked(len).ok_or_else(corrupt)?; + audience.push(std::str::from_utf8(jid).map_err(|_| corrupt())?); + rest = tail; + } + Ok(SentRecord { + inserted_ms, + secret, + audience, + body: rest, + }) +} + +fn dm_lock_jids(devices: &[Jid], preferred: &[Jid]) -> Vec { + let mut jids: Vec<_> = devices.iter().chain(preferred).cloned().collect(); + jids.sort_by_key(|jid| jid.to_protocol_address().as_str().to_owned()); + jids.dedup_by_key(|jid| jid.to_protocol_address().as_str().to_owned()); + jids +} + +/// A failure from wacore's send path. Errors this crate raised inside it +/// (bundle verification, its stores) come back typed; resolver and other +/// failures become [`OpsError::Send`]. +pub(crate) fn send_error(error: anyhow::Error) -> OpsError { + let error = match error.downcast::() { + Ok(error) => return error, + Err(error) => error, + }; + match error.downcast::() { + Ok(error) => OpsError::Store(error), + Err(error) => OpsError::Send(format!("{error:#}")), + } +} + +#[cfg(test)] +mod send_error_tests { + use super::*; + + fn fence_lost() -> OpsError { + OpsError::Store( + wacore_recordstore::FenceLost { + scope: "scope".into(), + generation: 1, + } + .into_store_error(), + ) + } + + #[test] + fn a_lost_fence_raised_inside_a_send_stays_a_lost_fence() { + let error = crate::trust::op_error(fence_lost()); + assert!(send_error(error.context("while fetching prekeys")).is_fence_lost()); + } + + #[test] + fn a_store_error_from_the_send_path_stays_a_store_error() { + let store = wacore_recordstore::FenceLost { + scope: "scope".into(), + generation: 1, + } + .into_store_error(); + assert!(send_error(anyhow::Error::new(store)).is_fence_lost()); + } + + #[test] + fn other_failures_are_send_errors() { + assert!(matches!( + send_error(anyhow::anyhow!("resolver down")), + OpsError::Send(_) + )); + } +} + +/// A finished group `` stanza. +#[derive(Debug, Clone)] +pub struct PreparedGroupSend { + pub stanza: Vec, + /// Devices this send distributed the sender key to. Once the server acks + /// the stanza, pass them to [`SignalOps::mark_sender_key_distributed`]. + pub distribution_targets: Vec, + /// The group's sender-key epoch this send distributed under. Pass it with + /// the acknowledgment, so an acknowledgment that arrives after a rotation + /// cannot mark devices as holding the replacement key. + pub distribution_epoch: u64, + /// Users whose devices were reported unregistered while fetching bundles; + /// the client should refresh their device lists. + pub stale_device_users: Vec, + pub phash: Option, +} + +/// Where a retried message goes, matching the client's retransmission routes. +#[derive(Debug, Clone)] +pub enum RetryRoute { + /// A direct message; `recipient` is the original chat user when the + /// requester is one of this account's own devices, and `None` otherwise. + /// Any other value is refused. + Direct { + recipient: Option, + }, + Group { + addressing_mode: AddressingMode, + }, + BroadcastList, +} + +/// A retry receipt the client received for a message this device sent. +#[derive(Debug, Clone)] +pub struct RetryRequest<'a> { + /// The chat the original went to (the recipient user, group or list). + pub chat: &'a Jid, + pub message_id: &'a str, + /// The device that asked, as addressed on the wire. + pub requester: &'a Jid, + /// The address to encrypt to (the requester's LID when known). + pub encryption_jid: &'a Jid, + pub route: RetryRoute, + pub retry_count: u8, + /// Keys the receipt carried, used to start a fresh session. + pub bundle: Option<&'a RemoteBundle>, +} + +struct OwnIdentity { + pn: Jid, + lid: Option, + account: Option, +} + +/// The address a device is encrypted to: its LID when the client knows the +/// mapping for a phone-number device, otherwise the device itself. +async fn encryption_jid(resolver: &dyn SendContextResolver, device: &Jid) -> Jid { + if device.is_pn() + && let Some(lid_user) = resolver.get_lid_for_phone(&device.user).await + && let Ok(lid) = format!("{lid_user}@lid").parse::() + { + return lid.with_device(device.device); + } + device.clone() +} + +fn sender_key_device_key(group: &str, device: &Jid) -> String { + format!("{group}\u{0}{device}") +} + +/// A device known to hold this group's current sender key. Unlike the +/// distribution mark beside it, a retry never clears it: only rotating the +/// key does, so a holder that leaves is always detected. +fn sender_key_holder_key(group: &str, device: &Jid) -> String { + format!("{group}\u{2}{device}") +} + +fn sender_key_holder_prefix(group: &str) -> String { + format!("{group}\u{2}") +} + +/// A group's sender-key epoch. It sits outside the `{group}\0` device +/// prefix, so clearing the distribution marks on rotation leaves it in place. +fn distribution_epoch_key(group: &str) -> String { + format!("{group}\u{1}epoch") +} + +/// The sending chain (this device's PN or LID address) a group's +/// distribution marks belong to. The group's addressing mode picks the chain, +/// and a mark for one chain says nothing about the other. +fn distribution_chain_key(group: &str) -> String { + format!("{group}\u{1}chain") +} + +fn new_epoch() -> [u8; 8] { + use rand::Rng; + rand::make_rng::() + .next_u64() + .to_be_bytes() +} + +fn decode_epoch(bytes: Option>) -> u64 { + bytes + .and_then(|bytes| <[u8; 8]>::try_from(bytes.as_slice()).ok()) + .map_or(0, u64::from_be_bytes) +} + +/// Whether `a` and `b` name the same account, across its PN and LID forms. +async fn same_account( + resolver: &dyn SendContextResolver, + group: Option<&GroupRoutingInfo>, + a: &Jid, + b: &Jid, +) -> bool { + if a.is_same_user_as(b) && a.is_pn() == b.is_pn() && a.is_lid() == b.is_lid() { + return true; + } + let (pn, lid) = match (a.is_pn(), b.is_pn(), a.is_lid(), b.is_lid()) { + (true, _, _, true) => (a, b), + (_, true, true, _) => (b, a), + _ => return false, + }; + if let Some(group) = group + && group + .lid_user_for_phone_user(&pn.user) + .is_some_and(|user| *user == lid.user) + { + return true; + } + resolver + .get_lid_for_phone(&pn.user) + .await + .is_some_and(|user| user == lid.user) +} + +impl SignalOps { + /// Refuse a retry for a device outside the original conversation before + /// any state changes: re-encrypting a stored message to a device the + /// caller picks would hand out past plaintext. Membership and PN/LID + /// mappings come from `resolver`, the same source a send trusts for its + /// device list. + async fn check_retry_requester( + &self, + resolver: &dyn SendContextResolver, + request: &RetryRequest<'_>, + own: &OwnIdentity, + audience: &[Jid], + ) -> Result<(), OpsError> { + let refuse = || OpsError::InvalidInput("the retry requester was not a recipient".into()); + // The route decides which membership check applies, so it must match + // the chat the message was saved under. + let route_matches = match request.route { + RetryRoute::Direct { .. } => { + !request.chat.is_group() && !request.chat.is_broadcast_list() + } + RetryRoute::Group { .. } => request.chat.is_group(), + RetryRoute::BroadcastList => request.chat.is_broadcast_list(), + }; + if !route_matches { + return Err(OpsError::InvalidInput( + "the retry route does not match the chat".into(), + )); + } + let group = match request.route { + RetryRoute::Direct { .. } => None, + RetryRoute::Group { .. } | RetryRoute::BroadcastList => Some( + resolver + .resolve_group_routing_info(request.chat) + .await + .map_err(send_error)?, + ), + }; + let group = group.as_deref(); + // The encryption address must belong to the requester itself. + if !same_account(resolver, group, request.requester, request.encryption_jid).await { + return Err(refuse()); + } + let mut from_own = false; + for own in std::iter::once(&own.pn).chain(own.lid.as_ref()) { + if same_account(resolver, group, own, request.requester).await { + from_own = true; + break; + } + } + // A direct retry's `recipient` is fixed by the saved chat: the chat + // user for one of this account's own devices, none otherwise. A + // wrong one would file the message under another conversation. + if let RetryRoute::Direct { recipient } = &request.route { + let recipient_ok = match (from_own, recipient) { + (true, Some(recipient)) => { + same_account(resolver, None, recipient, &request.chat.to_non_ad()).await + } + (false, None) => true, + _ => false, + }; + if !recipient_ok { + return Err(OpsError::InvalidInput( + "the retry recipient does not match the saved chat".into(), + )); + } + } + if from_own { + return Ok(()); + } + // Someone else must have been sent this message, and, for a group, + // still be a member: a later joiner gets no earlier messages, and a + // removed member gets nothing more. + let mut sent_to = false; + for account in audience { + if same_account(resolver, group, account, request.requester).await { + sent_to = true; + break; + } + } + if !sent_to { + return Err(refuse()); + } + if let Some(group) = group { + for member in &group.participants { + if same_account(resolver, Some(group), member, request.requester).await { + return Ok(()); + } + } + return Err(refuse()); + } + Ok(()) + } + + async fn own_identity(&self) -> Result { + let keys = self.keys().await; + let pn: Jid = keys + .own_pn + .as_deref() + .ok_or_else(|| OpsError::InvalidInput("device is not paired: own JID unknown".into()))? + .parse() + .map_err(|_| OpsError::InvalidInput("stored own JID does not parse".into()))?; + let lid: Option = keys + .own_lid + .as_deref() + .map(str::parse) + .transpose() + .map_err(|_| OpsError::InvalidInput("stored own LID does not parse".into()))?; + let account = keys + .account + .as_deref() + .map(waproto::codec::adv_signed_device_identity_decode) + .transpose() + .map_err(|_| { + OpsError::InvalidInput("stored account identity does not decode".into()) + })?; + Ok(OwnIdentity { pn, lid, account }) + } + + /// Build a direct-message stanza for `to`. Devices come from the + /// resolver (the WhatsApp client); missing sessions are set up from + /// bundles it fetches. + pub async fn send_direct( + &self, + runtime: &dyn Runtime, + resolver: &dyn SignalResolver, + to: &Jid, + message: &wa::Message, + message_id: &str, + ) -> Result { + let OwnIdentity { + pn: own_jid, + lid: own_lid, + account, + } = self.own_identity().await?; + + let mut devices = resolver + .resolve_devices(&[to.to_non_ad(), own_jid.to_non_ad()]) + .await + .map_err(send_error)?; + // Hosted (Cloud API) devices never get a copy, as in the client. + devices.retain(|device| !device.is_hosted()); + wacore::types::jid::sort_dedup_by_device(&mut devices); + let resolved = ResolvedDmDevices::new(devices, &own_jid, own_lid.as_ref()); + + // Fix each device's Signal address up front (its LID when the client + // knows one, as wacore's fan-out would) so the locks taken here cover + // exactly the sessions the encrypt will write. + let mut encryption = Vec::with_capacity(resolved.devices().len()); + for device in resolved.devices() { + encryption.push(encryption_jid(resolver, device).await); + } + // An existing PN session remains a valid fallback even when the client + // knows the preferred LID. Both addresses can be written by fan-out. + let lock_jids = dm_lock_jids(resolved.devices(), &encryption); + let dm_encryption = encryption.clone(); + let mut held_keys: Vec = lock_jids + .iter() + .map(|jid| session_key(&jid.to_protocol_address())) + .collect(); + held_keys.extend(lock_jids.iter().map(|jid| account_lock(&jid.user))); + let _ = resolved.signal_addressing_or_init(DmSignalAddressing::new(encryption, lock_jids)); + let held = self.lock_keys(held_keys).await?; + held.run(async { + let staged = std::sync::Arc::new(self.store().staged()); + let stores = self.stores_over(staged.clone()).await; + let (mut sender_keys, mut sessions, mut identities, mut prekeys, signed_prekeys) = ( + stores.clone(), + stores.clone(), + stores.clone(), + stores.clone(), + stores, + ); + let mut signal = SignalStores { + sender_key_store: &mut sender_keys, + session_store: &mut sessions, + identity_store: &mut identities, + prekey_store: &mut prekeys, + signed_prekey_store: &signed_prekeys, + }; + let verifying = Verifying::new(self, resolver, staged.clone()) + .with_addresses(resolved.devices().iter().zip(&dm_encryption)); + let prepared = prepare_dm_stanza( + runtime, + &mut signal, + &verifying, + DmStanzaRequest { + own_jid: &own_jid, + own_lid: own_lid.as_ref(), + account: account.as_ref(), + to, + message, + message_id, + edit: None, + extra_nodes: &[], + devices: &resolved, + pre_encoded: None, + }, + ) + .await + .map_err(send_error)?; + + let encoded = waproto::codec::message_to_vec(message); + staged + .put_aux( + Namespace::SentMessage, + &sent_key(to, message_id), + &encode_sent( + &encoded, + prepared.message_secret.as_ref(), + std::slice::from_ref(to), + ), + ) + .await?; + staged.commit().await?; + + let stanza = wacore_binary::marshal(&prepared.node) + .map_err(|e| OpsError::Send(format!("stanza did not marshal: {e}")))?; + Ok(PreparedSend { + stanza, + unreached_devices: prepared.unreached_devices, + phash: prepared.phash.map(|p| p.to_string()), + }) + }) + .await + } + + /// The message this device sent to `chat` with `message_id`, if still + /// kept. Used to answer retry receipts. + pub async fn sent_message( + &self, + chat: &Jid, + message_id: &str, + ) -> Result, OpsError> { + Ok(self + .sent_record(chat, message_id) + .await? + .map(|(message, _)| message)) + } + + /// A sent message and the audience it went to. + async fn sent_record( + &self, + chat: &Jid, + message_id: &str, + ) -> Result)>, OpsError> { + let Some(record) = self + .store() + .load_aux(Namespace::SentMessage, &sent_key(chat, message_id)) + .await? + else { + return Ok(None); + }; + let record = decode_sent(&record)?; + let mut message = waproto::codec::message_decode(record.body) + .map_err(|e| OpsError::InvalidInput(format!("sent message did not decode: {e}")))?; + if let Some(secret) = record.secret { + let mut context = message + .message_context_info + .as_option() + .cloned() + .unwrap_or_default(); + context.message_secret = Some(secret.to_vec()); + message.message_context_info = Some(context).into(); + } + let audience = record + .audience + .iter() + .map(|jid| jid.parse()) + .collect::, _>>() + .map_err(|_| OpsError::InvalidInput("sent-message audience is corrupt".into()))?; + Ok(Some((message, audience))) + } + + /// Build a group stanza: a sender-key message for the group plus, for + /// devices that do not yet hold this device's sender key, the key itself + /// encrypted pairwise. Membership and addressing come from the resolver. + pub async fn send_group( + &self, + runtime: &dyn Runtime, + resolver: &dyn SignalResolver, + group: &Jid, + message: &wa::Message, + message_id: &str, + ) -> Result { + let OwnIdentity { + pn: own_jid, + lid: own_lid, + account, + } = self.own_identity().await?; + let own_lid = own_lid.ok_or_else(|| { + OpsError::InvalidInput("group sends need this device's LID; call set_own_jids".into()) + })?; + let group_str = group.to_string(); + // Resolve membership while holding the rotation locks. A send that + // began before a removal may finish on the old chain, but cannot + // distribute a replacement chain using its old membership snapshot. + // g:/k: locks precede every s: lock in the common ordering. + let held = self + .lock_keys(vec![ + group_key(&group_str), + chain_key(&group_str, own_jid.to_protocol_address().as_str()), + chain_key(&group_str, own_lid.to_protocol_address().as_str()), + ]) + .await?; + held.run(async { + let routing = resolver + .resolve_group_routing_info(group) + .await + .map_err(send_error)?; + let is_lid_mode = routing.addressing_mode == AddressingMode::Lid; + let own_sending = if is_lid_mode { &own_lid } else { &own_jid }; + + // Same device query as the client's resolve_group_devices: LID-mode + // members are queried by phone number, then mapped back. + let mut queries: Vec = routing + .participants + .iter() + .map(|jid| { + if is_lid_mode + && jid.is_lid() + && let Some(pn) = routing.phone_jid_for_lid_user(&jid.user) + { + return pn.to_non_ad(); + } + jid.to_non_ad() + }) + .collect(); + if !routing + .participants + .iter() + .any(|participant| participant.is_same_user_as(own_sending)) + { + queries.push(own_jid.to_non_ad()); + } + let mut devices = resolver + .resolve_devices(&queries) + .await + .map_err(send_error)?; + if is_lid_mode { + devices = devices + .into_iter() + .map(|device| routing.phone_device_jid_into_lid(device)) + .collect(); + } + wacore::types::jid::sort_dedup_by_device(&mut devices); + let addressed = ResolvedGroupDevices::new(devices); + + let mut held_keys = Vec::new(); + let mut signal_addresses = std::collections::HashMap::new(); + for device in addressed.devices() { + let encryption = encryption_jid(resolver, device).await; + held_keys.push(session_key(&device.to_protocol_address())); + held_keys.push(session_key(&encryption.to_protocol_address())); + held_keys.push(account_lock(&device.user)); + held_keys.push(account_lock(&encryption.user)); + signal_addresses.insert(device.clone(), encryption); + } + let session_held = self.lock_keys(held_keys).await?; + session_held + .run(async { + let own_chain = SenderKeyName::from_parts( + &group_str, + own_sending.to_protocol_address().as_str(), + ); + // A missing chain means a fresh key: everyone needs it, as in the + // client's force_skdm path. + let force = self + .store() + .get_sender_key(own_chain.cache_key()) + .await? + .is_none(); + // Marks recorded for the other chain (the group switched + // between PN and LID addressing) do not count: start over. + let chain_changed = self + .store() + .load_aux( + Namespace::SenderKeyDevices, + &distribution_chain_key(&group_str), + ) + .await? + .as_deref() + != Some(own_chain.sender_id().as_bytes()); + // A device that holds the current key but is no longer + // addressed left the group (or unlinked): rotate, so it + // cannot read later messages on this chain. + let holders = self + .store() + .scan_aux( + Namespace::SenderKeyDevices, + &sender_key_holder_prefix(&group_str), + ) + .await?; + let addressed_holders: HashSet = addressed + .devices() + .iter() + .map(|device| sender_key_holder_key(&group_str, device)) + .collect(); + let addressed_users: HashSet<&str> = addressed + .devices() + .iter() + .map(|device| device.user.as_str()) + .collect(); + let holder_prefix = sender_key_holder_prefix(&group_str); + let removed = !force + && !chain_changed + && holders.iter().any(|holder| { + if addressed_holders.contains(holder) { + return false; + } + // A companion's primary is marked with it (see the + // warm check below) even when the server lists only + // the companion; that account has not left. + match holder + .strip_prefix(&holder_prefix) + .and_then(|device| device.parse::().ok()) + { + Some(device) => { + !(device.device == 0 + && addressed_users.contains(device.user.as_str())) + } + // An unreadable mark proves nothing either way; + // rotating is the safe choice. + None => true, + } + }); + let targets = if force || chain_changed || removed { + let mut all = addressed.devices().to_vec(); + retain_skdm_distribution_targets(&mut all, own_sending); + all + } else { + let warm: HashSet = self + .store() + .scan_aux(Namespace::SenderKeyDevices, &format!("{group_str}\u{0}")) + .await? + .into_iter() + .collect(); + let is_warm = + |jid: &Jid| warm.contains(&sender_key_device_key(&group_str, jid)); + addressed + .devices() + .iter() + .filter(|device| { + !(device.is_hosted() + || device.user == own_sending.user && device.device == own_sending.device + // WA Web treats a companion as warm only if its + // primary (device 0) is too. + || is_warm(device) && is_warm(&device.with_device(0))) + }) + .cloned() + .collect() + }; + + let staged = std::sync::Arc::new(self.store().staged()); + // A mode change deletes both chains too: returning to an + // earlier mode must not revive a chain whose holder history + // was cleared when the mode changed. + if removed || chain_changed { + for own in [&own_jid, &own_lid] { + let name = SenderKeyName::from_parts( + &group_str, + own.to_protocol_address().as_str(), + ); + staged.delete_sender_key(name.cache_key()).await?; + } + } + if chain_changed || removed { + let mut marks = staged + .scan_aux(Namespace::SenderKeyDevices, &format!("{group_str}\u{0}")) + .await?; + marks.extend( + staged + .scan_aux( + Namespace::SenderKeyDevices, + &sender_key_holder_prefix(&group_str), + ) + .await?, + ); + let refs: Vec<&str> = marks.iter().map(String::as_str).collect(); + staged + .delete_aux(Namespace::SenderKeyDevices, &refs) + .await?; + // A new epoch, so an acknowledgment of an old-chain send cannot + // mark a device for this chain. + staged + .put_aux( + Namespace::SenderKeyDevices, + &distribution_epoch_key(&group_str), + &new_epoch(), + ) + .await?; + staged + .put_aux( + Namespace::SenderKeyDevices, + &distribution_chain_key(&group_str), + own_chain.sender_id().as_bytes(), + ) + .await?; + } + let stores = self.stores_over(staged.clone()).await; + let ( + mut sender_keys, + mut sessions, + mut identities, + mut prekeys, + signed_prekeys, + ) = ( + stores.clone(), + stores.clone(), + stores.clone(), + stores.clone(), + stores, + ); + let mut signal = SignalStores { + sender_key_store: &mut sender_keys, + session_store: &mut sessions, + identity_store: &mut identities, + prekey_store: &mut prekeys, + signed_prekey_store: &signed_prekeys, + }; + let verifying = Verifying::new(self, resolver, staged.clone()) + .with_addresses(&signal_addresses); + let prepared = prepare_group_stanza_resolved( + runtime, + &mut signal, + &verifying, + GroupStanzaRequest { + group: &routing, + own_jid: &own_jid, + own_lid: &own_lid, + account: account.as_ref(), + to: group, + message, + message_id, + force_distribution: force || removed || chain_changed, + distribution_targets: Some(targets), + distribution_policy: SenderKeyDistributionPolicy::BestEffort, + phash_devices: Some(&addressed), + edit: None, + extra_nodes: &[], + pre_encoded: None, + }, + Some(&signal_addresses), + ) + .await + .map_err(send_error)?; + + // Record everyone this stanza gives the key to as a holder + // now, in the send's own batch: a lost runner reply must not + // leave a recipient who left later undetected. + for device in &prepared.skdm_devices { + staged + .put_aux( + Namespace::SenderKeyDevices, + &sender_key_holder_key(&group_str, device), + &[1], + ) + .await?; + } + let encoded = waproto::codec::message_to_vec(message); + staged + .put_aux( + Namespace::SentMessage, + &sent_key(group, message_id), + &encode_sent( + &encoded, + prepared.message_secret.as_ref(), + &routing.participants, + ), + ) + .await?; + staged.commit().await?; + + let stanza = wacore_binary::marshal(&prepared.node) + .map_err(|e| OpsError::Send(format!("stanza did not marshal: {e}")))?; + // Read under the group lock that rotation also takes. + let distribution_epoch = decode_epoch( + self.store() + .load_aux( + Namespace::SenderKeyDevices, + &distribution_epoch_key(&group_str), + ) + .await?, + ); + Ok(PreparedGroupSend { + stanza, + distribution_targets: prepared.skdm_devices, + distribution_epoch, + stale_device_users: prepared.stale_device_users, + phash: prepared.phash.map(|p| p.to_string()), + }) + }) + .await + }) + .await + } + + /// Record that the server acked a group send whose stanza carried the + /// sender key to `devices`. This account's own devices are never recorded + /// (WA Web's `!isMeDevice`), so they get the key on every send. + /// + /// `distribution_epoch` is the send's [`PreparedGroupSend::distribution_epoch`]. + /// Returns `false`, recording nothing, when the group's sender key has + /// rotated since that send. + pub async fn mark_sender_key_distributed( + &self, + group: &Jid, + devices: &[Jid], + distribution_epoch: u64, + ) -> Result { + let OwnIdentity { pn, lid, .. } = self.own_identity().await?; + let group_str = group.to_string(); + let held = self.lock_keys(vec![group_key(&group_str)]).await?; + held.run(async { + let current = decode_epoch( + self.store() + .load_aux( + Namespace::SenderKeyDevices, + &distribution_epoch_key(&group_str), + ) + .await?, + ); + if current != distribution_epoch { + return Ok(false); + } + for device in devices { + if device.is_same_user_as(&pn) + || lid.as_ref().is_some_and(|l| device.is_same_user_as(l)) + { + continue; + } + self.store() + .put_aux( + Namespace::SenderKeyDevices, + &sender_key_device_key(&group_str, device), + &[1], + ) + .await?; + self.store() + .put_aux( + Namespace::SenderKeyDevices, + &sender_key_holder_key(&group_str, device), + &[1], + ) + .await?; + } + Ok(true) + }) + .await + } + + /// Rotate this device's PN and LID sender-key chains, clear distribution + /// marks and start a new distribution epoch in one fenced batch. Use after membership changes so a removed + /// participant cannot decrypt later messages using the previous chain. + pub async fn forget_sender_key_devices(&self, group: &Jid) -> Result<(), OpsError> { + let OwnIdentity { pn, lid, .. } = self.own_identity().await?; + let group_str = group.to_string(); + let addresses: Vec<_> = std::iter::once(&pn) + .chain(lid.iter()) + .map(|jid| jid.to_protocol_address()) + .collect(); + let mut held_keys = vec![group_key(&group_str)]; + held_keys.extend( + addresses + .iter() + .map(|address| chain_key(&group_str, address.as_str())), + ); + let held = self.lock_keys(held_keys).await?; + held.run(async { + let staged = self.store().staged(); + for address in addresses { + let name = SenderKeyName::from_parts(&group_str, address.as_str()); + staged.delete_sender_key(name.cache_key()).await?; + } + let prefix = format!("{group}\u{0}"); + let mut keys = staged + .scan_aux(Namespace::SenderKeyDevices, &prefix) + .await?; + keys.extend( + staged + .scan_aux( + Namespace::SenderKeyDevices, + &sender_key_holder_prefix(&group_str), + ) + .await?, + ); + let refs: Vec<&str> = keys.iter().map(String::as_str).collect(); + staged + .delete_aux(Namespace::SenderKeyDevices, &refs) + .await?; + staged + .put_aux( + Namespace::SenderKeyDevices, + &distribution_epoch_key(&group_str), + &new_epoch(), + ) + .await?; + staged.commit().await?; + Ok(()) + }) + .await + } + + /// Re-encrypt a sent message for the device that asked for it. Without a + /// session and without keys in the receipt, a bundle is fetched through + /// the resolver, as the client does. Status messages are not handled here. + pub async fn resend( + &self, + resolver: &dyn SignalResolver, + request: RetryRequest<'_>, + ) -> Result, OpsError> { + let (message, audience) = self + .sent_record(request.chat, request.message_id) + .await? + .ok_or_else(|| OpsError::InvalidInput("no record of that sent message".into()))?; + let own = self.own_identity().await?; + let account = own.account.clone(); + let address = request.encryption_jid.to_protocol_address(); + + // A group retry also means the requester may have lost this device's + // sender key: forget its mark so the next group send redistributes it, + // as WA Web and the client do (mark_requester_for_fresh_skdm). + let mut held_keys = vec![ + session_key(&address), + account_lock(&request.encryption_jid.user), + account_lock(&request.requester.user), + ]; + if matches!(request.route, RetryRoute::Group { .. }) { + held_keys.push(group_key(&request.chat.to_string())); + } + let held = self.lock_keys(held_keys).await?; + held.run(async { + // Under the group lock that removal and rotation also take, so a + // membership answer cannot predate a removal that already ran. + self.check_retry_requester(resolver, &request, &own, &audience) + .await?; + let staged = std::sync::Arc::new(self.store().staged()); + // Staged with the session, so a refused bundle or a failed retry + // leaves the distribution state untouched. + if matches!(request.route, RetryRoute::Group { .. }) { + let group = request.chat.to_string(); + let keys: Vec = [request.requester, request.encryption_jid] + .into_iter() + .map(|jid| sender_key_device_key(&group, jid)) + .collect(); + let refs: Vec<&str> = keys.iter().map(String::as_str).collect(); + // A new epoch, so an acknowledgment still in flight from an + // earlier send cannot restore the mark deleted here. + staged + .put_aux( + Namespace::SenderKeyDevices, + &distribution_epoch_key(&group), + &new_epoch(), + ) + .await?; + staged + .delete_aux(Namespace::SenderKeyDevices, &refs) + .await?; + } + // The requester was checked to be the same account as its + // encryption address. A PN account pinned before its LID was + // known vouches for the LID too; an existing LID pin still + // decides, and this staged copy commits only with the retry. + if request.requester.is_pn() && request.encryption_jid.is_lid() { + self.mirror_pin( + &staged, + &request.requester.to_non_ad(), + &request.encryption_jid.to_non_ad(), + ) + .await?; + } + if let Some(bundle) = request.bundle { + self.establish_into(&staged, &address, bundle).await?; + } else if !staged.has_session(address.as_str()).await? { + let verifying = Verifying::new(self, resolver, staged.clone()); + let fetched = verifying + .fetch_prekeys(std::slice::from_ref(request.encryption_jid)) + .await + .map_err(send_error)?; + let bundle = fetched.get(request.encryption_jid).ok_or_else(|| { + OpsError::Send("no session and no prekey bundle for the requester".into()) + })?; + let mut sessions = self.stores_over(staged.clone()).await; + let mut identities = sessions.clone(); + process_prekey_bundle( + &address, + &mut sessions, + &mut identities, + bundle, + &mut rand::make_rng::(), + UsePQRatchet::No, + ) + .await?; + } + + let destination = match request.route { + RetryRoute::Direct { recipient } => PairwiseRetryDestination::Direct { + to: request.requester.clone(), + recipient, + }, + RetryRoute::Group { addressing_mode } => PairwiseRetryDestination::Participant { + to: request.chat.clone(), + participant: request.requester.clone(), + addressing_mode: Some(addressing_mode), + }, + RetryRoute::BroadcastList => PairwiseRetryDestination::Participant { + to: request.chat.clone(), + participant: request.requester.clone(), + addressing_mode: None, + }, + }; + let mut sessions = self.stores_over(staged.clone()).await; + let mut identities = sessions.clone(); + let node = prepare_pairwise_retry_stanza( + &mut sessions, + &mut identities, + PairwiseRetryRequest { + destination, + encryption_jid: request.encryption_jid.clone(), + message: &message, + message_id: request.message_id.to_owned(), + retry_count: request.retry_count, + account: account.as_ref(), + edit: EditAttribute::infer_from_message(&message), + pre_encoded: None, + }, + ) + .await + .map_err(send_error)?; + staged.commit().await?; + wacore_binary::marshal(&node) + .map_err(|e| OpsError::Send(format!("stanza did not marshal: {e}"))) + }) + .await + } + + /// Delete sent-message records inserted before `cutoff_ms`; a retry + /// receipt for an older message can no longer be answered. Returns how + /// many were removed. + pub async fn prune_sent_messages(&self, cutoff_ms: u64) -> Result { + let store = self.store(); + let mut expired = Vec::new(); + for key in store.scan_aux(Namespace::SentMessage, "").await? { + // A record that no longer decodes cannot serve a resend; delete it + // rather than let one corrupt entry stop every later prune. + if let Some(record) = store.load_aux(Namespace::SentMessage, &key).await? + && decode_sent(&record).map_or(true, |record| record.inserted_ms < cutoff_ms) + { + expired.push(key); + } + } + let refs: Vec<&str> = expired.iter().map(String::as_str).collect(); + store.delete_aux(Namespace::SentMessage, &refs).await?; + Ok(expired.len()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn direct_message_locks_cover_the_pn_fallback_and_preferred_lid() { + let pn: Jid = "222:1@s.whatsapp.net".parse().unwrap(); + let lid: Jid = "900222:1@lid".parse().unwrap(); + assert_eq!( + dm_lock_jids(std::slice::from_ref(&pn), std::slice::from_ref(&lid)), + vec![pn.clone(), lid] + ); + assert_eq!( + dm_lock_jids(std::slice::from_ref(&pn), std::slice::from_ref(&pn)), + vec![pn] + ); + } +} diff --git a/crates/signal-ops/src/prekeys.rs b/crates/signal-ops/src/prekeys.rs new file mode 100644 index 0000000..13ac984 --- /dev/null +++ b/crates/signal-ops/src/prekeys.rs @@ -0,0 +1,373 @@ +// SPDX-License-Identifier: MIT + +//! Durable public upload batches, separate from fresh retry prekeys. + +use super::*; +use rand::Rng; +use sha2::{Digest, Sha256}; +use std::collections::{BTreeMap, BTreeSet}; +use wacore::runtime::Runtime; +use wacore::store::error::StoreError; + +const PENDING: &str = "prekey-upload-v1"; +const LAST_ACK: &str = "prekey-upload-ack-v1"; +const MAX_SCAN_IDS: u32 = 4096; +const SCAN_CHUNK: u32 = 128; +const HEADER: usize = 1 + 16 + 2; +const ENTRY: usize = 4 + 32; + +/// Only public key material. Retry the same batch until the upload is acknowledged. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct PreKeyUploadBatch { + pub batch_id: String, + pub pre_keys: Vec, +} + +fn corrupt() -> OpsError { + StoreError::Validation("stored prekey upload batch is invalid".into()).into() +} + +fn count_valid(count: u32) -> Result<(), OpsError> { + if !(1..=812).contains(&count) { + return Err(OpsError::InvalidInput( + "prekey count must be 1 to 812".into(), + )); + } + Ok(()) +} + +impl PreKeyUploadBatch { + fn encode(&self) -> Result, OpsError> { + let id = hex::decode(&self.batch_id).map_err(|_| corrupt())?; + if id.len() != 16 || self.pre_keys.is_empty() || self.pre_keys.len() > 812 { + return Err(corrupt()); + } + let mut out = Vec::with_capacity(HEADER + ENTRY * self.pre_keys.len()); + out.push(1); + out.extend_from_slice(&id); + out.extend_from_slice(&(self.pre_keys.len() as u16).to_be_bytes()); + for key in &self.pre_keys { + out.extend_from_slice(&key.id.to_be_bytes()); + out.extend_from_slice(&key.public_key); + } + out.extend_from_slice(&Sha256::digest(&out)); + Ok(out) + } + + fn decode(bytes: &[u8]) -> Result { + if bytes.len() < HEADER || bytes[0] != 1 { + return Err(corrupt()); + } + let count = usize::from(u16::from_be_bytes([bytes[17], bytes[18]])); + let data_len = HEADER + count * ENTRY; + if count == 0 + || count > 812 + || bytes.len() != data_len + 32 + || Sha256::digest(&bytes[..data_len])[..] != bytes[data_len..] + { + return Err(corrupt()); + } + let mut seen = BTreeSet::new(); + let mut pre_keys = Vec::with_capacity(count); + // data_len is HEADER plus whole entries, so there is no remainder. + let (entries, _) = bytes[HEADER..data_len].as_chunks::(); + for entry in entries { + let id = u32::from_be_bytes(entry[..4].try_into().map_err(|_| corrupt())?); + if !(1..=MAX_PREKEY_ID).contains(&id) || !seen.insert(id) { + return Err(corrupt()); + } + pre_keys.push(PublicPreKey { + id, + public_key: entry[4..].try_into().map_err(|_| corrupt())?, + }); + } + Ok(Self { + batch_id: hex::encode(&bytes[1..17]), + pre_keys, + }) + } +} + +impl SignalOps { + async fn pending_upload(&self) -> Result, OpsError> { + self.store + .load_aux(Namespace::Device, PENDING) + .await? + .map(|bytes| PreKeyUploadBatch::decode(&bytes)) + .transpose() + } + + async fn live_upload_keys( + &self, + batch: &PreKeyUploadBatch, + ) -> Result, OpsError> { + let ids: Vec<_> = batch.pre_keys.iter().map(|key| key.id).collect(); + let records: BTreeMap<_, _> = self + .store + .load_prekeys_batch(&ids) + .await? + .into_iter() + .collect(); + let mut live = Vec::new(); + for key in &batch.pre_keys { + if let Some(bytes) = records.get(&key.id) { + let structure = + waproto::codec::pre_key_record_decode(bytes).map_err(|_| corrupt())?; + let record = + record_helpers::prekey_structure_to_record(structure).map_err(|_| corrupt())?; + let pair = record.key_pair().map_err(|_| corrupt())?; + if u32::from(record.id().map_err(|_| corrupt())?) != key.id + || public_bytes(&pair.public_key) != key.public_key + || public_bytes(&pair.private_key.public_key().map_err(|_| corrupt())?) + != key.public_key + { + return Err(corrupt()); + } + live.push(key.clone()); + } + } + Ok(live) + } + + /// Recover the outstanding batch after restart, excluding consumed keys. + /// `count` applies only when no live batch remains. Keep one SignalOps + /// instance per held scope; its device lock serializes upload and retry work. + /// New batches accept 1..=812 keys and inspect at most 4096 candidate IDs; + /// allocation exhaustion at that bound leaves the counter and keys unchanged. + /// Key generation runs through `runtime`'s blocking pool. + pub async fn prepare_prekey_upload( + &self, + runtime: &dyn Runtime, + count: u32, + ) -> Result { + count_valid(count)?; + let held = self.lock_device().await?; + held.run(async { + if let Some(batch) = self.pending_upload().await? { + let live = self.live_upload_keys(&batch).await?; + let staged = self.store.staged(); + if !live.is_empty() { + // A fenced write also refuses a superseded holder before any + // public batch is returned, including a recovered one. + staged + .put_aux(Namespace::Device, PENDING, &batch.encode()?) + .await?; + staged.commit().await?; + return Ok(PreKeyUploadBatch { + pre_keys: live, + ..batch + }); + } + staged.delete_aux(Namespace::Device, &[PENDING]).await?; + staged.commit().await?; + } + let mut batch_id = [0; 16]; + rng().fill_bytes(&mut batch_id); + let batch_id = hex::encode(batch_id); + let public = self + .allocate_prekeys(runtime, count, &BTreeSet::new(), Some(&batch_id)) + .await?; + Ok(PreKeyUploadBatch { + batch_id, + pre_keys: public, + }) + }) + .await + } + + /// Acknowledge exactly the uploaded IDs. They must be unique members of + /// this batch and cover every surviving private key. Consumed members may + /// be included so an in-flight upload can still be acknowledged safely. + /// The most recent exact acknowledgment remains replayable after restart. + pub async fn acknowledge_prekey_upload( + &self, + batch_id: &str, + exact_ids: &[u32], + ) -> Result<(), OpsError> { + let held = self.lock_device().await?; + held.run(async { + let invalid = || { + OpsError::InvalidInput( + "prekey upload batch or acknowledgment does not match".into(), + ) + }; + let ids: BTreeSet<_> = exact_ids.iter().copied().collect(); + if ids.is_empty() || ids.len() != exact_ids.len() { + return Err(invalid()); + } + if let Some(bytes) = self.store.load_aux(Namespace::Device, LAST_ACK).await? { + let acknowledged = PreKeyUploadBatch::decode(&bytes)?; + if acknowledged.batch_id == batch_id { + if acknowledged + .pre_keys + .iter() + .map(|key| key.id) + .collect::>() + != ids + { + return Err(invalid()); + } + let staged = self.store.staged(); + staged.put_aux(Namespace::Device, LAST_ACK, &bytes).await?; + staged.commit().await?; + return Ok(()); + } + } + let batch = self.pending_upload().await?.ok_or_else(invalid)?; + if batch.batch_id != batch_id { + return Err(invalid()); + } + let members: BTreeSet<_> = batch.pre_keys.iter().map(|key| key.id).collect(); + let live = self.live_upload_keys(&batch).await?; + if ids.len() != exact_ids.len() + || !ids.is_subset(&members) + || live.iter().any(|key| !ids.contains(&key.id)) + { + return Err(invalid()); + } + let staged = self.store.staged(); + staged.mark_prekeys_uploaded(exact_ids).await?; + let receipt = PreKeyUploadBatch { + batch_id: batch.batch_id, + pre_keys: batch + .pre_keys + .into_iter() + .filter(|key| ids.contains(&key.id)) + .collect(), + }; + staged + .put_aux(Namespace::Device, LAST_ACK, &receipt.encode()?) + .await?; + staged.delete_aux(Namespace::Device, &[PENDING]).await?; + staged.commit().await?; + Ok(()) + }) + .await + } + + pub(super) async fn generate_fresh_prekeys( + &self, + runtime: &dyn Runtime, + count: u32, + ) -> Result, OpsError> { + count_valid(count)?; + let held = self.lock_device().await?; + held.run(async { + let reserved = if let Some(batch) = self.pending_upload().await? { + self.live_upload_keys(&batch).await?; + batch.pre_keys.into_iter().map(|key| key.id).collect() + } else { + BTreeSet::new() + }; + self.allocate_prekeys(runtime, count, &reserved, None).await + }) + .await + } + + pub(super) async fn mark_fresh_prekeys_uploaded(&self, ids: &[u32]) -> Result<(), OpsError> { + let held = self.lock_device().await?; + held.run(async { + if let Some(batch) = self.pending_upload().await? { + let reserved: BTreeSet<_> = batch.pre_keys.iter().map(|key| key.id).collect(); + // Original membership, including consumed keys, remains batch + // authority until ACK or retirement. Check before any flag write. + if ids.iter().any(|id| reserved.contains(id)) { + return Err(OpsError::InvalidInput( + "pending upload members require batch acknowledgment".into(), + )); + } + } + Ok(self.store.mark_prekeys_uploaded(ids).await?) + }) + .await + } + + async fn allocate_prekeys( + &self, + runtime: &dyn Runtime, + count: u32, + reserved: &BTreeSet, + batch_id: Option<&str>, + ) -> Result, OpsError> { + let mut keys = self.durable_keys().await?; + if !(1..=MAX_PREKEY_ID).contains(&keys.next_prekey_id) { + return Err(corrupt()); + } + let mut ids = Vec::with_capacity(count as usize); + let mut next = keys.next_prekey_id; + for _ in 0..MAX_SCAN_IDS / SCAN_CHUNK { + let mut candidates = Vec::with_capacity(SCAN_CHUNK as usize); + for _ in 0..SCAN_CHUNK { + let id = next; + next = if next == MAX_PREKEY_ID { 1 } else { next + 1 }; + if !reserved.contains(&id) { + candidates.push(id); + } + } + let occupied: BTreeSet<_> = self + .store + .load_prekeys_batch(&candidates) + .await? + .into_iter() + .map(|(id, _)| id) + .collect(); + for id in candidates { + if !occupied.contains(&id) { + ids.push(id); + if ids.len() == count as usize { + next = if id == MAX_PREKEY_ID { 1 } else { id + 1 }; + break; + } + } + } + if ids.len() == count as usize { + break; + } + } + if ids.len() != count as usize { + return Err(OpsError::InvalidInput( + "prekey allocation exceeded the 4096-ID scan limit; no keys were created".into(), + )); + } + keys.next_prekey_id = next; + // Up to 812 key generations and encodes: off the async executor, as + // the client's own prekey upload does. + let (records, public) = wacore::runtime::blocking(runtime, move || { + let mut random = rng(); + let mut records = Vec::with_capacity(ids.len()); + let mut public = Vec::with_capacity(ids.len()); + for id in ids { + let pair = KeyPair::generate(&mut random); + let record = record_helpers::new_pre_key_record(id, &pair); + records.push(( + id, + bytes::Bytes::from(waproto::codec::pre_key_record_to_vec(&record)), + )); + public.push(PublicPreKey { + id, + public_key: public_bytes(&pair.public_key), + }); + } + (records, public) + }) + .await; + let staged = self.store.staged(); + staged.insert_prekeys_batch(&records).await?; + staged.save_device_keys(&keys.encode()?).await?; + if let Some(batch_id) = batch_id { + let batch = PreKeyUploadBatch { + batch_id: batch_id.to_owned(), + pre_keys: public.clone(), + }; + staged + .insert_aux(Namespace::Device, PENDING, &batch.encode()?) + .await?; + } + if let Err(error) = staged.commit().await { + self.refresh_keys().await; + return Err(error.into()); + } + *self.authority.keys.write().await = Arc::new(keys); + Ok(public) + } +} diff --git a/crates/signal-ops/src/stores.rs b/crates/signal-ops/src/stores.rs new file mode 100644 index 0000000..06d2c32 --- /dev/null +++ b/crates/signal-ops/src/stores.rs @@ -0,0 +1,286 @@ +// SPDX-License-Identifier: MIT + +//! libsignal protocol stores over a byte-level [`SignalStore`]. +//! +//! Records use the same for-store encodings as wacore's own direct-write +//! adapters, so a scope written here reads back in any wacore consumer. + +use crate::device::DeviceKeys; +use async_lock::Mutex; +use async_trait::async_trait; +use std::collections::HashMap; +use std::sync::{Arc, OnceLock}; +use wacore::libsignal::protocol::{ + Direction, GenericSignedPreKey, IdentityChange, IdentityKey, IdentityKeyPair, IdentityKeyStore, + PreKeyId, PreKeyRecord, PreKeyStore, ProtocolAddress, PublicKey, SenderKeyRecord, + SenderKeyStore, SessionRecord, SessionStore, SignalProtocolError, SignedPreKeyId, + SignedPreKeyRecord, SignedPreKeyStore, +}; +use wacore::libsignal::store::record_helpers; +use wacore::libsignal::store::sender_key_name::SenderKeyName; +use wacore::store::traits::SignalStore; + +type SignalResult = Result; + +/// One per process. Session and sender-key records carry the incarnation that +/// wrote them; a record read back under a different incarnation (another +/// process, or this one after a restart) is treated as possibly crashed, so +/// libsignal skips past chain positions it may already have used. +pub(crate) fn incarnation() -> &'static [u8; 16] { + static INCARNATION: OnceLock<[u8; 16]> = OnceLock::new(); + INCARNATION.get_or_init(|| { + use rand::Rng; + let mut bytes = [0u8; 16]; + rand::make_rng::().fill_bytes(&mut bytes); + bytes + }) +} + +fn backend(context: &'static str) -> impl FnOnce(E) -> SignalProtocolError +where + E: Into>, +{ + move |e| SignalProtocolError::BackendError(context, e.into()) +} + +pub(crate) struct Stores { + pub store: Arc, + pub keys: Arc, + pub sender_key_locks: Arc, +} + +impl Clone for Stores { + fn clone(&self) -> Self { + Self { + store: self.store.clone(), + keys: self.keys.clone(), + sender_key_locks: self.sender_key_locks.clone(), + } + } +} + +/// Lazily created per-key async mutexes. Entries are never removed; a node +/// serves a bounded set of chats per scope. +#[derive(Default)] +pub(crate) struct KeyedLocks { + locks: std::sync::Mutex>>>, +} + +impl KeyedLocks { + pub fn get(&self, key: &str) -> Arc> { + let mut locks = self + .locks + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + locks + .entry(key.to_owned()) + .or_insert_with(|| Arc::new(Mutex::new(()))) + .clone() + } +} + +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +impl SessionStore for Stores { + async fn load_session(&self, address: &ProtocolAddress) -> SignalResult> { + match self + .store + .get_session(address.as_str()) + .await + .map_err(backend("load_session"))? + { + Some(bytes) => Ok(Some(SessionRecord::deserialize_for_store( + &bytes, + incarnation(), + )?)), + None => Ok(None), + } + } + + async fn has_session(&self, address: &ProtocolAddress) -> SignalResult { + self.store + .has_session(address.as_str()) + .await + .map_err(backend("has_session")) + } + + async fn store_session( + &mut self, + address: &ProtocolAddress, + record: SessionRecord, + ) -> SignalResult<()> { + let mut bytes = Vec::new(); + record.serialize_into_for_store(&mut bytes, incarnation()); + self.store + .put_session(address.as_str(), &bytes) + .await + .map_err(backend("store_session")) + } +} + +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +impl IdentityKeyStore for Stores { + async fn get_identity_key_pair(&self) -> SignalResult { + Ok(self.keys.identity_pair()) + } + + async fn get_local_registration_id(&self) -> SignalResult { + Ok(self.keys.registration_id) + } + + async fn save_identity( + &mut self, + address: &ProtocolAddress, + identity: &IdentityKey, + ) -> SignalResult { + let existing = self.get_identity(address).await?; + let key: [u8; 32] = *identity.public_key().public_key_bytes(); + self.store + .put_identity(address.as_str(), key) + .await + .map_err(backend("save_identity"))?; + Ok(match existing { + Some(previous) if &previous != identity => IdentityChange::ReplacedExisting, + _ => IdentityChange::NewOrUnchanged, + }) + } + + async fn is_trusted_identity( + &self, + _address: &ProtocolAddress, + _identity: &IdentityKey, + _direction: Direction, + ) -> SignalResult { + // Same policy as wacore's own adapters and WA Web: identity changes + // surface through save_identity, never by refusing the message. + Ok(true) + } + + async fn get_identity(&self, address: &ProtocolAddress) -> SignalResult> { + match self + .store + .load_identity(address.as_str()) + .await + .map_err(backend("get_identity"))? + { + Some(bytes) => Ok(Some(IdentityKey::new( + PublicKey::from_djb_public_key_bytes(&bytes)?, + ))), + None => Ok(None), + } + } +} + +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +impl PreKeyStore for Stores { + async fn get_pre_key(&self, prekey_id: PreKeyId) -> SignalResult { + let bytes = self + .store + .load_prekey(prekey_id.into()) + .await + .map_err(backend("get_pre_key"))? + .ok_or(SignalProtocolError::InvalidPreKeyId)?; + let structure = waproto::codec::pre_key_record_decode(&bytes) + .map_err(|_| SignalProtocolError::InvalidProtobufEncoding)?; + record_helpers::prekey_structure_to_record(structure) + } + + async fn save_pre_key( + &mut self, + prekey_id: PreKeyId, + record: &PreKeyRecord, + ) -> SignalResult<()> { + let structure = record_helpers::prekey_record_to_structure(record)?; + self.store + .store_prekey( + prekey_id.into(), + &waproto::codec::pre_key_record_to_vec(&structure), + false, + ) + .await + .map_err(backend("save_pre_key")) + } + + async fn remove_pre_key(&mut self, prekey_id: PreKeyId) -> SignalResult<()> { + self.store + .remove_prekey(prekey_id.into()) + .await + .map_err(backend("remove_pre_key")) + } +} + +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +impl SignedPreKeyStore for Stores { + async fn get_signed_pre_key(&self, id: SignedPreKeyId) -> SignalResult { + let id: u32 = id.into(); + let structure = if id == self.keys.signed_prekey_id { + record_helpers::new_signed_pre_key_record( + id, + &self.keys.signed_prekey, + self.keys.signed_prekey_signature, + wacore::time::now_utc(), + )? + } else { + // A prekey message minted against a rotated-out signed prekey + // still names its old ID. + let bytes = self + .store + .load_signed_prekey(id) + .await + .map_err(backend("get_signed_pre_key"))? + .ok_or(SignalProtocolError::InvalidSignedPreKeyId)?; + waproto::codec::signed_pre_key_record_decode(&bytes) + .map_err(|_| SignalProtocolError::InvalidProtobufEncoding)? + }; + record_helpers::signed_prekey_structure_to_record(structure) + } + + async fn save_signed_pre_key( + &mut self, + id: SignedPreKeyId, + record: &SignedPreKeyRecord, + ) -> SignalResult<()> { + self.store + .store_signed_prekey(id.into(), &GenericSignedPreKey::serialize(record)?) + .await + .map_err(backend("save_signed_pre_key")) + } +} + +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +impl SenderKeyStore for Stores { + async fn store_sender_key( + &mut self, + name: &SenderKeyName, + record: SenderKeyRecord, + ) -> SignalResult<()> { + let bytes = record.serialize_for_store(incarnation())?; + self.store + .put_sender_key(name.cache_key(), &bytes) + .await + .map_err(backend("store_sender_key")) + } + + async fn load_sender_key(&self, name: &SenderKeyName) -> SignalResult> { + match self + .store + .get_sender_key(name.cache_key()) + .await + .map_err(backend("load_sender_key"))? + { + Some(bytes) => Ok(Some(SenderKeyRecord::deserialize_for_store( + &bytes, + incarnation(), + )?)), + None => Ok(None), + } + } + + async fn sender_key_lock(&self, name: &SenderKeyName) -> Arc> { + self.sender_key_locks.get(name.cache_key()) + } +} diff --git a/crates/signal-ops/src/trust.rs b/crates/signal-ops/src/trust.rs new file mode 100644 index 0000000..0e8a5f0 --- /dev/null +++ b/crates/signal-ops/src/trust.rs @@ -0,0 +1,452 @@ +// SPDX-License-Identifier: MIT + +//! Peer bundle verification. +//! +//! The client supplies every prekey bundle, so a bundle is only used once it +//! is bound to the peer account's pinned identity: +//! - a primary device's identity *is* the account key. The first one seen +//! is pinned; a different key later is refused until the application +//! accepts it with [`SignalOps::trust_account_identity`]; +//! - a companion device must arrive with its ADV ``, whose +//! account and device signatures must verify against the pinned key +//! (WA Web `WAWebSignalSessionApi.createSignalSession` and +//! `WAWebAdvSignatureApi.validateADVwithIdentityKey`; wacore's own +//! prekey parser applies the same rule). +//! +//! Unlike WA Web, a primary identity change is never accepted silently and an +//! in-blob account key never replaces the pinned one: the client that would +//! report either is the party this check distrusts. + +use crate::error::OpsError; +use crate::ops::{ServiceStore, SignalOps}; +use async_trait::async_trait; +use std::collections::HashMap; +use std::sync::Arc; +use wacore::adv::{AdvValidation, validate_adv_with_identity_key}; +use wacore::client::context::{GroupRoutingInfo, SendContextResolver, SessionLockGuard}; +use wacore::libsignal::protocol::{PreKeyBundle, ProtocolAddress}; +use wacore::prekeys::{PreKeyFetchOutcome, RejectedDevice}; +use wacore_binary::{Jid, JidExt as _}; +use wacore_recordstore::Namespace; + +/// A fetched bundle and the `` the server sent with it. +#[derive(Debug, Clone)] +pub struct FetchedBundle { + pub bundle: PreKeyBundle, + /// The `ADVSignedDeviceIdentity` bytes. Required for companion devices; + /// primary devices carry none. + pub device_identity: Option>, +} + +/// What one verifiable prekey fetch produced. +#[derive(Debug, Clone, Default)] +pub struct VerifiablePrekeys { + pub bundles: HashMap, + /// Devices the server rejected by name, as in [`PreKeyFetchOutcome`]. + pub rejected: Vec, +} + +/// The client side of a send: wacore's [`SendContextResolver`], plus prekey +/// fetches that keep each companion's ``. The service uses +/// only these fetches to set up sessions and verifies every bundle itself; +/// the resolver's own `fetch_prekeys` is not called. +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +pub trait SignalResolver: SendContextResolver { + async fn fetch_verifiable_prekeys(&self, jids: &[Jid]) -> anyhow::Result; +} + +/// Why a bundle was refused. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum BundleRejection { + #[error("the account's primary identity changed and was not accepted")] + IdentityChanged, + #[error("a companion bundle carries no device identity")] + MissingDeviceIdentity, + #[error("no pinned account identity to verify the companion against")] + NoAccountIdentity, + #[error("the companion's device identity does not verify against the account")] + InvalidDeviceIdentity, +} + +/// The device a Signal address names. WhatsApp addresses carry the device +/// in the name (Signal's device id is always 0) and spell the PN server as +/// legacy `c.us`; a plain libsignal address keeps it in the device id. +pub(crate) fn device_of(address: &ProtocolAddress) -> Result { + let name = address.name(); + let name = name + .strip_suffix("@c.us") + .map_or_else(|| name.to_owned(), |user| format!("{user}@s.whatsapp.net")); + let device = name + .parse::() + .map_err(|_| OpsError::InvalidInput(format!("{address} is not a device address")))?; + let device_id = u16::try_from(u32::from(address.device_id())) + .map_err(|_| OpsError::InvalidInput("device id out of range".into()))?; + Ok(if device.device == 0 && device_id != 0 { + device.with_device(device_id) + } else { + device + }) +} + +/// The record key of `device`'s account pin. A hosted device belongs to +/// its ordinary account (`hosted` to the phone number, `hosted.lid` to the +/// LID), as in WA Web's `asUserWidOrThrow`, so it shares that account's pin. +fn account_key(device: &Jid) -> String { + wacore::types::jid::adv_account_jid(device).to_string() +} + +fn is_primary(device: &Jid) -> bool { + device.device == 0 && !device.is_hosted() +} + +fn identity_bytes(bundle: &PreKeyBundle) -> Result<[u8; 32], OpsError> { + Ok(*bundle.identity_key()?.public_key().public_key_bytes()) +} + +fn decode_pin(bytes: Option>) -> Option<[u8; 32]> { + bytes.and_then(|bytes| <[u8; 32]>::try_from(bytes.as_slice()).ok()) +} + +impl SignalOps { + /// The pinned identity of `account` (any device of it), if one is known. + pub async fn account_identity(&self, account: &Jid) -> Result, OpsError> { + Self::pinned(self.store(), account).await + } + + /// Pin `identity` as `account`'s identity key, replacing any earlier pin. + /// Call this only once the application itself has accepted the key, for + /// example after the user confirmed a changed security code: the client + /// is not trusted to say so. It waits for sends, receives and migrations + /// that hold the account, so none of them can write over it afterwards. + pub async fn trust_account_identity( + &self, + account: &Jid, + identity: [u8; 32], + ) -> Result<(), OpsError> { + let held = self + .lock_keys(vec![crate::ops::account_lock(&account.user)]) + .await?; + held.run(async { + Ok(self + .store() + .put_aux(Namespace::AccountIdentity, &account_key(account), &identity) + .await?) + }) + .await + } + + /// Drop `account`'s pinned identity, so the next primary identity seen + /// for it is pinned in its place. For a service that trusts its client to + /// report identity changes. Pins are per address form, so call this for + /// each form (PN and LID) the account is known by. + pub async fn forget_account_identity(&self, account: &Jid) -> Result<(), OpsError> { + let held = self + .lock_keys(vec![crate::ops::account_lock(&account.user)]) + .await?; + let key = account_key(account); + held.run(async { + Ok(self + .store() + .delete_aux(Namespace::AccountIdentity, &[key.as_str()]) + .await?) + }) + .await + } + + async fn pinned(store: &S, device: &Jid) -> Result, OpsError> { + Ok(decode_pin( + store + .load_aux(Namespace::AccountIdentity, &account_key(device)) + .await?, + )) + } + + /// Whether `device` is one of this account's devices, by its stored PN + /// and LID. Never taken from the client. + pub(crate) async fn is_own_account(&self, device: &Jid) -> bool { + let keys = self.keys().await; + let account = wacore::types::jid::adv_account_jid(device); + [keys.own_pn.as_deref(), keys.own_lid.as_deref()] + .into_iter() + .flatten() + .filter_map(|jid| jid.parse::().ok()) + .any(|jid| jid.to_non_ad() == account) + } + + /// This device's own account key, from its pairing, when `device` belongs + /// to this account. Own devices are anchored to it rather than to a pin. + async fn own_account(&self, device: &Jid) -> Option<[u8; 32]> { + if !self.is_own_account(device).await { + return None; + } + let keys = self.keys().await; + let signed = + waproto::codec::adv_signed_device_identity_decode(keys.account.as_deref()?).ok()?; + <[u8; 32]>::try_from(signed.account_signature_key?.as_slice()).ok() + } + + /// Verify `bundle` for `device`; see [`Self::verify_identity`]. + pub(crate) async fn verify_bundle( + &self, + store: &S, + device: &Jid, + bundle: &PreKeyBundle, + device_identity: Option<&[u8]>, + primary_hint: Option<[u8; 32]>, + ) -> Result, OpsError> { + self.verify_identity( + store, + device, + identity_bytes(bundle)?, + device_identity, + primary_hint, + ) + .await + } + + /// Verify that `identity` may start a session with `device`, pinning a + /// first-seen account identity in `store`, the batch that session commits + /// in. `primary_hint` is the account's primary identity fetched in the + /// same batch, used when nothing is pinned yet. A first-use pin is an + /// exclusive insert, so a key the application accepted meanwhile is never + /// overwritten; that commit fails instead. + pub(crate) async fn verify_identity( + &self, + store: &S, + device: &Jid, + identity: [u8; 32], + device_identity: Option<&[u8]>, + primary_hint: Option<[u8; 32]>, + ) -> Result, OpsError> { + let own = self.own_account(device).await; + let pinned = match own { + Some(own) => Some(own), + None => Self::pinned(store, device).await?, + }; + if is_primary(device) { + return Ok(match pinned { + Some(pinned) if pinned != identity => Err(BundleRejection::IdentityChanged), + Some(_) => Ok(()), + None => { + store + .insert_aux(Namespace::AccountIdentity, &account_key(device), &identity) + .await?; + Ok(()) + } + }); + } + let Some(blob) = device_identity else { + return Ok(Err(BundleRejection::MissingDeviceIdentity)); + }; + let Some(account) = pinned.or(primary_hint) else { + return Ok(Err(BundleRejection::NoAccountIdentity)); + }; + if validate_adv_with_identity_key(blob, &identity, Some(&account), device) + != AdvValidation::Valid + { + return Ok(Err(BundleRejection::InvalidDeviceIdentity)); + } + if pinned.is_none() { + store + .insert_aux(Namespace::AccountIdentity, &account_key(device), &account) + .await?; + } + Ok(Ok(())) + } + + /// Copy `from`'s account pin to `to`'s account in `store` when `to` has + /// none. Both name the same account in different address forms. False + /// when `to` is already pinned to another key: a session at `to` must not + /// be built from keys that only `from`'s pin vouches for. + pub(crate) async fn mirror_pin( + &self, + store: &S, + from: &Jid, + to: &Jid, + ) -> Result { + let Some(pin) = Self::pinned(store, from).await? else { + return Ok(true); + }; + match Self::pinned(store, to).await? { + Some(existing) => Ok(existing == pin), + None => { + store + .insert_aux(Namespace::AccountIdentity, &account_key(to), &pin) + .await?; + Ok(true) + } + } + } + + /// Whether `device` is one of this account's own devices, anchored by + /// this device's pairing. Before pairing nothing is: a sender that only + /// names the stored own phone number or LID proves nothing. + pub(crate) async fn is_anchored_own_device(&self, device: &Jid) -> bool { + self.own_account(device).await.is_some() + } + + /// Verify a fetch, returning only the bundles that pass. Primary bundles + /// are checked first so a fresh account's primary can anchor its + /// companions in the same fetch. + pub(crate) async fn verify_fetched( + &self, + store: &S, + fetched: VerifiablePrekeys, + ) -> Result<(HashMap, Vec), OpsError> { + let mut accepted = HashMap::with_capacity(fetched.bundles.len()); + let mut primaries: HashMap = HashMap::new(); + let mut entries: Vec<(Jid, FetchedBundle)> = fetched.bundles.into_iter().collect(); + entries.sort_by_key(|(jid, _)| !is_primary(jid)); + for (jid, fetched) in entries { + let hint = primaries.get(&account_key(&jid)).copied(); + match self + .verify_bundle( + store, + &jid, + &fetched.bundle, + fetched.device_identity.as_deref(), + hint, + ) + .await? + { + Ok(()) => { + if is_primary(&jid) { + primaries.insert(account_key(&jid), identity_bytes(&fetched.bundle)?); + } + accepted.insert(jid, fetched.bundle); + } + Err(reason) => log::warn!("refusing a prekey bundle for {jid}: {reason}"), + } + } + Ok((accepted, fetched.rejected)) + } +} + +/// Wraps the client's resolver for wacore's send path, so every bundle a send +/// installs has passed [`SignalOps::verify_bundle`]. +pub(crate) struct Verifying<'a, S> { + pub ops: &'a SignalOps, + pub inner: &'a dyn SignalResolver, + pub store: Arc, + /// The LID account each phone-number user is encrypted to in this + /// operation, as fixed before its locks were taken. + pub lids: HashMap, +} + +impl<'a, S> Verifying<'a, S> { + pub fn new(ops: &'a SignalOps, inner: &'a dyn SignalResolver, store: Arc) -> Self { + Self { + ops, + inner, + store, + lids: HashMap::new(), + } + } + + /// Record the addresses an operation encrypts to, `(device, address)`. + pub fn with_addresses<'j>( + mut self, + pairs: impl IntoIterator, + ) -> Self { + for (device, address) in pairs { + if device.is_pn() && address.is_lid() { + self.lids + .insert(device.user.to_string(), address.to_non_ad()); + } + } + self + } +} + +/// Carry `error` through wacore's `anyhow` boundary intact, so the send +/// can return it typed (a lost fence must still read as one). +pub(crate) fn op_error(error: OpsError) -> anyhow::Error { + anyhow::Error::new(error) +} + +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +impl SendContextResolver for Verifying<'_, S> { + fn sender_identity_role(&self) -> wacore::send::SenderIdentityRole { + self.inner.sender_identity_role() + } + + async fn resolve_devices(&self, jids: &[Jid]) -> anyhow::Result> { + self.inner.resolve_devices(jids).await + } + + async fn fetch_prekeys(&self, jids: &[Jid]) -> anyhow::Result> { + Ok(self.fetch_prekeys_for_identity_check(jids).await?.bundles) + } + + async fn fetch_prekeys_for_identity_check( + &self, + jids: &[Jid], + ) -> anyhow::Result { + let fetched = self.inner.fetch_verifiable_prekeys(jids).await?; + let (bundles, rejected) = self + .ops + .verify_fetched(&self.store, fetched) + .await + .map_err(op_error)?; + // A session for a PN device is set up at its LID when the mapping is + // known, and later messages arrive from that LID; pin the account + // there too. An existing LID pin is kept, and when it names another + // key the user's bundles are dropped: they verified only against the + // PN pin, and the session would be built at the LID. + let mut bundles = bundles; + let mut mapped: Vec = bundles + .keys() + .filter(|jid| jid.is_pn() && self.lids.contains_key(jid.user.as_str())) + .cloned() + .collect(); + mapped.sort_by_key(|jid| jid.to_string()); + let mut checked = std::collections::HashSet::new(); + let mut conflicting = std::collections::HashSet::new(); + for jid in mapped { + let lid = &self.lids[jid.user.as_str()]; + // Per PN user: two users mapped to one LID each need their own + // pin to agree with it. + if checked.insert((jid.user.clone(), lid.clone())) + && !self + .ops + .mirror_pin(&self.store, &jid, lid) + .await + .map_err(op_error)? + { + conflicting.insert(jid.user.clone()); + } + } + bundles.retain(|jid, _| !(jid.is_pn() && conflicting.contains(&jid.user))); + Ok(PreKeyFetchOutcome { bundles, rejected }) + } + + async fn resolve_group_routing_info(&self, jid: &Jid) -> anyhow::Result> { + self.inner.resolve_group_routing_info(jid).await + } + + async fn get_lid_for_phone(&self, phone_user: &str) -> Option { + self.inner.get_lid_for_phone(phone_user).await + } + + fn on_local_identity_change(&self, jid: &Jid) { + self.inner.on_local_identity_change(jid); + } + + fn on_local_identity_change_with_keys( + &self, + jid: &Jid, + change: wacore::libsignal::protocol::IdentityChange, + ) { + self.inner.on_local_identity_change_with_keys(jid, change); + } + + fn on_unkeyable_devices(&self, reason: wacore::stats::UnkeyableDevice, count: u64) { + self.inner.on_unkeyable_devices(reason, count); + } + + async fn lock_device_sessions(&self, device_jids: &[Jid]) -> SessionLockGuard { + self.inner.lock_device_sessions(device_jids).await + } +} diff --git a/crates/signal-ops/tests/cancellation.rs b/crates/signal-ops/tests/cancellation.rs new file mode 100644 index 0000000..9499d0d --- /dev/null +++ b/crates/signal-ops/tests/cancellation.rs @@ -0,0 +1,393 @@ +// SPDX-License-Identifier: MIT + +//! Submitted storage writes may outlive the caller that awaited them. +#![cfg(not(target_arch = "wasm32"))] + +use async_trait::async_trait; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; +use wacore::libsignal::protocol::{DeviceId, ProtocolAddress}; +use wacore::store::error::{Result, StoreError}; +use wacore_recordstore::{ + AesGcmSealer, Fence, LeaseStore, MemoryRecordStore, Namespace, RecordSignalStore, RecordStore, + WriteOp, is_fence_lost, +}; +use wacore_signal_ops::{RemoteBundle, SignalOps}; + +mod common; +mod runtime; +use runtime::InlineRuntime; + +const TTL: u64 = 60_000; +const DATA_KEY: [u8; 32] = [42; 32]; + +struct DelayedRecords { + inner: Arc, + pause: AtomicBool, + submitted: tokio::sync::Notify, + resume: Arc, + finished: Arc, + committed: Arc, + refused: Arc, +} + +impl DelayedRecords { + fn new() -> Self { + Self { + inner: Arc::new(MemoryRecordStore::new()), + pause: AtomicBool::new(false), + submitted: tokio::sync::Notify::new(), + resume: Arc::new(tokio::sync::Notify::new()), + finished: Arc::new(tokio::sync::Notify::new()), + committed: Arc::new(AtomicBool::new(false)), + refused: Arc::new(AtomicBool::new(false)), + } + } +} + +#[async_trait] +impl RecordStore for DelayedRecords { + async fn get(&self, scope: &str, ns: Namespace, key: &str) -> Result>> { + self.inner.get(scope, ns, key).await + } + + async fn get_many( + &self, + scope: &str, + ns: Namespace, + keys: &[&str], + ) -> Result)>> { + self.inner.get_many(scope, ns, keys).await + } + + async fn scan_keys( + &self, + scope: &str, + ns: Namespace, + prefix: &str, + limit: Option, + ) -> Result> { + self.inner.scan_keys(scope, ns, prefix, limit).await + } + + async fn write(&self, fence: &Fence, ops: &[WriteOp]) -> Result<()> { + let changes_session = ops.iter().any(|op| { + matches!( + op, + WriteOp::Put { + ns: Namespace::Session, + .. + } + ) + }); + if !changes_session || !self.pause.swap(false, Ordering::SeqCst) { + return self.inner.write(fence, ops).await; + } + let (send, receive) = tokio::sync::oneshot::channel(); + let inner = self.inner.clone(); + let fence = fence.clone(); + let ops = ops.to_vec(); + let resume = self.resume.clone(); + let finished = self.finished.clone(); + let committed = self.committed.clone(); + let refused = self.refused.clone(); + // Model a submitted database operation: dropping its receiver does + // not cancel the work already handed to the database worker. + tokio::spawn(async move { + resume.notified().await; + let result = inner.write(&fence, &ops).await; + committed.store(result.is_ok(), Ordering::SeqCst); + refused.store(result.as_ref().is_err_and(is_fence_lost), Ordering::SeqCst); + let _ = send.send(result); + finished.notify_one(); + }); + self.submitted.notify_one(); + receive + .await + .map_err(|_| StoreError::Validation("fixture write worker stopped".into()))? + } +} + +async fn cancellation_case(release_before_takeover: bool) { + let records = Arc::new(DelayedRecords::new()); + let lease = records + .inner + .acquire("alice", "first", 0, TTL) + .await + .unwrap() + .unwrap(); + let alice = Arc::new( + SignalOps::create(Arc::new(RecordSignalStore::new( + records.clone(), + Arc::new(AesGcmSealer::new(&DATA_KEY)), + lease.fence, + ))) + .await + .unwrap(), + ); + let bob_records = Arc::new(MemoryRecordStore::new()); + let bob_lease = bob_records + .acquire("bob", "holder", 0, TTL) + .await + .unwrap() + .unwrap(); + let bob = SignalOps::create(Arc::new(RecordSignalStore::new( + bob_records, + Arc::new(AesGcmSealer::new(&DATA_KEY)), + bob_lease.fence, + ))) + .await + .unwrap(); + let device_identity = common::vouch(&alice, &bob, "222@s.whatsapp.net").await; + let public = bob.public_identity().await; + let prekey = bob + .generate_prekeys(&InlineRuntime, 1) + .await + .unwrap() + .remove(0); + let bundle = RemoteBundle { + registration_id: public.registration_id, + device_id: 1, + identity_key: public.identity_key, + signed_prekey_id: public.signed_prekey.id, + signed_prekey: public.signed_prekey.public_key, + signed_prekey_signature: public.signed_prekey.signature, + prekey: Some((prekey.id, prekey.public_key)), + device_identity: Some(device_identity), + }; + let alice_address = ProtocolAddress::new("111@s.whatsapp.net", DeviceId::from(1)); + let alice_identity = common::vouch(&bob, &alice, alice_address.name()).await; + let bob_address = ProtocolAddress::new("222@s.whatsapp.net", DeviceId::from(1)); + alice + .establish_session(&bob_address, &bundle) + .await + .unwrap(); + records.pause.store(true, Ordering::SeqCst); + let encrypt = { + let alice = alice.clone(); + let bob_address = bob_address.clone(); + tokio::spawn(async move { alice.encrypt(&bob_address, b"lost caller").await }) + }; + tokio::time::timeout( + std::time::Duration::from_secs(5), + records.submitted.notified(), + ) + .await + .expect("ratchet write was not submitted"); + let (started, waiting) = tokio::sync::oneshot::channel(); + let waiter = { + let alice = alice.clone(); + let bob_address = bob_address.clone(); + tokio::spawn(async move { + let _ = started.send(()); + alice + .encrypt(&bob_address, b"queued behind the uncertain write") + .await + }) + }; + waiting.await.unwrap(); + encrypt.abort(); + assert!(encrypt.await.unwrap_err().is_cancelled()); + assert!(!alice.is_usable()); + assert!(waiter.await.unwrap().unwrap_err().is_fence_lost()); + let refused = alice + .encrypt(&bob_address, b"must not publish under the uncertain fence") + .await + .unwrap_err(); + assert!(refused.is_fence_lost()); + if release_before_takeover { + records.resume.notify_one(); + records.finished.notified().await; + assert!(records.committed.load(Ordering::SeqCst)); + } + let higher = records + .inner + .acquire("alice", "replacement", TTL + 1, TTL) + .await + .unwrap() + .unwrap(); + let fresh = SignalOps::open(Arc::new(RecordSignalStore::new( + records.clone(), + Arc::new(AesGcmSealer::new(&DATA_KEY)), + higher.fence, + ))) + .await + .unwrap(); + let first = fresh + .encrypt(&bob_address, b"published first") + .await + .unwrap(); + assert_eq!( + bob.decrypt( + &alice_address, + first.kind, + &first.ciphertext, + Some(&alice_identity), + ) + .await + .unwrap() + .plaintext, + b"published first" + ); + if !release_before_takeover { + records.resume.notify_one(); + records.finished.notified().await; + assert!( + records.refused.load(Ordering::SeqCst), + "the delayed old-generation write was accepted after takeover" + ); + } + let second = fresh + .encrypt(&bob_address, b"published second") + .await + .unwrap(); + assert_eq!( + bob.decrypt( + &alice_address, + second.kind, + &second.ciphertext, + Some(&alice_identity), + ) + .await + .unwrap() + .plaintext, + b"published second" + ); +} + +#[tokio::test] +async fn a_cancelled_ratchet_write_cannot_be_followed_by_another_write_on_the_same_instance() { + cancellation_case(true).await; +} + +#[tokio::test] +async fn a_delayed_cancelled_write_cannot_roll_back_the_replacement_fence() { + cancellation_case(false).await; +} + +/// Pauses the first device-key read after it has read, before it returns. +struct SlowDeviceRead { + inner: Arc, + pause: AtomicBool, + reading: tokio::sync::Notify, + resume: tokio::sync::Notify, +} + +#[async_trait] +impl RecordStore for SlowDeviceRead { + async fn get(&self, scope: &str, ns: Namespace, key: &str) -> Result>> { + let value = self.inner.get(scope, ns, key).await; + if ns == Namespace::Device && self.pause.swap(false, Ordering::SeqCst) { + self.reading.notify_one(); + self.resume.notified().await; + } + value + } + + async fn get_many( + &self, + scope: &str, + ns: Namespace, + keys: &[&str], + ) -> Result)>> { + self.inner.get_many(scope, ns, keys).await + } + + async fn scan_keys( + &self, + scope: &str, + ns: Namespace, + prefix: &str, + limit: Option, + ) -> Result> { + self.inner.scan_keys(scope, ns, prefix, limit).await + } + + async fn write(&self, fence: &Fence, ops: &[WriteOp]) -> Result<()> { + self.inner.write(fence, ops).await + } +} + +#[tokio::test(flavor = "multi_thread")] +async fn an_open_overlapping_a_live_instance_cannot_restore_its_older_keys() { + let records = Arc::new(SlowDeviceRead { + inner: Arc::new(MemoryRecordStore::new()), + pause: AtomicBool::new(false), + reading: tokio::sync::Notify::new(), + resume: tokio::sync::Notify::new(), + }); + let lease = records + .inner + .acquire("slow-open", "node-a", 0, TTL) + .await + .unwrap() + .unwrap(); + let store = || { + Arc::new(RecordSignalStore::new( + records.clone(), + Arc::new(AesGcmSealer::new(&DATA_KEY)), + lease.fence.clone(), + )) + }; + let first = SignalOps::create(store()).await.unwrap(); + + // The second open starts while the first instance is live. If it reads + // the device keys before joining the first's authority, hold that read + // while the first rotates and goes away. + records.pause.store(true, Ordering::SeqCst); + let mut opening = tokio::spawn(SignalOps::open(store())); + let opened = tokio::select! { + () = records.reading.notified() => None, + opened = &mut opening => Some(opened), + }; + // The fixed open never reads; keep the pause for the open alone. + records.pause.store(false, Ordering::SeqCst); + let rotated = first.rotate_signed_prekey().await.unwrap(); + drop(first); + records.resume.notify_one(); + let second = match opened { + Some(opened) => opened, + None => opening.await, + } + .unwrap() + .unwrap(); + + assert_eq!( + second.public_identity().await.signed_prekey.id, + rotated.id, + "the second open must see the first instance's rotation" + ); + let next = second.rotate_signed_prekey().await.unwrap(); + assert_ne!(next.id, rotated.id, "a rotation must not reuse the live ID"); +} + +#[tokio::test(flavor = "multi_thread")] +async fn an_open_with_a_different_sealer_cannot_join_a_live_instance() { + let records = Arc::new(MemoryRecordStore::new()); + let lease = records + .acquire("wrong-sealer", "node-a", 0, TTL) + .await + .unwrap() + .unwrap(); + let store = |key: &[u8; 32]| { + Arc::new(RecordSignalStore::new( + records.clone(), + Arc::new(AesGcmSealer::new(key)), + lease.fence.clone(), + )) + }; + let first = SignalOps::create(store(&DATA_KEY)).await.unwrap(); + let identity = first.public_identity().await.identity_key; + + // Same backend, scope and fence, so it would share the live authority. + assert!( + SignalOps::open(store(&[7; 32])).await.is_err(), + "a store that cannot open the device keys must not join" + ); + + // Nothing was resealed: the original sealer still opens the device. + first.rotate_signed_prekey().await.unwrap(); + drop(first); + let reopened = SignalOps::open(store(&DATA_KEY)).await.unwrap(); + assert_eq!(reopened.public_identity().await.identity_key, identity); +} diff --git a/crates/signal-ops/tests/common/mod.rs b/crates/signal-ops/tests/common/mod.rs new file mode 100644 index 0000000..ecb6159 --- /dev/null +++ b/crates/signal-ops/tests/common/mod.rs @@ -0,0 +1,117 @@ +// SPDX-License-Identifier: MIT + +//! Helpers shared by the integration tests. +#![allow(dead_code)] + +/// A pair-success container as the primary phone builds it: the account key +/// signs our identity, and the whole blob is HMACed with the QR's ADV secret. +// Test-only protos with no waproto::codec helper; the repo allows this in tests. +#[allow(clippy::disallowed_methods)] +pub fn pair_success_container( + device_identity: [u8; 32], + adv_secret: &[u8; 32], + key_index: u32, +) -> (Vec, wacore::libsignal::protocol::KeyPair) { + let mut rng = rand::make_rng::(); + let account = wacore::libsignal::protocol::KeyPair::generate(&mut rng); + let container = pair_success_container_for(&account, device_identity, adv_secret, key_index); + (container, account) +} + +/// As [`pair_success_container`], signed by an existing `account` key, the way +/// a primary re-pairs a reset or replacement companion under the same account. +#[allow(clippy::disallowed_methods)] +pub fn pair_success_container_for( + account: &wacore::libsignal::protocol::KeyPair, + device_identity: [u8; 32], + adv_secret: &[u8; 32], + key_index: u32, +) -> Vec { + use buffa::Message; + use hmac::{KeyInit, Mac}; + use wacore::libsignal::protocol::{KeyPair, PublicKey}; + use waproto::whatsapp as wa; + + let mut rng = rand::make_rng::(); + // Only the public half is read when no device prefix is requested. + let device = KeyPair::new( + PublicKey::from_djb_public_key_bytes(&device_identity).unwrap(), + KeyPair::generate(&mut rng).private_key, + ); + let mut details = wa::ADVDeviceIdentity::default(); + details.raw_id = Some(1); + details.timestamp = Some(0); + details.key_index = Some(key_index); + details.account_type = Some(wa::ADVEncryptionType::E2EE); + details.device_type = Some(wa::ADVEncryptionType::E2EE); + let details = details.encode_to_vec(); + let signed = wacore::adv::test_util::signed_identity( + account, + &device, + &details, + wacore::adv::test_util::account_prefix(Some(wa::ADVEncryptionType::E2EE)), + None, + true, + ) + .encode_to_vec(); + let mut mac = as KeyInit>::new_from_slice(adv_secret).unwrap(); + mac.update(&signed); + let mut container = wa::ADVSignedDeviceIdentityHMAC::default(); + container.details = Some(signed); + container.hmac = Some(mac.finalize().into_bytes().to_vec()); + container.account_type = Some(wa::ADVEncryptionType::E2EE); + container.encode_to_vec() +} + +/// A paired device as its peers see it: the account key that signed it, and +/// the `` its prekey bundles carry. +pub struct Paired { + pub account: [u8; 32], + /// The account key pair, to re-pair a replacement device under it. + pub account_keys: wacore::libsignal::protocol::KeyPair, + pub device_identity: Vec, +} + +/// Pair `ops` the way a real primary would, so it holds an account identity. +pub async fn pair( + ops: &wacore_signal_ops::SignalOps, +) -> Paired { + let identity = ops.public_identity().await; + let (container, account) = + pair_success_container(identity.identity_key, &identity.adv_secret, 1); + let signed = ops.sign_pairing(&container).await.unwrap(); + Paired { + account: *account.public_key.public_key_bytes(), + account_keys: account, + device_identity: signed.signed_identity, + } +} + +/// Pair `ops` under an existing account key, as a primary does for a device +/// that was reset or replaced. Returns its new ``. +pub async fn pair_under( + ops: &wacore_signal_ops::SignalOps, + account: &wacore::libsignal::protocol::KeyPair, +) -> Vec { + let identity = ops.public_identity().await; + let container = + pair_success_container_for(account, identity.identity_key, &identity.adv_secret, 1); + ops.sign_pairing(&container).await.unwrap().signed_identity +} + +/// Pair `peer` under a fresh account and have `sender` pin that account for +/// `peer_jid`, as it would after meeting the peer's primary device. Returns +/// the `` the peer's bundle must carry. +pub async fn vouch( + sender: &wacore_signal_ops::SignalOps, + peer: &wacore_signal_ops::SignalOps

, + peer_jid: &str, +) -> Vec { + let paired = pair(peer).await; + let jid: wacore_binary::Jid = peer_jid.parse().unwrap(); + sender + .trust_account_identity(&jid, paired.account) + .await + .unwrap(); + paired.device_identity +} diff --git a/crates/signal-ops/tests/end_to_end.rs b/crates/signal-ops/tests/end_to_end.rs new file mode 100644 index 0000000..b6bd79c --- /dev/null +++ b/crates/signal-ops/tests/end_to_end.rs @@ -0,0 +1,473 @@ +// SPDX-License-Identifier: MIT + +//! Two linked devices talking through real Signal sessions, each keeping its +//! state in a sealed, fenced record store. + +use futures::executor::block_on; +use std::sync::Arc; +use wacore::libsignal::protocol::{DeviceId, ProtocolAddress}; +use wacore::store::traits::SignalStore; +use wacore_recordstore::{ + AesGcmSealer, LeaseStore, MemoryRecordStore, Namespace, RecordSignalStore, RecordStore, +}; +mod common; +use common::pair_success_container; +use wacore_signal_ops::{EncKind, OpsError, RemoteBundle, SignalOps}; + +mod runtime; +use runtime::InlineRuntime; + +type Store = RecordSignalStore; + +const TTL: u64 = 60_000; +const DATA_KEY: [u8; 32] = [42; 32]; + +struct Node { + records: Arc, + scope: &'static str, + ops: SignalOps, +} + +fn store_for(records: &Arc, scope: &str, holder: &str, now: u64) -> Arc { + let lease = block_on(records.acquire(scope, holder, now, TTL)) + .unwrap() + .expect("lease available"); + Arc::new(RecordSignalStore::new( + records.clone(), + Arc::new(AesGcmSealer::new(&DATA_KEY)), + lease.fence, + )) +} + +fn new_device(scope: &'static str) -> Node { + let records = Arc::new(MemoryRecordStore::new()); + let store = store_for(&records, scope, "node-a", 0); + let ops = block_on(SignalOps::create(store)).unwrap(); + Node { + records, + scope, + ops, + } +} + +fn address(user: &str) -> ProtocolAddress { + ProtocolAddress::new(&format!("{user}@s.whatsapp.net"), DeviceId::from(1)) +} + +/// What the WhatsApp client would fetch from the server for `node`. +/// `from` pins the account that signed `node`'s device, as it would after +/// meeting the peer's primary. +fn bundle_for( + from: &Node, + node: &Node, + to: &ProtocolAddress, + with_prekey: bool, +) -> (RemoteBundle, Option) { + let device_identity = block_on(common::vouch(&from.ops, &node.ops, to.name())); + let identity = block_on(node.ops.public_identity()); + let prekey = with_prekey.then(|| { + let keys = block_on(node.ops.generate_prekeys(&InlineRuntime, 1)).unwrap(); + (keys[0].id, keys[0].public_key) + }); + ( + RemoteBundle { + registration_id: identity.registration_id, + device_id: 1, + identity_key: identity.identity_key, + signed_prekey_id: identity.signed_prekey.id, + signed_prekey: identity.signed_prekey.public_key, + signed_prekey_signature: identity.signed_prekey.signature, + prekey, + device_identity: Some(device_identity), + }, + prekey.map(|(id, _)| id), + ) +} + +fn raw_records_contain(node: &Node, needle: &[u8]) -> bool { + Namespace::ALL.iter().any(|ns| { + let keys = block_on(node.records.scan_keys(node.scope, *ns, "", None)).unwrap(); + keys.iter().any(|key| { + let raw = block_on(node.records.get(node.scope, *ns, key)) + .unwrap() + .unwrap(); + raw.windows(needle.len()).any(|w| w == needle) + }) + }) +} + +#[test] +fn a_session_runs_both_ways_and_stays_sealed() { + let alice = new_device("alice-device"); + let bob = new_device("bob-device"); + let (alice_addr, bob_addr) = (address("111"), address("222")); + let alice_identity = block_on(common::vouch(&bob.ops, &alice.ops, alice_addr.name())); + + let (bundle, prekey_id) = bundle_for(&alice, &bob, &bob_addr, true); + block_on(alice.ops.establish_session(&bob_addr, &bundle)).unwrap(); + + let first = block_on(alice.ops.encrypt(&bob_addr, b"hello bob")).unwrap(); + assert_eq!(first.kind, EncKind::PreKey); + let opened = block_on(bob.ops.decrypt( + &alice_addr, + first.kind, + &first.ciphertext, + Some(&alice_identity), + )) + .unwrap(); + assert_eq!(opened.plaintext, b"hello bob"); + + // The consumed one-time prekey is gone, after the session became durable. + let bob_store = store_for(&bob.records, bob.scope, "node-a", 1); + assert_eq!( + block_on(bob_store.load_prekey(prekey_id.unwrap())).unwrap(), + None + ); + + let reply = block_on(bob.ops.encrypt(&alice_addr, b"hi alice")).unwrap(); + assert_eq!(reply.kind, EncKind::Message); + let opened = block_on( + alice + .ops + .decrypt(&bob_addr, reply.kind, &reply.ciphertext, None), + ) + .unwrap(); + assert_eq!(opened.plaintext, b"hi alice"); + + for round in 0..5u8 { + let to_bob = block_on(alice.ops.encrypt(&bob_addr, &[b'a', round])).unwrap(); + let got = block_on( + bob.ops + .decrypt(&alice_addr, to_bob.kind, &to_bob.ciphertext, None), + ) + .unwrap(); + assert_eq!(got.plaintext, [b'a', round]); + let to_alice = block_on(bob.ops.encrypt(&alice_addr, &[b'b', round])).unwrap(); + let got = block_on( + alice + .ops + .decrypt(&bob_addr, to_alice.kind, &to_alice.ciphertext, None), + ) + .unwrap(); + assert_eq!(got.plaintext, [b'b', round]); + } + + assert!(!raw_records_contain(&bob, b"hello bob")); + let bob_identity = block_on(bob.ops.public_identity()); + assert!( + !raw_records_contain(&bob, &bob_identity.adv_secret), + "device secrets reached the store unsealed" + ); +} + +#[test] +fn a_failed_over_node_keeps_the_session_and_the_old_node_is_fenced_out() { + let alice = new_device("alice-failover"); + let bob = new_device("bob-failover"); + let (alice_addr, bob_addr) = (address("111"), address("222")); + let alice_identity = block_on(common::vouch(&bob.ops, &alice.ops, alice_addr.name())); + let (bundle, _) = bundle_for(&alice, &bob, &bob_addr, true); + block_on(alice.ops.establish_session(&bob_addr, &bundle)).unwrap(); + let first = block_on(alice.ops.encrypt(&bob_addr, b"before failover")).unwrap(); + block_on(bob.ops.decrypt( + &alice_addr, + first.kind, + &first.ciphertext, + Some(&alice_identity), + )) + .unwrap(); + + // node-a's lease lapses; node-b takes the scope from the same records. + let takeover = store_for(&bob.records, bob.scope, "node-b", TTL + 1); + let bob_b = block_on(SignalOps::open(takeover)).unwrap(); + + let next = block_on(alice.ops.encrypt(&bob_addr, b"after failover")).unwrap(); + let stale = block_on(bob.ops.decrypt( + &alice_addr, + next.kind, + &next.ciphertext, + Some(&alice_identity), + )); + let err = stale.expect_err("the superseded node must not advance the ratchet"); + assert!(err.is_fence_lost(), "unexpected error: {err:?}"); + + let opened = block_on(bob_b.decrypt( + &alice_addr, + next.kind, + &next.ciphertext, + Some(&alice_identity), + )) + .unwrap(); + assert_eq!(opened.plaintext, b"after failover"); +} + +#[test] +fn group_messages_decrypt_with_a_distributed_sender_key() { + let alice = new_device("alice-group"); + let bob = new_device("bob-group"); + let (alice_addr, bob_addr) = (address("111"), address("222")); + let alice_identity = block_on(common::vouch(&bob.ops, &alice.ops, alice_addr.name())); + let group = "120363000000001@g.us"; + let (bundle, _) = bundle_for(&alice, &bob, &bob_addr, true); + block_on(alice.ops.establish_session(&bob_addr, &bundle)).unwrap(); + + let skdm = block_on(alice.ops.sender_key_distribution(group, &alice_addr)).unwrap(); + let carried = block_on(alice.ops.encrypt(&bob_addr, &skdm)).unwrap(); + let received = block_on(bob.ops.decrypt( + &alice_addr, + carried.kind, + &carried.ciphertext, + Some(&alice_identity), + )) + .unwrap(); + block_on( + bob.ops + .process_sender_key_distribution(group, &alice_addr, &received.plaintext), + ) + .unwrap(); + + let skmsg = block_on(alice.ops.group_encrypt(group, &alice_addr, b"hello group")).unwrap(); + let plaintext = block_on(bob.ops.group_decrypt(group, &alice_addr, &skmsg)).unwrap(); + assert_eq!(plaintext, b"hello group"); +} + +#[test] +fn a_prekey_message_for_a_rotated_signed_prekey_still_decrypts() { + let alice = new_device("alice-rotate"); + let bob = new_device("bob-rotate"); + let (alice_addr, bob_addr) = (address("111"), address("222")); + let alice_identity = block_on(common::vouch(&bob.ops, &alice.ops, alice_addr.name())); + let (bundle, _) = bundle_for(&alice, &bob, &bob_addr, false); + let before = block_on(bob.ops.public_identity()).signed_prekey; + block_on(alice.ops.establish_session(&bob_addr, &bundle)).unwrap(); + + let after = block_on(bob.ops.rotate_signed_prekey()).unwrap(); + assert_ne!(after.id, before.id); + + let first = block_on(alice.ops.encrypt(&bob_addr, b"old signed prekey")).unwrap(); + let opened = block_on(bob.ops.decrypt( + &alice_addr, + first.kind, + &first.ciphertext, + Some(&alice_identity), + )) + .unwrap(); + assert_eq!(opened.plaintext, b"old signed prekey"); +} + +#[test] +fn reopening_a_scope_restores_the_same_device() { + let bob = new_device("bob-reopen"); + let identity = block_on(bob.ops.public_identity()); + let reopened = block_on(SignalOps::open(store_for( + &bob.records, + bob.scope, + "node-a", + 1, + ))) + .unwrap(); + assert_eq!(block_on(reopened.public_identity()), identity); + // With no live instance under the fence, create reaches the store, whose + // exclusive insert must refuse the existing keys. + let Node { + records, + scope, + ops, + } = bob; + drop((ops, reopened)); + let again = block_on(SignalOps::create(store_for(&records, scope, "node-a", 2))); + assert!( + matches!(again, Err(OpsError::InvalidInput(_))), + "create overwrote existing keys" + ); +} + +#[test] +fn pairing_signs_a_valid_container_with_the_device_prefix() { + let bob = new_device("bob-pairing-ok"); + let identity = block_on(bob.ops.public_identity()); + let (container, account) = + pair_success_container(identity.identity_key, &identity.adv_secret, 7); + + let signed = block_on(bob.ops.sign_pairing(&container)).unwrap(); + assert_eq!(signed.key_index, 7); + + let decoded = + waproto::codec::adv_signed_device_identity_decode(&signed.signed_identity).unwrap(); + let details = decoded.details.as_deref().unwrap(); + let message = [ + &[6u8, 1][..], + details, + &identity.identity_key, + account.public_key.public_key_bytes(), + ] + .concat(); + let device_key = + wacore::libsignal::protocol::PublicKey::from_djb_public_key_bytes(&identity.identity_key) + .unwrap(); + assert!(device_key.verify_signature(&message, decoded.device_signature.as_deref().unwrap())); +} + +#[test] +fn pairing_refuses_a_container_for_another_secret() { + let bob = new_device("bob-pairing-secret"); + let identity = block_on(bob.ops.public_identity()); + let (container, _) = pair_success_container(identity.identity_key, &[1; 32], 7); + assert!(matches!( + block_on(bob.ops.sign_pairing(&container)), + Err(OpsError::Pairing { code: 401, .. }) + )); +} + +#[test] +fn pairing_refuses_a_forged_container() { + let bob = new_device("bob-pairing"); + let refused = block_on(bob.ops.sign_pairing(b"not a pairing container")); + assert!(matches!(refused, Err(OpsError::Pairing { .. }))); +} + +#[test] +fn forget_user_drops_every_device_of_that_user_only() { + let records = Arc::new(MemoryRecordStore::new()); + let store = store_for(&records, "forget", "node-a", 0); + let ops = block_on(SignalOps::create(store.clone())).unwrap(); + let forgotten = [ + "123@c.us.0", + "123:7@c.us.0", + "123:12@c.us.0", + "123:99@hosted.0", + // The plain libsignal form, as `establish_session` stores it. + "123@s.whatsapp.net.4", + ]; + let kept = [ + "1234@c.us.0", + "1234:7@c.us.0", + "1234:99@hosted.0", + "123@lid.0", + "123:7@lid.0", + "123:99@hosted.lid.0", + "1234@s.whatsapp.net.4", + "123@lid.4", + ]; + // Stored addresses use WA Web's `c.us` server for phone-number users. + for address in forgotten.iter().chain(&kept) { + block_on(store.put_session(address, b"session")).unwrap(); + block_on(store.put_identity(address, [9; 32])).unwrap(); + } + + let user: wacore_binary::Jid = "123:7@s.whatsapp.net".parse().unwrap(); + block_on(ops.forget_user(&user)).unwrap(); + + for address in forgotten { + assert!( + block_on(store.get_session(address)).unwrap().is_none(), + "{address}" + ); + assert!( + block_on(store.load_identity(address)).unwrap().is_none(), + "{address}" + ); + } + for address in kept { + assert!( + block_on(store.get_session(address)).unwrap().is_some(), + "{address}" + ); + assert!( + block_on(store.load_identity(address)).unwrap().is_some(), + "{address}" + ); + } + // Nothing left to forget is not an error. + block_on(ops.forget_user(&user)).unwrap(); +} + +#[test] +fn migrate_pn_to_lid_moves_regular_and_hosted_devices() { + let records = Arc::new(MemoryRecordStore::new()); + let store = store_for(&records, "migrate", "node-a", 0); + let ops = block_on(SignalOps::create(store.clone())).unwrap(); + // Device 0 and the hosted device exist only under PN. + block_on(store.put_session("123@c.us.0", b"pn-0")).unwrap(); + block_on(store.put_identity("123@c.us.0", [1; 32])).unwrap(); + block_on(store.put_session("123:99@hosted.0", b"pn-hosted")).unwrap(); + block_on(store.put_identity("123:99@hosted.0", [2; 32])).unwrap(); + // Device 7 exists on both sides: PN session wins, LID identity wins. + block_on(store.put_session("123:7@c.us.0", b"pn-7")).unwrap(); + block_on(store.put_identity("123:7@c.us.0", [3; 32])).unwrap(); + block_on(store.put_session("456:7@lid.0", b"lid-7")).unwrap(); + block_on(store.put_identity("456:7@lid.0", [4; 32])).unwrap(); + // The plain libsignal form carries the device in its suffix. + block_on(store.put_session("123@s.whatsapp.net.5", b"pn-5")).unwrap(); + // Device 7 is also stored under the plain form: the canonical row wins + // and the plain one is dropped, never copied over it. + block_on(store.put_session("123@s.whatsapp.net.7", b"pn-7-plain")).unwrap(); + block_on(store.put_identity("123@s.whatsapp.net.7", [8; 32])).unwrap(); + // Another user sharing the prefix is untouched. + block_on(store.put_session("1234@c.us.0", b"other")).unwrap(); + + let pn: wacore_binary::Jid = "123@s.whatsapp.net".parse().unwrap(); + let lid: wacore_binary::Jid = "456@lid".parse().unwrap(); + assert_eq!(block_on(ops.migrate_pn_to_lid(&pn, &lid)).unwrap(), 4); + + let session = |a: &str| block_on(store.get_session(a)).unwrap().map(|b| b.to_vec()); + let identity = |a: &str| block_on(store.load_identity(a)).unwrap(); + assert_eq!(session("456@lid.0").as_deref(), Some(&b"pn-0"[..])); + assert_eq!(identity("456@lid.0"), Some([1; 32])); + assert_eq!( + session("456:99@hosted.lid.0").as_deref(), + Some(&b"pn-hosted"[..]) + ); + assert_eq!(identity("456:99@hosted.lid.0"), Some([2; 32])); + assert_eq!(session("456:7@lid.0").as_deref(), Some(&b"pn-7"[..])); + assert_eq!(identity("456:7@lid.0"), Some([4; 32])); + assert_eq!(session("456:5@lid.0").as_deref(), Some(&b"pn-5"[..])); + assert_eq!(identity("456:7@lid.0"), Some([4; 32])); + assert_eq!(session("456@lid.0").as_deref(), Some(&b"pn-0"[..])); + for gone in [ + "123@c.us.0", + "123:7@c.us.0", + "123:99@hosted.0", + "123@s.whatsapp.net.5", + "123@s.whatsapp.net.7", + ] { + assert!( + session(gone).is_none() && identity(gone).is_none(), + "{gone}" + ); + } + assert!(session("1234@c.us.0").is_some()); + + // Already migrated: nothing left to move. + assert_eq!(block_on(ops.migrate_pn_to_lid(&pn, &lid)).unwrap(), 0); + // Arguments the wrong way round are refused. + assert!(matches!( + block_on(ops.migrate_pn_to_lid(&lid, &pn)), + Err(OpsError::InvalidInput(_)) + )); +} + +#[test] +fn a_second_open_under_the_same_fence_shares_one_authority() { + let records = Arc::new(MemoryRecordStore::new()); + let scope = "same-holder"; + let first = block_on(SignalOps::create(store_for(&records, scope, "node-a", 0))).unwrap(); + // Renewal by the same holder keeps the fence, so both instances write + // under one generation and the store cannot tell their writes apart. + let second = block_on(SignalOps::open(store_for(&records, scope, "node-a", 1))).unwrap(); + + let (a, b) = block_on(futures::future::join( + first.rotate_signed_prekey(), + second.rotate_signed_prekey(), + )); + let (a, b) = (a.unwrap(), b.unwrap()); + assert_ne!(a.id, b.id, "rotations under one fence must not reuse an id"); + + let latest = block_on(first.public_identity()).signed_prekey; + assert_eq!(block_on(second.public_identity()).signed_prekey, latest); + assert!(latest.id == a.id || latest.id == b.id); + + drop((first, second)); + let reopened = block_on(SignalOps::open(store_for(&records, scope, "node-a", 2))).unwrap(); + assert_eq!(block_on(reopened.public_identity()).signed_prekey, latest); +} diff --git a/crates/signal-ops/tests/prekey_upload.rs b/crates/signal-ops/tests/prekey_upload.rs new file mode 100644 index 0000000..71cab6b --- /dev/null +++ b/crates/signal-ops/tests/prekey_upload.rs @@ -0,0 +1,1221 @@ +// SPDX-License-Identifier: MIT + +//! Public upload retries recover from the sealed store, not process memory. +use async_trait::async_trait; +use futures::channel::oneshot; +use futures::executor::block_on; +use std::sync::{ + Arc, Mutex, + atomic::{AtomicU32, AtomicUsize, Ordering}, +}; +use wacore::store::{ + error::{Result as StoreResult, StoreError}, + traits::SignalStore, +}; +use wacore_recordstore::{ + AesGcmSealer, Fence, LeaseStore, MemoryRecordStore, Namespace, RecordSignalStore, RecordStore, + SealError, Sealer, WriteOp, +}; +use wacore_signal_ops::{DeviceKeys, SignalOps}; + +mod common; +mod runtime; +use runtime::InlineRuntime; + +const SCOPE: &str = "synthetic-upload-device"; +const PENDING: &str = "prekey-upload-v1"; +const MAX: u32 = 0x00ff_ffff; +type ReadGate = (oneshot::Sender<()>, oneshot::Receiver<()>); + +#[derive(Default)] +struct Records { + inner: MemoryRecordStore, + fail: AtomicUsize, + consume: AtomicU32, + batches: Mutex>>, + reads: AtomicUsize, + pending_read_gate: Mutex>, +} +#[async_trait] +impl RecordStore for Records { + async fn get(&self, scope: &str, ns: Namespace, key: &str) -> StoreResult>> { + self.reads.fetch_add(1, Ordering::SeqCst); + let value = self.inner.get(scope, ns, key).await?; + let gate = if ns == Namespace::Device && key == PENDING { + self.pending_read_gate.lock().unwrap().take() + } else { + None + }; + if let Some((entered, resume)) = gate { + entered.send(()).unwrap(); + resume.await.unwrap(); + } + Ok(value) + } + async fn scan_keys( + &self, + scope: &str, + ns: Namespace, + prefix: &str, + limit: Option, + ) -> StoreResult> { + self.inner.scan_keys(scope, ns, prefix, limit).await + } + async fn write(&self, fence: &Fence, ops: &[WriteOp]) -> StoreResult<()> { + self.batches.lock().unwrap().push(ops.to_vec()); + let failure = self.fail.swap(0, Ordering::SeqCst); + let fails = ops.iter().any(|op| match (failure, op) { + ( + 1, + WriteOp::Put { + ns: Namespace::Device, + key, + .. + }, + ) => key == "local", + ( + 2, + WriteOp::Insert { + ns: Namespace::PreKey, + .. + }, + ) => true, + ( + 3, + WriteOp::Insert { + ns: Namespace::Device, + key, + .. + }, + ) => key == PENDING, + ( + 4, + WriteOp::Delete { + ns: Namespace::Device, + key, + }, + ) => key == PENDING, + ( + 5, + WriteOp::Put { + ns: Namespace::Device, + key, + .. + }, + ) => key == "prekey-upload-ack-v1", + _ => false, + }); + if fails { + return Err(StoreError::Validation( + "synthetic atomic commit failure".into(), + )); + } + // Mode 6: a prekey batch commits, but its response is lost. + let lose_response = failure == 6 + && ops.iter().any(|op| { + matches!( + op, + WriteOp::Insert { + ns: Namespace::PreKey, + .. + } + ) + }); + // Mode 7: the acknowledgment commits, but its response is lost. + let lose_ack = failure == 7 + && ops.iter().any(|op| { + matches!( + op, + WriteOp::Put { + ns: Namespace::Device, + key, + .. + } if key == "prekey-upload-ack-v1" + ) + }); + let consumed = self.consume.swap(0, Ordering::SeqCst); + if consumed != 0 + && ops.iter().any(|op| { + matches!( + op, + WriteOp::Update { + ns: Namespace::PreKey, + .. + } + ) + }) + { + self.inner + .write( + fence, + &[WriteOp::Delete { + ns: Namespace::PreKey, + key: format!("{consumed:010}"), + }], + ) + .await?; + } + self.inner.write(fence, ops).await?; + if lose_response || lose_ack { + return Err(StoreError::Validation("synthetic lost response".into())); + } + Ok(()) + } +} + +struct Sealing { + inner: AesGcmSealer, + fail_after: AtomicUsize, +} +#[async_trait] +impl Sealer for Sealing { + async fn seal(&self, aad: &[u8], plain: &[u8]) -> Result, SealError> { + let remaining = self.fail_after.load(Ordering::SeqCst); + if remaining != 0 && self.fail_after.fetch_sub(1, Ordering::SeqCst) == 1 { + return Err(SealError::Backend("synthetic sealer failure".into())); + } + self.inner.seal(aad, plain).await + } + async fn open(&self, aad: &[u8], sealed: &[u8]) -> Result, SealError> { + self.inner.open(aad, sealed).await + } +} +type Store = RecordSignalStore; +struct Fixture { + records: Arc, + sealer: Arc, + store: Arc, + ops: SignalOps, +} +impl Fixture { + async fn new() -> Self { + let records = Arc::new(Records::default()); + let sealer = Arc::new(Sealing { + inner: AesGcmSealer::new(&[42; 32]), + fail_after: AtomicUsize::new(0), + }); + let lease = records + .inner + .acquire(SCOPE, "holder-a", 0, 60_000) + .await + .unwrap() + .unwrap(); + let store = Arc::new(RecordSignalStore::new( + records.clone(), + sealer.clone(), + lease.fence, + )); + let ops = SignalOps::create(store.clone()).await.unwrap(); + Self { + records, + sealer, + store, + ops, + } + } + async fn counter(&self) -> u32 { + DeviceKeys::decode(&self.store.load_device().await.unwrap().unwrap()) + .unwrap() + .next_prekey_id + } + async fn set_counter(&self, counter: u32) { + let mut keys = + DeviceKeys::decode(&self.store.load_device().await.unwrap().unwrap()).unwrap(); + keys.next_prekey_id = counter; + self.store + .save_device(&keys.encode().unwrap()) + .await + .unwrap(); + } + async fn uploaded(&self, id: u32) -> Option { + let key = format!("{id:010}"); + let sealed = self + .records + .get(SCOPE, Namespace::PreKey, &key) + .await + .unwrap()?; + let mut aad = Vec::new(); + for part in [ + b"wacore-recordstore/v1".as_slice(), + SCOPE.as_bytes(), + b"prekey", + key.as_bytes(), + ] { + aad.extend_from_slice(&(part.len() as u32).to_be_bytes()); + aad.extend_from_slice(part); + } + Some(self.sealer.open(&aad, &sealed).await.unwrap()[0] == 1) + } +} + +#[test] +fn legacy_mark_is_fresh_only_and_rejects_all_original_pending_members_atomically() { + block_on(async { + let f = Fixture::new().await; + let batch = f + .ops + .prepare_prekey_upload(&InlineRuntime, 2) + .await + .unwrap(); + let fresh = f.ops.generate_prekeys(&InlineRuntime, 1).await.unwrap()[0].id; + for ids in [vec![1], vec![fresh, 2]] { + let writes = f.records.batches.lock().unwrap().len(); + assert!(matches!( + f.ops.mark_prekeys_uploaded(&ids).await, + Err(wacore_signal_ops::OpsError::InvalidInput(_)) + )); + assert_eq!(f.records.batches.lock().unwrap().len(), writes); + assert_eq!(f.uploaded(fresh).await, Some(false)); + assert_eq!(f.uploaded(2).await, Some(false)); + } + f.store.remove_prekey(1).await.unwrap(); + let recovered = f + .ops + .prepare_prekey_upload(&InlineRuntime, 1) + .await + .unwrap(); + assert_eq!(recovered.pre_keys, batch.pre_keys[1..]); + assert!(f.ops.mark_prekeys_uploaded(&[fresh, 1]).await.is_err()); + assert_eq!(f.uploaded(1).await, None); + assert_eq!(f.uploaded(fresh).await, Some(false)); + f.ops.mark_prekeys_uploaded(&[fresh]).await.unwrap(); + assert_eq!(f.uploaded(fresh).await, Some(true)); + assert_eq!( + f.ops + .prepare_prekey_upload(&InlineRuntime, 1) + .await + .unwrap(), + recovered + ); + f.ops + .acknowledge_prekey_upload(&batch.batch_id, &[1, 2]) + .await + .unwrap(); + assert_eq!(f.uploaded(1).await, None); + assert_eq!(f.uploaded(2).await, Some(true)); + }); +} + +#[test] +fn legacy_mark_refuses_corrupt_pending_metadata_and_stale_fences() { + block_on(async { + let f = Fixture::new().await; + let fresh = f.ops.generate_prekeys(&InlineRuntime, 1).await.unwrap()[0].id; + f.store + .put_aux(Namespace::Device, PENDING, b"invalid-batch") + .await + .unwrap(); + assert!(f.ops.mark_prekeys_uploaded(&[fresh]).await.is_err()); + assert_eq!(f.uploaded(fresh).await, Some(false)); + f.store + .delete_aux(Namespace::Device, &[PENDING]) + .await + .unwrap(); + f.records + .inner + .acquire(SCOPE, "holder-b", 60_001, 60_000) + .await + .unwrap() + .unwrap(); + assert!( + f.ops + .mark_prekeys_uploaded(&[fresh]) + .await + .unwrap_err() + .is_fence_lost() + ); + assert_eq!(f.uploaded(fresh).await, Some(false)); + }); +} + +#[test] +fn legacy_mark_and_prepare_share_the_device_mutex() { + block_on(async { + let f = Fixture::new().await; + let (entered, observed) = oneshot::channel(); + let (resume, wait) = oneshot::channel(); + *f.records.pending_read_gate.lock().unwrap() = Some((entered, wait)); + // The mark reads no pending batch, then pauses before its flag write. + let mark = f.ops.mark_prekeys_uploaded(&[1]); + futures::pin_mut!(mark); + assert!(futures::poll!(&mut mark).is_pending()); + observed.await.unwrap(); + let prepare = f.ops.prepare_prekey_upload(&InlineRuntime, 1); + futures::pin_mut!(prepare); + assert!(futures::poll!(&mut prepare).is_pending()); + assert!(f.store.load_prekey(1).await.unwrap().is_none()); + resume.send(()).unwrap(); + let (marked, batch) = futures::join!(mark, prepare); + marked.unwrap(); + assert_eq!(batch.unwrap().pre_keys[0].id, 1); + // Prepare could not allocate between the guard read and legacy mark. + assert_eq!(f.uploaded(1).await, Some(false)); + }); +} + +#[test] +fn reopen_reuses_the_batch_and_repeated_requests_do_not_allocate_more() { + block_on(async { + let f = Fixture::new().await; + let first = f + .ops + .prepare_prekey_upload(&InlineRuntime, 3) + .await + .unwrap(); + assert_eq!(first.pre_keys.len(), 3); + for _ in 0..20 { + assert_eq!( + f.ops + .prepare_prekey_upload(&InlineRuntime, 812) + .await + .unwrap(), + first + ); + } + let reopened = SignalOps::open(f.store.clone()).await.unwrap(); + assert_eq!( + reopened + .prepare_prekey_upload(&InlineRuntime, 1) + .await + .unwrap(), + first + ); + assert_eq!(f.counter().await, 4); + assert_eq!( + f.records + .scan_keys(SCOPE, Namespace::PreKey, "", None) + .await + .unwrap() + .len(), + 3 + ); + let ids: Vec<_> = first.pre_keys.iter().map(|key| key.id).collect(); + reopened + .acknowledge_prekey_upload(&first.batch_id, &ids) + .await + .unwrap(); + for id in ids { + assert_eq!(f.uploaded(id).await, Some(true)); + } + reopened + .acknowledge_prekey_upload(&first.batch_id, &[1, 2, 3]) + .await + .unwrap(); + let next = reopened + .prepare_prekey_upload(&InlineRuntime, 2) + .await + .unwrap(); + assert_ne!(next.batch_id, first.batch_id); + assert_eq!( + next.pre_keys.iter().map(|key| key.id).collect::>(), + [4, 5] + ); + }); +} + +#[test] +fn counter_private_keys_and_pending_batch_fail_together_without_exposure() { + block_on(async { + for failure in 1..=3 { + let f = Fixture::new().await; + let before = f.store.load_device().await.unwrap(); + f.records.fail.store(failure, Ordering::SeqCst); + assert!( + f.ops + .prepare_prekey_upload(&InlineRuntime, 4) + .await + .is_err() + ); + assert_eq!(f.store.load_device().await.unwrap(), before); + assert!( + f.store + .load_aux(Namespace::Device, PENDING) + .await + .unwrap() + .is_none() + ); + assert!( + f.records + .scan_keys(SCOPE, Namespace::PreKey, "", None) + .await + .unwrap() + .is_empty() + ); + let failed = f.records.batches.lock().unwrap().last().unwrap().clone(); + assert_eq!( + failed + .iter() + .filter(|op| matches!( + op, + WriteOp::Insert { + ns: Namespace::PreKey, + .. + } + )) + .count(), + 4 + ); + assert!(failed.iter().any( + |op| matches!(op, WriteOp::Put { ns: Namespace::Device, key, .. } if key == "local") + )); + assert!(failed.iter().any(|op| matches!(op, WriteOp::Insert { ns: Namespace::Device, key, .. } if key == PENDING))); + let reopened = SignalOps::open(f.store.clone()).await.unwrap(); + assert_eq!( + reopened + .prepare_prekey_upload(&InlineRuntime, 1) + .await + .unwrap() + .pre_keys[0] + .id, + 1 + ); + } + let f = Fixture::new().await; + f.sealer.fail_after.store(2, Ordering::SeqCst); + assert!( + f.ops + .prepare_prekey_upload(&InlineRuntime, 2) + .await + .is_err() + ); + assert_eq!(f.counter().await, 1); + assert!(f.store.load_prekey(1).await.unwrap().is_none()); + assert!( + f.store + .load_aux(Namespace::Device, PENDING) + .await + .unwrap() + .is_none() + ); + assert_eq!( + f.ops + .prepare_prekey_upload(&InlineRuntime, 2) + .await + .unwrap() + .pre_keys[0] + .id, + 1 + ); + }); +} + +#[tokio::test] +async fn concurrent_prepare_is_bounded_and_retry_generation_stays_fresh() { + let f = Fixture::new().await; + let batches = + futures::future::join_all((0..24).map(|_| f.ops.prepare_prekey_upload(&InlineRuntime, 8))) + .await; + let batch = batches[0].as_ref().unwrap(); + for result in &batches { + assert_eq!(result.as_ref().unwrap(), batch); + } + let retries = + futures::future::join_all((0..12).map(|_| f.ops.generate_prekeys(&InlineRuntime, 2))).await; + let ids: std::collections::BTreeSet<_> = retries + .into_iter() + .flat_map(|keys| keys.unwrap().into_iter().map(|key| key.id)) + .collect(); + assert_eq!(ids.len(), 24); + assert!(batch.pre_keys.iter().all(|key| !ids.contains(&key.id))); + assert_eq!(f.counter().await, 33); +} + +#[test] +fn a_prepare_in_progress_holds_back_other_prepares_and_fresh_generation() { + block_on(async { + let f = Fixture::new().await; + let (entered, observed) = oneshot::channel(); + let (resume, wait) = oneshot::channel(); + *f.records.pending_read_gate.lock().unwrap() = Some((entered, wait)); + // The first prepare pauses at its pending-batch read. + let first = f.ops.prepare_prekey_upload(&InlineRuntime, 4); + futures::pin_mut!(first); + assert!(futures::poll!(&mut first).is_pending()); + observed.await.unwrap(); + let second = f.ops.prepare_prekey_upload(&InlineRuntime, 4); + futures::pin_mut!(second); + let fresh = f.ops.generate_prekeys(&InlineRuntime, 2); + futures::pin_mut!(fresh); + assert!(futures::poll!(&mut second).is_pending()); + assert!(futures::poll!(&mut fresh).is_pending()); + assert_eq!(f.counter().await, 1, "nothing allocated while held back"); + assert!(f.store.load_prekey(1).await.unwrap().is_none()); + resume.send(()).unwrap(); + let (first, second, fresh) = futures::join!(first, second, fresh); + let (first, second, fresh) = (first.unwrap(), second.unwrap(), fresh.unwrap()); + assert_eq!(first, second, "the second prepare reuses the batch"); + assert!( + fresh + .iter() + .all(|key| first.pre_keys.iter().all(|batch| batch.id != key.id)), + "fresh keys never reuse batch ids" + ); + assert_eq!(f.counter().await, 7); + }); +} + +#[test] +fn wrap_reserves_consumed_pending_ids_and_late_ack_never_marks_retry_keys() { + block_on(async { + let f = Fixture::new().await; + let old = f.ops.generate_prekeys(&InlineRuntime, 1).await.unwrap(); + let old_record = f.store.load_prekey(old[0].id).await.unwrap(); + f.set_counter(MAX - 1).await; + let batch = f + .ops + .prepare_prekey_upload(&InlineRuntime, 3) + .await + .unwrap(); + let ids: Vec<_> = batch.pre_keys.iter().map(|key| key.id).collect(); + assert_eq!(ids, [MAX - 1, MAX, 2]); + f.store.remove_prekey(MAX - 1).await.unwrap(); + f.set_counter(MAX - 1).await; + let fresh = f.ops.generate_prekeys(&InlineRuntime, 3).await.unwrap(); + assert_eq!( + fresh.iter().map(|key| key.id).collect::>(), + [3, 4, 5] + ); + assert_eq!(f.store.load_prekey(1).await.unwrap(), old_record); + let filtered = f + .ops + .prepare_prekey_upload(&InlineRuntime, 3) + .await + .unwrap(); + assert_eq!(filtered.batch_id, batch.batch_id); + assert_eq!( + filtered + .pre_keys + .iter() + .map(|key| key.id) + .collect::>(), + [MAX, 2] + ); + f.ops + .acknowledge_prekey_upload(&batch.batch_id, &ids) + .await + .unwrap(); + assert!(f.store.load_prekey(MAX - 1).await.unwrap().is_none()); + for key in fresh { + assert_eq!(f.uploaded(key.id).await, Some(false)); + } + assert_eq!(f.uploaded(1).await, Some(false)); + assert_eq!(f.uploaded(MAX).await, Some(true)); + assert_eq!(f.uploaded(2).await, Some(true)); + }); +} + +#[test] +fn consumed_keys_stay_gone_even_between_ack_read_and_atomic_commit() { + block_on(async { + let f = Fixture::new().await; + let batch = f + .ops + .prepare_prekey_upload(&InlineRuntime, 3) + .await + .unwrap(); + f.store.remove_prekey(1).await.unwrap(); + let filtered = f + .ops + .prepare_prekey_upload(&InlineRuntime, 3) + .await + .unwrap(); + assert_eq!( + filtered + .pre_keys + .iter() + .map(|key| key.id) + .collect::>(), + [2, 3] + ); + f.records.consume.store(2, Ordering::SeqCst); + f.ops + .acknowledge_prekey_upload(&batch.batch_id, &[2, 3]) + .await + .unwrap(); + assert_eq!(f.uploaded(1).await, None); + assert_eq!(f.uploaded(2).await, None); + assert_eq!(f.uploaded(3).await, Some(true)); + assert!( + f.store + .load_aux(Namespace::Device, PENDING) + .await + .unwrap() + .is_none() + ); + }); +} + +#[test] +fn ack_rejects_unknown_duplicate_missing_and_stale_batches_and_recovers_failed_commit() { + block_on(async { + let f = Fixture::new().await; + let batch = f + .ops + .prepare_prekey_upload(&InlineRuntime, 2) + .await + .unwrap(); + for (id, ids) in [ + ("unknown", vec![1, 2]), + (batch.batch_id.as_str(), vec![1]), + (batch.batch_id.as_str(), vec![1, 1, 2]), + (batch.batch_id.as_str(), vec![1, 2, 3]), + ] { + assert!(f.ops.acknowledge_prekey_upload(id, &ids).await.is_err()); + assert_eq!( + f.ops + .prepare_prekey_upload(&InlineRuntime, 2) + .await + .unwrap(), + batch + ); + assert_eq!(f.uploaded(1).await, Some(false)); + } + f.records.fail.store(4, Ordering::SeqCst); + assert!( + f.ops + .acknowledge_prekey_upload(&batch.batch_id, &[1, 2]) + .await + .is_err() + ); + assert_eq!(f.uploaded(1).await, Some(false)); + let reopened = SignalOps::open(f.store.clone()).await.unwrap(); + assert_eq!( + reopened + .prepare_prekey_upload(&InlineRuntime, 2) + .await + .unwrap(), + batch + ); + reopened + .acknowledge_prekey_upload(&batch.batch_id, &[1, 2]) + .await + .unwrap(); + let next = reopened + .prepare_prekey_upload(&InlineRuntime, 1) + .await + .unwrap(); + reopened + .acknowledge_prekey_upload(&batch.batch_id, &[1, 2]) + .await + .unwrap(); + assert!( + reopened + .acknowledge_prekey_upload(&batch.batch_id, &[1]) + .await + .is_err() + ); + assert_eq!( + reopened + .prepare_prekey_upload(&InlineRuntime, 1) + .await + .unwrap(), + next + ); + reopened + .acknowledge_prekey_upload(&next.batch_id, &[next.pre_keys[0].id]) + .await + .unwrap(); + assert!( + reopened + .acknowledge_prekey_upload(&batch.batch_id, &[1, 2]) + .await + .is_err() + ); + }); +} + +#[test] +fn consumed_batch_retires_and_stale_holder_cannot_prepare_or_ack() { + block_on(async { + let f = Fixture::new().await; + let batch = f + .ops + .prepare_prekey_upload(&InlineRuntime, 1) + .await + .unwrap(); + f.store.remove_prekey(batch.pre_keys[0].id).await.unwrap(); + let replacement = f + .ops + .prepare_prekey_upload(&InlineRuntime, 1) + .await + .unwrap(); + assert_ne!(replacement.batch_id, batch.batch_id); + assert!( + f.ops + .acknowledge_prekey_upload(&batch.batch_id, &[1]) + .await + .is_err() + ); + let lease = f + .records + .inner + .acquire(SCOPE, "holder-b", 60_001, 60_000) + .await + .unwrap() + .unwrap(); + assert!( + f.ops + .prepare_prekey_upload(&InlineRuntime, 1) + .await + .unwrap_err() + .is_fence_lost() + ); + assert!( + f.ops + .acknowledge_prekey_upload(&replacement.batch_id, &[replacement.pre_keys[0].id]) + .await + .unwrap_err() + .is_fence_lost() + ); + assert!( + f.ops + .generate_prekeys(&InlineRuntime, 1) + .await + .unwrap_err() + .is_fence_lost() + ); + let store = Arc::new(RecordSignalStore::new( + f.records.clone(), + f.sealer.clone(), + lease.fence, + )); + let new = SignalOps::open(store).await.unwrap(); + assert_eq!( + new.prepare_prekey_upload(&InlineRuntime, 1).await.unwrap(), + replacement + ); + }); +} + +#[test] +fn corrupted_pending_metadata_or_private_keys_fail_closed_without_replacement() { + block_on(async { + for kind in 0..5 { + let f = Fixture::new().await; + f.ops + .prepare_prekey_upload(&InlineRuntime, 2) + .await + .unwrap(); + let mut pending = f + .store + .load_aux(Namespace::Device, PENDING) + .await + .unwrap() + .unwrap(); + match kind { + 0 => pending[0] = 99, + 1 => { + pending.pop(); + } + 2 => pending[19..23].copy_from_slice(&0u32.to_be_bytes()), + 3 => { + let entry = pending[19..55].to_vec(); + pending[55..91].copy_from_slice(&entry); + } + _ => pending[23] ^= 1, + } + if kind >= 2 { + use sha2::Digest; + let data_len = pending.len() - 32; + let digest = sha2::Sha256::digest(&pending[..data_len]); + pending[data_len..].copy_from_slice(&digest); + } + f.store + .put_aux(Namespace::Device, PENDING, &pending) + .await + .unwrap(); + let before = f.store.load_device().await.unwrap(); + assert!( + f.ops + .prepare_prekey_upload(&InlineRuntime, 2) + .await + .is_err() + ); + assert!(f.ops.generate_prekeys(&InlineRuntime, 1).await.is_err()); + assert_eq!(f.store.load_device().await.unwrap(), before); + } + let f = Fixture::new().await; + f.ops + .prepare_prekey_upload(&InlineRuntime, 1) + .await + .unwrap(); + f.store + .store_prekey(1, b"corrupt-record", false) + .await + .unwrap(); + assert!( + f.ops + .prepare_prekey_upload(&InlineRuntime, 1) + .await + .is_err() + ); + // Count validation, on a healthy device so no other check can be + // what refuses. + let f = Fixture::new().await; + let count_error = |error: wacore_signal_ops::OpsError| { + assert!( + error.to_string().contains("prekey count must be 1 to 812"), + "{error}" + ) + }; + for count in [0, 813, u32::MAX] { + count_error( + f.ops + .prepare_prekey_upload(&InlineRuntime, count) + .await + .unwrap_err(), + ); + count_error( + f.ops + .generate_prekeys(&InlineRuntime, count) + .await + .unwrap_err(), + ); + } + assert_eq!(f.counter().await, 1, "a refused count allocates nothing"); + assert!(f.store.load_prekey(1).await.unwrap().is_none()); + }); +} + +#[test] +fn lost_ack_response_replays_after_restart_without_touching_new_pending_or_crossing_fence() { + block_on(async { + let f = Fixture::new().await; + let batch = f + .ops + .prepare_prekey_upload(&InlineRuntime, 2) + .await + .unwrap(); + f.records.fail.store(5, Ordering::SeqCst); + assert!( + f.ops + .acknowledge_prekey_upload(&batch.batch_id, &[1, 2]) + .await + .is_err() + ); + assert_eq!(f.uploaded(1).await, Some(false)); + assert_eq!( + f.ops + .prepare_prekey_upload(&InlineRuntime, 2) + .await + .unwrap(), + batch + ); + f.ops + .acknowledge_prekey_upload(&batch.batch_id, &[1, 2]) + .await + .unwrap(); + let reopened = SignalOps::open(f.store.clone()).await.unwrap(); + let next = reopened + .prepare_prekey_upload(&InlineRuntime, 1) + .await + .unwrap(); + reopened + .acknowledge_prekey_upload(&batch.batch_id, &[2, 1]) + .await + .unwrap(); + assert_eq!( + reopened + .prepare_prekey_upload(&InlineRuntime, 1) + .await + .unwrap(), + next + ); + assert!( + reopened + .acknowledge_prekey_upload(&batch.batch_id, &[1, 1, 2]) + .await + .is_err() + ); + f.records + .inner + .acquire(SCOPE, "holder-b", 60_001, 60_000) + .await + .unwrap() + .unwrap(); + assert!( + reopened + .acknowledge_prekey_upload(&batch.batch_id, &[1, 2]) + .await + .unwrap_err() + .is_fence_lost() + ); + }); +} + +#[test] +fn allocation_work_is_bounded_and_collision_refuses_the_entire_staged_batch() { + block_on(async { + let f = Fixture::new().await; + let occupied: Vec<_> = (1..=4096) + .map(|id| (id, bytes::Bytes::from_static(b"occupied-private-slot"))) + .collect(); + f.store.store_prekeys_batch(&occupied, false).await.unwrap(); + let before = f.store.load_device().await.unwrap(); + f.records.reads.store(0, Ordering::SeqCst); + let error = f + .ops + .prepare_prekey_upload(&InlineRuntime, 1) + .await + .unwrap_err(); + assert!(error.to_string().contains("4096-ID scan limit")); + assert!(f.records.reads.load(Ordering::SeqCst) <= 4098); + assert_eq!(f.store.load_device().await.unwrap(), before); + assert!( + f.store + .load_aux(Namespace::Device, PENDING) + .await + .unwrap() + .is_none() + ); + let staged = f.store.staged(); + staged + .insert_prekeys_batch(&[ + (5000, bytes::Bytes::from_static(b"new")), + (1, bytes::Bytes::from_static(b"replacement")), + ]) + .await + .unwrap(); + let mut keys = DeviceKeys::decode(before.as_deref().unwrap()).unwrap(); + keys.next_prekey_id = 5001; + staged.save_device(&keys.encode().unwrap()).await.unwrap(); + assert!(wacore_recordstore::is_record_exists( + &staged.commit().await.unwrap_err() + )); + assert_eq!(f.store.load_device().await.unwrap(), before); + assert!(f.store.load_prekey(5000).await.unwrap().is_none()); + assert_eq!( + f.store.load_prekey(1).await.unwrap().unwrap(), + occupied[0].1 + ); + }); +} + +#[test] +fn actual_signal_consumption_filters_the_recovered_public_batch() { + block_on(async { + use wacore::libsignal::protocol::{DeviceId, ProtocolAddress}; + use wacore_signal_ops::RemoteBundle; + let sender = Fixture::new().await; + let receiver = Fixture::new().await; + let batch = receiver + .ops + .prepare_prekey_upload(&InlineRuntime, 2) + .await + .unwrap(); + let device_identity = common::vouch(&sender.ops, &receiver.ops, "222@s.whatsapp.net").await; + let sender_identity = common::vouch(&receiver.ops, &sender.ops, "111@s.whatsapp.net").await; + let identity = receiver.ops.public_identity().await; + let sender_address = ProtocolAddress::new("111@s.whatsapp.net", DeviceId::from(1)); + let receiver_address = ProtocolAddress::new("222@s.whatsapp.net", DeviceId::from(1)); + sender + .ops + .establish_session( + &receiver_address, + &RemoteBundle { + registration_id: identity.registration_id, + device_id: 1, + identity_key: identity.identity_key, + signed_prekey_id: identity.signed_prekey.id, + signed_prekey: identity.signed_prekey.public_key, + signed_prekey_signature: identity.signed_prekey.signature, + prekey: Some((batch.pre_keys[0].id, batch.pre_keys[0].public_key)), + device_identity: Some(device_identity), + }, + ) + .await + .unwrap(); + let sent = sender + .ops + .encrypt(&receiver_address, b"synthetic-upload-roundtrip") + .await + .unwrap(); + let received = receiver + .ops + .decrypt( + &sender_address, + sent.kind, + &sent.ciphertext, + Some(&sender_identity), + ) + .await + .unwrap(); + assert_eq!(received.plaintext, b"synthetic-upload-roundtrip"); + let reopened = SignalOps::open(receiver.store.clone()).await.unwrap(); + let filtered = reopened + .prepare_prekey_upload(&InlineRuntime, 2) + .await + .unwrap(); + assert_eq!(filtered.batch_id, batch.batch_id); + assert_eq!(filtered.pre_keys, batch.pre_keys[1..]); + reopened + .acknowledge_prekey_upload(&batch.batch_id, &[1, 2]) + .await + .unwrap(); + assert_eq!(receiver.uploaded(1).await, None); + assert_eq!(receiver.uploaded(2).await, Some(true)); + }); +} + +#[test] +fn upload_ceiling_and_corrupt_ack_receipt_are_enforced() { + block_on(async { + let f = Fixture::new().await; + let batch = f + .ops + .prepare_prekey_upload(&InlineRuntime, 812) + .await + .unwrap(); + assert_eq!(batch.pre_keys.len(), 812); + f.ops + .acknowledge_prekey_upload( + &batch.batch_id, + &batch.pre_keys.iter().map(|key| key.id).collect::>(), + ) + .await + .unwrap(); + f.store + .put_aux( + Namespace::Device, + "prekey-upload-ack-v1", + b"corrupt-receipt", + ) + .await + .unwrap(); + assert!( + f.ops + .acknowledge_prekey_upload(&batch.batch_id, &[1]) + .await + .is_err() + ); + assert_eq!(f.counter().await, 813); + }); +} + +/// Counts blocking jobs and the thread each ran on. +struct CountingRuntime { + jobs: std::sync::atomic::AtomicUsize, +} + +impl wacore::runtime::Runtime for CountingRuntime { + fn spawn( + &self, + future: std::pin::Pin + Send + 'static>>, + ) -> wacore::runtime::AbortHandle { + InlineRuntime.spawn(future) + } + + fn sleep( + &self, + duration: std::time::Duration, + ) -> std::pin::Pin + Send>> { + InlineRuntime.sleep(duration) + } + + fn spawn_blocking( + &self, + f: Box, + ) -> std::pin::Pin + Send + 'static>> { + self.jobs.fetch_add(1, std::sync::atomic::Ordering::SeqCst); + InlineRuntime.spawn_blocking(f) + } + + fn yield_now(&self) -> Option + Send>>> { + None + } +} + +#[test] +fn prekey_generation_runs_as_blocking_work() { + block_on(async { + let f = Fixture::new().await; + let runtime = CountingRuntime { + jobs: std::sync::atomic::AtomicUsize::new(0), + }; + let batch = f.ops.prepare_prekey_upload(&runtime, 812).await.unwrap(); + assert_eq!(batch.pre_keys.len(), 812); + assert_eq!(runtime.jobs.load(std::sync::atomic::Ordering::SeqCst), 1); + f.ops.generate_prekeys(&runtime, 3).await.unwrap(); + assert_eq!(runtime.jobs.load(std::sync::atomic::Ordering::SeqCst), 2); + }); +} + +#[test] +fn a_lost_prekey_commit_response_cannot_rewind_the_counter() { + block_on(async { + let f = Fixture::new().await; + let before = f.counter().await; + f.records.fail.store(6, Ordering::SeqCst); + assert!( + f.ops + .prepare_prekey_upload(&InlineRuntime, 2) + .await + .is_err(), + "the caller sees the lost response" + ); + let committed = f.counter().await; + assert_eq!(committed, before + 2, "the batch did commit"); + + // A cached write after the uncertain commit must not write the old + // counter back. + f.ops + .set_own_jids("111:1@s.whatsapp.net", None) + .await + .unwrap(); + f.ops.rotate_signed_prekey().await.unwrap(); + assert_eq!(f.counter().await, committed); + }); +} + +#[test] +fn an_acknowledgment_that_committed_but_lost_its_response_replays_after_reopening() { + block_on(async { + let f = Fixture::new().await; + let batch = f + .ops + .prepare_prekey_upload(&InlineRuntime, 2) + .await + .unwrap(); + f.records.fail.store(7, Ordering::SeqCst); + assert!( + f.ops + .acknowledge_prekey_upload(&batch.batch_id, &[1, 2]) + .await + .is_err(), + "the caller sees the lost response" + ); + assert_eq!( + f.uploaded(1).await, + Some(true), + "the acknowledgment did commit" + ); + assert_eq!(f.uploaded(2).await, Some(true)); + + // The process restarts: only the sealed store remains, and no live + // instance is left for the reopen to join. + let Fixture { store, ops, .. } = f; + drop(ops); + let reopened = SignalOps::open(store).await.unwrap(); + reopened + .acknowledge_prekey_upload(&batch.batch_id, &[1, 2]) + .await + .expect("the exact acknowledgment replays"); + let next = reopened + .prepare_prekey_upload(&InlineRuntime, 1) + .await + .unwrap(); + assert_ne!(next.batch_id, batch.batch_id); + reopened + .acknowledge_prekey_upload(&batch.batch_id, &[1, 2]) + .await + .expect("a second replay is accepted too"); + assert_eq!( + reopened + .prepare_prekey_upload(&InlineRuntime, 1) + .await + .unwrap(), + next, + "the replay left the new pending batch unchanged" + ); + }); +} diff --git a/crates/signal-ops/tests/receive.rs b/crates/signal-ops/tests/receive.rs new file mode 100644 index 0000000..22e0587 --- /dev/null +++ b/crates/signal-ops/tests/receive.rs @@ -0,0 +1,708 @@ +// SPDX-License-Identifier: MIT + +//! Buffered receive: idempotent redelivery, atomic commits, sender keys and +//! the app-state key share gate, with real padded WhatsApp messages. + +use futures::executor::block_on; +use std::sync::Arc; +use wacore::libsignal::protocol::{DeviceId, KeyPair, ProtocolAddress}; +use wacore::messages::MessageUtils; +use wacore_recordstore::{ + AesGcmSealer, Fence, LeaseStore, MemoryRecordStore, Namespace, RecordSignalStore, RecordStore, + WriteOp, +}; +use wacore_signal_ops::{ + Encrypted, ReceiveKind, ReceiveRequest, Received, ReceivedMessage, RemoteBundle, SignalOps, +}; +use waproto::whatsapp as wa; + +mod common; +mod runtime; +use runtime::InlineRuntime; + +type Store = RecordSignalStore; + +const TTL: u64 = 60_000; +const GROUP: &str = "120363000000001@g.us"; + +struct Device { + records: Arc, + scope: &'static str, + ops: SignalOps, +} + +fn store_for(records: &Arc, scope: &str, holder: &str, now: u64) -> Arc { + let lease = block_on(records.acquire(scope, holder, now, TTL)) + .unwrap() + .expect("lease available"); + Arc::new(RecordSignalStore::new( + records.clone(), + Arc::new(AesGcmSealer::new(&[9; 32])), + lease.fence, + )) +} + +fn device(scope: &'static str) -> Device { + let records = Arc::new(MemoryRecordStore::new()); + let ops = block_on(SignalOps::create(store_for(&records, scope, "node-a", 0))).unwrap(); + Device { + records, + scope, + ops, + } +} + +fn address(user: &str) -> ProtocolAddress { + ProtocolAddress::new(&format!("{user}@s.whatsapp.net"), DeviceId::from(1)) +} + +thread_local! { + /// The `` each test sender's prekey messages carry, by + /// sender address. Leaked so a request can borrow it for any lifetime; + /// each test runs on its own thread. + static SENDER_IDENTITIES: std::cell::RefCell> = + std::cell::RefCell::default(); +} + +/// Have `receiver` pin `sender`'s account and remember the identity its +/// prekey messages must carry (see [`request`]). +fn accept_sender( + receiver: &SignalOps, + sender: &SignalOps, + sender_addr: &ProtocolAddress, +) { + let identity = block_on(common::vouch(receiver, sender, sender_addr.name())); + SENDER_IDENTITIES.with(|map| { + map.borrow_mut().insert( + sender_addr.name().to_owned(), + Box::leak(identity.into_boxed_slice()), + ); + }); +} + +fn connect(from: &Device, to: &Device, from_addr: &ProtocolAddress, to_addr: &ProtocolAddress) { + accept_sender(&to.ops, &from.ops, from_addr); + let device_identity = block_on(common::vouch(&from.ops, &to.ops, to_addr.name())); + let identity = block_on(to.ops.public_identity()); + let prekey = block_on(to.ops.generate_prekeys(&InlineRuntime, 1)) + .unwrap() + .remove(0); + block_on(from.ops.establish_session( + to_addr, + &RemoteBundle { + registration_id: identity.registration_id, + device_id: 1, + identity_key: identity.identity_key, + signed_prekey_id: identity.signed_prekey.id, + signed_prekey: identity.signed_prekey.public_key, + signed_prekey_signature: identity.signed_prekey.signature, + prekey: Some((prekey.id, prekey.public_key)), + device_identity: Some(device_identity), + }, + )) + .unwrap(); +} + +// Test-only proto encoding; the repo allows it in tests. +#[allow(clippy::disallowed_methods)] +fn padded(message: &wa::Message) -> Vec { + MessageUtils::pad_message_v2(waproto::codec::message_to_vec(message)) +} + +fn text(body: &str) -> wa::Message { + let mut message = wa::Message::default(); + message.conversation = Some(body.to_owned()); + message +} + +fn kind(encrypted: &Encrypted) -> ReceiveKind { + match encrypted.kind { + wacore_signal_ops::EncKind::PreKey => ReceiveKind::PreKey, + wacore_signal_ops::EncKind::Message => ReceiveKind::Message, + } +} + +fn request<'a>( + chat: &'a str, + sender: &'a ProtocolAddress, + kind: ReceiveKind, + ciphertext: &'a [u8], + is_from_me: bool, +) -> ReceiveRequest<'a> { + ReceiveRequest { + chat, + sender, + kind, + ciphertext, + padding_version: 2, + is_from_me, + // Only a prekey message carries the sender's device identity. + device_identity: (kind == ReceiveKind::PreKey) + .then(|| SENDER_IDENTITIES.with(|map| map.borrow().get(sender.name()).copied())) + .flatten(), + } +} + +fn message(received: Received) -> ReceivedMessage { + match received { + Received::Message(message) => *message, + Received::AlreadyDelivered { .. } => panic!("expected a message"), + } +} + +#[test] +fn redelivery_returns_the_buffered_result_until_delivered() { + let alice = device("alice-redeliver"); + let bob = device("bob-redeliver"); + let (alice_addr, bob_addr) = (address("111"), address("222")); + connect(&alice, &bob, &alice_addr, &bob_addr); + + let sent = block_on(alice.ops.encrypt(&bob_addr, &padded(&text("hello")))).unwrap(); + let chat = alice_addr.name().to_owned(); + let req = request(&chat, &alice_addr, kind(&sent), &sent.ciphertext, false); + + let first = message(block_on(bob.ops.receive(req)).unwrap()); + assert_eq!(first.content.message.conversation.as_deref(), Some("hello")); + assert!(!first.redelivered); + + let again = message(block_on(bob.ops.receive(req)).unwrap()); + assert!(again.redelivered); + assert_eq!(again.receipt_key, first.receipt_key); + assert_eq!(again.content.message.conversation.as_deref(), Some("hello")); + + block_on(bob.ops.mark_delivered(&first.receipt_key)).unwrap(); + assert!(matches!( + block_on(bob.ops.receive(req)).unwrap(), + Received::AlreadyDelivered { .. } + )); +} + +#[test] +fn a_fenced_out_receive_leaves_the_ciphertext_decryptable_by_the_successor() { + let alice = device("alice-atomic"); + let bob = device("bob-atomic"); + let (alice_addr, bob_addr) = (address("111"), address("222")); + connect(&alice, &bob, &alice_addr, &bob_addr); + let chat = alice_addr.name().to_owned(); + + let first = block_on(alice.ops.encrypt(&bob_addr, &padded(&text("one")))).unwrap(); + block_on(bob.ops.receive(request( + &chat, + &alice_addr, + kind(&first), + &first.ciphertext, + false, + ))) + .unwrap(); + + let second = block_on(alice.ops.encrypt(&bob_addr, &padded(&text("two")))).unwrap(); + let successor = store_for(&bob.records, bob.scope, "node-b", TTL + 1); + let stale = block_on(bob.ops.receive(request( + &chat, + &alice_addr, + kind(&second), + &second.ciphertext, + false, + ))); + assert!(stale.expect_err("fenced out").is_fence_lost()); + + // The stale node's ratchet advance never committed, so nothing was lost. + let bob_b = block_on(SignalOps::open(successor)).unwrap(); + let got = message( + block_on(bob_b.receive(request( + &chat, + &alice_addr, + kind(&second), + &second.ciphertext, + false, + ))) + .unwrap(), + ); + assert_eq!(got.content.message.conversation.as_deref(), Some("two")); +} + +#[test] +fn a_sender_key_carried_in_a_pairwise_message_unlocks_group_traffic() { + let alice = device("alice-skdm"); + let bob = device("bob-skdm"); + let (alice_addr, bob_addr) = (address("111"), address("222")); + connect(&alice, &bob, &alice_addr, &bob_addr); + + let skdm = block_on(alice.ops.sender_key_distribution(GROUP, &alice_addr)).unwrap(); + let mut distribution = wa::message::SenderKeyDistributionMessage::default(); + distribution.group_id = Some(GROUP.to_owned()); + distribution.axolotl_sender_key_distribution_message = Some(skdm); + let mut carrier = wa::Message::default(); + carrier.sender_key_distribution_message = Some(distribution).into(); + let sent = block_on(alice.ops.encrypt(&bob_addr, &padded(&carrier))).unwrap(); + let received = message( + block_on(bob.ops.receive(request( + GROUP, + &alice_addr, + kind(&sent), + &sent.ciphertext, + false, + ))) + .unwrap(), + ); + assert!(received.content.is_skdm_only); + + let skmsg = block_on( + alice + .ops + .group_encrypt(GROUP, &alice_addr, &padded(&text("hi group"))), + ) + .unwrap(); + let group = message( + block_on(bob.ops.receive(request( + GROUP, + &alice_addr, + ReceiveKind::SenderKey, + &skmsg, + false, + ))) + .unwrap(), + ); + assert_eq!( + group.content.message.conversation.as_deref(), + Some("hi group") + ); +} + +#[test] +fn only_own_devices_can_hand_over_app_state_keys() { + let alice = device("alice-keys"); + let bob = device("bob-keys"); + let (alice_addr, bob_addr) = (address("111"), address("222")); + connect(&alice, &bob, &alice_addr, &bob_addr); + let mut protocol = wa::message::ProtocolMessage::default(); + protocol.app_state_sync_key_share = Some(wa::message::AppStateSyncKeyShare::default()).into(); + let mut share = wa::Message::default(); + share.protocol_message = Some(protocol).into(); + let chat = alice_addr.name().to_owned(); + + let from_peer = block_on(alice.ops.encrypt(&bob_addr, &padded(&share))).unwrap(); + let peer = message( + block_on(bob.ops.receive(request( + &chat, + &alice_addr, + kind(&from_peer), + &from_peer.ciphertext, + false, + ))) + .unwrap(), + ); + assert!(peer.app_state_key_share.is_none()); + assert!( + peer.content + .message + .protocol_message + .as_option() + .is_none_or(|p| p.app_state_sync_key_share.as_option().is_none()), + "a peer's key share must not reach the app inside the message" + ); + + // The client claims Alice is one of Bob's own devices: refused, so her + // share cannot pass as Bob's own. + let claimed = block_on(alice.ops.encrypt(&bob_addr, &padded(&share))).unwrap(); + assert!(matches!( + block_on(bob.ops.receive(request( + &chat, + &alice_addr, + kind(&claimed), + &claimed.ciphertext, + true, + ))), + Err(wacore_signal_ops::OpsError::InvalidInput(_)) + )); + + block_on(bob.ops.set_own_jids("222:1@s.whatsapp.net", None)).unwrap(); + + // Bob's own second device, paired under Bob's account, may hand it over. + let account = KeyPair::generate(&mut rand::make_rng::()); + let bob_identity = block_on(common::pair_under(&bob.ops, &account)); + let second = device("bob-keys-second"); + block_on(second.ops.set_own_jids("222:2@s.whatsapp.net", None)).unwrap(); + let second_identity = block_on(common::pair_under(&second.ops, &account)); + let identity = block_on(bob.ops.public_identity()); + let prekey = block_on(bob.ops.generate_prekeys(&InlineRuntime, 1)) + .unwrap() + .remove(0); + block_on(second.ops.establish_session( + &bob_addr, + &RemoteBundle { + registration_id: identity.registration_id, + device_id: 1, + identity_key: identity.identity_key, + signed_prekey_id: identity.signed_prekey.id, + signed_prekey: identity.signed_prekey.public_key, + signed_prekey_signature: identity.signed_prekey.signature, + prekey: Some((prekey.id, prekey.public_key)), + device_identity: Some(bob_identity), + }, + )) + .unwrap(); + let second_addr = ProtocolAddress::new("222@s.whatsapp.net", DeviceId::from(2)); + let from_self = block_on(second.ops.encrypt(&bob_addr, &padded(&share))).unwrap(); + let own = message( + block_on(bob.ops.receive(ReceiveRequest { + chat: &chat, + sender: &second_addr, + kind: kind(&from_self), + ciphertext: &from_self.ciphertext, + padding_version: 2, + is_from_me: true, + device_identity: Some(&second_identity), + })) + .unwrap(), + ); + assert!(own.app_state_key_share.is_some()); +} + +#[test] +fn an_unpaired_account_takes_no_app_state_keys_from_its_own_number() { + let bob = device("bob-unpaired-keys"); + let mut protocol = wa::message::ProtocolMessage::default(); + protocol.app_state_sync_key_share = Some(wa::message::AppStateSyncKeyShare::default()).into(); + let mut share = wa::Message::default(); + share.protocol_message = Some(protocol).into(); + let chat = "222@s.whatsapp.net".to_owned(); + // Before pairing, a sender that only names Bob's own number proves + // nothing: another device poses as Bob's primary. + block_on(bob.ops.set_own_jids("222:1@s.whatsapp.net", None)).unwrap(); + let poser = device("bob-keys-poser"); + let own_primary = ProtocolAddress::new("222@s.whatsapp.net", DeviceId::from(0)); + let bob_public = block_on(bob.ops.public_identity()); + let bob_prekey = block_on(bob.ops.generate_prekeys(&InlineRuntime, 1)) + .unwrap() + .remove(0); + // The poser addresses Bob as a primary, which needs no device identity. + let bob_primary = ProtocolAddress::new("222@s.whatsapp.net", DeviceId::from(0)); + block_on(poser.ops.establish_session( + &bob_primary, + &RemoteBundle { + registration_id: bob_public.registration_id, + device_id: 0, + identity_key: bob_public.identity_key, + signed_prekey_id: bob_public.signed_prekey.id, + signed_prekey: bob_public.signed_prekey.public_key, + signed_prekey_signature: bob_public.signed_prekey.signature, + prekey: Some((bob_prekey.id, bob_prekey.public_key)), + device_identity: None, + }, + )) + .unwrap(); + let posed = block_on(poser.ops.encrypt(&bob_primary, &padded(&share))).unwrap(); + let posed_request = |is_from_me| ReceiveRequest { + chat: &chat, + sender: &own_primary, + kind: kind(&posed), + ciphertext: &posed.ciphertext, + padding_version: 2, + is_from_me, + device_identity: None, + }; + assert!(matches!( + block_on(bob.ops.receive(posed_request(true))), + Err(wacore_signal_ops::OpsError::InvalidInput(_)) + )); + let posed = message(block_on(bob.ops.receive(posed_request(false))).unwrap()); + assert!( + posed.app_state_key_share.is_none(), + "an unpaired account has no own devices to take keys from" + ); +} + +#[test] +fn pruning_drops_old_delivered_markers_and_keeps_pending_results() { + let alice = device("alice-prune"); + let bob = device("bob-prune"); + let (alice_addr, bob_addr) = (address("111"), address("222")); + connect(&alice, &bob, &alice_addr, &bob_addr); + let chat = alice_addr.name().to_owned(); + let sent = block_on(alice.ops.encrypt(&bob_addr, &padded(&text("old")))).unwrap(); + let req = request(&chat, &alice_addr, kind(&sent), &sent.ciphertext, false); + let first = message(block_on(bob.ops.receive(req)).unwrap()); + + // Not yet delivered: the buffer holds the only copy of the plaintext. + assert_eq!(block_on(bob.ops.prune_decrypt_buffer(u64::MAX)).unwrap(), 0); + assert!(message(block_on(bob.ops.receive(req)).unwrap()).redelivered); + + block_on(bob.ops.mark_delivered(&first.receipt_key)).unwrap(); + assert_eq!(block_on(bob.ops.prune_decrypt_buffer(0)).unwrap(), 0); + assert_eq!(block_on(bob.ops.prune_decrypt_buffer(u64::MAX)).unwrap(), 1); +} + +#[test] +fn a_bad_sender_key_does_not_block_the_message_carrying_it() { + let alice = device("alice-bad-skdm"); + let bob = device("bob-bad-skdm"); + let (alice_addr, bob_addr) = (address("111"), address("222")); + connect(&alice, &bob, &alice_addr, &bob_addr); + let mut distribution = wa::message::SenderKeyDistributionMessage::default(); + distribution.group_id = Some(GROUP.to_owned()); + distribution.axolotl_sender_key_distribution_message = Some(vec![0xde, 0xad]); + let mut carrier = wa::Message::default(); + carrier.conversation = Some("still readable".to_owned()); + carrier.sender_key_distribution_message = Some(distribution).into(); + let sent = block_on(alice.ops.encrypt(&bob_addr, &padded(&carrier))).unwrap(); + let req = request(GROUP, &alice_addr, kind(&sent), &sent.ciphertext, false); + let first = message(block_on(bob.ops.receive(req)).unwrap()); + assert!(first.sender_key_rejected); + assert_eq!( + first.content.message.conversation.as_deref(), + Some("still readable") + ); + // Committed: a redelivery is served from the buffer, not decrypted again. + let again = message(block_on(bob.ops.receive(req)).unwrap()); + assert!(again.redelivered); + assert!( + again.sender_key_rejected, + "a redelivery must still report the rejected distribution" + ); +} + +/// Fails the next sender-key read, as a transient backend error would. +struct FlakySenderKeys { + inner: Arc, + fail: std::sync::atomic::AtomicBool, +} + +impl FlakySenderKeys { + fn check(&self, ns: Namespace) -> wacore::store::error::Result<()> { + if ns == Namespace::SenderKey && self.fail.swap(false, std::sync::atomic::Ordering::SeqCst) + { + return Err(wacore::store::error::StoreError::Validation( + "backend unavailable".into(), + )); + } + Ok(()) + } +} + +#[async_trait::async_trait] +impl RecordStore for FlakySenderKeys { + async fn get( + &self, + scope: &str, + ns: Namespace, + key: &str, + ) -> wacore::store::error::Result>> { + self.check(ns)?; + self.inner.get(scope, ns, key).await + } + + async fn get_many( + &self, + scope: &str, + ns: Namespace, + keys: &[&str], + ) -> wacore::store::error::Result)>> { + self.check(ns)?; + self.inner.get_many(scope, ns, keys).await + } + + async fn scan_keys( + &self, + scope: &str, + ns: Namespace, + prefix: &str, + limit: Option, + ) -> wacore::store::error::Result> { + self.inner.scan_keys(scope, ns, prefix, limit).await + } + + async fn write(&self, fence: &Fence, ops: &[WriteOp]) -> wacore::store::error::Result<()> { + self.inner.write(fence, ops).await + } +} + +#[test] +fn a_storage_failure_installing_a_sender_key_is_retried_on_redelivery() { + let alice = device("alice-flaky"); + let records = Arc::new(FlakySenderKeys { + inner: Arc::new(MemoryRecordStore::new()), + fail: std::sync::atomic::AtomicBool::new(false), + }); + let lease = block_on(records.inner.acquire("bob-flaky", "node-a", 0, TTL)) + .unwrap() + .expect("lease available"); + let bob = block_on(SignalOps::create(Arc::new(RecordSignalStore::new( + records.clone(), + Arc::new(AesGcmSealer::new(&[9; 32])), + lease.fence, + )))) + .unwrap(); + let (alice_addr, bob_addr) = (address("111"), address("222")); + + let device_identity = block_on(common::vouch(&alice.ops, &bob, bob_addr.name())); + accept_sender(&bob, &alice.ops, &alice_addr); + let identity = block_on(bob.public_identity()); + let prekey = block_on(bob.generate_prekeys(&InlineRuntime, 1)) + .unwrap() + .remove(0); + block_on(alice.ops.establish_session( + &bob_addr, + &RemoteBundle { + registration_id: identity.registration_id, + device_id: 1, + identity_key: identity.identity_key, + signed_prekey_id: identity.signed_prekey.id, + signed_prekey: identity.signed_prekey.public_key, + signed_prekey_signature: identity.signed_prekey.signature, + prekey: Some((prekey.id, prekey.public_key)), + device_identity: Some(device_identity), + }, + )) + .unwrap(); + + let skdm = block_on(alice.ops.sender_key_distribution(GROUP, &alice_addr)).unwrap(); + let mut distribution = wa::message::SenderKeyDistributionMessage::default(); + distribution.group_id = Some(GROUP.to_owned()); + distribution.axolotl_sender_key_distribution_message = Some(skdm); + let mut carrier = wa::Message::default(); + carrier.sender_key_distribution_message = Some(distribution).into(); + let sent = block_on(alice.ops.encrypt(&bob_addr, &padded(&carrier))).unwrap(); + let req = request(GROUP, &alice_addr, kind(&sent), &sent.ciphertext, false); + + records + .fail + .store(true, std::sync::atomic::Ordering::SeqCst); + assert!( + block_on(bob.receive(req)).is_err(), + "a storage failure must not commit the message without its key" + ); + let retried = message(block_on(bob.receive(req)).unwrap()); + assert!( + !retried.redelivered, + "nothing was buffered by the failed attempt" + ); + assert!(!retried.sender_key_rejected); + + let skmsg = block_on(alice.ops.group_encrypt( + GROUP, + &alice_addr, + &padded(&text("after retry")), + )) + .unwrap(); + let group = message( + block_on(bob.receive(request( + GROUP, + &alice_addr, + ReceiveKind::SenderKey, + &skmsg, + false, + ))) + .unwrap(), + ); + assert_eq!( + group.content.message.conversation.as_deref(), + Some("after retry") + ); +} + +#[test] +fn a_sender_key_for_another_group_is_not_installed_under_this_chat() { + const OTHER: &str = "120363000000009@g.us"; + let alice = device("alice-other-group"); + let bob = device("bob-other-group"); + let (alice_addr, bob_addr) = (address("111"), address("222")); + connect(&alice, &bob, &alice_addr, &bob_addr); + + let skdm = block_on(alice.ops.sender_key_distribution(OTHER, &alice_addr)).unwrap(); + let mut distribution = wa::message::SenderKeyDistributionMessage::default(); + distribution.group_id = Some(OTHER.to_owned()); + distribution.axolotl_sender_key_distribution_message = Some(skdm); + let mut carrier = wa::Message::default(); + carrier.sender_key_distribution_message = Some(distribution).into(); + let sent = block_on(alice.ops.encrypt(&bob_addr, &padded(&carrier))).unwrap(); + let received = message( + block_on(bob.ops.receive(request( + GROUP, + &alice_addr, + kind(&sent), + &sent.ciphertext, + false, + ))) + .unwrap(), + ); + assert!(received.sender_key_rejected); + + // The other group's chain was not stored under this chat. + let skmsg = block_on(alice.ops.group_encrypt( + OTHER, + &alice_addr, + &padded(&text("other group")), + )) + .unwrap(); + assert!( + block_on(bob.ops.receive(request( + GROUP, + &alice_addr, + ReceiveKind::SenderKey, + &skmsg, + false, + ))) + .is_err(), + "a key for another group must not decrypt this chat's messages" + ); +} + +#[test] +fn a_message_that_replaces_the_stored_identity_reports_it() { + let alice = device("alice-identity-msg"); + let bob = device("bob-identity-msg"); + let (alice_addr, bob_addr) = (address("111"), address("222")); + connect(&alice, &bob, &alice_addr, &bob_addr); + let chat = alice_addr.name().to_owned(); + let bob_chat = bob_addr.name().to_owned(); + + // Establish both directions so Alice's next message is a plain `msg`. + let first = block_on(alice.ops.encrypt(&bob_addr, &padded(&text("one")))).unwrap(); + block_on(bob.ops.receive(request( + &chat, + &alice_addr, + kind(&first), + &first.ciphertext, + false, + ))) + .unwrap(); + let reply = block_on(bob.ops.encrypt(&alice_addr, &padded(&text("two")))).unwrap(); + block_on(alice.ops.receive(request( + &bob_chat, + &bob_addr, + kind(&reply), + &reply.ciphertext, + false, + ))) + .unwrap(); + + // Bob's stored identity for Alice no longer matches the session's. + let other = block_on(alice.ops.public_identity()) + .signed_prekey + .public_key; + let store = store_for(&bob.records, bob.scope, "node-a", 0); + block_on(wacore::store::traits::SignalStore::put_identity( + &*store, + alice_addr.as_str(), + other, + )) + .unwrap(); + + let next = block_on(alice.ops.encrypt(&bob_addr, &padded(&text("three")))).unwrap(); + assert!(matches!(kind(&next), ReceiveKind::Message)); + let received = message( + block_on(bob.ops.receive(request( + &chat, + &alice_addr, + kind(&next), + &next.ciphertext, + false, + ))) + .unwrap(), + ); + assert!( + received.identity_changed, + "a msg that replaces the stored identity must say so" + ); +} diff --git a/crates/signal-ops/tests/runtime/mod.rs b/crates/signal-ops/tests/runtime/mod.rs new file mode 100644 index 0000000..aa0c97a --- /dev/null +++ b/crates/signal-ops/tests/runtime/mod.rs @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: MIT + +//! A runtime for tests driven by `block_on`: blocking work runs inline. + +use std::future::Future; +use std::pin::Pin; +use std::time::Duration; +use wacore::runtime::{AbortHandle, Runtime}; + +type BoxFuture = Pin + Send + 'static>>; + +pub struct InlineRuntime; + +impl Runtime for InlineRuntime { + fn spawn(&self, future: BoxFuture) -> AbortHandle { + std::thread::spawn(move || futures::executor::block_on(future)); + AbortHandle::new(|| {}) + } + + fn sleep(&self, duration: Duration) -> Pin + Send>> { + Box::pin(async move { std::thread::sleep(duration) }) + } + + fn spawn_blocking(&self, f: Box) -> BoxFuture { + Box::pin(async move { f() }) + } + + fn yield_now(&self) -> Option + Send>>> { + None + } +} diff --git a/crates/signal-ops/tests/send.rs b/crates/signal-ops/tests/send.rs new file mode 100644 index 0000000..8588df2 --- /dev/null +++ b/crates/signal-ops/tests/send.rs @@ -0,0 +1,2178 @@ +// SPDX-License-Identifier: MIT + +//! Outbound: the service builds the stanza, the client only resolves devices +//! and bundles, and the recipient decrypts what was built. + +use async_trait::async_trait; +use std::collections::HashMap; +use std::future::Future; +use std::pin::Pin; +use std::sync::Arc; +use std::time::Duration; +use wacore::client::context::{GroupRoutingInfo, SendContextResolver, SessionLockGuard}; +use wacore::libsignal::protocol::{DeviceId, IdentityKey, PreKeyBundle, PublicKey}; +use wacore::prekeys::PreKeyFetchOutcome; +use wacore::runtime::{AbortHandle, Runtime}; +use wacore::types::jid::JidExt as _; +use wacore_binary::{Jid, NodeContent}; +use wacore_recordstore::{AesGcmSealer, LeaseStore, MemoryRecordStore, RecordSignalStore}; +mod common; +use wacore_signal_ops::{ + FetchedBundle, ReceiveKind, ReceiveRequest, Received, SignalOps, SignalResolver, + VerifiablePrekeys, +}; +use waproto::whatsapp as wa; + +mod runtime; +use runtime::InlineRuntime; + +type Store = RecordSignalStore; +type BoxFuture = Pin + Send + 'static>>; + +struct TokioRuntime; + +impl Runtime for TokioRuntime { + fn spawn(&self, future: BoxFuture) -> AbortHandle { + let task = tokio::spawn(future); + AbortHandle::new(move || task.abort()) + } + + fn sleep(&self, duration: Duration) -> Pin + Send>> { + Box::pin(tokio::time::sleep(duration)) + } + + fn spawn_blocking(&self, f: Box) -> BoxFuture { + Box::pin(async move { + let _ = tokio::task::spawn_blocking(f).await; + }) + } + + fn yield_now(&self) -> Option + Send>>> { + Some(Box::pin(tokio::task::yield_now())) + } +} + +async fn device(scope: &str, own_pn: &str) -> SignalOps { + device_with_lid(scope, own_pn, None).await +} + +async fn device_with_lid(scope: &str, own_pn: &str, own_lid: Option<&str>) -> SignalOps { + let records = Arc::new(MemoryRecordStore::new()); + let lease = records + .acquire(scope, "node-a", 0, 60_000) + .await + .unwrap() + .unwrap(); + let store = Arc::new(RecordSignalStore::new( + records, + Arc::new(AesGcmSealer::new(&[5; 32])), + lease.fence, + )); + let ops = SignalOps::create(store).await.unwrap(); + ops.set_own_jids(own_pn, own_lid).await.unwrap(); + ops +} + +/// Stands in for the WhatsApp client: answers device-list and prekey +/// queries from a fixed table, as a real client would from the server. +struct FakeClient { + devices: Vec, + bundles: HashMap, + group: Option>, + /// The `` the server sends with each companion's bundle. + device_identities: HashMap>, +} + +#[async_trait] +impl SendContextResolver for FakeClient { + async fn lock_device_sessions(&self, _device_jids: &[Jid]) -> SessionLockGuard { + SessionLockGuard::none() + } + + async fn resolve_devices(&self, _jids: &[Jid]) -> Result, anyhow::Error> { + Ok(self.devices.clone()) + } + + async fn fetch_prekeys( + &self, + jids: &[Jid], + ) -> Result, anyhow::Error> { + Ok(jids + .iter() + .filter_map(|jid| self.bundles.get(jid).map(|b| (jid.clone(), b.clone()))) + .collect()) + } + + async fn fetch_prekeys_for_identity_check( + &self, + jids: &[Jid], + ) -> Result { + Ok(PreKeyFetchOutcome { + bundles: self.fetch_prekeys(jids).await?, + rejected: Vec::new(), + }) + } + + async fn resolve_group_routing_info( + &self, + _jid: &Jid, + ) -> Result, anyhow::Error> { + self.group + .clone() + .ok_or_else(|| anyhow::anyhow!("not a group test")) + } +} + +async fn bundle_for(ops: &SignalOps) -> PreKeyBundle { + let identity = ops.public_identity().await; + let prekey = ops + .generate_prekeys(&InlineRuntime, 1) + .await + .unwrap() + .remove(0); + PreKeyBundle::new( + identity.registration_id, + DeviceId::from(1), + Some(( + prekey.id.into(), + PublicKey::from_djb_public_key_bytes(&prekey.public_key).unwrap(), + )), + identity.signed_prekey.id.into(), + PublicKey::from_djb_public_key_bytes(&identity.signed_prekey.public_key).unwrap(), + identity.signed_prekey.signature, + IdentityKey::new(PublicKey::from_djb_public_key_bytes(&identity.identity_key).unwrap()), + ) + .unwrap() +} + +/// Pair `peer` under a fresh account and have `sender` pin that account, as it +/// would after meeting the peer's primary device. Returns the companion's +/// `` entry for a client's prekey responses. +async fn vouch(sender: &SignalOps, peer: &SignalOps, jid: &Jid) -> (Jid, Vec) { + let paired = common::pair(peer).await; + sender + .trust_account_identity(jid, paired.account) + .await + .unwrap(); + (jid.clone(), paired.device_identity) +} + +/// The bundles `client` holds for `jids`, each with its companion identity. +fn verifiable(client: &FakeClient, jids: &[Jid]) -> VerifiablePrekeys { + VerifiablePrekeys { + bundles: jids + .iter() + .filter_map(|jid| { + client.bundles.get(jid).map(|bundle| { + ( + jid.clone(), + FetchedBundle { + bundle: bundle.clone(), + device_identity: client.device_identities.get(jid).cloned(), + }, + ) + }) + }) + .collect(), + rejected: Vec::new(), + } +} + +#[async_trait] +impl SignalResolver for FakeClient { + async fn fetch_verifiable_prekeys(&self, jids: &[Jid]) -> anyhow::Result { + Ok(verifiable(self, jids)) + } +} + +struct ChangingGroupClient { + current: std::sync::RwLock, + pause_devices: std::sync::atomic::AtomicBool, + entered: tokio::sync::Notify, + resume: tokio::sync::Notify, + change_mapping: bool, + mapping_reads: std::sync::atomic::AtomicUsize, +} + +impl ChangingGroupClient { + fn new(client: FakeClient, change_mapping: bool) -> Self { + Self { + current: std::sync::RwLock::new(client), + pause_devices: std::sync::atomic::AtomicBool::new(false), + entered: tokio::sync::Notify::new(), + resume: tokio::sync::Notify::new(), + change_mapping, + mapping_reads: std::sync::atomic::AtomicUsize::new(0), + } + } +} + +#[async_trait] +impl SendContextResolver for ChangingGroupClient { + async fn lock_device_sessions(&self, _device_jids: &[Jid]) -> SessionLockGuard { + SessionLockGuard::none() + } + + async fn resolve_devices(&self, _jids: &[Jid]) -> anyhow::Result> { + let snapshot = self.current.read().unwrap().devices.clone(); + if self + .pause_devices + .swap(false, std::sync::atomic::Ordering::SeqCst) + { + self.entered.notify_one(); + self.resume.notified().await; + } + Ok(snapshot) + } + + async fn fetch_prekeys(&self, jids: &[Jid]) -> anyhow::Result> { + let client = self.current.read().unwrap(); + Ok(jids + .iter() + .filter_map(|jid| { + client + .bundles + .get(jid) + .map(|bundle| (jid.clone(), bundle.clone())) + }) + .collect()) + } + + async fn fetch_prekeys_for_identity_check( + &self, + jids: &[Jid], + ) -> anyhow::Result { + Ok(PreKeyFetchOutcome { + bundles: self.fetch_prekeys(jids).await?, + rejected: Vec::new(), + }) + } + + async fn resolve_group_routing_info( + &self, + _jid: &Jid, + ) -> anyhow::Result> { + Ok(self + .current + .read() + .unwrap() + .group + .clone() + .expect("group fixture")) + } + + async fn get_lid_for_phone(&self, user: &str) -> Option { + if self.change_mapping && user == "222" { + let previous = self + .mapping_reads + .fetch_add(1, std::sync::atomic::Ordering::SeqCst); + return (previous > 0).then(|| "900222".into()); + } + None + } +} + +#[async_trait] +impl SignalResolver for ChangingGroupClient { + async fn fetch_verifiable_prekeys(&self, jids: &[Jid]) -> anyhow::Result { + Ok(verifiable(&self.current.read().unwrap(), jids)) + } +} + +async fn receive_group_component( + ops: &SignalOps, + group: &Jid, + from: &Jid, + from_identity: &[u8], + (kind, version, ciphertext): (String, u8, Vec), +) -> Result { + ops.receive(ReceiveRequest { + chat: &group.to_string(), + sender: &from.to_protocol_address(), + kind: ReceiveKind::try_from(kind.as_str()).expect("enc type"), + ciphertext: &ciphertext, + padding_version: version, + is_from_me: false, + device_identity: (kind == "pkmsg").then_some(from_identity), + }) + .await +} + +/// The `` a stanza carries for its prekey messages. +fn stanza_device_identity(stanza: &[u8]) -> Option> { + let node = parse(stanza); + let identity = node.get_optional_child("device-identity")?; + match &identity.content { + Some(NodeContent::Bytes(bytes)) => Some(bytes.clone()), + _ => None, + } +} + +fn parse(stanza: &[u8]) -> wacore_binary::Node { + wacore_binary::marshal::unmarshal_packed_ref(stanza) + .unwrap() + .to_owned() +} + +fn enc_parts(enc: &wacore_binary::Node) -> (String, u8, Vec) { + let kind = enc.attrs().optional_string("type").unwrap().to_string(); + let version = enc + .attrs() + .optional_string("v") + .map_or(2, |v| v.parse().unwrap()); + let Some(NodeContent::Bytes(bytes)) = &enc.content else { + panic!("enc without bytes") + }; + (kind, version, bytes.clone()) +} + +/// The pairwise `` addressed to `device`, if the stanza has one. +fn pairwise_enc_for(stanza: &[u8], device: &Jid) -> Option<(String, u8, Vec)> { + let node = parse(stanza); + let participants = node.get_optional_child("participants")?; + let to = participants.get_children_by_tag("to").find(|to| { + to.attrs().optional_string("jid").as_deref() == Some(device.to_string().as_str()) + })?; + Some(enc_parts(to.get_optional_child("enc").expect("enc"))) +} + +/// The `` addressed to `device` inside a marshaled stanza. +fn enc_for(stanza: &[u8], device: &Jid) -> (String, u8, Vec) { + pairwise_enc_for(stanza, device).expect("an entry for the device") +} + +/// The group-wide `skmsg` ``. +fn skmsg_of(stanza: &[u8]) -> (String, u8, Vec) { + let node = parse(stanza); + let enc = node + .get_children_by_tag("enc") + .find(|e| e.attrs().optional_string("type").as_deref() == Some("skmsg")) + .expect("skmsg"); + enc_parts(enc) +} + +fn text(body: &str) -> wa::Message { + let mut message = wa::Message::default(); + message.conversation = Some(body.to_owned()); + message +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_stanza_built_by_the_service_decrypts_at_the_recipient() { + let alice_jid: Jid = "111:1@s.whatsapp.net".parse().unwrap(); + let bob_jid: Jid = "222:1@s.whatsapp.net".parse().unwrap(); + let alice = device("alice-send", "111:1@s.whatsapp.net").await; + let bob = device("bob-send", "222:1@s.whatsapp.net").await; + let alice_identity = common::vouch(&bob, &alice, "111:1@s.whatsapp.net").await; + let client = FakeClient { + devices: vec![bob_jid.clone()], + bundles: HashMap::from([(bob_jid.clone(), bundle_for(&bob).await)]), + device_identities: HashMap::from([vouch(&alice, &bob, &bob_jid).await]), + group: None, + }; + let to = bob_jid.to_non_ad(); + + for (round, body) in ["first message", "second message"].into_iter().enumerate() { + let id = format!("MSG{round}"); + let sent = alice + .send_direct(&TokioRuntime, &client, &to, &text(body), &id) + .await + .unwrap(); + assert!(sent.unreached_devices.is_empty()); + let (kind, version, ciphertext) = enc_for(&sent.stanza, &bob_jid); + // Until the recipient replies, every message on a new session is a + // prekey message (libsignal's unacknowledged-prekey state). + assert_eq!(kind, "pkmsg"); + + let sender = alice_jid.to_protocol_address(); + let received = bob + .receive(ReceiveRequest { + chat: &alice_jid.to_non_ad().to_string(), + sender: &sender, + kind: if kind == "pkmsg" { + ReceiveKind::PreKey + } else { + ReceiveKind::Message + }, + ciphertext: &ciphertext, + padding_version: version, + is_from_me: false, + device_identity: (kind == "pkmsg").then_some(alice_identity.as_slice()), + }) + .await + .unwrap(); + let Received::Message(message) = received else { + panic!("expected a message") + }; + assert_eq!(message.content.message.conversation.as_deref(), Some(body)); + + let kept = alice.sent_message(&to, &id).await.unwrap().unwrap(); + assert_eq!(kept.conversation.as_deref(), Some(body)); + } + + assert_eq!(alice.prune_sent_messages(0).await.unwrap(), 0); + assert_eq!(alice.prune_sent_messages(u64::MAX).await.unwrap(), 2); + assert!(alice.sent_message(&to, "MSG0").await.unwrap().is_none()); +} + +#[tokio::test(flavor = "multi_thread")] +async fn an_unpaired_device_refuses_to_send() { + let records = Arc::new(MemoryRecordStore::new()); + let lease = records + .acquire("unpaired", "node-a", 0, 60_000) + .await + .unwrap() + .unwrap(); + let ops = SignalOps::create(Arc::new(RecordSignalStore::new( + records, + Arc::new(AesGcmSealer::new(&[5; 32])), + lease.fence, + ))) + .await + .unwrap(); + let client = FakeClient { + devices: Vec::new(), + bundles: HashMap::new(), + device_identities: HashMap::new(), + group: None, + }; + let to: Jid = "222@s.whatsapp.net".parse().unwrap(); + assert!( + ops.send_direct(&TokioRuntime, &client, &to, &text("x"), "ID") + .await + .is_err() + ); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_group_send_distributes_the_sender_key_once_then_reuses_it() { + use wacore::types::message::AddressingMode; + + let group: Jid = "120363000000001@g.us".parse().unwrap(); + let alice_jid: Jid = "111:1@s.whatsapp.net".parse().unwrap(); + // A primary device, so the primary-device gate is met once it is marked. + let bob_jid: Jid = "222@s.whatsapp.net".parse().unwrap(); + let alice = device_with_lid( + "alice-group-send", + "111:1@s.whatsapp.net", + Some("900111:1@lid"), + ) + .await; + let bob = device("bob-group-send", "222@s.whatsapp.net").await; + let alice_identity = common::vouch(&bob, &alice, "111:1@s.whatsapp.net").await; + let client = FakeClient { + devices: vec![alice_jid.clone(), bob_jid.clone()], + bundles: HashMap::from([(bob_jid.clone(), bundle_for(&bob).await)]), + device_identities: HashMap::new(), + group: Some(Arc::new(GroupRoutingInfo::new( + vec![alice_jid.to_non_ad(), bob_jid.clone()], + AddressingMode::Pn, + ))), + }; + let alice_addr = alice_jid.to_protocol_address(); + let group_str = group.to_string(); + let receive = |kind: &str, version: u8, ciphertext: Vec| { + let sender = alice_addr.clone(); + let chat = group_str.clone(); + let kind = ReceiveKind::try_from(kind).expect("enc type"); + let bob = &bob; + let identity = alice_identity.as_slice(); + async move { + bob.receive(ReceiveRequest { + chat: &chat, + sender: &sender, + kind, + ciphertext: &ciphertext, + padding_version: version, + is_from_me: false, + device_identity: (kind == ReceiveKind::PreKey).then_some(identity), + }) + .await + .unwrap() + } + }; + + let first = alice + .send_group(&TokioRuntime, &client, &group, &text("hello group"), "G1") + .await + .unwrap(); + assert_eq!(first.distribution_targets, vec![bob_jid.clone()]); + let (kind, version, carrier) = pairwise_enc_for(&first.stanza, &bob_jid).expect("key for bob"); + let Received::Message(_) = receive(&kind, version, carrier).await else { + panic!("sender key carrier was not delivered") + }; + let (_, version, skmsg) = skmsg_of(&first.stanza); + let Received::Message(message) = receive("skmsg", version, skmsg).await else { + panic!("expected the group message") + }; + assert_eq!( + message.content.message.conversation.as_deref(), + Some("hello group") + ); + + assert!( + alice + .mark_sender_key_distributed( + &group, + &first.distribution_targets, + first.distribution_epoch, + ) + .await + .unwrap() + ); + let second = alice + .send_group(&TokioRuntime, &client, &group, &text("second"), "G2") + .await + .unwrap(); + assert!(second.distribution_targets.is_empty()); + assert!(pairwise_enc_for(&second.stanza, &bob_jid).is_none()); + let (_, version, skmsg) = skmsg_of(&second.stanza); + let Received::Message(message) = receive("skmsg", version, skmsg).await else { + panic!("expected the group message") + }; + assert_eq!( + message.content.message.conversation.as_deref(), + Some("second") + ); + + // A retry receipt from Bob in the group makes the next send redistribute. + // Alice keeps the pairing Bob pinned above. + alice + .resend( + &client, + wacore_signal_ops::RetryRequest { + chat: &group, + message_id: "G2", + requester: &bob_jid, + encryption_jid: &bob_jid, + route: wacore_signal_ops::RetryRoute::Group { + addressing_mode: AddressingMode::Pn, + }, + retry_count: 1, + bundle: None, + }, + ) + .await + .unwrap(); + let after_retry = alice + .send_group(&TokioRuntime, &client, &group, &text("after retry"), "G2b") + .await + .unwrap(); + assert_eq!(after_retry.distribution_targets, vec![bob_jid.clone()]); + assert!( + alice + .mark_sender_key_distributed( + &group, + &after_retry.distribution_targets, + after_retry.distribution_epoch, + ) + .await + .unwrap() + ); + + alice.forget_sender_key_devices(&group).await.unwrap(); + let third = alice + .send_group(&TokioRuntime, &client, &group, &text("third"), "G3") + .await + .unwrap(); + assert_eq!(third.distribution_targets, vec![bob_jid.clone()]); + let (_, version, ciphertext) = skmsg_of(&third.stanza); + assert!( + bob.receive(ReceiveRequest { + chat: &group_str, + sender: &alice_addr, + kind: ReceiveKind::SenderKey, + ciphertext: &ciphertext, + padding_version: version, + is_from_me: false, + device_identity: None, + }) + .await + .is_err(), + "the old sender-key chain must not decrypt a message after rotation" + ); + // Bob verifies the identity the stanza actually carries. + assert_eq!( + stanza_device_identity(&third.stanza).as_deref(), + Some(alice_identity.as_slice()) + ); + let (kind, version, carrier) = + pairwise_enc_for(&third.stanza, &bob_jid).expect("fresh key for bob"); + assert!(matches!( + receive(&kind, version, carrier).await, + Received::Message(_) + )); + let (_, version, ciphertext) = skmsg_of(&third.stanza); + assert!(matches!( + receive("skmsg", version, ciphertext).await, + Received::Message(_) + )); +} + +#[tokio::test] +async fn a_send_waiting_on_devices_cannot_give_a_removed_member_the_replacement_chain() { + use std::task::Poll; + use wacore::types::message::AddressingMode; + let group: Jid = "12345@g.us".parse().unwrap(); + let alice_jid: Jid = "111:1@s.whatsapp.net".parse().unwrap(); + let bob_jid: Jid = "222@s.whatsapp.net".parse().unwrap(); + let carol_jid: Jid = "333@s.whatsapp.net".parse().unwrap(); + let alice = Arc::new( + device_with_lid( + "alice-removal-race", + &alice_jid.to_string(), + Some("900111:1@lid"), + ) + .await, + ); + let bob = device("bob-removal-race", &bob_jid.to_string()).await; + let carol = device("carol-removal-race", &carol_jid.to_string()).await; + // Pair Alice once; both recipients pin that one account. + let alice_paired = common::pair(&*alice).await; + for peer in [&bob, &carol] { + peer.trust_account_identity(&alice_jid, alice_paired.account) + .await + .unwrap(); + } + let alice_identity = alice_paired.device_identity.clone(); + let alice_identity_carol = alice_paired.device_identity.clone(); + let client = Arc::new(ChangingGroupClient::new( + FakeClient { + devices: vec![alice_jid.clone(), bob_jid.clone(), carol_jid.clone()], + bundles: HashMap::from([ + (bob_jid.clone(), bundle_for(&bob).await), + (carol_jid.clone(), bundle_for(&carol).await), + ]), + device_identities: HashMap::new(), + group: Some(Arc::new(GroupRoutingInfo::new( + vec![alice_jid.to_non_ad(), bob_jid.clone(), carol_jid.clone()], + AddressingMode::Pn, + ))), + }, + false, + )); + let first = alice + .send_group( + &TokioRuntime, + client.as_ref(), + &group, + &text("before removal"), + "BEFORE", + ) + .await + .unwrap(); + // The receivers verify with the identity the stanza actually carries. + assert_eq!( + stanza_device_identity(&first.stanza).as_deref(), + Some(alice_identity.as_slice()) + ); + receive_group_component( + &bob, + &group, + &alice_jid, + &alice_identity, + enc_for(&first.stanza, &bob_jid), + ) + .await + .unwrap(); + receive_group_component( + &bob, + &group, + &alice_jid, + &alice_identity, + skmsg_of(&first.stanza), + ) + .await + .unwrap(); + assert!( + alice + .mark_sender_key_distributed( + &group, + &first.distribution_targets, + first.distribution_epoch, + ) + .await + .unwrap() + ); + client + .pause_devices + .store(true, std::sync::atomic::Ordering::SeqCst); + let in_flight = { + let alice = alice.clone(); + let client = client.clone(); + let group = group.clone(); + tokio::spawn(async move { + alice + .send_group( + &TokioRuntime, + client.as_ref(), + &group, + &text("in flight"), + "INFLIGHT", + ) + .await + .unwrap() + }) + }; + client.entered.notified().await; + let mut rotation = Box::pin(alice.forget_sender_key_devices(&group)); + // Poll the rotation while the old device snapshot is suspended. A correct + // send finishes on its old chain before rotation can invalidate it. + let rotation_progress = + futures::future::poll_fn(|cx| Poll::Ready(rotation.as_mut().poll(cx))).await; + { + let mut current = client.current.write().unwrap(); + current.devices = vec![alice_jid.clone(), carol_jid.clone()]; + current.group = Some(Arc::new(GroupRoutingInfo::new( + vec![alice_jid.to_non_ad(), carol_jid.clone()], + AddressingMode::Pn, + ))); + } + client.resume.notify_one(); + let sent = in_flight.await.unwrap(); + match rotation_progress { + Poll::Ready(result) => result.unwrap(), + Poll::Pending => rotation.await.unwrap(), + } + if let Some(carrier) = pairwise_enc_for(&sent.stanza, &bob_jid) { + receive_group_component(&bob, &group, &alice_jid, &alice_identity, carrier) + .await + .unwrap(); + } + receive_group_component( + &bob, + &group, + &alice_jid, + &alice_identity, + skmsg_of(&sent.stanza), + ) + .await + .unwrap(); + let after = alice + .send_group( + &TokioRuntime, + client.as_ref(), + &group, + &text("after removal"), + "AFTER", + ) + .await + .unwrap(); + assert!(pairwise_enc_for(&after.stanza, &bob_jid).is_none()); + assert!( + receive_group_component( + &bob, + &group, + &alice_jid, + &alice_identity, + skmsg_of(&after.stanza) + ) + .await + .is_err(), + "a removed participant received the replacement sender key from a stale resolution" + ); + let carrier = enc_for(&after.stanza, &carol_jid); + if carrier.0 == "pkmsg" { + assert_eq!( + stanza_device_identity(&after.stanza).as_deref(), + Some(alice_identity_carol.as_slice()), + "Carol verifies the identity the stanza carries" + ); + } + receive_group_component(&carol, &group, &alice_jid, &alice_identity_carol, carrier) + .await + .unwrap(); + let Received::Message(message) = receive_group_component( + &carol, + &group, + &alice_jid, + &alice_identity_carol, + skmsg_of(&after.stanza), + ) + .await + .unwrap() else { + panic!("retained participant did not receive the message") + }; + assert_eq!( + message.content.message.conversation.as_deref(), + Some("after removal") + ); +} + +#[tokio::test] +async fn a_send_after_a_participant_is_removed_rotates_the_sender_key() { + use wacore::types::message::AddressingMode; + let group: Jid = "54321@g.us".parse().unwrap(); + let alice_jid: Jid = "111:1@s.whatsapp.net".parse().unwrap(); + let bob_jid: Jid = "222@s.whatsapp.net".parse().unwrap(); + let carol_jid: Jid = "333@s.whatsapp.net".parse().unwrap(); + let alice = device_with_lid( + "alice-removal-rotation", + &alice_jid.to_string(), + Some("900111:1@lid"), + ) + .await; + let bob = device("bob-removal-rotation", &bob_jid.to_string()).await; + let carol = device("carol-removal-rotation", &carol_jid.to_string()).await; + let alice_paired = common::pair(&alice).await; + for peer in [&bob, &carol] { + peer.trust_account_identity(&alice_jid, alice_paired.account) + .await + .unwrap(); + } + let identity = alice_paired.device_identity.clone(); + let client = ChangingGroupClient::new( + FakeClient { + devices: vec![alice_jid.clone(), bob_jid.clone(), carol_jid.clone()], + bundles: HashMap::from([ + (bob_jid.clone(), bundle_for(&bob).await), + (carol_jid.clone(), bundle_for(&carol).await), + ]), + device_identities: HashMap::new(), + group: Some(Arc::new(GroupRoutingInfo::new( + vec![alice_jid.to_non_ad(), bob_jid.clone(), carol_jid.clone()], + AddressingMode::Pn, + ))), + }, + false, + ); + let first = alice + .send_group(&TokioRuntime, &client, &group, &text("before"), "BEFORE") + .await + .unwrap(); + for (peer, jid) in [(&bob, &bob_jid), (&carol, &carol_jid)] { + receive_group_component( + peer, + &group, + &alice_jid, + &identity, + enc_for(&first.stanza, jid), + ) + .await + .unwrap(); + receive_group_component(peer, &group, &alice_jid, &identity, skmsg_of(&first.stanza)) + .await + .unwrap(); + } + assert!( + alice + .mark_sender_key_distributed( + &group, + &first.distribution_targets, + first.distribution_epoch + ) + .await + .unwrap() + ); + + // Bob asks for a retry first, which clears his distribution mark so a + // later send would redistribute to him. That must not hide that he still + // holds the current key. + alice + .resend( + &client, + wacore_signal_ops::RetryRequest { + chat: &group, + message_id: "BEFORE", + requester: &bob_jid, + encryption_jid: &bob_jid, + route: wacore_signal_ops::RetryRoute::Group { + addressing_mode: AddressingMode::Pn, + }, + retry_count: 1, + bundle: None, + }, + ) + .await + .unwrap(); + + // Bob leaves. Nothing calls forget_sender_key_devices: the next send + // has to notice that a device holding the key is no longer addressed. + { + let mut current = client.current.write().unwrap(); + current.devices = vec![alice_jid.clone(), carol_jid.clone()]; + current.group = Some(Arc::new(GroupRoutingInfo::new( + vec![alice_jid.to_non_ad(), carol_jid.clone()], + AddressingMode::Pn, + ))); + } + let after = alice + .send_group(&TokioRuntime, &client, &group, &text("after"), "AFTER") + .await + .unwrap(); + // The retry already started an epoch of its own, so compare chains: the + // remaining member must be sent a fresh sender key. + assert!( + after.distribution_targets.contains(&carol_jid), + "removing a participant must redistribute a new key to the others" + ); + assert!(pairwise_enc_for(&after.stanza, &bob_jid).is_none()); + assert!( + receive_group_component(&bob, &group, &alice_jid, &identity, skmsg_of(&after.stanza)) + .await + .is_err(), + "a removed participant can still read the group on the old sender key" + ); + receive_group_component( + &carol, + &group, + &alice_jid, + &identity, + enc_for(&after.stanza, &carol_jid), + ) + .await + .unwrap(); + let Received::Message(message) = receive_group_component( + &carol, + &group, + &alice_jid, + &identity, + skmsg_of(&after.stanza), + ) + .await + .unwrap() else { + panic!("the remaining participant did not receive the message") + }; + assert_eq!( + message.content.message.conversation.as_deref(), + Some("after") + ); +} + +/// Alice in a group with Bob and Carol, both holding her first PN sender key. +/// `acknowledge` records the server's ACK of that first send. +async fn group_of_three( + scope: &str, + acknowledge: bool, +) -> ( + SignalOps, + SignalOps, + SignalOps, + ChangingGroupClient, + Vec, + [Jid; 4], +) { + use wacore::types::message::AddressingMode; + let group: Jid = "54322@g.us".parse().unwrap(); + let alice_jid: Jid = "111:1@s.whatsapp.net".parse().unwrap(); + let bob_jid: Jid = "222@s.whatsapp.net".parse().unwrap(); + let carol_jid: Jid = "333@s.whatsapp.net".parse().unwrap(); + let alice = device_with_lid( + &format!("alice-{scope}"), + &alice_jid.to_string(), + Some("900111:1@lid"), + ) + .await; + let bob = device(&format!("bob-{scope}"), &bob_jid.to_string()).await; + let carol = device(&format!("carol-{scope}"), &carol_jid.to_string()).await; + let alice_paired = common::pair(&alice).await; + for peer in [&bob, &carol] { + peer.trust_account_identity(&alice_jid, alice_paired.account) + .await + .unwrap(); + } + let identity = alice_paired.device_identity.clone(); + let client = ChangingGroupClient::new( + FakeClient { + devices: vec![alice_jid.clone(), bob_jid.clone(), carol_jid.clone()], + bundles: HashMap::from([ + (bob_jid.clone(), bundle_for(&bob).await), + (carol_jid.clone(), bundle_for(&carol).await), + ]), + device_identities: HashMap::new(), + group: Some(Arc::new(GroupRoutingInfo::new( + vec![alice_jid.to_non_ad(), bob_jid.clone(), carol_jid.clone()], + AddressingMode::Pn, + ))), + }, + false, + ); + let first = alice + .send_group(&TokioRuntime, &client, &group, &text("before"), "BEFORE") + .await + .unwrap(); + for (peer, jid) in [(&bob, &bob_jid), (&carol, &carol_jid)] { + receive_group_component( + peer, + &group, + &alice_jid, + &identity, + enc_for(&first.stanza, jid), + ) + .await + .unwrap(); + receive_group_component(peer, &group, &alice_jid, &identity, skmsg_of(&first.stanza)) + .await + .unwrap(); + } + if acknowledge { + assert!( + alice + .mark_sender_key_distributed( + &group, + &first.distribution_targets, + first.distribution_epoch + ) + .await + .unwrap() + ); + } + ( + alice, + bob, + carol, + client, + identity, + [group, alice_jid, bob_jid, carol_jid], + ) +} + +/// Leave only Alice and Carol in the group, addressed by `mode`. +fn without_bob( + client: &ChangingGroupClient, + alice: &Jid, + carol: &Jid, + mode: wacore::types::message::AddressingMode, +) { + let mut current = client.current.write().unwrap(); + current.devices = vec![alice.clone(), carol.clone()]; + current.group = Some(Arc::new(GroupRoutingInfo::new( + vec![alice.to_non_ad(), carol.clone()], + mode, + ))); +} + +async fn assert_only_carol_reads( + after: &[u8], + bob: &SignalOps, + carol: &SignalOps, + identity: &[u8], + [group, alice_jid, bob_jid, carol_jid]: &[Jid; 4], +) { + assert!(pairwise_enc_for(after, bob_jid).is_none()); + assert!( + receive_group_component(bob, group, alice_jid, identity, skmsg_of(after)) + .await + .is_err(), + "a removed participant can still read the group on a sender key it holds" + ); + receive_group_component(carol, group, alice_jid, identity, enc_for(after, carol_jid)) + .await + .unwrap(); + let Received::Message(message) = + receive_group_component(carol, group, alice_jid, identity, skmsg_of(after)) + .await + .unwrap() + else { + panic!("the remaining participant did not receive the message") + }; + assert_eq!( + message.content.message.conversation.as_deref(), + Some("after") + ); +} + +#[tokio::test] +async fn a_recipient_whose_acknowledgment_was_lost_is_still_rotated_out() { + // The runner's reply to the first send was lost, so nothing marked it. + let (alice, bob, carol, client, identity, jids) = group_of_three("lost-ack", false).await; + let [group, alice_jid, _, carol_jid] = &jids; + without_bob( + &client, + alice_jid, + carol_jid, + wacore::types::message::AddressingMode::Pn, + ); + let after = alice + .send_group(&TokioRuntime, &client, group, &text("after"), "AFTER") + .await + .unwrap(); + assert_only_carol_reads(&after.stanza, &bob, &carol, &identity, &jids).await; +} + +#[tokio::test] +async fn returning_to_an_earlier_addressing_mode_starts_a_fresh_sender_key() { + use wacore::types::message::AddressingMode; + let (alice, bob, carol, client, identity, jids) = group_of_three("mode-trip", true).await; + let [group, alice_jid, _, carol_jid] = &jids; + // Bob leaves while the group is briefly addressed by LID. Alice is its only + // member then, so that send changes the chain without needing any bundle. + { + let mut current = client.current.write().unwrap(); + current.devices = vec![alice_jid.clone()]; + current.group = Some(Arc::new(GroupRoutingInfo::new( + vec!["900111@lid".parse().unwrap()], + AddressingMode::Lid, + ))); + } + alice + .send_group(&TokioRuntime, &client, group, &text("lid"), "LID") + .await + .unwrap(); + // Back to PN without Bob: the old PN chain he holds must not come back. + without_bob(&client, alice_jid, carol_jid, AddressingMode::Pn); + let after = alice + .send_group(&TokioRuntime, &client, group, &text("after"), "AFTER") + .await + .unwrap(); + assert_only_carol_reads(&after.stanza, &bob, &carol, &identity, &jids).await; +} + +#[tokio::test] +async fn group_encryption_keeps_the_address_selected_before_session_locking() { + use wacore::types::message::AddressingMode; + let group: Jid = "12345@g.us".parse().unwrap(); + let alice_jid: Jid = "111:1@s.whatsapp.net".parse().unwrap(); + let bob_jid: Jid = "222@s.whatsapp.net".parse().unwrap(); + let alice = device_with_lid( + "alice-mapping-race", + &alice_jid.to_string(), + Some("900111:1@lid"), + ) + .await; + let bob = device("bob-mapping-race", &bob_jid.to_string()).await; + let alice_identity = common::vouch(&bob, &alice, "111:1@s.whatsapp.net").await; + let client = ChangingGroupClient::new( + FakeClient { + devices: vec![alice_jid.clone(), bob_jid.clone()], + bundles: HashMap::from([(bob_jid.clone(), bundle_for(&bob).await)]), + device_identities: HashMap::new(), + group: Some(Arc::new(GroupRoutingInfo::new( + vec![alice_jid.to_non_ad(), bob_jid.clone()], + AddressingMode::Pn, + ))), + }, + true, + ); + let sent = alice + .send_group( + &TokioRuntime, + &client, + &group, + &text("frozen address"), + "FROZEN", + ) + .await + .unwrap(); + // A second lookup would change the encryption address to a LID that has + // no fetched bundle and was never included in the acquired lock set. + let carrier = pairwise_enc_for(&sent.stanza, &bob_jid) + .expect("the frozen PN session must deliver the sender key"); + receive_group_component(&bob, &group, &alice_jid, &alice_identity, carrier) + .await + .unwrap(); + let Received::Message(received) = receive_group_component( + &bob, + &group, + &alice_jid, + &alice_identity, + skmsg_of(&sent.stanza), + ) + .await + .unwrap() else { + panic!("expected the group payload") + }; + assert_eq!( + received.content.message.conversation.as_deref(), + Some("frozen address") + ); + assert_eq!( + client + .mapping_reads + .load(std::sync::atomic::Ordering::SeqCst), + 1 + ); +} + +/// The first `` anywhere in the stanza. +fn first_enc(stanza: &[u8]) -> (String, u8, Vec) { + fn find(node: &wacore_binary::Node) -> Option<&wacore_binary::Node> { + if node.tag == "enc" { + return Some(node); + } + node.children()?.iter().find_map(find) + } + enc_parts(find(&parse(stanza)).expect("enc")) +} + +fn remote_bundle_from( + bundle: &PreKeyBundle, + device_identity: Option>, +) -> wacore_signal_ops::RemoteBundle { + let key = |k: &PublicKey| -> [u8; 32] { *k.public_key_bytes() }; + wacore_signal_ops::RemoteBundle { + registration_id: bundle.registration_id().unwrap(), + device_id: bundle.device_id().unwrap().into(), + identity_key: key(bundle.identity_key().unwrap().public_key()), + signed_prekey_id: bundle.signed_pre_key_id().unwrap().into(), + signed_prekey: key(bundle.signed_pre_key_public().unwrap()), + signed_prekey_signature: bundle + .signed_pre_key_signature() + .unwrap() + .try_into() + .unwrap(), + prekey: bundle + .pre_key_id() + .unwrap() + .map(|id| (id.into(), key(bundle.pre_key_public().unwrap().unwrap()))), + device_identity, + } +} + +/// Decrypt the first `` of a stanza from `from` and return its text. +async fn deliver_message( + ops: &SignalOps, + from: &Jid, + from_identity: &[u8], + stanza: Vec, +) -> wa::Message { + let (kind, version, ciphertext) = first_enc(&stanza); + // A prekey message must carry the identity the receiver verifies. + if kind == "pkmsg" { + assert_eq!( + stanza_device_identity(&stanza).as_deref(), + Some(from_identity), + "the stanza carries a different device identity" + ); + } + let sender = from.to_protocol_address(); + let chat = from.to_non_ad().to_string(); + let received = ops + .receive(ReceiveRequest { + chat: &chat, + sender: &sender, + kind: if kind == "pkmsg" { + ReceiveKind::PreKey + } else { + ReceiveKind::Message + }, + ciphertext: &ciphertext, + padding_version: version, + is_from_me: false, + device_identity: (kind == "pkmsg").then_some(from_identity), + }) + .await + .unwrap(); + let Received::Message(message) = received else { + panic!("expected a message") + }; + message.content.message +} + +async fn deliver( + ops: &SignalOps, + from: &Jid, + from_identity: &[u8], + stanza: Vec, +) -> Option { + deliver_message(ops, from, from_identity, stanza) + .await + .conversation +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_retry_receipt_gets_the_message_re_encrypted() { + use wacore_signal_ops::{RetryRequest, RetryRoute}; + + let alice_jid: Jid = "111:1@s.whatsapp.net".parse().unwrap(); + let bob_jid: Jid = "222:1@s.whatsapp.net".parse().unwrap(); + let alice = device("alice-retry", "111:1@s.whatsapp.net").await; + // A retry pkmsg must carry the device identity, which pairing provides. + // Alice is paired once, and Bob pins that account. + let alice_paired = common::pair(&alice).await; + let bob = device("bob-retry", "222:1@s.whatsapp.net").await; + let bob_paired = common::pair(&bob).await; + alice + .trust_account_identity(&bob_jid, bob_paired.account) + .await + .unwrap(); + bob.trust_account_identity(&alice_jid, alice_paired.account) + .await + .unwrap(); + let alice_identity = alice_paired.device_identity.clone(); + let client = FakeClient { + devices: vec![bob_jid.clone()], + bundles: HashMap::from([(bob_jid.clone(), bundle_for(&bob).await)]), + device_identities: HashMap::from([(bob_jid.clone(), bob_paired.device_identity.clone())]), + group: None, + }; + let to = bob_jid.to_non_ad(); + // Bob never processes the original. + alice + .send_direct(&TokioRuntime, &client, &to, &text("lost once"), "R1") + .await + .unwrap(); + + let retried = alice + .resend( + &client, + RetryRequest { + chat: &to, + message_id: "R1", + requester: &bob_jid, + encryption_jid: &bob_jid, + route: RetryRoute::Direct { recipient: None }, + retry_count: 1, + bundle: None, + }, + ) + .await + .unwrap(); + let saved = alice.sent_message(&to, "R1").await.unwrap().unwrap(); + let secret = saved + .message_context_info + .as_option() + .and_then(|context| context.message_secret.as_ref()) + .expect("the original send generated a message secret"); + assert_eq!(secret.len(), 32); + let delivered = deliver_message(&bob, &alice_jid, &alice_identity, retried).await; + assert_eq!(delivered.conversation.as_deref(), Some("lost once")); + assert_eq!( + delivered + .message_context_info + .as_option() + .and_then(|context| context.message_secret.as_ref()), + Some(secret), + "retry encryption must preserve the original generated secret" + ); + + // Bob's device was reset: new keys arrive with the next retry receipt. + let fresh_bob = device("bob-retry-fresh", "222:1@s.whatsapp.net").await; + let alice_identity_fresh = common::vouch(&fresh_bob, &alice, "111:1@s.whatsapp.net").await; + // A reset device is re-paired by the same primary, under the same account. + let fresh_identity = common::pair_under(&fresh_bob, &bob_paired.account_keys).await; + let fresh_bundle = remote_bundle_from(&bundle_for(&fresh_bob).await, Some(fresh_identity)); + let retried = alice + .resend( + &client, + RetryRequest { + chat: &to, + message_id: "R1", + requester: &bob_jid, + encryption_jid: &bob_jid, + route: RetryRoute::Direct { recipient: None }, + retry_count: 2, + bundle: Some(&fresh_bundle), + }, + ) + .await + .unwrap(); + assert_eq!( + deliver(&fresh_bob, &alice_jid, &alice_identity_fresh, retried) + .await + .as_deref(), + Some("lost once") + ); + + let unknown = alice + .resend( + &client, + RetryRequest { + chat: &to, + message_id: "NOPE", + requester: &bob_jid, + encryption_jid: &bob_jid, + route: RetryRoute::Direct { recipient: None }, + retry_count: 1, + bundle: None, + }, + ) + .await; + assert!(unknown.is_err()); +} + +/// Holds every prekey fetch until released, like a slow peer lookup. +struct SlowClient { + inner: FakeClient, + gate: Arc, + /// Signalled once a fetch is held at the gate. + entered: Arc, +} + +#[async_trait] +impl SendContextResolver for SlowClient { + async fn lock_device_sessions(&self, _device_jids: &[Jid]) -> SessionLockGuard { + SessionLockGuard::none() + } + + async fn resolve_devices(&self, jids: &[Jid]) -> Result, anyhow::Error> { + self.inner.resolve_devices(jids).await + } + + async fn fetch_prekeys( + &self, + jids: &[Jid], + ) -> Result, anyhow::Error> { + self.gate.notified().await; + self.inner.fetch_prekeys(jids).await + } + + async fn fetch_prekeys_for_identity_check( + &self, + jids: &[Jid], + ) -> Result { + Ok(PreKeyFetchOutcome { + bundles: self.fetch_prekeys(jids).await?, + rejected: Vec::new(), + }) + } + + async fn resolve_group_routing_info( + &self, + jid: &Jid, + ) -> Result, anyhow::Error> { + self.inner.resolve_group_routing_info(jid).await + } +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_slow_send_does_not_block_receiving_from_another_peer() { + let alice_jid: Jid = "111:1@s.whatsapp.net".parse().unwrap(); + let carol_jid: Jid = "333:1@s.whatsapp.net".parse().unwrap(); + let dave_jid: Jid = "444:1@s.whatsapp.net".parse().unwrap(); + let alice = device("alice-slow", "111:1@s.whatsapp.net").await; + let carol = device("carol-slow", "333:1@s.whatsapp.net").await; + let dave = Arc::new(device("dave-slow", "444:1@s.whatsapp.net").await); + let alice_identity = common::vouch(&dave, &alice, "111:1@s.whatsapp.net").await; + + let gate = Arc::new(tokio::sync::Notify::new()); + let entered = Arc::new(tokio::sync::Notify::new()); + let slow = Arc::new(SlowClient { + inner: FakeClient { + devices: vec![carol_jid.clone()], + bundles: HashMap::from([(carol_jid.clone(), bundle_for(&carol).await)]), + device_identities: HashMap::from([vouch(&dave, &carol, &carol_jid).await]), + group: None, + }, + gate: gate.clone(), + entered: entered.clone(), + }); + let send = { + let dave = dave.clone(); + let slow = slow.clone(); + let to = carol_jid.to_non_ad(); + tokio::spawn(async move { + dave.send_direct(&TokioRuntime, slow.as_ref(), &to, &text("to carol"), "S1") + .await + }) + }; + + // Dave's send is now held inside the fetch, so the receive below only + // passes if it really overlaps a blocked send. + tokio::time::timeout(Duration::from_secs(5), entered.notified()) + .await + .expect("the send never reached its prekey fetch"); + assert!(!send.is_finished(), "the send must still be blocked"); + + // Alice writes to Dave while Dave's send is stuck fetching Carol's keys. + let to_dave = FakeClient { + devices: vec![dave_jid.clone()], + bundles: HashMap::from([(dave_jid.clone(), bundle_for(&dave).await)]), + device_identities: HashMap::from([vouch(&alice, &dave, &dave_jid).await]), + group: None, + }; + let sent = alice + .send_direct( + &TokioRuntime, + &to_dave, + &dave_jid.to_non_ad(), + &text("to dave"), + "A1", + ) + .await + .unwrap(); + let received = tokio::time::timeout( + Duration::from_secs(5), + deliver(&dave, &alice_jid, &alice_identity, sent.stanza), + ) + .await + .expect("receive was blocked by an unrelated send"); + assert_eq!(received.as_deref(), Some("to dave")); + + gate.notify_one(); + send.await.unwrap().unwrap(); +} + +#[tokio::test] +async fn a_corrupt_record_is_pruned_instead_of_stopping_the_prune() { + let records = Arc::new(MemoryRecordStore::new()); + let lease = records + .acquire("prune-corrupt", "node-a", 0, 60_000) + .await + .unwrap() + .unwrap(); + let store = Arc::new(RecordSignalStore::new( + records, + Arc::new(AesGcmSealer::new(&[5; 32])), + lease.fence, + )); + let ops = SignalOps::create(store.clone()).await.unwrap(); + use wacore_recordstore::Namespace; + store + .put_aux(Namespace::SentMessage, "corrupt", b"x") + .await + .unwrap(); + store + .put_aux(Namespace::DecryptBuffer, "corrupt", b"x") + .await + .unwrap(); + // Cutoff 0 keeps every decodable record, so only the corrupt ones go. + assert_eq!(ops.prune_sent_messages(0).await.unwrap(), 1); + assert_eq!(ops.prune_decrypt_buffer(0).await.unwrap(), 1); + assert!( + store + .load_aux(Namespace::SentMessage, "corrupt") + .await + .unwrap() + .is_none() + ); + assert!( + store + .load_aux(Namespace::DecryptBuffer, "corrupt") + .await + .unwrap() + .is_none() + ); +} + +#[async_trait] +impl SignalResolver for SlowClient { + async fn fetch_verifiable_prekeys(&self, jids: &[Jid]) -> anyhow::Result { + self.entered.notify_one(); + self.gate.notified().await; + self.inner.fetch_verifiable_prekeys(jids).await + } +} + +/// Maps phone user `222` to LID `900222` and holds no prekey bundles, so a +/// send can only reach that user through a session that already exists. +struct MappedClient { + inner: FakeClient, +} + +#[async_trait] +impl SendContextResolver for MappedClient { + async fn lock_device_sessions(&self, _device_jids: &[Jid]) -> SessionLockGuard { + SessionLockGuard::none() + } + + async fn resolve_devices(&self, jids: &[Jid]) -> anyhow::Result> { + self.inner.resolve_devices(jids).await + } + + async fn fetch_prekeys(&self, jids: &[Jid]) -> anyhow::Result> { + self.inner.fetch_prekeys(jids).await + } + + async fn fetch_prekeys_for_identity_check( + &self, + jids: &[Jid], + ) -> anyhow::Result { + self.inner.fetch_prekeys_for_identity_check(jids).await + } + + async fn resolve_group_routing_info(&self, jid: &Jid) -> anyhow::Result> { + self.inner.resolve_group_routing_info(jid).await + } + + async fn get_lid_for_phone(&self, user: &str) -> Option { + (user == "222").then(|| "900222".into()) + } +} + +#[async_trait] +impl SignalResolver for MappedClient { + async fn fetch_verifiable_prekeys(&self, jids: &[Jid]) -> anyhow::Result { + self.inner.fetch_verifiable_prekeys(jids).await + } +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_group_send_reuses_an_older_pn_session_when_the_mapped_lid_has_none() { + use wacore::types::message::AddressingMode; + let group: Jid = "120363000000002@g.us".parse().unwrap(); + let alice_jid: Jid = "111:1@s.whatsapp.net".parse().unwrap(); + let bob_jid: Jid = "222@s.whatsapp.net".parse().unwrap(); + let alice = device_with_lid( + "alice-pn-fallback", + "111:1@s.whatsapp.net", + Some("900111:1@lid"), + ) + .await; + let bob = device("bob-pn-fallback", "222@s.whatsapp.net").await; + let alice_identity = common::vouch(&bob, &alice, "111:1@s.whatsapp.net").await; + + // A PN session from before the LID mapping was known. + let dm_client = FakeClient { + devices: vec![bob_jid.clone()], + bundles: HashMap::from([(bob_jid.clone(), bundle_for(&bob).await)]), + device_identities: HashMap::new(), + group: None, + }; + let dm = alice + .send_direct( + &TokioRuntime, + &dm_client, + &bob_jid.to_non_ad(), + &text("before"), + "DM1", + ) + .await + .unwrap(); + let (kind, version, ciphertext) = enc_for(&dm.stanza, &bob_jid); + let dm_chat = alice_jid.to_non_ad().to_string(); + receive_group_component( + &bob, + &dm_chat.parse().unwrap(), + &alice_jid, + &alice_identity, + (kind, version, ciphertext), + ) + .await + .unwrap(); + + // The frozen map now points bob at a LID with no session and no bundle. + // The send must still deliver the sender key over the PN session, which + // `SignalOps` locks together with the mapped LID address. + let client = MappedClient { + inner: FakeClient { + devices: vec![alice_jid.clone(), bob_jid.clone()], + bundles: HashMap::new(), + device_identities: HashMap::new(), + group: Some(Arc::new(GroupRoutingInfo::new( + vec![alice_jid.to_non_ad(), bob_jid.clone()], + AddressingMode::Pn, + ))), + }, + }; + let sent = alice + .send_group(&TokioRuntime, &client, &group, &text("after mapping"), "G1") + .await + .unwrap(); + assert_eq!(sent.distribution_targets, vec![bob_jid.clone()]); + let carrier = pairwise_enc_for(&sent.stanza, &bob_jid).expect("the PN session carries the key"); + receive_group_component(&bob, &group, &alice_jid, &alice_identity, carrier) + .await + .unwrap(); + let Received::Message(received) = receive_group_component( + &bob, + &group, + &alice_jid, + &alice_identity, + skmsg_of(&sent.stanza), + ) + .await + .unwrap() else { + panic!("expected the group payload") + }; + assert_eq!( + received.content.message.conversation.as_deref(), + Some("after mapping") + ); +} + +#[tokio::test(flavor = "multi_thread")] +async fn an_acknowledgment_from_before_a_rotation_does_not_mark_the_new_key() { + use wacore::types::message::AddressingMode; + + let group: Jid = "120363000000003@g.us".parse().unwrap(); + let alice_jid: Jid = "111:1@s.whatsapp.net".parse().unwrap(); + let bob_jid: Jid = "222@s.whatsapp.net".parse().unwrap(); + let alice = device_with_lid( + "alice-stale-ack", + "111:1@s.whatsapp.net", + Some("900111:1@lid"), + ) + .await; + let bob = device("bob-stale-ack", "222@s.whatsapp.net").await; + let client = FakeClient { + devices: vec![alice_jid.clone(), bob_jid.clone()], + bundles: HashMap::from([(bob_jid.clone(), bundle_for(&bob).await)]), + device_identities: HashMap::new(), + group: Some(Arc::new(GroupRoutingInfo::new( + vec![alice_jid.to_non_ad(), bob_jid.clone()], + AddressingMode::Pn, + ))), + }; + let send = |text_body: &'static str, id: &'static str| { + let (alice, client, group) = (&alice, &client, &group); + async move { + alice + .send_group(&TokioRuntime, client, group, &text(text_body), id) + .await + .unwrap() + } + }; + + let first = send("before rotation", "S1").await; + assert_eq!(first.distribution_targets, vec![bob_jid.clone()]); + alice.forget_sender_key_devices(&group).await.unwrap(); + // The first send with the replacement key is never acknowledged. + let lost = send("lost", "S2").await; + assert_eq!(lost.distribution_targets, vec![bob_jid.clone()]); + assert_ne!(lost.distribution_epoch, first.distribution_epoch); + + // The old send's acknowledgment arrives late and must record nothing. + assert!( + !alice + .mark_sender_key_distributed( + &group, + &first.distribution_targets, + first.distribution_epoch + ) + .await + .unwrap() + ); + let next = send("after the late ack", "S3").await; + assert_eq!( + next.distribution_targets, + vec![bob_jid.clone()], + "bob never acknowledged the replacement key" + ); + assert!( + alice + .mark_sender_key_distributed( + &group, + &next.distribution_targets, + next.distribution_epoch + ) + .await + .unwrap() + ); + assert!(send("warm", "S4").await.distribution_targets.is_empty()); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_retry_for_a_device_outside_the_conversation_is_refused() { + use wacore::types::message::AddressingMode; + use wacore_signal_ops::{OpsError, RetryRequest, RetryRoute}; + + let bob_jid: Jid = "222:1@s.whatsapp.net".parse().unwrap(); + let eve_jid: Jid = "333:1@s.whatsapp.net".parse().unwrap(); + let alice = device_with_lid( + "alice-retry-check", + "111:1@s.whatsapp.net", + Some("900111:1@lid"), + ) + .await; + common::pair(&alice).await; + let bob = device("bob-retry-check", "222:1@s.whatsapp.net").await; + let eve = device("eve-retry-check", "333:1@s.whatsapp.net").await; + let eve_bundle = remote_bundle_from(&bundle_for(&eve).await, None); + let group: Jid = "120363000000004@g.us".parse().unwrap(); + let client = FakeClient { + devices: vec![bob_jid.clone()], + bundles: HashMap::from([(bob_jid.clone(), bundle_for(&bob).await)]), + device_identities: HashMap::from([vouch(&alice, &bob, &bob_jid).await]), + group: Some(Arc::new(GroupRoutingInfo::new( + vec!["111@s.whatsapp.net".parse().unwrap(), bob_jid.to_non_ad()], + AddressingMode::Pn, + ))), + }; + let to = bob_jid.to_non_ad(); + alice + .send_direct(&TokioRuntime, &client, &to, &text("for bob only"), "D1") + .await + .unwrap(); + alice + .send_group(&TokioRuntime, &client, &group, &text("members only"), "G1") + .await + .unwrap(); + + let refused = |result: Result, OpsError>| { + assert!( + matches!(result, Err(OpsError::InvalidInput(_))), + "expected a refused retry, got {result:?}" + ); + }; + // A stranger asks for the direct message with its own keys. + refused( + alice + .resend( + &client, + RetryRequest { + chat: &to, + message_id: "D1", + requester: &eve_jid, + encryption_jid: &eve_jid, + route: RetryRoute::Direct { recipient: None }, + retry_count: 1, + bundle: Some(&eve_bundle), + }, + ) + .await, + ); + // The real recipient is named, but the encryption address is someone else's. + refused( + alice + .resend( + &client, + RetryRequest { + chat: &to, + message_id: "D1", + requester: &bob_jid, + encryption_jid: &eve_jid, + route: RetryRoute::Direct { recipient: None }, + retry_count: 1, + bundle: Some(&eve_bundle), + }, + ) + .await, + ); + // A non-member asks for the group message. + refused( + alice + .resend( + &client, + RetryRequest { + chat: &group, + message_id: "G1", + requester: &eve_jid, + encryption_jid: &eve_jid, + route: RetryRoute::Group { + addressing_mode: AddressingMode::Pn, + }, + retry_count: 1, + bundle: Some(&eve_bundle), + }, + ) + .await, + ); + // Nothing was written for the stranger. + assert!( + !alice + .has_session(&eve_jid.to_protocol_address()) + .await + .unwrap() + ); + + // Bob asking from his LID is the same account as the PN chat. + let bob_lid: Jid = "900222:1@lid".parse().unwrap(); + // The LID form of the same account carries Bob's own device identity. + let bob_paired = common::pair(&bob).await; + alice + .trust_account_identity(&bob_lid, bob_paired.account) + .await + .unwrap(); + let bob_lid_bundle = + remote_bundle_from(&bundle_for(&bob).await, Some(bob_paired.device_identity)); + let mapped = MappedClient { inner: client }; + let retried = alice + .resend( + &mapped, + RetryRequest { + chat: &to, + message_id: "D1", + requester: &bob_lid, + encryption_jid: &bob_lid, + route: RetryRoute::Direct { recipient: None }, + retry_count: 1, + bundle: Some(&bob_lid_bundle), + }, + ) + .await; + assert!(retried.is_ok(), "the recipient's LID form is accepted"); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_group_retry_is_answered_only_for_members_the_message_went_to() { + use wacore::types::message::AddressingMode; + use wacore_signal_ops::{OpsError, RetryRequest, RetryRoute}; + + let alice_member: Jid = "111@s.whatsapp.net".parse().unwrap(); + let bob_jid: Jid = "222:1@s.whatsapp.net".parse().unwrap(); + let carol_jid: Jid = "444:1@s.whatsapp.net".parse().unwrap(); + let eve_jid: Jid = "333:1@s.whatsapp.net".parse().unwrap(); + let alice = device_with_lid( + "alice-audience", + "111:1@s.whatsapp.net", + Some("900111:1@lid"), + ) + .await; + common::pair(&alice).await; + let bob = device("bob-audience", "222:1@s.whatsapp.net").await; + let carol = device("carol-audience", "444:1@s.whatsapp.net").await; + let eve = device("eve-audience", "333:1@s.whatsapp.net").await; + let group: Jid = "120363000000005@g.us".parse().unwrap(); + let identities = HashMap::from([ + vouch(&alice, &bob, &bob_jid).await, + vouch(&alice, &carol, &carol_jid).await, + ]); + let client_for = |members: Vec, bundles: HashMap| FakeClient { + devices: vec![bob_jid.clone(), carol_jid.clone()], + bundles, + device_identities: identities.clone(), + group: Some(Arc::new(GroupRoutingInfo::new(members, AddressingMode::Pn))), + }; + let bundles = HashMap::from([ + (bob_jid.clone(), bundle_for(&bob).await), + (carol_jid.clone(), bundle_for(&carol).await), + ]); + let before = client_for( + vec![ + alice_member.clone(), + bob_jid.to_non_ad(), + carol_jid.to_non_ad(), + ], + bundles, + ); + alice + .send_group(&TokioRuntime, &before, &group, &text("before eve"), "G1") + .await + .unwrap(); + + // Eve joined and Bob left after the send. + let after = client_for( + vec![alice_member, carol_jid.to_non_ad(), eve_jid.to_non_ad()], + HashMap::new(), + ); + fn retry<'a>( + group: &'a Jid, + requester: &'a Jid, + bundle: Option<&'a wacore_signal_ops::RemoteBundle>, + ) -> RetryRequest<'a> { + RetryRequest { + chat: group, + message_id: "G1", + requester, + encryption_jid: requester, + route: RetryRoute::Group { + addressing_mode: AddressingMode::Pn, + }, + retry_count: 1, + bundle, + } + } + let eve_bundle = remote_bundle_from(&bundle_for(&eve).await, None); + let bob_bundle = remote_bundle_from(&bundle_for(&bob).await, None); + assert!( + matches!( + alice + .resend(&after, retry(&group, &eve_jid, Some(&eve_bundle))) + .await, + Err(OpsError::InvalidInput(_)) + ), + "a member who joined later gets no earlier message" + ); + assert!( + matches!( + alice + .resend(&after, retry(&group, &bob_jid, Some(&bob_bundle))) + .await, + Err(OpsError::InvalidInput(_)) + ), + "a removed member gets nothing more" + ); + let retried = alice.resend(&after, retry(&group, &carol_jid, None)).await; + assert!( + retried.is_ok(), + "a current recipient is answered: {retried:?}" + ); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_retry_whose_route_does_not_match_its_chat_is_refused() { + use wacore::types::message::AddressingMode; + use wacore_signal_ops::{OpsError, RetryRequest, RetryRoute}; + + let bob_jid: Jid = "222:1@s.whatsapp.net".parse().unwrap(); + let alice = device_with_lid( + "alice-route-check", + "111:1@s.whatsapp.net", + Some("900111:1@lid"), + ) + .await; + common::pair(&alice).await; + let bob = device("bob-route-check", "222:1@s.whatsapp.net").await; + let group: Jid = "120363000000006@g.us".parse().unwrap(); + let client = FakeClient { + devices: vec![bob_jid.clone()], + bundles: HashMap::from([(bob_jid.clone(), bundle_for(&bob).await)]), + device_identities: HashMap::from([vouch(&alice, &bob, &bob_jid).await]), + group: Some(Arc::new(GroupRoutingInfo::new( + vec!["111@s.whatsapp.net".parse().unwrap(), bob_jid.to_non_ad()], + AddressingMode::Pn, + ))), + }; + let to = bob_jid.to_non_ad(); + alice + .send_direct(&TokioRuntime, &client, &to, &text("direct"), "D1") + .await + .unwrap(); + alice + .send_group(&TokioRuntime, &client, &group, &text("group"), "G1") + .await + .unwrap(); + + let retry = |chat: &Jid, message_id: &'static str, route: RetryRoute| { + let (alice, client, bob_jid) = (&alice, &client, &bob_jid); + let chat = chat.clone(); + async move { + alice + .resend( + client, + RetryRequest { + chat: &chat, + message_id, + requester: bob_jid, + encryption_jid: bob_jid, + route, + retry_count: 1, + bundle: None, + }, + ) + .await + } + }; + let direct = || RetryRoute::Direct { recipient: None }; + let in_group = || RetryRoute::Group { + addressing_mode: AddressingMode::Pn, + }; + + // A saved group message cannot be re-sent through the direct route. + let result = retry(&group, "G1", direct()).await; + assert!( + matches!(result, Err(OpsError::InvalidInput(_))), + "expected a refused retry, got {result:?}" + ); + // A saved direct message cannot be re-sent through the group route. + let result = retry(&to, "D1", in_group()).await; + assert!( + matches!(result, Err(OpsError::InvalidInput(_))), + "expected a refused retry, got {result:?}" + ); + // A broadcast-list route does not fit either chat. + let result = retry(&to, "D1", RetryRoute::BroadcastList).await; + assert!( + matches!(result, Err(OpsError::InvalidInput(_))), + "expected a refused retry, got {result:?}" + ); + + // The matching routes still work. + assert!(retry(&to, "D1", direct()).await.is_ok()); + assert!(retry(&group, "G1", in_group()).await.is_ok()); + + // A peer's direct retry names no recipient: the chat is theirs. + let result = retry( + &to, + "D1", + RetryRoute::Direct { + recipient: Some("333@s.whatsapp.net".parse().unwrap()), + }, + ) + .await; + assert!( + matches!(&result, Err(OpsError::InvalidInput(m)) if m.contains("recipient")), + "expected a refused recipient, got {result:?}" + ); + + // An own device's direct retry must name the saved chat's user. + let own_device: Jid = "111:2@s.whatsapp.net".parse().unwrap(); + let own_retry = |recipient: Option| { + let (alice, client, to, own_device) = (&alice, &client, &to, &own_device); + async move { + alice + .resend( + client, + RetryRequest { + chat: to, + message_id: "D1", + requester: own_device, + encryption_jid: own_device, + route: RetryRoute::Direct { recipient }, + retry_count: 1, + bundle: None, + }, + ) + .await + } + }; + for wrong in [None, Some("333@s.whatsapp.net".parse().unwrap())] { + let result = own_retry(wrong).await; + assert!( + matches!(&result, Err(OpsError::InvalidInput(m)) if m.contains("recipient")), + "expected a refused recipient, got {result:?}" + ); + } + // The right recipient passes the check; this client has no keys for the + // own device, so the retry then stops at the prekey fetch instead. + let result = own_retry(Some(to.clone())).await; + assert!( + !matches!(&result, Err(OpsError::InvalidInput(m)) if m.contains("recipient")), + "the saved chat's user is accepted, got {result:?}" + ); +} + +#[tokio::test] +async fn acknowledgments_for_one_addressing_chain_do_not_skip_the_other() { + use wacore::types::message::AddressingMode; + let group: Jid = "120363000000031@g.us".parse().unwrap(); + let alice_jid: Jid = "111:1@s.whatsapp.net".parse().unwrap(); + // Bob has no known LID, so his resolved JID is the same in both modes. + let bob_jid: Jid = "222@s.whatsapp.net".parse().unwrap(); + let alice = device_with_lid( + "alice-chain-switch", + &alice_jid.to_string(), + Some("900111:1@lid"), + ) + .await; + common::pair(&alice).await; + let bob = device("bob-chain-switch", &bob_jid.to_string()).await; + let client = |mode| FakeClient { + devices: vec![alice_jid.clone(), bob_jid.clone()], + bundles: HashMap::new(), + device_identities: HashMap::new(), + group: Some(Arc::new(GroupRoutingInfo::new( + vec![alice_jid.to_non_ad(), bob_jid.clone()], + mode, + ))), + }; + let mut pn = client(AddressingMode::Pn); + pn.bundles.insert(bob_jid.clone(), bundle_for(&bob).await); + + // A phone-number send, acknowledged: Bob holds the PN chain. + let first = alice + .send_group(&TokioRuntime, &pn, &group, &text("pn"), "C1") + .await + .unwrap(); + assert_eq!(first.distribution_targets, vec![bob_jid.clone()]); + assert!( + alice + .mark_sender_key_distributed( + &group, + &first.distribution_targets, + first.distribution_epoch + ) + .await + .unwrap() + ); + + // The group switches to LID addressing. The first LID send carries the + // new chain, but its stanza is lost and never acknowledged. + let lid = client(AddressingMode::Lid); + let lost = alice + .send_group(&TokioRuntime, &lid, &group, &text("lid lost"), "C2") + .await + .unwrap(); + assert!(lost.distribution_targets.contains(&bob_jid)); + // A late acknowledgment of the PN send cannot mark the LID chain. + assert!( + !alice + .mark_sender_key_distributed( + &group, + &first.distribution_targets, + first.distribution_epoch + ) + .await + .unwrap() + ); + let next = alice + .send_group(&TokioRuntime, &lid, &group, &text("lid again"), "C3") + .await + .unwrap(); + assert!( + next.distribution_targets.contains(&bob_jid), + "Bob never received the LID chain, so it is sent again: {:?}", + next.distribution_targets + ); +} diff --git a/crates/signal-ops/tests/trust.rs b/crates/signal-ops/tests/trust.rs new file mode 100644 index 0000000..88c63b5 --- /dev/null +++ b/crates/signal-ops/tests/trust.rs @@ -0,0 +1,1240 @@ +// SPDX-License-Identifier: MIT + +//! Peer prekey bundles must be bound to the peer account before a session is +//! built from them, whether they come from a send's fetch, a retry receipt +//! or `establish_session`. + +use async_trait::async_trait; +use std::collections::HashMap; +use std::future::Future; +use std::pin::Pin; +use std::sync::Arc; +use std::time::Duration; +use wacore::client::context::{GroupRoutingInfo, SendContextResolver, SessionLockGuard}; +use wacore::libsignal::protocol::{DeviceId, IdentityKey, KeyPair, PreKeyBundle, PublicKey}; +use wacore::prekeys::PreKeyFetchOutcome; +use wacore::runtime::{AbortHandle, Runtime}; +use wacore::types::jid::JidExt as _; +use wacore_binary::{Jid, NodeContent}; +use wacore_recordstore::{AesGcmSealer, LeaseStore, MemoryRecordStore, RecordSignalStore}; +use wacore_signal_ops::{ + BundleRejection, FetchedBundle, OpsError, ReceiveKind, ReceiveRequest, Received, RemoteBundle, + RetryRequest, RetryRoute, SignalOps, SignalResolver, VerifiablePrekeys, +}; +use waproto::whatsapp as wa; + +mod common; +mod runtime; +use runtime::InlineRuntime; + +type Store = RecordSignalStore; +type BoxFuture = Pin + Send + 'static>>; + +struct TokioRuntime; + +impl Runtime for TokioRuntime { + fn spawn(&self, future: BoxFuture) -> AbortHandle { + let task = tokio::spawn(future); + AbortHandle::new(move || task.abort()) + } + + fn sleep(&self, duration: Duration) -> Pin + Send>> { + Box::pin(tokio::time::sleep(duration)) + } + + fn spawn_blocking(&self, f: Box) -> BoxFuture { + Box::pin(async move { + let _ = tokio::task::spawn_blocking(f).await; + }) + } + + fn yield_now(&self) -> Option + Send>>> { + Some(Box::pin(tokio::task::yield_now())) + } +} + +async fn device(scope: &str, own_pn: &str) -> SignalOps { + let records = Arc::new(MemoryRecordStore::new()); + let lease = records + .acquire(scope, "node-a", 0, 60_000) + .await + .unwrap() + .unwrap(); + let store = Arc::new(RecordSignalStore::new( + records, + Arc::new(AesGcmSealer::new(&[5; 32])), + lease.fence, + )); + let ops = SignalOps::create(store).await.unwrap(); + ops.set_own_jids(own_pn, None).await.unwrap(); + ops +} + +/// `ops`'s bundle as the server hands it out, for device `device_id`. +async fn bundle_for(ops: &SignalOps, device_id: u32) -> PreKeyBundle { + let identity = ops.public_identity().await; + let prekey = ops + .generate_prekeys(&InlineRuntime, 1) + .await + .unwrap() + .remove(0); + PreKeyBundle::new( + identity.registration_id, + DeviceId::from(device_id), + Some(( + prekey.id.into(), + PublicKey::from_djb_public_key_bytes(&prekey.public_key).unwrap(), + )), + identity.signed_prekey.id.into(), + PublicKey::from_djb_public_key_bytes(&identity.signed_prekey.public_key).unwrap(), + identity.signed_prekey.signature, + IdentityKey::new(PublicKey::from_djb_public_key_bytes(&identity.identity_key).unwrap()), + ) + .unwrap() +} + +fn remote(bundle: &PreKeyBundle, device_identity: Option>) -> RemoteBundle { + let key = |k: &PublicKey| -> [u8; 32] { *k.public_key_bytes() }; + RemoteBundle { + registration_id: bundle.registration_id().unwrap(), + device_id: bundle.device_id().unwrap().into(), + identity_key: key(bundle.identity_key().unwrap().public_key()), + signed_prekey_id: bundle.signed_pre_key_id().unwrap().into(), + signed_prekey: key(bundle.signed_pre_key_public().unwrap()), + signed_prekey_signature: bundle + .signed_pre_key_signature() + .unwrap() + .try_into() + .unwrap(), + prekey: bundle + .pre_key_id() + .unwrap() + .map(|id| (id.into(), key(bundle.pre_key_public().unwrap().unwrap()))), + device_identity, + } +} + +/// A primary device's bundle: its identity is the account key itself. +fn primary_bundle(account: &KeyPair) -> PreKeyBundle { + let mut rng = rand::make_rng::(); + let signed = KeyPair::generate(&mut rng); + let signature = account + .private_key + .calculate_signature(&signed.public_key.serialize(), &mut rng) + .unwrap(); + PreKeyBundle::new( + 7, + DeviceId::from(0), + None, + 1.into(), + signed.public_key, + signature.to_vec(), + IdentityKey::new(account.public_key), + ) + .unwrap() +} + +/// A client that answers with fixed devices and bundles, keeping each +/// companion's device identity. +struct Client { + devices: Vec, + bundles: HashMap, + group: Option>, + /// When set, a fetch signals `entered` and waits for `release`. + gate: Option<(Arc, Arc)>, +} + +#[async_trait] +impl SendContextResolver for Client { + async fn lock_device_sessions(&self, _device_jids: &[Jid]) -> SessionLockGuard { + SessionLockGuard::none() + } + + async fn resolve_devices(&self, _jids: &[Jid]) -> anyhow::Result> { + Ok(self.devices.clone()) + } + + async fn fetch_prekeys(&self, _jids: &[Jid]) -> anyhow::Result> { + anyhow::bail!("session setup must use the verifiable fetch") + } + + async fn fetch_prekeys_for_identity_check( + &self, + _jids: &[Jid], + ) -> anyhow::Result { + anyhow::bail!("session setup must use the verifiable fetch") + } + + async fn resolve_group_routing_info( + &self, + _jid: &Jid, + ) -> anyhow::Result> { + self.group + .clone() + .ok_or_else(|| anyhow::anyhow!("no group here")) + } +} + +#[async_trait] +impl SignalResolver for Client { + async fn fetch_verifiable_prekeys(&self, jids: &[Jid]) -> anyhow::Result { + if let Some((entered, release)) = &self.gate { + entered.notify_one(); + release.notified().await; + } + Ok(VerifiablePrekeys { + bundles: jids + .iter() + .filter_map(|jid| self.bundles.get(jid).map(|b| (jid.clone(), b.clone()))) + .collect(), + rejected: Vec::new(), + }) + } +} + +fn text(body: &str) -> wa::Message { + let mut message = wa::Message::default(); + message.conversation = Some(body.to_owned()); + message +} + +fn enc_for(stanza: &[u8], device: &Jid) -> Option<(String, Vec)> { + let node = wacore_binary::marshal::unmarshal_packed_ref(stanza) + .unwrap() + .to_owned(); + let participants = node.get_optional_child("participants")?; + let to = participants.get_children_by_tag("to").find(|to| { + to.attrs().optional_string("jid").as_deref() == Some(device.to_string().as_str()) + })?; + let enc = to.get_optional_child("enc")?; + let kind = enc.attrs().optional_string("type")?.to_string(); + let Some(NodeContent::Bytes(bytes)) = &enc.content else { + return None; + }; + Some((kind, bytes.clone())) +} + +/// Pair `ops` under a fresh account, returning the account key pair and the +/// device identity its bundles carry. +async fn pair_with_account(ops: &SignalOps) -> (KeyPair, Vec) { + let identity = ops.public_identity().await; + let (container, account) = + common::pair_success_container(identity.identity_key, &identity.adv_secret, 1); + let signed = ops.sign_pairing(&container).await.unwrap(); + (account, signed.signed_identity) +} + +fn refused(result: Result<(), OpsError>, reason: BundleRejection) { + match result { + Err(OpsError::BundleRefused(actual)) => assert_eq!(actual, reason), + other => panic!("expected the bundle to be refused with {reason:?}, got {other:?}"), + } +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_companion_bundle_is_installed_only_with_a_device_identity_from_its_account() { + let alice = device("alice-adv", "111:1@s.whatsapp.net").await; + let bob = device("bob-adv", "222:1@s.whatsapp.net").await; + let eve = device("eve-adv", "333:1@s.whatsapp.net").await; + let bob_paired = common::pair(&bob).await; + let eve_paired = common::pair(&eve).await; + let bob_jid: Jid = "222:1@s.whatsapp.net".parse().unwrap(); + alice + .trust_account_identity(&bob_jid, bob_paired.account) + .await + .unwrap(); + let address = bob_jid.to_protocol_address(); + + // No device identity at all. + refused( + alice + .establish_session(&address, &remote(&bundle_for(&bob, 1).await, None)) + .await, + BundleRejection::MissingDeviceIdentity, + ); + // Eve's keys under Bob's name, with Eve's own valid device identity. + refused( + alice + .establish_session( + &address, + &remote( + &bundle_for(&eve, 1).await, + Some(eve_paired.device_identity.clone()), + ), + ) + .await, + BundleRejection::InvalidDeviceIdentity, + ); + // Eve's keys carrying Bob's device identity: it binds Bob's key, not Eve's. + refused( + alice + .establish_session( + &address, + &remote( + &bundle_for(&eve, 1).await, + Some(bob_paired.device_identity.clone()), + ), + ) + .await, + BundleRejection::InvalidDeviceIdentity, + ); + assert!(!alice.has_session(&address).await.unwrap()); + + alice + .establish_session( + &address, + &remote(&bundle_for(&bob, 1).await, Some(bob_paired.device_identity)), + ) + .await + .unwrap(); + assert!(alice.has_session(&address).await.unwrap()); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_companion_with_no_known_account_is_refused() { + let alice = device("alice-no-anchor", "111:1@s.whatsapp.net").await; + let bob = device("bob-no-anchor", "222:1@s.whatsapp.net").await; + let paired = common::pair(&bob).await; + let bob_jid: Jid = "222:1@s.whatsapp.net".parse().unwrap(); + refused( + alice + .establish_session( + &bob_jid.to_protocol_address(), + &remote(&bundle_for(&bob, 1).await, Some(paired.device_identity)), + ) + .await, + BundleRejection::NoAccountIdentity, + ); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_changed_primary_identity_is_refused_until_the_application_accepts_it() { + let mut rng = rand::make_rng::(); + let alice = device("alice-primary", "111:1@s.whatsapp.net").await; + let primary: Jid = "222@s.whatsapp.net".parse().unwrap(); + let address = primary.to_protocol_address(); + let first = KeyPair::generate(&mut rng); + let reinstalled = KeyPair::generate(&mut rng); + + // First contact pins the account. + alice + .establish_session(&address, &remote(&primary_bundle(&first), None)) + .await + .unwrap(); + assert_eq!( + alice.account_identity(&primary).await.unwrap(), + Some(*first.public_key.public_key_bytes()) + ); + + // A different primary key, as a client could supply at any time. + refused( + alice + .establish_session(&address, &remote(&primary_bundle(&reinstalled), None)) + .await, + BundleRejection::IdentityChanged, + ); + + // Only the application can accept the change. + alice + .trust_account_identity(&primary, *reinstalled.public_key.public_key_bytes()) + .await + .unwrap(); + alice + .establish_session(&address, &remote(&primary_bundle(&reinstalled), None)) + .await + .unwrap(); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_send_skips_a_companion_whose_bundle_does_not_verify() { + let alice = device("alice-send-adv", "111:1@s.whatsapp.net").await; + let alice_paired = common::pair(&alice).await; + let bob = device("bob-send-adv", "222:1@s.whatsapp.net").await; + let eve = device("eve-send-adv", "333:1@s.whatsapp.net").await; + let bob_paired = common::pair(&bob).await; + let eve_paired = common::pair(&eve).await; + let bob_jid: Jid = "222:1@s.whatsapp.net".parse().unwrap(); + alice + .trust_account_identity(&bob_jid, bob_paired.account) + .await + .unwrap(); + let to = bob_jid.to_non_ad(); + + let forged = Client { + devices: vec![bob_jid.clone()], + bundles: HashMap::from([( + bob_jid.clone(), + FetchedBundle { + bundle: bundle_for(&eve, 1).await, + device_identity: Some(eve_paired.device_identity), + }, + )]), + group: None, + gate: None, + }; + // Bob's only device fails verification, so there is nothing to send. + let sent = alice + .send_direct(&TokioRuntime, &forged, &to, &text("for bob"), "F1") + .await; + assert!( + matches!(sent, Err(OpsError::Send(_))), + "no stanza may be built from a refused bundle: {sent:?}" + ); + assert!( + !alice + .has_session(&bob_jid.to_protocol_address()) + .await + .unwrap() + ); + + let honest = Client { + devices: vec![bob_jid.clone()], + bundles: HashMap::from([( + bob_jid.clone(), + FetchedBundle { + bundle: bundle_for(&bob, 1).await, + device_identity: Some(bob_paired.device_identity), + }, + )]), + group: None, + gate: None, + }; + let sent = alice + .send_direct(&TokioRuntime, &honest, &to, &text("for bob"), "F2") + .await + .unwrap(); + let (kind, ciphertext) = enc_for(&sent.stanza, &bob_jid).expect("bob is reached"); + let alice_jid: Jid = "111:1@s.whatsapp.net".parse().unwrap(); + // Bob verifies Alice's prekey message against her pinned account. + bob.trust_account_identity(&alice_jid, alice_paired.account) + .await + .unwrap(); + let received = bob + .receive(ReceiveRequest { + chat: &alice_jid.to_non_ad().to_string(), + sender: &alice_jid.to_protocol_address(), + kind: ReceiveKind::try_from(kind.as_str()).unwrap(), + ciphertext: &ciphertext, + padding_version: 2, + is_from_me: false, + device_identity: Some(&alice_paired.device_identity), + }) + .await + .unwrap(); + let Received::Message(message) = received else { + panic!("expected a message") + }; + assert_eq!( + message.content.message.conversation.as_deref(), + Some("for bob") + ); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_fresh_account_is_anchored_by_its_primary_in_the_same_fetch() { + let mut rng = rand::make_rng::(); + let alice = device("alice-fresh", "111:1@s.whatsapp.net").await; + let bob = device("bob-fresh", "222:1@s.whatsapp.net").await; + let paired = common::pair(&bob).await; + let primary: Jid = "222@s.whatsapp.net".parse().unwrap(); + let companion: Jid = "222:1@s.whatsapp.net".parse().unwrap(); + let impostor = KeyPair::generate(&mut rng); + + // A primary that is not the account which signed the companion. + let mismatched = Client { + devices: vec![primary.clone(), companion.clone()], + bundles: HashMap::from([ + ( + primary.clone(), + FetchedBundle { + bundle: primary_bundle(&impostor), + device_identity: None, + }, + ), + ( + companion.clone(), + FetchedBundle { + bundle: bundle_for(&bob, 1).await, + device_identity: Some(paired.device_identity.clone()), + }, + ), + ]), + group: None, + gate: None, + }; + let sent = alice + .send_direct(&TokioRuntime, &mismatched, &primary, &text("hi"), "A1") + .await + .unwrap(); + assert!(enc_for(&sent.stanza, &companion).is_none()); + assert!(sent.unreached_devices.contains(&companion)); + // The impostor's primary was pinned on first use, so nothing later can + // swap it either; that is what trust on first use means. + assert_eq!( + alice.account_identity(&primary).await.unwrap(), + Some(*impostor.public_key.public_key_bytes()) + ); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_companion_is_accepted_when_its_account_primary_is_in_the_same_fetch() { + let alice = device("alice-same-fetch", "111:1@s.whatsapp.net").await; + let bob = device("bob-same-fetch", "222:1@s.whatsapp.net").await; + let (account, device_identity) = pair_with_account(&bob).await; + let primary: Jid = "222@s.whatsapp.net".parse().unwrap(); + let companion: Jid = "222:1@s.whatsapp.net".parse().unwrap(); + + let client = Client { + devices: vec![primary.clone(), companion.clone()], + bundles: HashMap::from([ + ( + primary.clone(), + FetchedBundle { + bundle: primary_bundle(&account), + device_identity: None, + }, + ), + ( + companion.clone(), + FetchedBundle { + bundle: bundle_for(&bob, 1).await, + device_identity: Some(device_identity), + }, + ), + ]), + group: None, + gate: None, + }; + let sent = alice + .send_direct(&TokioRuntime, &client, &primary, &text("hi"), "A2") + .await + .unwrap(); + assert!(enc_for(&sent.stanza, &companion).is_some()); + assert_eq!( + alice.account_identity(&companion).await.unwrap(), + Some(*account.public_key.public_key_bytes()) + ); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_retry_naming_a_recipient_with_another_accounts_keys_is_refused() { + let alice = device("alice-retry-adv", "111:1@s.whatsapp.net").await; + common::pair(&alice).await; + let bob = device("bob-retry-adv", "222:1@s.whatsapp.net").await; + let eve = device("eve-retry-adv", "333:1@s.whatsapp.net").await; + let bob_paired = common::pair(&bob).await; + let eve_paired = common::pair(&eve).await; + let bob_jid: Jid = "222:1@s.whatsapp.net".parse().unwrap(); + alice + .trust_account_identity(&bob_jid, bob_paired.account) + .await + .unwrap(); + let to = bob_jid.to_non_ad(); + let client = Client { + devices: vec![bob_jid.clone()], + bundles: HashMap::from([( + bob_jid.clone(), + FetchedBundle { + bundle: bundle_for(&bob, 1).await, + device_identity: Some(bob_paired.device_identity.clone()), + }, + )]), + group: None, + gate: None, + }; + alice + .send_direct(&TokioRuntime, &client, &to, &text("for bob only"), "R1") + .await + .unwrap(); + // Alice lost the session, so the retry receipt's keys would be used. + alice.forget_user(&bob_jid.to_non_ad()).await.unwrap(); + + // Bob named as requester and encryption address, with Eve's keys. + let eve_bundle = remote(&bundle_for(&eve, 1).await, Some(eve_paired.device_identity)); + let result = alice + .resend( + &client, + RetryRequest { + chat: &to, + message_id: "R1", + requester: &bob_jid, + encryption_jid: &bob_jid, + route: RetryRoute::Direct { recipient: None }, + retry_count: 1, + bundle: Some(&eve_bundle), + }, + ) + .await; + assert!( + matches!( + result, + Err(OpsError::BundleRefused( + BundleRejection::InvalidDeviceIdentity + )) + ), + "got {result:?}" + ); + assert!( + !alice + .has_session(&bob_jid.to_protocol_address()) + .await + .unwrap() + ); + + // Bob's own keys are still accepted. + let bob_bundle = remote(&bundle_for(&bob, 1).await, Some(bob_paired.device_identity)); + alice + .resend( + &client, + RetryRequest { + chat: &to, + message_id: "R1", + requester: &bob_jid, + encryption_jid: &bob_jid, + route: RetryRoute::Direct { recipient: None }, + retry_count: 2, + bundle: Some(&bob_bundle), + }, + ) + .await + .unwrap(); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_refused_group_retry_bundle_leaves_distribution_state_untouched() { + use wacore::types::message::AddressingMode; + + let group: Jid = "120363000000010@g.us".parse().unwrap(); + let alice = device("alice-group-retry-adv", "111:1@s.whatsapp.net").await; + alice + .set_own_jids("111:1@s.whatsapp.net", Some("900111:1@lid")) + .await + .unwrap(); + common::pair(&alice).await; + let bob = device("bob-group-retry-adv", "222:1@s.whatsapp.net").await; + let eve = device("eve-group-retry-adv", "333:1@s.whatsapp.net").await; + let bob_paired = common::pair(&bob).await; + let eve_paired = common::pair(&eve).await; + let bob_jid: Jid = "222:1@s.whatsapp.net".parse().unwrap(); + alice + .trust_account_identity(&bob_jid, bob_paired.account) + .await + .unwrap(); + let client = Client { + devices: vec![bob_jid.clone()], + bundles: HashMap::from([( + bob_jid.clone(), + FetchedBundle { + bundle: bundle_for(&bob, 1).await, + device_identity: Some(bob_paired.device_identity), + }, + )]), + group: Some(Arc::new(GroupRoutingInfo::new( + vec!["111@s.whatsapp.net".parse().unwrap(), bob_jid.to_non_ad()], + AddressingMode::Pn, + ))), + gate: None, + }; + let sent = alice + .send_group(&TokioRuntime, &client, &group, &text("members"), "G1") + .await + .unwrap(); + assert_eq!(sent.distribution_targets, vec![bob_jid.clone()]); + // The server acknowledged the send, so Bob holds the sender key. A + // companion counts as warm only with its primary, so mark both. + let acknowledged = [bob_jid.with_device(0), bob_jid.clone()]; + assert!( + alice + .mark_sender_key_distributed(&group, &acknowledged, sent.distribution_epoch) + .await + .unwrap() + ); + // Alice lost Bob's session, so the retry receipt's keys would be used. + alice.forget_user(&bob_jid.to_non_ad()).await.unwrap(); + + let eve_bundle = remote(&bundle_for(&eve, 1).await, Some(eve_paired.device_identity)); + let result = alice + .resend( + &client, + RetryRequest { + chat: &group, + message_id: "G1", + requester: &bob_jid, + encryption_jid: &bob_jid, + route: RetryRoute::Group { + addressing_mode: AddressingMode::Pn, + }, + retry_count: 1, + bundle: Some(&eve_bundle), + }, + ) + .await; + assert!( + matches!( + result, + Err(OpsError::BundleRefused( + BundleRejection::InvalidDeviceIdentity + )) + ), + "got {result:?}" + ); + assert!( + !alice + .has_session(&bob_jid.to_protocol_address()) + .await + .unwrap(), + "a refused retry must not create a session" + ); + // The refused retry started no new epoch, so the original send's + // acknowledgment still counts. No devices are named, so this check + // writes no mark of its own. + assert!( + alice + .mark_sender_key_distributed(&group, &[], sent.distribution_epoch) + .await + .unwrap(), + "a refused retry must not change the distribution epoch" + ); + // Bob's mark survived: the next send does not distribute the key to him + // again. + let next = alice + .send_group(&TokioRuntime, &client, &group, &text("again"), "G2") + .await + .unwrap(); + assert!( + next.distribution_targets.is_empty(), + "a refused retry must keep Bob's distribution mark, got {:?}", + next.distribution_targets + ); +} + +#[tokio::test(flavor = "multi_thread")] +async fn forgetting_a_user_waits_for_a_session_setup_already_in_flight() { + let alice = Arc::new(device("alice-forget-race", "111:1@s.whatsapp.net").await); + let bob = device("bob-forget-race", "222:1@s.whatsapp.net").await; + let paired = common::pair(&bob).await; + let bob_jid: Jid = "222:1@s.whatsapp.net".parse().unwrap(); + alice + .trust_account_identity(&bob_jid, paired.account) + .await + .unwrap(); + let (entered, release) = ( + Arc::new(tokio::sync::Notify::new()), + Arc::new(tokio::sync::Notify::new()), + ); + let client = Arc::new(Client { + devices: vec![bob_jid.clone()], + bundles: HashMap::from([( + bob_jid.clone(), + FetchedBundle { + bundle: bundle_for(&bob, 1).await, + device_identity: Some(paired.device_identity), + }, + )]), + group: None, + gate: Some((entered.clone(), release.clone())), + }); + + // A first send to Bob is setting up his session. + let send = { + let (alice, client, to) = (alice.clone(), client.clone(), bob_jid.to_non_ad()); + tokio::spawn(async move { + alice + .send_direct(&TokioRuntime, &*client, &to, &text("hi"), "S1") + .await + }) + }; + entered.notified().await; + + // An identity reset for Bob must not finish while that setup can still + // commit a session it would have deleted. + let forget = { + let (alice, user) = (alice.clone(), bob_jid.to_non_ad()); + tokio::spawn(async move { alice.forget_user(&user).await }) + }; + tokio::time::sleep(Duration::from_millis(100)).await; + assert!(!forget.is_finished(), "the reset must wait for the send"); + + release.notify_one(); + send.await.unwrap().unwrap(); + forget.await.unwrap().unwrap(); + assert!( + !alice + .has_session(&bob_jid.to_protocol_address()) + .await + .unwrap(), + "no session may survive the reset" + ); +} + +/// `Client` with a known phone-number to LID mapping, so sends set up +/// sessions at the LID address. +struct Mapped { + inner: Client, + phones: &'static [&'static str], + lid: &'static str, +} + +#[async_trait] +impl SendContextResolver for Mapped { + async fn lock_device_sessions(&self, device_jids: &[Jid]) -> SessionLockGuard { + self.inner.lock_device_sessions(device_jids).await + } + + async fn resolve_devices(&self, jids: &[Jid]) -> anyhow::Result> { + self.inner.resolve_devices(jids).await + } + + async fn fetch_prekeys(&self, jids: &[Jid]) -> anyhow::Result> { + self.inner.fetch_prekeys(jids).await + } + + async fn fetch_prekeys_for_identity_check( + &self, + jids: &[Jid], + ) -> anyhow::Result { + self.inner.fetch_prekeys_for_identity_check(jids).await + } + + async fn resolve_group_routing_info(&self, jid: &Jid) -> anyhow::Result> { + self.inner.resolve_group_routing_info(jid).await + } + + async fn get_lid_for_phone(&self, phone_user: &str) -> Option { + self.phones.contains(&phone_user).then(|| self.lid.into()) + } +} + +#[async_trait] +impl SignalResolver for Mapped { + async fn fetch_verifiable_prekeys(&self, jids: &[Jid]) -> anyhow::Result { + self.inner.fetch_verifiable_prekeys(jids).await + } +} + +async fn mapped_client( + bob: &SignalOps, + account: &KeyPair, + device_identity: Vec, +) -> Mapped { + let primary: Jid = "222@s.whatsapp.net".parse().unwrap(); + let companion: Jid = "222:1@s.whatsapp.net".parse().unwrap(); + Mapped { + inner: Client { + devices: vec![primary.clone(), companion.clone()], + bundles: HashMap::from([ + ( + primary, + FetchedBundle { + bundle: primary_bundle(account), + device_identity: None, + }, + ), + ( + companion, + FetchedBundle { + bundle: bundle_for(bob, 1).await, + device_identity: Some(device_identity), + }, + ), + ]), + group: None, + gate: None, + }, + phones: &["222"], + lid: "900222", + } +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_first_send_at_a_mapped_lid_pins_the_account_there_too() { + let alice = device("alice-mapped", "111:1@s.whatsapp.net").await; + let bob = device("bob-mapped", "222:1@s.whatsapp.net").await; + let (account, device_identity) = pair_with_account(&bob).await; + let primary: Jid = "222@s.whatsapp.net".parse().unwrap(); + let lid_companion: Jid = "900222:1@lid".parse().unwrap(); + + let client = mapped_client(&bob, &account, device_identity.clone()).await; + alice + .send_direct(&TokioRuntime, &client, &primary, &text("hi"), "M1") + .await + .unwrap(); + assert_eq!( + alice.account_identity(&lid_companion).await.unwrap(), + Some(*account.public_key.public_key_bytes()) + ); + + // A companion that later arrives at the LID verifies against that pin. + let reset = remote(&bundle_for(&bob, 1).await, Some(device_identity)); + alice + .establish_session(&lid_companion.to_protocol_address(), &reset) + .await + .unwrap(); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_first_send_at_a_mapped_lid_refuses_keys_that_conflict_with_its_pin() { + let mut rng = rand::make_rng::(); + let alice = device("alice-mapped-kept", "111:1@s.whatsapp.net").await; + let bob = device("bob-mapped-kept", "222:1@s.whatsapp.net").await; + let (account, device_identity) = pair_with_account(&bob).await; + let primary: Jid = "222@s.whatsapp.net".parse().unwrap(); + let lid: Jid = "900222@lid".parse().unwrap(); + let accepted = KeyPair::generate(&mut rng); + let accepted = *accepted.public_key.public_key_bytes(); + alice.trust_account_identity(&lid, accepted).await.unwrap(); + + // Bob's PN bundles verify only against the PN pin, but the session would + // be built at the LID, where another key was accepted: refused. + let client = mapped_client(&bob, &account, device_identity).await; + let sent = alice + .send_direct(&TokioRuntime, &client, &primary, &text("hi"), "M2") + .await; + for device in [ + "900222@lid", + "900222:1@lid", + "222@s.whatsapp.net", + "222:1@s.whatsapp.net", + ] { + let device: Jid = device.parse().unwrap(); + assert!( + !alice + .has_session(&device.to_protocol_address()) + .await + .unwrap(), + "no session for {device}" + ); + } + if let Ok(sent) = sent { + let companion: Jid = "222:1@s.whatsapp.net".parse().unwrap(); + assert!(enc_for(&sent.stanza, &primary).is_none()); + assert!(enc_for(&sent.stanza, &companion).is_none()); + } + assert_eq!(alice.account_identity(&lid).await.unwrap(), Some(accepted)); +} + +/// Records that hold the first armed write touching account pins until +/// released, as a slow database would. +struct PinWritePause { + inner: MemoryRecordStore, + armed: std::sync::atomic::AtomicBool, + entered: tokio::sync::Notify, + release: tokio::sync::Notify, +} + +#[async_trait] +impl wacore_recordstore::RecordStore for PinWritePause { + async fn get( + &self, + scope: &str, + ns: wacore_recordstore::Namespace, + key: &str, + ) -> wacore::store::error::Result>> { + self.inner.get(scope, ns, key).await + } + + async fn get_many( + &self, + scope: &str, + ns: wacore_recordstore::Namespace, + keys: &[&str], + ) -> wacore::store::error::Result)>> { + self.inner.get_many(scope, ns, keys).await + } + + async fn scan_keys( + &self, + scope: &str, + ns: wacore_recordstore::Namespace, + prefix: &str, + limit: Option, + ) -> wacore::store::error::Result> { + self.inner.scan_keys(scope, ns, prefix, limit).await + } + + async fn write( + &self, + fence: &wacore_recordstore::Fence, + ops: &[wacore_recordstore::WriteOp], + ) -> wacore::store::error::Result<()> { + use wacore_recordstore::{Namespace, WriteOp}; + let pins = ops.iter().any(|op| { + matches!( + op, + WriteOp::Put { + ns: Namespace::AccountIdentity, + .. + } | WriteOp::Insert { + ns: Namespace::AccountIdentity, + .. + } + ) + }); + if pins && self.armed.swap(false, std::sync::atomic::Ordering::SeqCst) { + self.entered.notify_one(); + self.release.notified().await; + } + self.inner.write(fence, ops).await + } +} + +#[tokio::test(flavor = "multi_thread")] +async fn an_explicit_trust_waits_for_a_migration_and_is_not_overwritten() { + let records = Arc::new(PinWritePause { + inner: MemoryRecordStore::new(), + armed: std::sync::atomic::AtomicBool::new(false), + entered: tokio::sync::Notify::new(), + release: tokio::sync::Notify::new(), + }); + let lease = records + .inner + .acquire("alice-migrate-trust", "node-a", 0, 60_000) + .await + .unwrap() + .unwrap(); + let alice = Arc::new( + SignalOps::create(Arc::new(RecordSignalStore::new( + records.clone(), + Arc::new(AesGcmSealer::new(&[5; 32])), + lease.fence, + ))) + .await + .unwrap(), + ); + let pn: Jid = "222@s.whatsapp.net".parse().unwrap(); + let lid: Jid = "900222@lid".parse().unwrap(); + alice.trust_account_identity(&pn, [1; 32]).await.unwrap(); + + records + .armed + .store(true, std::sync::atomic::Ordering::SeqCst); + let migration = { + let (alice, pn, lid) = (alice.clone(), pn.clone(), lid.clone()); + tokio::spawn(async move { alice.migrate_pn_to_lid(&pn, &lid).await }) + }; + tokio::time::timeout(Duration::from_secs(5), records.entered.notified()) + .await + .expect("the migration reaches its commit"); + + // The application accepts a new key for the LID while the migration's + // batch, which copies the old PN key there, is still being written. + // Polled here, so the attempt has certainly reached the account lock. + let trust = alice.trust_account_identity(&lid, [2; 32]); + futures::pin_mut!(trust); + assert!( + futures::poll!(&mut trust).is_pending(), + "explicit trust waits for the migration" + ); + + records.release.notify_one(); + migration.await.unwrap().unwrap(); + trust.await.unwrap(); + assert_eq!(alice.account_identity(&lid).await.unwrap(), Some([2; 32])); +} + +#[tokio::test(flavor = "multi_thread")] +async fn each_phone_number_mapped_to_one_lid_must_agree_with_its_pin() { + use wacore::types::message::AddressingMode; + let mut rng = rand::make_rng::(); + let alice = device("alice-two-phones", "111:1@s.whatsapp.net").await; + alice + .set_own_jids("111:1@s.whatsapp.net", Some("900111:1@lid")) + .await + .unwrap(); + common::pair(&alice).await; + let eve = device("eve-two-phones", "333:1@s.whatsapp.net").await; + let (eve_account, eve_identity) = pair_with_account(&eve).await; + let bob_account = KeyPair::generate(&mut rng); + let lid: Jid = "900222@lid".parse().unwrap(); + alice + .trust_account_identity(&lid, *bob_account.public_key.public_key_bytes()) + .await + .unwrap(); + let bob_primary: Jid = "222@s.whatsapp.net".parse().unwrap(); + let eve_primary: Jid = "333@s.whatsapp.net".parse().unwrap(); + let eve_companion: Jid = "333:1@s.whatsapp.net".parse().unwrap(); + // Both phone numbers map to the LID pinned for Bob. Bob's key agrees + // with it and is checked first; Eve's does not. + let client = Mapped { + inner: Client { + devices: vec![ + bob_primary.clone(), + eve_primary.clone(), + eve_companion.clone(), + ], + bundles: HashMap::from([ + ( + bob_primary.clone(), + FetchedBundle { + bundle: primary_bundle(&bob_account), + device_identity: None, + }, + ), + ( + eve_primary.clone(), + FetchedBundle { + bundle: primary_bundle(&eve_account), + device_identity: None, + }, + ), + ( + eve_companion.clone(), + FetchedBundle { + bundle: bundle_for(&eve, 1).await, + device_identity: Some(eve_identity), + }, + ), + ]), + group: Some(Arc::new(GroupRoutingInfo::new( + vec![ + "111@s.whatsapp.net".parse().unwrap(), + bob_primary.clone(), + eve_primary.clone(), + ], + AddressingMode::Pn, + ))), + gate: None, + }, + phones: &["222", "333"], + lid: "900222", + }; + let group: Jid = "120363000000020@g.us".parse().unwrap(); + alice + .send_group(&TokioRuntime, &client, &group, &text("hi"), "T1") + .await + .unwrap(); + // Only Eve's keys could set up a session for device 1 at the LID. + let lid_companion: Jid = "900222:1@lid".parse().unwrap(); + assert!( + !alice + .has_session(&lid_companion.to_protocol_address()) + .await + .unwrap(), + "keys the LID pin does not accept built a session there" + ); + assert_eq!( + alice.account_identity(&lid).await.unwrap(), + Some(*bob_account.public_key.public_key_bytes()) + ); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_retry_at_a_lid_uses_the_pin_saved_for_its_phone_number() { + let alice = device("alice-retry-lid", "111:1@s.whatsapp.net").await; + common::pair(&alice).await; + let bob = device("bob-retry-lid", "222:1@s.whatsapp.net").await; + let (account, device_identity) = pair_with_account(&bob).await; + let primary: Jid = "222@s.whatsapp.net".parse().unwrap(); + let companion: Jid = "222:1@s.whatsapp.net".parse().unwrap(); + let lid_companion: Jid = "900222:1@lid".parse().unwrap(); + + // First send, before the LID is known: the pin is saved under the PN. + let unmapped = Mapped { + phones: &[], + ..mapped_client(&bob, &account, device_identity.clone()).await + }; + alice + .send_direct(&TokioRuntime, &unmapped, &primary, &text("hi"), "R1") + .await + .unwrap(); + assert!( + alice + .account_identity(&"900222@lid".parse().unwrap()) + .await + .unwrap() + .is_none() + ); + + // Now the companion asks for a retry at its LID, where no session exists. + let mut mapped = mapped_client(&bob, &account, device_identity.clone()).await; + mapped.inner.bundles.insert( + lid_companion.clone(), + FetchedBundle { + bundle: bundle_for(&bob, 1).await, + device_identity: Some(device_identity), + }, + ); + alice + .resend( + &mapped, + RetryRequest { + chat: &primary, + message_id: "R1", + requester: &companion, + encryption_jid: &lid_companion, + route: RetryRoute::Direct { recipient: None }, + retry_count: 1, + bundle: None, + }, + ) + .await + .expect("the PN pin vouches for the LID companion"); + assert_eq!( + alice + .account_identity(&"900222@lid".parse().unwrap()) + .await + .unwrap(), + Some(*account.public_key.public_key_bytes()) + ); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_hosted_companion_verifies_against_its_ordinary_accounts_pin() { + use wacore::adv::test_util::{account_prefix, signed_identity}; + use waproto::whatsapp::ADVEncryptionType; + let mut rng = rand::make_rng::(); + let alice = device("alice-hosted", "111:1@s.whatsapp.net").await; + let account = KeyPair::generate(&mut rng); + // The account is pinned under its phone number, as its primary would be. + alice + .trust_account_identity( + &"222@s.whatsapp.net".parse().unwrap(), + *account.public_key.public_key_bytes(), + ) + .await + .unwrap(); + + // A hosted (Cloud API) companion of that account, with a hosted ADV chain. + let hosted_device = KeyPair::generate(&mut rng); + let mut details = waproto::whatsapp::ADVDeviceIdentity::default(); + details.raw_id = Some(1); + details.key_index = Some(1); + details.device_type = Some(ADVEncryptionType::HOSTED); + let details = waproto::codec::adv_device_identity_to_vec(&details); + let identity = signed_identity( + &account, + &hosted_device, + &details, + account_prefix(Some(ADVEncryptionType::HOSTED)), + Some(&[6, 6]), + false, + ); + let identity = waproto::codec::adv_signed_device_identity_to_vec(&identity); + let hosted: Jid = "222:99@hosted".parse().unwrap(); + let bundle = remote(&primary_bundle(&hosted_device), Some(identity.clone())); + alice + .establish_session(&hosted.to_protocol_address(), &bundle) + .await + .expect("the hosted companion verifies against the phone-number pin"); + + // A hosted companion signed by another account is still refused. + let other = KeyPair::generate(&mut rng); + let forged = waproto::codec::adv_signed_device_identity_to_vec(&signed_identity( + &other, + &hosted_device, + &details, + account_prefix(Some(ADVEncryptionType::HOSTED)), + Some(&[6, 6]), + false, + )); + let forged_jid: Jid = "222:98@hosted".parse().unwrap(); + refused( + alice + .establish_session( + &forged_jid.to_protocol_address(), + &remote(&primary_bundle(&hosted_device), Some(forged)), + ) + .await, + BundleRejection::InvalidDeviceIdentity, + ); +} diff --git a/crates/signal-ops/tests/verified_receive.rs b/crates/signal-ops/tests/verified_receive.rs new file mode 100644 index 0000000..65b8597 --- /dev/null +++ b/crates/signal-ops/tests/verified_receive.rs @@ -0,0 +1,310 @@ +// SPDX-License-Identifier: MIT + +//! A prekey message starts a session, so its sender is verified like a +//! fetched bundle; pins follow a PN-to-LID migration; own JIDs are checked. + +use futures::executor::block_on; +use std::sync::Arc; +use wacore::types::jid::JidExt as _; +use wacore_binary::Jid; +use wacore_recordstore::{AesGcmSealer, LeaseStore, MemoryRecordStore, RecordSignalStore}; +use wacore_signal_ops::{ + BundleRejection, EncKind, OpsError, RemoteBundle, ServiceStore, SignalOps, +}; + +mod common; +mod runtime; +use runtime::InlineRuntime; + +type Store = RecordSignalStore; + +fn store(scope: &str) -> Arc { + let records = Arc::new(MemoryRecordStore::new()); + let lease = block_on(records.acquire(scope, "node-a", 0, 60_000)) + .unwrap() + .expect("lease available"); + Arc::new(RecordSignalStore::new( + records, + Arc::new(AesGcmSealer::new(&[9; 32])), + lease.fence, + )) +} + +fn device(scope: &str) -> SignalOps { + block_on(SignalOps::create(store(scope))).unwrap() +} + +fn jid(text: &str) -> Jid { + text.parse().unwrap() +} + +/// What the client would fetch for `ops`: one one-time prekey, optionally. +fn bundle_of(ops: &SignalOps, device_identity: Option>) -> RemoteBundle { + let identity = block_on(ops.public_identity()); + let prekey = block_on(ops.generate_prekeys(&InlineRuntime, 1)) + .unwrap() + .remove(0); + RemoteBundle { + registration_id: identity.registration_id, + device_id: 1, + identity_key: identity.identity_key, + signed_prekey_id: identity.signed_prekey.id, + signed_prekey: identity.signed_prekey.public_key, + signed_prekey_signature: identity.signed_prekey.signature, + prekey: Some((prekey.id, prekey.public_key)), + device_identity, + } +} + +fn refusal(result: Result) -> BundleRejection { + match result { + Err(OpsError::BundleRefused(reason)) => reason, + other => panic!("expected a refused bundle, got {other:?}"), + } +} + +#[test] +fn a_prekey_message_is_verified_against_the_pinned_account_before_it_creates_a_session() { + let alice_jid = jid("111:1@s.whatsapp.net"); + let bob_jid = jid("222@s.whatsapp.net"); + let (alice, eve, bob) = (device("alice-pk"), device("eve-pk"), device("bob-pk")); + let alice_addr = alice_jid.to_protocol_address(); + let bob_addr = bob_jid.to_protocol_address(); + + // Bob knows Alice's account. Eve has her own account and valid identity. + let alice_paired = block_on(common::pair(&alice)); + block_on(bob.trust_account_identity(&alice_jid, alice_paired.account)).unwrap(); + let eve_paired = block_on(common::pair(&eve)); + + // Both start a session from the same bundle, so a refusal that consumed + // the one-time prekey would break the honest message later. + let bundle = bundle_of(&bob, None); + block_on(alice.establish_session(&bob_addr, &bundle)).unwrap(); + block_on(eve.establish_session(&bob_addr, &bundle)).unwrap(); + + // Eve's message under Alice's name, with Eve's own valid device identity. + let forged = block_on(eve.encrypt(&bob_addr, b"forged")).unwrap(); + assert_eq!(forged.kind, EncKind::PreKey); + let reason = refusal(block_on(bob.decrypt( + &alice_addr, + forged.kind, + &forged.ciphertext, + Some(&eve_paired.device_identity), + ))); + assert_eq!(reason, BundleRejection::InvalidDeviceIdentity); + assert!(!block_on(bob.has_session(&alice_addr)).unwrap()); + + // A companion's prekey message without its device identity. + let honest = block_on(alice.encrypt(&bob_addr, b"hello bob")).unwrap(); + let reason = refusal(block_on(bob.decrypt( + &alice_addr, + honest.kind, + &honest.ciphertext, + None, + ))); + assert_eq!(reason, BundleRejection::MissingDeviceIdentity); + assert!(!block_on(bob.has_session(&alice_addr)).unwrap()); + + // The legitimate message still works afterwards. + let opened = block_on(bob.decrypt( + &alice_addr, + honest.kind, + &honest.ciphertext, + Some(&alice_paired.device_identity), + )) + .unwrap(); + assert_eq!(opened.plaintext, b"hello bob"); + assert!(block_on(bob.has_session(&alice_addr)).unwrap()); +} + +#[test] +fn a_primary_that_changes_identity_is_refused_until_the_application_accepts_it() { + let carol_jid = jid("444@s.whatsapp.net"); + let bob_jid = jid("222@s.whatsapp.net"); + let (carol, reset_carol, bob) = ( + device("carol-pk"), + device("carol-reset-pk"), + device("bob-pk2"), + ); + let carol_addr = carol_jid.to_protocol_address(); + let bob_addr = bob_jid.to_protocol_address(); + + block_on(carol.establish_session(&bob_addr, &bundle_of(&bob, None))).unwrap(); + block_on(reset_carol.establish_session(&bob_addr, &bundle_of(&bob, None))).unwrap(); + + // A primary pins its identity key on first use. + let first = block_on(carol.encrypt(&bob_addr, b"first")).unwrap(); + block_on(bob.decrypt(&carol_addr, first.kind, &first.ciphertext, None)).unwrap(); + let pinned = block_on(bob.account_identity(&carol_jid)).unwrap(); + assert_eq!(pinned, Some(block_on(carol.public_identity()).identity_key)); + + // The same address now arrives with different keys. + let reset = block_on(reset_carol.encrypt(&bob_addr, b"after reset")).unwrap(); + assert_eq!(reset.kind, EncKind::PreKey); + let reason = refusal(block_on(bob.decrypt( + &carol_addr, + reset.kind, + &reset.ciphertext, + None, + ))); + assert_eq!(reason, BundleRejection::IdentityChanged); + + let accepted = block_on(reset_carol.public_identity()).identity_key; + block_on(bob.trust_account_identity(&carol_jid, accepted)).unwrap(); + let opened = block_on(bob.decrypt(&carol_addr, reset.kind, &reset.ciphertext, None)).unwrap(); + assert_eq!(opened.plaintext, b"after reset"); +} + +#[test] +fn a_prekey_message_from_another_device_of_the_same_account_needs_no_pin() { + let me_jid = jid("111:1@s.whatsapp.net"); + let other_jid = jid("111:2@s.whatsapp.net"); + let stranger_jid = jid("111:3@s.whatsapp.net"); + let (me, other, stranger) = ( + device("me-own"), + device("other-own"), + device("stranger-own"), + ); + block_on(me.set_own_jids(&me_jid.to_string(), None)).unwrap(); + block_on(other.set_own_jids(&other_jid.to_string(), None)).unwrap(); + let me_paired = block_on(common::pair(&me)); + let other_identity = block_on(common::pair_under(&other, &me_paired.account_keys)); + let (me_addr, other_addr) = ( + me_jid.to_protocol_address(), + other_jid.to_protocol_address(), + ); + + // The other device verifies `me` against the account it was paired under. + block_on(other.establish_session( + &me_addr, + &bundle_of(&me, Some(me_paired.device_identity.clone())), + )) + .unwrap(); + let sent = block_on(other.encrypt(&me_addr, b"from my other phone")).unwrap(); + assert_eq!(sent.kind, EncKind::PreKey); + assert_eq!(block_on(me.account_identity(&other_jid)).unwrap(), None); + let opened = block_on(me.decrypt( + &other_addr, + sent.kind, + &sent.ciphertext, + Some(&other_identity), + )) + .unwrap(); + assert_eq!(opened.plaintext, b"from my other phone"); + + // A device claiming the same user but paired under another account fails. + block_on(stranger.trust_account_identity(&me_jid, me_paired.account)).unwrap(); + block_on(stranger.establish_session( + &me_addr, + &bundle_of(&me, Some(me_paired.device_identity.clone())), + )) + .unwrap(); + let stranger_paired = block_on(common::pair(&stranger)); + let forged = block_on(stranger.encrypt(&me_addr, b"not yours")).unwrap(); + let reason = refusal(block_on(me.decrypt( + &stranger_jid.to_protocol_address(), + forged.kind, + &forged.ciphertext, + Some(&stranger_paired.device_identity), + ))); + assert_eq!(reason, BundleRejection::InvalidDeviceIdentity); +} + +/// Alice holds a session with Bob's PN companion, so a migration has +/// something to move, and pins his phone-number account. +fn alice_with_a_pn_session( + scope: &str, + bob_a: &SignalOps, + paired: &common::Paired, +) -> SignalOps { + let alice = device(scope); + let pn_device = jid("222:1@s.whatsapp.net"); + block_on(alice.trust_account_identity(&pn_device, paired.account)).unwrap(); + block_on(alice.establish_session( + &pn_device.to_protocol_address(), + &bundle_of(bob_a, Some(paired.device_identity.clone())), + )) + .unwrap(); + alice +} + +#[test] +fn migrating_to_a_lid_carries_the_account_pin() { + let bob_a = device("bob-a-migrate"); + let bob_b = device("bob-b-migrate"); + let paired = block_on(common::pair(&bob_a)); + let b_identity = block_on(common::pair_under(&bob_b, &paired.account_keys)); + let alice = alice_with_a_pn_session("alice-migrate", &bob_a, &paired); + + block_on(alice.migrate_pn_to_lid(&jid("222@s.whatsapp.net"), &jid("900222@lid"))).unwrap(); + assert_eq!( + block_on(alice.account_identity(&jid("900222@lid"))).unwrap(), + Some(paired.account) + ); + + // A second companion of the same account, first met at its LID address. + let lid_device = jid("900222:2@lid"); + block_on(alice.establish_session( + &lid_device.to_protocol_address(), + &bundle_of(&bob_b, Some(b_identity)), + )) + .expect("the migrated pin anchors the LID companion"); +} + +#[test] +fn migrating_keeps_a_pin_the_lid_already_has() { + let bob_a = device("bob-a-keep"); + let bob_b = device("bob-b-keep"); + let paired = block_on(common::pair(&bob_a)); + let b_identity = block_on(common::pair_under(&bob_b, &paired.account_keys)); + let alice = alice_with_a_pn_session("alice-keep", &bob_a, &paired); + let other_key = [9u8; 32]; + block_on(alice.trust_account_identity(&jid("900222@lid"), other_key)).unwrap(); + + block_on(alice.migrate_pn_to_lid(&jid("222@s.whatsapp.net"), &jid("900222@lid"))).unwrap(); + assert_eq!( + block_on(alice.account_identity(&jid("900222@lid"))).unwrap(), + Some(other_key) + ); + let reason = refusal(block_on(alice.establish_session( + &jid("900222:2@lid").to_protocol_address(), + &bundle_of(&bob_b, Some(b_identity)), + ))); + assert_eq!(reason, BundleRejection::InvalidDeviceIdentity); +} + +#[test] +fn own_jids_are_checked_and_a_refusal_leaves_the_device_keys_alone() { + let store = store("own-jids"); + let ops = block_on(SignalOps::create(store.clone())).unwrap(); + block_on(ops.set_own_jids("111:1@s.whatsapp.net", Some("900111:1@lid"))).unwrap(); + let blob = block_on(store.load_device_keys()).unwrap(); + let identity = block_on(ops.public_identity()); + + let long = format!("{}@s.whatsapp.net", "1".repeat(70_000)); + let long_lid = format!("{}@lid", "9".repeat(70_000)); + let attempts: [(&str, Option<&str>); 6] = [ + // Swapped. + ("900111:1@lid", Some("111:1@s.whatsapp.net")), + // A LID where the phone number belongs. + ("900111:1@lid", None), + // A phone number where the LID belongs. + ("111:1@s.whatsapp.net", Some("222:1@s.whatsapp.net")), + // Too long for the device-key record. + (&long, None), + ("111:1@s.whatsapp.net", Some(&long_lid)), + ("not a jid", None), + ]; + for (pn, lid) in attempts { + let result = block_on(ops.set_own_jids(pn, lid)); + assert!( + matches!(result, Err(OpsError::InvalidInput(_))), + "expected {:?} to be refused, got {result:?}", + (pn.len(), lid.map(str::len)) + ); + assert_eq!(block_on(store.load_device_keys()).unwrap(), blob); + assert_eq!(block_on(ops.public_identity()), identity); + } + let reopened = block_on(SignalOps::open(store)).unwrap(); + assert_eq!(block_on(reopened.public_identity()), identity); +} diff --git a/docs/app-api.openapi.json b/docs/app-api.openapi.json new file mode 100644 index 0000000..aff4931 --- /dev/null +++ b/docs/app-api.openapi.json @@ -0,0 +1,608 @@ +{ + "openapi": "3.1.0", + "info": { + "title": "Signal node local application API", + "version": "1", + "description": "Server-only sidecar boundary. App bearer secret is distinct from the upstream Polymorfa project credential. History delivery is off by default and requires explicit operator consent." + }, + "servers": [ + { + "url": "http://127.0.0.1:8787" + } + ], + "security": [ + { + "appBearer": [] + } + ], + "paths": { + "/healthz": { + "get": { + "operationId": "health", + "security": [], + "responses": { + "200": { + "description": "Success", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Health" + } + } + } + } + } + } + }, + "/v1/connections": { + "get": { + "operationId": "listConnections", + "responses": { + "200": { + "description": "Success", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Connections" + } + } + } + }, + "401": { + "description": "Missing or invalid application secret" + }, + "403": { + "description": "Origin-bearing browser request is refused" + } + } + } + }, + "/v1/connections/{id}/messages": { + "post": { + "operationId": "sendMessage", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/SendRequest" + } + } + } + }, + "responses": { + "200": { + "description": "Success", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/SendOutcome" + } + } + } + }, + "400": { + "description": "Invalid request" + }, + "404": { + "description": "Unknown configured connection" + }, + "409": { + "description": "Not lease holder, unpaired or unavailable connection" + }, + "429": { + "description": "Runner send ceiling reached" + }, + "503": { + "description": "Relay disconnected, or storage/runner unavailable before any send acknowledgment. A resend failure after a participant-hash mismatch acknowledgment returns 504 send_unconfirmed with the original message ID." + }, + "504": { + "description": "Send outcome is unconfirmed because a queued send lost its answer or a required resend failed after the first acknowledgment. Retain error.message_id for reconciliation; do not automatically retry, because a new request creates another message ID. This ID is not an idempotency key or a lookup endpoint.", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/UnconfirmedSend" + } + } + } + }, + "401": { + "description": "Missing or invalid application secret" + }, + "403": { + "description": "Origin-bearing browser request is refused" + } + } + } + }, + "/v1/events": { + "get": { + "operationId": "events", + "parameters": [ + { + "name": "connection_id", + "in": "query", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "SSE records use Event JSON with id and event fields. No server cursor replay: unacknowledged ciphertext must be retried by the relay.", + "content": { + "text/event-stream": { + "schema": { + "type": "string" + } + } + } + }, + "404": { + "description": "SSE delivery disabled" + }, + "401": { + "description": "Missing or invalid application secret" + }, + "403": { + "description": "Origin-bearing browser request is refused" + } + } + } + }, + "/v1/events/{id}/ack": { + "post": { + "operationId": "ackEvent", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "204": { + "description": "Application acknowledgement accepted" + }, + "404": { + "description": "No active delivery waits for this event ID" + }, + "401": { + "description": "Missing or invalid application secret" + }, + "403": { + "description": "Origin-bearing browser request is refused" + } + } + } + } + }, + "components": { + "securitySchemes": { + "appBearer": { + "type": "http", + "scheme": "bearer" + } + }, + "schemas": { + "MediaDescriptor": { + "type": "object", + "additionalProperties": false, + "required": [ + "kind", + "direct_path", + "media_key", + "file_sha256", + "file_enc_sha256", + "file_length", + "mimetype" + ], + "properties": { + "kind": { + "type": "string", + "enum": [ + "image", + "video", + "audio", + "document", + "sticker" + ] + }, + "direct_path": { + "type": "string" + }, + "url": { + "type": "string" + }, + "media_key": { + "type": "string" + }, + "file_sha256": { + "type": "string" + }, + "file_enc_sha256": { + "type": "string" + }, + "mimetype": { + "type": "string" + }, + "caption": { + "type": "string" + }, + "file_name": { + "type": "string" + }, + "file_length": { + "type": "integer", + "format": "int64" + }, + "width": { + "type": "integer", + "format": "int64" + }, + "height": { + "type": "integer", + "format": "int64" + }, + "seconds": { + "type": "integer", + "format": "int64" + }, + "media_key_timestamp": { + "type": "integer", + "format": "int64" + }, + "ptt": { + "type": "boolean" + } + } + }, + "HistoryDescriptor": { + "type": "object", + "required": [ + "notification_proto" + ], + "properties": { + "direct_path": { + "type": [ + "string", + "null" + ] + }, + "media_key": { + "type": [ + "string", + "null" + ] + }, + "file_sha256": { + "type": [ + "string", + "null" + ] + }, + "file_enc_sha256": { + "type": [ + "string", + "null" + ] + }, + "original_message_id": { + "type": [ + "string", + "null" + ] + }, + "peer_data_request_session_id": { + "type": [ + "string", + "null" + ] + }, + "inline_payload": { + "type": [ + "string", + "null" + ] + }, + "sync_type": { + "type": [ + "integer", + "null" + ], + "format": "int64" + }, + "chunk_order": { + "type": [ + "integer", + "null" + ], + "format": "int64" + }, + "progress": { + "type": [ + "integer", + "null" + ], + "format": "int64" + }, + "file_length": { + "type": [ + "integer", + "null" + ], + "format": "int64" + }, + "notification_proto": { + "type": "string" + } + } + }, + "Event": { + "type": "object", + "required": [ + "id", + "type", + "connection_id", + "chat", + "sender", + "message_id", + "timestamp", + "is_from_me", + "redelivered", + "identity_changed", + "message_proto" + ], + "properties": { + "id": { + "type": "string" + }, + "type": { + "type": "string", + "enum": [ + "message", + "history.sync" + ] + }, + "connection_id": { + "type": "string" + }, + "chat": { + "type": "string" + }, + "sender": { + "type": "string" + }, + "sender_alt": { + "type": "string" + }, + "message_id": { + "type": "string" + }, + "message_proto": { + "type": "string" + }, + "text": { + "type": "string" + }, + "timestamp": { + "type": "integer", + "format": "int64" + }, + "is_from_me": { + "type": "boolean" + }, + "redelivered": { + "type": "boolean" + }, + "identity_changed": { + "type": "boolean" + }, + "media": { + "$ref": "#/components/schemas/MediaDescriptor" + }, + "history": { + "$ref": "#/components/schemas/HistoryDescriptor" + } + } + }, + "SendRequest": { + "type": "object", + "additionalProperties": false, + "required": [ + "to" + ], + "properties": { + "to": { + "type": "string" + }, + "text": { + "type": "string" + }, + "media": { + "$ref": "#/components/schemas/MediaDescriptor" + } + }, + "oneOf": [ + { + "required": [ + "text" + ], + "not": { + "required": [ + "media" + ] + } + }, + { + "required": [ + "media" + ], + "not": { + "required": [ + "text" + ] + } + } + ] + }, + "UnconfirmedSend": { + "type": "object", + "additionalProperties": false, + "required": ["error"], + "properties": { + "error": { + "type": "object", + "additionalProperties": false, + "required": ["code", "message", "message_id"], + "properties": { + "code": { "type": "string", "const": "send_unconfirmed" }, + "message": { "type": "string" }, + "message_id": { "type": "string", "minLength": 1 } + } + } + } + }, + "SendOutcome": { + "type": "object", + "required": [ + "message_id", + "server_timestamp", + "resent" + ], + "properties": { + "message_id": { + "type": "string" + }, + "server_timestamp": { + "type": "integer", + "format": "int64" + }, + "resent": { + "type": "boolean" + } + } + }, + "ConnectionInfo": { + "type": "object", + "required": [ + "id", + "lease_generation", + "claimed", + "has_device_keys", + "state", + "pn", + "lid" + ], + "properties": { + "id": { + "type": "string" + }, + "lease_generation": { + "type": "integer", + "format": "int64" + }, + "claimed": { + "type": "boolean" + }, + "has_device_keys": { + "type": "boolean" + }, + "state": { + "type": "string" + }, + "pn": { + "type": [ + "string", + "null" + ] + }, + "lid": { + "type": [ + "string", + "null" + ] + } + } + }, + "Connections": { + "type": "object", + "required": [ + "node_id", + "connections" + ], + "properties": { + "node_id": { + "type": "string" + }, + "connections": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ConnectionInfo" + } + } + } + }, + "Health": { + "type": "object", + "required": [ + "status" + ], + "properties": { + "status": { + "type": "string", + "const": "ok" + } + } + }, + "Error": { + "type": "object", + "required": [ + "error" + ], + "properties": { + "error": { + "type": "object", + "required": [ + "code", + "message" + ], + "properties": { + "code": { + "type": "string" + }, + "message": { + "type": "string" + } + } + } + } + } + } + }, + "x-webhook": { + "contentType": "application/json", + "eventSchema": { + "$ref": "#/components/schemas/Event" + }, + "headers": { + "Signal-Node-Timestamp": "Unix seconds", + "Signal-Node-Signature": "v1= lowercase hex HMAC-SHA256(secret, timestamp + . + exact body bytes)", + "Signal-Node-Event-Id": "Event.id" + }, + "acknowledgement": "Any 2xx response confirms the application durably accepted the event. Redirects are refused." + } +} diff --git a/docs/integration-status.md b/docs/integration-status.md new file mode 100644 index 0000000..5316809 --- /dev/null +++ b/docs/integration-status.md @@ -0,0 +1,96 @@ +# Signal service integration status + +The service is private and unmerged. Local verification does not establish live +WhatsApp, hosted transport, deployment or package availability. This Connection +candidate preserves rename source `61209057729b002a2122b898d744a8813a702fc7` and +implementation PR [#1](https://github.com/polymorfa/signal-node/pull/1) target +`4d9694eaf97deca34c78990d1293b2b533e19d17`. Its existing PR +[#3](https://github.com/polymorfa/signal-node/pull/3) targets +`feat/signal-service-completion-20261014`, not `main`. Attribution PR #2 is separate. + +## Producer and client ABI + +| Boundary | Current source and contract | +| --- | --- | +| Signal core | `wacore`, `wacore-binary` and `waproto` use oxidezap/whatsapp-rust `2671430db6161c01c507587d0e3c90dcf153aa85`. | +| Record store and Signal operations | Workspace crates under `crates/recordstore` and `crates/signal-ops`, preserved from target4d9694e. Their cryptographic sessions, trust/cancellation/prekey behavior and storage domains are not account-resource aliases. | +| Toolchain and native checks | Rust1.95.0; accepted workspace/lock, CI matrix and Docker inputs remain. | +| Runner protocol | `proto/polymorfa/signal/v1/signal_service.proto`, protocol1 and subprotocol `polymorfa.signal.v1`. Account symbols change; numeric tags, types, enum values and oneof positions remain. | +| Companion identity | Accepted target adds `Receive.device_identity` tag10 and `Bundle.device_identity` tag8. Bytes are forwarded to existing verification; they are not synthesized or substituted from the service's own identity. | +| Application API | `docs/app-api.openapi.json`; `/v1/connections`, `connection_id` events/filter and canonical account errors. Actual phone and per-peer Signal session fields retain their meanings. | +| Configuration | `polymorfa.connections`, `SIGNAL_NODE_CONNECTIONS`; stable configured selector values and local lease generations retain their values. | + +Account messages are `ClaimConnection`, `ReleaseConnection`, `ConnectionClaimed` +and `ConnectionEvent`. The complete descriptor matches target4d9694e after only +the exact account-name mapping, including the accepted companion additions. +Two native wire regressions pin lease/event tags and companion byte fields. +The unavailable-account enum remains numeric8. No account compatibility alias, +retired HTTP route or permissive old event-filter reader is added. + +Signal ratchets, `Namespace::Session`, upstream Signal errors, +`peer_data_request_session_id`, stored record keys, sealing domains, IDs and +fences retain their meanings and bytes. Retry-receipt bundles establish a fresh +Signal session, not a fresh account Connection. Application event IDs retain the +accepted target's domain and length-prefixed account selector, so an ACK cannot +complete another Connection's event. + +## Companion producer dependencies + +The pinned backend HyperMeow `c858e73f841a` does not carry these identity bytes +through `RemoteReceive`, and its existing `prekey.Bundle` conversion does not +supply the companion identity. Regeneration alone cannot populate them. + +Existing counterpart [HyperMeow #69](https://github.com/polymorfa/hypermeow/pull/69), +head `4792d8dfc24c793711dd563079bba8fae787ec98`, adds +`RemoteReceive.DeviceIdentity` for prekey messages and +`FetchPreKeysWithIdentityNoError` / `FetchedPreKey.DeviceIdentity`. Existing bundle +APIs stay unchanged; its shadow relay still has no identity bytes. This source +has a separate human-maintainer and native/provider acceptance boundary. + +The existing backend counterpart is [Polymorfa #1037](https://github.com/polymorfa/polymorfa/pull/1037), +source `e8ab3e02b00f76b63c6918232e3ca7c6ddd8d6c0`, supplied by the family owner. +It carries the narrow HyperMeow companion producer, receive/bundle projections, +WebSocket regression and generated bindings. Root owns preserved integration into +[Backend #1022](https://github.com/polymorfa/polymorfa/pull/1022) and regeneration +against this exact final Signal proto. No monorepo/library pin, binding or runtime +producer was edited in this service lane. Consumer composition remains pending; +this document does not claim that the published backend already carries it. + +Cellar shipped-client evidence is bundle `whatsapp-1054794655`, indexed2026-11-16, +with zero chunk/codegen failures and5719 unresolved dependencies. +`WAWebMsgProcessingDecryptApi.js:24-39` passes received device identity into +companion verification before decryption. `WAWebAdvSignatureApi.js:42-59,80-91` +checks account/device signatures and selects the prekey verification path. +These are shipped-client observations, not current server or live-provider +acceptance. The service retains the target's verification and refusal gates; +missing data does not justify bypassing them. + +## Verification and availability + +The preserved candidate passes178 workspace/all-target native tests with zero +failed or ignored cases, including two new binary contract regressions. Tests +use a dedicated real PostgreSQL16.14 instance, real SQLite, sealed state and +actual synthetic-device Signal crypto with a framed fixture peer. Authentication, +Origin rejection, canonical routes, rejected old filters, claim/generation fences, +trust/cancellation, consent, prekey batch recovery and ACK isolation remain tested. +Strict workspace/all-target Clippy, formatting and Buf lint/build pass. + +The actual debug binary reports `signal-node/0.1.0`. Release compilation is not +attempted at the observed1.6GiB free disk. Docker runtime is unavailable locally; +container/TLS/native provider acceptance remains hosted. Official MBX Cargo is +used without manual cache, target, profile, cap or authentication changes. +Automatic MBX collection is host-managed and remains in the command receipts. + +The customer API, six SDKs, CLI, integrations, Inspector, Console, Connect, +Admin, docs and Help Center remain in the same coordinated family. This producer +adds no eligibility, enrollment, feature flag, metering/financial authority, +QuickLink/browser tunnel grant, staff authority or hosted history access. Existing +app-state/history opt-ins and customer custody boundaries remain. Source API date +and public-spec availability are owned by the family release record. + +Real pairing, send/receive/media/history/group/retry and retained/signed-state +acceptance remain separate gates. Private AGPL/legal review, human notice/approval, +exact merged source cohorts and owner publication authority remain. No merge, +deployment, publication, provider configuration or main-only cutover occurs here. +Rollback must preserve sealed/device/prekey state, event identity and fences; +never reset keys or weaken verification to fit an older consumer. diff --git a/docs/review-followup-2026-10-27.md b/docs/review-followup-2026-10-27.md new file mode 100644 index 0000000..b1c754e --- /dev/null +++ b/docs/review-followup-2026-10-27.md @@ -0,0 +1,42 @@ +# Review follow-up, 2026-10-27 + +This source batch follows `4312d82ae5811cd1b94861a530a7864a4596e576` in PR #1. It does not establish a deployed service or live WhatsApp session. + +| Review remark | Change | Behavioral evidence | +| --- | --- | --- | +| Codex and CodeRabbit: SQLite cancellation leaves a transaction open | Own the pooled connection before `BEGIN IMMEDIATE`; detach and close it on cancellation or an unconfirmed commit/rollback | Cancellation regression first exposed an uncommitted row through the pool; it now rolls back and permits an independent writer. A deferred foreign-key commit failure also rolls back before reuse. | +| CodeRabbit: timeout is an unknown send outcome | Return `504 send_unconfirmed` and the original `error.message_id` after a queued send loses its answer | Actual HTTP/WebSocket tests cover timeout and disconnect after accepting one stanza, with the returned ID matching the stanza and no retry. | +| CodeRabbit: sender-key bookkeeping failure must not turn an accepted send into an error | Preserve the runner-confirmed result and log metadata only; fence loss still retires the old session | Actual group send with SQLite trigger failure and a durable generation change returns `200`, sends once and invalidates the superseded session. | +| CodeRabbit: message context is not application content | Remove context only from the content probe | Real encrypted protocol-only shares with context require no application ACK; mixed messages retain their text and context. | +| CodeRabbit: remove per-scope receipt directories | Close and remove the synthetic receipt after its durable write and ACK | SQLite framed upload/receive recovery test passes with cleanup. | +| CodeRabbit review-body suggestion: checkout credentials | Set `persist-credentials: false` on both checkout steps | Workflow diff; hosted workflow execution remains required. | +| CodeRabbit review-body suggestion: count matching SSE subscribers | Use channels for the exact session filter and all-session subscribers; prune channels without receivers | Library and actual HTTP/encrypted-receive tests refuse unwatched sessions immediately, while matching and unfiltered ACK paths pass. | + +Native local checks use Rust 1.94.0 under the shared `run.py` resource lease. The all-target harness reports 50 passing cases; two PostgreSQL-gated cases return without running because the local fixture URL is unset, so 48 cases execute locally. Hosted CI supplies PostgreSQL and remains a separate gate. No resource bound, assertion or timeout was raised. + +Shipped-client evidence was refreshed to Cellar bundle `whatsapp-1051710191`. `modules/WAWebSendGroupSkmsgJob.js:188-205` receives/parses the send ACK before recording sender-key distribution and handling participant-hash changes. `modules/WAWebDeviceSentMessageProtoUtils.js:14-29` treats message context as accompanying metadata when wrapping a device-sent message. These sources support the ordering and context distinction; they do not establish server acceptance or exactly-once delivery. The explicit unconfirmed HTTP result is this service's contract. + +Owner decisions: address valid bot remarks and make staging available for the owner's team/project. Agent decisions: retain the existing PR, preserve the pinned Signal core, keep the original message identity on uncertainty, and apply the smallest fixes above. Open items: exact staging team/project, deployment source, API version selection, release/runtime policy and enrollment, service hosting and real-device acceptance. Production controls and package publication are outside the staging request. + +The local application HTTP API, README and OpenAPI change together. No Signal wire field, core storage ABI, crypto algorithm, runner, Temporal, platform API, Admin API/MCP/UI, Console, Inspector, SDK/CLI source, billing meter, analytics event, retained content policy or production release control changes in this batch. The repository SDKs use the platform APIs and do not call this private sidecar endpoint. Integrators of the sidecar must handle the documented unconfirmed result without automatic resends. + +Rollback before deployment can revert this source batch. After deployment, preserve sealed records, durable lease generations, pending prekey batches and application receipts. A rollback must not turn an unconfirmed send into a new message. Staging test access and customer release have separate receipts. + +Session: T3 `319f8994-cdc7-4427-b681-cb437bbb9da2`, Codex `gpt-6.1-sol`, existing registration polymorfa/polymorfa#511. The [selected visible checkpoint](https://github.com/polymorfa/polymorfa/blob/3f52123a64715db4cd9486087d39e78fcd400bd0/internal/agent-sessions/sessions/purpshell-t3-codex/signal-review-staging-20261027/1df203a245094aa0c2c800cb4f4f653773d1559d27766f5ca8b3d1fa9dae2e32.md) is committed in the monorepo at `3f52123a64715db4cd9486087d39e78fcd400bd0`. It records nine selected entries, summarized tool evidence, checkpoint timestamps and manual capture gaps. It excludes authentication URLs/screens and hidden reasoning. No automated review was requested. Linear lookup remains inaccessible; the existing Notion feature records were read with their truncation limits. + +## Mismatch resend follow-up + +Two new findings on `f3d6a8c8fe4d6b76021fe08227e72d16dc566fb8` were valid. + +| Review thread | Finding and repair | Behavioral evidence | +| --- | --- | --- | +| `PRRT_kwDOVETUrM6wf9zO` | Failure after the first mismatch ACK escaped as an ordinary error. Convert every failure in that resend attempt, including preparation and pre-queue failures, to `504 send_unconfirmed` with the original ID. Update README and OpenAPI. | HTTP/WebSocket regression first returned `502 send_failed` without the ID. It now returns the same ID for routing disconnect, runner refusal and a lost resend reply. One or two observed stanzas, as appropriate, all retain that ID; no third send occurs. | +| `PRRT_kwDOVETUrM6wf9zP` | A distribution bookkeeping error suppressed the mismatch resend. Continue after ordinary storage errors; only fence loss returns the confirmed result early and retires the session. | SQLite trigger and durable generation-change tests cover both errors with and without a mismatch. Ordinary storage failure plus mismatch first sent once instead of twice; it now resends once under the original ID. Fence loss sends once and invalidates the old holder. | + +Rust 1.94.0 validation through `run.py` reports 51 passing all-target cases, including 17 actual HTTP/WebSocket cases. Two PostgreSQL-gated cases return without running because the local fixture URL is unset, so 49 cases execute locally. Strict all-target Clippy, formatting, zero doctests, diff checks, OpenAPI JSON and local references pass at the standard resource bounds. Hosted checks for the new head remain required. + +Cellar bundle `whatsapp-1051710191`, `modules/WAWebSendGroupSkmsgJob.js:188-205`, confirms the ACK precedes sender-key bookkeeping and mismatch handling. Lines 209-224 catch a resend failure while preserving the original message record. This supports the ordering; the service's explicit `send_unconfirmed` response remains its own contract. Live server acceptance is unverified. + +The existing owner decisions, impact assessment and rollback rules above still apply. No core repin or wire/storage change is included. Rust #5 remains blocked on four CodSpeed upload `401 Unauthorized` results and E2E workflow validation at its empty `BARTENDER_GHCR_TOKEN` password. The repository secret list is empty; organization-level secret availability was not independently inspected. Preserve the authorized order: green Rust #5, then service repin and green service #1. No registry publication or deployment occurred. + +The [selected resend checkpoint](https://github.com/polymorfa/polymorfa/blob/b94794a9e9d0992ffcbc7c4b541ac8c89874f858/internal/agent-sessions/sessions/purpshell-t3-codex/signal-resend-followup-20261027/f85e98ab600c7c2d0e807e45d6b27d222603d15964b83dd9a09b3b3107daebf6.md), evidence commit `b94794a9e9d0992ffcbc7c4b541ac8c89874f858`, records nine selected entries with manual capture gaps and checkpoint timestamps. Registration and T3 thread are unchanged. No automated review was requested. diff --git a/internal/agent-sessions/sessions/purpshell-t3-codex/connections-rename-20261114/2026-11-17-signal-target-merge.md b/internal/agent-sessions/sessions/purpshell-t3-codex/connections-rename-20261114/2026-11-17-signal-target-merge.md new file mode 100644 index 0000000..ecd4703 --- /dev/null +++ b/internal/agent-sessions/sessions/purpshell-t3-codex/connections-rename-20261114/2026-11-17-signal-target-merge.md @@ -0,0 +1,61 @@ +# Signal Connection target merge + +Registration511. Rajeh Taher, Codex/T3 Code/OpenAI/gpt-6.1-sol/xhigh, +writer on rajeh/connection-rename-signal-node. No new delegate or checkout. +Selected visible evidence is manual; complete event/tool/timestamp/attachment +capture has gaps. Thread ID is not exposed. No credentials, customer content, +authentication screens or hidden reasoning are included. + +The user assigned existing Signal PR3 conflict repair in the task-owned checkout. +The clean accepted head61209057729b002a2122b898d744a8813a702fc7 is preserve-merged +with actual target4d9694eaf97deca34c78990d1293b2b533e19d17 on +feat/signal-service-completion-20261014. Target main is not substituted. The +three conflicted files are the proto and two boundary-test files. Accepted +workspace crates, lock, toolchain, Docker/workflow inputs and all target authors +remain intact. New account event-ID/trace/test bindings use Connection while +preserving the target's exact hash domain, selector bytes and ACK isolation. +Target trust/vouch, cancellation, refusal, companion and prekey behavior remains. + +Full descriptors compare equal to target4d after only the exact account-symbol +mapping:135 top-level fields, all tags/types/enum numbers/oneofs and protocol1. +Receive.device_identity retains tag10; Bundle.device_identity retains tag8. +Proto SHA256 is1bbda926d7e3ae251bcb18abe14bc516c795a670b7cad65c2aacea0c7f1e118a. +Two native wire regressions pin claims/events and additive opaque identity bytes. +Signal cryptographic sessions, Namespace::Session, upstream errors, stored keys, +sealing domains and peer_data_request_session_id retain their meanings/bytes. +The retry-bundle comment correctly refers to a fresh Signal session. + +Official /Users/purpshell/Library/Application Support/mbx/bin/cargo is the only +Cargo route used, version1.95.0. All178 workspace/all-target tests pass with zero +failed/ignored cases and a dedicated real PostgreSQL16.14 fixture, plus real +SQLite/crypto and framed synthetic peer. Final strict workspace Clippy, formatting, +Buf lint/build and the workspace documentation-test command pass; docs contain +zero executable examples. The debug binary reports signal-node/0.1.0. +Owned PostgreSQL PID31614 is stopped. All own local commands are terminal. + +Disk fell to117MiB. No further build/docs/Cargo/PG process is started. Release +build is unrun for capacity; Docker runtime is unavailable locally. These are +genuine remaining gates, not replaced by tests. Official MBX automatic collection +appears in logs; no manual shared-cache/target/profile/cap/auth change occurred. + +Current README/integration-status now identify the actual accepted local crates, +oxidezap2671430d source, Rust1.95 and client ABI dependencies. Existing HyperMeow +PR69 head4792d8dfc24c793711dd563079bba8fae787ec98 and backend PR1037 sourcee8ab3e02 +are counterparts. The current old Go producer lacks receive/bundle companion +identity propagation. Root owns exact integration and regeneration in Backend1022; +no monorepo, library pin or binding is edited here. Missing identity is not filled +with an empty/own identity and verification is never weakened. + +Cellar latest stored1054794655 is checked: zero parse/codegen failures and5719 +unresolved dependencies. WAWebMsgProcessingDecryptApi24-39 and +WAWebAdvSignatureApi42-59/80-91 show received companion identity verification. +This is shipped-client evidence, not current server acceptance. Full paths/hashes +and owner/counterpart links are retained in the external source receipt. + +Four application heads remain untouched. No live Signal credentials, real pairing, +provider/browser action, CI cancellation/rerun, merge/deploy/publication or legal/ +main-only action. Preserve own executing jobs before a qualified PR3 push and +immediately link it. Current-head Codex correctness/security and available scoped +CodeRabbit are authorized; old/unavailable reviews are never counted as a pass. +Family readiness, native/retained/product, human-maintainer, legal approval/notice, +source/API-date and publication gates remain. Child never watches PRs. diff --git a/migrations/postgres/0001_records_and_leases.sql b/migrations/postgres/0001_records_and_leases.sql new file mode 100644 index 0000000..b52c8c7 --- /dev/null +++ b/migrations/postgres/0001_records_and_leases.sql @@ -0,0 +1,22 @@ +-- SPDX-License-Identifier: AGPL-3.0-only +-- Sealed Signal records and per-session leases for signal-node. + +-- One row per session (scope). The generation only ever increases; a record +-- write applies only while its fence generation equals this row's. +CREATE TABLE signal_leases ( + scope TEXT PRIMARY KEY, + -- Empty after a release. + holder TEXT NOT NULL, + generation BIGINT NOT NULL CHECK (generation > 0), + expires_at_ms BIGINT NOT NULL +); + +-- Values are sealed (AES-256-GCM) before they reach the database. Keys are +-- bytes so they can hold any character and sort in byte order. +CREATE TABLE signal_records ( + scope TEXT NOT NULL, + ns TEXT NOT NULL, + key BYTEA NOT NULL, + value BYTEA NOT NULL, + PRIMARY KEY (scope, ns, key) +); diff --git a/migrations/sqlite/0001_records_and_leases.sql b/migrations/sqlite/0001_records_and_leases.sql new file mode 100644 index 0000000..4e9df7b --- /dev/null +++ b/migrations/sqlite/0001_records_and_leases.sql @@ -0,0 +1,22 @@ +-- SPDX-License-Identifier: AGPL-3.0-only +-- Sealed Signal records and per-session leases for signal-node. + +-- One row per session (scope). The generation only ever increases; a record +-- write applies only while its fence generation equals this row's. +CREATE TABLE signal_leases ( + scope TEXT PRIMARY KEY NOT NULL, + -- Empty after a release. + holder TEXT NOT NULL, + generation INTEGER NOT NULL CHECK (generation > 0), + expires_at_ms INTEGER NOT NULL +) STRICT; + +-- Values are sealed (AES-256-GCM) before they reach the database. Keys are +-- BLOBs so they can hold any character and sort in byte order. +CREATE TABLE signal_records ( + scope TEXT NOT NULL, + ns TEXT NOT NULL, + key BLOB NOT NULL, + value BLOB NOT NULL, + PRIMARY KEY (scope, ns, key) +) STRICT, WITHOUT ROWID; diff --git a/proto/polymorfa/signal/v1/signal_service.proto b/proto/polymorfa/signal/v1/signal_service.proto index 7985817..fe642a7 100644 --- a/proto/polymorfa/signal/v1/signal_service.proto +++ b/proto/polymorfa/signal/v1/signal_service.proto @@ -1,5 +1,5 @@ // Contract between Polymorfa's WhatsApp runner and a customer-run Signal -// service for zero-knowledge (ZK) sessions. +// service for zero-knowledge (ZK) connections. // // The Signal service holds a linked device's Signal private keys, sessions // and message plaintext. Polymorfa keeps the WhatsApp connection and asks the @@ -13,18 +13,18 @@ // Authorization header. Polymorfa never connects to the service. // - Each binary WebSocket message is exactly one Frame. // - Both sides send requests. A request carries an id unique per sender per -// connection; its response or Error carries the same id. Requests may be +// WebSocket link; its response or Error carries the same id. Requests may be // answered out of order. -// - One connection serves any number of sessions. Every session-scoped -// request names the session and the lease generation it was sent under. +// - One WebSocket link serves any number of Connections. Every account-scoped +// request names the Connection and the lease generation it was sent under. // // Leases and fencing -// - Before Polymorfa routes a session's operations to a connection, the -// service claims the session with ClaimSession, giving the generation of -// the lease it holds in its own store. Polymorfa routes the session to the +// - Before Polymorfa routes a Connection's operations to a service link, the +// service claims the Connection with ClaimConnection and its stored lease +// generation. Polymorfa routes the Connection to the // highest generation it has accepted and rejects claims and requests at a // lower generation with CODE_NOT_LEASE_HOLDER. -// - While no connection holds a session, the session is paused: Polymorfa +// - While no service link holds a Connection, the Connection is paused: Polymorfa // stops reading messages for it and resumes when a service claims it. // // Versioning @@ -40,7 +40,7 @@ option go_package = "github.com/polymorfa/polymorfa/apps/api/gen/proto/polymorfa // Frame is one WebSocket message. message Frame { // Request id, or the id of the request this frame answers. Zero for - // Hello, Welcome and SessionEvent. + // Hello, Welcome and ConnectionEvent. uint64 id = 1; oneof body { @@ -59,7 +59,7 @@ message Frame { // Either direction. Answers a request that failed. Error error = 8; // Polymorfa → service. Unsolicited; never answered. - SessionEvent session_event = 9; + ConnectionEvent connection_event = 9; } } @@ -74,18 +74,18 @@ message Hello { message Welcome { uint32 protocol_version = 1; - // Project the credential belongs to. Sessions outside it are refused. + // Project the credential belongs to. Connections outside it are refused. string project_id = 2; // The service sends a Ping at least this often; Polymorfa closes a silent // connection after three intervals. uint32 heartbeat_interval_ms = 3; - // Ceiling on SendStanza requests per session per second. + // Ceiling on SendStanza requests per connection per second. uint32 max_sends_per_second = 4; } // RunnerRequest is an operation Polymorfa's runner needs from the service. message RunnerRequest { - string session_id = 1; + string connection_id = 1; uint64 lease_generation = 2; oneof op { @@ -99,6 +99,8 @@ message RunnerRequest { Resend resend = 17; ForgetIdentity forget_identity = 18; MigratePnToLid migrate_pn_to_lid = 19; + PreparePreKeyUpload prepare_pre_key_upload = 20; + AcknowledgePreKeyUpload acknowledge_pre_key_upload = 21; } } @@ -113,19 +115,20 @@ message ServiceResponse { SignedPairing signed_pairing = 15; Stanza stanza = 16; MigrateResult migrate = 17; + PreKeyUploadBatch pre_key_upload_batch = 18; } } // ServiceRequest is something the service needs from Polymorfa, chiefly to // send messages it built. message ServiceRequest { - string session_id = 1; - // Zero for ClaimSession and Ping. + string connection_id = 1; + // Zero for ClaimConnection and Ping. uint64 lease_generation = 2; oneof op { - ClaimSession claim_session = 10; - ReleaseSession release_session = 11; + ClaimConnection claim_connection = 10; + ReleaseConnection release_connection = 11; SendStanza send_stanza = 12; ResolveDevices resolve_devices = 13; FetchPreKeys fetch_pre_keys = 14; @@ -137,7 +140,7 @@ message ServiceRequest { message RunnerResponse { oneof result { Ok ok = 10; - SessionClaimed session_claimed = 11; + ConnectionClaimed connection_claimed = 11; SendResult send = 12; Devices devices = 13; Bundles bundles = 14; @@ -153,7 +156,7 @@ message Error { // A dependency is down (database, WhatsApp connection). Retry later. CODE_UNAVAILABLE = 2; CODE_INVALID_ARGUMENT = 3; - // The request's lease generation is not the current one for the session. + // The request's lease generation is not the current one for the connection. CODE_NOT_LEASE_HOLDER = 4; // Receive only: the payload could not be decrypted. See decrypt_failure. CODE_DECRYPT_FAILED = 5; @@ -161,10 +164,11 @@ message Error { CODE_UNSUPPORTED = 6; // SendStanza exceeded Welcome.max_sends_per_second. CODE_RATE_LIMITED = 7; - // The session is not paired, logged out, or not in this project. - CODE_SESSION_UNAVAILABLE = 8; - // The operation is refused for zero-knowledge sessions (for example a - // Campaign send). + // The connection is not paired, logged out, or not in this project. + CODE_CONNECTION_UNAVAILABLE = 8; + // The operation is refused: for zero-knowledge connections (for example a + // Campaign send), or because a recipient device's identity did not verify + // against its account. CODE_REFUSED = 9; } Code code = 1; @@ -177,7 +181,7 @@ message Error { enum DecryptFailure { // Any other failure; the runner sends a retry receipt. DECRYPT_FAILURE_UNSPECIFIED = 0; - // Older than the session can decrypt; ignore the payload. + // Older than the Signal session can decrypt; ignore the payload. DECRYPT_FAILURE_OLD_COUNTER = 1; // Group message without a sender key. DECRYPT_FAILURE_NO_SENDER_KEY = 2; @@ -209,6 +213,9 @@ message Receive { int64 timestamp = 8; // Sender's alternate JID (PN for a LID sender and vice versa), if known. string sender_alt = 9; + // The stanza's , which a pkmsg from a companion device + // carries: a serialized ADVSignedDeviceIdentity. Empty otherwise. + bytes device_identity = 10; } message ReceiveResult { @@ -221,9 +228,32 @@ message ReceiveResult { } message GeneratePreKeys { + // Fresh keys for retry receipts, not ordinary public upload recovery. + // Integer count 1..812. uint32 count = 1; } +message PreparePreKeyUpload { + // Fresh allocation size 1..812. An outstanding live batch is returned in + // full and original order, even when its size differs from this count. + uint32 count = 1; +} + +message PreKeyUploadBatch { + // Opaque 32-character lowercase hexadecimal token; not an ordering key. + string batch_id = 1; + // Public keys only, 1..812 entries. Consumed original members are omitted. + repeated PreKey pre_keys = 2; +} + +message AcknowledgePreKeyUpload { + string batch_id = 1; + // Unique original members covering every surviving private key. Consumed + // original IDs may remain in an in-flight ACK and are never resurrected. + // The most recent exact ACK remains replayable after restart. + repeated uint32 ids = 2; +} + message PreKeys { repeated PreKey pre_keys = 1; } @@ -235,6 +265,8 @@ message PreKey { } message MarkPreKeysUploaded { + // Fresh retry keys only. Any original pending upload member rejects the + // entire call; use AcknowledgePreKeyUpload for durable upload batches. repeated uint32 ids = 1; } @@ -292,7 +324,7 @@ message Resend { // "pn" or "lid"; empty when not a group. string addressing_mode = 8; uint32 retry_count = 9; - // Keys carried by the retry receipt, to start a fresh session. + // Keys carried by the retry receipt, to start a fresh Signal session. Bundle bundle = 10; } @@ -317,25 +349,25 @@ message MigrateResult { // ─── Service → runner operations ──────────────────────────────────────── -message ClaimSession { +message ClaimConnection { // Generation of the lease the service holds in its own store. Must be // greater than or equal to any generation Polymorfa accepted before. uint64 generation = 1; } -message SessionClaimed { - // Polymorfa's view of the session when the claim was accepted. - SessionEvent.State state = 1; +message ConnectionClaimed { + // Polymorfa's view of the connection when the claim was accepted. + ConnectionEvent.State state = 1; // This device's JIDs once paired. string pn = 2; string lid = 3; } -message ReleaseSession {} +message ReleaseConnection {} // SendStanza transmits a message stanza the service built. Polymorfa checks -// the stanza is a for this session, applies the send ceiling and -// session safety pacing, and waits for the server acknowledgement. +// the stanza is a for this connection, applies the send ceiling and +// connection safety pacing, and waits for the server acknowledgement. message SendStanza { bytes node = 1; // Participant hash the stanza was built for (groups), to report a @@ -376,6 +408,11 @@ message Bundle { bytes signed_pre_key_signature = 6; // Absent when the server had no one-time prekey left. PreKey pre_key = 7; + // The returned with this bundle: a serialized + // ADVSignedDeviceIdentity. Companion devices carry one; empty for a primary + // device. The service verifies a companion against its account's pinned + // identity before encrypting to it. + bytes device_identity = 8; } message ResolveGroup { @@ -394,7 +431,7 @@ message Ping {} // ─── Notifications ────────────────────────────────────────────────────── -message SessionEvent { +message ConnectionEvent { enum State { STATE_UNSPECIFIED = 0; // Waiting for the QR code to be scanned. @@ -402,12 +439,12 @@ message SessionEvent { STATE_CONNECTED = 2; // Reconnecting to WhatsApp. STATE_DISCONNECTED = 3; - // No Signal service holds the session; messages are held by WhatsApp. + // No Signal service holds the connection; messages are held by WhatsApp. STATE_PAUSED = 4; - // Unlinked. The service may delete the session's state. + // Unlinked. The service may delete the connection's state. STATE_LOGGED_OUT = 5; } - string session_id = 1; + string connection_id = 1; State state = 2; string pn = 3; string lid = 4; diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..6360c18 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,3 @@ +[toolchain] +channel = "1.95.0" +components = ["rustfmt", "clippy"] diff --git a/signal-node.example.toml b/signal-node.example.toml new file mode 100644 index 0000000..dd04fd8 --- /dev/null +++ b/signal-node.example.toml @@ -0,0 +1,47 @@ +# Operator example. Replace the URL and connection IDs with eligible real values. +node_id = "customer-node-1" + +[polymorfa] +url = "wss://api.example.invalid/api/zk/signal" +credential_file = "/run/secrets/polymorfa-project-credential" +connections = ["replace-with-connection-id"] +request_timeout_ms = 15000 +reconnect_min_ms = 500 +reconnect_max_ms = 30000 + +[storage] +driver = "sqlite" +url = "sqlite:///var/lib/signal-node/state.sqlite" +master_key_file = "/run/secrets/signal-master-key" +max_connections = 10 +# Multi-host deployments instead use driver = "postgres" and url_file. +# Keep the master key outside the database and consistent across replicas. + +[leases] +ttl_ms = 30000 +renew_interval_ms = 10000 + +[http] +listen = "0.0.0.0:8787" +secret_file = "/run/secrets/signal-application-secret" + +[delivery] +sse = true +ack_timeout_ms = 10000 +# Optional alternative/additional channel; return 2xx only after durable commit. +# webhook_url = "https://customer.example.invalid/signal-events" +# webhook_secret_file = "/run/secrets/signal-webhook-secret" + +[history] +# Explicit operator consent. This never grants backend connection eligibility. +deliver = false + +[app_state] +# Explicit metadata disclosure consent, independent of history delivery. +# Own-device keys let the runner decrypt contact names, labels and chat state. +share_keys = false + +[retention] +decrypt_buffer_hours = 336 +sent_message_hours = 168 +prune_interval_minutes = 60 diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..7624b71 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,773 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +//! Configuration: a TOML file, then `SIGNAL_NODE_*` environment variables on +//! top. Every secret can also be read from a file (`*_file` keys), so it +//! never has to appear in the config file or the process environment. + +use base64::Engine as _; +use serde::Deserialize; +use std::net::SocketAddr; +use std::path::{Path, PathBuf}; +use std::time::Duration; + +#[derive(Debug, thiserror::Error)] +pub enum ConfigError { + #[error("reading {path}: {source}")] + Read { + path: PathBuf, + source: std::io::Error, + }, + #[error("config file is not valid TOML; check field names and value types")] + Parse, + #[error("{0}")] + Invalid(String), +} + +impl From for ConfigError { + fn from(_: toml::de::Error) -> Self { + Self::Parse + } +} + +fn invalid(message: impl Into) -> ConfigError { + ConfigError::Invalid(message.into()) +} + +/// A value that must not be printed. `Debug` shows only whether it is set. +#[derive(Clone, Default, Deserialize, PartialEq, Eq)] +#[serde(transparent)] +pub struct Secret(String); + +impl Secret { + pub fn new(value: impl Into) -> Self { + Self(value.into()) + } + + pub fn expose(&self) -> &str { + &self.0 + } + + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } +} + +impl std::fmt::Debug for Secret { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(if self.0.is_empty() { + "" + } else { + "" + }) + } +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Config { + /// Stable identifier of this node; the lease holder name. Defaults to the + /// host name. + #[serde(default)] + pub node_id: String, + pub polymorfa: PolymorfaConfig, + pub storage: StorageConfig, + #[serde(default)] + pub leases: LeaseConfig, + pub http: HttpConfig, + #[serde(default)] + pub delivery: DeliveryConfig, + #[serde(default)] + pub history: HistoryConfig, + #[serde(default)] + pub app_state: AppStateConfig, + #[serde(default)] + pub retention: RetentionConfig, +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct PolymorfaConfig { + /// WebSocket URL of Polymorfa's Signal service endpoint. + pub url: String, + /// Project credential, sent as `Authorization: Bearer `. + #[serde(default)] + pub credential: Secret, + #[serde(default)] + pub credential_file: Option, + /// Zero-knowledge connection IDs this deployment serves. Every node of a + /// deployment lists the same connections; leases decide which node serves + /// each one. + #[serde(default)] + pub connections: Vec, + /// Longest wait for a response to a request this node sends. + #[serde(default = "default_request_timeout_ms")] + pub request_timeout_ms: u64, + /// Reconnect backoff bounds. + #[serde(default = "default_backoff_min_ms")] + pub reconnect_min_ms: u64, + #[serde(default = "default_backoff_max_ms")] + pub reconnect_max_ms: u64, +} + +#[derive(Debug, Clone, Copy, Deserialize, PartialEq, Eq)] +#[serde(rename_all = "lowercase")] +pub enum StorageDriver { + Postgres, + Sqlite, + /// In-process only. State is lost on restart; for tests and trials. + Memory, +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct StorageConfig { + pub driver: StorageDriver, + /// `postgres://...` or `sqlite://path?mode=rwc`. Unused for `memory`. + #[serde(default)] + pub url: Secret, + #[serde(default)] + pub url_file: Option, + /// Base64 of 32 random bytes. Wraps each connection's data key. + #[serde(default)] + pub master_key: Secret, + #[serde(default)] + pub master_key_file: Option, + #[serde(default = "default_max_connections")] + pub max_connections: u32, +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct LeaseConfig { + /// How long a lease lives without renewal. Failover takes at most this + /// long after a node stops. + #[serde(default = "default_lease_ttl_ms")] + pub ttl_ms: u64, + /// How often leases are renewed and free connections are tried. + #[serde(default = "default_lease_renew_ms")] + pub renew_interval_ms: u64, +} + +impl Default for LeaseConfig { + fn default() -> Self { + Self { + ttl_ms: default_lease_ttl_ms(), + renew_interval_ms: default_lease_renew_ms(), + } + } +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct HttpConfig { + /// Address of the application API. Keep it on a private network. + #[serde(default = "default_listen")] + pub listen: SocketAddr, + /// Bearer secret every application request must present. + #[serde(default)] + pub secret: Secret, + #[serde(default)] + pub secret_file: Option, +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct DeliveryConfig { + /// Signed webhook target. Optional when the application reads the event + /// stream instead. + #[serde(default)] + pub webhook_url: Option, + #[serde(default)] + pub webhook_secret: Secret, + #[serde(default)] + pub webhook_secret_file: Option, + /// Serve `GET /v1/events` as a server-sent-events stream. + #[serde(default = "default_true")] + pub sse: bool, + /// How long a received message waits for the application to acknowledge + /// it before the receive fails and WhatsApp redelivers it later. + #[serde(default = "default_ack_timeout_ms")] + pub ack_timeout_ms: u64, +} + +impl Default for DeliveryConfig { + fn default() -> Self { + Self { + webhook_url: None, + webhook_secret: Secret::default(), + webhook_secret_file: None, + sse: true, + ack_timeout_ms: default_ack_timeout_ms(), + } + } +} + +#[derive(Debug, Clone, Default, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct HistoryConfig { + /// Deliver history-sync descriptors to the application. Off by default: + /// history notifications are decrypted and dropped. Turning this on is + /// the operator's decision and responsibility. + #[serde(default)] + pub deliver: bool, +} + +#[derive(Debug, Clone, Default, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct AppStateConfig { + /// Share own-device app-state keys with the runner. Off by default; + /// enabling this discloses the metadata those keys protect. + #[serde(default)] + pub share_keys: bool, +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct RetentionConfig { + /// Decrypt-buffer entries (sealed) older than this are deleted. + #[serde(default = "default_buffer_hours")] + pub decrypt_buffer_hours: u64, + /// Sent-message records, kept to answer retry receipts. + #[serde(default = "default_sent_hours")] + pub sent_message_hours: u64, + #[serde(default = "default_prune_minutes")] + pub prune_interval_minutes: u64, +} + +impl Default for RetentionConfig { + fn default() -> Self { + Self { + decrypt_buffer_hours: default_buffer_hours(), + sent_message_hours: default_sent_hours(), + prune_interval_minutes: default_prune_minutes(), + } + } +} + +fn default_request_timeout_ms() -> u64 { + 15_000 +} +fn default_backoff_min_ms() -> u64 { + 500 +} +fn default_backoff_max_ms() -> u64 { + 30_000 +} +fn default_max_connections() -> u32 { + 10 +} +fn default_lease_ttl_ms() -> u64 { + 30_000 +} +fn default_lease_renew_ms() -> u64 { + 10_000 +} +fn default_listen() -> SocketAddr { + SocketAddr::from(([127, 0, 0, 1], 8787)) +} +fn default_true() -> bool { + true +} +fn default_ack_timeout_ms() -> u64 { + 10_000 +} +fn default_buffer_hours() -> u64 { + 14 * 24 +} +fn default_sent_hours() -> u64 { + 7 * 24 +} +fn default_prune_minutes() -> u64 { + 60 +} + +/// Settings resolved from the file, environment and secret files. +#[derive(Clone)] +pub struct Resolved { + pub node_id: String, + pub polymorfa_url: String, + pub credential: Secret, + pub connections: Vec, + pub request_timeout: Duration, + pub reconnect_min: Duration, + pub reconnect_max: Duration, + pub storage_driver: StorageDriver, + pub storage_url: Secret, + pub max_connections: u32, + pub master_key: [u8; 32], + pub lease_ttl_ms: u64, + pub lease_renew: Duration, + pub http_listen: SocketAddr, + pub http_secret: Secret, + pub webhook_url: Option, + pub webhook_secret: Secret, + pub sse: bool, + pub ack_timeout: Duration, + pub deliver_history: bool, + pub share_app_state_keys: bool, + pub decrypt_buffer_retention: Duration, + pub sent_message_retention: Duration, + pub prune_interval: Duration, +} + +impl std::fmt::Debug for Resolved { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Resolved") + .field("node_id", &self.node_id) + .field("connections", &self.connections) + .field("storage_driver", &self.storage_driver) + .field("master_key", &"") + .field("credential", &self.credential) + .field("http_secret", &self.http_secret) + .field("webhook_secret", &self.webhook_secret) + .finish_non_exhaustive() + } +} + +/// The environment variables read on top of the file. +pub const ENV_VARS: &[&str] = &[ + "SIGNAL_NODE_ID", + "SIGNAL_NODE_POLYMORFA_URL", + "SIGNAL_NODE_POLYMORFA_CREDENTIAL", + "SIGNAL_NODE_CONNECTIONS", + "SIGNAL_NODE_STORAGE_DRIVER", + "SIGNAL_NODE_STORAGE_URL", + "SIGNAL_NODE_MASTER_KEY", + "SIGNAL_NODE_HTTP_LISTEN", + "SIGNAL_NODE_HTTP_SECRET", + "SIGNAL_NODE_WEBHOOK_URL", + "SIGNAL_NODE_WEBHOOK_SECRET", + "SIGNAL_NODE_HISTORY_DELIVER", + "SIGNAL_NODE_APP_STATE_SHARE_KEYS", +]; + +impl Config { + pub fn from_toml(text: &str) -> Result { + Ok(toml::from_str(text)?) + } + + pub fn load(path: &Path) -> Result { + let text = std::fs::read_to_string(path).map_err(|source| ConfigError::Read { + path: path.to_owned(), + source, + })?; + Self::from_toml(&text) + } + + /// Apply `SIGNAL_NODE_*` overrides read through `env`. + pub fn apply_env(&mut self, env: impl Fn(&str) -> Option) -> Result<(), ConfigError> { + if let Some(v) = env("SIGNAL_NODE_ID") { + self.node_id = v; + } + if let Some(v) = env("SIGNAL_NODE_POLYMORFA_URL") { + self.polymorfa.url = v; + } + if let Some(v) = env("SIGNAL_NODE_POLYMORFA_CREDENTIAL") { + self.polymorfa.credential = Secret::new(v); + } + if let Some(v) = env("SIGNAL_NODE_CONNECTIONS") { + self.polymorfa.connections = v + .split(',') + .map(str::trim) + .filter(|s| !s.is_empty()) + .map(str::to_owned) + .collect(); + } + if let Some(v) = env("SIGNAL_NODE_STORAGE_DRIVER") { + self.storage.driver = match v.as_str() { + "postgres" => StorageDriver::Postgres, + "sqlite" => StorageDriver::Sqlite, + "memory" => StorageDriver::Memory, + other => return Err(invalid(format!("unknown storage driver {other}"))), + }; + } + if let Some(v) = env("SIGNAL_NODE_STORAGE_URL") { + self.storage.url = Secret::new(v); + } + if let Some(v) = env("SIGNAL_NODE_MASTER_KEY") { + self.storage.master_key = Secret::new(v); + } + if let Some(v) = env("SIGNAL_NODE_HTTP_LISTEN") { + self.http.listen = v + .parse() + .map_err(|_| invalid("SIGNAL_NODE_HTTP_LISTEN is not host:port"))?; + } + if let Some(v) = env("SIGNAL_NODE_HTTP_SECRET") { + self.http.secret = Secret::new(v); + } + if let Some(v) = env("SIGNAL_NODE_WEBHOOK_URL") { + self.delivery.webhook_url = Some(v).filter(|u| !u.is_empty()); + } + if let Some(v) = env("SIGNAL_NODE_WEBHOOK_SECRET") { + self.delivery.webhook_secret = Secret::new(v); + } + if let Some(v) = env("SIGNAL_NODE_HISTORY_DELIVER") { + self.history.deliver = matches!(v.as_str(), "1" | "true" | "yes"); + } + if let Some(v) = env("SIGNAL_NODE_APP_STATE_SHARE_KEYS") { + self.app_state.share_keys = match v.as_str() { + "1" | "true" | "yes" => true, + "0" | "false" | "no" => false, + _ => { + return Err(invalid( + "SIGNAL_NODE_APP_STATE_SHARE_KEYS must be true or false", + )); + } + }; + } + Ok(()) + } + + /// Read secret files and check every setting. + pub fn resolve(self) -> Result { + let credential = secret_or_file( + self.polymorfa.credential, + self.polymorfa.credential_file.as_deref(), + "polymorfa.credential", + )?; + if credential.is_empty() { + return Err(invalid("polymorfa.credential is required")); + } + // Fail at startup, not on every handshake: the credential travels as + // `Authorization: Bearer `. + if !credential.expose().bytes().all(|b| b.is_ascii_graphic()) { + return Err(invalid( + "polymorfa.credential must be printable ASCII without spaces", + )); + } + let relay = reqwest::Url::parse(&self.polymorfa.url) + .map_err(|_| invalid("polymorfa.url is invalid"))?; + let local = matches!(relay.host_str(), Some("localhost" | "127.0.0.1" | "[::1]")); + if !(relay.scheme() == "wss" || relay.scheme() == "ws" && local) + || !relay.username().is_empty() + || relay.password().is_some() + || relay.query().is_some() + || relay.fragment().is_some() + { + return Err(invalid( + "polymorfa.url requires wss (ws only on loopback) without credentials, query or fragment", + )); + } + if let Some(target) = &self.delivery.webhook_url { + let url = reqwest::Url::parse(target) + .map_err(|_| invalid("delivery.webhook_url is invalid"))?; + let local = matches!(url.host_str(), Some("localhost" | "127.0.0.1" | "[::1]")); + if !(url.scheme() == "https" || url.scheme() == "http" && local) + || !url.username().is_empty() + || url.password().is_some() + || url.fragment().is_some() + { + return Err(invalid( + "delivery.webhook_url requires HTTPS (HTTP only on loopback) without URL credentials or fragment", + )); + } + } + if self.polymorfa.request_timeout_ms == 0 + || self.polymorfa.reconnect_min_ms == 0 + || self.polymorfa.reconnect_max_ms < self.polymorfa.reconnect_min_ms + || self.leases.ttl_ms == 0 + || self.leases.renew_interval_ms == 0 + || self.delivery.ack_timeout_ms == 0 + { + return Err(invalid( + "timeouts and lease intervals must be positive and reconnect bounds ordered", + )); + } + let storage_url = secret_or_file( + self.storage.url, + self.storage.url_file.as_deref(), + "storage.url", + )?; + if self.storage.driver != StorageDriver::Memory && storage_url.is_empty() { + return Err(invalid("storage.url is required for this driver")); + } + let master_key = secret_or_file( + self.storage.master_key, + self.storage.master_key_file.as_deref(), + "storage.master_key", + )?; + let master_key = decode_master_key(master_key.expose())?; + let http_secret = secret_or_file( + self.http.secret, + self.http.secret_file.as_deref(), + "http.secret", + )?; + if http_secret.expose().len() < 16 + || !http_secret + .expose() + .bytes() + .all(|b| (33..=126).contains(&b)) + { + return Err(invalid( + "http.secret must contain at least 16 visible ASCII characters without spaces", + )); + } + let webhook_secret = secret_or_file( + self.delivery.webhook_secret, + self.delivery.webhook_secret_file.as_deref(), + "delivery.webhook_secret", + )?; + if self.delivery.webhook_url.is_some() && webhook_secret.expose().len() < 16 { + return Err(invalid( + "delivery.webhook_secret (16 characters or more) is required with a webhook_url", + )); + } + if self.delivery.webhook_url.is_none() && !self.delivery.sse { + return Err(invalid( + "set delivery.webhook_url or enable delivery.sse; messages need somewhere to go", + )); + } + if self.leases.renew_interval_ms > self.leases.ttl_ms / 2 { + return Err(invalid( + "leases.renew_interval_ms must be at most half of leases.ttl_ms", + )); + } + for id in &self.polymorfa.connections { + if id.is_empty() || id.len() > 200 { + return Err(invalid("connection IDs must be 1 to 200 characters")); + } + } + let node_id = if self.node_id.is_empty() { + default_node_id() + } else { + self.node_id + }; + let buffer_seconds = self + .retention + .decrypt_buffer_hours + .checked_mul(3600) + .ok_or_else(|| invalid("decrypt buffer retention is too large"))?; + let sent_seconds = self + .retention + .sent_message_hours + .checked_mul(3600) + .ok_or_else(|| invalid("sent message retention is too large"))?; + let prune_seconds = self + .retention + .prune_interval_minutes + .max(1) + .checked_mul(60) + .ok_or_else(|| invalid("prune interval is too large"))?; + Ok(Resolved { + node_id, + polymorfa_url: self.polymorfa.url, + credential, + connections: self.polymorfa.connections, + request_timeout: Duration::from_millis(self.polymorfa.request_timeout_ms), + reconnect_min: Duration::from_millis(self.polymorfa.reconnect_min_ms), + reconnect_max: Duration::from_millis(self.polymorfa.reconnect_max_ms), + storage_driver: self.storage.driver, + storage_url, + max_connections: self.storage.max_connections.max(1), + master_key, + lease_ttl_ms: self.leases.ttl_ms, + lease_renew: Duration::from_millis(self.leases.renew_interval_ms), + http_listen: self.http.listen, + http_secret, + webhook_url: self.delivery.webhook_url, + webhook_secret, + sse: self.delivery.sse, + ack_timeout: Duration::from_millis(self.delivery.ack_timeout_ms), + deliver_history: self.history.deliver, + share_app_state_keys: self.app_state.share_keys, + decrypt_buffer_retention: Duration::from_secs(buffer_seconds), + sent_message_retention: Duration::from_secs(sent_seconds), + prune_interval: Duration::from_secs(prune_seconds), + }) + } +} + +fn secret_or_file(inline: Secret, file: Option<&Path>, name: &str) -> Result { + match (inline.is_empty(), file) { + (false, Some(_)) => Err(invalid(format!("set {name} or {name}_file, not both"))), + (true, Some(path)) => { + let text = std::fs::read_to_string(path).map_err(|source| ConfigError::Read { + path: path.to_owned(), + source, + })?; + Ok(Secret::new(text.trim())) + } + (_, None) => Ok(inline), + } +} + +fn decode_master_key(text: &str) -> Result<[u8; 32], ConfigError> { + if text.is_empty() { + return Err(invalid( + "storage.master_key is required (base64 of 32 random bytes)", + )); + } + let bytes = base64::engine::general_purpose::STANDARD + .decode(text.trim()) + .map_err(|_| invalid("storage.master_key is not valid base64"))?; + bytes + .try_into() + .map_err(|_| invalid("storage.master_key must decode to exactly 32 bytes")) +} + +fn default_node_id() -> String { + std::env::var("HOSTNAME") + .ok() + .filter(|h| !h.is_empty()) + .unwrap_or_else(|| format!("node-{:08x}", rand::random::())) +} + +#[cfg(test)] +mod tests { + use super::*; + + const MINIMAL: &str = r#" + [polymorfa] + url = "wss://example.invalid/signal" + credential = "pk_test_credential" + connections = ["sess_a", "sess_b"] + + [storage] + driver = "memory" + master_key = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" + + [http] + secret = "local-secret-0123456789" + "#; + + #[test] + fn a_minimal_file_resolves_with_defaults() { + let resolved = Config::from_toml(MINIMAL).unwrap().resolve().unwrap(); + assert_eq!(resolved.connections, ["sess_a", "sess_b"]); + assert_eq!(resolved.lease_ttl_ms, 30_000); + assert!(!resolved.deliver_history, "history must be off by default"); + assert!( + !resolved.share_app_state_keys, + "app-state keys must stay local by default" + ); + assert!(resolved.sse); + assert_eq!(resolved.master_key, [0; 32]); + } + + #[test] + fn a_credential_that_cannot_form_a_header_is_rejected_at_startup() { + for bad in ["pk_test\ncredential", "pk test", "pk_tést"] { + let mut config = Config::from_toml(MINIMAL).unwrap(); + config + .apply_env(|name| (name == "SIGNAL_NODE_POLYMORFA_CREDENTIAL").then(|| bad.into())) + .unwrap(); + let err = config.resolve().unwrap_err().to_string(); + assert!(err.contains("polymorfa.credential"), "{bad:?}: {err}"); + } + } + + #[test] + fn environment_overrides_the_file() { + let mut config = Config::from_toml(MINIMAL).unwrap(); + config + .apply_env(|name| match name { + "SIGNAL_NODE_CONNECTIONS" => Some("one, two".into()), + "SIGNAL_NODE_HISTORY_DELIVER" => Some("true".into()), + "SIGNAL_NODE_ID" => Some("node-9".into()), + _ => None, + }) + .unwrap(); + let resolved = config.resolve().unwrap(); + assert_eq!(resolved.connections, ["one", "two"]); + assert!(resolved.deliver_history); + assert_eq!(resolved.node_id, "node-9"); + } + + #[test] + fn app_state_consent_is_independent_and_environment_can_revoke_it() { + let file = format!("{MINIMAL}\n[app_state]\nshare_keys = true\n"); + let config = Config::from_toml(&file).unwrap(); + let resolved = config.clone().resolve().unwrap(); + assert!(resolved.share_app_state_keys); + assert!(!resolved.deliver_history); + for (value, enabled) in [ + ("1", true), + ("true", true), + ("yes", true), + ("0", false), + ("false", false), + ("no", false), + ] { + let mut config = config.clone(); + config + .apply_env(|name| { + (name == "SIGNAL_NODE_APP_STATE_SHARE_KEYS").then(|| value.into()) + }) + .unwrap(); + assert_eq!(config.resolve().unwrap().share_app_state_keys, enabled); + } + let mut config = Config::from_toml(MINIMAL).unwrap(); + config + .apply_env(|name| (name == "SIGNAL_NODE_HISTORY_DELIVER").then(|| "true".into())) + .unwrap(); + let resolved = config.resolve().unwrap(); + assert!(resolved.deliver_history); + assert!(!resolved.share_app_state_keys); + assert!(ENV_VARS.contains(&"SIGNAL_NODE_APP_STATE_SHARE_KEYS")); + for value in ["", "TRUE", "unexpected-secret-value"] { + let mut config = Config::from_toml(MINIMAL).unwrap(); + let error = config + .apply_env(|name| { + (name == "SIGNAL_NODE_APP_STATE_SHARE_KEYS").then(|| value.into()) + }) + .unwrap_err(); + assert_eq!( + error.to_string(), + "SIGNAL_NODE_APP_STATE_SHARE_KEYS must be true or false" + ); + assert!(!config.app_state.share_keys); + } + } + + #[test] + fn secrets_never_print() { + let resolved = Config::from_toml(MINIMAL).unwrap().resolve().unwrap(); + let printed = format!("{resolved:?}"); + assert!(!printed.contains("pk_test_credential")); + assert!(!printed.contains("local-secret")); + } + + #[test] + fn bad_values_are_refused() { + let short_key = MINIMAL.replace("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=", "AAAA"); + assert!(Config::from_toml(&short_key).unwrap().resolve().is_err()); + let http_url = MINIMAL.replace("wss://", "https://"); + assert!(Config::from_toml(&http_url).unwrap().resolve().is_err()); + let weak = MINIMAL.replace("local-secret-0123456789", "short"); + assert!(Config::from_toml(&weak).unwrap().resolve().is_err()); + let mut no_sink = Config::from_toml(MINIMAL).unwrap(); + no_sink.delivery.sse = false; + assert!(no_sink.resolve().is_err()); + } + #[test] + fn configuration_refuses_unsafe_urls_and_overflows_without_echoing_secrets() { + for url in [ + "ws://example.invalid/signal", + "wss://secret@example.invalid/signal", + "wss://example.invalid/signal?token=secret", + "wss://example.invalid/signal#secret", + ] { + let mut config = Config::from_toml(MINIMAL).unwrap(); + config.polymorfa.url = url.into(); + let error = config.resolve().unwrap_err().to_string(); + assert!(!error.contains("secret")); + } + let error = Config::from_toml("http = secret-material-not-toml").unwrap_err(); + assert!(!format!("{error:?} {error}").contains("secret-material")); + for secret in [ + "non-ascii-çççççççç", + "line-break-secret\n", + "space-in secret-value", + ] { + let mut config = Config::from_toml(MINIMAL).unwrap(); + config.http.secret = Secret::new(secret); + assert!(config.resolve().is_err()); + } + let mut config = Config::from_toml(MINIMAL).unwrap(); + config.retention.decrypt_buffer_hours = u64::MAX; + assert!(config.resolve().is_err()); + let mut config = Config::from_toml(MINIMAL).unwrap(); + config.leases.renew_interval_ms = u64::MAX; + assert!(config.resolve().is_err()); + } +} diff --git a/src/delivery.rs b/src/delivery.rs new file mode 100644 index 0000000..fa52fff --- /dev/null +++ b/src/delivery.rs @@ -0,0 +1,391 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +//! Delivery of decrypted messages to the customer's application. +//! +//! An event counts as delivered when the application acknowledges it: a 2xx +//! answer to the signed webhook, or `POST /v1/events/{id}/ack` after reading +//! it from the event stream. With both configured, the first +//! acknowledgement wins. The receive that produced the event waits for that +//! acknowledgement; if none arrives within the timeout the receive fails, +//! Polymorfa does not send the delivery receipt, and the same ciphertext +//! comes back later and is served again from the sealed decrypt buffer. + +use crate::message::Event; +use hmac::{Hmac, Mac}; +use sha2::Sha256; +use std::collections::HashMap; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Arc, Mutex as StdMutex}; +use std::time::Duration; +use tokio::sync::{Mutex, broadcast, oneshot}; + +/// Webhook headers. +pub const SIGNATURE_HEADER: &str = "Signal-Node-Signature"; +pub const TIMESTAMP_HEADER: &str = "Signal-Node-Timestamp"; +pub const EVENT_ID_HEADER: &str = "Signal-Node-Event-Id"; + +const WEBHOOK_TIMEOUT: Duration = Duration::from_secs(10); +const STREAM_BUFFER: usize = 1024; + +#[derive(Debug, thiserror::Error, PartialEq, Eq)] +pub enum DeliveryError { + #[error("no application is listening: no webhook configured and no event-stream client")] + NoListener, + #[error("the application did not acknowledge the event in time")] + NotAcknowledged, + #[error("the webhook answered with status {0}")] + WebhookRejected(u16), + #[error("the webhook could not be reached")] + WebhookUnreachable, +} + +/// `v1=`. +pub fn sign(secret: &[u8], timestamp: i64, body: &[u8]) -> String { + let mut mac = Hmac::::new_from_slice(secret).expect("HMAC accepts any key length"); + mac.update(timestamp.to_string().as_bytes()); + mac.update(b"."); + mac.update(body); + format!("v1={}", hex::encode(mac.finalize().into_bytes())) +} + +/// Check a signature in constant time. Applications can copy this logic. +pub fn verify(secret: &[u8], timestamp: i64, body: &[u8], signature: &str) -> bool { + let Some(hex_mac) = signature.strip_prefix("v1=") else { + return false; + }; + let Ok(expected) = hex::decode(hex_mac) else { + return false; + }; + let mut mac = Hmac::::new_from_slice(secret).expect("HMAC accepts any key length"); + mac.update(timestamp.to_string().as_bytes()); + mac.update(b"."); + mac.update(body); + mac.verify_slice(&expected).is_ok() +} + +/// Verify both the MAC and the timestamp window before processing an event. +/// This does not replace durable deduplication by the event ID. +pub fn verify_recent( + secret: &[u8], + timestamp: i64, + body: &[u8], + signature: &str, + now: i64, + max_age: Duration, +) -> bool { + timestamp.abs_diff(now) <= max_age.as_secs() && verify(secret, timestamp, body, signature) +} + +pub struct Webhook { + pub url: String, + pub secret: crate::config::Secret, +} + +type EventStreams = HashMap, broadcast::Sender>>; + +pub struct Delivery { + webhook: Option, + http: reqwest::Client, + streams: Option>, + waiting: Mutex>>>, + next_waiter: AtomicU64, + ack_timeout: Duration, +} + +impl Delivery { + pub fn new(webhook: Option, sse: bool, ack_timeout: Duration) -> Self { + let http = reqwest::Client::builder() + .timeout(WEBHOOK_TIMEOUT) + .redirect(reqwest::redirect::Policy::none()) + .build() + .expect("the HTTP client builds with static settings"); + Self { + webhook, + http, + streams: sse.then(|| StdMutex::new(HashMap::new())), + waiting: Mutex::new(HashMap::new()), + next_waiter: AtomicU64::new(1), + ack_timeout, + } + } + + /// A new event-stream subscriber, if the stream is enabled. + pub fn subscribe(&self) -> Option>> { + self.subscribe_for(None) + } + + /// Subscribe to one connection, or all connections when the filter is absent. + pub fn subscribe_for( + &self, + connection_id: Option, + ) -> Option>> { + let mut streams = self + .streams + .as_ref()? + .lock() + .expect("stream registry poisoned"); + streams.retain(|_, stream| stream.receiver_count() > 0); + Some( + streams + .entry(connection_id) + .or_insert_with(|| broadcast::channel(STREAM_BUFFER).0) + .subscribe(), + ) + } + + /// Record an acknowledgement from the event stream. Returns whether an + /// event with that id was waiting. + pub async fn acknowledge(&self, event_id: &str) -> bool { + match self.waiting.lock().await.remove(event_id) { + Some(waiters) => { + for (_, waiter) in waiters { + let _ = waiter.send(()); + } + true + } + None => false, + } + } + + /// Hand `event` to the application and wait for its acknowledgement. + pub async fn deliver(&self, event: Event) -> Result<(), DeliveryError> { + let event = Arc::new(event); + let streams: Vec<_> = self + .streams + .as_ref() + .map(|registry| { + let mut registry = registry.lock().expect("stream registry poisoned"); + registry.retain(|_, stream| stream.receiver_count() > 0); + registry + .iter() + .filter(|(filter, _)| { + filter.as_ref().is_none_or(|id| *id == event.connection_id) + }) + .map(|(_, stream)| stream.clone()) + .collect() + }) + .unwrap_or_default(); + if self.webhook.is_none() && streams.is_empty() { + return Err(DeliveryError::NoListener); + } + + let waiter_id = self.next_waiter.fetch_add(1, Ordering::Relaxed); + let acked = if !streams.is_empty() { + let (tx, rx) = oneshot::channel(); + self.waiting + .lock() + .await + .entry(event.id.clone()) + .or_default() + .insert(waiter_id, tx); + for stream in &streams { + let _ = stream.send(event.clone()); + } + Some(rx) + } else { + None + }; + + let result = tokio::time::timeout(self.ack_timeout, async { + match (&self.webhook, acked) { + (Some(webhook), None) => self.post(webhook, &event).await, + (None, Some(rx)) => rx.await.map_err(|_| DeliveryError::NotAcknowledged), + (Some(webhook), Some(rx)) => { + // Either acknowledgement completes delivery. A webhook + // failure leaves the stream acknowledgement to decide. + let posted = self.post(webhook, &event); + tokio::pin!(posted, rx); + tokio::select! { + r = &mut posted => match r { + Ok(()) => Ok(()), + Err(_) => rx.await.map_err(|_| DeliveryError::NotAcknowledged), + }, + r = &mut rx => r.map_err(|_| DeliveryError::NotAcknowledged), + } + } + (None, None) => Err(DeliveryError::NoListener), + } + }) + .await + .unwrap_or(Err(DeliveryError::NotAcknowledged)); + + let mut waiting = self.waiting.lock().await; + if let Some(waiters) = waiting.get_mut(&event.id) { + waiters.remove(&waiter_id); + if waiters.is_empty() { + waiting.remove(&event.id); + } + } + result + } + + async fn post(&self, webhook: &Webhook, event: &Event) -> Result<(), DeliveryError> { + let body = serde_json::to_vec(event).expect("events always serialize"); + let timestamp = now_secs(); + let signature = sign(webhook.secret.expose().as_bytes(), timestamp, &body); + let response = self + .http + .post(&webhook.url) + .header("Content-Type", "application/json") + .header(TIMESTAMP_HEADER, timestamp.to_string()) + .header(SIGNATURE_HEADER, signature) + .header(EVENT_ID_HEADER, &event.id) + .body(body) + .send() + .await + .map_err(|_| DeliveryError::WebhookUnreachable)?; + let status = response.status(); + if status.is_success() { + Ok(()) + } else { + Err(DeliveryError::WebhookRejected(status.as_u16())) + } + } +} + +pub fn now_secs() -> i64 { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_or(0, |d| d.as_secs() as i64) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn event(id: &str) -> Event { + Event { + id: id.into(), + kind: "message", + connection_id: "s".into(), + chat: "c".into(), + sender: "x".into(), + sender_alt: None, + message_id: "m".into(), + timestamp: 0, + is_from_me: false, + redelivered: false, + identity_changed: false, + text: Some("t".into()), + media: None, + history: None, + message_proto: String::new(), + } + } + + #[test] + fn signatures_verify_and_reject_changes() { + let sig = sign(b"secret", 100, b"{}"); + assert!(verify(b"secret", 100, b"{}", &sig)); + assert!(!verify(b"secret", 101, b"{}", &sig)); + assert!(!verify(b"other", 100, b"{}", &sig)); + assert!(!verify(b"secret", 100, b"{ }", &sig)); + assert!(!verify(b"secret", 100, b"{}", "garbage")); + assert!(verify_recent( + b"secret", + 100, + b"{}", + &sig, + 105, + Duration::from_secs(5) + )); + assert!(!verify_recent( + b"secret", + 100, + b"{}", + &sig, + 106, + Duration::from_secs(5) + )); + assert!(!verify_recent( + b"secret", + 100, + b"{}", + &sig, + 94, + Duration::from_secs(5) + )); + } + + #[tokio::test] + async fn without_a_listener_delivery_fails_fast() { + let delivery = Delivery::new(None, true, Duration::from_secs(5)); + assert_eq!( + delivery.deliver(event("a")).await, + Err(DeliveryError::NoListener) + ); + } + + #[tokio::test] + async fn only_matching_live_subscribers_count_as_listeners() { + let delivery = Delivery::new(None, true, Duration::from_secs(5)); + let mut other = delivery.subscribe_for(Some("other".into())).unwrap(); + assert_eq!( + delivery.deliver(event("unwatched")).await, + Err(DeliveryError::NoListener) + ); + assert!(matches!( + other.try_recv(), + Err(broadcast::error::TryRecvError::Empty) + )); + let mut matching = delivery.subscribe_for(Some("s".into())).unwrap(); + let send = delivery.deliver(event("matched")); + let ack = async { + let got = matching.recv().await.unwrap(); + assert!(delivery.acknowledge(&got.id).await); + }; + let (result, ()) = tokio::join!(send, ack); + assert_eq!(result, Ok(())); + drop(matching); + assert_eq!( + delivery.deliver(event("gone")).await, + Err(DeliveryError::NoListener) + ); + } + + #[tokio::test] + async fn a_stream_acknowledgement_completes_delivery() { + let delivery = Arc::new(Delivery::new(None, true, Duration::from_secs(5))); + let mut rx = delivery.subscribe().unwrap(); + let acker = { + let delivery = delivery.clone(); + tokio::spawn(async move { + let got = rx.recv().await.unwrap(); + assert!(delivery.acknowledge(&got.id).await); + }) + }; + delivery.deliver(event("b")).await.unwrap(); + acker.await.unwrap(); + assert!(!delivery.acknowledge("b").await, "the waiter must be gone"); + } + + #[tokio::test] + async fn an_unacknowledged_event_times_out() { + let delivery = Delivery::new(None, true, Duration::from_millis(50)); + let _rx = delivery.subscribe().unwrap(); + assert_eq!( + delivery.deliver(event("c")).await, + Err(DeliveryError::NotAcknowledged) + ); + } + #[tokio::test(start_paused = true)] + async fn one_duplicate_timeout_does_not_remove_another_waiter() { + let delivery = Arc::new(Delivery::new(None, true, Duration::from_millis(250))); + let mut rx = delivery.subscribe().unwrap(); + let first = { + let d = delivery.clone(); + tokio::spawn(async move { d.deliver(event("same")).await }) + }; + rx.recv().await.unwrap(); + tokio::time::advance(Duration::from_millis(100)).await; + let second = { + let d = delivery.clone(); + tokio::spawn(async move { d.deliver(event("same")).await }) + }; + rx.recv().await.unwrap(); + tokio::time::advance(Duration::from_millis(151)).await; + assert_eq!(first.await.unwrap(), Err(DeliveryError::NotAcknowledged)); + assert!(delivery.acknowledge("same").await); + assert_eq!(second.await.unwrap(), Ok(())); + } +} diff --git a/src/http.rs b/src/http.rs new file mode 100644 index 0000000..a935159 --- /dev/null +++ b/src/http.rs @@ -0,0 +1,206 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +//! The local application API. Every route except `/healthz` requires +//! `Authorization: Bearer `. See README for the reference. + +use crate::message::SendRequest; +use crate::node::Node; +use crate::send::{self, SendError}; +use axum::extract::{Path, Query, Request, State}; +use axum::http::{StatusCode, header}; +use axum::middleware::{self, Next}; +use axum::response::sse::{Event as SseEvent, KeepAlive, Sse}; +use axum::response::{IntoResponse, Response}; +use axum::routing::{get, post}; +use axum::{Json, Router}; +use futures_util::Stream; +use serde::{Deserialize, Serialize}; +use serde_json::json; +use std::convert::Infallible; +use std::sync::Arc; +use subtle::ConstantTimeEq; +use tokio_stream::StreamExt as _; +use tokio_stream::wrappers::BroadcastStream; + +fn error(status: StatusCode, code: &str, message: impl Into) -> Response { + ( + status, + Json(json!({ "error": { "code": code, "message": message.into() } })), + ) + .into_response() +} + +pub fn router(node: Arc) -> Router { + let api = Router::new() + .route("/v1/connections", get(list_connections)) + .route("/v1/connections/{id}/messages", post(send_message)) + .route("/v1/events", get(events)) + .route("/v1/events/{id}/ack", post(ack)) + .layer(middleware::from_fn_with_state(node.clone(), authenticate)); + Router::new() + .route("/healthz", get(health)) + .merge(api) + .with_state(node) +} + +async fn authenticate(State(node): State>, request: Request, next: Next) -> Response { + if request.headers().contains_key(header::ORIGIN) { + return error( + StatusCode::FORBIDDEN, + "origin_forbidden", + "the application API accepts server clients only", + ); + } + let presented = request + .headers() + .get(header::AUTHORIZATION) + .and_then(|v| v.to_str().ok()) + .and_then(|v| v.strip_prefix("Bearer ")) + .unwrap_or(""); + let expected = node.cfg.http_secret.expose().as_bytes(); + if bool::from(presented.as_bytes().ct_eq(expected)) { + next.run(request).await + } else { + error( + StatusCode::UNAUTHORIZED, + "unauthorized", + "missing or wrong bearer secret", + ) + } +} + +async fn health(State(node): State>) -> Response { + let _ = node; + Json(json!({ "status": "ok" })).into_response() +} + +#[derive(Serialize)] +struct ConnectionInfo { + id: String, + lease_generation: u64, + claimed: bool, + has_device_keys: bool, + #[serde(flatten)] + view: crate::node::ConnectionView, +} + +async fn list_connections(State(node): State>) -> Response { + let connections: Vec = node + .connections() + .into_iter() + .map(|s| ConnectionInfo { + id: s.id.clone(), + lease_generation: s.generation, + claimed: s.is_claimed(), + has_device_keys: s.ops().is_some(), + view: s.view(), + }) + .collect(); + Json(json!({ "node_id": node.cfg.node_id, "connections": connections })).into_response() +} + +async fn send_message( + State(node): State>, + Path(id): Path, + body: Result, axum::extract::rejection::JsonRejection>, +) -> Response { + let Json(request) = match body { + Ok(b) => b, + Err(e) => return error(StatusCode::BAD_REQUEST, "invalid_request", e.body_text()), + }; + if !node.is_configured(&id) { + return error( + StatusCode::NOT_FOUND, + "unknown_connection", + "connection is not configured", + ); + } + let Some(connection) = node.connection(&id) else { + return error( + StatusCode::CONFLICT, + "not_lease_holder", + "another node serves this connection; send through that node", + ); + }; + match send::send(&node, &connection, request).await { + Ok(outcome) => (StatusCode::OK, Json(outcome)).into_response(), + Err(SendError::Unconfirmed { message_id }) => ( + StatusCode::GATEWAY_TIMEOUT, + Json(json!({ "error": { + "code": "send_unconfirmed", + "message": "send outcome is unconfirmed; do not automatically resend", + "message_id": message_id, + }})), + ) + .into_response(), + Err(e) => { + let (status, code) = match &e { + SendError::Invalid(_) => (StatusCode::BAD_REQUEST, "invalid_request"), + SendError::NotLeaseHolder => (StatusCode::CONFLICT, "not_lease_holder"), + SendError::NotPaired => (StatusCode::CONFLICT, "connection_not_paired"), + SendError::Disconnected => (StatusCode::SERVICE_UNAVAILABLE, "disconnected"), + SendError::RateLimited => (StatusCode::TOO_MANY_REQUESTS, "rate_limited"), + SendError::Refused(err) => match err.code_enum() { + crate::proto::Code::ConnectionUnavailable => { + (StatusCode::CONFLICT, "connection_unavailable") + } + crate::proto::Code::Refused => (StatusCode::FORBIDDEN, "refused"), + crate::proto::Code::Unavailable => { + (StatusCode::SERVICE_UNAVAILABLE, "unavailable") + } + _ => (StatusCode::BAD_GATEWAY, "send_failed"), + }, + SendError::StorageUnavailable => { + (StatusCode::SERVICE_UNAVAILABLE, "storage_unavailable") + } + SendError::Build => (StatusCode::BAD_GATEWAY, "send_failed"), + SendError::Unconfirmed { .. } => unreachable!("handled above"), + }; + error(status, code, e.to_string()) + } + } +} + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct EventFilter { + connection_id: Option, +} + +// An error Response is the standard axum handler shape; it is built only on refusal. +#[allow(clippy::result_large_err)] +async fn events( + State(node): State>, + Query(filter): Query, +) -> Result>>, Response> { + let Some(rx) = node.delivery.subscribe_for(filter.connection_id) else { + return Err(error( + StatusCode::NOT_FOUND, + "stream_disabled", + "the event stream is turned off (delivery.sse = false)", + )); + }; + let stream = BroadcastStream::new(rx).filter_map(move |item| { + // A lagging client misses events; they are not acknowledged, so the + // receive fails and the message is redelivered later. + let event = item.ok()?; + let data = serde_json::to_string(&*event).ok()?; + Some(Ok(SseEvent::default() + .id(event.id.clone()) + .event(event.kind) + .data(data))) + }); + Ok(Sse::new(stream).keep_alive(KeepAlive::default())) +} + +async fn ack(State(node): State>, Path(id): Path) -> Response { + if node.delivery.acknowledge(&id).await { + StatusCode::NO_CONTENT.into_response() + } else { + error( + StatusCode::NOT_FOUND, + "unknown_event", + "no delivery is waiting for that event; it was acknowledged already or timed out", + ) + } +} diff --git a/src/keys.rs b/src/keys.rs new file mode 100644 index 0000000..9a9caea --- /dev/null +++ b/src/keys.rs @@ -0,0 +1,189 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +//! Key custody for server mode. +//! +//! The operator supplies one 256-bit master key. Each connection has its own +//! random 256-bit data key, stored in the connection's records wrapped +//! (AES-256-GCM) by the master key. A node unwraps the data key once, when it +//! takes the connection's lease, and seals every record with it from then on. +//! The master key never touches the database, and the data key is never +//! stored unwrapped. +//! +//! The wrapped data key lives in the connection's `device` namespace under +//! [`DATA_KEY_RECORD`]. It is written with an exclusive, fenced insert, so +//! two nodes that race to initialise a connection cannot both pick a key. + +use aes_gcm::aead::{Aead, KeyInit, Payload}; +use aes_gcm::{Aes256Gcm, Nonce}; +use rand::RngCore; +use wacore_recordstore::{Fence, Namespace, RecordStore, WriteOp, is_record_exists}; + +/// Record key of the wrapped data key, in [`Namespace::Device`]. The Signal +/// store uses only the key `local` in that namespace. +pub const DATA_KEY_RECORD: &str = "signal-node.data-key"; + +const WRAP_FORMAT_V1: u8 = 1; +const WRAP_DOMAIN: &[u8] = b"signal-node/data-key/v1"; +const NONCE_LEN: usize = 12; + +#[derive(Debug, thiserror::Error)] +pub enum KeyError { + #[error("the connection's data key does not open with this master key")] + WrongMasterKey, + #[error("the connection's wrapped data key is malformed")] + Malformed, + #[error("storage: {0}")] + Store(#[from] wacore::store::error::StoreError), +} + +/// The operator's master key. Holds the key only in memory. +pub struct MasterKey { + cipher: Aes256Gcm, +} + +impl MasterKey { + pub fn new(key: &[u8; 32]) -> Self { + Self { + cipher: Aes256Gcm::new(key.into()), + } + } + + fn aad(scope: &str) -> Vec { + let mut aad = Vec::with_capacity(WRAP_DOMAIN.len() + scope.len() + 8); + for part in [WRAP_DOMAIN, scope.as_bytes()] { + aad.extend_from_slice(&(part.len() as u32).to_be_bytes()); + aad.extend_from_slice(part); + } + aad + } + + /// `[format][nonce][ciphertext||tag]`, bound to `scope`. + pub fn wrap(&self, scope: &str, data_key: &[u8; 32]) -> Vec { + let mut nonce = [0u8; NONCE_LEN]; + rand::thread_rng().fill_bytes(&mut nonce); + let aad = Self::aad(scope); + let sealed = self + .cipher + .encrypt( + Nonce::from_slice(&nonce), + Payload { + msg: data_key, + aad: &aad, + }, + ) + .expect("AES-GCM encryption of 32 bytes cannot fail"); + let mut out = Vec::with_capacity(1 + NONCE_LEN + sealed.len()); + out.push(WRAP_FORMAT_V1); + out.extend_from_slice(&nonce); + out.extend_from_slice(&sealed); + out + } + + pub fn unwrap(&self, scope: &str, wrapped: &[u8]) -> Result<[u8; 32], KeyError> { + let (&format, rest) = wrapped.split_first().ok_or(KeyError::Malformed)?; + if format != WRAP_FORMAT_V1 || rest.len() < NONCE_LEN + 16 { + return Err(KeyError::Malformed); + } + let (nonce, sealed) = rest.split_at(NONCE_LEN); + let aad = Self::aad(scope); + let opened = self + .cipher + .decrypt( + Nonce::from_slice(nonce), + Payload { + msg: sealed, + aad: &aad, + }, + ) + .map_err(|_| KeyError::WrongMasterKey)?; + opened.try_into().map_err(|_| KeyError::Malformed) + } + + /// The scope's data key, creating it on first use. Call once per lease. + pub async fn data_key_for( + &self, + records: &R, + fence: &Fence, + ) -> Result<[u8; 32], KeyError> { + let scope = &*fence.scope; + if let Some(wrapped) = records + .get(scope, Namespace::Device, DATA_KEY_RECORD) + .await? + { + return self.unwrap(scope, &wrapped); + } + let mut data_key = [0u8; 32]; + rand::thread_rng().fill_bytes(&mut data_key); + let insert = WriteOp::Insert { + ns: Namespace::Device, + key: DATA_KEY_RECORD.to_owned(), + value: self.wrap(scope, &data_key), + }; + match records.write(fence, &[insert]).await { + Ok(()) => Ok(data_key), + // Another holder initialised it between our read and write. + Err(e) if is_record_exists(&e) => { + let wrapped = records + .get(scope, Namespace::Device, DATA_KEY_RECORD) + .await? + .ok_or(KeyError::Malformed)?; + self.unwrap(scope, &wrapped) + } + Err(e) => Err(e.into()), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use wacore_recordstore::{LeaseStore, MemoryRecordStore}; + + #[test] + fn a_wrapped_key_opens_only_for_its_scope_and_master_key() { + let master = MasterKey::new(&[1; 32]); + let wrapped = master.wrap("sess-a", &[7; 32]); + assert_eq!(master.unwrap("sess-a", &wrapped).unwrap(), [7; 32]); + assert!(matches!( + master.unwrap("sess-b", &wrapped), + Err(KeyError::WrongMasterKey) + )); + assert!(matches!( + MasterKey::new(&[2; 32]).unwrap("sess-a", &wrapped), + Err(KeyError::WrongMasterKey) + )); + assert!(matches!( + master.unwrap("sess-a", &[1, 2, 3]), + Err(KeyError::Malformed) + )); + } + + #[tokio::test] + async fn the_data_key_is_created_once_and_stays_the_same() { + let records = MemoryRecordStore::new(); + let master = MasterKey::new(&[1; 32]); + let lease = records.acquire("s", "a", 0, 10_000).await.unwrap().unwrap(); + let first = master.data_key_for(&records, &lease.fence).await.unwrap(); + let again = master.data_key_for(&records, &lease.fence).await.unwrap(); + assert_eq!(first, again); + let raw = records + .get("s", Namespace::Device, DATA_KEY_RECORD) + .await + .unwrap() + .unwrap(); + assert!( + !raw.windows(32).any(|w| w == first), + "the data key was stored unwrapped" + ); + // A later holder of the scope opens the same key. + let next = records + .acquire("s", "b", 20_000, 10_000) + .await + .unwrap() + .unwrap(); + assert_eq!( + master.data_key_for(&records, &next.fence).await.unwrap(), + first + ); + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..35bfd14 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,89 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +//! signal-node: the self-hosted Signal service for Polymorfa +//! zero-knowledge connections. + +pub mod config; +pub mod delivery; +pub mod http; +pub mod keys; +pub mod link; +pub mod message; +pub mod node; +pub mod proto; +pub mod ratelimit; +pub mod resolver; +pub mod send; +pub mod serve; +mod share_replay; +pub mod store; + +use std::net::SocketAddr; +use std::sync::Arc; +use tokio::task::JoinHandle; + +/// Build identifier sent in `Hello.service_version`. +pub const SERVICE_VERSION: &str = concat!("signal-node/", env!("CARGO_PKG_VERSION")); + +/// A running node. +pub struct Running { + pub node: Arc, + pub http_addr: SocketAddr, + tasks: Vec>, +} + +impl Running { + /// Release every connection, then stop. + pub async fn shutdown(mut self) { + // Open SSE streams or a stalled peer cannot hold shutdown forever. + // Aborting after this deadline leaves any unreleased lease to expire. + let cleanup = async { + self.node.shutdown().await; + for task in &mut self.tasks { + let _ = task.await; + } + }; + if tokio::time::timeout(std::time::Duration::from_secs(10), cleanup) + .await + .is_err() + { + for task in &self.tasks { + task.abort(); + } + tracing::warn!("shutdown deadline reached; remaining leases will expire"); + } + } + + /// Stop every task at once without releasing leases, as a crash would. + /// For tests. + pub fn abort(&self) { + for task in &self.tasks { + task.abort(); + } + } +} + +/// Start a node: bind the application API, then run the link, lease and +/// pruning loops. +pub async fn start(cfg: config::Resolved, backend: store::Backend) -> anyhow::Result { + let listener = tokio::net::TcpListener::bind(cfg.http_listen).await?; + let http_addr = listener.local_addr()?; + let node = node::Node::new(cfg, backend); + let mut tasks = node.spawn_loops(SERVICE_VERSION); + let app = http::router(node.clone()); + let mut stop = node.shutdown_signal(); + tasks.push(tokio::spawn(async move { + let served = axum::serve(listener, app).with_graceful_shutdown(async move { + let _ = stop.changed().await; + }); + if let Err(e) = served.await { + tracing::error!(error = %e, "application API stopped"); + } + })); + tracing::info!(node = %node.cfg.node_id, %http_addr, "signal-node started"); + Ok(Running { + node, + http_addr, + tasks, + }) +} diff --git a/src/link.rs b/src/link.rs new file mode 100644 index 0000000..f4c84ba --- /dev/null +++ b/src/link.rs @@ -0,0 +1,479 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +//! The WebSocket link to Polymorfa. +//! +//! The node dials out, sends `Hello`, waits for `Welcome`, then multiplexes +//! requests in both directions over binary frames. A Ping goes out every +//! `Welcome.heartbeat_interval_ms`; a missing answer closes the link. On any +//! failure the link reconnects with jittered exponential backoff. + +use crate::proto::{ + self, Code, ConnectionEvent, Frame, Hello, RunnerRequest, ServiceRequest, ServiceResponse, + Welcome, frame::Body, runner_response, service_request, +}; +use async_trait::async_trait; +use futures_util::{SinkExt, StreamExt}; +use prost::Message as _; +use rand::Rng; +use std::collections::HashMap; +use std::sync::Arc; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::time::Duration; +use tokio::sync::{Mutex, mpsc, oneshot, watch}; +use tokio_tungstenite::tungstenite::{self, client::IntoClientRequest, http::HeaderValue}; + +/// Defaults when Welcome leaves a value at zero. +const DEFAULT_HEARTBEAT: Duration = Duration::from_secs(10); +pub const DEFAULT_MAX_SENDS_PER_SECOND: u32 = 5; +const WELCOME_TIMEOUT: Duration = Duration::from_secs(10); +/// A link that stayed up this long resets the reconnect backoff. +const STABLE_AFTER: Duration = Duration::from_secs(30); + +#[derive(Debug, Clone)] +pub struct LinkSettings { + pub url: String, + pub credential: crate::config::Secret, + pub node_id: String, + pub service_version: String, + pub request_timeout: Duration, + pub reconnect_min: Duration, + pub reconnect_max: Duration, +} + +#[derive(Debug, thiserror::Error)] +pub enum LinkError { + #[error("not connected to Polymorfa")] + Disconnected, + #[error("Polymorfa did not answer in time")] + Timeout, + #[error("the connection closed before Polymorfa answered the queued request")] + ResponseLost, + #[error("Polymorfa refused the request: {0}")] + Remote(proto::Error), + #[error("Polymorfa sent an unexpected response")] + UnexpectedResponse, +} + +impl LinkError { + pub fn remote_code(&self) -> Option { + match self { + LinkError::Remote(e) => Some(e.code_enum()), + _ => None, + } + } +} + +type Pending = oneshot::Sender>; + +/// One live connection. Cloned handles stay valid until it closes; requests +/// on a closed link fail with [`LinkError::Disconnected`]. +pub struct Link { + out: mpsc::UnboundedSender, + pending: Mutex>, + next_id: AtomicU64, + welcome: Welcome, + timeout: Duration, + closed: watch::Sender, +} + +impl Link { + #[cfg(test)] + pub(crate) fn fixture() -> Arc { + let (out, _) = mpsc::unbounded_channel(); + Arc::new(Self { + out, + pending: Mutex::new(HashMap::new()), + next_id: AtomicU64::new(1), + welcome: Welcome::default(), + timeout: Duration::from_secs(1), + closed: watch::channel(false).0, + }) + } + + pub fn welcome(&self) -> &Welcome { + &self.welcome + } + + /// The send ceiling Polymorfa announced. + pub fn max_sends_per_second(&self) -> u32 { + match self.welcome.max_sends_per_second { + 0 => DEFAULT_MAX_SENDS_PER_SECOND, + n => n.min(DEFAULT_MAX_SENDS_PER_SECOND), + } + } + + pub fn is_closed(&self) -> bool { + *self.closed.borrow() + } + + fn close(&self) { + let _ = self.closed.send(true); + } + + /// Send a request and wait for its response. + pub async fn request( + &self, + connection_id: &str, + lease_generation: u64, + op: service_request::Op, + ) -> Result { + if self.is_closed() { + return Err(LinkError::Disconnected); + } + let id = self.next_id.fetch_add(1, Ordering::Relaxed); + let (tx, rx) = oneshot::channel(); + self.pending.lock().await.insert(id, tx); + let frame = Frame::with( + id, + Body::ServiceRequest(ServiceRequest { + connection_id: connection_id.to_owned(), + lease_generation, + op: Some(op), + }), + ); + if self.out.send(frame).is_err() { + self.pending.lock().await.remove(&id); + return Err(LinkError::Disconnected); + } + match tokio::time::timeout(self.timeout, rx).await { + Ok(Ok(Ok(result))) => Ok(result), + Ok(Ok(Err(error))) => Err(LinkError::Remote(error)), + Ok(Err(_)) => Err(LinkError::ResponseLost), + Err(_) => { + self.pending.lock().await.remove(&id); + Err(LinkError::Timeout) + } + } + } + + async fn complete(&self, id: u64, result: Result) { + if let Some(waiter) = self.pending.lock().await.remove(&id) { + let _ = waiter.send(result); + } + } + + fn reply(&self, frame: Frame) { + let _ = self.out.send(frame); + } + + async fn fail_all(&self) { + // Dropping the senders wakes every waiter with Disconnected. + self.pending.lock().await.clear(); + } +} + +/// What the node does with traffic from Polymorfa. +#[async_trait] +pub trait Handler: Send + Sync + 'static { + async fn runner_request(&self, request: RunnerRequest) + -> Result; + /// `origin` is the link that delivered the event; a handler must ignore + /// events from a link that is no longer current. + async fn connection_event(&self, event: ConnectionEvent, origin: Arc); + /// A link finished its handshake. Claims go out from here. + async fn connected(&self, link: Arc); + async fn disconnected(&self); +} + +#[derive(Debug, thiserror::Error)] +enum ConnectError { + // Boxed: tungstenite::Error is large and this travels on every Result. + #[error("WebSocket: {0}")] + WebSocket(Box), + #[error("handshake: {0}")] + Handshake(String), + #[error("Polymorfa refused the connection: {0}")] + Refused(proto::Error), +} + +impl From for ConnectError { + fn from(err: tungstenite::Error) -> Self { + Self::WebSocket(Box::new(err)) + } +} + +/// Keep a link to Polymorfa until `shutdown` turns true. +pub async fn run( + settings: LinkSettings, + handler: Arc, + mut shutdown: watch::Receiver, +) { + let mut attempt: u32 = 0; + while !*shutdown.borrow() { + let started = tokio::time::Instant::now(); + let result = tokio::select! { + r = connect_and_serve(&settings, handler.clone(), shutdown.clone()) => r, + _ = shutdown.changed() => break, + }; + match &result { + Ok(()) => tracing::info!("link to Polymorfa closed"), + Err(e) => tracing::warn!(error = %e, "link to Polymorfa failed"), + } + if *shutdown.borrow() { + break; + } + if started.elapsed() >= STABLE_AFTER { + attempt = 0; + } + let delay = backoff(settings.reconnect_min, settings.reconnect_max, attempt); + attempt = attempt.saturating_add(1); + tokio::select! { + _ = tokio::time::sleep(delay) => {} + _ = shutdown.changed() => break, + } + } +} + +/// Full-jitter exponential backoff. +pub fn backoff(min: Duration, max: Duration, attempt: u32) -> Duration { + let ceiling = min + .saturating_mul(1u32 << attempt.min(16)) + .min(max) + .max(min); + let millis = ceiling.as_millis().max(1) as u64; + Duration::from_millis(rand::thread_rng().gen_range(min.as_millis() as u64..=millis)) +} + +async fn connect_and_serve( + settings: &LinkSettings, + handler: Arc, + mut shutdown: watch::Receiver, +) -> Result<(), ConnectError> { + let mut request = settings.url.as_str().into_client_request()?; + let headers = request.headers_mut(); + headers.insert( + "Sec-WebSocket-Protocol", + HeaderValue::from_static(proto::SUBPROTOCOL), + ); + let mut auth = HeaderValue::from_str(&format!("Bearer {}", settings.credential.expose())) + .map_err(|_| ConnectError::Handshake("credential is not a valid header value".into()))?; + auth.set_sensitive(true); + headers.insert("Authorization", auth); + + let (socket, response) = tokio_tungstenite::connect_async(request).await?; + let agreed = response + .headers() + .get("Sec-WebSocket-Protocol") + .and_then(|v| v.to_str().ok()); + if agreed != Some(proto::SUBPROTOCOL) { + return Err(ConnectError::Handshake( + "Polymorfa did not accept subprotocol polymorfa.signal.v1".into(), + )); + } + let (mut sink, mut stream) = socket.split(); + + let hello = Frame::with( + 0, + Body::Hello(Hello { + protocol_version: proto::PROTOCOL_VERSION, + service_version: settings.service_version.clone(), + node_id: settings.node_id.clone(), + }), + ); + sink.send(tungstenite::Message::Binary(hello.encode_to_vec().into())) + .await?; + + let welcome = tokio::time::timeout(WELCOME_TIMEOUT, async { + while let Some(message) = stream.next().await { + let tungstenite::Message::Binary(bytes) = message? else { + continue; + }; + let frame = Frame::decode(bytes.as_ref()) + .map_err(|_| ConnectError::Handshake("undecodable frame".into()))?; + match frame.body { + Some(Body::Welcome(welcome)) => return Ok(welcome), + Some(Body::Error(error)) => return Err(ConnectError::Refused(error)), + _ => { + return Err(ConnectError::Handshake( + "expected Welcome as the first frame".into(), + )); + } + } + } + Err(ConnectError::Handshake("closed before Welcome".into())) + }) + .await + .map_err(|_| ConnectError::Handshake("no Welcome in time".into()))??; + if welcome.protocol_version != proto::PROTOCOL_VERSION { + return Err(ConnectError::Handshake(format!( + "Polymorfa speaks protocol {}, this build speaks {}", + welcome.protocol_version, + proto::PROTOCOL_VERSION + ))); + } + tracing::info!( + project = %welcome.project_id, + heartbeat_ms = welcome.heartbeat_interval_ms, + max_sends_per_second = welcome.max_sends_per_second, + "connected to Polymorfa" + ); + + let (out_tx, mut out_rx) = mpsc::unbounded_channel::(); + let (closed_tx, mut closed_rx) = watch::channel(false); + let heartbeat = match welcome.heartbeat_interval_ms { + 0 => DEFAULT_HEARTBEAT, + ms => Duration::from_millis(u64::from(ms)), + }; + let link = Arc::new(Link { + out: out_tx, + pending: Mutex::new(HashMap::new()), + next_id: AtomicU64::new(1), + welcome, + timeout: settings.request_timeout, + closed: closed_tx, + }); + + let writer = tokio::spawn(async move { + while let Some(frame) = out_rx.recv().await { + let bytes = frame.encode_to_vec(); + if sink + .send(tungstenite::Message::Binary(bytes.into())) + .await + .is_err() + { + break; + } + } + let _ = sink.close().await; + }); + + let pinger = { + let link = link.clone(); + tokio::spawn(async move { + let mut ticker = tokio::time::interval(heartbeat); + ticker.tick().await; + loop { + ticker.tick().await; + if link.is_closed() { + break; + } + let ping = service_request::Op::Ping(proto::Ping {}); + if let Err(e) = link.request("", 0, ping).await { + tracing::warn!(error = %e, "heartbeat failed; closing the link"); + link.close(); + break; + } + } + }) + }; + + // Abort spawned socket tasks even if the enclosing future is cancelled + // during a crash, shutdown select, or handshake callback. + struct SocketTasks(tokio::task::JoinHandle<()>, tokio::task::JoinHandle<()>); + impl Drop for SocketTasks { + fn drop(&mut self) { + self.0.abort(); + self.1.abort(); + } + } + let _socket_tasks = SocketTasks(writer, pinger); + handler.connected(link.clone()).await; + + let result = loop { + tokio::select! { + message = stream.next() => { + let message = match message { + Some(Ok(m)) => m, + Some(Err(e)) => break Err(ConnectError::from(e)), + None => break Ok(()), + }; + match message { + tungstenite::Message::Binary(bytes) => match Frame::decode(bytes.as_ref()) { + Ok(frame) => dispatch(&link, &handler, frame).await, + Err(_) => tracing::warn!("dropped an undecodable frame"), + }, + tungstenite::Message::Close(_) => break Ok(()), + _ => {} + } + } + _ = closed_rx.changed() => break Ok(()), + _ = shutdown.changed() => break Ok(()), + } + }; + + link.close(); + link.fail_all().await; + handler.disconnected().await; + result +} + +async fn dispatch(link: &Arc, handler: &Arc, frame: Frame) { + let id = frame.id; + match frame.body { + Some(Body::RunnerRequest(request)) => { + let link = link.clone(); + let handler = handler.clone(); + tokio::spawn(async move { + let body = match handler.runner_request(request).await { + Ok(response) => Body::ServiceResponse(response), + Err(error) => Body::Error(error), + }; + link.reply(Frame::with(id, body)); + }); + } + Some(Body::RunnerResponse(response)) => match response.result { + Some(result) => link.complete(id, Ok(result)).await, + None => { + link.complete( + id, + Err(proto::Error::new( + Code::Unsupported, + "response with an unknown result", + )), + ) + .await + } + }, + Some(Body::Error(error)) => { + if id == 0 { + tracing::warn!( + code = error.code_enum().as_str_name(), + "Polymorfa reported an error" + ); + } else { + link.complete(id, Err(error)).await; + } + } + Some(Body::ConnectionEvent(event)) => { + let handler = handler.clone(); + let origin = link.clone(); + tokio::spawn(async move { handler.connection_event(event, origin).await }); + } + Some(Body::Welcome(_)) => {} + Some(Body::Hello(_) | Body::ServiceRequest(_) | Body::ServiceResponse(_)) => { + if id != 0 { + link.reply(Frame::with( + id, + Body::Error(proto::Error::new( + Code::InvalidArgument, + "frame type is not sent by Polymorfa", + )), + )); + } + } + None => { + if id != 0 { + link.reply(Frame::with( + id, + Body::Error(proto::Error::new(Code::Unsupported, "unknown frame type")), + )); + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn backoff_stays_within_bounds_and_grows() { + let min = Duration::from_millis(100); + let max = Duration::from_secs(5); + for attempt in 0..40 { + let d = backoff(min, max, attempt); + assert!(d >= min && d <= max, "{d:?} at attempt {attempt}"); + } + assert!(backoff(min, max, 0) <= Duration::from_millis(100)); + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..041fe01 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +use base64::Engine as _; +use rand::RngCore; +use signal_node::config::Config; +use std::path::PathBuf; + +const USAGE: &str = "usage: signal-node [--config ] | signal-node generate-key"; + +#[tokio::main] +async fn main() -> anyhow::Result<()> { + let args: Vec = std::env::args().skip(1).collect(); + if args.as_slice() == ["--version"] { + println!("{}", signal_node::SERVICE_VERSION); + return Ok(()); + } + if args.as_slice() == ["--help"] { + println!("{USAGE}"); + return Ok(()); + } + if args.as_slice() == ["generate-key"] { + // For the operator to store as storage.master_key; printed once. + let mut key = [0u8; 32]; + rand::thread_rng().fill_bytes(&mut key); + println!("{}", base64::engine::general_purpose::STANDARD.encode(key)); + return Ok(()); + } + let path = match args.as_slice() { + [] => std::env::var("SIGNAL_NODE_CONFIG") + .map(PathBuf::from) + .unwrap_or_else(|_| PathBuf::from("signal-node.toml")), + [flag, path] if flag == "--config" => PathBuf::from(path), + _ => anyhow::bail!(USAGE), + }; + + let json = std::env::var("SIGNAL_NODE_LOG_FORMAT").as_deref() == Ok("json"); + let filter = tracing_subscriber::EnvFilter::try_from_env("SIGNAL_NODE_LOG") + .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")); + if json { + tracing_subscriber::fmt() + .json() + .with_env_filter(filter) + .init(); + } else { + tracing_subscriber::fmt().with_env_filter(filter).init(); + } + + let mut config = Config::load(&path)?; + config.apply_env(|name| std::env::var(name).ok())?; + let resolved = config.resolve()?; + let backend = signal_node::store::Backend::connect(&resolved).await?; + tracing::info!(driver = backend.name(), "storage ready"); + let running = signal_node::start(resolved, backend).await?; + + wait_for_stop().await; + tracing::info!("stopping: releasing connections"); + running.shutdown().await; + Ok(()) +} + +async fn wait_for_stop() { + #[cfg(unix)] + { + let mut term = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) + .expect("SIGTERM handler"); + tokio::select! { + _ = tokio::signal::ctrl_c() => {} + _ = term.recv() => {} + } + } + #[cfg(not(unix))] + { + let _ = tokio::signal::ctrl_c().await; + } +} diff --git a/src/message.rs b/src/message.rs new file mode 100644 index 0000000..323464e --- /dev/null +++ b/src/message.rs @@ -0,0 +1,520 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +//! Conversions between WhatsApp messages and the application-facing JSON. + +use base64::Engine as _; +use base64::engine::general_purpose::STANDARD as B64; +use serde::{Deserialize, Serialize}; +use waproto::whatsapp as wa; + +fn b64(bytes: &[u8]) -> String { + B64.encode(bytes) +} + +fn opt_b64(bytes: &Option>) -> Option { + bytes.as_deref().map(b64) +} + +/// One event delivered to the application, by webhook or event stream. +#[derive(Clone, Serialize)] +pub struct Event { + /// Stable across redeliveries of the same ciphertext; use it to + /// deduplicate and to acknowledge. + pub id: String, + /// `message` or `history.sync`. + #[serde(rename = "type")] + pub kind: &'static str, + pub connection_id: String, + pub chat: String, + pub sender: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub sender_alt: Option, + pub message_id: String, + /// Seconds since the epoch, from the stanza. + pub timestamp: i64, + pub is_from_me: bool, + /// An earlier delivery attempt was not acknowledged. + pub redelivered: bool, + /// The sender's identity key changed before this message. + pub identity_changed: bool, + #[serde(skip_serializing_if = "Option::is_none")] + pub text: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub media: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub history: Option, + /// Base64 of the serialized `waE2E.Message`, the complete content. + pub message_proto: String, +} + +/// Where to download one media attachment and how to decrypt it. +#[derive(Clone, Serialize, Deserialize, PartialEq, Eq)] +pub struct MediaDescriptor { + /// `image`, `video`, `audio`, `document` or `sticker`. + pub kind: String, + pub direct_path: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub url: Option, + /// Base64. + pub media_key: String, + /// Base64 SHA-256 of the plaintext file. + pub file_sha256: String, + /// Base64 SHA-256 of the encrypted file. + pub file_enc_sha256: String, + pub file_length: u64, + pub mimetype: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub caption: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub file_name: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub width: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub height: Option, + /// Audio and video length in seconds. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub seconds: Option, + /// A voice note rather than an audio file. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub ptt: Option, + /// Seconds since the epoch the media key was made. Defaults to now on + /// send. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub media_key_timestamp: Option, +} + +/// A history-sync notification: where the chunk is and how to decrypt it. +/// signal-node never downloads history. +#[derive(Clone, Serialize)] +pub struct HistoryDescriptor { + pub sync_type: Option, + pub chunk_order: Option, + pub progress: Option, + pub direct_path: Option, + pub media_key: Option, + pub file_sha256: Option, + pub file_enc_sha256: Option, + pub file_length: Option, + pub original_message_id: Option, + pub peer_data_request_session_id: Option, + /// Base64. Small initial chunks arrive inline instead of by download. + pub inline_payload: Option, + /// Base64 of the serialized `HistorySyncNotification`. + pub notification_proto: String, +} + +macro_rules! redacted_debug { + ($($ty:ty),+ $(,)?) => {$( + impl std::fmt::Debug for $ty { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(concat!(stringify!($ty), "()")) + } + } + )+}; +} +redacted_debug!(Event, MediaDescriptor, HistoryDescriptor, SendRequest); + +/// The text a person would read: the body, or a media caption. +pub fn text_of(message: &wa::Message) -> Option { + if let Some(text) = &message.conversation { + return Some(text.clone()); + } + if let Some(ext) = message.extended_text_message.as_option() { + return ext.text.clone(); + } + None +} + +macro_rules! media_descriptor { + ($kind:literal, $m:expr, $extra:expr) => {{ + let m = $m; + let mut d = MediaDescriptor { + kind: $kind.to_owned(), + direct_path: m.direct_path.clone().unwrap_or_default(), + url: m.url.clone(), + media_key: opt_b64(&m.media_key).unwrap_or_default(), + file_sha256: opt_b64(&m.file_sha256).unwrap_or_default(), + file_enc_sha256: opt_b64(&m.file_enc_sha256).unwrap_or_default(), + file_length: m.file_length.unwrap_or_default(), + mimetype: m.mimetype.clone().unwrap_or_default(), + caption: None, + file_name: None, + width: None, + height: None, + seconds: None, + ptt: None, + media_key_timestamp: m.media_key_timestamp, + }; + #[allow(clippy::redundant_closure_call)] + ($extra)(&mut d, m); + d + }}; +} + +/// The first media attachment of `message`, if any. +pub fn media_of(message: &wa::Message) -> Option { + if let Some(m) = message.image_message.as_option() { + return Some(media_descriptor!( + "image", + m, + |d: &mut MediaDescriptor, m: &wa::message::ImageMessage| { + d.caption = m.caption.clone(); + d.width = m.width; + d.height = m.height; + } + )); + } + if let Some(m) = message.video_message.as_option() { + return Some(media_descriptor!( + "video", + m, + |d: &mut MediaDescriptor, m: &wa::message::VideoMessage| { + d.caption = m.caption.clone(); + d.width = m.width; + d.height = m.height; + d.seconds = m.seconds; + } + )); + } + if let Some(m) = message.audio_message.as_option() { + return Some(media_descriptor!( + "audio", + m, + |d: &mut MediaDescriptor, m: &wa::message::AudioMessage| { + d.seconds = m.seconds; + d.ptt = m.ptt; + } + )); + } + if let Some(m) = message.document_message.as_option() { + return Some(media_descriptor!( + "document", + m, + |d: &mut MediaDescriptor, m: &wa::message::DocumentMessage| { + d.caption = m.caption.clone(); + d.file_name = m.file_name.clone(); + } + )); + } + if let Some(m) = message.sticker_message.as_option() { + return Some(media_descriptor!( + "sticker", + m, + |d: &mut MediaDescriptor, m: &wa::message::StickerMessage| { + d.width = m.width; + d.height = m.height; + } + )); + } + None +} + +pub fn history_descriptor(n: &wa::message::HistorySyncNotification) -> HistoryDescriptor { + HistoryDescriptor { + sync_type: n.sync_type.map(|t| t as i32), + chunk_order: n.chunk_order, + progress: n.progress, + direct_path: n.direct_path.clone(), + media_key: opt_b64(&n.media_key), + file_sha256: opt_b64(&n.file_sha256), + file_enc_sha256: opt_b64(&n.file_enc_sha256), + file_length: n.file_length, + original_message_id: n.original_message_id.clone(), + peer_data_request_session_id: n.peer_data_request_session_id.clone(), + inline_payload: opt_b64(&n.initial_hist_bootstrap_inline_payload), + notification_proto: b64(&encode(n)), + } +} + +pub fn encode(message: &M) -> Vec { + message.encode_to_vec() +} + +/// What the application asks to send. +#[derive(Clone, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct SendRequest { + /// A user (`15551234567@s.whatsapp.net`, or a LID) or a group + /// (`...@g.us`). + pub to: String, + #[serde(default)] + pub text: Option, + /// A file already encrypted and uploaded to WhatsApp's media servers. + #[serde(default)] + pub media: Option, +} + +#[derive(Debug, thiserror::Error, PartialEq, Eq)] +pub enum BuildError { + #[error("send exactly one of text or media")] + NeedOneBody, + #[error("text must not be empty")] + EmptyText, + #[error("media.{0} is required")] + Missing(&'static str), + #[error("media.{0} is not valid base64")] + BadBase64(&'static str), + #[error("media.{field} must be {len} bytes")] + BadLength { field: &'static str, len: usize }, + #[error("unknown media kind {0}; use image, video, audio, document or sticker")] + UnknownKind(String), +} + +fn decode_field(value: &str, field: &'static str, len: usize) -> Result, BuildError> { + if value.is_empty() { + return Err(BuildError::Missing(field)); + } + let bytes = B64 + .decode(value) + .map_err(|_| BuildError::BadBase64(field))?; + if bytes.len() != len { + return Err(BuildError::BadLength { field, len }); + } + Ok(bytes) +} + +/// Build the `waE2E.Message` for a send request. +pub fn build_message(request: &SendRequest, now_secs: i64) -> Result { + match (&request.text, &request.media) { + (Some(text), None) => { + if text.is_empty() { + return Err(BuildError::EmptyText); + } + let mut message = wa::Message::default(); + message.conversation = Some(text.clone()); + Ok(message) + } + (None, Some(media)) => build_media(media, now_secs), + _ => Err(BuildError::NeedOneBody), + } +} + +fn build_media(d: &MediaDescriptor, now_secs: i64) -> Result { + if d.direct_path.is_empty() { + return Err(BuildError::Missing("direct_path")); + } + if d.mimetype.is_empty() { + return Err(BuildError::Missing("mimetype")); + } + if d.file_length == 0 { + return Err(BuildError::Missing("file_length")); + } + let media_key = decode_field(&d.media_key, "media_key", 32)?; + let file_sha256 = decode_field(&d.file_sha256, "file_sha256", 32)?; + let file_enc_sha256 = decode_field(&d.file_enc_sha256, "file_enc_sha256", 32)?; + let ts = Some(d.media_key_timestamp.unwrap_or(now_secs)); + let mut message = wa::Message::default(); + match d.kind.as_str() { + "image" => { + message.image_message = { + let mut inner = wa::message::ImageMessage::default(); + inner.url = d.url.clone(); + inner.direct_path = Some(d.direct_path.clone()); + inner.media_key = Some(media_key); + inner.file_sha256 = Some(file_sha256); + inner.file_enc_sha256 = Some(file_enc_sha256); + inner.file_length = Some(d.file_length); + inner.mimetype = Some(d.mimetype.clone()); + inner.caption = d.caption.clone(); + inner.width = d.width; + inner.height = d.height; + inner.media_key_timestamp = ts; + inner + } + .into(); + } + "video" => { + message.video_message = { + let mut inner = wa::message::VideoMessage::default(); + inner.url = d.url.clone(); + inner.direct_path = Some(d.direct_path.clone()); + inner.media_key = Some(media_key); + inner.file_sha256 = Some(file_sha256); + inner.file_enc_sha256 = Some(file_enc_sha256); + inner.file_length = Some(d.file_length); + inner.mimetype = Some(d.mimetype.clone()); + inner.caption = d.caption.clone(); + inner.width = d.width; + inner.height = d.height; + inner.seconds = d.seconds; + inner.media_key_timestamp = ts; + inner + } + .into(); + } + "audio" => { + message.audio_message = { + let mut inner = wa::message::AudioMessage::default(); + inner.url = d.url.clone(); + inner.direct_path = Some(d.direct_path.clone()); + inner.media_key = Some(media_key); + inner.file_sha256 = Some(file_sha256); + inner.file_enc_sha256 = Some(file_enc_sha256); + inner.file_length = Some(d.file_length); + inner.mimetype = Some(d.mimetype.clone()); + inner.seconds = d.seconds; + inner.ptt = d.ptt; + inner.media_key_timestamp = ts; + inner + } + .into(); + } + "document" => { + message.document_message = { + let mut inner = wa::message::DocumentMessage::default(); + inner.url = d.url.clone(); + inner.direct_path = Some(d.direct_path.clone()); + inner.media_key = Some(media_key); + inner.file_sha256 = Some(file_sha256); + inner.file_enc_sha256 = Some(file_enc_sha256); + inner.file_length = Some(d.file_length); + inner.mimetype = Some(d.mimetype.clone()); + inner.caption = d.caption.clone(); + inner.file_name = d.file_name.clone(); + inner.media_key_timestamp = ts; + inner + } + .into(); + } + "sticker" => { + message.sticker_message = { + let mut inner = wa::message::StickerMessage::default(); + inner.url = d.url.clone(); + inner.direct_path = Some(d.direct_path.clone()); + inner.media_key = Some(media_key); + inner.file_sha256 = Some(file_sha256); + inner.file_enc_sha256 = Some(file_enc_sha256); + inner.file_length = Some(d.file_length); + inner.mimetype = Some(d.mimetype.clone()); + inner.width = d.width; + inner.height = d.height; + inner.media_key_timestamp = ts; + inner + } + .into(); + } + other => return Err(BuildError::UnknownKind(other.to_owned())), + } + Ok(message) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn image() -> MediaDescriptor { + MediaDescriptor { + kind: "image".into(), + direct_path: "/v/t62.7118-24/abc".into(), + url: Some("https://mmg.whatsapp.net/v/t62.7118-24/abc".into()), + media_key: b64(&[1; 32]), + file_sha256: b64(&[2; 32]), + file_enc_sha256: b64(&[3; 32]), + file_length: 1234, + mimetype: "image/jpeg".into(), + caption: Some("look".into()), + file_name: None, + width: Some(10), + height: Some(20), + seconds: None, + ptt: None, + media_key_timestamp: None, + } + } + + #[test] + fn text_and_media_round_trip_through_the_message() { + let text = build_message( + &SendRequest { + to: "1@s.whatsapp.net".into(), + text: Some("hi".into()), + media: None, + }, + 0, + ) + .unwrap(); + assert_eq!(text_of(&text).as_deref(), Some("hi")); + + let sent = image(); + let message = build_message( + &SendRequest { + to: "1@s.whatsapp.net".into(), + text: None, + media: Some(sent.clone()), + }, + 1_700_000_000, + ) + .unwrap(); + let back = media_of(&message).unwrap(); + assert_eq!(back.media_key_timestamp, Some(1_700_000_000)); + assert_eq!( + MediaDescriptor { + media_key_timestamp: None, + ..back + }, + sent + ); + } + + #[test] + fn invalid_requests_are_refused() { + let both = SendRequest { + to: "x".into(), + text: Some("a".into()), + media: Some(image()), + }; + assert_eq!(build_message(&both, 0), Err(BuildError::NeedOneBody)); + let mut short = image(); + short.media_key = b64(&[1; 5]); + let request = SendRequest { + to: "x".into(), + text: None, + media: Some(short), + }; + assert!(matches!( + build_message(&request, 0), + Err(BuildError::BadLength { + field: "media_key", + .. + }) + )); + let mut odd = image(); + odd.kind = "hologram".into(); + let request = SendRequest { + to: "x".into(), + text: None, + media: Some(odd), + }; + assert!(matches!( + build_message(&request, 0), + Err(BuildError::UnknownKind(_)) + )); + } + #[test] + fn debug_never_prints_application_text_or_descriptors() { + let request = SendRequest { + to: "111@s.whatsapp.net".into(), + text: Some("private-plaintext".into()), + media: None, + }; + assert_eq!(format!("{request:?}"), "SendRequest()"); + let descriptor = MediaDescriptor { + kind: "image".into(), + direct_path: "/private".into(), + media_key: "private-key".into(), + file_sha256: "h".into(), + file_enc_sha256: "e".into(), + file_length: 1, + mimetype: "image/png".into(), + url: Some("https://private.invalid/?auth=private-auth".into()), + caption: None, + file_name: None, + width: None, + height: None, + seconds: None, + ptt: None, + media_key_timestamp: None, + }; + assert_eq!(format!("{descriptor:?}"), "MediaDescriptor()"); + } +} diff --git a/src/node.rs b/src/node.rs new file mode 100644 index 0000000..01932ad --- /dev/null +++ b/src/node.rs @@ -0,0 +1,918 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +//! The node: which connections it holds, its link to Polymorfa, and the loops +//! that keep both current. +//! +//! Every configured connection is tried on each lease tick. A node serves a +//! connection only while it holds the connection's lease in the shared store. +//! Holding the lease, it claims the connection from Polymorfa at that lease's +//! generation; Polymorfa routes to the highest generation it accepted. If +//! the node stops renewing (crash, partition), the lease expires and another +//! node takes it at a higher generation. Any write the old node still +//! attempts carries the old generation and the store refuses it. + +use crate::config::Resolved; +use crate::delivery::{Delivery, Webhook}; +use crate::keys::MasterKey; +use crate::link::{self, Handler, Link, LinkError, LinkSettings}; +use crate::proto::{ + self, ClaimConnection, Code, ConnectionEvent, ReleaseConnection, RunnerRequest, + ServiceResponse, connection_event::State, runner_response, service_request, +}; +use crate::ratelimit::SendLimiter; +use crate::share_replay::ShareReplay; +use crate::store::Backend; +use async_trait::async_trait; +use serde::Serialize; +use std::collections::{HashMap, HashSet}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, RwLock}; +use tokio::sync::{Mutex, OnceCell, watch}; +use tokio::task::JoinHandle; +use wacore_recordstore::{AesGcmSealer, Lease, LeaseStore, RecordSignalStore}; +use wacore_signal_ops::{OpsError, SignalOps}; + +pub type Store = RecordSignalStore; +pub type Ops = SignalOps; + +pub fn now_ms() -> u64 { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_or(0, |d| d.as_millis() as u64) +} + +/// What the application sees about a connection on this node. +#[derive(Debug, Clone, Default, Serialize)] +pub struct ConnectionView { + pub state: String, + pub pn: Option, + pub lid: Option, +} + +fn state_name(state: i32) -> String { + State::try_from(state) + .unwrap_or(State::Unspecified) + .as_str_name() + .trim_start_matches("STATE_") + .to_ascii_lowercase() +} + +/// A connection this node holds the lease for. +pub struct ActiveConnection { + pub id: String, + pub generation: u64, + lease: Mutex, + store: Arc, + shares: ShareReplay, + ops: OnceCell>, + claimed: AtomicBool, + claiming: AtomicBool, + lost: AtomicBool, + pub limiter: SendLimiter, + view: RwLock, +} + +impl ActiveConnection { + /// The Signal operations, if the device keys exist. + pub fn ops(&self) -> Option> { + self.ops.get().cloned() + } + + /// Surviving local uploaded prekeys, checked against this store's fence. + pub(crate) async fn uploaded_prekey_count(&self) -> Result { + Ok(self.store.uploaded_prekey_count().await?) + } + + pub(crate) async fn save_app_state_share( + &self, + receipt: &str, + share: &[u8], + ) -> Result<(), OpsError> { + Ok(self.shares.save(receipt, share, now_ms()).await?) + } + + pub(crate) async fn replay_app_state_share( + &self, + receipt: &str, + cutoff: u64, + ) -> Result, OpsError> { + Ok(self.shares.load(receipt, cutoff).await?) + } + + /// The Signal operations, creating the device keys on first use. + pub async fn ops_or_create(&self) -> Result, OpsError> { + self.ops + .get_or_try_init(|| async { + match SignalOps::create(self.store.clone()).await { + Ok(ops) => Ok(Arc::new(ops)), + // Created by an earlier holder after this lease opened. + Err(OpsError::InvalidInput(_)) => { + Ok(Arc::new(SignalOps::open(self.store.clone()).await?)) + } + Err(e) => Err(e), + } + }) + .await + .cloned() + } + + pub fn is_claimed(&self) -> bool { + self.claimed.load(Ordering::SeqCst) + } + + pub fn is_lost(&self) -> bool { + self.lost.load(Ordering::SeqCst) + } + + /// Fail closed when a paused process resumes beyond its lease deadline. + pub async fn can_serve(&self) -> bool { + !self.is_lost() && now_ms() < self.lease.lock().await.expires_at_ms + } + + pub fn view(&self) -> ConnectionView { + self.view.read().expect("view lock").clone() + } + + fn set_view(&self, state: i32, pn: &str, lid: &str) { + let mut view = self.view.write().expect("view lock"); + view.state = state_name(state); + if !pn.is_empty() { + view.pn = Some(pn.to_owned()); + } + if !lid.is_empty() { + view.lid = Some(lid.to_owned()); + } + } + + pub async fn lease(&self) -> Lease { + self.lease.lock().await.clone() + } +} + +pub struct Node { + me: std::sync::Weak, + pub cfg: Arc, + backend: Arc, + master: MasterKey, + /// Unique process incarnation; stable diagnostic node IDs cannot share a fence. + lease_holder: String, + connections: RwLock>>, + /// Connections Polymorfa reported logged out; not taken again until restart. + retired: RwLock>, + /// Highest generation this process stopped serving without a confirmed + /// release. A reacquired lease at or below it shares the old fence, so it is + /// released again instead of served. + abandoned: RwLock>, + link: RwLock>>, + pub delivery: Delivery, + shutdown: watch::Sender, +} + +impl Node { + pub fn new(cfg: Resolved, backend: Backend) -> Arc { + let webhook = cfg.webhook_url.clone().map(|url| Webhook { + url, + secret: cfg.webhook_secret.clone(), + }); + let delivery = Delivery::new(webhook, cfg.sse, cfg.ack_timeout); + Arc::new_cyclic(|me| Self { + me: me.clone(), + master: MasterKey::new(&cfg.master_key), + lease_holder: format!( + "{}:{}", + cfg.node_id, + hex::encode(rand::random::<[u8; 16]>()) + ), + cfg: Arc::new(cfg), + backend: Arc::new(backend), + connections: RwLock::new(HashMap::new()), + retired: RwLock::new(HashSet::new()), + abandoned: RwLock::new(HashMap::new()), + link: RwLock::new(None), + delivery, + shutdown: watch::channel(false).0, + }) + } + + pub fn backend(&self) -> &Arc { + &self.backend + } + + pub fn connection(&self, id: &str) -> Option> { + self.connections + .read() + .expect("connections lock") + .get(id) + .cloned() + } + + pub fn connections(&self) -> Vec> { + let mut all: Vec<_> = self + .connections + .read() + .expect("connections lock") + .values() + .cloned() + .collect(); + all.sort_by(|a, b| a.id.cmp(&b.id)); + all + } + + pub fn is_configured(&self, id: &str) -> bool { + self.cfg.connections.iter().any(|s| s == id) + } + + pub fn link(&self) -> Option> { + self.link + .read() + .expect("link lock") + .clone() + .filter(|l| !l.is_closed()) + } + + pub fn shutdown_signal(&self) -> watch::Receiver { + self.shutdown.subscribe() + } + + /// Stop serving `id` because another node owns it now. The lease is not + /// released: it is no longer ours. + pub fn lose(&self, id: &str, generation: u64, why: &str) { + let removed = { + let mut connections = self.connections.write().expect("connections lock"); + let Some(connection) = connections.get(id).filter(|s| s.generation == generation) + else { + return; + }; + // A lease tick must see the refused fence before the missing + // connection, even when the later durable release fails. + connection.lost.store(true, Ordering::SeqCst); + self.remember_abandoned(id, generation); + connections.remove(id) + }; + if let Some(connection) = removed { + tracing::warn!( + connection = id, + generation = connection.generation, + reason = why, + "stopped serving connection" + ); + } + } + + /// Give a connection up voluntarily: tell Polymorfa, then release the lease + /// so another node can take it at once. + pub async fn release(&self, id: &str) { + let removed = { + let mut connections = self.connections.write().expect("connections lock"); + let removed = connections.remove(id); + if let Some(connection) = &removed { + connection.lost.store(true, Ordering::SeqCst); + // Publish the refusal before a lease tick can observe the + // missing connection, and before either release can suspend. + self.remember_abandoned(id, connection.generation); + } + removed + }; + let Some(connection) = removed else { return }; + if let Some(link) = self.link() + && connection.is_claimed() + { + let op = service_request::Op::ReleaseConnection(ReleaseConnection {}); + if let Err(e) = link.request(id, connection.generation, op).await { + tracing::debug!(connection = id, error = %e, "ReleaseConnection was not acknowledged"); + } + } + let lease = connection.lease().await; + self.abandon(id, &lease).await; + tracing::info!( + connection = id, + generation = connection.generation, + "released connection" + ); + } + + /// Map an operation error to a contract error, dropping the connection if + /// the store says another node took it. + pub fn ops_error(&self, connection: &ActiveConnection, error: &OpsError) -> proto::Error { + if error.is_fence_lost() { + self.lose( + &connection.id, + connection.generation, + "store refused a write: lease moved to another node", + ); + return proto::Error::new( + Code::NotLeaseHolder, + "this node no longer holds the connection", + ); + } + match error { + OpsError::Store(_) => proto::Error::new(Code::Unavailable, "storage is unavailable"), + OpsError::InvalidInput(message) => { + proto::Error::new(Code::InvalidArgument, message.clone()) + } + OpsError::NotInitialised => proto::Error::new( + Code::ConnectionUnavailable, + "device keys do not exist yet; GetIdentity creates them", + ), + OpsError::Pairing { code, text } => proto::Error::new( + Code::InvalidArgument, + format!("pairing refused: {code} {text}"), + ), + OpsError::Send(_) => proto::Error::new(Code::Unavailable, "building the stanza failed"), + OpsError::BundleRefused(rejection) => proto::Error::new( + Code::Refused, + format!("recipient identity not verified: {rejection}"), + ), + OpsError::Signal(_) => proto::Error::new(Code::Internal, "signal protocol error"), + } + } + + async fn activate(&self, lease: Lease) -> anyhow::Result> { + let data_key = self + .master + .data_key_for(self.backend.as_ref(), &lease.fence) + .await?; + let sealer = Arc::new(AesGcmSealer::new(&data_key)); + let store = Arc::new(RecordSignalStore::new( + self.backend.clone(), + sealer.clone(), + lease.fence.clone(), + )); + let shares = ShareReplay::new(self.backend.clone(), sealer, lease.fence.clone()); + let ops = OnceCell::new(); + match SignalOps::open(store.clone()).await { + Ok(opened) => { + let _ = ops.set(Arc::new(opened)); + } + Err(OpsError::NotInitialised) => {} + Err(e) => { + return Err(anyhow::anyhow!( + "opening the connection's Signal state: {e}" + )); + } + } + Ok(Arc::new(ActiveConnection { + id: lease.fence.scope.to_string(), + generation: lease.fence.generation, + lease: Mutex::new(lease), + store, + shares, + ops, + claimed: AtomicBool::new(false), + claiming: AtomicBool::new(false), + lost: AtomicBool::new(false), + limiter: SendLimiter::new(), + view: RwLock::new(ConnectionView { + state: "unclaimed".into(), + ..Default::default() + }), + })) + } + + /// Publish only while the connection that began acquisition is current. + /// Holding the link read lock through insertion orders publication before + /// disconnect's link removal and subsequent connection snapshot. + fn publish_acquired( + &self, + id: &str, + connection: Arc, + origin: &Arc, + ) -> bool { + let current = self.link.read().expect("link lock"); + if *self.shutdown.borrow() + || !current + .as_ref() + .is_some_and(|link| Arc::ptr_eq(link, origin) && !link.is_closed()) + { + return false; + } + self.connections + .write() + .expect("connections lock") + .insert(id.to_owned(), connection); + true + } + + /// One pass over every configured connection: renew what we hold, try to + /// take what is free, and claim anything Polymorfa has not accepted yet. + pub async fn tick_leases(self: &Arc) { + let ttl = self.cfg.lease_ttl_ms; + for id in &self.cfg.connections { + if *self.shutdown.borrow() { + return; + } + if self.retired.read().expect("retired lock").contains(id) { + continue; + } + match self.connection(id) { + Some(connection) => { + let lease = connection.lease().await; + let now = now_ms(); + if now >= lease.expires_at_ms { + self.lose(id, connection.generation, "lease expired before renewal"); + self.abandon(id, &lease).await; + continue; + } + match self.backend.renew(&lease, now, ttl).await { + Ok(Some(renewed)) => *connection.lease.lock().await = renewed, + Ok(None) => { + self.lose(id, connection.generation, "lease taken over"); + continue; + } + Err(e) => { + tracing::warn!(connection = %id, error = %e, "lease renewal failed"); + // Stop before another node can take it. + let margin = self.cfg.lease_renew.as_millis() as u64; + if now.saturating_add(margin) >= lease.expires_at_ms { + self.lose( + id, + connection.generation, + "lease could not be renewed before it expired", + ); + self.abandon(id, &lease).await; + continue; + } + } + } + if !connection.is_claimed() { + self.spawn_claim(connection); + } + } + None => { + // A partitioned replica must leave free leases for a healthy one. + let Some(origin) = self.link() else { continue }; + let acquired = self + .backend + .acquire(id, &self.lease_holder, now_ms(), ttl) + .await; + match acquired { + Ok(Some(lease)) => { + let generation = lease.fence.generation; + let stale = self + .abandoned + .read() + .expect("abandoned lock") + .get(id) + .is_some_and(|g| generation <= *g); + if stale { + // Same holder, same fence: release so the next + // acquisition increments the generation. + if self.backend.release(&lease).await.is_ok() { + tracing::info!(connection = %id, generation, "released an abandoned fence before reacquiring"); + } + continue; + } + match self.activate(lease.clone()).await { + Ok(connection) => { + if !self.publish_acquired(id, connection.clone(), &origin) { + self.abandon(id, &lease).await; + continue; + } + tracing::info!(connection = %id, generation, "took connection lease"); + self.spawn_claim(connection); + } + Err(e) => { + tracing::error!(connection = %id, error = %e, "cannot serve connection; releasing its lease"); + self.abandon(id, &lease).await; + } + } + } + Ok(None) => {} + Err(e) => { + tracing::warn!(connection = %id, error = %e, "lease acquire failed") + } + } + } + } + } + } + + /// Polymorfa refused this connection's generation, for example after a + /// runner-side pause. Stop serving it and release the durable lease, so the + /// next acquisition claims a strictly higher generation instead of + /// retrying the refused one forever. + pub(crate) async fn refused_generation(&self, connection: &ActiveConnection, why: &str) { + let lease = connection.lease().await; + self.lose(&connection.id, connection.generation, why); + self.abandon(&connection.id, &lease).await; + } + + /// Stop owning `lease` after serving stopped without a renewal. Release it so + /// the next acquisition increments the generation; remember the generation in + /// case the release fails, so this process never serves that fence again. + async fn abandon(&self, id: &str, lease: &Lease) { + let generation = lease.fence.generation; + self.remember_abandoned(id, generation); + if let Err(e) = self.backend.release(lease).await { + tracing::warn!(connection = id, generation, error = %e, "abandoned lease release failed; its fence stays refused"); + } + } + + fn remember_abandoned(&self, id: &str, generation: u64) { + let mut abandoned = self.abandoned.write().expect("abandoned lock"); + let entry = abandoned.entry(id.to_owned()).or_insert(generation); + *entry = (*entry).max(generation); + } + + fn spawn_claim(&self, connection: Arc) { + let Some(link) = self.link() else { return }; + let Some(node) = self.me.upgrade() else { + return; + }; + if connection.claiming.swap(true, Ordering::SeqCst) { + return; + } + tokio::spawn(async move { + node.claim(&connection, &link).await; + connection.claiming.store(false, Ordering::SeqCst); + }); + } + + async fn claim(&self, connection: &Arc, link: &Arc) { + if !connection.can_serve().await { + return; + } + let op = service_request::Op::ClaimConnection(ClaimConnection { + generation: connection.generation, + }); + match link.request(&connection.id, 0, op).await { + Ok(runner_response::Result::ConnectionClaimed(claimed)) => { + if !connection.can_serve().await { + return; + } + connection.claimed.store(true, Ordering::SeqCst); + connection.set_view(claimed.state, &claimed.pn, &claimed.lid); + tracing::info!( + connection = %connection.id, + generation = connection.generation, + state = %state_name(claimed.state), + "Polymorfa accepted the claim" + ); + if claimed.state == State::LoggedOut as i32 { + self.retire(&connection.id).await; + return; + } + if !claimed.pn.is_empty() + && let Some(ops) = connection.ops() + { + let lid = Some(claimed.lid.as_str()).filter(|l| !l.is_empty()); + if let Err(e) = ops.set_own_jids(&claimed.pn, lid).await { + let error = self.ops_error(connection, &e); + tracing::warn!(connection = %connection.id, error = %error, "could not record own JIDs"); + } + } + } + Ok(_) => { + tracing::warn!(connection = %connection.id, "unexpected answer to ClaimConnection") + } + Err(LinkError::Remote(error)) => match error.code_enum() { + Code::NotLeaseHolder => { + self.refused_generation(connection, "Polymorfa refused this claim generation") + .await; + } + Code::ConnectionUnavailable => tracing::warn!( + connection = %connection.id, + "Polymorfa refused the claim: the connection is not in this project or is logged out" + ), + _ => tracing::warn!(connection = %connection.id, error = %error, "claim refused"), + }, + Err(e) => tracing::warn!(connection = %connection.id, error = %e, "claim failed"), + } + } + + async fn retire(&self, id: &str) { + self.retired + .write() + .expect("retired lock") + .insert(id.to_owned()); + tracing::warn!( + connection = id, + "connection is logged out; stopped serving it. Its sealed state is kept; remove it from the configuration and delete its records when no longer needed" + ); + self.release(id).await; + } + + async fn prune(self: &Arc) { + let now = now_ms(); + let buffer_cutoff = + now.saturating_sub(self.cfg.decrypt_buffer_retention.as_millis() as u64); + let sent_cutoff = now.saturating_sub(self.cfg.sent_message_retention.as_millis() as u64); + for connection in self.connections() { + if !connection.can_serve().await { + continue; + } + let Some(ops) = connection.ops() else { + continue; + }; + let result = async { + let buffered = ops.prune_decrypt_buffer(buffer_cutoff).await?; + let sent = ops.prune_sent_messages(sent_cutoff).await?; + let shares = connection.shares.prune(buffer_cutoff).await?; + Ok::<_, OpsError>((buffered, sent, shares)) + } + .await; + match result { + Ok((buffered, sent, shares)) if buffered + sent + shares > 0 => { + tracing::info!(connection = %connection.id, buffered, sent, shares, "pruned expired records") + } + Ok(_) => {} + Err(e) => { + let error = self.ops_error(&connection, &e); + tracing::warn!(connection = %connection.id, error = %error, "pruning failed"); + } + } + } + } + + /// Start the link, lease and pruning loops. + pub fn spawn_loops(self: &Arc, service_version: &str) -> Vec> { + let settings = LinkSettings { + url: self.cfg.polymorfa_url.clone(), + credential: self.cfg.credential.clone(), + node_id: self.cfg.node_id.clone(), + service_version: service_version.to_owned(), + request_timeout: self.cfg.request_timeout, + reconnect_min: self.cfg.reconnect_min, + reconnect_max: self.cfg.reconnect_max, + }; + let link_task = tokio::spawn(link::run( + settings, + self.clone() as Arc, + self.shutdown_signal(), + )); + let leases = { + let node = self.clone(); + let mut stop = self.shutdown_signal(); + tokio::spawn(async move { + let mut ticker = tokio::time::interval(node.cfg.lease_renew); + ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); + loop { + tokio::select! { + _ = ticker.tick() => node.tick_leases().await, + _ = stop.changed() => break, + } + } + }) + }; + let pruner = { + let node = self.clone(); + let mut stop = self.shutdown_signal(); + tokio::spawn(async move { + let mut ticker = tokio::time::interval(node.cfg.prune_interval); + loop { + tokio::select! { + _ = ticker.tick() => node.prune().await, + _ = stop.changed() => break, + } + } + }) + }; + vec![link_task, leases, pruner] + } + + /// Release every connection, then stop the loops. + pub async fn shutdown(&self) { + // Prevent renewal/reacquisition while releasing connections. Socket closure + // removes routing claims even if ReleaseConnection races the stop signal. + self.shutdown.send_replace(true); + // Wait for publications that read the previous flag before taking the + // connection snapshot. Later publications observe shutdown and refuse. + drop(self.link.write().expect("link lock")); + let ids: Vec = self.connections().iter().map(|s| s.id.clone()).collect(); + for id in ids { + self.release(&id).await; + } + } +} + +#[async_trait] +impl Handler for Node { + async fn runner_request( + &self, + request: RunnerRequest, + ) -> Result { + let Some(connection) = self.connection(&request.connection_id) else { + return Err(proto::Error::new( + Code::NotLeaseHolder, + "this node does not hold the connection", + )); + }; + if request.lease_generation != connection.generation || !connection.can_serve().await { + return Err(proto::Error::new( + Code::NotLeaseHolder, + format!( + "request generation {} is not this node's generation {}", + request.lease_generation, connection.generation + ), + )); + } + let Some(op) = request.op else { + return Err(proto::Error::new(Code::Unsupported, "unknown operation")); + }; + crate::serve::handle(self, &connection, op).await + } + + async fn connection_event(&self, event: ConnectionEvent, origin: Arc) { + // Events carry no generation. One from a replaced link describes the + // old connection and must not retire or reclaim its successor. + if !self + .link() + .is_some_and(|current| Arc::ptr_eq(¤t, &origin)) + { + tracing::debug!(connection = %event.connection_id, "ignored a connection event from a previous link"); + return; + } + let Some(connection) = self.connection(&event.connection_id) else { + return; + }; + connection.set_view(event.state, &event.pn, &event.lid); + tracing::info!(connection = %connection.id, state = %state_name(event.state), "connection state changed"); + match State::try_from(event.state).unwrap_or(State::Unspecified) { + State::LoggedOut => self.retire(&connection.id).await, + // Polymorfa no longer routes to us; claim again. + State::Paused => { + connection.claimed.store(false, Ordering::SeqCst); + self.spawn_claim(connection); + } + _ => {} + } + } + + async fn connected(&self, link: Arc) { + *self.link.write().expect("link lock") = Some(link); + for connection in self.connections() { + self.spawn_claim(connection); + } + } + + async fn disconnected(&self) { + *self.link.write().expect("link lock") = None; + // A new socket must claim a strictly higher generation. Relinquish + // these fences before reconnect so the durable store increments them. + let ids: Vec<_> = self.connections().iter().map(|s| s.id.clone()).collect(); + for id in ids { + self.release(&id).await; + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::config::Config; + use std::future::Future; + use std::task::{Context, Poll, Waker}; + use wacore_recordstore::MemoryRecordStore; + + const CONNECTION: &str = "fixture-session"; + + fn node() -> Arc { + let cfg = Config::from_toml( + r#" +node_id = "fixture-node" +[polymorfa] +url = "ws://127.0.0.1:1/signal" +credential = "fixture-project-principal" +connections = ["fixture-session"] +[storage] +driver = "memory" +master_key = "CQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQk=" +[http] +listen = "127.0.0.1:0" +secret = "fixture-application-secret" +"#, + ) + .unwrap() + .resolve() + .unwrap(); + Node::new(cfg, Backend::Memory(Arc::new(MemoryRecordStore::new()))) + } + + async fn acquired(node: &Node) -> (Lease, Arc) { + let lease = node + .backend + .acquire( + CONNECTION, + &node.lease_holder, + now_ms(), + node.cfg.lease_ttl_ms, + ) + .await + .unwrap() + .unwrap(); + let connection = node.activate(lease.clone()).await.unwrap(); + (lease, connection) + } + + #[tokio::test] + async fn release_refuses_the_fence_before_waiting_for_storage() { + let node = node(); + let (lease, connection) = acquired(&node).await; + node.connections + .write() + .unwrap() + .insert(CONNECTION.into(), connection.clone()); + // Suspend release before durable storage can acknowledge it. Even if + // that operation later fails, acquisition must already refuse the fence. + let held = connection.lease.lock().await; + let mut release = std::pin::pin!(node.release(CONNECTION)); + assert!(matches!( + release + .as_mut() + .poll(&mut Context::from_waker(Waker::noop())), + Poll::Pending + )); + assert!(connection.is_lost()); + assert!(node.connection(CONNECTION).is_none()); + assert_eq!( + node.abandoned.read().unwrap().get(CONNECTION), + Some(&lease.fence.generation) + ); + drop(held); + release.await; + let (next, _) = acquired(&node).await; + assert!(next.fence.generation > lease.fence.generation); + } + + #[tokio::test] + async fn losing_a_fence_records_refusal_before_exposing_a_missing_connection() { + let node = node(); + let (lease, connection) = acquired(&node).await; + node.connections + .write() + .unwrap() + .insert(CONNECTION.into(), connection.clone()); + let removal_is_locked = { + let held = node.abandoned.write().unwrap(); + let losing = { + let node = node.clone(); + std::thread::spawn(move || { + node.lose(CONNECTION, lease.fence.generation, "fixture refusal") + }) + }; + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2); + while !connection.is_lost() { + if std::time::Instant::now() >= deadline { + break; + } + std::thread::yield_now(); + } + // Refusal is waiting for the held map. No lease tick may observe a + // missing connection until that refusal has become visible. + let locked = connection.is_lost() && node.connections.try_read().is_err(); + drop(held); + losing.join().unwrap(); + locked + }; + assert!(removal_is_locked); + assert!(node.connection(CONNECTION).is_none()); + assert_eq!( + node.abandoned.read().unwrap().get(CONNECTION), + Some(&connection.generation) + ); + *node.link.write().unwrap() = Some(Link::fixture()); + node.tick_leases().await; + assert!( + node.connection(CONNECTION).is_none(), + "the refused generation was served again" + ); + let (next, _) = acquired(&node).await; + assert!(next.fence.generation > connection.generation); + } + + #[tokio::test] + async fn shutdown_refuses_a_late_acquisition_even_without_watch_receivers() { + let node = node(); + let origin = Link::fixture(); + *node.link.write().unwrap() = Some(origin.clone()); + let (lease, connection) = acquired(&node).await; + node.shutdown().await; + assert!(*node.shutdown.borrow()); + assert!(!node.publish_acquired(CONNECTION, connection, &origin)); + assert!(node.connection(CONNECTION).is_none()); + node.abandon(CONNECTION, &lease).await; + } + + #[tokio::test] + async fn acquisition_cannot_publish_after_its_link_disconnects_or_is_replaced() { + let node = node(); + let origin = Link::fixture(); + *node.link.write().unwrap() = Some(origin.clone()); + let (lease, connection) = acquired(&node).await; + + // Acquisition/activation awaited storage while disconnect took its + // connection snapshot. The late result must never become a live connection. + node.disconnected().await; + assert!(!node.publish_acquired(CONNECTION, connection.clone(), &origin)); + assert!(node.connection(CONNECTION).is_none()); + let successor = Link::fixture(); + *node.link.write().unwrap() = Some(successor.clone()); + assert!(!node.publish_acquired(CONNECTION, connection.clone(), &origin)); + node.abandon(CONNECTION, &lease).await; + + let (_, next) = acquired(&node).await; + assert!(node.publish_acquired(CONNECTION, next, &successor)); + node.disconnected().await; + assert!(node.connection(CONNECTION).is_none()); + } +} diff --git a/src/proto.rs b/src/proto.rs new file mode 100644 index 0000000..7b27b24 --- /dev/null +++ b/src/proto.rs @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +//! Generated types for `polymorfa.signal.v1` plus small constructors. + +#![allow(clippy::all, missing_docs)] + +include!(concat!(env!("OUT_DIR"), "/polymorfa.signal.v1.rs")); + +/// The contract version this build speaks. +pub const PROTOCOL_VERSION: u32 = 1; + +/// WebSocket subprotocol name. +pub const SUBPROTOCOL: &str = "polymorfa.signal.v1"; + +pub use error::Code; + +impl Error { + pub fn new(code: Code, message: impl Into) -> Self { + Self { + code: code as i32, + message: message.into(), + decrypt_failure: DecryptFailure::Unspecified as i32, + } + } + + pub fn decrypt_failed(failure: DecryptFailure, message: impl Into) -> Self { + Self { + code: Code::DecryptFailed as i32, + message: message.into(), + decrypt_failure: failure as i32, + } + } + + pub fn code_enum(&self) -> Code { + Code::try_from(self.code).unwrap_or(Code::Unspecified) + } +} + +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}: {}", self.code_enum().as_str_name(), self.message) + } +} + +impl std::error::Error for Error {} + +impl Frame { + pub fn with(id: u64, body: frame::Body) -> Self { + Self { + id, + body: Some(body), + } + } +} diff --git a/src/ratelimit.rs b/src/ratelimit.rs new file mode 100644 index 0000000..19d0a78 --- /dev/null +++ b/src/ratelimit.rs @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +//! Client-side send ceiling: at most `limit` sends in any one-second window +//! per connection, matching `Welcome.max_sends_per_second`. Polymorfa enforces +//! the same ceiling and answers `CODE_RATE_LIMITED` past it; pacing here +//! keeps a burst from the application from turning into refused sends. + +use std::collections::VecDeque; +use std::time::Duration; +use tokio::sync::Mutex; +use tokio::time::Instant; + +const WINDOW: Duration = Duration::from_secs(1); + +pub struct SendLimiter { + sent: Mutex>, +} + +impl Default for SendLimiter { + fn default() -> Self { + Self::new() + } +} + +impl SendLimiter { + pub fn new() -> Self { + Self { + sent: Mutex::new(VecDeque::new()), + } + } + + /// Wait until one more send fits under `limit` per second, then record + /// it. Callers queue in arrival order on the internal lock. + pub async fn acquire(&self, limit: u32) { + let limit = limit.max(1) as usize; + let mut sent = self.sent.lock().await; + loop { + let now = Instant::now(); + while sent + .front() + .is_some_and(|t| now.duration_since(*t) >= WINDOW) + { + sent.pop_front(); + } + if sent.len() < limit { + sent.push_back(now); + return; + } + let oldest = *sent.front().expect("the window is full"); + tokio::time::sleep_until(oldest + WINDOW).await; + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test(start_paused = true)] + async fn no_window_ever_holds_more_than_the_limit() { + let limiter = SendLimiter::new(); + let start = Instant::now(); + let mut stamps = Vec::new(); + for _ in 0..12 { + limiter.acquire(5).await; + stamps.push(Instant::now().duration_since(start)); + } + for (i, t) in stamps.iter().enumerate() { + let in_window = stamps[i..].iter().filter(|u| **u < *t + WINDOW).count(); + assert!(in_window <= 5, "{in_window} sends within one second"); + } + // 12 sends at 5 per second need two full windows. + assert!(stamps[11] >= Duration::from_secs(2)); + } +} diff --git a/src/resolver.rs b/src/resolver.rs new file mode 100644 index 0000000..e90d7d0 --- /dev/null +++ b/src/resolver.rs @@ -0,0 +1,207 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +//! The [`SendContextResolver`] the stanza builder uses, answered by Polymorfa +//! over the link: device lists (`ResolveDevices`), prekey bundles +//! (`FetchPreKeys`) and group routing (`ResolveGroup`). Polymorfa keeps the +//! WhatsApp connection, so these lookups are the only way the node learns +//! who to encrypt for. Nothing here is cached; each send asks again. + +use crate::link::{Link, LinkError}; +use crate::proto::{self, runner_response, service_request}; +use async_trait::async_trait; +use std::collections::HashMap; +use std::future::Future; +use std::pin::Pin; +use std::sync::Arc; +use std::time::Duration; +use wacore::client::context::{GroupRoutingInfo, SendContextResolver, SessionLockGuard}; +use wacore::libsignal::protocol::PreKeyBundle; +use wacore::prekeys::PreKeyFetchOutcome; +use wacore::runtime::{AbortHandle, Runtime}; +use wacore::types::message::AddressingMode; +use wacore_binary::Jid; +use wacore_signal_ops::{FetchedBundle, RemoteBundle, SignalResolver, VerifiablePrekeys}; + +/// Tokio-backed [`Runtime`] for the stanza builder. +pub struct TokioRuntime; + +type BoxFuture = Pin + Send + 'static>>; + +impl Runtime for TokioRuntime { + fn spawn(&self, future: BoxFuture) -> AbortHandle { + let task = tokio::spawn(future); + AbortHandle::new(move || task.abort()) + } + + fn sleep(&self, duration: Duration) -> Pin + Send>> { + Box::pin(tokio::time::sleep(duration)) + } + + fn spawn_blocking(&self, f: Box) -> BoxFuture { + Box::pin(async move { + let _ = tokio::task::spawn_blocking(f).await; + }) + } + + fn yield_now(&self) -> Option + Send>>> { + Some(Box::pin(tokio::task::yield_now())) + } +} + +pub struct RemoteResolver { + pub link: Arc, + pub connection_id: String, + pub generation: u64, +} + +fn parse_jids(jids: &[String]) -> anyhow::Result> { + jids.iter() + .map(|j| { + j.parse::() + .map_err(|_| anyhow::anyhow!("Polymorfa returned an invalid JID")) + }) + .collect() +} + +fn array(bytes: &[u8], what: &str) -> anyhow::Result<[u8; N]> { + bytes + .try_into() + .map_err(|_| anyhow::anyhow!("{what} must be {N} bytes")) +} + +/// Contract bundle to the form `wacore-signal-ops` validates. +pub fn remote_bundle(bundle: &proto::Bundle) -> anyhow::Result { + Ok(RemoteBundle { + registration_id: bundle.registration_id, + device_id: bundle.device_id, + identity_key: array(&bundle.identity_key, "identity_key")?, + signed_prekey_id: bundle.signed_pre_key_id, + signed_prekey: array(&bundle.signed_pre_key, "signed_pre_key")?, + signed_prekey_signature: array( + &bundle.signed_pre_key_signature, + "signed_pre_key_signature", + )?, + prekey: match &bundle.pre_key { + Some(pk) => Some((pk.id, array(&pk.public_key, "pre_key.public_key")?)), + None => None, + }, + device_identity: (!bundle.device_identity.is_empty()) + .then(|| bundle.device_identity.clone()), + }) +} + +impl RemoteResolver { + async fn ask(&self, op: service_request::Op) -> anyhow::Result { + self.link + .request(&self.connection_id, self.generation, op) + .await + .map_err(|e: LinkError| anyhow::Error::new(e)) + } + + /// Fetch bundles from Polymorfa, keeping each companion's device identity. + async fn fetch_bundles(&self, jids: &[Jid]) -> anyhow::Result> { + let op = service_request::Op::FetchPreKeys(proto::FetchPreKeys { + jids: jids.iter().map(ToString::to_string).collect(), + }); + let runner_response::Result::Bundles(bundles) = self.ask(op).await? else { + return Err(LinkError::UnexpectedResponse.into()); + }; + let mut out = Vec::with_capacity(bundles.bundles.len()); + for (jid, bundle) in &bundles.bundles { + let jid: Jid = jid + .parse() + .map_err(|_| anyhow::anyhow!("Polymorfa returned an invalid JID"))?; + out.push((jid, remote_bundle(bundle)?)); + } + Ok(out) + } +} + +fn prekey_bundle(bundle: &RemoteBundle) -> anyhow::Result { + bundle + .to_prekey_bundle() + .map_err(|_| anyhow::anyhow!("Polymorfa returned an invalid prekey bundle")) +} + +#[async_trait] +impl SignalResolver for RemoteResolver { + async fn fetch_verifiable_prekeys(&self, jids: &[Jid]) -> anyhow::Result { + let mut out = VerifiablePrekeys::default(); + for (jid, bundle) in self.fetch_bundles(jids).await? { + out.bundles.insert( + jid, + FetchedBundle { + bundle: prekey_bundle(&bundle)?, + device_identity: bundle.device_identity, + }, + ); + } + Ok(out) + } +} + +#[async_trait] +impl SendContextResolver for RemoteResolver { + async fn resolve_devices(&self, jids: &[Jid]) -> anyhow::Result> { + let op = service_request::Op::ResolveDevices(proto::ResolveDevices { + jids: jids.iter().map(ToString::to_string).collect(), + }); + match self.ask(op).await? { + runner_response::Result::Devices(devices) => parse_jids(&devices.jids), + _ => Err(LinkError::UnexpectedResponse.into()), + } + } + + async fn fetch_prekeys(&self, jids: &[Jid]) -> anyhow::Result> { + let mut out = HashMap::new(); + for (jid, bundle) in self.fetch_bundles(jids).await? { + out.insert(jid, prekey_bundle(&bundle)?); + } + Ok(out) + } + + async fn fetch_prekeys_for_identity_check( + &self, + jids: &[Jid], + ) -> anyhow::Result { + Ok(PreKeyFetchOutcome { + bundles: self.fetch_prekeys(jids).await?, + rejected: Vec::new(), + }) + } + + /// `SignalOps` already holds every addressed device's session and account + /// locks, in both address forms, for the whole group fan-out, so wacore + /// needs no second lock set here. + async fn lock_device_sessions(&self, _device_jids: &[Jid]) -> SessionLockGuard { + SessionLockGuard::none() + } + + async fn resolve_group_routing_info(&self, jid: &Jid) -> anyhow::Result> { + let op = service_request::Op::ResolveGroup(proto::ResolveGroup { + jid: jid.to_string(), + }); + let runner_response::Result::GroupRouting(routing) = self.ask(op).await? else { + return Err(LinkError::UnexpectedResponse.into()); + }; + let mode = match routing.addressing_mode.as_str() { + "lid" => AddressingMode::Lid, + "pn" => AddressingMode::Pn, + // The contract allows only pn or lid; never guess the address family. + _ => anyhow::bail!("Polymorfa returned an unknown addressing mode"), + }; + let participants = parse_jids(&routing.participants)?; + let mut lid_to_pn = HashMap::with_capacity(routing.lid_to_pn.len()); + for (lid_user, pn) in &routing.lid_to_pn { + let pn: Jid = pn + .parse() + .map_err(|_| anyhow::anyhow!("Polymorfa returned an invalid JID"))?; + lid_to_pn.insert(lid_user.as_str().into(), pn); + } + Ok(Arc::new(GroupRoutingInfo::with_lid_to_pn_map( + participants, + mode, + lid_to_pn, + ))) + } +} diff --git a/src/send.rs b/src/send.rs new file mode 100644 index 0000000..ee00280 --- /dev/null +++ b/src/send.rs @@ -0,0 +1,237 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +//! Sending a message the application handed to the node. +//! +//! The node builds the encrypted stanza itself (device lists, prekeys and +//! group routing come from Polymorfa through [`RemoteResolver`]), then asks +//! Polymorfa to transmit it with `SendStanza`. Every `SendStanza`, including +//! a resend, first waits for the per-connection ceiling. +//! +//! When the server reports a participant-hash mismatch, the device list +//! changed while the message was in flight. WhatsApp Web then refreshes the +//! device list or group and resends the same message ID +//! (`WAWebSendUserMsgJob.js:53-79`, `WAWebSendGroupSkmsgJob.js:194-213`, +//! Cellar bundle `whatsapp-1049950004`). The node rebuilds once with fresh +//! lookups and the same ID. WA Web excludes previously addressed devices; +//! this implementation re-encrypts for every current device. Provider +//! acceptance of that broader resend remains an integration gate. + +use crate::link::LinkError; +use crate::message::{self, BuildError, SendRequest}; +use crate::node::{ActiveConnection, Node}; +use crate::proto::{self, Code, SendStanza, runner_response, service_request}; +use crate::resolver::{RemoteResolver, TokioRuntime}; +use serde::Serialize; +use std::sync::Arc; +use wacore_binary::{Jid, JidExt as _}; +use wacore_signal_ops::OpsError; + +#[derive(Debug, Clone, Serialize, PartialEq, Eq)] +pub struct SendOutcome { + pub message_id: String, + pub server_timestamp: i64, + /// The stanza was sent a second time after a device-list change. + pub resent: bool, +} + +#[derive(Debug, thiserror::Error)] +pub enum SendError { + #[error("{0}")] + Invalid(String), + #[error("this node does not hold the connection")] + NotLeaseHolder, + #[error("the connection is not paired yet")] + NotPaired, + #[error("not connected to Polymorfa")] + Disconnected, + #[error("send outcome is unconfirmed; do not automatically resend")] + Unconfirmed { message_id: String }, + #[error("Polymorfa refused the send: {0}")] + Refused(proto::Error), + #[error("send ceiling reached")] + RateLimited, + #[error("connection storage is unavailable")] + StorageUnavailable, + #[error("building the stanza failed")] + Build, +} + +impl From for SendError { + fn from(e: BuildError) -> Self { + SendError::Invalid(e.to_string()) + } +} + +enum Built { + Direct { + stanza: Vec, + phash: Option, + }, + Group { + stanza: Vec, + phash: Option, + targets: Vec, + epoch: u64, + }, +} + +fn ops_failure(node: &Node, connection: &ActiveConnection, error: &OpsError) -> SendError { + if error.is_fence_lost() { + node.ops_error(connection, error); + return SendError::NotLeaseHolder; + } + match error { + OpsError::InvalidInput(m) if m.contains("not paired") || m.contains("own JID") => { + SendError::NotPaired + } + OpsError::InvalidInput(m) => SendError::Invalid(m.clone()), + // A storage outage is retryable; it is not a stanza construction failure. + OpsError::Store(_) => SendError::StorageUnavailable, + _ => SendError::Build, + } +} + +pub async fn send( + node: &Node, + connection: &Arc, + request: SendRequest, +) -> Result { + let to: Jid = request + .to + .parse() + .map_err(|_| SendError::Invalid("to is not a valid JID".into()))?; + let now = crate::delivery::now_secs(); + let message = message::build_message(&request, now)?; + let ops = connection.ops().ok_or(SendError::NotPaired)?; + let own = connection.view().pn.and_then(|pn| pn.parse::().ok()); + let message_id = + wacore::request::RequestUtils::message_id_at(own.as_ref(), u64::try_from(now).unwrap_or(0)); + + let mut resent = false; + let outcome = async { + loop { + let link = node.link().ok_or(SendError::Disconnected)?; + if !connection.can_serve().await || !connection.is_claimed() { + return Err(SendError::NotLeaseHolder); + } + let resolver = RemoteResolver { + link: link.clone(), + connection_id: connection.id.clone(), + generation: connection.generation, + }; + let built = if to.is_group() { + let prepared = ops + .send_group(&TokioRuntime, &resolver, &to, &message, &message_id) + .await + .map_err(|e| ops_failure(node, connection, &e))?; + Built::Group { + stanza: prepared.stanza, + phash: prepared.phash, + targets: prepared.distribution_targets, + epoch: prepared.distribution_epoch, + } + } else { + let prepared = ops + .send_direct(&TokioRuntime, &resolver, &to, &message, &message_id) + .await + .map_err(|e| ops_failure(node, connection, &e))?; + Built::Direct { + stanza: prepared.stanza, + phash: prepared.phash, + } + }; + let (stanza, phash) = match &built { + Built::Direct { stanza, phash } | Built::Group { stanza, phash, .. } => { + (stanza.clone(), phash.clone().unwrap_or_default()) + } + }; + + connection.limiter.acquire(link.max_sends_per_second()).await; + if !connection.can_serve().await || !connection.is_claimed() { + return Err(SendError::NotLeaseHolder); + } + let op = service_request::Op::SendStanza(SendStanza { + node: stanza, + phash, + }); + let result = match link.request(&connection.id, connection.generation, op).await { + Ok(runner_response::Result::Send(result)) => result, + Ok(_) => { + return Err(SendError::Unconfirmed { + message_id: message_id.clone(), + }); + } + Err(LinkError::Remote(error)) => { + return Err(match error.code_enum() { + Code::RateLimited => SendError::RateLimited, + Code::NotLeaseHolder => { + node.refused_generation( + connection, + "Polymorfa refused a send at this generation", + ) + .await; + SendError::NotLeaseHolder + } + _ => SendError::Refused(error), + }); + } + Err(LinkError::Disconnected) => return Err(SendError::Disconnected), + Err(LinkError::Timeout | LinkError::ResponseLost | LinkError::UnexpectedResponse) => { + return Err(SendError::Unconfirmed { + message_id: message_id.clone(), + }); + } + }; + + // WA Web marks the sender key as held once the server acks, before + // it looks at the participant hash (WAWebSendGroupSkmsgJob.js:188-205, + // Cellar bundle whatsapp-1051710191). + if let Built::Group { targets, epoch, .. } = &built + && !targets.is_empty() + { + match ops.mark_sender_key_distributed(&to, targets, *epoch).await { + Ok(true) => {} + // The group's sender key rotated after this send was built, + // so its targets hold the old key; the next send redistributes. + Ok(false) => tracing::debug!(connection = %connection.id, message_id = %message_id, "sender key rotated before the send was acknowledged"), + Err(e) => { + // The runner has already confirmed the send. A failed local + // distribution mark must not invite a new message ID. Only fence + // loss stops the same-ID mismatch resend and retires this connection. + node.ops_error(connection, &e); + tracing::warn!(connection = %connection.id, message_id = %message_id, "send accepted; sender-key distribution was not recorded"); + if e.is_fence_lost() { + return Ok(SendOutcome { + message_id: message_id.clone(), + server_timestamp: result.server_timestamp, + resent, + }); + } + } + } + } + tracing::info!(connection = %connection.id, message_id = %message_id, resent, "sent"); + if result.participant_hash_mismatch && !resent { + tracing::info!(connection = %connection.id, message_id = %message_id, "device list changed; resending once"); + resent = true; + continue; + } + return Ok(SendOutcome { + message_id: message_id.clone(), + server_timestamp: result.server_timestamp, + resent, + }); + } + } + .await; + // A mismatch ACK may already represent delivery to some participants. + // Any later failure belongs to this message, even if the resend was never + // built or queued. Ordinary retryable errors would invite a fresh ID. + outcome.map_err(|error| { + if resent { + SendError::Unconfirmed { message_id } + } else { + error + } + }) +} diff --git a/src/serve.rs b/src/serve.rs new file mode 100644 index 0000000..91d4193 --- /dev/null +++ b/src/serve.rs @@ -0,0 +1,495 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +//! Serving `RunnerRequest` operations with `wacore-signal-ops`. +//! +//! Nothing returned here carries plaintext, with the contract's one +//! exception: an explicitly enabled app-state key share from an own device. +//! Error messages name the failure, never content or key material. + +use crate::message::{self, Event}; +use crate::node::{ActiveConnection, Node}; +use crate::proto::{ + self, Code, DecryptFailure, Identity, MigrateResult, PreKey, PreKeyCount, PreKeyUploadBatch, + PreKeys, ReceiveResult, ServiceResponse, SignedPairing, Stanza, runner_request::Op, + service_response::Result as Out, +}; +use crate::resolver::{RemoteResolver, TokioRuntime, remote_bundle}; +use std::sync::Arc; +use wacore::libsignal::protocol::SignalProtocolError; +use wacore::types::jid::JidExt as _; +use wacore::types::message::AddressingMode; +use wacore_binary::Jid; +use wacore_signal_ops::{ + OpsError, ReceiveKind, ReceiveRequest, Received, RetryRequest, RetryRoute, +}; +use waproto::whatsapp as wa; + +fn respond(result: Out) -> Result { + Ok(ServiceResponse { + result: Some(result), + }) +} + +fn invalid(message: impl Into) -> proto::Error { + proto::Error::new(Code::InvalidArgument, message) +} + +fn jid(value: &str, field: &str) -> Result { + value + .parse() + .map_err(|_| invalid(format!("{field} is not a valid JID"))) +} + +pub async fn handle( + node: &Node, + connection: &Arc, + op: Op, +) -> Result { + if let Op::GetIdentity(_) = op { + let ops = connection + .ops_or_create() + .await + .map_err(|e| node.ops_error(connection, &e))?; + let identity = ops.public_identity().await; + return respond(Out::Identity(Identity { + registration_id: identity.registration_id, + identity_key: identity.identity_key.to_vec(), + signed_pre_key_id: identity.signed_prekey.id, + signed_pre_key: identity.signed_prekey.public_key.to_vec(), + signed_pre_key_signature: identity.signed_prekey.signature.to_vec(), + adv_secret: identity.adv_secret.to_vec(), + })); + } + let Some(ops) = connection.ops() else { + return Err(proto::Error::new( + Code::ConnectionUnavailable, + "device keys do not exist yet; GetIdentity creates them", + )); + }; + let fail = |e: OpsError| node.ops_error(connection, &e); + match op { + Op::GetIdentity(_) => unreachable!("handled above"), + Op::Receive(receive) => self::receive(node, connection, &ops, receive).await, + Op::GeneratePreKeys(request) => { + let keys = ops + .generate_prekeys(&TokioRuntime, request.count) + .await + .map_err(fail)?; + respond(Out::PreKeys(PreKeys { + pre_keys: keys + .into_iter() + .map(|k| PreKey { + id: k.id, + public_key: k.public_key.to_vec(), + }) + .collect(), + })) + } + Op::MarkPreKeysUploaded(request) => { + ops.mark_prekeys_uploaded(&request.ids) + .await + .map_err(fail)?; + respond(Out::Ok(proto::Ok {})) + } + Op::PreparePreKeyUpload(request) => { + let batch = ops + .prepare_prekey_upload(&TokioRuntime, request.count) + .await + .map_err(fail)?; + respond(Out::PreKeyUploadBatch(PreKeyUploadBatch { + batch_id: batch.batch_id, + pre_keys: batch + .pre_keys + .into_iter() + .map(|key| PreKey { + id: key.id, + public_key: key.public_key.to_vec(), + }) + .collect(), + })) + } + Op::AcknowledgePreKeyUpload(request) => { + ops.acknowledge_prekey_upload(&request.batch_id, &request.ids) + .await + .map_err(fail)?; + respond(Out::Ok(proto::Ok {})) + } + Op::UploadedPreKeyCount(_) => { + let count = uploaded_prekey_count(node, connection).await?; + respond(Out::PreKeyCount(PreKeyCount { count })) + } + Op::SignPairing(request) => { + let signed = ops + .sign_pairing(&request.device_identity_container) + .await + .map_err(fail)?; + respond(Out::SignedPairing(SignedPairing { + signed_device_identity: signed.signed_identity, + })) + } + Op::SetOwnJids(request) => { + jid(&request.pn, "pn")?; + let lid = Some(request.lid.as_str()).filter(|l| !l.is_empty()); + ops.set_own_jids(&request.pn, lid).await.map_err(fail)?; + respond(Out::Ok(proto::Ok {})) + } + Op::Resend(request) => { + let Some(link) = node.link() else { + return Err(proto::Error::new(Code::Unavailable, "not connected")); + }; + let chat = jid(&request.chat, "chat")?; + let requester = jid(&request.requester, "requester")?; + let encryption_jid = if request.encryption_jid.is_empty() { + requester.clone() + } else { + jid(&request.encryption_jid, "encryption_jid")? + }; + let route = if request.is_group { + RetryRoute::Group { + addressing_mode: match request.addressing_mode.as_str() { + "lid" => AddressingMode::Lid, + "pn" => AddressingMode::Pn, + _ => return Err(invalid("group addressing_mode must be pn or lid")), + }, + } + } else if request.is_broadcast { + RetryRoute::BroadcastList + } else { + RetryRoute::Direct { + recipient: if request.recipient.is_empty() { + None + } else { + Some(jid(&request.recipient, "recipient")?) + }, + } + }; + let bundle = match &request.bundle { + Some(b) => Some(remote_bundle(b).map_err(|e| invalid(e.to_string()))?), + None => None, + }; + let resolver = RemoteResolver { + link, + connection_id: connection.id.clone(), + generation: connection.generation, + }; + let node_bytes = ops + .resend( + &resolver, + RetryRequest { + chat: &chat, + message_id: &request.message_id, + requester: &requester, + encryption_jid: &encryption_jid, + route, + retry_count: u8::try_from(request.retry_count).unwrap_or(u8::MAX), + bundle: bundle.as_ref(), + }, + ) + .await + .map_err(fail)?; + respond(Out::Stanza(Stanza { node: node_bytes })) + } + Op::ForgetIdentity(request) => { + let user = jid(&request.user, "user")?; + ops.forget_user(&user).await.map_err(fail)?; + // Polymorfa is trusted to report identity changes (README), so + // its report also releases the account's pin: the next primary + // identity seen is pinned in its place. + ops.forget_account_identity(&user).await.map_err(fail)?; + respond(Out::Ok(proto::Ok {})) + } + Op::MigratePnToLid(request) => { + let pn = jid(&request.pn, "pn")?; + let lid = jid(&request.lid, "lid")?; + let moved = ops.migrate_pn_to_lid(&pn, &lid).await.map_err(fail)?; + respond(Out::Migrate(MigrateResult { + moved: u32::try_from(moved).unwrap_or(u32::MAX), + })) + } + } +} + +/// Map a receive failure to the contract's decrypt failures. +pub fn receive_error(node: &Node, connection: &ActiveConnection, error: &OpsError) -> proto::Error { + if error.is_fence_lost() { + return node.ops_error(connection, error); + } + match error { + OpsError::Store(_) | OpsError::Signal(SignalProtocolError::BackendError(..)) => { + proto::Error::new(Code::Unavailable, "storage is unavailable") + } + OpsError::Signal(SignalProtocolError::DuplicatedMessage(..)) => { + proto::Error::decrypt_failed(DecryptFailure::OldCounter, "message counter already used") + } + OpsError::Signal( + SignalProtocolError::NoSenderKeyState(_) | SignalProtocolError::InvalidSenderKeySession, + ) => proto::Error::decrypt_failed( + DecryptFailure::NoSenderKey, + "no sender key for this sender", + ), + OpsError::InvalidInput(message) if message.starts_with("plaintext did not decode") => { + proto::Error::decrypt_failed( + DecryptFailure::Undecodable, + "decrypted payload is not a message", + ) + } + OpsError::NotInitialised => node.ops_error(connection, error), + _ => proto::Error::decrypt_failed(DecryptFailure::Unspecified, "decryption failed"), + } +} + +async fn receive( + node: &Node, + connection: &Arc, + ops: &crate::node::Ops, + receive: proto::Receive, +) -> Result { + let sender = jid(&receive.sender, "sender")?; + let address = sender.to_protocol_address(); + let kind = match receive.kind.as_str() { + "pkmsg" => ReceiveKind::PreKey, + "msg" => ReceiveKind::Message, + "skmsg" => ReceiveKind::SenderKey, + _ => return Err(invalid("kind must be pkmsg, msg or skmsg")), + }; + let padding_version = u8::try_from(receive.padding_version) + .map_err(|_| invalid("padding_version out of range"))?; + let received = ops + .receive(ReceiveRequest { + chat: &receive.chat, + sender: &address, + kind, + ciphertext: &receive.ciphertext, + padding_version, + is_from_me: receive.is_from_me, + device_identity: (!receive.device_identity.is_empty()) + .then_some(receive.device_identity.as_slice()), + }) + .await + .map_err(|e| { + let error = receive_error(node, connection, &e); + tracing::info!( + connection = %connection.id, + message_id = %receive.message_id, + kind = %receive.kind, + code = error.code_enum().as_str_name(), + "receive failed" + ); + error + })?; + let message = match received { + Received::AlreadyDelivered { receipt_key } => { + let app_state_key_share = if node.cfg.share_app_state_keys && receive.is_from_me { + let cutoff = crate::node::now_ms() + .saturating_sub(node.cfg.decrypt_buffer_retention.as_millis() as u64); + connection + .replay_app_state_share(&receipt_key, cutoff) + .await + .map_err(|e| node.ops_error(connection, &e))? + } else { + Vec::new() + }; + if !connection.can_serve().await { + return Err(proto::Error::new( + Code::NotLeaseHolder, + "the lease expired before receive replay", + )); + } + return respond(Out::Receive(ReceiveResult { + already_delivered: true, + app_state_key_share, + ..Default::default() + })); + } + Received::Message(message) => message, + }; + + // Explicit operator consent in addition to the pinned core own-device gate. + let app_state_key_share = if node.cfg.share_app_state_keys && receive.is_from_me { + message + .app_state_key_share + .as_ref() + .map(message::encode) + .unwrap_or_default() + } else { + Vec::new() + }; + if let Some(event) = event_for(node, connection, &receive, &message) { + let kind = event.kind; + if let Err(e) = node.delivery.deliver(event).await { + tracing::info!( + connection = %connection.id, + message_id = %receive.message_id, + error = %e, + "delivery not acknowledged; the message stays sealed in the buffer for redelivery" + ); + return Err(proto::Error::new(Code::Unavailable, e.to_string())); + } + tracing::debug!(connection = %connection.id, message_id = %receive.message_id, kind, "delivered"); + } + if !connection.can_serve().await { + return Err(proto::Error::new( + Code::NotLeaseHolder, + "the lease expired before delivery was recorded", + )); + } + // The runner has not acknowledged this response. Preserve only the share + // before dropping buffered plaintext, so a lost reply remains replayable. + if !app_state_key_share.is_empty() { + connection + .save_app_state_share(&message.receipt_key, &app_state_key_share) + .await + .map_err(|e| node.ops_error(connection, &e))?; + } + ops.mark_delivered(&message.receipt_key) + .await + .map_err(|e| node.ops_error(connection, &e))?; + respond(Out::Receive(ReceiveResult { + already_delivered: false, + redelivered: message.redelivered, + sender_key_rejected: message.sender_key_rejected, + app_state_key_share, + })) +} + +/// The application event ID for a received payload. The receipt key keys +/// the connection's own decrypt buffer and is identical for every connection that +/// receives the same ciphertext (two connections in one group), so the event ID +/// also binds the connection: acknowledging one connection's event never completes +/// another's. Stable across redelivery to the same connection. +pub fn event_id(connection_id: &str, receipt_key: &str) -> String { + use sha2::{Digest, Sha256}; + let mut hash = Sha256::new(); + hash.update(b"signal-node app event v1\0"); + hash.update((connection_id.len() as u64).to_be_bytes()); + hash.update(connection_id.as_bytes()); + hash.update(receipt_key.as_bytes()); + hex::encode(hash.finalize()) +} + +/// The application event for a received message, or `None` when the +/// message is protocol-only (sender keys, app-state keys, peer data +/// operations) or is history and history delivery is off. +fn event_for( + node: &Node, + connection: &ActiveConnection, + receive: &proto::Receive, + received: &wacore_signal_ops::ReceivedMessage, +) -> Option { + let content = &received.content; + if content.is_skdm_only { + return None; + } + let id = event_id(&connection.id, &received.receipt_key); + let base = |kind: &'static str| Event { + id: id.clone(), + kind, + connection_id: connection.id.clone(), + chat: receive.chat.clone(), + sender: receive.sender.clone(), + sender_alt: Some(receive.sender_alt.clone()).filter(|s| !s.is_empty()), + message_id: receive.message_id.clone(), + timestamp: receive.timestamp, + is_from_me: receive.is_from_me, + redelivered: received.redelivered, + identity_changed: received.identity_changed, + text: None, + media: None, + history: None, + message_proto: String::new(), + }; + // Protocol fields are independent of ordinary fields in waE2E.Message. + // Preserve mixed-message content while removing protocol keys/descriptors + // from its application protobuf (including history when consent is off). + let mut app_message = content.message.clone(); + app_message.protocol_message = Default::default(); + app_message.sender_key_distribution_message = Default::default(); + // Context accompanies content; by itself it is not an application event. + // Clear only the probe so mixed-message output retains its context. + let mut content_probe = app_message.clone(); + content_probe.message_context_info = Default::default(); + let has_app_content = !message::encode(&content_probe).is_empty(); + let has_protocol_request = + content + .message + .protocol_message + .as_option() + .is_some_and(|protocol| { + protocol.app_state_sync_key_request.is_set() + || protocol.peer_data_operation_request_message.is_set() + || matches!(protocol.r#type, Some(wa::message::protocol_message::Type::AppStateSyncKeyShare | wa::message::protocol_message::Type::AppStateSyncKeyRequest | wa::message::protocol_message::Type::PeerDataOperationRequestMessage | wa::message::protocol_message::Type::PeerDataOperationRequestResponseMessage)) + }); + if has_protocol_request && !has_app_content { + return None; + } + if let Some(protocol) = &content.protocol_message { + if let Some(history) = &protocol.history_sync_notification + && !has_app_content + { + // History comes only from this account's own devices. + if !node.cfg.deliver_history || !receive.is_from_me { + tracing::debug!(connection = %connection.id, "history notification not delivered (history delivery is off)"); + return None; + } + return Some(Event { + history: Some(message::history_descriptor(history)), + ..base("history.sync") + }); + } + if !has_app_content + && (protocol.app_state_sync_key_share.is_some() + || protocol.peer_data_operation_request_response.is_some()) + { + return None; + } + } + let wa_message = if has_app_content { + &app_message + } else { + &content.message + }; + Some(Event { + text: message::text_of(wa_message), + media: message::media_of(wa_message), + message_proto: base64_of(&message::encode(wa_message)), + history: content + .protocol_message + .as_ref() + .and_then(|p| p.history_sync_notification.as_ref()) + .filter(|_| node.cfg.deliver_history && receive.is_from_me) + .map(message::history_descriptor), + ..base("message") + }) +} + +fn base64_of(bytes: &[u8]) -> String { + use base64::Engine as _; + base64::engine::general_purpose::STANDARD.encode(bytes) +} + +/// Surviving uploaded keys in customer-local state, not a provider pool count. +pub async fn uploaded_prekey_count( + node: &Node, + connection: &ActiveConnection, +) -> Result { + connection + .uploaded_prekey_count() + .await + .map_err(|error| node.ops_error(connection, &error)) +} + +#[cfg(test)] +mod tests { + use super::event_id; + + #[test] + fn event_ids_are_stable_per_connection_and_distinct_across_connections() { + let receipt = "same-group-ciphertext"; + assert_eq!(event_id("alpha", receipt), event_id("alpha", receipt)); + assert_ne!(event_id("alpha", receipt), event_id("beta", receipt)); + assert_ne!(event_id("alpha", receipt), event_id("alpha", "other")); + // Length-prefixing the connection keeps ("ab", "c…") apart from ("a", "bc…"). + assert_ne!(event_id("ab", "cd"), event_id("a", "bcd")); + assert_eq!(event_id("alpha", receipt).len(), 64); + } +} diff --git a/src/share_replay.rs b/src/share_replay.rs new file mode 100644 index 0000000..26961ab --- /dev/null +++ b/src/share_replay.rs @@ -0,0 +1,192 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +//! Sealed own-device key shares that survive a lost runner receive response. +//! No application plaintext is retained here. Entries share the decrypt +//! buffer's retention window, but do not depend on its private record format. + +use std::sync::Arc; +use wacore::store::error::{Result, StoreError}; +use wacore_recordstore::{Fence, Namespace, RecordStore, Sealer, WriteOp, is_record_exists}; + +const PREFIX: &str = "signal-node.app-state-share/"; +const FORMAT: u8 = 1; + +pub(crate) struct ShareReplay { + records: Arc, + sealer: Arc, + fence: Fence, +} + +impl ShareReplay { + pub(crate) fn new(records: Arc, sealer: Arc, fence: Fence) -> Self { + Self { + records, + sealer, + fence, + } + } + + fn aad(&self, key: &str) -> Vec { + let mut aad = b"signal-node/app-state-share/v1\0".to_vec(); + for part in [self.fence.scope.as_bytes(), key.as_bytes()] { + aad.extend_from_slice(&(part.len() as u64).to_be_bytes()); + aad.extend_from_slice(part); + } + aad + } + + async fn open(&self, key: &str, bytes: &[u8]) -> Result> { + self.sealer + .open(&self.aad(key), bytes) + .await + .map_err(|e| StoreError::Serialization(Box::new(e))) + } + + fn inserted_ms(bytes: &[u8]) -> Result { + if bytes.len() < 9 || bytes[0] != FORMAT { + return Err(StoreError::Validation( + "invalid app-state share replay record".into(), + )); + } + Ok(u64::from_be_bytes( + bytes[1..9].try_into().expect("checked length"), + )) + } + + pub(crate) async fn save(&self, receipt: &str, share: &[u8], now: u64) -> Result<()> { + let key = format!("{PREFIX}{receipt}"); + let mut plain = Vec::with_capacity(9 + share.len()); + plain.push(FORMAT); + plain.extend_from_slice(&now.to_be_bytes()); + plain.extend_from_slice(share); + let sealed = self + .sealer + .seal(&self.aad(&key), &plain) + .await + .map_err(|e| StoreError::Serialization(Box::new(e)))?; + match self + .records + .write( + &self.fence, + &[WriteOp::Insert { + ns: Namespace::Device, + key, + value: sealed, + }], + ) + .await + { + // Repeating receive must not extend the original retention window. + Err(e) if is_record_exists(&e) => Ok(()), + result => result, + } + } + + pub(crate) async fn load(&self, receipt: &str, cutoff: u64) -> Result> { + let key = format!("{PREFIX}{receipt}"); + let Some(sealed) = self + .records + .get(&self.fence.scope, Namespace::Device, &key) + .await? + else { + return Ok(Vec::new()); + }; + let plain = self.open(&key, &sealed).await?; + if Self::inserted_ms(&plain)? < cutoff { + return Ok(Vec::new()); + } + Ok(plain[9..].to_vec()) + } + + pub(crate) async fn prune(&self, cutoff: u64) -> Result { + let mut expired = Vec::new(); + for key in self + .records + .scan_keys(&self.fence.scope, Namespace::Device, PREFIX, None) + .await? + { + let Some(sealed) = self + .records + .get(&self.fence.scope, Namespace::Device, &key) + .await? + else { + continue; + }; + let inserted = self + .open(&key, &sealed) + .await + .and_then(|b| Self::inserted_ms(&b)); + // A corrupt entry cannot answer a retry; do not let it prevent + // pruning later entries or retain unusable key material forever. + if inserted.map_or(true, |time| time < cutoff) { + expired.push(WriteOp::Delete { + ns: Namespace::Device, + key, + }); + } + } + if !expired.is_empty() { + self.records.write(&self.fence, &expired).await?; + } + Ok(expired.len()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use wacore_recordstore::{AesGcmSealer, LeaseStore, MemoryRecordStore}; + + #[tokio::test] + async fn replay_is_sealed_fenced_and_retains_the_original_expiry() { + let records = Arc::new(MemoryRecordStore::new()); + let lease = records + .acquire("scope", "first", 0, 100) + .await + .unwrap() + .unwrap(); + let sealer = Arc::new(AesGcmSealer::new(&[7; 32])); + let replay = ShareReplay::new(records.clone(), sealer.clone(), lease.fence.clone()); + let share = [9; 32]; + replay.save("receipt", &share, 10).await.unwrap(); + replay.save("receipt", &[8; 32], 20).await.unwrap(); + assert_eq!(replay.load("receipt", 10).await.unwrap(), share); + assert!(replay.load("receipt", 11).await.unwrap().is_empty()); + let key = format!("{PREFIX}receipt"); + let sealed = records + .get("scope", Namespace::Device, &key) + .await + .unwrap() + .unwrap(); + assert!(!sealed.windows(share.len()).any(|b| b == share)); + records + .write( + &lease.fence, + &[WriteOp::Put { + ns: Namespace::Device, + key: format!("{PREFIX}other"), + value: sealed, + }], + ) + .await + .unwrap(); + assert!( + replay.load("other", 0).await.is_err(), + "moving a sealed share to another receipt must fail" + ); + let next = records + .acquire("scope", "second", 101, 100) + .await + .unwrap() + .unwrap(); + assert!(replay.save("late", &share, 101).await.is_err()); + assert!( + replay.prune(11).await.is_err(), + "the old fence cannot delete shares" + ); + let replacement = ShareReplay::new(records, sealer, next.fence); + assert_eq!(replacement.load("receipt", 10).await.unwrap(), share); + assert_eq!(replacement.prune(11).await.unwrap(), 2); + assert!(replacement.load("receipt", 0).await.unwrap().is_empty()); + } +} diff --git a/src/store/mod.rs b/src/store/mod.rs new file mode 100644 index 0000000..c6a6165 --- /dev/null +++ b/src/store/mod.rs @@ -0,0 +1,217 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +//! Storage drivers for [`RecordStore`] and [`LeaseStore`]. +//! +//! Every driver passes the `wacore-recordstore` conformance suite. Values +//! arrive already sealed; drivers store opaque bytes and enforce two rules in +//! the database itself: a write batch applies only while its fence is the +//! scope's current lease generation, and an exclusive insert fails the whole +//! batch if its key exists. + +mod postgres; +mod sqlite; + +pub use postgres::PostgresStore; +pub use sqlite::SqliteStore; + +use async_trait::async_trait; +use std::sync::Arc; +use wacore::store::error::{Result, StoreError}; +use wacore_recordstore::{ + Fence, Lease, LeaseStore, MemoryRecordStore, Namespace, RecordStore, WriteOp, +}; + +use crate::config::{Resolved, StorageDriver}; + +/// The configured driver. +#[derive(Clone)] +pub enum Backend { + Memory(Arc), + Postgres(PostgresStore), + Sqlite(SqliteStore), +} + +impl Backend { + /// Connect and run migrations. + pub async fn connect(config: &Resolved) -> anyhow::Result { + Ok(match config.storage_driver { + StorageDriver::Memory => Backend::Memory(Arc::new(MemoryRecordStore::new())), + StorageDriver::Postgres => Backend::Postgres( + PostgresStore::connect(config.storage_url.expose(), config.max_connections).await?, + ), + StorageDriver::Sqlite => Backend::Sqlite( + SqliteStore::connect(config.storage_url.expose(), config.max_connections).await?, + ), + }) + } + + pub fn name(&self) -> &'static str { + match self { + Backend::Memory(_) => "memory", + Backend::Postgres(_) => "postgres", + Backend::Sqlite(_) => "sqlite", + } + } +} + +#[async_trait] +impl RecordStore for Backend { + async fn get(&self, scope: &str, ns: Namespace, key: &str) -> Result>> { + match self { + Backend::Memory(s) => s.get(scope, ns, key).await, + Backend::Postgres(s) => s.get(scope, ns, key).await, + Backend::Sqlite(s) => s.get(scope, ns, key).await, + } + } + + async fn get_many( + &self, + scope: &str, + ns: Namespace, + keys: &[&str], + ) -> Result)>> { + match self { + Backend::Memory(s) => s.get_many(scope, ns, keys).await, + Backend::Postgres(s) => s.get_many(scope, ns, keys).await, + Backend::Sqlite(s) => s.get_many(scope, ns, keys).await, + } + } + + async fn scan_keys( + &self, + scope: &str, + ns: Namespace, + prefix: &str, + limit: Option, + ) -> Result> { + match self { + Backend::Memory(s) => s.scan_keys(scope, ns, prefix, limit).await, + Backend::Postgres(s) => s.scan_keys(scope, ns, prefix, limit).await, + Backend::Sqlite(s) => s.scan_keys(scope, ns, prefix, limit).await, + } + } + + async fn write(&self, fence: &Fence, ops: &[WriteOp]) -> Result<()> { + match self { + Backend::Memory(s) => s.write(fence, ops).await, + Backend::Postgres(s) => s.write(fence, ops).await, + Backend::Sqlite(s) => s.write(fence, ops).await, + } + } +} + +#[async_trait] +impl LeaseStore for Backend { + async fn acquire( + &self, + scope: &str, + holder: &str, + now_ms: u64, + ttl_ms: u64, + ) -> Result> { + match self { + Backend::Memory(s) => s.acquire(scope, holder, now_ms, ttl_ms).await, + Backend::Postgres(s) => s.acquire(scope, holder, now_ms, ttl_ms).await, + Backend::Sqlite(s) => s.acquire(scope, holder, now_ms, ttl_ms).await, + } + } + + async fn renew(&self, lease: &Lease, now_ms: u64, ttl_ms: u64) -> Result> { + match self { + Backend::Memory(s) => s.renew(lease, now_ms, ttl_ms).await, + Backend::Postgres(s) => s.renew(lease, now_ms, ttl_ms).await, + Backend::Sqlite(s) => s.renew(lease, now_ms, ttl_ms).await, + } + } + + async fn release(&self, lease: &Lease) -> Result<()> { + match self { + Backend::Memory(s) => s.release(lease).await, + Backend::Postgres(s) => s.release(lease).await, + Backend::Sqlite(s) => s.release(lease).await, + } + } +} + +/// Wrap a driver error. The message names the operation only; it never +/// carries values. +pub(crate) fn db_error(op: &'static str, error: sqlx::Error) -> StoreError { + StoreError::Database(Box::new(DbError { op, source: error })) +} + +#[derive(Debug, thiserror::Error)] +#[error("database {op} failed")] +pub(crate) struct DbError { + op: &'static str, + #[source] + source: sqlx::Error, +} + +/// Exclusive upper bound of the keys starting with `prefix`, in byte order. +/// `None` means unbounded (empty prefix, or a prefix of only 0xFF bytes). +pub(crate) fn prefix_end(prefix: &[u8]) -> Option> { + let mut end = prefix.to_vec(); + while let Some(last) = end.pop() { + if last < 0xFF { + end.push(last + 1); + return Some(end); + } + } + None +} + +/// Lease decision shared by the SQL drivers, given the current row. +pub(crate) enum LeaseDecision { + Refuse, + Take { generation: u64 }, +} + +pub(crate) fn decide(row: Option<(&str, u64, u64)>, holder: &str, now_ms: u64) -> LeaseDecision { + match row { + None => LeaseDecision::Take { generation: 1 }, + Some((current, generation, _)) if current == holder => LeaseDecision::Take { generation }, + Some((_, _, expires_at_ms)) if expires_at_ms > now_ms => LeaseDecision::Refuse, + Some((_, generation, _)) => LeaseDecision::Take { + generation: generation + 1, + }, + } +} + +pub(crate) fn empty_holder() -> StoreError { + StoreError::Validation("lease holder must not be empty".into()) +} + +/// SQL integers are signed; lease times and generations stay below 2^63. +pub(crate) fn to_i64(value: u64) -> i64 { + i64::try_from(value).unwrap_or(i64::MAX) +} + +pub(crate) fn to_u64(value: i64) -> u64 { + u64::try_from(value).unwrap_or(0) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn prefix_end_is_the_next_byte_string() { + assert_eq!(prefix_end(b""), None); + assert_eq!(prefix_end(b"ab").as_deref(), Some(&b"ac"[..])); + assert_eq!(prefix_end(b"a\xff").as_deref(), Some(&b"b"[..])); + assert_eq!(prefix_end(b"\xff\xff"), None); + } + + #[test] + fn the_memory_driver_passes_conformance() { + let store = Backend::Memory(Arc::new(MemoryRecordStore::new())); + futures_executor_block_on(wacore_recordstore::conformance::run_all(&store)); + } + + fn futures_executor_block_on(f: F) -> F::Output { + tokio::runtime::Builder::new_current_thread() + .build() + .unwrap() + .block_on(f) + } +} diff --git a/src/store/postgres.rs b/src/store/postgres.rs new file mode 100644 index 0000000..a4f2fdc --- /dev/null +++ b/src/store/postgres.rs @@ -0,0 +1,309 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +//! PostgreSQL driver. +//! +//! A write batch runs in one transaction that first locks the scope's lease +//! row with `FOR SHARE`. Taking over a lease updates that row, which needs a +//! conflicting lock, so a takeover and a write from the old holder are +//! serialised: the write either commits before the generation moves or sees +//! the new generation and applies nothing. + +use super::{LeaseDecision, db_error, decide, empty_holder, prefix_end, to_i64, to_u64}; +use async_trait::async_trait; +use sqlx::postgres::{PgPool, PgPoolOptions}; +use sqlx::{Postgres, Row, Transaction}; +use std::sync::Arc; +use wacore::store::error::{Result, StoreError}; +use wacore_recordstore::{ + Fence, FenceLost, Lease, LeaseStore, Namespace, RecordExists, RecordStore, WriteOp, +}; + +static MIGRATOR: sqlx::migrate::Migrator = sqlx::migrate!("./migrations/postgres"); + +#[derive(Clone)] +pub struct PostgresStore { + pool: PgPool, +} + +impl PostgresStore { + pub async fn connect(url: &str, max_connections: u32) -> anyhow::Result { + let pool = PgPoolOptions::new() + .max_connections(max_connections) + .connect(url) + .await + .map_err(|e| anyhow::anyhow!("connecting to PostgreSQL: {e}"))?; + Self::from_pool(pool).await + } + + /// Use an existing pool; runs migrations. + pub async fn from_pool(pool: PgPool) -> anyhow::Result { + MIGRATOR + .run(&pool) + .await + .map_err(|e| anyhow::anyhow!("running PostgreSQL migrations: {e}"))?; + Ok(Self { pool }) + } + + async fn begin(&self, op: &'static str) -> Result> { + self.pool.begin().await.map_err(|e| db_error(op, e)) + } +} + +fn fence_lost(fence: &Fence) -> StoreError { + FenceLost { + scope: fence.scope.clone(), + generation: fence.generation, + } + .into_store_error() +} + +#[async_trait] +impl RecordStore for PostgresStore { + async fn get(&self, scope: &str, ns: Namespace, key: &str) -> Result>> { + let row = sqlx::query( + "SELECT value FROM signal_records WHERE scope = $1 AND ns = $2 AND key = $3", + ) + .bind(scope) + .bind(ns.tag()) + .bind(key.as_bytes()) + .fetch_optional(&self.pool) + .await + .map_err(|e| db_error("get", e))?; + Ok(row.map(|r| r.get::, _>(0))) + } + + async fn get_many( + &self, + scope: &str, + ns: Namespace, + keys: &[&str], + ) -> Result)>> { + if keys.is_empty() { + return Ok(Vec::new()); + } + let keys: Vec> = keys.iter().map(|k| k.as_bytes().to_vec()).collect(); + let rows = sqlx::query( + "SELECT key, value FROM signal_records \ + WHERE scope = $1 AND ns = $2 AND key = ANY($3)", + ) + .bind(scope) + .bind(ns.tag()) + .bind(&keys) + .fetch_all(&self.pool) + .await + .map_err(|e| db_error("get_many", e))?; + rows.into_iter() + .map(|r| Ok((key_string(r.get(0))?, r.get(1)))) + .collect() + } + + async fn scan_keys( + &self, + scope: &str, + ns: Namespace, + prefix: &str, + limit: Option, + ) -> Result> { + let start = prefix.as_bytes().to_vec(); + let end = prefix_end(prefix.as_bytes()); + let limit = limit.map(|l| i64::try_from(l).unwrap_or(i64::MAX)); + let rows = sqlx::query( + "SELECT key FROM signal_records \ + WHERE scope = $1 AND ns = $2 AND key >= $3 AND ($4::bytea IS NULL OR key < $4) \ + ORDER BY key LIMIT $5", + ) + .bind(scope) + .bind(ns.tag()) + .bind(start) + .bind(end) + .bind(limit) + .fetch_all(&self.pool) + .await + .map_err(|e| db_error("scan_keys", e))?; + rows.into_iter().map(|r| key_string(r.get(0))).collect() + } + + async fn write(&self, fence: &Fence, ops: &[WriteOp]) -> Result<()> { + let mut tx = self.begin("write").await?; + let current: Option = sqlx::query_scalar( + "SELECT generation FROM signal_leases WHERE scope = $1 AND holder <> '' FOR SHARE", + ) + .bind(&*fence.scope) + .fetch_optional(&mut *tx) + .await + .map_err(|e| db_error("write fence check", e))?; + if current.map(to_u64) != Some(fence.generation) { + return Err(fence_lost(fence)); + } + for op in ops { + let applied = match op { + WriteOp::Put { ns, key, value } => sqlx::query( + "INSERT INTO signal_records (scope, ns, key, value) VALUES ($1, $2, $3, $4) \ + ON CONFLICT (scope, ns, key) DO UPDATE SET value = EXCLUDED.value", + ) + .bind(&*fence.scope) + .bind(ns.tag()) + .bind(key.as_bytes()) + .bind(value.as_slice()) + .execute(&mut *tx) + .await + .map(|_| true), + WriteOp::Insert { ns, key, value } => sqlx::query( + "INSERT INTO signal_records (scope, ns, key, value) VALUES ($1, $2, $3, $4) \ + ON CONFLICT (scope, ns, key) DO NOTHING", + ) + .bind(&*fence.scope) + .bind(ns.tag()) + .bind(key.as_bytes()) + .bind(value.as_slice()) + .execute(&mut *tx) + .await + .map(|done| done.rows_affected() == 1), + WriteOp::Update { ns, key, value } => sqlx::query( + "UPDATE signal_records SET value = $4 \ + WHERE scope = $1 AND ns = $2 AND key = $3", + ) + .bind(&*fence.scope) + .bind(ns.tag()) + .bind(key.as_bytes()) + .bind(value.as_slice()) + .execute(&mut *tx) + .await + .map(|_| true), + WriteOp::Delete { ns, key } => sqlx::query( + "DELETE FROM signal_records WHERE scope = $1 AND ns = $2 AND key = $3", + ) + .bind(&*fence.scope) + .bind(ns.tag()) + .bind(key.as_bytes()) + .execute(&mut *tx) + .await + .map(|_| true), + } + .map_err(|e| db_error("write", e))?; + if !applied && let WriteOp::Insert { ns, key, .. } = op { + // Dropping the transaction rolls back the earlier ops. + return Err(RecordExists { + ns: *ns, + key: key.clone(), + } + .into_store_error()); + } + } + tx.commit().await.map_err(|e| db_error("write commit", e)) + } +} + +#[async_trait] +impl LeaseStore for PostgresStore { + async fn acquire( + &self, + scope: &str, + holder: &str, + now_ms: u64, + ttl_ms: u64, + ) -> Result> { + if holder.is_empty() { + return Err(empty_holder()); + } + let expires_at_ms = now_ms.saturating_add(ttl_ms); + let mut tx = self.begin("acquire").await?; + // Make sure a row exists so it can be locked; a fresh row starts at + // generation 1 for this holder. + let inserted = sqlx::query( + "INSERT INTO signal_leases (scope, holder, generation, expires_at_ms) \ + VALUES ($1, $2, 1, $3) ON CONFLICT (scope) DO NOTHING", + ) + .bind(scope) + .bind(holder) + .bind(to_i64(expires_at_ms)) + .execute(&mut *tx) + .await + .map_err(|e| db_error("acquire insert", e))? + .rows_affected() + == 1; + let generation = if inserted { + 1 + } else { + let row = sqlx::query( + "SELECT holder, generation, expires_at_ms FROM signal_leases \ + WHERE scope = $1 FOR UPDATE", + ) + .bind(scope) + .fetch_one(&mut *tx) + .await + .map_err(|e| db_error("acquire select", e))?; + let current: String = row.get(0); + let decision = decide( + Some((¤t, to_u64(row.get(1)), to_u64(row.get(2)))), + holder, + now_ms, + ); + let LeaseDecision::Take { generation } = decision else { + return Ok(None); + }; + sqlx::query( + "UPDATE signal_leases SET holder = $2, generation = $3, expires_at_ms = $4 \ + WHERE scope = $1", + ) + .bind(scope) + .bind(holder) + .bind(to_i64(generation)) + .bind(to_i64(expires_at_ms)) + .execute(&mut *tx) + .await + .map_err(|e| db_error("acquire update", e))?; + generation + }; + tx.commit() + .await + .map_err(|e| db_error("acquire commit", e))?; + Ok(Some(Lease { + fence: Fence { + scope: Arc::from(scope), + generation, + }, + holder: Arc::from(holder), + expires_at_ms, + })) + } + + async fn renew(&self, lease: &Lease, now_ms: u64, ttl_ms: u64) -> Result> { + let expires_at_ms = now_ms.saturating_add(ttl_ms); + let done = sqlx::query( + "UPDATE signal_leases SET expires_at_ms = $4 \ + WHERE scope = $1 AND generation = $2 AND holder = $3", + ) + .bind(&*lease.fence.scope) + .bind(to_i64(lease.fence.generation)) + .bind(&*lease.holder) + .bind(to_i64(expires_at_ms)) + .execute(&self.pool) + .await + .map_err(|e| db_error("renew", e))?; + Ok((done.rows_affected() == 1).then(|| Lease { + expires_at_ms, + ..lease.clone() + })) + } + + async fn release(&self, lease: &Lease) -> Result<()> { + sqlx::query( + "UPDATE signal_leases SET holder = '', expires_at_ms = 0 \ + WHERE scope = $1 AND generation = $2 AND holder = $3", + ) + .bind(&*lease.fence.scope) + .bind(to_i64(lease.fence.generation)) + .bind(&*lease.holder) + .execute(&self.pool) + .await + .map_err(|e| db_error("release", e))?; + Ok(()) + } +} + +/// Keys are written from `&str`, so they are always UTF-8. +pub(crate) fn key_string(bytes: Vec) -> Result { + String::from_utf8(bytes) + .map_err(|_| StoreError::Validation("stored record key is not UTF-8".into())) +} diff --git a/src/store/sqlite.rs b/src/store/sqlite.rs new file mode 100644 index 0000000..f424afb --- /dev/null +++ b/src/store/sqlite.rs @@ -0,0 +1,456 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +//! SQLite driver, for a single host. Every write and lease change runs in a +//! `BEGIN IMMEDIATE` transaction, which takes SQLite's write lock up front, +//! so the fence check and the batch it guards cannot interleave with a lease +//! takeover. Several processes on one host may share the file; nodes on +//! different hosts need PostgreSQL. + +use super::postgres::key_string; +use super::{LeaseDecision, db_error, decide, empty_holder, prefix_end, to_i64, to_u64}; +use async_trait::async_trait; +use sqlx::pool::PoolConnection; +use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePool, SqlitePoolOptions}; +use sqlx::{Row, Sqlite, SqliteConnection}; +use std::ops::{Deref, DerefMut}; +use std::str::FromStr; +use std::sync::Arc; +use std::time::Duration; +use wacore::store::error::Result; +use wacore_recordstore::{ + Fence, FenceLost, Lease, LeaseStore, Namespace, RecordExists, RecordStore, WriteOp, +}; + +static MIGRATOR: sqlx::migrate::Migrator = sqlx::migrate!("./migrations/sqlite"); + +#[derive(Clone)] +pub struct SqliteStore { + pool: SqlitePool, +} + +/// Own the connection before BEGIN is queued. Cancellation at any await, +/// including BEGIN/COMMIT/ROLLBACK, closes rather than pools an uncertain +/// connection. SQLite rolls back an open transaction when it closes. +struct Conn(Option>); + +impl Conn { + fn recycle(&mut self) { + // Only a confirmed COMMIT or ROLLBACK may return it to the pool. + drop(self.0.take()); + } +} + +impl Deref for Conn { + type Target = SqliteConnection; + + fn deref(&self) -> &Self::Target { + self.0.as_ref().expect("transaction connection is owned") + } +} + +impl DerefMut for Conn { + fn deref_mut(&mut self) -> &mut Self::Target { + self.0.as_mut().expect("transaction connection is owned") + } +} + +impl Drop for Conn { + fn drop(&mut self) { + if let Some(conn) = self.0.take() { + drop(conn.detach()); + } + } +} + +impl SqliteStore { + /// `url` is `sqlite://path/to/file.db` (created if missing) or + /// `sqlite::memory:` for a throwaway database. + pub async fn connect(url: &str, max_connections: u32) -> anyhow::Result { + let in_memory = url.contains(":memory:"); + let mut options = SqliteConnectOptions::from_str(url) + .map_err(|e| anyhow::anyhow!("invalid SQLite URL: {e}"))? + .create_if_missing(true) + .busy_timeout(Duration::from_secs(10)) + .foreign_keys(true); + if !in_memory { + options = options.journal_mode(SqliteJournalMode::Wal); + } + // Each in-memory connection would be a separate database. + let max = if in_memory { 1 } else { max_connections }; + let pool = SqlitePoolOptions::new() + .max_connections(max) + .connect_with(options) + .await + .map_err(|e| anyhow::anyhow!("opening SQLite: {e}"))?; + MIGRATOR + .run(&pool) + .await + .map_err(|e| anyhow::anyhow!("running SQLite migrations: {e}"))?; + Ok(Self { pool }) + } + + async fn begin_immediate(&self, op: &'static str) -> Result { + let mut conn = Conn(Some( + self.pool.acquire().await.map_err(|e| db_error(op, e))?, + )); + sqlx::query("BEGIN IMMEDIATE") + .execute(&mut *conn) + .await + .map_err(|e| db_error(op, e))?; + Ok(conn) + } + + /// Commit on success, roll back on any error, so a connection never goes + /// back to the pool inside a transaction. + async fn finish(mut conn: Conn, result: Result) -> Result { + match result { + Ok(value) => { + if let Err(error) = sqlx::query("COMMIT").execute(&mut *conn).await { + if sqlx::query("ROLLBACK").execute(&mut *conn).await.is_ok() { + conn.recycle(); + } + return Err(db_error("commit", error)); + } + conn.recycle(); + Ok(value) + } + Err(error) => { + if sqlx::query("ROLLBACK").execute(&mut *conn).await.is_ok() { + conn.recycle(); + } + Err(error) + } + } + } + + async fn write_in(conn: &mut Conn, fence: &Fence, ops: &[WriteOp]) -> Result<()> { + let current: Option = sqlx::query_scalar( + "SELECT generation FROM signal_leases WHERE scope = ? AND holder <> ''", + ) + .bind(&*fence.scope) + .fetch_optional(&mut **conn) + .await + .map_err(|e| db_error("write fence check", e))?; + if current.map(to_u64) != Some(fence.generation) { + return Err(FenceLost { + scope: fence.scope.clone(), + generation: fence.generation, + } + .into_store_error()); + } + for op in ops { + let applied = match op { + WriteOp::Put { ns, key, value } => sqlx::query( + "INSERT INTO signal_records (scope, ns, key, value) VALUES (?, ?, ?, ?) \ + ON CONFLICT (scope, ns, key) DO UPDATE SET value = excluded.value", + ) + .bind(&*fence.scope) + .bind(ns.tag()) + .bind(key.as_bytes()) + .bind(value.as_slice()) + .execute(&mut **conn) + .await + .map(|_| true), + WriteOp::Insert { ns, key, value } => sqlx::query( + "INSERT INTO signal_records (scope, ns, key, value) VALUES (?, ?, ?, ?) \ + ON CONFLICT (scope, ns, key) DO NOTHING", + ) + .bind(&*fence.scope) + .bind(ns.tag()) + .bind(key.as_bytes()) + .bind(value.as_slice()) + .execute(&mut **conn) + .await + .map(|done| done.rows_affected() == 1), + WriteOp::Update { ns, key, value } => sqlx::query( + "UPDATE signal_records SET value = ? WHERE scope = ? AND ns = ? AND key = ?", + ) + .bind(value.as_slice()) + .bind(&*fence.scope) + .bind(ns.tag()) + .bind(key.as_bytes()) + .execute(&mut **conn) + .await + .map(|_| true), + WriteOp::Delete { ns, key } => { + sqlx::query("DELETE FROM signal_records WHERE scope = ? AND ns = ? AND key = ?") + .bind(&*fence.scope) + .bind(ns.tag()) + .bind(key.as_bytes()) + .execute(&mut **conn) + .await + .map(|_| true) + } + } + .map_err(|e| db_error("write", e))?; + if !applied && let WriteOp::Insert { ns, key, .. } = op { + return Err(RecordExists { + ns: *ns, + key: key.clone(), + } + .into_store_error()); + } + } + Ok(()) + } + + async fn acquire_in( + conn: &mut Conn, + scope: &str, + holder: &str, + now_ms: u64, + expires_at_ms: u64, + ) -> Result> { + let row = sqlx::query( + "SELECT holder, generation, expires_at_ms FROM signal_leases WHERE scope = ?", + ) + .bind(scope) + .fetch_optional(&mut **conn) + .await + .map_err(|e| db_error("acquire select", e))?; + let current = row.map(|r| { + ( + r.get::(0), + to_u64(r.get::(1)), + to_u64(r.get::(2)), + ) + }); + let decision = decide( + current.as_ref().map(|(h, g, e)| (h.as_str(), *g, *e)), + holder, + now_ms, + ); + let LeaseDecision::Take { generation } = decision else { + return Ok(None); + }; + sqlx::query( + "INSERT INTO signal_leases (scope, holder, generation, expires_at_ms) \ + VALUES (?, ?, ?, ?) \ + ON CONFLICT (scope) DO UPDATE SET holder = excluded.holder, \ + generation = excluded.generation, expires_at_ms = excluded.expires_at_ms", + ) + .bind(scope) + .bind(holder) + .bind(to_i64(generation)) + .bind(to_i64(expires_at_ms)) + .execute(&mut **conn) + .await + .map_err(|e| db_error("acquire upsert", e))?; + Ok(Some(generation)) + } +} + +#[async_trait] +impl RecordStore for SqliteStore { + async fn get(&self, scope: &str, ns: Namespace, key: &str) -> Result>> { + let row = + sqlx::query("SELECT value FROM signal_records WHERE scope = ? AND ns = ? AND key = ?") + .bind(scope) + .bind(ns.tag()) + .bind(key.as_bytes()) + .fetch_optional(&self.pool) + .await + .map_err(|e| db_error("get", e))?; + Ok(row.map(|r| r.get::, _>(0))) + } + + async fn scan_keys( + &self, + scope: &str, + ns: Namespace, + prefix: &str, + limit: Option, + ) -> Result> { + let end = prefix_end(prefix.as_bytes()); + // SQLite treats a negative LIMIT as no limit. + let limit = limit.map_or(-1, |l| i64::try_from(l).unwrap_or(i64::MAX)); + let rows = sqlx::query( + "SELECT key FROM signal_records \ + WHERE scope = ? AND ns = ? AND key >= ? AND (? IS NULL OR key < ?) \ + ORDER BY key LIMIT ?", + ) + .bind(scope) + .bind(ns.tag()) + .bind(prefix.as_bytes()) + .bind(end.as_deref()) + .bind(end.as_deref()) + .bind(limit) + .fetch_all(&self.pool) + .await + .map_err(|e| db_error("scan_keys", e))?; + rows.into_iter().map(|r| key_string(r.get(0))).collect() + } + + async fn write(&self, fence: &Fence, ops: &[WriteOp]) -> Result<()> { + let mut conn = self.begin_immediate("write").await?; + let result = Self::write_in(&mut conn, fence, ops).await; + Self::finish(conn, result).await + } +} + +#[async_trait] +impl LeaseStore for SqliteStore { + async fn acquire( + &self, + scope: &str, + holder: &str, + now_ms: u64, + ttl_ms: u64, + ) -> Result> { + if holder.is_empty() { + return Err(empty_holder()); + } + let expires_at_ms = now_ms.saturating_add(ttl_ms); + let mut conn = self.begin_immediate("acquire").await?; + let result = Self::acquire_in(&mut conn, scope, holder, now_ms, expires_at_ms).await; + let generation = Self::finish(conn, result).await?; + Ok(generation.map(|generation| Lease { + fence: Fence { + scope: Arc::from(scope), + generation, + }, + holder: Arc::from(holder), + expires_at_ms, + })) + } + + async fn renew(&self, lease: &Lease, now_ms: u64, ttl_ms: u64) -> Result> { + let expires_at_ms = now_ms.saturating_add(ttl_ms); + let done = sqlx::query( + "UPDATE signal_leases SET expires_at_ms = ? \ + WHERE scope = ? AND generation = ? AND holder = ?", + ) + .bind(to_i64(expires_at_ms)) + .bind(&*lease.fence.scope) + .bind(to_i64(lease.fence.generation)) + .bind(&*lease.holder) + .execute(&self.pool) + .await + .map_err(|e| db_error("renew", e))?; + Ok((done.rows_affected() == 1).then(|| Lease { + expires_at_ms, + ..lease.clone() + })) + } + + async fn release(&self, lease: &Lease) -> Result<()> { + sqlx::query( + "UPDATE signal_leases SET holder = '', expires_at_ms = 0 \ + WHERE scope = ? AND generation = ? AND holder = ?", + ) + .bind(&*lease.fence.scope) + .bind(to_i64(lease.fence.generation)) + .bind(&*lease.holder) + .execute(&self.pool) + .await + .map_err(|e| db_error("release", e))?; + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test] + async fn a_failed_commit_rolls_back_before_the_pool_is_reused() { + let store = SqliteStore::connect("sqlite::memory:", 1).await.unwrap(); + sqlx::query("CREATE TABLE parent (id INTEGER PRIMARY KEY)") + .execute(&store.pool) + .await + .unwrap(); + sqlx::query("CREATE TABLE child (parent_id INTEGER REFERENCES parent(id) DEFERRABLE INITIALLY DEFERRED)") + .execute(&store.pool).await.unwrap(); + let mut conn = store.begin_immediate("commit fixture").await.unwrap(); + sqlx::query("INSERT INTO child VALUES (7)") + .execute(&mut *conn) + .await + .unwrap(); + assert!(SqliteStore::finish(conn, Ok(())).await.is_err()); + let count: i64 = sqlx::query_scalar("SELECT count(*) FROM child") + .fetch_one(&store.pool) + .await + .unwrap(); + assert_eq!(count, 0); + assert!( + store + .acquire("scope", "holder", 0, 1000) + .await + .unwrap() + .is_some() + ); + } + + #[tokio::test] + async fn cancelling_a_transaction_rolls_back_and_releases_the_writer() { + let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("target/test-state"); + std::fs::create_dir_all(&root).unwrap(); + let path = root.join(format!("cancel-{}.sqlite", rand::random::())); + let url = format!("sqlite://{}", path.display()); + let store = SqliteStore::connect(&url, 1).await.unwrap(); + let other = SqliteStore::connect(&url, 1).await.unwrap(); + let lease = store + .acquire("scope", "holder", 0, 1000) + .await + .unwrap() + .unwrap(); + let (ready, started) = tokio::sync::oneshot::channel(); + let worker = { + let store = store.clone(); + let fence = lease.fence.clone(); + tokio::spawn(async move { + let mut conn = store.begin_immediate("cancel fixture").await.unwrap(); + SqliteStore::write_in( + &mut conn, + &fence, + &[WriteOp::Put { + ns: Namespace::Device, + key: "uncommitted".into(), + value: b"must roll back".to_vec(), + }], + ) + .await + .unwrap(); + ready.send(()).unwrap(); + std::future::pending::<()>().await; + drop(conn); + }) + }; + started.await.unwrap(); + worker.abort(); + assert!(worker.await.unwrap_err().is_cancelled()); + // Use the original one-connection pool first: an abandoned raw + // transaction would expose its uncommitted row here. + assert_eq!( + store + .get("scope", Namespace::Device, "uncommitted") + .await + .unwrap(), + None + ); + tokio::time::timeout( + Duration::from_secs(2), + other.write( + &lease.fence, + &[WriteOp::Put { + ns: Namespace::Device, + key: "committed".into(), + value: b"new writer".to_vec(), + }], + ), + ) + .await + .expect("cancelled transaction retained the write lock") + .unwrap(); + assert_eq!( + store + .get("scope", Namespace::Device, "committed") + .await + .unwrap(), + Some(b"new writer".to_vec()) + ); + store.pool.close().await; + other.pool.close().await; + std::fs::remove_file(path).unwrap(); + } +} diff --git a/tests/prekey_upload_boundary.rs b/tests/prekey_upload_boundary.rs new file mode 100644 index 0000000..2ec6ee8 --- /dev/null +++ b/tests/prekey_upload_boundary.rs @@ -0,0 +1,839 @@ +//! Durable upload recovery through the actual serialized WebSocket boundary. +#[macro_use] +mod support; + +use futures_util::StreamExt; +use prost::Message as _; +use signal_node::{ + proto::{self, frame::Body, runner_request::Op, service_response::Result as Out}, + store::{Backend, PostgresStore, SqliteStore}, +}; +use std::{path::PathBuf, time::Duration}; +use support::{APP_SECRET, Hub, config, pair, peer, vouch, wait}; +use wacore::types::jid::JidExt as _; +use wacore_recordstore::{Namespace, RecordStore}; +use waproto::whatsapp as wa; + +enum Database { + Sqlite { + url: String, + path: PathBuf, + pool: sqlx::SqlitePool, + }, + Postgres { + pool: sqlx::PgPool, + }, +} +impl Database { + async fn sqlite() -> Self { + let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("target/test-state"); + std::fs::create_dir_all(&root).unwrap(); + let path = root.join(format!("upload-{}.sqlite", rand::random::())); + let url = format!("sqlite://{}", path.display()); + let _ = SqliteStore::connect(&url, 2).await.unwrap(); + let pool = sqlx::SqlitePool::connect(&url).await.unwrap(); + Self::Sqlite { url, path, pool } + } + async fn postgres() -> Option { + let Ok(url) = std::env::var("SIGNAL_NODE_TEST_POSTGRES_URL") else { + assert!( + std::env::var("CI").is_err(), + "CI requires PostgreSQL proofs" + ); + eprintln!("PostgreSQL upload proof not run: fixture URL unset"); + return None; + }; + Some(Self::Postgres { + pool: sqlx::PgPool::connect(&url).await.unwrap(), + }) + } + async fn reopen(&self) -> Backend { + match self { + Self::Sqlite { url, .. } => { + Backend::Sqlite(SqliteStore::connect(url, 4).await.unwrap()) + } + Self::Postgres { pool } => { + Backend::Postgres(PostgresStore::from_pool(pool.clone()).await.unwrap()) + } + } + } + // Inject a real SQL error at pending publication or ACK receipt insertion. + // Conditions isolate this trigger to the test's random connection scope. + async fn fail_on(&self, scope: &str, key: &str) { + let token = scope.strip_prefix("upload_").unwrap(); + let key = hex::encode(key); + match self { + Self::Sqlite { pool, .. } => { + let sql = format!( + "CREATE TRIGGER upload_failure BEFORE INSERT ON signal_records WHEN NEW.scope = '{scope}' AND NEW.ns = 'device' AND NEW.key = X'{key}' BEGIN SELECT RAISE(ABORT, 'synthetic atomic failure'); END" + ); + sqlx::query(&sql).execute(pool).await.unwrap(); + } + Self::Postgres { pool } => { + let sql = format!( + "CREATE FUNCTION upload_failure_{token}() RETURNS TRIGGER LANGUAGE plpgsql AS $$ BEGIN IF NEW.scope = '{scope}' AND NEW.ns = 'device' AND NEW.key = decode('{key}', 'hex') THEN RAISE EXCEPTION 'synthetic atomic failure'; END IF; RETURN NEW; END $$" + ); + sqlx::query(&sql).execute(pool).await.unwrap(); + sqlx::query(&format!("CREATE TRIGGER upload_failure_{token} BEFORE INSERT ON signal_records FOR EACH ROW EXECUTE FUNCTION upload_failure_{token}()")) + .execute(pool).await.unwrap(); + } + } + } + async fn clear_failure(&self, scope: &str) { + let token = scope.strip_prefix("upload_").unwrap(); + match self { + Self::Sqlite { pool, .. } => { + sqlx::query("DROP TRIGGER upload_failure") + .execute(pool) + .await + .unwrap(); + } + Self::Postgres { pool } => { + sqlx::query(&format!( + "DROP TRIGGER upload_failure_{token} ON signal_records" + )) + .execute(pool) + .await + .unwrap(); + sqlx::query(&format!("DROP FUNCTION upload_failure_{token}()")) + .execute(pool) + .await + .unwrap(); + } + } + } + async fn cleanup(self, scope: &str) { + match self { + Self::Sqlite { pool, path, .. } => { + pool.close().await; + let _ = std::fs::remove_file(&path); + let _ = std::fs::remove_file(path.with_extension("sqlite-wal")); + let _ = std::fs::remove_file(path.with_extension("sqlite-shm")); + } + Self::Postgres { pool } => { + sqlx::query("DELETE FROM signal_records WHERE scope = $1") + .bind(scope) + .execute(&pool) + .await + .unwrap(); + sqlx::query("DELETE FROM signal_leases WHERE scope = $1") + .bind(scope) + .execute(&pool) + .await + .unwrap(); + pool.close().await; + } + } + } +} + +fn request(scope: &str, generation: u64, op: Op) -> proto::RunnerRequest { + proto::RunnerRequest { + connection_id: scope.into(), + lease_generation: generation, + op: Some(op), + } +} +async fn start( + hub: &Hub, + name: &str, + scope: &str, + backend: Backend, + ttl: u64, +) -> signal_node::Running { + let mut cfg = config(name, &hub.url, false); + cfg.connections = vec![scope.into()]; + cfg.lease_ttl_ms = ttl; + // Keep fixture reacquisition within Hub's five-second test deadline; + // bulk SQL work still gets the configured, unchanged lease TTL. + cfg.lease_renew = Duration::from_millis((ttl / 4).min(100)); + let running = signal_node::start(cfg, backend).await.unwrap(); + wait(|| { + running + .node + .connection(scope) + .is_some_and(|s| s.is_claimed()) + }) + .await; + running +} +fn result(frame: proto::Frame) -> Out { + let Some(Body::ServiceResponse(proto::ServiceResponse { + result: Some(result), + })) = frame.body + else { + panic!("expected service response"); + }; + result +} +fn error(frame: proto::Frame, expected: proto::Code) { + let Some(Body::Error(error)) = frame.body else { + panic!("expected safe contract error"); + }; + assert_eq!(error.code_enum(), expected); + assert!(!error.message.contains("synthetic atomic failure")); +} +fn ok(frame: proto::Frame) { + assert!(matches!(result(frame), Out::Ok(_))); +} +async fn prepare( + hub: &Hub, + name: &str, + scope: &str, + generation: u64, + count: u32, +) -> proto::PreKeyUploadBatch { + let Out::PreKeyUploadBatch(batch) = result( + hub.request( + name, + request( + scope, + generation, + Op::PreparePreKeyUpload(proto::PreparePreKeyUpload { count }), + ), + ) + .await, + ) else { + panic!("expected public batch"); + }; + assert_eq!(batch.batch_id.len(), 32); + assert!( + batch + .batch_id + .bytes() + .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) + ); + assert!((1..=812).contains(&batch.pre_keys.len())); + let ids: std::collections::BTreeSet<_> = batch.pre_keys.iter().map(|key| key.id).collect(); + assert_eq!(ids.len(), batch.pre_keys.len()); + for key in &batch.pre_keys { + assert!((1..=0x00ff_ffff).contains(&key.id)); + assert_eq!(key.public_key.len(), 32); + } + batch +} +async fn count(hub: &Hub, name: &str, scope: &str, generation: u64) -> u32 { + let Out::PreKeyCount(count) = result( + hub.request( + name, + request( + scope, + generation, + Op::UploadedPreKeyCount(proto::UploadedPreKeyCount {}), + ), + ) + .await, + ) else { + panic!("expected local count"); + }; + count.count +} +fn ack(batch: &proto::PreKeyUploadBatch, ids: Vec) -> Op { + Op::AcknowledgePreKeyUpload(proto::AcknowledgePreKeyUpload { + batch_id: batch.batch_id.clone(), + ids, + }) +} + +// Models a future unrecognized operation; the service must refuse it rather +// than silently allocate fresh keys. The raw request traverses actual framing. +#[derive(prost::Message)] +struct FutureRequest { + #[prost(string, tag = "1")] + connection_id: String, + #[prost(uint64, tag = "2")] + lease_generation: u64, + #[prost(bytes = "vec", tag = "99")] + future_operation: Vec, +} +// An old receiver knows only legacy fresh generation. Newly added request +// cases therefore decode to None under the existing additive protocol v1. +#[derive(prost::Message)] +struct OldRequest { + #[prost(string, tag = "1")] + connection_id: String, + #[prost(uint64, tag = "2")] + lease_generation: u64, + #[prost(message, optional, tag = "11")] + generate: Option, +} + +async fn consume_pending( + hub: &Hub, + name: &str, + scope: &str, + running: &signal_node::Running, + batch: &proto::PreKeyUploadBatch, + identity: proto::Identity, +) { + let generation = running.node.connection(scope).unwrap().generation; + let (sender, sender_bundle) = peer("upload-sender-fixture").await; + let target: wacore_binary::Jid = "222:1@s.whatsapp.net".parse().unwrap(); + // The node is a paired companion; each side pins the other's account. + let node_ops = running.node.connection(scope).unwrap().ops().unwrap(); + let node = pair(&*node_ops).await; + vouch(&sender, "222:1@s.whatsapp.net", &node.device_identity).await; + vouch( + &*node_ops, + "111:1@s.whatsapp.net", + &sender_bundle.device_identity, + ) + .await; + sender + .establish_session( + &target.to_protocol_address(), + &signal_node::resolver::remote_bundle(&proto::Bundle { + registration_id: identity.registration_id, + device_id: 1, + identity_key: identity.identity_key, + signed_pre_key_id: identity.signed_pre_key_id, + signed_pre_key: identity.signed_pre_key, + signed_pre_key_signature: identity.signed_pre_key_signature, + pre_key: Some(batch.pre_keys[0].clone()), + device_identity: node.device_identity.clone(), + }) + .unwrap(), + ) + .await + .unwrap(); + let body = proto!(wa::Message { + conversation: Some("synthetic pending-prekey consumption".into()) + }); + let bytes = wacore::messages::MessageUtils::pad_message_v2(signal_node::message::encode(&body)); + let encrypted = sender + .encrypt(&target.to_protocol_address(), &bytes) + .await + .unwrap(); + let client = reqwest::Client::new(); + let base = format!("http://{}", running.http_addr); + let mut events = client + .get(format!("{base}/v1/events")) + .bearer_auth(APP_SECRET) + .send() + .await + .unwrap() + .bytes_stream(); + let receive = proto::Receive { + chat: "111@s.whatsapp.net".into(), + sender: "111:1@s.whatsapp.net".into(), + message_id: "upload-consumption".into(), + kind: encrypted.kind.as_str().into(), + ciphertext: encrypted.ciphertext, + padding_version: 2, + is_from_me: false, + timestamp: 1720000000, + sender_alt: String::new(), + device_identity: sender_bundle.device_identity, + }; + let delivery = hub.request(name, request(scope, generation, Op::Receive(receive))); + let app = async { + let event = tokio::time::timeout(Duration::from_secs(3), async { + let mut bytes = Vec::new(); + loop { + bytes.extend_from_slice(&events.next().await.unwrap().unwrap()); + if let Some(data) = String::from_utf8_lossy(&bytes) + .lines() + .find_map(|line| line.strip_prefix("data: ")) + { + break serde_json::from_str::(data).unwrap(); + } + } + }) + .await + .unwrap(); + assert_eq!(event["text"], "synthetic pending-prekey consumption"); + // The app's fixture receipt is durable before its explicit HTTP ACK. + let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("target/test-state") + .join(scope); + std::fs::create_dir_all(&dir).unwrap(); + use std::io::Write as _; + let mut file = std::fs::File::create(dir.join("event.json")).unwrap(); + file.write_all(&serde_json::to_vec(&event).unwrap()) + .unwrap(); + file.sync_all().unwrap(); + std::fs::File::open(&dir).unwrap().sync_all().unwrap(); + drop(file); + assert_eq!( + client + .post(format!( + "{base}/v1/events/{}/ack", + event["id"].as_str().unwrap() + )) + .bearer_auth(APP_SECRET) + .send() + .await + .unwrap() + .status(), + 204 + ); + std::fs::remove_dir_all(&dir).unwrap(); + }; + let (received, ()) = tokio::join!(delivery, app); + assert!(matches!( + result(received), + Out::Receive(proto::ReceiveResult { + already_delivered: false, + .. + }) + )); +} + +async fn exercise_database(db: Database) { + let scope = format!("upload_{:016x}", rand::random::()); + let hub = Hub::start().await; + let backend = db.reopen().await; + let first = start(&hub, "upload-first", &scope, backend.clone(), 30_000).await; + let generation = first.node.connection(&scope).unwrap().generation; + let init = hub + .request( + "upload-first", + request(&scope, generation, Op::GetIdentity(proto::GetIdentity {})), + ) + .await; + let Out::Identity(identity) = result(init) else { + panic!("missing identity"); + }; + let before = backend + .get(&scope, Namespace::Device, "local") + .await + .unwrap(); + for value in [0, 813, u32::MAX] { + error( + hub.request( + "upload-first", + request( + &scope, + generation, + Op::PreparePreKeyUpload(proto::PreparePreKeyUpload { count: value }), + ), + ) + .await, + proto::Code::InvalidArgument, + ); + } + db.fail_on(&scope, "prekey-upload-v1").await; + error( + hub.request( + "upload-first", + request( + &scope, + generation, + Op::PreparePreKeyUpload(proto::PreparePreKeyUpload { count: 812 }), + ), + ) + .await, + proto::Code::Unavailable, + ); + assert!( + backend + .scan_keys(&scope, Namespace::PreKey, "", None) + .await + .unwrap() + .is_empty() + ); + assert_eq!( + backend + .get(&scope, Namespace::Device, "local") + .await + .unwrap(), + before + ); + db.clear_failure(&scope).await; + let batch = prepare(&hub, "upload-first", &scope, generation, 812).await; + assert_eq!(batch.pre_keys.len(), 812); + for _ in 0..3 { + assert_eq!( + prepare(&hub, "upload-first", &scope, generation, 1).await, + batch + ); + } + assert_eq!(count(&hub, "upload-first", &scope, generation).await, 0); + first.shutdown().await; + drop(backend); + + let backend = db.reopen().await; + let second = start(&hub, "upload-second", &scope, backend.clone(), 30_000).await; + let generation2 = second.node.connection(&scope).unwrap().generation; + assert!(generation2 > generation); + assert_eq!( + prepare(&hub, "upload-second", &scope, generation2, 50).await, + batch + ); + error( + hub.request( + "upload-second", + request( + &scope, + generation, + Op::PreparePreKeyUpload(proto::PreparePreKeyUpload { count: 50 }), + ), + ) + .await, + proto::Code::NotLeaseHolder, + ); + error( + hub.request( + "upload-second", + request( + "other-session", + generation2, + Op::PreparePreKeyUpload(proto::PreparePreKeyUpload { count: 1 }), + ), + ) + .await, + proto::Code::NotLeaseHolder, + ); + let pending_before = backend + .get(&scope, Namespace::Device, "prekey-upload-v1") + .await + .unwrap(); + let keys_before = backend + .scan_keys(&scope, Namespace::PreKey, "", None) + .await + .unwrap(); + error( + hub.raw_request( + "upload-second", + FutureRequest { + connection_id: scope.clone(), + lease_generation: generation2, + future_operation: vec![1], + } + .encode_to_vec(), + ) + .await, + proto::Code::Unsupported, + ); + assert_eq!( + backend + .scan_keys(&scope, Namespace::PreKey, "", None) + .await + .unwrap(), + keys_before + ); + assert_eq!( + backend + .get(&scope, Namespace::Device, "prekey-upload-v1") + .await + .unwrap(), + pending_before + ); + let added = request( + &scope, + generation2, + Op::PreparePreKeyUpload(proto::PreparePreKeyUpload { count: 50 }), + ); + assert!( + OldRequest::decode(added.encode_to_vec().as_slice()) + .unwrap() + .generate + .is_none() + ); + let Out::PreKeys(fresh) = result( + hub.request( + "upload-second", + request( + &scope, + generation2, + Op::GeneratePreKeys(proto::GeneratePreKeys { count: 1 }), + ), + ) + .await, + ) else { + panic!("missing fresh retry keys"); + }; + let fresh = fresh.pre_keys[0].id; + error( + hub.request( + "upload-second", + request( + &scope, + generation2, + Op::MarkPreKeysUploaded(proto::MarkPreKeysUploaded { + ids: vec![fresh, batch.pre_keys[0].id], + }), + ), + ) + .await, + proto::Code::InvalidArgument, + ); + assert_eq!(count(&hub, "upload-second", &scope, generation2).await, 0); + ok(hub + .request( + "upload-second", + request( + &scope, + generation2, + Op::MarkPreKeysUploaded(proto::MarkPreKeysUploaded { ids: vec![fresh] }), + ), + ) + .await); + consume_pending(&hub, "upload-second", &scope, &second, &batch, identity).await; + assert!( + backend + .get( + &scope, + Namespace::PreKey, + &format!("{:010}", batch.pre_keys[0].id) + ) + .await + .unwrap() + .is_none() + ); + let filtered = prepare(&hub, "upload-second", &scope, generation2, 50).await; + assert_eq!(filtered.batch_id, batch.batch_id); + assert_eq!(filtered.pre_keys, batch.pre_keys[1..]); + error( + hub.request( + "upload-second", + request( + &scope, + generation2, + Op::MarkPreKeysUploaded(proto::MarkPreKeysUploaded { + ids: vec![batch.pre_keys[0].id], + }), + ), + ) + .await, + proto::Code::InvalidArgument, + ); + let ids: Vec<_> = batch.pre_keys.iter().map(|key| key.id).collect(); + for invalid in [ + ack(&batch, vec![]), + ack(&batch, vec![0]), + ack(&batch, vec![0x0100_0000]), + ack(&batch, vec![ids[1]]), + ack(&batch, vec![ids[1], ids[1]]), + ack(&batch, vec![fresh]), + ack( + &proto::PreKeyUploadBatch { + batch_id: "foreign".into(), + pre_keys: vec![], + }, + ids.clone(), + ), + ] { + error( + hub.request("upload-second", request(&scope, generation2, invalid)) + .await, + proto::Code::InvalidArgument, + ); + assert_eq!(count(&hub, "upload-second", &scope, generation2).await, 1); + } + db.fail_on(&scope, "prekey-upload-ack-v1").await; + error( + hub.request( + "upload-second", + request(&scope, generation2, ack(&batch, ids.clone())), + ) + .await, + proto::Code::Unavailable, + ); + assert_eq!(count(&hub, "upload-second", &scope, generation2).await, 1); + assert_eq!( + prepare(&hub, "upload-second", &scope, generation2, 50).await, + filtered + ); + db.clear_failure(&scope).await; + hub.request_losing_response( + "upload-second", + request(&scope, generation2, ack(&batch, ids.clone())), + ) + .await; + assert_eq!(count(&hub, "upload-second", &scope, generation2).await, 812); + second.shutdown().await; + drop(backend); + + let backend = db.reopen().await; + let third = start(&hub, "upload-third", &scope, backend.clone(), 30_000).await; + let generation3 = third.node.connection(&scope).unwrap().generation; + assert!(generation3 > generation2); + let next = prepare(&hub, "upload-third", &scope, generation3, 2).await; + assert_ne!(next.batch_id, batch.batch_id); + ok(hub + .request( + "upload-third", + request(&scope, generation3, ack(&batch, ids.clone())), + ) + .await); + assert_eq!( + prepare(&hub, "upload-third", &scope, generation3, 50).await, + next + ); + assert_eq!(count(&hub, "upload-third", &scope, generation3).await, 812); + error( + hub.request( + "upload-third", + request(&scope, generation2, ack(&batch, ids)), + ) + .await, + proto::Code::NotLeaseHolder, + ); + assert!( + backend + .get( + &scope, + Namespace::PreKey, + &format!("{:010}", batch.pre_keys[0].id) + ) + .await + .unwrap() + .is_none() + ); + third.shutdown().await; + drop(backend); + db.cleanup(&scope).await; +} + +#[tokio::test] +async fn sqlite_framed_uploads_recover_and_ack_atomically() { + exercise_database(Database::sqlite().await).await; +} + +#[tokio::test] +async fn postgres_framed_uploads_recover_and_ack_atomically() { + if let Some(db) = Database::postgres().await { + exercise_database(db).await; + } +} + +#[tokio::test] +async fn durable_batch_survives_process_crash_and_refuses_the_old_holder() { + let db = Database::sqlite().await; + let scope = format!("upload_{:016x}", rand::random::()); + let hub = Hub::start().await; + let backend = db.reopen().await; + let first = start(&hub, "upload-crash-first", &scope, backend.clone(), 500).await; + let generation = first.node.connection(&scope).unwrap().generation; + result( + hub.request( + "upload-crash-first", + request(&scope, generation, Op::GetIdentity(proto::GetIdentity {})), + ) + .await, + ); + let batch = prepare(&hub, "upload-crash-first", &scope, generation, 3).await; + let stale_ops = first.node.connection(&scope).unwrap().ops().unwrap(); + first.abort(); + tokio::time::sleep(Duration::from_millis(650)).await; + let second = start(&hub, "upload-crash-second", &scope, db.reopen().await, 500).await; + let generation2 = second.node.connection(&scope).unwrap().generation; + assert!(generation2 > generation); + assert_eq!( + prepare(&hub, "upload-crash-second", &scope, generation2, 1).await, + batch + ); + let ids: Vec<_> = batch.pre_keys.iter().map(|key| key.id).collect(); + assert!( + stale_ops + .acknowledge_prekey_upload(&batch.batch_id, &ids) + .await + .unwrap_err() + .is_fence_lost() + ); + ok(hub + .request( + "upload-crash-second", + request(&scope, generation2, ack(&batch, ids)), + ) + .await); + assert_eq!( + count(&hub, "upload-crash-second", &scope, generation2).await, + 3 + ); + second.shutdown().await; + drop(stale_ops); + drop(first); + drop(backend); + db.cleanup(&scope).await; +} + +#[test] +fn added_wire_tags_preserve_legacy_decoding_without_a_fresh_fallback() { + let prepare = request( + "", + 0, + Op::PreparePreKeyUpload(proto::PreparePreKeyUpload { count: 1 }), + ); + assert_eq!(prepare.encode_to_vec(), [0xa2, 0x01, 2, 8, 1]); + let acknowledge = request( + "", + 0, + Op::AcknowledgePreKeyUpload(proto::AcknowledgePreKeyUpload { + batch_id: "b".into(), + ids: vec![1, 2], + }), + ); + assert_eq!( + acknowledge.encode_to_vec(), + [0xaa, 0x01, 7, 10, 1, b'b', 18, 2, 1, 2] + ); + for encoded in [prepare.encode_to_vec(), acknowledge.encode_to_vec()] { + assert!( + OldRequest::decode(encoded.as_slice()) + .unwrap() + .generate + .is_none() + ); + } + let response = proto::ServiceResponse { + result: Some(Out::PreKeyUploadBatch(proto::PreKeyUploadBatch { + batch_id: "b".into(), + pre_keys: vec![], + })), + }; + assert_eq!(response.encode_to_vec(), [0x92, 0x01, 3, 10, 1, b'b']); + assert_eq!(signal_node::proto::PROTOCOL_VERSION, 1); +} + +#[tokio::test] +async fn malformed_and_oversized_frames_do_not_allocate_or_lose_the_pending_batch() { + let db = Database::sqlite().await; + let scope = format!("upload_{:016x}", rand::random::()); + let hub = Hub::start().await; + let backend = db.reopen().await; + let running = start(&hub, "upload-framing", &scope, backend.clone(), 30_000).await; + let generation = running.node.connection(&scope).unwrap().generation; + result( + hub.request( + "upload-framing", + request(&scope, generation, Op::GetIdentity(proto::GetIdentity {})), + ) + .await, + ); + let batch = prepare(&hub, "upload-framing", &scope, generation, 2).await; + hub.send_unanswered_frame("upload-framing", vec![0x80]) + .await; + assert_eq!( + prepare(&hub, "upload-framing", &scope, generation, 50).await, + batch + ); + // Existing tungstenite 0.27 transport ceiling:16MiB per frame. The + // oversized frame closes the link; reconnection takes a higher fence. + hub.send_unanswered_frame("upload-framing", vec![0; 17 << 20]) + .await; + wait(|| { + running + .node + .connection(&scope) + .is_some_and(|s| s.generation > generation && s.is_claimed()) + }) + .await; + let successor = running.node.connection(&scope).unwrap().generation; + assert_eq!( + prepare(&hub, "upload-framing", &scope, successor, 50).await, + batch + ); + assert_eq!( + backend + .scan_keys(&scope, Namespace::PreKey, "", None) + .await + .unwrap() + .len(), + 2 + ); + running.shutdown().await; + drop(backend); + db.cleanup(&scope).await; +} diff --git a/tests/protocol_contract.rs b/tests/protocol_contract.rs new file mode 100644 index 0000000..a813107 --- /dev/null +++ b/tests/protocol_contract.rs @@ -0,0 +1,70 @@ +use prost::Message; +use signal_node::proto::{self, frame::Body, service_request::Op}; + +#[test] +fn connection_lease_names_preserve_version_one_wire_tags() { + assert_eq!(proto::PROTOCOL_VERSION, 1); + assert_eq!(proto::SUBPROTOCOL, "polymorfa.signal.v1"); + assert_eq!(proto::Code::ConnectionUnavailable as i32, 8); + let request = proto::ServiceRequest { + connection_id: "stable".into(), + lease_generation: 9, + op: Some(Op::ClaimConnection(proto::ClaimConnection { + generation: 7, + })), + }; + // Root selector tag1, generation tag2 and claim tag10 are unchanged. + let wire = b"\x0a\x06stable\x10\x09\x52\x02\x08\x07"; + assert_eq!(request.encode_to_vec(), wire); + assert_eq!( + proto::ServiceRequest::decode(wire.as_slice()).unwrap(), + request + ); + + let event = proto::Frame::with( + 0, + Body::ConnectionEvent(proto::ConnectionEvent { + connection_id: "stable".into(), + state: proto::connection_event::State::Connected as i32, + ..Default::default() + }), + ); + // The unsolicited account event retains frame tag9 and state value2. + let wire = b"\x4a\x0a\x0a\x06stable\x10\x02"; + assert_eq!(event.encode_to_vec(), wire); + assert_eq!(proto::Frame::decode(wire.as_slice()).unwrap(), event); +} + +#[test] +fn companion_identity_fields_preserve_additive_tags_and_opaque_bytes() { + let receive = proto::Receive { + device_identity: vec![1, 2, 3], + ..Default::default() + }; + let bundle = proto::Bundle { + device_identity: vec![1, 2, 3], + ..Default::default() + }; + assert_eq!(receive.encode_to_vec(), [0x52, 3, 1, 2, 3]); + assert_eq!(bundle.encode_to_vec(), [0x42, 3, 1, 2, 3]); + assert_eq!( + proto::Receive::decode(&[0x52, 3, 1, 2, 3][..]).unwrap(), + receive + ); + assert_eq!( + proto::Bundle::decode(&[0x42, 3, 1, 2, 3][..]).unwrap(), + bundle + ); + assert!( + proto::Receive::decode(&[][..]) + .unwrap() + .device_identity + .is_empty() + ); + assert!( + proto::Bundle::decode(&[][..]) + .unwrap() + .device_identity + .is_empty() + ); +} diff --git a/tests/service_boundary.rs b/tests/service_boundary.rs new file mode 100644 index 0000000..661cda0 --- /dev/null +++ b/tests/service_boundary.rs @@ -0,0 +1,1409 @@ +#[macro_use] +mod support; +use futures_util::StreamExt; +use serde_json::Value; +use signal_node::{ + proto::{self, frame::Body, runner_request::Op, service_response::Result as Out}, + store::Backend, +}; +use std::sync::Arc; +use support::*; +use wacore::types::jid::JidExt as _; +use wacore_recordstore::{MemoryRecordStore, Namespace, RecordStore}; +use waproto::whatsapp as wa; + +async fn identity(hub: &Hub, node: &str, generation: u64) -> proto::Identity { + let response = hub + .command(node, generation, Op::GetIdentity(proto::GetIdentity {})) + .await; + let Some(Body::ServiceResponse(proto::ServiceResponse { + result: Some(Out::Identity(identity)), + })) = response.body + else { + panic!("identity response missing") + }; + identity +} +async fn encrypted( + hub: &Hub, + node: &str, + running: &signal_node::Running, + body: wa::Message, + from_me: bool, +) -> proto::Receive { + let connection = running.node.connection(CONNECTION).unwrap(); + let identity = identity(hub, node, connection.generation).await; + let response = hub + .command( + node, + connection.generation, + Op::GeneratePreKeys(proto::GeneratePreKeys { count: 1 }), + ) + .await; + let Some(Body::ServiceResponse(proto::ServiceResponse { + result: Some(Out::PreKeys(keys)), + })) = response.body + else { + panic!("prekeys response missing") + }; + // The node is a paired companion of its own account. The sender is either + // a companion of another account the node has pinned, or, for an own + // device message, another device paired under the node's own account. + let node_ops = connection.ops().unwrap(); + let node = pair(&*node_ops).await; + // The hub's claim reply names this account's phone number; the service + // cannot apply it before the device keys exist. + node_ops + .set_own_jids("222:1@s.whatsapp.net", None) + .await + .unwrap(); + let bob: wacore_binary::Jid = "222:1@s.whatsapp.net".parse().unwrap(); + let (peer, peer_bundle) = + peer_under("alice-fixture", from_me.then_some(&node.account_keys)).await; + let sender = if from_me { + peer.set_own_jids("222:2@s.whatsapp.net", None) + .await + .unwrap(); + "222:2@s.whatsapp.net" + } else { + vouch(&peer, "222:1@s.whatsapp.net", &node.device_identity).await; + vouch( + &*node_ops, + "111:1@s.whatsapp.net", + &peer_bundle.device_identity, + ) + .await; + "111:1@s.whatsapp.net" + }; + let bundle = proto::Bundle { + registration_id: identity.registration_id, + device_id: 1, + identity_key: identity.identity_key, + signed_pre_key_id: identity.signed_pre_key_id, + signed_pre_key: identity.signed_pre_key, + signed_pre_key_signature: identity.signed_pre_key_signature, + pre_key: Some(keys.pre_keys[0].clone()), + device_identity: node.device_identity, + }; + peer.establish_session( + &bob.to_protocol_address(), + &signal_node::resolver::remote_bundle(&bundle).unwrap(), + ) + .await + .unwrap(); + let bytes = wacore::messages::MessageUtils::pad_message_v2(signal_node::message::encode(&body)); + let cipher = peer + .encrypt(&bob.to_protocol_address(), &bytes) + .await + .unwrap(); + proto::Receive { + chat: "111@s.whatsapp.net".into(), + sender: sender.into(), + message_id: "message-fixture".into(), + kind: match cipher.kind { + wacore_signal_ops::EncKind::PreKey => "pkmsg", + wacore_signal_ops::EncKind::Message => "msg", + } + .into(), + ciphertext: cipher.ciphertext, + padding_version: 2, + is_from_me: from_me, + timestamp: 1720000000, + sender_alt: String::new(), + device_identity: peer_bundle.device_identity, + } +} +async fn next_event(stream: &mut S) -> Value +where + S: futures_util::Stream> + Unpin, +{ + tokio::time::timeout(std::time::Duration::from_secs(3), async { + let mut data = Vec::new(); + loop { + data.extend_from_slice(&stream.next().await.unwrap().unwrap()); + if let Some(line) = String::from_utf8_lossy(&data) + .lines() + .find_map(|line| line.strip_prefix("data: ")) + { + return serde_json::from_str(line).unwrap(); + } + } + }) + .await + .unwrap() +} +#[tokio::test] +async fn app_auth_origin_and_public_liveness_keep_private_state_hidden() { + let hub = Hub::start().await; + let running = start( + &hub, + "node-auth", + Backend::Memory(Arc::new(MemoryRecordStore::new())), + false, + ) + .await; + let url = format!("http://{}", running.http_addr); + let client = reqwest::Client::new(); + let health = client + .get(format!("{url}/healthz")) + .send() + .await + .unwrap() + .json::() + .await + .unwrap(); + assert_eq!(health, serde_json::json!({"status":"ok"})); + assert_eq!( + client + .get(format!("{url}/v1/connections")) + .send() + .await + .unwrap() + .status(), + 401 + ); + assert_eq!( + client + .get(format!("{url}/v1/connections")) + .bearer_auth(APP_SECRET) + .header("Origin", "null") + .send() + .await + .unwrap() + .status(), + 403 + ); + let connections = client + .get(format!("{url}/v1/connections")) + .bearer_auth(APP_SECRET) + .send() + .await + .unwrap() + .json::() + .await + .unwrap(); + assert_eq!(connections["connections"][0]["claimed"], true); + assert_eq!(connections["connections"][0]["id"], CONNECTION); + assert!(connections.get("sessions").is_none()); + assert!(!connections.to_string().contains("master_key")); + assert_eq!( + client + .get(format!("{url}/v1/sessions")) + .bearer_auth(APP_SECRET) + .send() + .await + .unwrap() + .status(), + 404 + ); + // A retired filter must fail instead of silently subscribing to every Connection. + assert_eq!( + client + .get(format!("{url}/v1/events?session_id=other")) + .bearer_auth(APP_SECRET) + .send() + .await + .unwrap() + .status(), + 400 + ); + running.shutdown().await; +} + +#[tokio::test] +async fn an_unrelated_sse_filter_does_not_make_a_connection_wait_for_an_ack() { + let hub = Hub::start().await; + let running = start( + &hub, + "node-filter", + Backend::Memory(Arc::new(MemoryRecordStore::new())), + false, + ) + .await; + let generation = running.node.connection(CONNECTION).unwrap().generation; + let receive = encrypted( + &hub, + "node-filter", + &running, + proto!(wa::Message { + conversation: Some("unwatched fixture".into()) + }), + false, + ) + .await; + let response = reqwest::Client::new() + .get(format!( + "http://{}/v1/events?connection_id=other", + running.http_addr + )) + .bearer_auth(APP_SECRET) + .send() + .await + .unwrap(); + let result = hub + .command("node-filter", generation, Op::Receive(receive)) + .await; + let Some(Body::Error(error)) = result.body else { + panic!("unwatched receive was acknowledged"); + }; + assert_eq!(error.code_enum(), proto::Code::Unavailable); + assert!(error.message.starts_with("no application is listening")); + drop(response); + running.shutdown().await; +} +#[tokio::test] +async fn real_encrypted_receive_requires_app_ack_then_is_durable_and_sealed() { + let hub = Hub::start().await; + let records = Arc::new(MemoryRecordStore::new()); + let running = start( + &hub, + "node-receive", + Backend::Memory(records.clone()), + false, + ) + .await; + let generation = running.node.connection(CONNECTION).unwrap().generation; + let receive = encrypted( + &hub, + "node-receive", + &running, + proto!(wa::Message { + conversation: Some("application-private-text".into()) + }), + false, + ) + .await; + let client = reqwest::Client::new(); + let url = format!("http://{}", running.http_addr); + let mut stream = client + .get(format!("{url}/v1/events")) + .bearer_auth(APP_SECRET) + .send() + .await + .unwrap() + .bytes_stream(); + let command = hub.command("node-receive", generation, Op::Receive(receive.clone())); + let app = async { + let event = next_event(&mut stream).await; + assert_eq!(event["text"], "application-private-text"); + assert_eq!(event["type"], "message"); + assert!(!event["redelivered"].as_bool().unwrap()); + assert_eq!( + client + .post(format!( + "{url}/v1/events/{}/ack", + event["id"].as_str().unwrap() + )) + .bearer_auth(APP_SECRET) + .send() + .await + .unwrap() + .status(), + 204 + ); + event + }; + let (response, event) = tokio::join!(command, app); + assert!(matches!( + response.body, + Some(Body::ServiceResponse(proto::ServiceResponse { + result: Some(Out::Receive(proto::ReceiveResult { + already_delivered: false, + .. + })) + })) + )); + let again = hub + .command("node-receive", generation, Op::Receive(receive)) + .await; + assert!(matches!( + again.body, + Some(Body::ServiceResponse(proto::ServiceResponse { + result: Some(Out::Receive(proto::ReceiveResult { + already_delivered: true, + .. + })) + })) + )); + for ns in Namespace::ALL { + for key in records.scan_keys(CONNECTION, ns, "", None).await.unwrap() { + let value = records.get(CONNECTION, ns, &key).await.unwrap().unwrap(); + assert!( + !value + .windows(b"application-private-text".len()) + .any(|part| part == b"application-private-text") + ); + } + } + assert_eq!( + client + .post(format!( + "{url}/v1/events/{}/ack", + event["id"].as_str().unwrap() + )) + .bearer_auth(APP_SECRET) + .send() + .await + .unwrap() + .status(), + 404 + ); + drop(stream); + running.shutdown().await; +} +#[tokio::test] +async fn unacknowledged_receive_replays_across_crash_and_old_holder_is_fenced() { + let hub = Hub::start().await; + let records = Arc::new(MemoryRecordStore::new()); + let first = start( + &hub, + "node-crash-a", + Backend::Memory(records.clone()), + false, + ) + .await; + let old = first.node.connection(CONNECTION).unwrap(); + let generation = old.generation; + let receive = encrypted( + &hub, + "node-crash-a", + &first, + proto!(wa::Message { + conversation: Some("application-private-text".into()) + }), + false, + ) + .await; + let outcome = hub + .command("node-crash-a", generation, Op::Receive(receive.clone())) + .await; + assert!( + matches!(outcome.body,Some(Body::Error(proto::Error{code,..})) if code==proto::Code::Unavailable as i32) + ); + let stale = old.ops().unwrap(); + first.abort(); + tokio::time::sleep(std::time::Duration::from_millis(650)).await; + let second = start(&hub, "node-crash-b", Backend::Memory(records), false).await; + let generation2 = second.node.connection(CONNECTION).unwrap().generation; + assert!(generation2 > generation); + let old_write = stale + .generate_prekeys(&signal_node::resolver::TokioRuntime, 1) + .await + .unwrap_err(); + assert!(old_write.is_fence_lost()); + let client = reqwest::Client::new(); + let url = format!("http://{}", second.http_addr); + let mut stream = client + .get(format!("{url}/v1/events")) + .bearer_auth(APP_SECRET) + .send() + .await + .unwrap() + .bytes_stream(); + let command = hub.command("node-crash-b", generation2, Op::Receive(receive)); + let app = async { + let event = next_event(&mut stream).await; + assert_eq!(event["redelivered"], true); + assert_eq!(event["text"], "application-private-text"); + assert_eq!( + client + .post(format!( + "{url}/v1/events/{}/ack", + event["id"].as_str().unwrap() + )) + .bearer_auth(APP_SECRET) + .send() + .await + .unwrap() + .status(), + 204 + ); + }; + let (response, _) = tokio::join!(command, app); + assert!(matches!(response.body, Some(Body::ServiceResponse(_)))); + drop(stream); + second.shutdown().await; +} + +fn app_state_share() -> wa::message::AppStateSyncKeyShare { + proto!(wa::message::AppStateSyncKeyShare { + keys: vec![proto!(wa::message::AppStateSyncKey { + key_id: Some(proto!(wa::message::AppStateSyncKeyId { + key_id: Some(vec![3; 32]) + })) + .into(), + key_data: Some(proto!(wa::message::AppStateSyncKeyData { + key_data: Some(vec![4; 32]) + })) + .into() + })] + }) +} + +#[tokio::test] +async fn app_state_keys_require_separate_consent_and_an_own_device() { + for (consent, from_me, history) in [ + (false, true, false), + (false, true, true), + (true, true, false), + (true, false, false), + ] { + let hub = Hub::start().await; + let mut cfg = config("node-app-state", &hub.url, history); + assert!(!cfg.share_app_state_keys); + cfg.share_app_state_keys = consent; + let running = signal_node::start(cfg, Backend::Memory(Arc::new(MemoryRecordStore::new()))) + .await + .unwrap(); + wait(|| { + running + .node + .connection(CONNECTION) + .is_some_and(|s| s.is_claimed()) + }) + .await; + let generation = running.node.connection(CONNECTION).unwrap().generation; + let expected = app_state_share(); + let receive = encrypted( + &hub, + "node-app-state", + &running, + proto!(wa::Message { + protocol_message: proto!(wa::message::ProtocolMessage { + r#type: Some(wa::message::protocol_message::Type::AppStateSyncKeyShare), + app_state_sync_key_share: Some(expected.clone()).into() + }) + .into(), + message_context_info: Some(proto!(wa::MessageContextInfo { + message_secret: Some(vec![9; 32]) + })) + .into() + }), + from_me, + ) + .await; + let mut stream = reqwest::Client::new() + .get(format!("http://{}/v1/events", running.http_addr)) + .bearer_auth(APP_SECRET) + .send() + .await + .unwrap() + .bytes_stream(); + let response = hub + .command("node-app-state", generation, Op::Receive(receive.clone())) + .await; + let Some(Body::ServiceResponse(proto::ServiceResponse { + result: Some(Out::Receive(result)), + })) = response.body + else { + panic!("receive response missing"); + }; + let expected_bytes = if consent && from_me { + signal_node::message::encode(&expected) + } else { + Vec::new() + }; + assert_eq!( + result.app_state_key_share, expected_bytes, + "consent={consent}, own_device={from_me}, history={history}" + ); + let replay = hub + .command("node-app-state", generation, Op::Receive(receive)) + .await; + assert!( + matches!(replay.body, Some(Body::ServiceResponse(proto::ServiceResponse { result: Some(Out::Receive(proto::ReceiveResult { already_delivered: true, app_state_key_share, .. })) })) if app_state_key_share == expected_bytes) + ); + assert!( + tokio::time::timeout(std::time::Duration::from_millis(100), stream.next()) + .await + .is_err() + ); + drop(stream); + running.shutdown().await; + } +} + +#[tokio::test] +async fn app_state_share_survives_a_lost_reply_and_restart_with_live_consent_checks() { + let hub = Hub::start().await; + let records = Arc::new(MemoryRecordStore::new()); + let mut cfg = config("node-share-replay", &hub.url, false); + cfg.share_app_state_keys = true; + let running = signal_node::start(cfg.clone(), Backend::Memory(records.clone())) + .await + .unwrap(); + wait(|| { + running + .node + .connection(CONNECTION) + .is_some_and(|s| s.is_claimed()) + }) + .await; + let generation = running.node.connection(CONNECTION).unwrap().generation; + let share = app_state_share(); + let receive = encrypted( + &hub, + "node-share-replay", + &running, + proto!(wa::Message { + protocol_message: proto!(wa::message::ProtocolMessage { + r#type: Some(wa::message::protocol_message::Type::AppStateSyncKeyShare), + app_state_sync_key_share: Some(share.clone()).into() + }) + .into() + }), + true, + ) + .await; + let first = hub + .command( + "node-share-replay", + generation, + Op::Receive(receive.clone()), + ) + .await; + let Some(Body::ServiceResponse(proto::ServiceResponse { + result: Some(Out::Receive(first)), + })) = first.body + else { + panic!("first receive result missing") + }; + assert!(!first.already_delivered); + // Discard the response as if transport dropped it after the delivery write. + running.shutdown().await; + for consent in [true, false] { + cfg.share_app_state_keys = consent; + let replacement = signal_node::start(cfg.clone(), Backend::Memory(records.clone())) + .await + .unwrap(); + wait(|| { + replacement + .node + .connection(CONNECTION) + .is_some_and(|s| s.is_claimed()) + }) + .await; + let generation = replacement.node.connection(CONNECTION).unwrap().generation; + for from_me in [true, false] { + let mut retry = receive.clone(); + retry.is_from_me = from_me; + let replay = hub + .command("node-share-replay", generation, Op::Receive(retry)) + .await; + let Some(Body::ServiceResponse(proto::ServiceResponse { + result: Some(Out::Receive(result)), + })) = replay.body + else { + panic!("receive replay missing") + }; + assert!(result.already_delivered); + assert_eq!( + result.app_state_key_share, + if consent && from_me { + signal_node::message::encode(&share) + } else { + Vec::new() + } + ); + } + replacement.shutdown().await; + } +} + +#[tokio::test] +async fn history_stays_off_until_explicit_consent_and_delivers_only_the_notification() { + for (consent, from_me) in [(false, true), (true, false), (true, true)] { + let hub = Hub::start().await; + let running = start( + &hub, + "node-history", + Backend::Memory(Arc::new(MemoryRecordStore::new())), + consent, + ) + .await; + let generation = running.node.connection(CONNECTION).unwrap().generation; + let message = proto!(wa::Message { + protocol_message: proto!(wa::message::ProtocolMessage { + r#type: Some(wa::message::protocol_message::Type::HistorySyncNotification), + history_sync_notification: proto!(wa::message::HistorySyncNotification { + direct_path: Some("/fixture/history".into()), + media_key: Some(vec![1; 32]), + file_sha256: Some(vec![2; 32]), + file_enc_sha256: Some(vec![3; 32]), + file_length: Some(1234), + chunk_order: Some(1) + }) + .into() + }) + .into() + }); + let receive = encrypted(&hub, "node-history", &running, message, from_me).await; + let client = reqwest::Client::new(); + let url = format!("http://{}", running.http_addr); + let mut stream = client + .get(format!("{url}/v1/events")) + .bearer_auth(APP_SECRET) + .send() + .await + .unwrap() + .bytes_stream(); + if consent && from_me { + let command = hub.command("node-history", generation, Op::Receive(receive)); + let app = async { + let event = next_event(&mut stream).await; + assert_eq!(event["type"], "history.sync"); + assert_eq!(event["history"]["direct_path"], "/fixture/history"); + assert_eq!(event["message_proto"], ""); + assert!(event.get("text").is_none()); + assert!(event.get("media").is_none()); + assert_eq!( + client + .post(format!( + "{url}/v1/events/{}/ack", + event["id"].as_str().unwrap() + )) + .bearer_auth(APP_SECRET) + .send() + .await + .unwrap() + .status(), + 204 + ); + }; + let (result, _) = tokio::join!(command, app); + assert!(matches!(result.body, Some(Body::ServiceResponse(_)))); + } else { + let result = hub + .command("node-history", generation, Op::Receive(receive)) + .await; + assert!(matches!(result.body, Some(Body::ServiceResponse(_)))); + assert!( + tokio::time::timeout(std::time::Duration::from_millis(100), stream.next()) + .await + .is_err() + ); + } + drop(stream); + running.shutdown().await; + } +} +#[tokio::test] +async fn http_send_stanza_round_trips_into_the_other_device_and_never_exceeds_five_per_second() { + let hub = Hub::start().await; + let running = start( + &hub, + "node-send", + Backend::Memory(Arc::new(MemoryRecordStore::new())), + false, + ) + .await; + let generation = running.node.connection(CONNECTION).unwrap().generation; + identity(&hub, "node-send", generation).await; + let ops = running.node.connection(CONNECTION).unwrap().ops().unwrap(); + let node = pair(&*ops).await; + ops.set_own_jids("222:1@s.whatsapp.net", None) + .await + .unwrap(); + let (peer, bundle) = peer("send-peer").await; + vouch(&peer, "222:1@s.whatsapp.net", &node.device_identity).await; + vouch(&*ops, "111:1@s.whatsapp.net", &bundle.device_identity).await; + hub.bundle("111:1@s.whatsapp.net", bundle).await; + let client = reqwest::Client::new(); + let url = format!( + "http://{}/v1/connections/{CONNECTION}/messages", + running.http_addr + ); + for _ in 0..7 { + let response = client + .post(&url) + .bearer_auth(APP_SECRET) + .json(&serde_json::json!({"to":"111@s.whatsapp.net","text":"application-private-text"})) + .send() + .await + .unwrap(); + let status = response.status(); + let value = response.json::().await.unwrap(); + assert!( + status.is_success(), + "send error code: {:?}", + value["error"]["code"] + ); + assert!(value["message_id"].is_string()); + } + let sends = hub.sends().await; + assert_eq!(sends.len(), 7); + for (i, (when, _)) in sends.iter().enumerate() { + assert!( + sends[i..] + .iter() + .filter(|(time, _)| time.duration_since(*when) < std::time::Duration::from_secs(1)) + .count() + <= 5 + ); + } + fn enc(node: &wacore_binary::Node) -> Option<&wacore_binary::Node> { + if node.tag == "enc" { + return Some(node); + } + node.children()?.iter().find_map(enc) + } + let node = wacore_binary::marshal::unmarshal_packed_ref(&sends[0].1) + .unwrap() + .to_owned(); + assert_eq!(node.tag, "message"); + let payload = enc(&node).unwrap(); + let Some(wacore_binary::node::NodeContent::Bytes(ciphertext)) = &payload.content else { + panic!("encrypted content missing") + }; + let device_identity = match node + .get_optional_child("device-identity") + .and_then(|child| child.content.as_ref()) + { + Some(wacore_binary::node::NodeContent::Bytes(bytes)) => Some(bytes.as_slice()), + _ => None, + }; + let bob: wacore_binary::Jid = "222:1@s.whatsapp.net".parse().unwrap(); + let opened = peer + .decrypt( + &bob.to_protocol_address(), + wacore_signal_ops::EncKind::PreKey, + ciphertext, + device_identity, + ) + .await + .unwrap(); + let plaintext = + wacore::messages::MessageUtils::unpad_message_ref(&opened.plaintext, 2).unwrap(); + let message = waproto::codec::message_decode(plaintext).unwrap(); + assert_eq!( + message.conversation.as_deref(), + Some("application-private-text") + ); + running.shutdown().await; +} + +#[tokio::test] +async fn a_lost_send_reply_reports_the_original_unconfirmed_message_without_retrying() { + for disconnect in [false, true] { + let hub = Hub::start().await; + let running = start( + &hub, + "node-uncertain", + Backend::Memory(Arc::new(MemoryRecordStore::new())), + false, + ) + .await; + let generation = running.node.connection(CONNECTION).unwrap().generation; + identity(&hub, "node-uncertain", generation).await; + let ops = running.node.connection(CONNECTION).unwrap().ops().unwrap(); + pair(&*ops).await; + ops.set_own_jids("222:1@s.whatsapp.net", Some("22201:1@lid")) + .await + .unwrap(); + let (_peer, bundle) = peer("uncertain-peer").await; + vouch(&*ops, "111:1@s.whatsapp.net", &bundle.device_identity).await; + hub.bundle("111:1@s.whatsapp.net", bundle).await; + hub.lose_send_reply(disconnect); + let response = reqwest::Client::new() + .post(format!( + "http://{}/v1/connections/{CONNECTION}/messages", + running.http_addr + )) + .bearer_auth(APP_SECRET) + .json(&serde_json::json!({"to":"111@s.whatsapp.net","text":"application-private-text"})) + .send() + .await + .unwrap(); + assert_eq!(response.status(), 504, "disconnect={disconnect}"); + let body = response.json::().await.unwrap(); + assert_eq!(body["error"]["code"], "send_unconfirmed"); + let sends = hub.sends().await; + assert_eq!(sends.len(), 1, "an uncertain send must not be repeated"); + let stanza = wacore_binary::marshal::unmarshal_packed_ref(&sends[0].1) + .unwrap() + .to_owned(); + assert_eq!( + body["error"]["message_id"].as_str().unwrap(), + stanza.attrs.get("id").unwrap().as_str() + ); + running.shutdown().await; + } +} + +#[tokio::test] +async fn a_failed_mismatch_resend_keeps_the_original_message_unconfirmed() { + for failure in [ + ResendFailure::DisconnectLookup, + ResendFailure::RefuseSend, + ResendFailure::LoseReply, + ] { + let hub = Hub::start().await; + let running = start( + &hub, + "node-resend", + Backend::Memory(Arc::new(MemoryRecordStore::new())), + false, + ) + .await; + let connection = running.node.connection(CONNECTION).unwrap(); + identity(&hub, "node-resend", connection.generation).await; + let ops = connection.ops().unwrap(); + pair(&*ops).await; + ops.set_own_jids("222:1@s.whatsapp.net", Some("22201:1@lid")) + .await + .unwrap(); + let (_peer, bundle) = peer("resend-peer").await; + vouch(&*ops, "111:1@s.whatsapp.net", &bundle.device_identity).await; + hub.bundle("111:1@s.whatsapp.net", bundle).await; + hub.fail_resend(failure); + let response = reqwest::Client::new() + .post(format!( + "http://{}/v1/connections/{CONNECTION}/messages", + running.http_addr + )) + .bearer_auth(APP_SECRET) + .json(&serde_json::json!({"to":"12345@g.us","text":"application-private-text"})) + .send() + .await + .unwrap(); + let status = response.status(); + let body = response.json::().await.unwrap(); + assert_eq!(status, 504, "failure={failure:?}, body={body}"); + assert_eq!(body["error"]["code"], "send_unconfirmed"); + let sends = hub.sends().await; + assert_eq!( + sends.len(), + if matches!(failure, ResendFailure::DisconnectLookup) { + 1 + } else { + 2 + } + ); + for (_, bytes) in sends { + let stanza = wacore_binary::marshal::unmarshal_packed_ref(&bytes) + .unwrap() + .to_owned(); + assert_eq!( + body["error"]["message_id"].as_str().unwrap(), + stanza.attrs.get("id").unwrap().as_str() + ); + } + running.shutdown().await; + } +} + +#[tokio::test] +async fn an_accepted_group_send_survives_distribution_storage_failure_or_fence_loss() { + use signal_node::store::SqliteStore; + for (fence_lost, mismatch) in [(false, false), (true, false), (false, true), (true, true)] { + let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("target/test-state"); + std::fs::create_dir_all(&root).unwrap(); + let path = root.join(format!("send-{}.sqlite", rand::random::())); + let url = format!("sqlite://{}", path.display()); + let backend = SqliteStore::connect(&url, 2).await.unwrap(); + let pool = sqlx::SqlitePool::connect(&url).await.unwrap(); + let hub = Hub::start().await; + let running = start(&hub, "node-mark-failure", Backend::Sqlite(backend), false).await; + let connection = running.node.connection(CONNECTION).unwrap(); + identity(&hub, "node-mark-failure", connection.generation).await; + let ops = connection.ops().unwrap(); + pair(&*ops).await; + ops.set_own_jids("222:1@s.whatsapp.net", Some("22201:1@lid")) + .await + .unwrap(); + let (_peer, bundle) = peer("mark-failure-peer").await; + vouch(&*ops, "111:1@s.whatsapp.net", &bundle.device_identity).await; + hub.bundle("111:1@s.whatsapp.net", bundle).await; + if mismatch { + hub.mismatch_next_send(); + } + if !fence_lost { + sqlx::query("CREATE TRIGGER fail_distribution BEFORE INSERT ON signal_records WHEN NEW.ns = 'sender_key_devices' AND instr(NEW.key, x'00') > 0 BEGIN SELECT RAISE(ABORT, 'synthetic distribution write failure'); END") + .execute(&pool).await.unwrap(); + } + let (observed, release) = hub.gate_next_send().await; + let send = reqwest::Client::new() + .post(format!( + "http://{}/v1/connections/{CONNECTION}/messages", + running.http_addr + )) + .bearer_auth(APP_SECRET) + .json(&serde_json::json!({"to":"12345@g.us","text":"application-private-text"})) + .send(); + let change = async { + tokio::time::timeout(std::time::Duration::from_secs(3), observed.notified()) + .await + .expect("send never reached the runner"); + if fence_lost { + sqlx::query("UPDATE signal_leases SET generation = generation + 1 WHERE scope = ?") + .bind(CONNECTION) + .execute(&pool) + .await + .unwrap(); + } + release.notify_one(); + }; + let (response, ()) = tokio::join!(send, change); + let response = response.unwrap(); + let status = response.status(); + let body = response.json::().await.unwrap(); + assert_eq!( + status, 200, + "fence_lost={fence_lost}, mismatch={mismatch}, error={:?}", + body["error"]["code"] + ); + assert!(body["message_id"].is_string()); + assert_eq!(body["server_timestamp"], 1720000000); + let sends = hub.sends().await; + let expected_resend = mismatch && !fence_lost; + assert_eq!(sends.len(), if expected_resend { 2 } else { 1 }); + assert_eq!(body["resent"], expected_resend); + for (_, bytes) in sends { + let stanza = wacore_binary::marshal::unmarshal_packed_ref(&bytes) + .unwrap() + .to_owned(); + assert_eq!( + body["message_id"].as_str().unwrap(), + stanza.attrs.get("id").unwrap().as_str() + ); + } + if fence_lost { + assert!( + !connection.can_serve().await, + "fence loss must still retire the old connection" + ); + } + drop(ops); + drop(connection); + running.shutdown().await; + pool.close().await; + std::fs::remove_file(&path).unwrap(); + } +} + +#[tokio::test] +async fn webhook_signature_failure_and_ack_gate_durable_delivery() { + use axum::{ + Router, + extract::State, + http::{HeaderMap, StatusCode}, + routing::post, + }; + use hmac::Mac; + use std::sync::atomic::{AtomicU16, Ordering}; + #[derive(Clone)] + struct Sink { + status: Arc, + observed: Arc, + release: Arc, + events: Arc>>, + } + async fn receive( + State(sink): State, + headers: HeaderMap, + body: bytes::Bytes, + ) -> StatusCode { + let timestamp = headers[signal_node::delivery::TIMESTAMP_HEADER] + .to_str() + .unwrap(); + let mut mac = + as Mac>::new_from_slice(b"webhook-fixture-secret").unwrap(); + mac.update(timestamp.as_bytes()); + mac.update(b"."); + mac.update(&body); + assert_eq!( + headers[signal_node::delivery::SIGNATURE_HEADER] + .to_str() + .unwrap(), + format!("v1={}", hex::encode(mac.finalize().into_bytes())) + ); + assert!( + headers.get("Authorization").is_none(), + "no app or project bearer goes to webhook" + ); + let event: Value = serde_json::from_slice(&body).unwrap(); + assert_eq!( + headers[signal_node::delivery::EVENT_ID_HEADER] + .to_str() + .unwrap(), + event["id"].as_str().unwrap() + ); + sink.events.lock().await.push(event); + let status = sink.status.load(Ordering::SeqCst); + sink.observed.notify_one(); + if status == 204 { + sink.release.notified().await; + } + StatusCode::from_u16(status).unwrap() + } + let sink = Sink { + status: Arc::new(AtomicU16::new(500)), + observed: Arc::new(tokio::sync::Notify::new()), + release: Arc::new(tokio::sync::Notify::new()), + events: Arc::new(tokio::sync::Mutex::new(Vec::new())), + }; + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let addr = listener.local_addr().unwrap(); + let app = Router::new() + .route("/events", post(receive)) + .with_state(sink.clone()); + let server = tokio::spawn(async move { + axum::serve(listener, app).await.unwrap(); + }); + let hub = Hub::start().await; + let mut cfg = config("node-webhook", &hub.url, false); + cfg.sse = false; + cfg.webhook_url = Some(format!("http://{addr}/events")); + cfg.webhook_secret = signal_node::config::Secret::new("webhook-fixture-secret"); + let running = signal_node::start(cfg, Backend::Memory(Arc::new(MemoryRecordStore::new()))) + .await + .unwrap(); + wait(|| { + running + .node + .connection(CONNECTION) + .is_some_and(|s| s.is_claimed()) + }) + .await; + let generation = running.node.connection(CONNECTION).unwrap().generation; + let ciphertext = encrypted( + &hub, + "node-webhook", + &running, + proto!(wa::Message { + conversation: Some("webhook-secret-text".into()) + }), + false, + ) + .await; + let first = hub + .command("node-webhook", generation, Op::Receive(ciphertext.clone())) + .await; + assert!( + matches!(first.body, Some(Body::Error(proto::Error {code, ..})) if code == proto::Code::Unavailable as i32) + ); + sink.observed.notified().await; + // A redirect is also a rejection; the client must never follow it. + sink.status.store(307, Ordering::SeqCst); + let redirected = hub + .command("node-webhook", generation, Op::Receive(ciphertext.clone())) + .await; + assert!(matches!(redirected.body, Some(Body::Error(_)))); + sink.observed.notified().await; + sink.status.store(204, Ordering::SeqCst); + let command = hub.command("node-webhook", generation, Op::Receive(ciphertext.clone())); + tokio::pin!(command); + tokio::select! { + result = &mut command => panic!("delivery completed before webhook ACK: {result:?}"), + _ = sink.observed.notified() => {} + } + assert!( + tokio::time::timeout(std::time::Duration::from_millis(20), &mut command) + .await + .is_err() + ); + sink.release.notify_one(); + assert!(matches!(command.await.body, Some(Body::ServiceResponse(_)))); + let again = hub + .command("node-webhook", generation, Op::Receive(ciphertext)) + .await; + assert!(matches!( + again.body, + Some(Body::ServiceResponse(proto::ServiceResponse { + result: Some(Out::Receive(proto::ReceiveResult { + already_delivered: true, + .. + })) + })) + )); + let events = sink.events.lock().await; + assert_eq!(events.len(), 3); + assert_eq!(events[0]["redelivered"], false); + assert_eq!(events[1]["redelivered"], true); + assert_eq!(events[2]["redelivered"], true); + assert_eq!(events[0]["id"], events[2]["id"]); + drop(events); + running.shutdown().await; + server.abort(); +} + +#[tokio::test] +async fn expired_holder_refuses_requests_and_old_callbacks_preserve_new_generation() { + use signal_node::link::Handler; + let hub = Hub::start().await; + let records = Arc::new(MemoryRecordStore::new()); + let old = start( + &hub, + "node-expired", + Backend::Memory(records.clone()), + false, + ) + .await; + let old_connection = old.node.connection(CONNECTION).unwrap(); + old.abort(); + tokio::time::sleep(std::time::Duration::from_millis(650)).await; + assert!(!old_connection.can_serve().await); + let error = old + .node + .runner_request(proto::RunnerRequest { + connection_id: CONNECTION.into(), + lease_generation: old_connection.generation, + op: Some(Op::GetIdentity(proto::GetIdentity {})), + }) + .await + .unwrap_err(); + assert_eq!(error.code_enum(), proto::Code::NotLeaseHolder); + old.node.tick_leases().await; // Releases the expired generation. + old.node.tick_leases().await; // Same holder ID must take a new generation. + let new_connection = old.node.connection(CONNECTION).unwrap(); + assert!(new_connection.generation > old_connection.generation); + old.node + .lose(CONNECTION, old_connection.generation, "late old callback"); + assert_eq!( + old.node.connection(CONNECTION).unwrap().generation, + new_connection.generation + ); + old.node.shutdown().await; +} + +#[tokio::test] +async fn process_incarnations_with_the_same_node_id_cannot_share_a_fence() { + // Both processes hold live links: a node without one takes no leases. + let hub = Hub::start().await; + let records = Arc::new(MemoryRecordStore::new()); + let first = start( + &hub, + "same-stable-node", + Backend::Memory(records.clone()), + false, + ) + .await; + let old = first.node.connection(CONNECTION).unwrap(); + let ops = old.ops_or_create().await.unwrap(); + let replacement = signal_node::start( + config("same-stable-node", &hub.url, false), + Backend::Memory(records), + ) + .await + .unwrap(); + tokio::time::sleep(std::time::Duration::from_millis(250)).await; + assert!( + replacement.node.connection(CONNECTION).is_none(), + "an active lease cannot be reacquired by another process with the same node ID" + ); + first.abort(); + wait(|| { + replacement + .node + .connection(CONNECTION) + .is_some_and(|s| s.generation > old.generation) + }) + .await; + assert!( + ops.generate_prekeys(&signal_node::resolver::TokioRuntime, 1) + .await + .unwrap_err() + .is_fence_lost() + ); + first.node.shutdown().await; + replacement.shutdown().await; +} + +#[tokio::test] +async fn group_retry_rejects_unknown_or_missing_addressing_mode() { + let hub = Hub::start().await; + let running = start( + &hub, + "node-group-validation", + Backend::Memory(Arc::new(MemoryRecordStore::new())), + false, + ) + .await; + let generation = running.node.connection(CONNECTION).unwrap().generation; + identity(&hub, "node-group-validation", generation).await; + for mode in ["", "unknown"] { + let response = hub + .command( + "node-group-validation", + generation, + Op::Resend(proto::Resend { + chat: "123@g.us".into(), + requester: "111:1@s.whatsapp.net".into(), + is_group: true, + addressing_mode: mode.into(), + ..Default::default() + }), + ) + .await; + assert!( + matches!(response.body, Some(Body::Error(error)) if error.code_enum()==proto::Code::InvalidArgument) + ); + } + running.shutdown().await; +} + +#[tokio::test] +async fn mixed_protocol_text_is_delivered_without_foreign_keys_or_unconsented_history() { + for (protocol, from_me) in [ + ( + proto!(wa::message::ProtocolMessage { + r#type: Some(wa::message::protocol_message::Type::AppStateSyncKeyShare), + app_state_sync_key_share: Some(app_state_share()).into() + }), + true, + ), + ( + proto!(wa::message::ProtocolMessage { + r#type: Some(wa::message::protocol_message::Type::AppStateSyncKeyShare), + app_state_sync_key_share: Some(proto!(wa::message::AppStateSyncKeyShare { + keys: vec![proto!(wa::message::AppStateSyncKey { + key_id: Some(proto!(wa::message::AppStateSyncKeyId { + key_id: Some(vec![3; 32]) + })) + .into(), + key_data: Some(proto!(wa::message::AppStateSyncKeyData { + key_data: Some(vec![4; 32]) + })) + .into() + })] + })) + .into() + }), + false, + ), + ( + proto!(wa::message::ProtocolMessage { + r#type: Some(wa::message::protocol_message::Type::HistorySyncNotification), + history_sync_notification: Some(proto!(wa::message::HistorySyncNotification { + direct_path: Some("/never-release-without-consent".into()), + media_key: Some(vec![5; 32]) + })) + .into() + }), + true, + ), + ( + proto!(wa::message::ProtocolMessage { + r#type: Some( + wa::message::protocol_message::Type::PeerDataOperationRequestResponseMessage, + ), + peer_data_operation_request_response_message: Some( + wa::message::PeerDataOperationRequestResponseMessage::default(), + ) + .into() + }), + false, + ), + ] { + let hub = Hub::start().await; + let running = start( + &hub, + "node-mixed", + Backend::Memory(Arc::new(MemoryRecordStore::new())), + false, + ) + .await; + let generation = running.node.connection(CONNECTION).unwrap().generation; + let ciphertext = encrypted( + &hub, + "node-mixed", + &running, + proto!(wa::Message { + conversation: Some("mixed-user-content".into()), + protocol_message: Some(protocol).into(), + message_context_info: Some(proto!(wa::MessageContextInfo { + message_secret: Some(vec![9; 32]) + })) + .into() + }), + from_me, + ) + .await; + let client = reqwest::Client::new(); + let url = format!("http://{}", running.http_addr); + let mut stream = client + .get(format!("{url}/v1/events")) + .bearer_auth(APP_SECRET) + .send() + .await + .unwrap() + .bytes_stream(); + let command = hub.command("node-mixed", generation, Op::Receive(ciphertext)); + let app = async { + let event = next_event(&mut stream).await; + assert_eq!(event["text"], "mixed-user-content"); + assert_eq!(event["type"], "message"); + assert!(event.get("history").is_none()); + use base64::Engine as _; + let message = waproto::codec::message_decode( + &base64::engine::general_purpose::STANDARD + .decode(event["message_proto"].as_str().unwrap()) + .unwrap(), + ) + .unwrap(); + assert!(!message.protocol_message.is_set()); + assert_eq!( + message + .message_context_info + .as_option() + .unwrap() + .message_secret, + Some(vec![9; 32]) + ); + assert_eq!( + client + .post(format!( + "{url}/v1/events/{}/ack", + event["id"].as_str().unwrap() + )) + .bearer_auth(APP_SECRET) + .send() + .await + .unwrap() + .status(), + 204 + ); + }; + let (response, ()) = tokio::join!(command, app); + assert!( + matches!(response.body, Some(Body::ServiceResponse(proto::ServiceResponse {result: Some(Out::Receive(proto::ReceiveResult { app_state_key_share, .. }))})) if app_state_key_share.is_empty()) + ); + drop(stream); + running.shutdown().await; + } +} + +#[tokio::test] +async fn reconnect_claims_a_strictly_higher_durable_generation() { + let hub = Hub::start().await; + let running = start( + &hub, + "node-reconnect", + Backend::Memory(Arc::new(MemoryRecordStore::new())), + false, + ) + .await; + let first = running.node.connection(CONNECTION).unwrap().generation; + hub.disconnect("node-reconnect").await; + wait(|| { + running + .node + .connection(CONNECTION) + .is_some_and(|s| s.generation > first && s.is_claimed()) + }) + .await; + assert!(running.node.connection(CONNECTION).unwrap().generation > first); + running.shutdown().await; +} + +#[tokio::test] +async fn a_node_without_a_live_link_takes_no_leases() { + // A partitioned replica must leave free connections to a connected one. + let node = signal_node::node::Node::new( + config("partitioned", "ws://127.0.0.1:1/signal", false), + Backend::Memory(Arc::new(MemoryRecordStore::new())), + ); + node.tick_leases().await; + assert!(node.connection(CONNECTION).is_none()); + node.shutdown().await; +} diff --git a/tests/store_conformance.rs b/tests/store_conformance.rs new file mode 100644 index 0000000..170d89c --- /dev/null +++ b/tests/store_conformance.rs @@ -0,0 +1,182 @@ +use signal_node::store::{PostgresStore, SqliteStore}; +use wacore::store::traits::SignalStore; +use wacore_recordstore::{LeaseStore, Namespace, RecordStore, WriteOp, is_fence_lost}; + +#[tokio::test] +async fn sqlite_runs_every_recordstore_check() { + let store = SqliteStore::connect("sqlite::memory:", 1).await.unwrap(); + wacore_recordstore::conformance::run_all(&store).await; +} +#[tokio::test] +async fn sqlite_two_pools_fence_takeover_and_exclusive_batches() { + let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("target/test-state"); + std::fs::create_dir_all(&root).unwrap(); + let path = root.join(format!("store-{}.sqlite", rand::random::())); + let url = format!("sqlite://{}", path.display()); + let first = SqliteStore::connect(&url, 2).await.unwrap(); + let second = SqliteStore::connect(&url, 2).await.unwrap(); + let a = first + .acquire("scope", "first", 1000, 100) + .await + .unwrap() + .unwrap(); + assert!( + second + .acquire("scope", "second", 1099, 100) + .await + .unwrap() + .is_none() + ); + let b = second + .acquire("scope", "second", 1100, 100) + .await + .unwrap() + .unwrap(); + assert!(b.fence.generation > a.fence.generation); + let write = WriteOp::Put { + ns: Namespace::Device, + key: "key".into(), + value: b"sealed-fixture".to_vec(), + }; + assert!(is_fence_lost( + &first + .write(&a.fence, std::slice::from_ref(&write)) + .await + .unwrap_err() + )); + second.write(&b.fence, &[write]).await.unwrap(); + assert!(first.renew(&a, 1101, 100).await.unwrap().is_none()); + assert_eq!( + first + .get("scope", Namespace::Device, "key") + .await + .unwrap() + .unwrap(), + b"sealed-fixture" + ); + drop(first); + drop(second); + let _ = std::fs::remove_file(path); +} +#[tokio::test] +async fn postgres_runs_every_recordstore_check() { + let Ok(url) = std::env::var("SIGNAL_NODE_TEST_POSTGRES_URL") else { + assert!( + std::env::var("CI").is_err(), + "CI must provide PostgreSQL conformance database" + ); + eprintln!("PostgreSQL conformance not run: SIGNAL_NODE_TEST_POSTGRES_URL unset"); + return; + }; + let store = PostgresStore::connect(&url, 8).await.unwrap(); + wacore_recordstore::conformance::run_all(&store).await; +} + +#[tokio::test] +async fn sql_records_cannot_be_swapped_between_scope_namespace_or_key() { + use std::sync::Arc; + use wacore_recordstore::{AesGcmSealer, RecordSignalStore}; + let records = Arc::new(SqliteStore::connect("sqlite::memory:", 1).await.unwrap()); + let a = records + .acquire("a", "holder", 0, 1000) + .await + .unwrap() + .unwrap(); + let b = records + .acquire("b", "holder", 0, 1000) + .await + .unwrap() + .unwrap(); + let sealer = Arc::new(AesGcmSealer::new(&[7; 32])); + let first = RecordSignalStore::new(records.clone(), sealer.clone(), a.fence.clone()); + let second = RecordSignalStore::new(records.clone(), sealer, b.fence.clone()); + first + .put_session("alice", b"sealed-secret-fixture") + .await + .unwrap(); + let raw = records + .get("a", Namespace::Session, "alice") + .await + .unwrap() + .unwrap(); + assert!(!raw.windows(21).any(|p| p == b"sealed-secret-fixture")); + records + .write( + &a.fence, + &[ + WriteOp::Put { + ns: Namespace::Session, + key: "mallory".into(), + value: raw.clone(), + }, + WriteOp::Put { + ns: Namespace::Device, + key: "local".into(), + value: raw.clone(), + }, + ], + ) + .await + .unwrap(); + records + .write( + &b.fence, + &[WriteOp::Put { + ns: Namespace::Session, + key: "alice".into(), + value: raw, + }], + ) + .await + .unwrap(); + assert!(first.get_session("mallory").await.is_err()); + assert!(first.load_device().await.is_err()); + assert!(second.get_session("alice").await.is_err()); + assert_eq!( + first.get_session("alice").await.unwrap().unwrap().as_ref(), + b"sealed-secret-fixture" + ); +} + +#[tokio::test] +async fn released_sql_fences_cannot_write_before_the_next_takeover() { + let store = SqliteStore::connect("sqlite::memory:", 1).await.unwrap(); + let lease = store + .acquire("released", "a", 0, 1000) + .await + .unwrap() + .unwrap(); + store.release(&lease).await.unwrap(); + assert!(is_fence_lost( + &store + .write( + &lease.fence, + &[WriteOp::Put { + ns: Namespace::Device, + key: "local".into(), + value: vec![1] + }] + ) + .await + .unwrap_err() + )); + if let Ok(url) = std::env::var("SIGNAL_NODE_TEST_POSTGRES_URL") { + let store = PostgresStore::connect(&url, 2).await.unwrap(); + let scope = format!("released-{}", rand::random::()); + let lease = store.acquire(&scope, "a", 0, 1000).await.unwrap().unwrap(); + store.release(&lease).await.unwrap(); + assert!(is_fence_lost( + &store + .write( + &lease.fence, + &[WriteOp::Put { + ns: Namespace::Device, + key: "local".into(), + value: vec![1] + }] + ) + .await + .unwrap_err() + )); + } +} diff --git a/tests/support/mod.rs b/tests/support/mod.rs new file mode 100644 index 0000000..4363b53 --- /dev/null +++ b/tests/support/mod.rs @@ -0,0 +1,601 @@ +#![allow(dead_code)] + +/// Build a generated protobuf message, whose structs are `#[non_exhaustive]` +/// and cannot be written as struct literals outside their crate: start from +/// `Default` and set the listed fields. +#[allow(unused_macros)] +macro_rules! proto { + ($ty:path { $($field:ident: $value:expr),* $(,)? }) => {{ + let mut message = <$ty>::default(); + $(message.$field = $value;)* + message + }}; +} +use futures_util::{SinkExt, StreamExt}; +use prost::Message; +use signal_node::{ + config::Config, + proto::{self, frame::Body, runner_response, service_request}, + store::Backend, +}; +use std::{ + collections::HashMap, + sync::{ + Arc, + atomic::{AtomicBool, AtomicU8, AtomicU64, Ordering}, + }, + time::Duration, +}; +use tokio::{ + net::TcpListener, + sync::{Mutex, mpsc, oneshot}, + task::JoinHandle, +}; +use tokio_tungstenite::tungstenite; + +pub const APP_SECRET: &str = "fixture-application-secret"; +pub const CONNECTION: &str = "connection-fixture"; +type SendGate = (Arc, Arc); +struct State { + connections: Mutex>>>, + pending: Mutex>, + claims: Mutex>, + disconnects: Mutex>>, + next: AtomicU64, + sends: Mutex)>>, + bundles: Mutex>, + send_reply: AtomicU8, + mismatch_next_send: AtomicBool, + resend_failure: AtomicU8, + send_gate: Mutex>, +} +enum PendingReply { + Frame(oneshot::Sender), + Lost(oneshot::Sender<()>), +} +impl PendingReply { + fn complete(self, frame: proto::Frame) { + match self { + Self::Frame(sender) => { + let _ = sender.send(frame); + } + // Deliberately discard the serialized service response while + // informing the test that it reached the transport boundary. + Self::Lost(sender) => { + let _ = sender.send(()); + } + } + } +} +#[derive(prost::Message)] +struct RawRunnerFrame { + #[prost(uint64, tag = "1")] + id: u64, + #[prost(bytes = "vec", tag = "4")] + request: Vec, +} +struct ContractHandshake; +impl tungstenite::handshake::server::Callback for ContractHandshake { + fn on_request( + self, + request: &tungstenite::handshake::server::Request, + mut response: tungstenite::handshake::server::Response, + ) -> Result< + tungstenite::handshake::server::Response, + tungstenite::handshake::server::ErrorResponse, + > { + assert_eq!( + request.headers()["sec-websocket-protocol"], + proto::SUBPROTOCOL + ); + assert_eq!( + request.headers()["authorization"], + "Bearer fixture-project-principal" + ); + assert!(!request.headers().contains_key("origin")); + response.headers_mut().insert( + "sec-websocket-protocol", + proto::SUBPROTOCOL.parse().unwrap(), + ); + Ok(response) + } +} +pub struct Hub { + pub url: String, + state: Arc, + task: JoinHandle<()>, +} +#[derive(Clone, Copy, Debug)] +pub enum ResendFailure { + DisconnectLookup = 1, + RefuseSend = 2, + LoseReply = 3, +} +impl Hub { + pub async fn start() -> Self { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("ws://{}/signal", listener.local_addr().unwrap()); + let state = Arc::new(State { + connections: Mutex::new(HashMap::new()), + pending: Mutex::new(HashMap::new()), + claims: Mutex::new(HashMap::new()), + disconnects: Mutex::new(HashMap::new()), + next: AtomicU64::new(10000), + sends: Mutex::new(vec![]), + bundles: Mutex::new(HashMap::new()), + send_reply: AtomicU8::new(0), + mismatch_next_send: AtomicBool::new(false), + resend_failure: AtomicU8::new(0), + send_gate: Mutex::new(None), + }); + let shared = state.clone(); + let task = tokio::spawn(async move { + loop { + let Ok((socket, _)) = listener.accept().await else { + break; + }; + let state = shared.clone(); + tokio::spawn(async move { + let socket = match tokio_tungstenite::accept_hdr_async( + socket, + ContractHandshake, + ) + .await + { + Ok(socket) => socket, + // Crash/reconnect scenarios may close before the + // handshake completes. Keep contract failures visible. + Err(tungstenite::Error::Io(error)) + if matches!( + error.kind(), + std::io::ErrorKind::ConnectionReset + | std::io::ErrorKind::UnexpectedEof + | std::io::ErrorKind::BrokenPipe + ) => + { + return; + } + Err(error) => panic!("fixture handshake failed: {error}"), + }; + let (mut sink, mut stream) = socket.split(); + let first = stream.next().await.unwrap().unwrap(); + let first = proto::Frame::decode(first.into_data()).unwrap(); + let Some(Body::Hello(hello)) = first.body else { + panic!("Expected Hello") + }; + assert_eq!(hello.protocol_version, 1); + let node_id = hello.node_id; + let link_id = + format!("{node_id}:{}", state.next.fetch_add(1, Ordering::SeqCst)); + let (disconnect_tx, mut disconnect_rx) = oneshot::channel(); + state + .disconnects + .lock() + .await + .insert(node_id.clone(), disconnect_tx); + sink.send(tungstenite::Message::Binary( + proto::Frame::with( + 0, + Body::Welcome(proto::Welcome { + protocol_version: 1, + project_id: "project-fixture".into(), + heartbeat_interval_ms: 100, + max_sends_per_second: 5, + }), + ) + .encode_to_vec() + .into(), + )) + .await + .unwrap(); + let (tx, mut rx) = mpsc::unbounded_channel::>(); + state + .connections + .lock() + .await + .insert(node_id.clone(), tx.clone()); + loop { + tokio::select! { + _ = &mut disconnect_rx => break, + out = rx.recv() => { + let Some(frame) = out else { break }; + if sink.send(tungstenite::Message::Binary(frame.into())).await.is_err() { break; } + } + incoming = stream.next() => { + let Some(Ok(tungstenite::Message::Binary(bytes))) = incoming else { break }; + let frame = proto::Frame::decode(bytes).unwrap(); + match frame.body { + Some(Body::ServiceRequest(request)) => { + let op = request.op.unwrap(); + let mut failure = None; + let result = match op { + service_request::Op::ClaimConnection(claim) => { + let mut claims = state.claims.lock().await; + if claims.get(&request.connection_id).is_some_and(|(generation, owner)| *generation > claim.generation || *generation == claim.generation && *owner != link_id) { + failure = Some(proto::Error::new(proto::Code::NotLeaseHolder, "stale claim")); + } else { + claims.insert(request.connection_id.clone(), (claim.generation, link_id.clone())); + } + runner_response::Result::ConnectionClaimed(proto::ConnectionClaimed { state: proto::connection_event::State::Connected as i32, pn: "222:1@s.whatsapp.net".into(), lid: String::new() }) + } + service_request::Op::ResolveDevices(_) => runner_response::Result::Devices(proto::Devices { jids: state.bundles.lock().await.keys().cloned().collect() }), + service_request::Op::FetchPreKeys(query) => runner_response::Result::Bundles(proto::Bundles { bundles: state.bundles.lock().await.iter().filter(|(jid, _)| query.jids.contains(jid)).map(|(jid, bundle)| (jid.clone(), bundle.clone())).collect() }), + service_request::Op::ResolveGroup(_) => { + if state.resend_failure.load(Ordering::SeqCst) == ResendFailure::DisconnectLookup as u8 && !state.sends.lock().await.is_empty() { + break; + } + runner_response::Result::GroupRouting(proto::GroupRouting { + participants: vec!["111@s.whatsapp.net".into(), "222@s.whatsapp.net".into()], + addressing_mode: "pn".into(), + lid_to_pn: HashMap::new(), + }) + }, + service_request::Op::SendStanza(stanza) => { + assert!(!stanza.node.windows(b"application-private-text".len()).any(|b| b == b"application-private-text")); + let resend = { + let mut sends = state.sends.lock().await; + sends.push((tokio::time::Instant::now(), stanza.node)); + sends.len() > 1 + }; + if let Some((observed, release)) = state.send_gate.lock().await.take() { + observed.notify_one(); + release.notified().await; + } + match state.send_reply.load(Ordering::SeqCst) { + 1 => continue, // Accepted, but its reply is lost. + 2 => break, // Link lost after accepting the stanza. + _ => {} + } + if resend { + match state.resend_failure.load(Ordering::SeqCst) { + 2 => failure = Some(proto::Error::new(proto::Code::Unavailable, "synthetic resend refusal")), + 3 => continue, + _ => {} + } + } + runner_response::Result::Send(proto::SendResult { server_timestamp: 1720000000, participant_hash_mismatch: state.mismatch_next_send.swap(false, Ordering::SeqCst) }) + } + service_request::Op::Ping(_) | service_request::Op::ReleaseConnection(_) => runner_response::Result::Ok(proto::Ok {}), + }; + let body = match failure { Some(error) => Body::Error(error), None => Body::RunnerResponse(proto::RunnerResponse { result: Some(result) }) }; + tx.send(proto::Frame::with(frame.id, body).encode_to_vec()).unwrap(); + } + Some(Body::ServiceResponse(response)) => { + if let Some(pending) = state.pending.lock().await.remove(&frame.id) { pending.complete(proto::Frame::with(frame.id, Body::ServiceResponse(response))); } + } + Some(Body::Error(error)) => { + if let Some(pending) = state.pending.lock().await.remove(&frame.id) { pending.complete(proto::Frame::with(frame.id, Body::Error(error))); } + } + _ => panic!("Unexpected fixture frame"), + } + } + } + } + }); + } + }); + Self { url, state, task } + } + pub async fn command( + &self, + node: &str, + generation: u64, + op: proto::runner_request::Op, + ) -> proto::Frame { + self.request( + node, + proto::RunnerRequest { + connection_id: CONNECTION.into(), + lease_generation: generation, + op: Some(op), + }, + ) + .await + } + pub async fn request(&self, node: &str, request: proto::RunnerRequest) -> proto::Frame { + self.raw_request(node, request.encode_to_vec()).await + } + pub async fn raw_request(&self, node: &str, request: Vec) -> proto::Frame { + let id = self.state.next.fetch_add(1, Ordering::Relaxed); + let (tx, rx) = oneshot::channel(); + self.state + .pending + .lock() + .await + .insert(id, PendingReply::Frame(tx)); + let out = self + .state + .connections + .lock() + .await + .get(node) + .unwrap() + .clone(); + out.send(RawRunnerFrame { id, request }.encode_to_vec()) + .unwrap(); + tokio::time::timeout(Duration::from_secs(5), rx) + .await + .unwrap() + .unwrap() + } + pub async fn request_losing_response(&self, node: &str, request: proto::RunnerRequest) { + let id = self.state.next.fetch_add(1, Ordering::Relaxed); + let (tx, rx) = oneshot::channel(); + self.state + .pending + .lock() + .await + .insert(id, PendingReply::Lost(tx)); + self.state + .connections + .lock() + .await + .get(node) + .unwrap() + .send( + RawRunnerFrame { + id, + request: request.encode_to_vec(), + } + .encode_to_vec(), + ) + .unwrap(); + tokio::time::timeout(Duration::from_secs(5), rx) + .await + .unwrap() + .unwrap(); + } + pub async fn send_unanswered_frame(&self, node: &str, bytes: Vec) { + self.state + .connections + .lock() + .await + .get(node) + .unwrap() + .send(bytes) + .unwrap(); + } + pub async fn disconnect(&self, node: &str) { + self.state + .disconnects + .lock() + .await + .remove(node) + .unwrap() + .send(()) + .unwrap(); + } + pub async fn bundle(&self, jid: &str, bundle: proto::Bundle) { + self.state.bundles.lock().await.insert(jid.into(), bundle); + } + pub async fn sends(&self) -> Vec<(tokio::time::Instant, Vec)> { + self.state.sends.lock().await.clone() + } + pub fn lose_send_reply(&self, disconnect: bool) { + self.state + .send_reply + .store(if disconnect { 2 } else { 1 }, Ordering::SeqCst); + } + pub fn mismatch_next_send(&self) { + self.state.mismatch_next_send.store(true, Ordering::SeqCst); + } + pub fn fail_resend(&self, failure: ResendFailure) { + self.mismatch_next_send(); + self.state + .resend_failure + .store(failure as u8, Ordering::SeqCst); + } + pub async fn gate_next_send(&self) -> (Arc, Arc) { + let observed = Arc::new(tokio::sync::Notify::new()); + let release = Arc::new(tokio::sync::Notify::new()); + *self.state.send_gate.lock().await = Some((observed.clone(), release.clone())); + (observed, release) + } +} +impl Drop for Hub { + fn drop(&mut self) { + self.task.abort(); + } +} + +pub fn config(node: &str, url: &str, history: bool) -> signal_node::config::Resolved { + let file = format!( + r#"node_id="{node}" + [polymorfa] + url="{url}" + credential="fixture-project-principal" + connections=["{CONNECTION}"] + request_timeout_ms=1000 + reconnect_min_ms=10 + reconnect_max_ms=50 + [storage] + driver="memory" + master_key="CQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQk=" + [http] + listen="127.0.0.1:0" + secret="{APP_SECRET}" + [leases] + ttl_ms=500 + renew_interval_ms=100 + [delivery] + ack_timeout_ms=250 + [history] + deliver={history} + "# + ); + Config::from_toml(&file).unwrap().resolve().unwrap() +} +pub async fn start(hub: &Hub, node: &str, backend: Backend, history: bool) -> signal_node::Running { + let running = signal_node::start(config(node, &hub.url, history), backend) + .await + .unwrap(); + wait(|| { + running + .node + .connection(CONNECTION) + .is_some_and(|s| s.is_claimed()) + }) + .await; + running +} +pub async fn wait(test: impl Fn() -> bool) { + tokio::time::timeout(Duration::from_secs(5), async { + while !test() { + tokio::time::sleep(Duration::from_millis(5)).await; + } + }) + .await + .unwrap(); +} +pub async fn peer( + scope: &str, +) -> ( + Arc< + wacore_signal_ops::SignalOps< + wacore_recordstore::RecordSignalStore< + wacore_recordstore::MemoryRecordStore, + wacore_recordstore::AesGcmSealer, + >, + >, + >, + proto::Bundle, +) { + peer_under(scope, None).await +} +/// A fixture peer paired under `account` when given (another device of that +/// account), otherwise under a fresh account of its own. +pub async fn peer_under( + scope: &str, + account: Option<&wacore::libsignal::protocol::KeyPair>, +) -> ( + Arc< + wacore_signal_ops::SignalOps< + wacore_recordstore::RecordSignalStore< + wacore_recordstore::MemoryRecordStore, + wacore_recordstore::AesGcmSealer, + >, + >, + >, + proto::Bundle, +) { + use wacore_recordstore::{AesGcmSealer, LeaseStore, MemoryRecordStore, RecordSignalStore}; + let records = Arc::new(MemoryRecordStore::new()); + let lease = records + .acquire(scope, "peer", 0, 60000) + .await + .unwrap() + .unwrap(); + let store = Arc::new(RecordSignalStore::new( + records, + Arc::new(AesGcmSealer::new(&[7; 32])), + lease.fence, + )); + let ops = Arc::new(wacore_signal_ops::SignalOps::create(store).await.unwrap()); + // Every fixture peer is a paired companion, so its bundle carries the + // device identity a sender verifies it with. + let device_identity = match account { + Some(account) => pair_under(&*ops, account).await, + None => pair(&*ops).await.device_identity, + }; + let identity = ops.public_identity().await; + let pre = ops + .generate_prekeys(&signal_node::resolver::TokioRuntime, 1) + .await + .unwrap() + .remove(0); + let bundle = proto::Bundle { + registration_id: identity.registration_id, + device_id: 1, + identity_key: identity.identity_key.to_vec(), + signed_pre_key_id: identity.signed_prekey.id, + signed_pre_key: identity.signed_prekey.public_key.to_vec(), + signed_pre_key_signature: identity.signed_prekey.signature.to_vec(), + pre_key: Some(proto::PreKey { + id: pre.id, + public_key: pre.public_key.to_vec(), + }), + device_identity, + }; + (ops, bundle) +} +/// A paired device as its peers see it: the account key that signed it, and +/// the `` its bundles and prekey messages carry. +pub struct Paired { + pub account: [u8; 32], + /// The account key pair, to pair another device under the same account. + pub account_keys: wacore::libsignal::protocol::KeyPair, + pub device_identity: Vec, +} + +/// Pin the account that signed `device_identity` for `jid` on `ops`, as a +/// sender does once it has met that account's primary device. +pub async fn vouch( + ops: &wacore_signal_ops::SignalOps, + jid: &str, + device_identity: &[u8], +) { + let signed = waproto::codec::adv_signed_device_identity_decode(device_identity).unwrap(); + let account: [u8; 32] = signed + .account_signature_key + .unwrap() + .as_slice() + .try_into() + .unwrap(); + ops.trust_account_identity(&jid.parse().unwrap(), account) + .await + .unwrap(); +} + +pub async fn pair( + ops: &wacore_signal_ops::SignalOps, +) -> Paired { + use wacore::libsignal::protocol::KeyPair; + let mut rng = rand10::make_rng::(); + let account = KeyPair::generate(&mut rng); + let device_identity = pair_under(ops, &account).await; + Paired { + account: *account.public_key.public_key_bytes(), + account_keys: account, + device_identity, + } +} + +/// Pair `ops` under an existing `account` key, as a primary does for another +/// device of the same account. Returns its ``. +pub async fn pair_under( + ops: &wacore_signal_ops::SignalOps, + account: &wacore::libsignal::protocol::KeyPair, +) -> Vec { + use hmac::Mac; + use wacore::libsignal::protocol::{KeyPair, PublicKey}; + use waproto::whatsapp as wa; + let mut rng = rand10::make_rng::(); + let identity = ops.public_identity().await; + let device = KeyPair::new( + PublicKey::from_djb_public_key_bytes(&identity.identity_key).unwrap(), + KeyPair::generate(&mut rng).private_key, + ); + let details = signal_node::message::encode(&proto!(wa::ADVDeviceIdentity { + raw_id: Some(1), + timestamp: Some(0), + key_index: Some(1), + account_type: Some(wa::ADVEncryptionType::E2EE), + device_type: Some(wa::ADVEncryptionType::E2EE) + })); + let signed = signal_node::message::encode(&wacore::adv::test_util::signed_identity( + account, + &device, + &details, + wacore::adv::test_util::account_prefix(Some(wa::ADVEncryptionType::E2EE)), + None, + true, + )); + let mut mac = as Mac>::new_from_slice(&identity.adv_secret).unwrap(); + mac.update(&signed); + let container = signal_node::message::encode(&proto!(wa::ADVSignedDeviceIdentityHMAC { + details: Some(signed), + hmac: Some(mac.finalize().into_bytes().to_vec()), + account_type: Some(wa::ADVEncryptionType::E2EE) + })); + ops.sign_pairing(&container).await.unwrap().signed_identity +}