summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-03-10 13:28:46 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-03-10 13:28:46 +0700
commit9995dc06402d6dbe33862c151eab6150c4de14e5 (patch)
treec3c1ab51a7cdd8bbbda6d14411900ddea4666fab
parentRemove unnecessary methods (diff)
downloadBouncyCastle.NET-ed25519-9995dc06402d6dbe33862c151eab6150c4de14e5.tar.xz
Refactor IDisposable implementations
-rw-r--r--crypto/src/asn1/ASN1Generator.cs2
-rw-r--r--crypto/src/asn1/Asn1InputStream.cs5
-rw-r--r--crypto/src/asn1/x509/TBSCertList.cs10
-rw-r--r--crypto/src/cms/CMSContentInfoParser.cs2
-rw-r--r--crypto/src/crypto/prng/CryptoApiRandomGenerator.cs1
-rw-r--r--crypto/src/pqc/crypto/bike/BikeKemGenerator.cs5
-rw-r--r--crypto/src/pqc/crypto/crystals/kyber/KyberKEMGenerator.cs3
-rw-r--r--crypto/src/pqc/crypto/hqc/HqcKemGenerator.cs5
-rw-r--r--crypto/src/pqc/crypto/ntru/NtruEncapsulation.cs3
-rw-r--r--crypto/src/pqc/crypto/ntruprime/NtruLPRimeKemGenerator.cs20
-rw-r--r--crypto/src/pqc/crypto/ntruprime/SNtruPrimeKemGenerator.cs20
-rw-r--r--crypto/src/pqc/crypto/utils/SecretWithEncapsulationImpl.cs17
-rw-r--r--crypto/src/util/io/pem/PemReader.cs2
-rw-r--r--crypto/src/util/io/pem/PemWriter.cs2
14 files changed, 65 insertions, 32 deletions
diff --git a/crypto/src/asn1/ASN1Generator.cs b/crypto/src/asn1/ASN1Generator.cs
index 28fffc6ab..1e8351149 100644
--- a/crypto/src/asn1/ASN1Generator.cs
+++ b/crypto/src/asn1/ASN1Generator.cs
@@ -30,7 +30,7 @@ namespace Org.BouncyCastle.Asn1
 
         public void Dispose()
         {
-            Dispose(true);
+            Dispose(disposing: true);
             GC.SuppressFinalize(this);
         }
 
diff --git a/crypto/src/asn1/Asn1InputStream.cs b/crypto/src/asn1/Asn1InputStream.cs
index bd93d4758..b4a87eae1 100644
--- a/crypto/src/asn1/Asn1InputStream.cs
+++ b/crypto/src/asn1/Asn1InputStream.cs
@@ -82,10 +82,7 @@ namespace Org.BouncyCastle.Asn1
 
         protected override void Dispose(bool disposing)
         {
-            if (disposing)
-            {
-                tmpBuffers = null;
-            }
+            tmpBuffers = null;
 
             if (m_leaveOpen)
             {
diff --git a/crypto/src/asn1/x509/TBSCertList.cs b/crypto/src/asn1/x509/TBSCertList.cs
index 2b288850f..009773e79 100644
--- a/crypto/src/asn1/x509/TBSCertList.cs
+++ b/crypto/src/asn1/x509/TBSCertList.cs
@@ -97,7 +97,7 @@ namespace Org.BouncyCastle.Asn1.X509
 				return new RevokedCertificatesEnumerator(en.GetEnumerator());
 			}
 
-			private class RevokedCertificatesEnumerator
+			private sealed class RevokedCertificatesEnumerator
 				: IEnumerator<CrlEntry>
 			{
 				private readonly IEnumerator<Asn1Encodable> e;
@@ -107,11 +107,13 @@ namespace Org.BouncyCastle.Asn1.X509
 					this.e = e;
 				}
 
-				public virtual void Dispose()
+				public void Dispose()
 				{
-				}
+					e.Dispose();
+                    GC.SuppressFinalize(this);
+                }
 
-				public bool MoveNext()
+                public bool MoveNext()
 				{
 					return e.MoveNext();
 				}
diff --git a/crypto/src/cms/CMSContentInfoParser.cs b/crypto/src/cms/CMSContentInfoParser.cs
index 32b0d6359..cacf3b4fb 100644
--- a/crypto/src/cms/CMSContentInfoParser.cs
+++ b/crypto/src/cms/CMSContentInfoParser.cs
@@ -47,7 +47,7 @@ namespace Org.BouncyCastle.Cms
 
 		public void Dispose()
 		{
-			Dispose(true);
+			Dispose(disposing: true);
 			GC.SuppressFinalize(this);
 		}
 
diff --git a/crypto/src/crypto/prng/CryptoApiRandomGenerator.cs b/crypto/src/crypto/prng/CryptoApiRandomGenerator.cs
index bafb4fd5e..ed894811b 100644
--- a/crypto/src/crypto/prng/CryptoApiRandomGenerator.cs
+++ b/crypto/src/crypto/prng/CryptoApiRandomGenerator.cs
@@ -83,6 +83,7 @@ namespace Org.BouncyCastle.Crypto.Prng
         public void Dispose()
         {
             m_randomNumberGenerator.Dispose();
+            GC.SuppressFinalize(this);
         }
 
         #endregion
