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:
@@ -83,6 +83,19 @@ public sealed class BundleSecretEncryptor
|
|||||||
throw new CryptographicException("Unsupported bundle encryption parameters.");
|
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 salt = Convert.FromBase64String(metadata.SaltB64);
|
||||||
var nonce = Convert.FromBase64String(metadata.IvB64);
|
var nonce = Convert.FromBase64String(metadata.IvB64);
|
||||||
var key = DeriveKey(passphrase, salt, metadata.Iterations);
|
var key = DeriveKey(passphrase, salt, metadata.Iterations);
|
||||||
|
|||||||
@@ -118,3 +118,45 @@ public sealed class BundleSecretEncryptorTests
|
|||||||
Assert.Equal(plaintext, recovered);
|
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