@@ -6,6 +6,11 @@ namespace DSInternals.Common.Test;
66[ TestClass ]
77public class DnsSigningKeyTester
88{
9+ // The Windows SID key cache API overwrites the entire L0 key file on each write.
10+ // All TFM test processes must serialize their write+decrypt sequences to avoid
11+ // a process overwriting another's cache entry between WriteToCache() and Decrypt().
12+ private static readonly Mutex CacheMutex = new Mutex ( false , @"Local\DSInternals_DnsSigningKeyTest" ) ;
13+
914 [ TestMethod ]
1015 public void DnsSigningKey_KSK_RSA ( )
1116 {
@@ -30,8 +35,17 @@ public void DnsSigningKey_KSK_RSA()
3035
3136 // Decrypt the key
3237 var gke = GroupKeyEnvelope . Create ( kdsRootKey , signingKey . ProtectedKeyBlob . ProtectionKeyIdentifier , signingKey . ProtectedKeyBlob . TargetSid ) ;
33- gke . WriteToCache ( ) ;
34- ReadOnlySpan < byte > decryptedKey = signingKey . ProtectedKeyBlob . Decrypt ( ) ;
38+ CacheMutex . WaitOne ( ) ;
39+ ReadOnlySpan < byte > decryptedKey ;
40+ try
41+ {
42+ gke . WriteToCache ( ) ;
43+ decryptedKey = signingKey . ProtectedKeyBlob . Decrypt ( ) ;
44+ }
45+ finally
46+ {
47+ CacheMutex . ReleaseMutex ( ) ;
48+ }
3549
3650 // Assert decrypted key magic
3751 Assert . StartsWith ( "52534132" , decryptedKey . ToHex ( ) ) ; // "RSA2" encoded in HEX (RSAPUBKEY structure magic)
@@ -57,8 +71,17 @@ public void DnsSigningKey_ZSK_RSA()
5771
5872 // Decrypt the key
5973 var gke = GroupKeyEnvelope . Create ( kdsRootKey , signingKey . ProtectedKeyBlob . ProtectionKeyIdentifier , signingKey . ProtectedKeyBlob . TargetSid ) ;
60- gke . WriteToCache ( ) ;
61- ReadOnlySpan < byte > decryptedKey = signingKey . ProtectedKeyBlob . Decrypt ( ) ;
74+ CacheMutex . WaitOne ( ) ;
75+ ReadOnlySpan < byte > decryptedKey ;
76+ try
77+ {
78+ gke . WriteToCache ( ) ;
79+ decryptedKey = signingKey . ProtectedKeyBlob . Decrypt ( ) ;
80+ }
81+ finally
82+ {
83+ CacheMutex . ReleaseMutex ( ) ;
84+ }
6285
6386 // Assert decrypted key magic
6487 Assert . StartsWith ( "52534132" , decryptedKey . ToHex ( ) ) ; // "RSA2" encoded in HEX (RSAPUBKEY structure magic)
@@ -84,8 +107,17 @@ public void DnsSigningKey_KSK_P256()
84107
85108 // Decrypt the key
86109 var gke = GroupKeyEnvelope . Create ( kdsRootKey , signingKey . ProtectedKeyBlob . ProtectionKeyIdentifier , signingKey . ProtectedKeyBlob . TargetSid ) ;
87- gke . WriteToCache ( ) ;
88- ReadOnlySpan < byte > decryptedKey = signingKey . ProtectedKeyBlob . Decrypt ( ) ;
110+ CacheMutex . WaitOne ( ) ;
111+ ReadOnlySpan < byte > decryptedKey ;
112+ try
113+ {
114+ gke . WriteToCache ( ) ;
115+ decryptedKey = signingKey . ProtectedKeyBlob . Decrypt ( ) ;
116+ }
117+ finally
118+ {
119+ CacheMutex . ReleaseMutex ( ) ;
120+ }
89121
90122 // Assert decrypted key magic
91123 Assert . StartsWith ( "45435332" , decryptedKey . ToHex ( ) ) ; // "ECK2" encoded in HEX
0 commit comments