diff --git a/crypto/src/pqc/crypto/bike/BikeKemGenerator.cs b/crypto/src/pqc/crypto/bike/BikeKemGenerator.cs
index 280bb6474..51efbd67d 100644
--- a/crypto/src/pqc/crypto/bike/BikeKemGenerator.cs
+++ b/crypto/src/pqc/crypto/bike/BikeKemGenerator.cs
@@ -31,7 +31,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Bike
             return new SecretWithEncapsulationImpl(Arrays.CopyOfRange(K, 0, parameters.DefaultKeySize / 8), c01);
         }
 
-        private class SecretWithEncapsulationImpl
+        private sealed class SecretWithEncapsulationImpl
             : ISecretWithEncapsulation
         {
             private volatile bool hasBeenDestroyed = false;
@@ -62,10 +62,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Bike
             {
                 if (!hasBeenDestroyed)
                 {
-                    hasBeenDestroyed = true;
                     Arrays.Clear(sessionKey);
                     Arrays.Clear(cipher_text);
+                    hasBeenDestroyed = true;
                 }
+                GC.SuppressFinalize(this);
             }
 
             public bool IsDestroyed()
diff --git a/crypto/src/pqc/crypto/crystals/kyber/KyberKEMGenerator.cs b/crypto/src/pqc/crypto/crystals/kyber/KyberKEMGenerator.cs
index 394890838..b8667162a 100644
--- a/crypto/src/pqc/crypto/crystals/kyber/KyberKEMGenerator.cs
+++ b/crypto/src/pqc/crypto/crystals/kyber/KyberKEMGenerator.cs
@@ -60,10 +60,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Crystals.Kyber
             {
                 if (!m_hasBeenDestroyed)
                 {
-                    m_hasBeenDestroyed = true;
                     Arrays.Clear(m_sessionKey);
                     Arrays.Clear(m_cipherText);
+                    m_hasBeenDestroyed = true;
                 }
+                GC.SuppressFinalize(this);
             }
 
             internal bool IsDestroyed()
diff --git a/crypto/src/pqc/crypto/hqc/HqcKemGenerator.cs b/crypto/src/pqc/crypto/hqc/HqcKemGenerator.cs
index 53f59be16..aa5718d0f 100644
--- a/crypto/src/pqc/crypto/hqc/HqcKemGenerator.cs
+++ b/crypto/src/pqc/crypto/hqc/HqcKemGenerator.cs
@@ -35,7 +35,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Hqc
             return new SecretWithEncapsulationImpl(K, cipherText);
         }
 
-        private class SecretWithEncapsulationImpl : ISecretWithEncapsulation
+        private sealed class SecretWithEncapsulationImpl : ISecretWithEncapsulation
         {
             private volatile bool hasBeenDestroyed;
 
@@ -65,10 +65,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Hqc
             {
                 if (!hasBeenDestroyed)
                 {
-                    hasBeenDestroyed = true;
                     Arrays.Clear(sessionKey);
                     Arrays.Clear(cipher_text);
+                    hasBeenDestroyed = true;
                 }
+                GC.SuppressFinalize(this);
             }
 
             public bool IsDestroyed()
diff --git a/crypto/src/pqc/crypto/ntru/NtruEncapsulation.cs b/crypto/src/pqc/crypto/ntru/NtruEncapsulation.cs
index b00fbef31..1cf12a096 100644
--- a/crypto/src/pqc/crypto/ntru/NtruEncapsulation.cs
+++ b/crypto/src/pqc/crypto/ntru/NtruEncapsulation.cs
@@ -6,7 +6,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Ntru
     /// <summary>
     /// Encapsulated secret encapsulated by NTRU.
     /// </summary>
-    internal class NtruEncapsulation : ISecretWithEncapsulation
+    internal sealed class NtruEncapsulation : ISecretWithEncapsulation
     {
         private readonly byte[] _sharedKey;
         private readonly byte[] _ciphertext;
@@ -27,6 +27,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Ntru
                 Array.Clear(_ciphertext, 0, _ciphertext.Length);
                 _hasBeenDestroyed = true;
             }
+            GC.SuppressFinalize(this);
         }
 
         public byte[] GetSecret()
diff --git a/crypto/src/pqc/crypto/ntruprime/NtruLPRimeKemGenerator.cs b/crypto/src/pqc/crypto/ntruprime/NtruLPRimeKemGenerator.cs
index d7c52e357..fc43f31d8 100644
--- a/crypto/src/pqc/crypto/ntruprime/NtruLPRimeKemGenerator.cs
+++ b/crypto/src/pqc/crypto/ntruprime/NtruLPRimeKemGenerator.cs
@@ -24,6 +24,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.NtruPrime
             return new NtruLPRimeKemGenerator.SecretWithEncapsulationImpl(sessionKey, cipherText);
         }
 
