summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--crypto/src/asn1/Asn1TaggedObject.cs2
-rw-r--r--crypto/src/asn1/Asn1UniversalType.cs5
-rw-r--r--crypto/src/asn1/Asn1Utilities.cs70
-rw-r--r--crypto/src/asn1/DERExternal.cs8
-rw-r--r--crypto/src/asn1/DerObjectIdentifier.cs2
-rw-r--r--crypto/src/asn1/cms/Evidence.cs2
-rw-r--r--crypto/src/asn1/crmf/PKIArchiveOptions.cs2
-rw-r--r--crypto/src/asn1/isismtt/x509/DeclarationOfMajority.cs2
-rw-r--r--crypto/src/asn1/x509/X509Extensions.cs9
9 files changed, 56 insertions, 46 deletions
diff --git a/crypto/src/asn1/Asn1TaggedObject.cs b/crypto/src/asn1/Asn1TaggedObject.cs
index d6722c020..a5a5e9506 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.CheckTagClass(this, Asn1Tags.ContextSpecific);
+            Asn1Utilities.CheckContextTag(this);
 
             return m_object.ToAsn1Object();
         }
diff --git a/crypto/src/asn1/Asn1UniversalType.cs b/crypto/src/asn1/Asn1UniversalType.cs
index 46cacb436..81f03a540 100644
--- a/crypto/src/asn1/Asn1UniversalType.cs
+++ b/crypto/src/asn1/Asn1UniversalType.cs
@@ -42,10 +42,7 @@ namespace Org.BouncyCastle.Asn1
 
         internal Asn1Object GetContextInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
         {
-            if (Asn1Tags.ContextSpecific != taggedObject.TagClass)
-                throw new InvalidOperationException("this method only valid for CONTEXT_SPECIFIC tags");
-
-            return CheckedCast(taggedObject.GetBaseUniversal(declaredExplicit, this));
+            return CheckedCast(Asn1Utilities.CheckContextTag(taggedObject).GetBaseUniversal(declaredExplicit, this));
         }
 
         internal Asn1Tag Tag
diff --git a/crypto/src/asn1/Asn1Utilities.cs b/crypto/src/asn1/Asn1Utilities.cs
index 6706ace72..4207d132e 100644
--- a/crypto/src/asn1/Asn1Utilities.cs
+++ b/crypto/src/asn1/Asn1Utilities.cs
@@ -8,26 +8,14 @@ namespace Org.BouncyCastle.Asn1
     // TODO[api] Make static
     public abstract class Asn1Utilities
     {
-        internal static Asn1TaggedObject CheckTagClass(Asn1TaggedObject taggedObject, int tagClass)
+        internal static Asn1TaggedObject CheckContextTag(Asn1TaggedObject taggedObject)
         {
-            if (!taggedObject.HasTagClass(tagClass))
-            {
-                string expected = GetTagClassText(tagClass);
-                string found = GetTagClassText(taggedObject);
-                throw new InvalidOperationException("Expected " + expected + " tag but found " + found);
-            }
-            return taggedObject;
+            return CheckTagClass(taggedObject, Asn1Tags.ContextSpecific);
         }
 
-        internal static Asn1TaggedObjectParser CheckTagClass(Asn1TaggedObjectParser taggedObjectParser, int tagClass)
+        internal static Asn1TaggedObjectParser CheckContextTag(Asn1TaggedObjectParser taggedObjectParser)
         {
-            if (taggedObjectParser.TagClass != tagClass)
-            {
-                string expected = GetTagClassText(tagClass);
-                string found = GetTagClassText(taggedObjectParser);
-                throw new InvalidOperationException("Expected " + expected + " tag but found " + found);
-            }
-            return taggedObjectParser;
+            return CheckTagClass(taggedObjectParser, Asn1Tags.ContextSpecific);
         }
 
         internal static Asn1TaggedObject CheckTag(Asn1TaggedObject taggedObject, int tagClass, int tagNo)
@@ -53,6 +41,27 @@ namespace Org.BouncyCastle.Asn1
             return taggedObjectParser;
         }
 
+        internal static Asn1TaggedObject CheckTagClass(Asn1TaggedObject taggedObject, int tagClass)
+        {
+            if (!taggedObject.HasTagClass(tagClass))
+            {
+                string expected = GetTagClassText(tagClass);
+                string found = GetTagClassText(taggedObject);
+                throw new InvalidOperationException("Expected " + expected + " tag but found " + found);
+            }
+            return taggedObject;
+        }
+
+        internal static Asn1TaggedObjectParser CheckTagClass(Asn1TaggedObjectParser taggedObjectParser, int tagClass)
+        {
+            if (taggedObjectParser.TagClass != tagClass)
+            {
+                string expected = GetTagClassText(tagClass);
+                string found = GetTagClassText(taggedObjectParser);
+                throw new InvalidOperationException("Expected " + expected + " tag but found " + found);
+            }
+            return taggedObjectParser;
+        }
 
         internal static TChoice GetInstanceFromChoice<TChoice>(Asn1TaggedObject taggedObject, bool declaredExplicit,
             Func<object, TChoice> constructor)
