diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2024-01-02 17:19:19 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2024-01-02 17:19:19 +0700 |
commit | 165f6ce1f276080e1a871cf7eba2f41b381abb3c (patch) | |
tree | 9e8b85b81098c4599f68a21da480e546ee27ed3d | |
parent | Update X509NameTests, including some TODOs (diff) | |
download | BouncyCastle.NET-ed25519-165f6ce1f276080e1a871cf7eba2f41b381abb3c.tar.xz |
Refactoring around Asn1TaggedObject
-rw-r--r-- | crypto/src/asn1/Asn1TaggedObject.cs | 2 | ||||
-rw-r--r-- | crypto/src/asn1/Asn1UniversalType.cs | 6 | ||||
-rw-r--r-- | crypto/src/asn1/Asn1Utilities.cs | 14 | ||||
-rw-r--r-- | crypto/src/asn1/BERTaggedObjectParser.cs | 34 | ||||
-rw-r--r-- | crypto/src/asn1/DERExternal.cs | 2 | ||||
-rw-r--r-- | crypto/src/asn1/DLTaggedObjectParser.cs | 45 | ||||
-rw-r--r-- | crypto/src/asn1/cms/Evidence.cs | 2 | ||||
-rw-r--r-- | crypto/src/asn1/crmf/PKIArchiveOptions.cs | 2 | ||||
-rw-r--r-- | crypto/src/asn1/isismtt/x509/DeclarationOfMajority.cs | 2 | ||||
-rw-r--r-- | crypto/src/asn1/x509/X509Extensions.cs | 2 |
10 files changed, 43 insertions, 68 deletions
diff --git a/crypto/src/asn1/Asn1TaggedObject.cs b/crypto/src/asn1/Asn1TaggedObject.cs index a5a5e9506..01d10cbb2 100644 --- a/crypto/src/asn1/Asn1TaggedObject.cs +++ b/crypto/src/asn1/Asn1TaggedObject.cs @@ -231,7 +231,7 @@ namespace Org.BouncyCastle.Asn1 [Obsolete("Will be removed")] public Asn1Object GetObject() { - Asn1Utilities.CheckContextTag(this); + Asn1Utilities.CheckContextTagClass(this); return m_object.ToAsn1Object(); } diff --git a/crypto/src/asn1/Asn1UniversalType.cs b/crypto/src/asn1/Asn1UniversalType.cs index 81f03a540..dedc29333 100644 --- a/crypto/src/asn1/Asn1UniversalType.cs +++ b/crypto/src/asn1/Asn1UniversalType.cs @@ -40,10 +40,8 @@ namespace Org.BouncyCastle.Asn1 return CheckedCast(Asn1Object.FromByteArray(bytes)); } - internal Asn1Object GetContextInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) - { - return CheckedCast(Asn1Utilities.CheckContextTag(taggedObject).GetBaseUniversal(declaredExplicit, this)); - } + internal Asn1Object GetContextInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) => + CheckedCast(Asn1Utilities.CheckContextTagClass(taggedObject).GetBaseUniversal(declaredExplicit, this)); internal Asn1Tag Tag { diff --git a/crypto/src/asn1/Asn1Utilities.cs b/crypto/src/asn1/Asn1Utilities.cs index 4207d132e..eacbf499a 100644 --- a/crypto/src/asn1/Asn1Utilities.cs +++ b/crypto/src/asn1/Asn1Utilities.cs @@ -8,12 +8,22 @@ namespace Org.BouncyCastle.Asn1 // TODO[api] Make static public abstract class Asn1Utilities { - internal static Asn1TaggedObject CheckContextTag(Asn1TaggedObject taggedObject) + internal static Asn1TaggedObject CheckContextTag(Asn1TaggedObject taggedObject, int tagNo) + { + return CheckTag(taggedObject, Asn1Tags.ContextSpecific, tagNo); + } + + internal static Asn1TaggedObjectParser CheckContextTag(Asn1TaggedObjectParser taggedObjectParser, int tagNo) + { + return CheckTag(taggedObjectParser, Asn1Tags.ContextSpecific, tagNo); + } + + internal static Asn1TaggedObject CheckContextTagClass(Asn1TaggedObject taggedObject) { return CheckTagClass(taggedObject, Asn1Tags.ContextSpecific); } - internal static Asn1TaggedObjectParser CheckContextTag(Asn1TaggedObjectParser taggedObjectParser) + internal static Asn1TaggedObjectParser CheckContextTagClass(Asn1TaggedObjectParser taggedObjectParser) { return CheckTagClass(taggedObjectParser, Asn1Tags.ContextSpecific); } diff --git a/crypto/src/asn1/BERTaggedObjectParser.cs b/crypto/src/asn1/BERTaggedObjectParser.cs index b1a8cd896..56e677f28 100644 --- a/crypto/src/asn1/BERTaggedObjectParser.cs +++ b/crypto/src/asn1/BERTaggedObjectParser.cs @@ -1,9 +1,8 @@ -using System; using System.IO; namespace Org.BouncyCastle.Asn1 { - internal class BerTaggedObjectParser + internal class BerTaggedObjectParser : Asn1TaggedObjectParser { internal readonly int m_tagClass; @@ -17,20 +16,11 @@ namespace Org.BouncyCastle.Asn1 m_parser = parser; } - public virtual bool IsConstructed - { - get { return true; } - } + public virtual bool IsConstructed => true; - public int TagClass - { - get { return m_tagClass; } - } + public int TagClass => m_tagClass; - public int TagNo - { - get { return m_tagNo; } - } + public int TagNo => m_tagNo; public bool HasContextTag() { @@ -60,20 +50,12 @@ namespace Org.BouncyCastle.Asn1 return m_parser.ParseImplicitConstructedIL(baseTagNo); } - public virtual IAsn1Convertible ParseExplicitBaseObject() - { - return m_parser.ReadObject(); - } + public virtual IAsn1Convertible ParseExplicitBaseObject() => m_parser.ReadObject(); - public virtual Asn1TaggedObjectParser ParseExplicitBaseTagged() - { - return m_parser.ParseTaggedObject(); - } + public virtual Asn1TaggedObjectParser ParseExplicitBaseTagged() => m_parser.ParseTaggedObject(); - public virtual Asn1TaggedObjectParser ParseImplicitBaseTagged(int baseTagClass, int baseTagNo) - { - return new BerTaggedObjectParser(baseTagClass, baseTagNo, m_parser); - } + public virtual Asn1TaggedObjectParser ParseImplicitBaseTagged(int baseTagClass, int baseTagNo) => + new BerTaggedObjectParser(baseTagClass, baseTagNo, m_parser); public virtual Asn1Object ToAsn1Object() { diff --git a/crypto/src/asn1/DERExternal.cs b/crypto/src/asn1/DERExternal.cs index cfe0ae701..975cd1665 100644 --- a/crypto/src/asn1/DERExternal.cs +++ b/crypto/src/asn1/DERExternal.cs @@ -251,7 +251,7 @@ namespace Org.BouncyCastle.Asn1 private static Asn1Object GetExternalContent(Asn1TaggedObject encoding) { - Asn1Utilities.CheckContextTag(encoding); + Asn1Utilities.CheckContextTagClass(encoding); switch (encoding.TagNo) { diff --git a/crypto/src/asn1/DLTaggedObjectParser.cs b/crypto/src/asn1/DLTaggedObjectParser.cs index 75e8995be..ea1c5f6c7 100644 --- a/crypto/src/asn1/DLTaggedObjectParser.cs +++ b/crypto/src/asn1/DLTaggedObjectParser.cs @@ -1,5 +1,4 @@ -using System; -using System.IO; +using System.IO; namespace Org.BouncyCastle.Asn1 { @@ -17,46 +16,24 @@ namespace Org.BouncyCastle.Asn1 m_constructed = constructed; } - public override bool IsConstructed - { - get { return m_constructed; } - } + public override bool IsConstructed => m_constructed; public override IAsn1Convertible ParseBaseUniversal(bool declaredExplicit, int baseTagNo) { if (declaredExplicit) - { - if (!m_constructed) - throw new IOException("Explicit tags must be constructed (see X.690 8.14.2)"); - - return m_parser.ParseObject(baseTagNo); - } + return CheckConstructed().ParseObject(baseTagNo); return m_constructed ? m_parser.ParseImplicitConstructedDL(baseTagNo) : m_parser.ParseImplicitPrimitive(baseTagNo); } - public override IAsn1Convertible ParseExplicitBaseObject() - { - if (!m_constructed) - throw new IOException("Explicit tags must be constructed (see X.690 8.14.2)"); - - return m_parser.ReadObject(); - } - - public override Asn1TaggedObjectParser ParseExplicitBaseTagged() - { - if (!m_constructed) - throw new IOException("Explicit tags must be constructed (see X.690 8.14.2)"); + public override IAsn1Convertible ParseExplicitBaseObject() => CheckConstructed().ReadObject(); - return m_parser.ParseTaggedObject(); - } + public override Asn1TaggedObjectParser ParseExplicitBaseTagged() => CheckConstructed().ParseTaggedObject(); - public override Asn1TaggedObjectParser ParseImplicitBaseTagged(int baseTagClass, int baseTagNo) - { - return new DLTaggedObjectParser(baseTagClass, baseTagNo, m_constructed, m_parser); - } + public override Asn1TaggedObjectParser ParseImplicitBaseTagged(int baseTagClass, int baseTagNo) => + new DLTaggedObjectParser(baseTagClass, baseTagNo, m_constructed, m_parser); public override Asn1Object ToAsn1Object() { @@ -69,5 +46,13 @@ namespace Org.BouncyCastle.Asn1 throw new Asn1ParsingException(e.Message); } } + + private Asn1StreamParser CheckConstructed() + { + if (!m_constructed) + throw new IOException("Explicit tags must be constructed (see X.690 8.14.2)"); + + return m_parser; + } } } diff --git a/crypto/src/asn1/cms/Evidence.cs b/crypto/src/asn1/cms/Evidence.cs index d240d234b..baa93d6b9 100644 --- a/crypto/src/asn1/cms/Evidence.cs +++ b/crypto/src/asn1/cms/Evidence.cs @@ -15,7 +15,7 @@ namespace Org.BouncyCastle.Asn1.Cms if (obj is Evidence evidence) return evidence; if (obj is Asn1TaggedObject taggedObject) - return new Evidence(Asn1Utilities.CheckContextTag(taggedObject)); + return new Evidence(Asn1Utilities.CheckContextTagClass(taggedObject)); throw new ArgumentException("Unknown object in GetInstance: " + Platform.GetTypeName(obj), nameof(obj)); } diff --git a/crypto/src/asn1/crmf/PKIArchiveOptions.cs b/crypto/src/asn1/crmf/PKIArchiveOptions.cs index 31103a5be..85815c5db 100644 --- a/crypto/src/asn1/crmf/PKIArchiveOptions.cs +++ b/crypto/src/asn1/crmf/PKIArchiveOptions.cs @@ -19,7 +19,7 @@ namespace Org.BouncyCastle.Asn1.Crmf return pkiArchiveOptions; if (obj is Asn1TaggedObject taggedObject) - return new PkiArchiveOptions(Asn1Utilities.CheckContextTag(taggedObject)); + return new PkiArchiveOptions(Asn1Utilities.CheckContextTagClass(taggedObject)); throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj"); } diff --git a/crypto/src/asn1/isismtt/x509/DeclarationOfMajority.cs b/crypto/src/asn1/isismtt/x509/DeclarationOfMajority.cs index d76a351bf..645d9bd28 100644 --- a/crypto/src/asn1/isismtt/x509/DeclarationOfMajority.cs +++ b/crypto/src/asn1/isismtt/x509/DeclarationOfMajority.cs @@ -74,7 +74,7 @@ namespace Org.BouncyCastle.Asn1.IsisMtt.X509 return declarationOfMajority; if (obj is Asn1TaggedObject taggedObject) - return new DeclarationOfMajority(Asn1Utilities.CheckContextTag(taggedObject)); + return new DeclarationOfMajority(Asn1Utilities.CheckContextTagClass(taggedObject)); throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), nameof(obj)); } diff --git a/crypto/src/asn1/x509/X509Extensions.cs b/crypto/src/asn1/x509/X509Extensions.cs index af0655f8e..6396f9ddc 100644 --- a/crypto/src/asn1/x509/X509Extensions.cs +++ b/crypto/src/asn1/x509/X509Extensions.cs @@ -216,7 +216,7 @@ namespace Org.BouncyCastle.Asn1.X509 // TODO[api] Rename this class to just Extensions and drop support for this if (obj is Asn1TaggedObject taggedObject) - return GetInstance(Asn1Utilities.CheckContextTag(taggedObject).GetBaseObject().ToAsn1Object()); + return GetInstance(Asn1Utilities.CheckContextTagClass(taggedObject).GetBaseObject().ToAsn1Object()); throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), nameof(obj)); } |