summary refs log tree commit diff
path: root/crypto/src/asn1/cms
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-06-27 21:11:59 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-06-27 21:11:59 +0700
commit35cfd51633bbbbc92c2c17c76646e9a9dc945b11 (patch)
tree563cda15bf08c63de832198be2b9e0263bae5e60 /crypto/src/asn1/cms
parentSplit up fast/slow tests (diff)
downloadBouncyCastle.NET-ed25519-35cfd51633bbbbc92c2c17c76646e9a9dc945b11.tar.xz
Generics migration in Asn1
Diffstat (limited to 'crypto/src/asn1/cms')
-rw-r--r--crypto/src/asn1/cms/AttributeTable.cs174
-rw-r--r--crypto/src/asn1/cms/ContentInfo.cs1
-rw-r--r--crypto/src/asn1/cms/SignedData.cs41
-rw-r--r--crypto/src/asn1/cms/SignerInfo.cs24
4 files changed, 116 insertions, 124 deletions
diff --git a/crypto/src/asn1/cms/AttributeTable.cs b/crypto/src/asn1/cms/AttributeTable.cs
index e20e6e9b2..aa312a13f 100644
--- a/crypto/src/asn1/cms/AttributeTable.cs
+++ b/crypto/src/asn1/cms/AttributeTable.cs
@@ -1,5 +1,5 @@
 using System;
-using System.Collections;
+using System.Collections.Generic;
 
 using Org.BouncyCastle.Utilities;
 