@@ -73,6 +82,25 @@ namespace Org.BouncyCastle.Asn1
         }
 
 
+        /*
+         * Tag text methods
+         */
+
+        internal static string GetTagClassText(Asn1Tag tag)
+        {
+            return GetTagClassText(tag.TagClass);
+        }
+
+        public static string GetTagClassText(Asn1TaggedObject taggedObject)
+        {
+            return GetTagClassText(taggedObject.TagClass);
+        }
+
+        public static string GetTagClassText(Asn1TaggedObjectParser taggedObjectParser)
+        {
+            return GetTagClassText(taggedObjectParser.TagClass);
+        }
+
         public static string GetTagClassText(int tagClass)
         {
             switch (tagClass)
@@ -88,16 +116,6 @@ namespace Org.BouncyCastle.Asn1
             }
         }
 
-        public static string GetTagClassText(Asn1TaggedObject taggedObject)
-        {
-            return GetTagClassText(taggedObject.TagClass);
-        }
-
-        public static string GetTagClassText(Asn1TaggedObjectParser taggedObjectParser)
-        {
-            return GetTagClassText(taggedObjectParser.TagClass);
-        }
-
         internal static string GetTagText(Asn1Tag tag)
         {
             return GetTagText(tag.TagClass, tag.TagNo);
diff --git a/crypto/src/asn1/DERExternal.cs b/crypto/src/asn1/DERExternal.cs
index 8cb7d040b..cfe0ae701 100644
--- a/crypto/src/asn1/DERExternal.cs
+++ b/crypto/src/asn1/DERExternal.cs
@@ -251,11 +251,9 @@ namespace Org.BouncyCastle.Asn1
 
         private static Asn1Object GetExternalContent(Asn1TaggedObject encoding)
         {
-            int tagClass = encoding.TagClass, tagNo = encoding.TagNo;
-            if (Asn1Tags.ContextSpecific != tagClass)
-                throw new ArgumentException("invalid tag: " + Asn1Utilities.GetTagText(tagClass, tagNo), "encoding");
+            Asn1Utilities.CheckContextTag(encoding);
 
-            switch (tagNo)
+            switch (encoding.TagNo)
             {
             case 0:
                 return encoding.GetExplicitBaseObject().ToAsn1Object();
@@ -264,7 +262,7 @@ namespace Org.BouncyCastle.Asn1
             case 2:
                 return DerBitString.GetInstance(encoding, false);
             default:
-                throw new ArgumentException("invalid tag: " + Asn1Utilities.GetTagText(tagClass, tagNo), "encoding");
+                throw new ArgumentException("invalid tag: " + Asn1Utilities.GetTagText(encoding), nameof(encoding));
             }
         }
 
diff --git a/crypto/src/asn1/DerObjectIdentifier.cs b/crypto/src/asn1/DerObjectIdentifier.cs
index 12e8ea0a8..f43724e1f 100644
--- a/crypto/src/asn1/DerObjectIdentifier.cs
+++ b/crypto/src/asn1/DerObjectIdentifier.cs
@@ -69,7 +69,7 @@ namespace Org.BouncyCastle.Asn1
              * 
              * - see https://github.com/bcgit/bc-java/issues/1015
              */
-            if (!declaredExplicit && !taggedObject.IsParsed() && Asn1Tags.ContextSpecific == taggedObject.TagClass)
+            if (!declaredExplicit && !taggedObject.IsParsed() && taggedObject.HasContextTag())
             {
                 Asn1Object baseObject = taggedObject.GetBaseObject().ToAsn1Object();
                 if (!(baseObject is DerObjectIdentifier))
diff --git a/crypto/src/asn1/cms/Evidence.cs b/crypto/src/asn1/cms/Evidence.cs
index 19dab50c2..d240d234b 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.CheckTagClass(taggedObject, Asn1Tags.ContextSpecific));
+                return new Evidence(Asn1Utilities.CheckContextTag(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 f1a35541a..31103a5be 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.CheckTagClass(taggedObject, Asn1Tags.ContextSpecific));
+                return new PkiArchiveOptions(Asn1Utilities.CheckContextTag(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 f20d17f6b..d76a351bf 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.CheckTagClass(taggedObject, Asn1Tags.ContextSpecific));
+				return new DeclarationOfMajority(Asn1Utilities.CheckContextTag(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 d4497d255..af0655f8e 100644
--- a/crypto/src/asn1/x509/X509Extensions.cs
+++ b/crypto/src/asn1/x509/X509Extensions.cs
@@ -214,14 +214,11 @@ namespace Org.BouncyCastle.Asn1.X509
             if (obj is Asn1Sequence sequence)
                 return new X509Extensions(sequence);
 
+            // TODO[api] Rename this class to just Extensions and drop support for this
             if (obj is Asn1TaggedObject taggedObject)
-            {
-                Asn1Utilities.CheckTagClass(taggedObject, Asn1Tags.ContextSpecific);
-
-                return GetInstance(taggedObject.GetBaseObject().ToAsn1Object());
-            }
+                return GetInstance(Asn1Utilities.CheckContextTag(taggedObject).GetBaseObject().ToAsn1Object());
 
-            throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj");
+            throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), nameof(obj));
         }
 
         /**