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.
This commit is contained in:
@@ -118,3 +118,45 @@ public sealed class BundleSecretEncryptorTests
|
||||
Assert.Equal(plaintext, recovered);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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]));
|
||||
|
||||
/// <summary>A metadata record whose iteration count was set past the ceiling is refused before PBKDF2 runs.</summary>
|
||||
[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<CryptographicException>(() =>
|
||||
new BundleSecretEncryptor().Decrypt(new byte[32], metadata, "pass"));
|
||||
Assert.True(sw.Elapsed < TimeSpan.FromSeconds(5), "the key must not be derived first");
|
||||
}
|
||||
|
||||
/// <summary>A manifest JSON carrying an absurd iteration count does not yield a usable metadata record.</summary>
|
||||
[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<Exception>(() =>
|
||||
System.Text.Json.JsonSerializer.Deserialize<EncryptionMetadata>(
|
||||
json, ZB.MOM.WW.ScadaBridge.Transport.Serialization.BundleJsonOptions.Default));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user