Skip to content

Commit 7978f36

Browse files
authored
Dispose secret instances (#51253)
1 parent 68a0278 commit 7978f36

7 files changed

+17
-16
lines changed

src/DataProtection/DataProtection/src/AuthenticatedEncryption/CngCbcAuthenticatedEncryptorFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ public CngCbcAuthenticatedEncryptorFactory(ILoggerFactory loggerFactory)
5555
return null;
5656
}
5757

58+
using var key = new Secret(secret);
5859
return new CbcAuthenticatedEncryptor(
59-
keyDerivationKey: new Secret(secret),
60+
keyDerivationKey: key,
6061
symmetricAlgorithmHandle: GetSymmetricBlockCipherAlgorithmHandle(configuration),
6162
symmetricAlgorithmKeySizeInBytes: (uint)(configuration.EncryptionAlgorithmKeySize / 8),
6263
hmacAlgorithmHandle: GetHmacAlgorithmHandle(configuration));

src/DataProtection/DataProtection/src/AuthenticatedEncryption/CngGcmAuthenticatedEncryptorFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ public CngGcmAuthenticatedEncryptorFactory(ILoggerFactory loggerFactory)
5757
return null;
5858
}
5959

60+
using var key = new Secret(secret);
6061
return new CngGcmAuthenticatedEncryptor(
61-
keyDerivationKey: new Secret(secret),
62+
keyDerivationKey: key,
6263
symmetricAlgorithmHandle: GetSymmetricBlockCipherAlgorithmHandle(configuration),
6364
symmetricAlgorithmKeySizeInBytes: (uint)(configuration.EncryptionAlgorithmKeySize / 8));
6465
}

src/DataProtection/DataProtection/src/AuthenticatedEncryption/ConfigurationModel/AuthenticatedEncryptorConfiguration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ void IInternalAlgorithmConfiguration.Validate()
4444
{
4545
var factory = new AuthenticatedEncryptorFactory(NullLoggerFactory.Instance);
4646
// Run a sample payload through an encrypt -> decrypt operation to make sure data round-trips properly.
47-
var encryptor = factory.CreateAuthenticatedEncryptorInstance(Secret.Random(512 / 8), this);
47+
using var secret = Secret.Random(512 / 8);
48+
var encryptor = factory.CreateAuthenticatedEncryptorInstance(secret, this);
4849
try
4950
{
5051
encryptor.PerformSelfTest();

src/DataProtection/DataProtection/src/AuthenticatedEncryption/ConfigurationModel/CngCbcAuthenticatedEncryptorConfiguration.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,8 @@ void IInternalAlgorithmConfiguration.Validate()
9494
{
9595
var factory = new CngCbcAuthenticatedEncryptorFactory(NullLoggerFactory.Instance);
9696
// Run a sample payload through an encrypt -> decrypt operation to make sure data round-trips properly.
97-
using (var encryptor = factory.CreateAuthenticatedEncryptorInstance(Secret.Random(512 / 8), this))
98-
{
99-
encryptor.PerformSelfTest();
100-
}
97+
using var secret = Secret.Random(512 / 8);
98+
using var encryptor = factory.CreateAuthenticatedEncryptorInstance(secret, this);
99+
encryptor.PerformSelfTest();
101100
}
102101
}

src/DataProtection/DataProtection/src/AuthenticatedEncryption/ConfigurationModel/CngGcmAuthenticatedEncryptorConfiguration.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ void IInternalAlgorithmConfiguration.Validate()
7070
{
7171
var factory = new CngGcmAuthenticatedEncryptorFactory(NullLoggerFactory.Instance);
7272
// Run a sample payload through an encrypt -> decrypt operation to make sure data round-trips properly.
73-
using (var encryptor = factory.CreateAuthenticatedEncryptorInstance(Secret.Random(512 / 8), this))
74-
{
75-
encryptor.PerformSelfTest();
76-
}
73+
using var secret = Secret.Random(512 / 8);
74+
using var encryptor = factory.CreateAuthenticatedEncryptorInstance(secret, this);
75+
encryptor.PerformSelfTest();
7776
}
7877
}

src/DataProtection/DataProtection/src/AuthenticatedEncryption/ConfigurationModel/ManagedAuthenticatedEncryptorConfiguration.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,9 @@ void IInternalAlgorithmConfiguration.Validate()
7373
{
7474
var factory = new ManagedAuthenticatedEncryptorFactory(NullLoggerFactory.Instance);
7575
// Run a sample payload through an encrypt -> decrypt operation to make sure data round-trips properly.
76-
using (var encryptor = factory.CreateAuthenticatedEncryptorInstance(Secret.Random(512 / 8), this))
77-
{
78-
encryptor.PerformSelfTest();
79-
}
76+
using var secret = Secret.Random(512 / 8);
77+
using var encryptor = factory.CreateAuthenticatedEncryptorInstance(secret, this);
78+
encryptor.PerformSelfTest();
8079
}
8180

8281
// Any changes to this method should also be be reflected

src/DataProtection/DataProtection/src/Cng/DpapiSecretSerializerHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public static bool CanProtectToCurrentUserAccount()
3030
try
3131
{
3232
Guid dummy;
33-
ProtectWithDpapi(new Secret((byte*)&dummy, sizeof(Guid)), protectToLocalMachine: false);
33+
using var secret = new Secret((byte*)&dummy, sizeof(Guid));
34+
ProtectWithDpapi(secret, protectToLocalMachine: false);
3435
return true;
3536
}
3637
catch

0 commit comments

Comments
 (0)