diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-03-07 16:03:07 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-03-07 16:03:07 +0700 |
commit | e47f41dbe4e61cb190154693b72bc99eb6ca6c3e (patch) | |
tree | d92fd90fc444d6141745c8ae0d60838f92502e06 | |
parent | Add BasicGcmMultiplier.IsHardwareAccelerated (diff) | |
download | BouncyCastle.NET-ed25519-e47f41dbe4e61cb190154693b72bc99eb6ca6c3e.tar.xz |
Remove lazy ASN.1 behaviour
-rw-r--r-- | crypto/src/asn1/Asn1InputStream.cs | 29 | ||||
-rw-r--r-- | crypto/src/asn1/Asn1Sequence.cs | 7 | ||||
-rw-r--r-- | crypto/src/asn1/Asn1Set.cs | 7 | ||||
-rw-r--r-- | crypto/src/asn1/ConstructedLazyDLEncoding.cs | 31 | ||||
-rw-r--r-- | crypto/src/asn1/LazyASN1InputStream.cs | 29 | ||||
-rw-r--r-- | crypto/src/asn1/LazyDLEnumerator.cs | 65 | ||||
-rw-r--r-- | crypto/src/asn1/LazyDLSequence.cs | 128 | ||||
-rw-r--r-- | crypto/src/asn1/LazyDLSet.cs | 127 | ||||
-rw-r--r-- | crypto/src/x509/X509CrlParser.cs | 17 |
9 files changed, 19 insertions, 421 deletions
diff --git a/crypto/src/asn1/Asn1InputStream.cs b/crypto/src/asn1/Asn1InputStream.cs index a972460c4..bd93d4758 100644 --- a/crypto/src/asn1/Asn1InputStream.cs +++ b/crypto/src/asn1/Asn1InputStream.cs @@ -29,7 +29,7 @@ namespace Org.BouncyCastle.Asn1 return limited.Limit; if (input is Asn1InputStream asn1) - return asn1.Limit; + return asn1.limit; if (input is MemoryStream memory) return Convert.ToInt32(memory.Length - memory.Position); @@ -123,9 +123,9 @@ namespace Org.BouncyCastle.Asn1 case Asn1Tags.OctetString: return BuildConstructedOctetString(ReadVector(defIn)); case Asn1Tags.Sequence: - return CreateDLSequence(defIn); + return DLSequence.FromVector(ReadVector(defIn)); case Asn1Tags.Set: - return CreateDLSet(defIn); + return DLSet.FromVector(ReadVector(defIn)); case Asn1Tags.External: return DLSequence.FromVector(ReadVector(defIn)).ToAsn1External(); default: @@ -145,7 +145,7 @@ namespace Org.BouncyCastle.Asn1 return Asn1TaggedObject.CreateConstructedDL(tagClass, tagNo, contentsElements); } - internal virtual Asn1EncodableVector ReadVector() + private Asn1EncodableVector ReadVector() { Asn1Object o = ReadObject(); if (null == o) @@ -160,7 +160,7 @@ namespace Org.BouncyCastle.Asn1 return v; } - internal virtual Asn1EncodableVector ReadVector(DefiniteLengthInputStream defIn) + private Asn1EncodableVector ReadVector(DefiniteLengthInputStream defIn) { int remaining = defIn.Remaining; if (remaining < 1) @@ -172,16 +172,6 @@ namespace Org.BouncyCastle.Asn1 } } - internal virtual Asn1Sequence CreateDLSequence(DefiniteLengthInputStream defIn) - { - return DLSequence.FromVector(ReadVector(defIn)); - } - - internal virtual Asn1Set CreateDLSet(DefiniteLengthInputStream defIn) - { - return DLSet.FromVector(ReadVector(defIn)); - } - public Asn1Object ReadObject() { int tagHdr = s.ReadByte(); @@ -239,7 +229,7 @@ namespace Org.BouncyCastle.Asn1 } } - internal virtual DerBitString BuildConstructedBitString(Asn1EncodableVector contentsElements) + private DerBitString BuildConstructedBitString(Asn1EncodableVector contentsElements) { DerBitString[] bitStrings = new DerBitString[contentsElements.Count]; @@ -256,7 +246,7 @@ namespace Org.BouncyCastle.Asn1 return new DLBitString(BerBitString.FlattenBitStrings(bitStrings), false); } - internal virtual Asn1OctetString BuildConstructedOctetString(Asn1EncodableVector contentsElements) + private Asn1OctetString BuildConstructedOctetString(Asn1EncodableVector contentsElements) { Asn1OctetString[] octetStrings = new Asn1OctetString[contentsElements.Count]; @@ -274,11 +264,6 @@ namespace Org.BouncyCastle.Asn1 return new DerOctetString(BerOctetString.FlattenOctetStrings(octetStrings)); } - internal virtual int Limit - { - get { return limit; } - } - internal static int ReadTagNumber(Stream s, int tagHdr) { int tagNo = tagHdr & 0x1f; diff --git a/crypto/src/asn1/Asn1Sequence.cs b/crypto/src/asn1/Asn1Sequence.cs index a8191de99..5b404d248 100644 --- a/crypto/src/asn1/Asn1Sequence.cs +++ b/crypto/src/asn1/Asn1Sequence.cs @@ -76,8 +76,7 @@ namespace Org.BouncyCastle.Asn1 return (Asn1Sequence)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit); } - // NOTE: Only non-readonly to support LazyDLSequence - internal Asn1Encodable[] elements; + internal readonly Asn1Encodable[] elements; protected internal Asn1Sequence() { @@ -145,7 +144,6 @@ namespace Org.BouncyCastle.Asn1 Asn1Sequence outer) { this.outer = outer; - // NOTE: Call Count here to 'force' a LazyDerSequence this.max = outer.Count; } @@ -198,7 +196,6 @@ namespace Org.BouncyCastle.Asn1 public virtual T[] MapElements<T>(Func<Asn1Encodable, T> func) { - // NOTE: Call Count here to 'force' a LazyDerSequence int count = Count; T[] result = new T[count]; for (int i = 0; i < count; ++i) @@ -215,7 +212,6 @@ namespace Org.BouncyCastle.Asn1 protected override int Asn1GetHashCode() { - // NOTE: Call Count here to 'force' a LazyDerSequence int i = Count; int hc = i + 1; @@ -234,7 +230,6 @@ namespace Org.BouncyCastle.Asn1 if (null == that) return false; - // NOTE: Call Count here (on both) to 'force' a LazyDerSequence int count = this.Count; if (that.Count != count) return false; diff --git a/crypto/src/asn1/Asn1Set.cs b/crypto/src/asn1/Asn1Set.cs index 222a6c727..514c8c77c 100644 --- a/crypto/src/asn1/Asn1Set.cs +++ b/crypto/src/asn1/Asn1Set.cs @@ -77,9 +77,8 @@ namespace Org.BouncyCastle.Asn1 return (Asn1Set)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit); } - // NOTE: Only non-readonly to support LazyDLSet - internal Asn1Encodable[] m_elements; internal Asn1Encodable[] m_sortedElements; + internal readonly Asn1Encodable[] m_elements; protected internal Asn1Set() { @@ -167,7 +166,6 @@ namespace Org.BouncyCastle.Asn1 public virtual T[] MapElements<T>(Func<Asn1Encodable, T> func) { - // NOTE: Call Count here to 'force' a LazyDerSet int count = Count; T[] result = new T[count]; for (int i = 0; i < count; ++i) @@ -193,7 +191,6 @@ namespace Org.BouncyCastle.Asn1 Asn1Set outer) { this.outer = outer; - // NOTE: Call Count here to 'force' a LazyDerSet this.max = outer.Count; } @@ -229,7 +226,6 @@ namespace Org.BouncyCastle.Asn1 protected override int Asn1GetHashCode() { - // NOTE: Call Count here to 'force' a LazyDerSet int i = Count; int hc = i + 1; @@ -247,7 +243,6 @@ namespace Org.BouncyCastle.Asn1 if (!(asn1Object is Asn1Set that)) return false; - // NOTE: Call Count here (on both) to 'force' a LazyDerSet int count = this.Count; if (that.Count != count) return false; diff --git a/crypto/src/asn1/ConstructedLazyDLEncoding.cs b/crypto/src/asn1/ConstructedLazyDLEncoding.cs deleted file mode 100644 index ee9d21c66..000000000 --- a/crypto/src/asn1/ConstructedLazyDLEncoding.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; - -namespace Org.BouncyCastle.Asn1 -{ - internal class ConstructedLazyDLEncoding - : IAsn1Encoding - { - private readonly int m_tagClass; - private readonly int m_tagNo; - private readonly byte[] m_contentsOctets; - - internal ConstructedLazyDLEncoding(int tagClass, int tagNo, byte[] contentsOctets) - { - m_tagClass = tagClass; - m_tagNo = tagNo; - m_contentsOctets = contentsOctets; - } - - void IAsn1Encoding.Encode(Asn1OutputStream asn1Out) - { - asn1Out.WriteIdentifier(Asn1Tags.Constructed | m_tagClass, m_tagNo); - asn1Out.WriteDL(m_contentsOctets.Length); - asn1Out.Write(m_contentsOctets, 0, m_contentsOctets.Length); - } - - int IAsn1Encoding.GetLength() - { - return Asn1OutputStream.GetLengthOfEncodingDL(m_tagNo, m_contentsOctets.Length); - } - } -} diff --git a/crypto/src/asn1/LazyASN1InputStream.cs b/crypto/src/asn1/LazyASN1InputStream.cs index 8807416f4..7935bc85a 100644 --- a/crypto/src/asn1/LazyASN1InputStream.cs +++ b/crypto/src/asn1/LazyASN1InputStream.cs @@ -3,6 +3,8 @@ using System.IO; namespace Org.BouncyCastle.Asn1 { + /// <remarks>No longer provides any laziness.</remarks> + [Obsolete("Will be removed")] public class LazyAsn1InputStream : Asn1InputStream { @@ -25,32 +27,5 @@ namespace Org.BouncyCastle.Asn1 : base(input, limit, leaveOpen) { } - - internal LazyAsn1InputStream(Stream input, int limit, bool leaveOpen, byte[][] tmpBuffers) - : base(input, limit, leaveOpen, tmpBuffers) - { - } - - internal override Asn1Sequence CreateDLSequence(DefiniteLengthInputStream defIn) - { - return new LazyDLSequence(defIn.ToArray()); - } - - internal override Asn1Set CreateDLSet(DefiniteLengthInputStream defIn) - { - return new LazyDLSet(defIn.ToArray()); - } - - internal override Asn1EncodableVector ReadVector(DefiniteLengthInputStream defIn) - { - int remaining = defIn.Remaining; - if (remaining < 1) - return new Asn1EncodableVector(0); - - using (var sub = new LazyAsn1InputStream(defIn, remaining, leaveOpen: true, tmpBuffers)) - { - return sub.ReadVector(); - } - } } } diff --git a/crypto/src/asn1/LazyDLEnumerator.cs b/crypto/src/asn1/LazyDLEnumerator.cs deleted file mode 100644 index b71cb49e9..000000000 --- a/crypto/src/asn1/LazyDLEnumerator.cs +++ /dev/null @@ -1,65 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; - -namespace Org.BouncyCastle.Asn1 -{ - internal class LazyDLEnumerator - : IEnumerator<Asn1Encodable> - { - private readonly byte[] m_contents; - - private Asn1InputStream m_input; - private Asn1Object m_current; - - internal LazyDLEnumerator(byte[] contents) - { - this.m_contents = contents; - - Reset(); - } - - object System.Collections.IEnumerator.Current - { - get { return Current; } - } - - public Asn1Encodable Current - { - get - { - if (null == m_current) - throw new InvalidOperationException(); - - return m_current; - } - } - - public virtual void Dispose() - { - } - - public bool MoveNext() - { - return null != (this.m_current = ReadObject()); - } - - public void Reset() - { - this.m_input = new LazyAsn1InputStream(m_contents); - this.m_current = null; - } - - private Asn1Object ReadObject() - { - try - { - return m_input.ReadObject(); - } - catch (IOException e) - { - throw new Asn1ParsingException("malformed ASN.1: " + e.Message, e); - } - } - } -} diff --git a/crypto/src/asn1/LazyDLSequence.cs b/crypto/src/asn1/LazyDLSequence.cs deleted file mode 100644 index a9f155359..000000000 --- a/crypto/src/asn1/LazyDLSequence.cs +++ /dev/null @@ -1,128 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; - -namespace Org.BouncyCastle.Asn1 -{ - internal class LazyDLSequence - : DLSequence - { - private byte[] encoded; - - internal LazyDLSequence(byte[] encoded) - : base() - { - if (null == encoded) - throw new ArgumentNullException("encoded"); - - this.encoded = encoded; - } - - public override Asn1Encodable this[int index] - { - get - { - Force(); - - return base[index]; - } - } - - public override IEnumerator<Asn1Encodable> GetEnumerator() - { - byte[] encoded = GetContents(); - if (null != encoded) - { - return new LazyDLEnumerator(encoded); - } - - return base.GetEnumerator(); - } - - public override int Count - { - get - { - Force(); - - return base.Count; - } - } - - public override Asn1Encodable[] ToArray() - { - Force(); - - return base.ToArray(); - } - - public override string ToString() - { - Force(); - - return base.ToString(); - } - - internal override IAsn1Encoding GetEncoding(int encoding) - { - if (Asn1OutputStream.EncodingBer == encoding) - { - byte[] encoded = GetContents(); - if (encoded != null) - return new ConstructedLazyDLEncoding(Asn1Tags.Universal, Asn1Tags.Sequence, encoded); - } - else - { - Force(); - } - - return base.GetEncoding(encoding); - } - - internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo) - { - if (Asn1OutputStream.EncodingBer == encoding) - { - byte[] encoded = GetContents(); - if (encoded != null) - return new ConstructedLazyDLEncoding(tagClass, tagNo, encoded); - } - else - { - Force(); - } - - return base.GetEncodingImplicit(encoding, tagClass, tagNo); - } - - private void Force() - { - lock (this) - { - if (null != encoded) - { - try - { - Asn1EncodableVector v; - using (var input = new LazyAsn1InputStream(encoded)) - { - v = input.ReadVector(); - } - - this.elements = v.TakeElements(); - this.encoded = null; - } - catch (IOException e) - { - throw new Asn1ParsingException("malformed ASN.1: " + e.Message, e); - } - } - } - } - - private byte[] GetContents() - { - lock (this) return encoded; - } - } -} diff --git a/crypto/src/asn1/LazyDLSet.cs b/crypto/src/asn1/LazyDLSet.cs deleted file mode 100644 index 87354d394..000000000 --- a/crypto/src/asn1/LazyDLSet.cs +++ /dev/null @@ -1,127 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; - -namespace Org.BouncyCastle.Asn1 -{ - internal class LazyDLSet - : DLSet - { - private byte[] m_encoded; - - internal LazyDLSet(byte[] encoded) - : base() - { - if (null == encoded) - throw new ArgumentNullException(nameof(encoded)); - - m_encoded = encoded; - } - - public override Asn1Encodable this[int index] - { - get - { - Force(); - - return base[index]; - } - } - - public override IEnumerator<Asn1Encodable> GetEnumerator() - { - byte[] encoded = GetContents(); - if (null != encoded) - return new LazyDLEnumerator(encoded); - - return base.GetEnumerator(); - } - - public override int Count - { - get - { - Force(); - - return base.Count; - } - } - - public override Asn1Encodable[] ToArray() - { - Force(); - - return base.ToArray(); - } - - public override string ToString() - { - Force(); - - return base.ToString(); - } - - internal override IAsn1Encoding GetEncoding(int encoding) - { - if (Asn1OutputStream.EncodingBer == encoding) - { - byte[] encoded = GetContents(); - if (null != encoded) - return new ConstructedLazyDLEncoding(Asn1Tags.Universal, Asn1Tags.Set, encoded); - } - else - { - Force(); - } - - return base.GetEncoding(encoding); - } - - internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo) - { - if (Asn1OutputStream.EncodingBer == encoding) - { - byte[] encoded = GetContents(); - if (null != encoded) - return new ConstructedLazyDLEncoding(tagClass, tagNo, encoded); - } - else - { - Force(); - } - - return base.GetEncodingImplicit(encoding, tagClass, tagNo); - } - - private void Force() - { - lock (this) - { - if (null != m_encoded) - { - try - { - 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; - m_encoded = null; - } - catch (IOException e) - { - throw new Asn1ParsingException("malformed ASN.1: " + e.Message, e); - } - } - } - } - - private byte[] GetContents() - { - lock (this) return m_encoded; - } - } -} diff --git a/crypto/src/x509/X509CrlParser.cs b/crypto/src/x509/X509CrlParser.cs index a60a591c4..53a02393b 100644 --- a/crypto/src/x509/X509CrlParser.cs +++ b/crypto/src/x509/X509CrlParser.cs @@ -14,15 +14,18 @@ namespace Org.BouncyCastle.X509 { private static readonly PemParser PemCrlParser = new PemParser("CRL"); - private readonly bool lazyAsn1; - private Asn1Set sCrlData; private int sCrlDataObjectCount; private Stream currentCrlStream; - public X509CrlParser(bool lazyAsn1 = false) + public X509CrlParser() + { + } + + /// <remarks>Value of <paramref name="lazyAsn1"/> is ignored.</remarks> + [Obsolete("Will be removed")] + public X509CrlParser(bool lazyAsn1) { - this.lazyAsn1 = lazyAsn1; } private X509Crl ReadDerCrl(Asn1InputStream dIn) @@ -130,11 +133,7 @@ namespace Org.BouncyCastle.X509 if (tag != 0x30) // assume ascii PEM encoded. return ReadPemCrl(inStream); - Asn1InputStream asn1 = lazyAsn1 - ? new LazyAsn1InputStream(inStream, int.MaxValue, leaveOpen: true) - : new Asn1InputStream(inStream, int.MaxValue, leaveOpen: true); - - using (asn1) + using (var asn1 = new Asn1InputStream(inStream, int.MaxValue, leaveOpen: true)) { return ReadDerCrl(asn1); } |