diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2021-11-06 15:21:31 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2021-11-06 15:21:31 +0700 |
commit | 7c465b786d77b85e2d12d6f90d8c10468ca3e5c2 (patch) | |
tree | 21fc3b10f009b58ce6ec0b52304f389408933c51 /crypto | |
parent | Add null-safe GetHashCode (diff) | |
download | BouncyCastle.NET-ed25519-7c465b786d77b85e2d12d6f90d8c10468ca3e5c2.tar.xz |
ASN.1 tagged objects
- guard against null base objects - Obsolete IsEmpty
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/src/asn1/Asn1TaggedObject.cs | 59 | ||||
-rw-r--r-- | crypto/src/asn1/BerTaggedObject.cs | 53 | ||||
-rw-r--r-- | crypto/src/asn1/DerTaggedObject.cs | 34 | ||||
-rw-r--r-- | crypto/src/asn1/util/Asn1Dump.cs | 11 |
4 files changed, 61 insertions, 96 deletions
diff --git a/crypto/src/asn1/Asn1TaggedObject.cs b/crypto/src/asn1/Asn1TaggedObject.cs index 14b1bc65f..42c6db381 100644 --- a/crypto/src/asn1/Asn1TaggedObject.cs +++ b/crypto/src/asn1/Asn1TaggedObject.cs @@ -22,10 +22,9 @@ namespace Org.BouncyCastle.Asn1 return IsConstructed(tagged.IsExplicit(), tagged.GetObject()); } - internal int tagNo; -// internal bool empty; - internal bool explicitly = true; - internal Asn1Encodable obj; + internal readonly int tagNo; + internal readonly bool explicitly; + internal readonly Asn1Encodable obj; static public Asn1TaggedObject GetInstance( Asn1TaggedObject obj, @@ -54,13 +53,9 @@ namespace Org.BouncyCastle.Asn1 * @param tagNo the tag number for this object. * @param obj the tagged object. */ - protected Asn1TaggedObject( - int tagNo, - Asn1Encodable obj) + protected Asn1TaggedObject(int tagNo, Asn1Encodable obj) + : this(true, tagNo, obj) { - this.explicitly = true; - this.tagNo = tagNo; - this.obj = obj; } /** @@ -68,12 +63,12 @@ namespace Org.BouncyCastle.Asn1 * @param tagNo the tag number for this object. * @param obj the tagged object. */ - protected Asn1TaggedObject( - bool explicitly, - int tagNo, - Asn1Encodable obj) + protected Asn1TaggedObject(bool explicitly, int tagNo, Asn1Encodable obj) { - // IAsn1Choice marker interface 'insists' on explicit tagging + if (null == obj) + throw new ArgumentNullException("obj"); + + // IAsn1Choice marker interface 'insists' on explicit tagging this.explicitly = explicitly || (obj is IAsn1Choice); this.tagNo = tagNo; this.obj = obj; @@ -87,10 +82,9 @@ namespace Org.BouncyCastle.Asn1 if (other == null) return false; - return this.tagNo == other.tagNo -// && this.empty == other.empty - && this.explicitly == other.explicitly // TODO Should this be part of equality? - && Platform.Equals(GetObject(), other.GetObject()); + return this.tagNo == other.tagNo + && this.explicitly == other.explicitly // TODO Should this be part of equality? + && this.GetObject().Equals(other.GetObject()); } protected override int Asn1GetHashCode() @@ -104,10 +98,7 @@ namespace Org.BouncyCastle.Asn1 // compare the encodings... // code ^= explicitly.GetHashCode(); - if (obj != null) - { - code ^= obj.GetHashCode(); - } + code ^= obj.GetHashCode(); return code; } @@ -131,9 +122,10 @@ namespace Org.BouncyCastle.Asn1 return explicitly; } + [Obsolete("Will be removed. Replace with constant return value of 'false'")] public bool IsEmpty() { - return false; //empty; + return false; } /** @@ -145,12 +137,7 @@ namespace Org.BouncyCastle.Asn1 */ public Asn1Object GetObject() { - if (obj != null) - { - return obj.ToAsn1Object(); - } - - return null; + return obj.ToAsn1Object(); } /** @@ -164,12 +151,12 @@ namespace Org.BouncyCastle.Asn1 { switch (tag) { - case Asn1Tags.Set: - return Asn1Set.GetInstance(this, isExplicit).Parser; - case Asn1Tags.Sequence: - return Asn1Sequence.GetInstance(this, isExplicit).Parser; - case Asn1Tags.OctetString: - return Asn1OctetString.GetInstance(this, isExplicit).Parser; + case Asn1Tags.Set: + return Asn1Set.GetInstance(this, isExplicit).Parser; + case Asn1Tags.Sequence: + return Asn1Sequence.GetInstance(this, isExplicit).Parser; + case Asn1Tags.OctetString: + return Asn1OctetString.GetInstance(this, isExplicit).Parser; } if (isExplicit) diff --git a/crypto/src/asn1/BerTaggedObject.cs b/crypto/src/asn1/BerTaggedObject.cs index 82a65dba7..b41df44ec 100644 --- a/crypto/src/asn1/BerTaggedObject.cs +++ b/crypto/src/asn1/BerTaggedObject.cs @@ -63,46 +63,43 @@ namespace Org.BouncyCastle.Asn1 asn1Out.WriteByte(0x80); - if (!IsEmpty()) + if (!explicitly) { - if (!explicitly) + IEnumerable eObj; + if (obj is Asn1OctetString) { - IEnumerable eObj; - if (obj is Asn1OctetString) + if (obj is BerOctetString) { - if (obj is BerOctetString) - { - eObj = (BerOctetString) obj; - } - else - { - Asn1OctetString octs = (Asn1OctetString)obj; - eObj = new BerOctetString(octs.GetOctets()); - } - } - else if (obj is Asn1Sequence) - { - eObj = (Asn1Sequence) obj; - } - else if (obj is Asn1Set) - { - eObj = (Asn1Set) obj; + eObj = (BerOctetString) obj; } else { - throw Platform.CreateNotImplementedException(Platform.GetTypeName(obj)); - } - - foreach (Asn1Encodable o in eObj) - { - asn1Out.WritePrimitive(o.ToAsn1Object(), true); + Asn1OctetString octs = (Asn1OctetString)obj; + eObj = new BerOctetString(octs.GetOctets()); } } + else if (obj is Asn1Sequence) + { + eObj = (Asn1Sequence) obj; + } + else if (obj is Asn1Set) + { + eObj = (Asn1Set) obj; + } else { - asn1Out.WritePrimitive(obj.ToAsn1Object(), true); + throw Platform.CreateNotImplementedException(Platform.GetTypeName(obj)); + } + + foreach (Asn1Encodable o in eObj) + { + asn1Out.WritePrimitive(o.ToAsn1Object(), true); } } + else + { + asn1Out.WritePrimitive(obj.ToAsn1Object(), true); + } asn1Out.WriteByte(0x00); asn1Out.WriteByte(0x00); diff --git a/crypto/src/asn1/DerTaggedObject.cs b/crypto/src/asn1/DerTaggedObject.cs index 8a001428b..5ef4310e6 100644 --- a/crypto/src/asn1/DerTaggedObject.cs +++ b/crypto/src/asn1/DerTaggedObject.cs @@ -53,32 +53,22 @@ namespace Org.BouncyCastle.Asn1 internal override void Encode(Asn1OutputStream asn1Out, bool withID) { - if (!IsEmpty()) - { - byte[] bytes = obj.GetDerEncoded(); - - if (explicitly) - { - asn1Out.WriteEncodingDL(withID, Asn1Tags.Constructed | Asn1Tags.ContextSpecific, tagNo, bytes); - } - else - { - // - // need to mark constructed types... (preserve Constructed tag) - // - if (withID) - { - int flags = (bytes[0] & Asn1Tags.Constructed) | Asn1Tags.ContextSpecific; - asn1Out.WriteIdentifier(true, flags, tagNo); - } + byte[] bytes = obj.GetDerEncoded(); - asn1Out.Write(bytes, 1, bytes.Length - 1); - } + if (explicitly) + { + asn1Out.WriteEncodingDL(withID, Asn1Tags.Constructed | Asn1Tags.ContextSpecific, tagNo, bytes); } else { - asn1Out.WriteEncodingDL(withID, Asn1Tags.Constructed | Asn1Tags.ContextSpecific, tagNo, - Asn1OctetString.EmptyOctets); + if (withID) + { + // need to mark constructed types... (preserve Constructed tag) + int flags = (bytes[0] & Asn1Tags.Constructed) | Asn1Tags.ContextSpecific; + asn1Out.WriteIdentifier(true, flags, tagNo); + } + + asn1Out.Write(bytes, 1, bytes.Length - 1); } } } diff --git a/crypto/src/asn1/util/Asn1Dump.cs b/crypto/src/asn1/util/Asn1Dump.cs index 511bc30af..c82abd5c6 100644 --- a/crypto/src/asn1/util/Asn1Dump.cs +++ b/crypto/src/asn1/util/Asn1Dump.cs @@ -110,16 +110,7 @@ namespace Org.BouncyCastle.Asn1.Utilities buf.Append(NewLine); - if (o.IsEmpty()) - { - buf.Append(tab); - buf.Append("EMPTY"); - buf.Append(NewLine); - } - else - { - AsString(tab, verbose, o.GetObject(), buf); - } + AsString(tab, verbose, o.GetObject(), buf); } else if (obj is DerObjectIdentifier) { |