@@ -7,76 +7,64 @@ namespace Org.BouncyCastle.Asn1.Cms
 {
     public class AttributeTable
     {
-        private readonly IDictionary attributes;
+        private readonly Dictionary<DerObjectIdentifier, object> m_attributes;
 
-        public AttributeTable(
-            IDictionary attrs)
+        public AttributeTable(IDictionary<DerObjectIdentifier, object> attrs)
         {
-            this.attributes = Platform.CreateHashtable(attrs);
+            m_attributes = new Dictionary<DerObjectIdentifier, object>(attrs);
         }
 
-        public AttributeTable(
-            Asn1EncodableVector v)
+        public AttributeTable(Asn1EncodableVector v)
         {
-            this.attributes = Platform.CreateHashtable(v.Count);
+            m_attributes = new Dictionary<DerObjectIdentifier, object>(v.Count);
 
-			foreach (Asn1Encodable o in v)
+            foreach (Asn1Encodable e in v)
             {
-                Attribute a = Attribute.GetInstance(o);
-
-				AddAttribute(a);
+				AddAttribute(Attribute.GetInstance(e));
             }
         }
 
-        public AttributeTable(
-            Asn1Set s)
+        public AttributeTable(Asn1Set s)
         {
-            this.attributes = Platform.CreateHashtable(s.Count);
+            m_attributes = new Dictionary<DerObjectIdentifier, object>(s.Count);
 
-			for (int i = 0; i != s.Count; i++)
+            foreach (Asn1Encodable e in s)
             {
-                Attribute a = Attribute.GetInstance(s[i]);
-
-                AddAttribute(a);
+                AddAttribute(Attribute.GetInstance(e));
             }
         }
 
-		public AttributeTable(
-			Attributes attrs)
+		public AttributeTable(Attributes attrs)
 			: this(Asn1Set.GetInstance(attrs.ToAsn1Object()))
 		{
 		}
 
-		private void AddAttribute(
-            Attribute a)
+		private void AddAttribute(Attribute a)
         {
 			DerObjectIdentifier oid = a.AttrType;
-            object obj = attributes[oid];
 
-            if (obj == null)
+            if (!m_attributes.TryGetValue(oid, out object existingValue))
             {
-                attributes[oid] = a;
+                m_attributes[oid] = a;
+                return;
             }
-            else
-            {
-                IList v;
-
-                if (obj is Attribute)
-                {
-                    v = Platform.CreateArrayList();
 
-                    v.Add(obj);
-                    v.Add(a);
-                }
-                else
-                {
-                    v = (IList) obj;
-
-                    v.Add(a);
-                }
+            if (existingValue is IList<Attribute> existingList)
+            {
+                existingList.Add(a);
+                return;
+            }
 
-                attributes[oid] = v;
+            if (existingValue is Attribute existingAttr)
+            {
+                var newList = new List<Attribute>();
+                newList.Add(existingAttr);
+                newList.Add(a);
+                m_attributes[oid] = newList;
+                return;
             }
+
+            throw new InvalidOperationException();
         }
 
 		/// <summary>Return the first attribute matching the given OBJECT IDENTIFIER</summary>
@@ -84,16 +72,18 @@ namespace Org.BouncyCastle.Asn1.Cms
 		{
 			get
 			{
-				object obj = attributes[oid];
+                if (!m_attributes.TryGetValue(oid, out object existingValue))
+                    return null;
 
-				if (obj is IList)
-				{
-					return (Attribute)((IList)obj)[0];
-				}
+                if (existingValue is IList<Attribute> existingList)
+                    return existingList[0];
 
-				return (Attribute) obj;
-			}
-		}
+                if (existingValue is Attribute existingAttr)
+                    return existingAttr;
+
+                throw new InvalidOperationException();
+            }
+        }
 
 		/**
         * Return all the attributes matching the OBJECT IDENTIFIER oid. The vector will be
@@ -102,26 +92,30 @@ namespace Org.BouncyCastle.Asn1.Cms
         * @param oid type of attribute required.
         * @return a vector of all the attributes found of type oid.
         */
-        public Asn1EncodableVector GetAll(
-            DerObjectIdentifier oid)
+        public Asn1EncodableVector GetAll(DerObjectIdentifier oid)
         {
             Asn1EncodableVector v = new Asn1EncodableVector();
 
-            object obj = attributes[oid];
-
-			if (obj is IList)
+            if (m_attributes.TryGetValue(oid, out object existingValue))
             {
-                foreach (Attribute a in (IList)obj)
+                if (existingValue is IList<Attribute> existingList)
                 {
-                    v.Add(a);
+                    foreach (var attr in existingList)
+                    {
+                        v.Add(attr);
+                    }
+                }
+                else if (existingValue is Attribute existingAttr)
+                {
+                    v.Add(existingAttr);
+                }
+                else
+                {
+                    throw new InvalidOperationException();
                 }
-            }
-            else if (obj != null)
-            {
-                v.Add((Attribute) obj);
             }
 
-			return v;
+            return v;
         }
 
 		public int Count
@@ -130,52 +124,60 @@ namespace Org.BouncyCastle.Asn1.Cms
 			{
 				int total = 0;
 
-				foreach (object o in attributes.Values)
-				{
-					if (o is IList)
-					{
-						total += ((IList)o).Count;
-					}
-					else
-					{
-						++total;
-					}
-				}
+                foreach (object existingValue in m_attributes.Values)
+                {
+                    if (existingValue is IList<Attribute> existingList)
+                    {
+                        total += existingList.Count;
+                    }
+                    else if (existingValue is Attribute existingAttr)
+                    {
+                        ++total;
+                    }
+                    else
+                    {
+                        throw new InvalidOperationException();
+                    }
+                }
 
 				return total;
 			}
 		}
 
