From c207809ef398a770230093b769b4dfaf953da757 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Thu, 3 Dec 2026 09:43:00 -0500 Subject: [PATCH] fix(transport): range-check the manifest KDF iteration count before deriving the key (TRN-10, WP2 task 2.7) BundleSecretEncryptor.Decrypt now refuses an iteration count outside [MinPbkdf2Iterations, MaxPbkdf2Iterations] before PBKDF2 runs. The manifest JSON path was already guarded by the EncryptionMetadata constructor (pinned by a new test), but a record 'with' (or any non-ctor path) bypassed it and derived with int.MaxValue iterations; the encryptor no longer relies on it. --- .../Encryption/BundleSecretEncryptor.cs | 13 ++++++ .../Encryption/BundleSecretEncryptorTests.cs | 42 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/ZB.MOM.WW.ScadaBridge.Transport/Encryption/BundleSecretEncryptor.cs b/src/ZB.MOM.WW.ScadaBridge.Transport/Encryption/BundleSecretEncryptor.cs index ec5fcfd5..7c515a12 100644 --- a/src/ZB.MOM.WW.ScadaBridge.Transport/Encryption/BundleSecretEncryptor.cs +++ b/src/ZB.MOM.WW.ScadaBridge.Transport/Encryption/BundleSecretEncryptor.cs @@ -83,6 +83,19 @@ public sealed class BundleSecretEncryptor throw new CryptographicException("Unsupported bundle encryption parameters."); } + // TRN-10: the iteration count comes from the uploaded (untrusted) manifest. + // EncryptionMetadata's constructor range-checks it, but a record `with` + // expression or any other non-constructor path bypasses that, so re-check + // here — before PBKDF2 runs — or a hostile bundle declaring int.MaxValue + // pins a thread (and the single-flight transport gate) for tens of minutes. + if (metadata.Iterations < EncryptionMetadata.MinPbkdf2Iterations + || metadata.Iterations > EncryptionMetadata.MaxPbkdf2Iterations) + { + throw new CryptographicException( + $"Bundle key-derivation iteration count {metadata.Iterations} is outside the supported range " + + $"[{EncryptionMetadata.MinPbkdf2Iterations}, {EncryptionMetadata.MaxPbkdf2Iterations}]."); + } + var salt = Convert.FromBase64String(metadata.SaltB64); var nonce = Convert.FromBase64String(metadata.IvB64); var key = DeriveKey(passphrase, salt, metadata.Iterations); diff --git a/tests/ZB.MOM.WW.ScadaBridge.Transport.Tests/Encryption/BundleSecretEncryptorTests.cs b/tests/ZB.MOM.WW.ScadaBridge.Transport.Tests/Encryption/BundleSecretEncryptorTests.cs index c643fade..b519b065 100644 --- a/tests/ZB.MOM.WW.ScadaBridge.Transport.Tests/Encryption/BundleSecretEncryptorTests.cs +++ b/tests/ZB.MOM.WW.ScadaBridge.Transport.Tests/Encryption/BundleSecretEncryptorTests.cs @@ -118,3 +118,45 @@ public sealed class BundleSecretEncryptorTests Assert.Equal(plaintext, recovered); } } + +/// +/// TRN-10 (WP2 task 2.7): the PBKDF2 iteration count read from an uploaded manifest +/// must be range-checked before the key is derived, so a hostile bundle cannot pin a +/// thread (and the single-flight transport gate) with an absurd count. +/// +public sealed class BundleSecretEncryptorIterationBoundTests +{ + private static EncryptionMetadata ValidMetadata() => + new("AES-256-GCM", "PBKDF2-SHA256", EncryptionMetadata.MinPbkdf2Iterations, + Convert.ToBase64String(new byte[16]), Convert.ToBase64String(new byte[12])); + + /// A metadata record whose iteration count was set past the ceiling is refused before PBKDF2 runs. + [Theory] + [InlineData(int.MaxValue)] + [InlineData(EncryptionMetadata.MaxPbkdf2Iterations + 1)] + [InlineData(EncryptionMetadata.MinPbkdf2Iterations - 1)] + [InlineData(0)] + [InlineData(-1)] + public void Decrypt_rejects_out_of_range_iterations_without_deriving(int iterations) + { + // `with` bypasses the record constructor's range check — the encryptor must + // not rely on it. + var metadata = ValidMetadata() with { Iterations = iterations }; + var sw = System.Diagnostics.Stopwatch.StartNew(); + Assert.ThrowsAny(() => + new BundleSecretEncryptor().Decrypt(new byte[32], metadata, "pass")); + Assert.True(sw.Elapsed < TimeSpan.FromSeconds(5), "the key must not be derived first"); + } + + /// A manifest JSON carrying an absurd iteration count does not yield a usable metadata record. + [Fact] + public void Manifest_with_absurd_iterations_is_rejected_on_read() + { + var json = """ + {"algorithm":"AES-256-GCM","kdf":"PBKDF2-SHA256","iterations":2147483647,"saltB64":"","ivB64":""} + """; + Assert.ThrowsAny(() => + System.Text.Json.JsonSerializer.Deserialize( + json, ZB.MOM.WW.ScadaBridge.Transport.Serialization.BundleJsonOptions.Default)); + } +}