feat(windows): add DPAPI token protection
Dpapi.Protect/Unprotect wrap CryptProtectData/CryptUnprotectData in the current-user scope with optional entropy and no UI; decrypted native buffers are zeroed before LocalFree. String helpers return Base64 for JSON settings. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
2 files changed
+159
No files matched your search
@@ -0,0 +1,48 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Titano.Core.Windows.Security;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Core.Windows.Tests.Security;
|
||||
|
||||
[WindowsOnly]
|
||||
public sealed class DpapiTests
|
||||
{
|
||||
private static readonly byte[] Secret = Encoding.UTF8.GetBytes("titano-refresh-token-0123456789");
|
||||
|
||||
[Test]
|
||||
public void RoundTripsBytes()
|
||||
{
|
||||
var protectedData = Dpapi.Protect(Secret);
|
||||
Assert.False(protectedData.AsSpan().SequenceEqual(Secret), "ciphertext equals plaintext");
|
||||
Assert.Greater(protectedData.Length, Secret.Length);
|
||||
Assert.BytesEqual(Secret, Dpapi.Unprotect(protectedData));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EntropyMustMatch()
|
||||
{
|
||||
var entropy = "Titano.Uploader.Token"u8.ToArray();
|
||||
var protectedData = Dpapi.Protect(Secret, entropy);
|
||||
Assert.BytesEqual(Secret, Dpapi.Unprotect(protectedData, entropy));
|
||||
Assert.Throws<CryptographicException>(() => Dpapi.Unprotect(protectedData, "other"u8));
|
||||
Assert.Throws<CryptographicException>(() => Dpapi.Unprotect(protectedData));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TamperedDataIsRejected()
|
||||
{
|
||||
var protectedData = Dpapi.Protect(Secret);
|
||||
protectedData[^1] ^= 0x5A;
|
||||
Assert.Throws<CryptographicException>(() => Dpapi.Unprotect(protectedData));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RoundTripsStringsAndEmptyValues()
|
||||
{
|
||||
var stored = Dpapi.ProtectString("päss wörd ✓");
|
||||
Assert.Equal("päss wörd ✓", Dpapi.UnprotectString(stored));
|
||||
Assert.Equal(string.Empty, Dpapi.UnprotectString(Dpapi.ProtectString(string.Empty)));
|
||||
Assert.Empty(Dpapi.Unprotect(Dpapi.Protect([])));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user