diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-03-06 19:43:25 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-03-06 19:43:25 +0700 |
commit | f93624749322b60eb96c80d466ea234a1a46d4c4 (patch) | |
tree | 780a228fa70aa25c44842ee5d20538f453a620f2 | |
parent | Avoid recoding issuer name (diff) | |
download | BouncyCastle.NET-ed25519-f93624749322b60eb96c80d466ea234a1a46d4c4.tar.xz |
Refactoring in Asn1
-rw-r--r-- | crypto/src/asn1/Asn1Object.cs | 7 | ||||
-rw-r--r-- | crypto/src/asn1/Asn1OutputStream.cs | 4 | ||||
-rw-r--r-- | crypto/src/asn1/LazyDLSequence.cs | 7 | ||||
-rw-r--r-- | crypto/src/asn1/LazyDLSet.cs | 7 | ||||
-rw-r--r-- | crypto/src/pkix/PkixCertPath.cs | 24 | ||||
-rw-r--r-- | crypto/src/tls/TlsUtilities.cs | 2 |
6 files changed, 31 insertions, 20 deletions
diff --git a/crypto/src/asn1/Asn1Object.cs b/crypto/src/asn1/Asn1Object.cs index 523476f3d..d84350cd2 100644 --- a/crypto/src/asn1/Asn1Object.cs +++ b/crypto/src/asn1/Asn1Object.cs @@ -31,7 +31,7 @@ namespace Org.BouncyCastle.Asn1 using (var asn1Out = Asn1OutputStream.Create(new MemoryStream(result, true), encoding)) { asn1Encoding.Encode(asn1Out); - Debug.Assert(result.Length == Convert.ToInt32(asn1Out.Position)); + Debug.Assert(result.Length == asn1Out.Position); } return result; } @@ -73,7 +73,10 @@ namespace Org.BouncyCastle.Asn1 { try { - return new Asn1InputStream(inStr).ReadObject(); + using (var asn1In = new Asn1InputStream(inStr, int.MaxValue, leaveOpen: true)) + { + return asn1In.ReadObject(); + } } catch (InvalidCastException) { diff --git a/crypto/src/asn1/Asn1OutputStream.cs b/crypto/src/asn1/Asn1OutputStream.cs index 588c3733d..4770235d3 100644 --- a/crypto/src/asn1/Asn1OutputStream.cs +++ b/crypto/src/asn1/Asn1OutputStream.cs @@ -44,7 +44,7 @@ namespace Org.BouncyCastle.Asn1 private readonly bool m_leaveOpen; - internal Asn1OutputStream(Stream output, bool leaveOpen) + protected internal Asn1OutputStream(Stream output, bool leaveOpen) : base(output) { if (!output.CanWrite) @@ -101,7 +101,7 @@ namespace Org.BouncyCastle.Asn1 get { return EncodingBer; } } - internal void FlushInternal() + private void FlushInternal() { // Placeholder to support future internal buffering } diff --git a/crypto/src/asn1/LazyDLSequence.cs b/crypto/src/asn1/LazyDLSequence.cs index d45706bb3..a9f155359 100644 --- a/crypto/src/asn1/LazyDLSequence.cs +++ b/crypto/src/asn1/LazyDLSequence.cs @@ -101,10 +101,13 @@ namespace Org.BouncyCastle.Asn1 { if (null != encoded) { - Asn1InputStream input = new LazyAsn1InputStream(encoded); try { - Asn1EncodableVector v = input.ReadVector(); + Asn1EncodableVector v; + using (var input = new LazyAsn1InputStream(encoded)) + { + v = input.ReadVector(); + } this.elements = v.TakeElements(); this.encoded = null; diff --git a/crypto/src/asn1/LazyDLSet.cs b/crypto/src/asn1/LazyDLSet.cs index 7386e72ba..87354d394 100644 --- a/crypto/src/asn1/LazyDLSet.cs +++ b/crypto/src/asn1/LazyDLSet.cs @@ -99,10 +99,13 @@ namespace Org.BouncyCastle.Asn1 { if (null != m_encoded) { - Asn1InputStream input = new LazyAsn1InputStream(m_encoded); try { - Asn1EncodableVector v = input.ReadVector(); + Asn1EncodableVector v; + using (var input = new LazyAsn1InputStream(m_encoded)) + { + v = input.ReadVector(); + } m_elements = v.TakeElements(); m_sortedElements = m_elements.Length <= 1 ? m_elements : null; diff --git a/crypto/src/pkix/PkixCertPath.cs b/crypto/src/pkix/PkixCertPath.cs index a2ea3074d..950b999b2 100644 --- a/crypto/src/pkix/PkixCertPath.cs +++ b/crypto/src/pkix/PkixCertPath.cs @@ -191,20 +191,22 @@ namespace Org.BouncyCastle.Pkix { if (Platform.EqualsIgnoreCase("PkiPath", encoding)) { - Asn1InputStream derInStream = new Asn1InputStream(inStream); - if (!(derInStream.ReadObject() is Asn1Sequence asn1Sequence)) - { - throw new CertificateException( - "input stream does not contain a ASN1 SEQUENCE while reading PkiPath encoded data to load CertPath"); - } + using (var asn1In = new Asn1InputStream(inStream, int.MaxValue, leaveOpen: true)) + { + if (!(asn1In.ReadObject() is Asn1Sequence asn1Sequence)) + { + throw new CertificateException( + "input stream does not contain a ASN1 SEQUENCE while reading PkiPath encoded data to load CertPath"); + } - var certArray = asn1Sequence.MapElements( - element => new X509Certificate(X509CertificateStructure.GetInstance(element.ToAsn1Object()))); + var certArray = asn1Sequence.MapElements( + element => new X509Certificate(X509CertificateStructure.GetInstance(element.ToAsn1Object()))); - Array.Reverse(certArray); + Array.Reverse(certArray); - certs = new List<X509Certificate>(certArray); - } + certs = new List<X509Certificate>(certArray); + } + } else if (Platform.EqualsIgnoreCase("PEM", encoding) || Platform.EqualsIgnoreCase("PKCS7", encoding)) { diff --git a/crypto/src/tls/TlsUtilities.cs b/crypto/src/tls/TlsUtilities.cs index d187aeed2..0d2a971a0 100644 --- a/crypto/src/tls/TlsUtilities.cs +++ b/crypto/src/tls/TlsUtilities.cs @@ -971,7 +971,7 @@ namespace Org.BouncyCastle.Tls Asn1Object result = asn1In.ReadObject(); if (null == result) throw new TlsFatalAlert(AlertDescription.decode_error); - if (asn1In.Length != asn1In.Position) + if (encoding.Length != asn1In.Position) throw new TlsFatalAlert(AlertDescription.decode_error); return result; } |