-        public IDictionary ToDictionary()
+        public IDictionary<DerObjectIdentifier, object> ToDictionary()
         {
-            return Platform.CreateHashtable(attributes);
+            return new Dictionary<DerObjectIdentifier, object>(m_attributes);
         }
 
 		public Asn1EncodableVector ToAsn1EncodableVector()
         {
             Asn1EncodableVector v = new Asn1EncodableVector();
 
-			foreach (object obj in attributes.Values)
+            foreach (object existingValue in m_attributes.Values)
             {
-                if (obj is IList)
+                if (existingValue is IList<Attribute> existingList)
                 {
-                    foreach (object el in (IList)obj)
+                    foreach (Attribute existingAttr in existingList)
                     {
-                        v.Add(Attribute.GetInstance(el));
+                        v.Add(existingAttr);
                     }
                 }
+                else if (existingValue is Attribute existingAttr)
+                {
+                    v.Add(existingAttr);
+                }
                 else
                 {
-                    v.Add(Attribute.GetInstance(obj));
+                    throw new InvalidOperationException();
                 }
             }
 
-			return v;
+            return v;
         }
 
 		public Attributes ToAttributes()
 		{
-			return new Attributes(this.ToAsn1EncodableVector());
+			return new Attributes(ToAsn1EncodableVector());
 		}
 
 		/**
@@ -187,7 +189,7 @@ namespace Org.BouncyCastle.Asn1.Cms
 		 */
 		public AttributeTable Add(DerObjectIdentifier attrType, Asn1Encodable attrValue)
 		{
-			AttributeTable newTable = new AttributeTable(attributes);
+			AttributeTable newTable = new AttributeTable(m_attributes);
 
 			newTable.AddAttribute(new Attribute(attrType, new DerSet(attrValue)));
 
@@ -196,9 +198,9 @@ namespace Org.BouncyCastle.Asn1.Cms
 
 		public AttributeTable Remove(DerObjectIdentifier attrType)
 		{
-			AttributeTable newTable = new AttributeTable(attributes);
+			AttributeTable newTable = new AttributeTable(m_attributes);
 
-			newTable.attributes.Remove(attrType);
+			newTable.m_attributes.Remove(attrType);
 
 			return newTable;
 		}
diff --git a/crypto/src/asn1/cms/ContentInfo.cs b/crypto/src/asn1/cms/ContentInfo.cs
index f130a4bc7..847df6dd8 100644
--- a/crypto/src/asn1/cms/ContentInfo.cs
+++ b/crypto/src/asn1/cms/ContentInfo.cs
@@ -1,5 +1,4 @@
 using System;
-using System.Collections;
 
 using Org.BouncyCastle.Utilities;
 
diff --git a/crypto/src/asn1/cms/SignedData.cs b/crypto/src/asn1/cms/SignedData.cs
index 1e97346e6..789f8bd72 100644
--- a/crypto/src/asn1/cms/SignedData.cs
+++ b/crypto/src/asn1/cms/SignedData.cs
@@ -1,7 +1,4 @@
 using System;
-using System.Collections;
-
-using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Asn1.Cms
 {
@@ -140,8 +137,7 @@ namespace Org.BouncyCastle.Asn1.Cms
             return Version1;
         }
 
-        private bool CheckForVersion3(
-            Asn1Set signerInfs)
+        private bool CheckForVersion3(Asn1Set signerInfs)
         {
             foreach (object obj in signerInfs)
             {
@@ -156,45 +152,42 @@ namespace Org.BouncyCastle.Asn1.Cms
             return false;
         }
 
-        private SignedData(
-            Asn1Sequence seq)
+        private SignedData(Asn1Sequence seq)
         {
-            IEnumerator e = seq.GetEnumerator();
+            var e = seq.GetEnumerator();
 
             e.MoveNext();
             version = (DerInteger)e.Current;
 
             e.MoveNext();
-            digestAlgorithms = ((Asn1Set)e.Current);
+            digestAlgorithms = (Asn1Set)e.Current.ToAsn1Object();
 
             e.MoveNext();
-            contentInfo = ContentInfo.GetInstance(e.Current);
+            contentInfo = ContentInfo.GetInstance(e.Current.ToAsn1Object());
 
             while (e.MoveNext())
             {
-                Asn1Object o = (Asn1Object)e.Current;
+                Asn1Object o = e.Current.ToAsn1Object();
 
                 //
                 // an interesting feature of SignedData is that there appear
                 // to be varying implementations...
                 // for the moment we ignore anything which doesn't fit.
                 //
-                if (o is Asn1TaggedObject)
+                if (o is Asn1TaggedObject tagged)
                 {
-                    Asn1TaggedObject tagged = (Asn1TaggedObject)o;
-
                     switch (tagged.TagNo)
                     {
-                        case 0:
-                            certsBer = tagged is BerTaggedObject;
-                            certificates = Asn1Set.GetInstance(tagged, false);
-                            break;
-                        case 1:
-                            crlsBer = tagged is BerTaggedObject;
-                            crls = Asn1Set.GetInstance(tagged, false);
-                            break;
-                        default:
-                            throw new ArgumentException("unknown tag value " + tagged.TagNo);
+                    case 0:
+                        certsBer = tagged is BerTaggedObject;
+                        certificates = Asn1Set.GetInstance(tagged, false);
+                        break;
+                    case 1:
+                        crlsBer = tagged is BerTaggedObject;
+                        crls = Asn1Set.GetInstance(tagged, false);
+                        break;
+                    default:
+                        throw new ArgumentException("unknown tag value " + tagged.TagNo);
                     }
                 }
                 else
diff --git a/crypto/src/asn1/cms/SignerInfo.cs b/crypto/src/asn1/cms/SignerInfo.cs
index 82ed04082..dbd0125c4 100644
--- a/crypto/src/asn1/cms/SignerInfo.cs
+++ b/crypto/src/asn1/cms/SignerInfo.cs
@@ -1,5 +1,4 @@
 using System;
-using System.Collections;
 
 using Org.BouncyCastle.Asn1.X509;
 using Org.BouncyCastle.Utilities;
@@ -17,8 +16,7 @@ namespace Org.BouncyCastle.Asn1.Cms
         private Asn1OctetString         encryptedDigest;
         private Asn1Set                 unauthenticatedAttributes;
 
-        public static SignerInfo GetInstance(
-            object obj)
+        public static SignerInfo GetInstance(object obj)
         {
             if (obj == null || obj is SignerInfo)
                 return (SignerInfo) obj;
@@ -65,26 +63,26 @@ namespace Org.BouncyCastle.Asn1.Cms
 
         private SignerInfo(Asn1Sequence seq)
         {
-            IEnumerator e = seq.GetEnumerator();
+            var e = seq.GetEnumerator();
 
             e.MoveNext();
-            version = (DerInteger) e.Current;
+            version = (DerInteger)e.Current;
 
             e.MoveNext();
-            sid = SignerIdentifier.GetInstance(e.Current);
+            sid = SignerIdentifier.GetInstance(e.Current.ToAsn1Object());
 
             e.MoveNext();
-            digAlgorithm = AlgorithmIdentifier.GetInstance(e.Current);
+            digAlgorithm = AlgorithmIdentifier.GetInstance(e.Current.ToAsn1Object());
 
             e.MoveNext();
-            object obj = e.Current;
+            var obj = e.Current.ToAsn1Object();
 
-            if (obj is Asn1TaggedObject)
+            if (obj is Asn1TaggedObject tagged)
             {
-                authenticatedAttributes = Asn1Set.GetInstance((Asn1TaggedObject) obj, false);
+                authenticatedAttributes = Asn1Set.GetInstance(tagged, false);
 
                 e.MoveNext();
-                digEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(e.Current);
+                digEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(e.Current.ToAsn1Object());
             }
             else
             {
@@ -93,11 +91,11 @@ namespace Org.BouncyCastle.Asn1.Cms
             }
 
             e.MoveNext();
-            encryptedDigest = DerOctetString.GetInstance(e.Current);
+            encryptedDigest = Asn1OctetString.GetInstance(e.Current.ToAsn1Object());
 
             if (e.MoveNext())
             {
-                unauthenticatedAttributes = Asn1Set.GetInstance((Asn1TaggedObject) e.Current, false);
+                unauthenticatedAttributes = Asn1Set.GetInstance((Asn1TaggedObject)e.Current.ToAsn1Object(), false);
             }
             else
             {