Skip to content

Commit d136ec9

Browse files
authored
Fix dispose pattern in PQC types
1 parent 0c7d43e commit d136ec9

File tree

8 files changed

+27
-13
lines changed

8 files changed

+27
-13
lines changed

src/libraries/Common/src/System/Security/Cryptography/MLDsaCng.Windows.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,13 @@ internal static MLDsaCng ImportPkcs8PrivateKey(byte[] source, out int bytesRead)
359359
/// <inheritdoc/>
360360
protected override void Dispose(bool disposing)
361361
{
362-
_key.Dispose();
363-
_key = null!;
362+
if (disposing)
363+
{
364+
_key.Dispose();
365+
_key = null!;
366+
}
367+
368+
base.Dispose(disposing);
364369
}
365370

366371
private void ExportKey(

src/libraries/Common/src/System/Security/Cryptography/MLDsaImplementation.Windows.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,13 @@ protected override bool TryExportPkcs8PrivateKeyCore(Span<byte> destination, out
160160

161161
protected override void Dispose(bool disposing)
162162
{
163-
_key?.Dispose();
164-
_key = null!;
163+
if (disposing)
164+
{
165+
_key?.Dispose();
166+
_key = null!;
167+
}
168+
169+
base.Dispose(disposing);
165170
}
166171

167172
private void ExportKey(string keyBlobType, int expectedKeySize, Span<byte> destination)

src/libraries/Common/src/System/Security/Cryptography/MLKemCng.Windows.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,11 @@ protected override void Dispose(bool disposing)
218218
{
219219
if (disposing)
220220
{
221-
_key.Dispose();
221+
_key?.Dispose();
222222
_key = null!;
223223
}
224+
225+
base.Dispose(disposing);
224226
}
225227

226228
private void ExportKey(KeyBlobMagicNumber kind, Span<byte> destination)

src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/MLDsaOpenSsl.OpenSsl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ private static partial MLDsaAlgorithm AlgorithmFromHandle(
6060
/// <inheritdoc />
6161
protected override void Dispose(bool disposing)
6262
{
63-
base.Dispose(disposing);
64-
6563
if (disposing)
6664
{
6765
_key.Dispose();
6866
}
67+
68+
base.Dispose(disposing);
6969
}
7070

7171
/// <inheritdoc />

src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/MLKemImplementation.OpenSsl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ internal static MLKemImplementation ImportEncapsulationKeyImpl(MLKemAlgorithm al
6464

6565
protected override void Dispose(bool disposing)
6666
{
67-
base.Dispose(disposing);
68-
6967
if (disposing)
7068
{
7169
_key.Dispose();
7270
}
71+
72+
base.Dispose(disposing);
7373
}
7474

7575
internal SafeEvpPKeyHandle DuplicateHandle() => _key.DuplicateHandle();

src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/MLKemOpenSsl.OpenSsl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ private static partial MLKemAlgorithm AlgorithmFromHandle(
5959
/// <inheritdoc />
6060
protected override void Dispose(bool disposing)
6161
{
62-
base.Dispose(disposing);
63-
6462
if (disposing)
6563
{
6664
_key.Dispose();
6765
}
66+
67+
base.Dispose(disposing);
6868
}
6969

7070
/// <inheritdoc />

src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/SlhDsaImplementation.OpenSsl.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ protected override void Dispose(bool disposing)
5050
_key?.Dispose();
5151
_key = null!;
5252
}
53+
54+
base.Dispose(disposing);
5355
}
5456

5557
internal static partial SlhDsaImplementation GenerateKeyCore(SlhDsaAlgorithm algorithm)

src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/SlhDsaOpenSsl.OpenSsl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ private static partial SlhDsaAlgorithm AlgorithmFromHandle(SafeEvpPKeyHandle pke
7272
/// <inheritdoc />
7373
protected override void Dispose(bool disposing)
7474
{
75-
base.Dispose(disposing);
76-
7775
if (disposing)
7876
{
7977
_key.Dispose();
8078
}
79+
80+
base.Dispose(disposing);
8181
}
8282

8383
protected override void SignDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byte> context, Span<byte> destination) =>

0 commit comments

Comments
 (0)