+        // TODO[api] private sealed
         public class SecretWithEncapsulationImpl : ISecretWithEncapsulation
         {
             private volatile bool hasBeenDestroyed = false;
@@ -50,14 +51,23 @@ namespace Org.BouncyCastle.Pqc.Crypto.NtruPrime
 
             public void Dispose()
             {
-                if (!hasBeenDestroyed)
+                Dispose(disposing: true);
+                GC.SuppressFinalize(this);
+            }
+
+            protected virtual void Dispose(bool disposing)
+            {
+                if (disposing)
                 {
-                    hasBeenDestroyed = true;
-                    Arrays.Clear(sessionKey);
-                    Arrays.Clear(cipherText);
+                    if (!hasBeenDestroyed)
+                    {
+                        Arrays.Clear(sessionKey);
+                        Arrays.Clear(cipherText);
+                        hasBeenDestroyed = true;
+                    }
                 }
             }
-            
+
             public bool IsDestroyed()
             {
                 return hasBeenDestroyed;
diff --git a/crypto/src/pqc/crypto/ntruprime/SNtruPrimeKemGenerator.cs b/crypto/src/pqc/crypto/ntruprime/SNtruPrimeKemGenerator.cs
index 43ca38b09..b7f777623 100644
--- a/crypto/src/pqc/crypto/ntruprime/SNtruPrimeKemGenerator.cs
+++ b/crypto/src/pqc/crypto/ntruprime/SNtruPrimeKemGenerator.cs
@@ -24,6 +24,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.NtruPrime
             return new NtruLPRimeKemGenerator.SecretWithEncapsulationImpl(sessionKey, cipherText);
         }
 
+        // TODO[api] private sealed
         public class SecretWithEncapsulationImpl : ISecretWithEncapsulation
         {
             private volatile bool hasBeenDestroyed = false;
@@ -50,14 +51,23 @@ namespace Org.BouncyCastle.Pqc.Crypto.NtruPrime
 
             public void Dispose()
             {
-                if (!hasBeenDestroyed)
+                Dispose(disposing: true);
+                GC.SuppressFinalize(this);
+            }
+
+            protected virtual void Dispose(bool disposing)
+            {
+                if (disposing)
                 {
-                    hasBeenDestroyed = true;
-                    Arrays.Clear(sessionKey);
-                    Arrays.Clear(cipherText);
+                    if (!hasBeenDestroyed)
+                    {
+                        Arrays.Clear(sessionKey);
+                        Arrays.Clear(cipherText);
+                        hasBeenDestroyed = true;
+                    }
                 }
             }
-            
+
             public bool IsDestroyed()
             {
                 return hasBeenDestroyed;
diff --git a/crypto/src/pqc/crypto/utils/SecretWithEncapsulationImpl.cs b/crypto/src/pqc/crypto/utils/SecretWithEncapsulationImpl.cs
index c4d3eb44f..5c41b36cd 100644
--- a/crypto/src/pqc/crypto/utils/SecretWithEncapsulationImpl.cs
+++ b/crypto/src/pqc/crypto/utils/SecretWithEncapsulationImpl.cs
@@ -35,11 +35,20 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities
 
         public void Dispose()
         {
-            if (!hasBeenDestroyed)
+            Dispose(disposing: true);
+            GC.SuppressFinalize(this);
+        }
+
+        protected virtual void Dispose(bool disposing)
+        {
+            if (disposing)
             {
-                hasBeenDestroyed = true;
-                Arrays.Clear(sessionKey);
-                Arrays.Clear(cipher_text);
+                if (!hasBeenDestroyed)
+                {
+                    Arrays.Clear(sessionKey);
+                    Arrays.Clear(cipher_text);
+                    hasBeenDestroyed = true;
+                }
             }
         }
 
diff --git a/crypto/src/util/io/pem/PemReader.cs b/crypto/src/util/io/pem/PemReader.cs
index 6e27a3749..a10aaa257 100644
--- a/crypto/src/util/io/pem/PemReader.cs
+++ b/crypto/src/util/io/pem/PemReader.cs
@@ -28,7 +28,7 @@ namespace Org.BouncyCastle.Utilities.IO.Pem
 
         public void Dispose()
         {
-            Dispose(true);
+            Dispose(disposing: true);
             GC.SuppressFinalize(this);
         }
 
diff --git a/crypto/src/util/io/pem/PemWriter.cs b/crypto/src/util/io/pem/PemWriter.cs
index ce7b38821..179f1c127 100644
--- a/crypto/src/util/io/pem/PemWriter.cs
+++ b/crypto/src/util/io/pem/PemWriter.cs
@@ -32,7 +32,7 @@ namespace Org.BouncyCastle.Utilities.IO.Pem
 
         public void Dispose()
         {
-            Dispose(true);
+            Dispose(disposing: true);
             GC.SuppressFinalize(this);
         }