summary refs log tree commit diff
path: root/crypto/src/asn1/x509
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/asn1/x509')
-rw-r--r--crypto/src/asn1/x509/AuthorityInformationAccess.cs6
-rw-r--r--crypto/src/asn1/x509/CertificateList.cs23
-rw-r--r--crypto/src/asn1/x509/DistributionPoint.cs92
-rw-r--r--crypto/src/asn1/x509/DistributionPointName.cs82
-rw-r--r--crypto/src/asn1/x509/ExtendedKeyUsage.cs4
-rw-r--r--crypto/src/asn1/x509/GeneralNames.cs45
-rw-r--r--crypto/src/asn1/x509/IssuingDistributionPoint.cs70
-rw-r--r--crypto/src/asn1/x509/KeyUsage.cs8
-rw-r--r--crypto/src/asn1/x509/NameConstraints.cs10
-rw-r--r--crypto/src/asn1/x509/SubjectDirectoryAttributes.cs34
-rw-r--r--crypto/src/asn1/x509/SubjectPublicKeyInfo.cs1
-rw-r--r--crypto/src/asn1/x509/Time.cs4
-rw-r--r--crypto/src/asn1/x509/V2TBSCertListGenerator.cs2
-rw-r--r--crypto/src/asn1/x509/X509CertificateStructure.cs11
-rw-r--r--crypto/src/asn1/x509/X509Extensions.cs4
-rw-r--r--crypto/src/asn1/x509/X509ExtensionsGenerator.cs2
16 files changed, 147 insertions, 251 deletions
diff --git a/crypto/src/asn1/x509/AuthorityInformationAccess.cs b/crypto/src/asn1/x509/AuthorityInformationAccess.cs
index 382513674..c601322c5 100644
--- a/crypto/src/asn1/x509/AuthorityInformationAccess.cs
+++ b/crypto/src/asn1/x509/AuthorityInformationAccess.cs
@@ -1,8 +1,6 @@
 using System;
 using System.Text;
 
-using Org.BouncyCastle.Utilities;
-
 namespace Org.BouncyCastle.Asn1.X509
 {
     /**
@@ -31,10 +29,10 @@ namespace Org.BouncyCastle.Asn1.X509
 
         public static AuthorityInformationAccess GetInstance(object obj)
         {
-            if (obj is AuthorityInformationAccess)
-                return (AuthorityInformationAccess)obj;
             if (obj == null)
                 return null;
+            if (obj is AuthorityInformationAccess authorityInformationAccess)
+                return authorityInformationAccess;
             return new AuthorityInformationAccess(Asn1Sequence.GetInstance(obj));
         }
 
diff --git a/crypto/src/asn1/x509/CertificateList.cs b/crypto/src/asn1/x509/CertificateList.cs
index 3d5d2e557..5d73cf411 100644
--- a/crypto/src/asn1/x509/CertificateList.cs
+++ b/crypto/src/asn1/x509/CertificateList.cs
@@ -23,26 +23,21 @@ namespace Org.BouncyCastle.Asn1.X509
         private readonly AlgorithmIdentifier sigAlgID;
         private readonly DerBitString sig;
 
-		public static CertificateList GetInstance(
-            Asn1TaggedObject	obj,
-            bool				explicitly)
+        public static CertificateList GetInstance(Asn1TaggedObject obj, bool explicitly)
         {
             return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
         }
 
-		public static CertificateList GetInstance(
-            object obj)
+        public static CertificateList GetInstance(object obj)
         {
-            if (obj is CertificateList)
-                return (CertificateList) obj;
-
-			if (obj != null)
-				return new CertificateList(Asn1Sequence.GetInstance(obj));
-
-			return null;
-		}
+            if (obj == null)
+                return null;
+            if (obj is CertificateList certificateList)
+                return certificateList;
+            return new CertificateList(Asn1Sequence.GetInstance(obj));
+        }
 
-		private CertificateList(
+        private CertificateList(
             Asn1Sequence seq)
         {
 			if (seq.Count != 3)
diff --git a/crypto/src/asn1/x509/DistributionPoint.cs b/crypto/src/asn1/x509/DistributionPoint.cs
index f35586016..077c9321e 100644
--- a/crypto/src/asn1/x509/DistributionPoint.cs
+++ b/crypto/src/asn1/x509/DistributionPoint.cs
@@ -1,8 +1,5 @@
-using System;
 using System.Text;
 
-using Org.BouncyCastle.Utilities;
-
 namespace Org.BouncyCastle.Asn1.X509
 {
     /**
@@ -18,35 +15,25 @@ namespace Org.BouncyCastle.Asn1.X509
     public class DistributionPoint
         : Asn1Encodable
     {
-        internal readonly DistributionPointName	distributionPoint;
-        internal readonly ReasonFlags			reasons;
-        internal readonly GeneralNames			cRLIssuer;
-
-		public static DistributionPoint GetInstance(
-            Asn1TaggedObject	obj,
-            bool				explicitly)
+		public static DistributionPoint GetInstance(object obj)
         {
-            return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
+            if (obj == null)
+                return null;
+            if (obj is DistributionPoint distributionPoint)
+                return distributionPoint;
+            return new DistributionPoint(Asn1Sequence.GetInstance(obj));
         }
 
-		public static DistributionPoint GetInstance(
-            object obj)
+        public static DistributionPoint GetInstance(Asn1TaggedObject obj, bool explicitly)
         {
-            if(obj == null || obj is DistributionPoint)
-            {
-                return (DistributionPoint) obj;
-            }
-
-			if(obj is Asn1Sequence)
-            {
-                return new DistributionPoint((Asn1Sequence) obj);
-            }
-
-            throw new ArgumentException("Invalid DistributionPoint: " + Platform.GetTypeName(obj));
+            return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
         }
 
-		private DistributionPoint(
-            Asn1Sequence seq)
+        private readonly DistributionPointName m_distributionPoint;
+        private readonly ReasonFlags m_reasons;
+        private readonly GeneralNames m_crlIssuer;
+
+        private DistributionPoint(Asn1Sequence seq)
         {
             for (int i = 0; i != seq.Count; i++)
             {
@@ -55,52 +42,41 @@ namespace Org.BouncyCastle.Asn1.X509
 				switch (t.TagNo)
                 {
                 case 0:
-                    distributionPoint = DistributionPointName.GetInstance(t, true);
+                    m_distributionPoint = DistributionPointName.GetInstance(t, true);
                     break;
                 case 1:
-                    reasons = new ReasonFlags(DerBitString.GetInstance(t, false));
+                    m_reasons = new ReasonFlags(DerBitString.GetInstance(t, false));
                     break;
                 case 2:
-                    cRLIssuer = GeneralNames.GetInstance(t, false);
+                    m_crlIssuer = GeneralNames.GetInstance(t, false);
                     break;
                 }
             }
         }
 
-		public DistributionPoint(
-            DistributionPointName	distributionPointName,
-            ReasonFlags				reasons,
-            GeneralNames			crlIssuer)
+		public DistributionPoint(DistributionPointName distributionPointName, ReasonFlags reasons,
+            GeneralNames crlIssuer)
         {
-            this.distributionPoint = distributionPointName;
-            this.reasons = reasons;
-            this.cRLIssuer = crlIssuer;
+            m_distributionPoint = distributionPointName;
+            m_reasons = reasons;
+            m_crlIssuer = crlIssuer;
         }
 
-		public DistributionPointName DistributionPointName
-        {
-			get { return distributionPoint; }
-        }
+        public DistributionPointName DistributionPointName => m_distributionPoint;
 
-		public ReasonFlags Reasons
-        {
-			get { return reasons; }
-        }
+		public ReasonFlags Reasons => m_reasons;
 
-		public GeneralNames CrlIssuer
-        {
-			get { return cRLIssuer; }
-        }
+		public GeneralNames CrlIssuer => m_crlIssuer;
 
         public override Asn1Object ToAsn1Object()
         {
-            Asn1EncodableVector v = new Asn1EncodableVector();
+            Asn1EncodableVector v = new Asn1EncodableVector(3);
 
             // As this is a CHOICE it must be explicitly tagged
-            v.AddOptionalTagged(true, 0, distributionPoint);
+            v.AddOptionalTagged(true, 0, m_distributionPoint);
 
-            v.AddOptionalTagged(false, 1, reasons);
-            v.AddOptionalTagged(false, 2, cRLIssuer);
+            v.AddOptionalTagged(false, 1, m_reasons);
+            v.AddOptionalTagged(false, 2, m_crlIssuer);
             return new DerSequence(v);
         }
 
@@ -108,17 +84,17 @@ namespace Org.BouncyCastle.Asn1.X509
 		{
 			StringBuilder buf = new StringBuilder();
 			buf.AppendLine("DistributionPoint: [");
-			if (distributionPoint != null)
+			if (m_distributionPoint != null)
 			{
-                AppendObject(buf, "distributionPoint", distributionPoint.ToString());
+                AppendObject(buf, "distributionPoint", m_distributionPoint.ToString());
 			}
-			if (reasons != null)
+			if (m_reasons != null)
 			{
-                AppendObject(buf, "reasons", reasons.ToString());
+                AppendObject(buf, "reasons", m_reasons.ToString());
 			}
-			if (cRLIssuer != null)
+			if (m_crlIssuer != null)
 			{
-                AppendObject(buf, "cRLIssuer", cRLIssuer.ToString());
+                AppendObject(buf, "cRLIssuer", m_crlIssuer.ToString());
 			}
 			buf.AppendLine("]");
 			return buf.ToString();
diff --git a/crypto/src/asn1/x509/DistributionPointName.cs b/crypto/src/asn1/x509/DistributionPointName.cs
index bca54fa06..bdb7219be 100644
--- a/crypto/src/asn1/x509/DistributionPointName.cs
+++ b/crypto/src/asn1/x509/DistributionPointName.cs
@@ -1,8 +1,5 @@
-using System;
 using System.Text;
 
-using Org.BouncyCastle.Utilities;
-
 namespace Org.BouncyCastle.Asn1.X509
 {
     /**
@@ -17,90 +14,71 @@ namespace Org.BouncyCastle.Asn1.X509
     public class DistributionPointName
         : Asn1Encodable, IAsn1Choice
     {
-        internal readonly Asn1Encodable	name;
-        internal readonly int			type;
-
-		public const int FullName					= 0;
-        public const int NameRelativeToCrlIssuer	= 1;
+        public const int FullName = 0;
+        public const int NameRelativeToCrlIssuer = 1;
 
-		public static DistributionPointName GetInstance(
-            Asn1TaggedObject	obj,
-            bool				explicitly)
+		public static DistributionPointName GetInstance(object obj)
         {
-            return GetInstance(Asn1TaggedObject.GetInstance(obj, true));
-        }
-
-		public static DistributionPointName GetInstance(
-            object obj)
-        {
-            if (obj == null || obj is DistributionPointName)
-            {
-                return (DistributionPointName) obj;
-            }
-
-			if (obj is Asn1TaggedObject)
-            {
-                return new DistributionPointName((Asn1TaggedObject) obj);
-            }
-
-            throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj");
+            if (obj == null)
+                return null;
+            if (obj is DistributionPointName distributionPointName)
+                return distributionPointName;
+            return new DistributionPointName(Asn1TaggedObject.GetInstance(obj));
 		}
 
-        public DistributionPointName(
-            int				type,
-            Asn1Encodable	name)
+		public static DistributionPointName GetInstance(Asn1TaggedObject obj, bool explicitly)
         {
-            this.type = type;
-            this.name = name;
+            return GetInstance(Asn1TaggedObject.GetInstance(obj, true));
         }
 
-		public DistributionPointName(
-			GeneralNames name)
-			:	this(FullName, name)
-		{
-		}
+        private readonly Asn1Encodable m_name;
+        private readonly int m_type;
 
-		public int PointType
+        public DistributionPointName(GeneralNames name)
+            : this(FullName, name)
         {
-			get { return type; }
         }
 
-		public Asn1Encodable Name
+        public DistributionPointName(int type, Asn1Encodable name)
         {
-			get { return name; }
+            m_type = type;
+            m_name = name;
         }
 
-		public DistributionPointName(
-            Asn1TaggedObject obj)
+		public int PointType => m_type;
+
+		public Asn1Encodable Name => m_name;
+
+		public DistributionPointName(Asn1TaggedObject obj)
         {
-            this.type = obj.TagNo;
+            m_type = obj.TagNo;
 
-			if (type == FullName)
+			if (m_type == FullName)
             {
-                this.name = GeneralNames.GetInstance(obj, false);
+                m_name = GeneralNames.GetInstance(obj, false);
             }
             else
             {
-                this.name = Asn1Set.GetInstance(obj, false);
+                m_name = Asn1Set.GetInstance(obj, false);
             }
         }
 
 		public override Asn1Object ToAsn1Object()
         {
-            return new DerTaggedObject(false, type, name);
+            return new DerTaggedObject(false, m_type, m_name);
         }
 
 		public override string ToString()
 		{
 			StringBuilder buf = new StringBuilder();
 			buf.AppendLine("DistributionPointName: [");
-			if (type == FullName)
+			if (m_type == FullName)
 			{
-				AppendObject(buf, "fullName", name.ToString());
+				AppendObject(buf, "fullName", m_name.ToString());
 			}
 			else
 			{
-				AppendObject(buf, "nameRelativeToCRLIssuer", name.ToString());
+				AppendObject(buf, "nameRelativeToCRLIssuer", m_name.ToString());
 			}
 			buf.AppendLine("]");
 			return buf.ToString();
diff --git a/crypto/src/asn1/x509/ExtendedKeyUsage.cs b/crypto/src/asn1/x509/ExtendedKeyUsage.cs
index f812c308d..08962ab72 100644
--- a/crypto/src/asn1/x509/ExtendedKeyUsage.cs
+++ b/crypto/src/asn1/x509/ExtendedKeyUsage.cs
@@ -67,10 +67,8 @@ namespace Org.BouncyCastle.Asn1.X509
         {
             Asn1EncodableVector v = new Asn1EncodableVector();
 
-            foreach (object usage in usages)
+            foreach (var oid in usages)
             {
-                DerObjectIdentifier oid = DerObjectIdentifier.GetInstance(usage);
-
                 v.Add(oid);
                 m_usageTable.Add(oid);
             }
diff --git a/crypto/src/asn1/x509/GeneralNames.cs b/crypto/src/asn1/x509/GeneralNames.cs
index acf263f84..3937b3279 100644
--- a/crypto/src/asn1/x509/GeneralNames.cs
+++ b/crypto/src/asn1/x509/GeneralNames.cs
@@ -1,24 +1,16 @@
-using System;
 using System.Text;
 
-using Org.BouncyCastle.Utilities;
-
 namespace Org.BouncyCastle.Asn1.X509
 {
 	public class GeneralNames
 		: Asn1Encodable
 	{
-        private static GeneralName[] Copy(GeneralName[] names)
-        {
-            return (GeneralName[])names.Clone();
-        }
-
         public static GeneralNames GetInstance(object obj)
 		{
-            if (obj is GeneralNames)
-                return (GeneralNames)obj;
             if (obj == null)
                 return null;
+            if (obj is GeneralNames generalNames)
+                return generalNames;
             return new GeneralNames(Asn1Sequence.GetInstance(obj));
 		}
 
@@ -32,36 +24,33 @@ namespace Org.BouncyCastle.Asn1.X509
             return GetInstance(X509Extensions.GetExtensionParsedValue(extensions, extOid));
         }
 
-        private readonly GeneralName[] names;
+        private static GeneralName[] Copy(GeneralName[] names)
+        {
+            return (GeneralName[])names.Clone();
+        }
+
+        private readonly GeneralName[] m_names;
 
         /// <summary>Construct a GeneralNames object containing one GeneralName.</summary>
 		/// <param name="name">The name to be contained.</param>
-		public GeneralNames(
-			GeneralName name)
+		public GeneralNames(GeneralName name)
 		{
-			names = new GeneralName[]{ name };
+			m_names = new GeneralName[]{ name };
 		}
 
-        public GeneralNames(
-            GeneralName[] names)
+        public GeneralNames(GeneralName[] names)
         {
-            this.names = Copy(names);
+            m_names = Copy(names);
         }
 
-		private GeneralNames(
-			Asn1Sequence seq)
+		private GeneralNames(Asn1Sequence seq)
 		{
-			this.names = new GeneralName[seq.Count];
-
-			for (int i = 0; i != seq.Count; i++)
-			{
-				names[i] = GeneralName.GetInstance(seq[i]);
-			}
+			m_names = seq.MapElements(GeneralName.GetInstance);
 		}
 
 		public GeneralName[] GetNames()
 		{
-            return Copy(names);
+            return Copy(m_names);
 		}
 
 		/**
@@ -72,14 +61,14 @@ namespace Org.BouncyCastle.Asn1.X509
 		 */
 		public override Asn1Object ToAsn1Object()
 		{
-			return new DerSequence(names);
+			return new DerSequence(m_names);
 		}
 
 		public override string ToString()
 		{
 			StringBuilder buf = new StringBuilder();
 			buf.AppendLine("GeneralNames:");
-			foreach (GeneralName name in names)
+			foreach (GeneralName name in m_names)
 			{
 				buf.Append("    ")
 				   .Append(name)
diff --git a/crypto/src/asn1/x509/IssuingDistributionPoint.cs b/crypto/src/asn1/x509/IssuingDistributionPoint.cs
index a05efffa4..0287fd8f5 100644
--- a/crypto/src/asn1/x509/IssuingDistributionPoint.cs
+++ b/crypto/src/asn1/x509/IssuingDistributionPoint.cs
@@ -1,8 +1,6 @@
 using System;
 using System.Text;
 
-using Org.BouncyCastle.Utilities;
-
 namespace Org.BouncyCastle.Asn1.X509
 {
 	/**
@@ -28,27 +26,18 @@ namespace Org.BouncyCastle.Asn1.X509
 
 		private readonly Asn1Sequence seq;
 
-		public static IssuingDistributionPoint GetInstance(
-            Asn1TaggedObject	obj,
-            bool				explicitly)
+		public static IssuingDistributionPoint GetInstance(Asn1TaggedObject	obj, bool explicitly)
         {
             return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
         }
 
-		public static IssuingDistributionPoint GetInstance(
-            object obj)
+		public static IssuingDistributionPoint GetInstance(object obj)
         {
-            if (obj == null || obj is IssuingDistributionPoint)
-            {
-                return (IssuingDistributionPoint) obj;
-            }
-
-			if (obj is Asn1Sequence)
-            {
-                return new IssuingDistributionPoint((Asn1Sequence) obj);
-            }
-
-            throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj");
+			if (obj == null)
+				return null;
+            if (obj is IssuingDistributionPoint issuingDistributionPoint)
+                return issuingDistributionPoint;
+            return new IssuingDistributionPoint(Asn1Sequence.GetInstance(obj));
 		}
 
 		/**
@@ -113,8 +102,7 @@ namespace Org.BouncyCastle.Asn1.X509
 		/**
          * Constructor from Asn1Sequence
          */
-        private IssuingDistributionPoint(
-            Asn1Sequence seq)
+        private IssuingDistributionPoint(Asn1Sequence seq)
         {
             this.seq = seq;
 
@@ -124,27 +112,27 @@ namespace Org.BouncyCastle.Asn1.X509
 
 				switch (o.TagNo)
                 {
-					case 0:
-						// CHOICE so explicit
-						_distributionPoint = DistributionPointName.GetInstance(o, true);
-						break;
-					case 1:
-						_onlyContainsUserCerts = DerBoolean.GetInstance(o, false).IsTrue;
-						break;
-					case 2:
-						_onlyContainsCACerts = DerBoolean.GetInstance(o, false).IsTrue;
-						break;
-					case 3:
-						_onlySomeReasons = new ReasonFlags(ReasonFlags.GetInstance(o, false));
-						break;
-					case 4:
-						_indirectCRL = DerBoolean.GetInstance(o, false).IsTrue;
-						break;
-					case 5:
-						_onlyContainsAttributeCerts = DerBoolean.GetInstance(o, false).IsTrue;
-						break;
-					default:
-						throw new ArgumentException("unknown tag in IssuingDistributionPoint");
+				case 0:
+					// CHOICE so explicit
+					_distributionPoint = DistributionPointName.GetInstance(o, true);
+					break;
+				case 1:
+					_onlyContainsUserCerts = DerBoolean.GetInstance(o, false).IsTrue;
+					break;
+				case 2:
+					_onlyContainsCACerts = DerBoolean.GetInstance(o, false).IsTrue;
+					break;
+				case 3:
+					_onlySomeReasons = new ReasonFlags(ReasonFlags.GetInstance(o, false));
+					break;
+				case 4:
+					_indirectCRL = DerBoolean.GetInstance(o, false).IsTrue;
+					break;
+				case 5:
+					_onlyContainsAttributeCerts = DerBoolean.GetInstance(o, false).IsTrue;
+					break;
+				default:
+					throw new ArgumentException("unknown tag in IssuingDistributionPoint");
                 }
             }
         }
diff --git a/crypto/src/asn1/x509/KeyUsage.cs b/crypto/src/asn1/x509/KeyUsage.cs
index b31b54341..dd69cc63b 100644
--- a/crypto/src/asn1/x509/KeyUsage.cs
+++ b/crypto/src/asn1/x509/KeyUsage.cs
@@ -32,10 +32,10 @@ namespace Org.BouncyCastle.Asn1.X509
 
 		public static new KeyUsage GetInstance(object obj)
 		{
-			if (obj is KeyUsage)
-				return (KeyUsage)obj;
-            if (obj is X509Extension)
-				return GetInstance(X509Extension.ConvertValueToObject((X509Extension)obj));
+			if (obj is KeyUsage keyUsage)
+				return keyUsage;
+            if (obj is X509Extension x509Extension)
+				return GetInstance(X509Extension.ConvertValueToObject(x509Extension));
             if (obj == null)
                 return null;
 			return new KeyUsage(DerBitString.GetInstance(obj));
diff --git a/crypto/src/asn1/x509/NameConstraints.cs b/crypto/src/asn1/x509/NameConstraints.cs
index 9fe4fdd01..590b14aa0 100644
--- a/crypto/src/asn1/x509/NameConstraints.cs
+++ b/crypto/src/asn1/x509/NameConstraints.cs
@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Linq;
 
 using Org.BouncyCastle.Utilities;
 
@@ -68,12 +69,7 @@ namespace Org.BouncyCastle.Asn1.X509
 
 		private DerSequence CreateSequence(IList<GeneralSubtree> subtrees)
 		{
-            GeneralSubtree[] gsts = new GeneralSubtree[subtrees.Count];
-            for (int i = 0; i < subtrees.Count; ++i)
-            {
-                gsts[i] = subtrees[i];
-            }
-            return new DerSequence(gsts);
+            return new DerSequence(subtrees.ToArray());
 		}
 
 		public Asn1Sequence PermittedSubtrees
@@ -92,7 +88,7 @@ namespace Org.BouncyCastle.Asn1.X509
 		 */
         public override Asn1Object ToAsn1Object()
         {
-            Asn1EncodableVector v = new Asn1EncodableVector();
+            Asn1EncodableVector v = new Asn1EncodableVector(2);
             v.AddOptionalTagged(false, 0, permitted);
             v.AddOptionalTagged(false, 1, excluded);
             return new DerSequence(v);
diff --git a/crypto/src/asn1/x509/SubjectDirectoryAttributes.cs b/crypto/src/asn1/x509/SubjectDirectoryAttributes.cs
index 6ebd35e21..c8f24ecd5 100644
--- a/crypto/src/asn1/x509/SubjectDirectoryAttributes.cs
+++ b/crypto/src/asn1/x509/SubjectDirectoryAttributes.cs
@@ -1,7 +1,5 @@
-using System;
 using System.Collections.Generic;
 
-using Org.BouncyCastle.Utilities;
 using Org.BouncyCastle.Utilities.Collections;
 
 namespace Org.BouncyCastle.Asn1.X509
@@ -28,23 +26,16 @@ namespace Org.BouncyCastle.Asn1.X509
 	public class SubjectDirectoryAttributes
 		: Asn1Encodable
 	{
-		private readonly IList<AttributeX509> m_attributes;
+		private readonly List<AttributeX509> m_attributes;
 
-		public static SubjectDirectoryAttributes GetInstance(
-			object obj)
-		{
-			if (obj == null || obj is SubjectDirectoryAttributes)
-			{
-				return (SubjectDirectoryAttributes) obj;
-			}
-
-			if (obj is Asn1Sequence)
-			{
-				return new SubjectDirectoryAttributes((Asn1Sequence) obj);
-			}
-
-            throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj");
-		}
+        public static SubjectDirectoryAttributes GetInstance(object obj)
+        {
+            if (obj == null)
+                return null;
+            if (obj is SubjectDirectoryAttributes subjectDirectoryAttributes)
+                return subjectDirectoryAttributes;
+            return new SubjectDirectoryAttributes(Asn1Sequence.GetInstance(obj));
+        }
 
 		/**
 		 * Constructor from Asn1Sequence.
@@ -114,12 +105,7 @@ namespace Org.BouncyCastle.Asn1.X509
 		 */
 		public override Asn1Object ToAsn1Object()
 		{
-            AttributeX509[] v = new AttributeX509[m_attributes.Count];
-            for (int i = 0; i < m_attributes.Count; ++i)
-            {
-                v[i] = m_attributes[i];
-            }
-            return new DerSequence(v);
+            return new DerSequence(m_attributes.ToArray());
 		}
 
         /**
diff --git a/crypto/src/asn1/x509/SubjectPublicKeyInfo.cs b/crypto/src/asn1/x509/SubjectPublicKeyInfo.cs
index 52f977e91..4875152eb 100644
--- a/crypto/src/asn1/x509/SubjectPublicKeyInfo.cs
+++ b/crypto/src/asn1/x509/SubjectPublicKeyInfo.cs
@@ -1,5 +1,4 @@
 using System;
-using System.IO;
 
 namespace Org.BouncyCastle.Asn1.X509
 {
diff --git a/crypto/src/asn1/x509/Time.cs b/crypto/src/asn1/x509/Time.cs
index e03055f6d..f7746257c 100644
--- a/crypto/src/asn1/x509/Time.cs
+++ b/crypto/src/asn1/x509/Time.cs
@@ -31,7 +31,7 @@ namespace Org.BouncyCastle.Asn1.X509
 
         public Time(Asn1GeneralizedTime generalizedTime)
         {
-            this.m_timeObject = generalizedTime ?? throw new ArgumentNullException(nameof(generalizedTime));
+            m_timeObject = generalizedTime ?? throw new ArgumentNullException(nameof(generalizedTime));
         }
 
         public Time(Asn1UtcTime utcTime)
@@ -42,7 +42,7 @@ namespace Org.BouncyCastle.Asn1.X509
             // Validate utcTime is in the appropriate year range
             utcTime.ToDateTime(2049);
 
-            this.m_timeObject = utcTime;
+            m_timeObject = utcTime;
         }
 
         /**
diff --git a/crypto/src/asn1/x509/V2TBSCertListGenerator.cs b/crypto/src/asn1/x509/V2TBSCertListGenerator.cs
index aa1a0b95d..bf016c22d 100644
--- a/crypto/src/asn1/x509/V2TBSCertListGenerator.cs
+++ b/crypto/src/asn1/x509/V2TBSCertListGenerator.cs
@@ -2,8 +2,6 @@ using System;
 using System.Collections.Generic;
 using System.IO;
 
-using Org.BouncyCastle.Utilities;
-
 namespace Org.BouncyCastle.Asn1.X509
 {
     /**
diff --git a/crypto/src/asn1/x509/X509CertificateStructure.cs b/crypto/src/asn1/x509/X509CertificateStructure.cs
index 6e7c85de6..5394b2be3 100644
--- a/crypto/src/asn1/x509/X509CertificateStructure.cs
+++ b/crypto/src/asn1/x509/X509CertificateStructure.cs
@@ -21,20 +21,17 @@ namespace Org.BouncyCastle.Asn1.X509
         private readonly AlgorithmIdentifier		sigAlgID;
         private readonly DerBitString				sig;
 
-        public static X509CertificateStructure GetInstance(
-            Asn1TaggedObject	obj,
-            bool				explicitly)
+        public static X509CertificateStructure GetInstance(Asn1TaggedObject obj, bool explicitly)
         {
             return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
         }
 
-        public static X509CertificateStructure GetInstance(
-            object obj)
+        public static X509CertificateStructure GetInstance(object obj)
         {
-            if (obj is X509CertificateStructure)
-                return (X509CertificateStructure)obj;
             if (obj == null)
                 return null;
+            if (obj is X509CertificateStructure x509CertificateStructure)
+                return x509CertificateStructure;
             return new X509CertificateStructure(Asn1Sequence.GetInstance(obj));
         }
 
diff --git a/crypto/src/asn1/x509/X509Extensions.cs b/crypto/src/asn1/x509/X509Extensions.cs
index a399058c2..aa5205800 100644
--- a/crypto/src/asn1/x509/X509Extensions.cs
+++ b/crypto/src/asn1/x509/X509Extensions.cs
@@ -175,12 +175,12 @@ namespace Org.BouncyCastle.Asn1.X509
 
         public static X509Extension GetExtension(X509Extensions extensions, DerObjectIdentifier oid)
         {
-            return null == extensions ? null : extensions.GetExtension(oid);
+            return extensions?.GetExtension(oid);
         }
 
         public static Asn1Encodable GetExtensionParsedValue(X509Extensions extensions, DerObjectIdentifier oid)
         {
-            return null == extensions ? null : extensions.GetExtensionParsedValue(oid);
+            return extensions?.GetExtensionParsedValue(oid);
         }
 
 		public static X509Extensions GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
diff --git a/crypto/src/asn1/x509/X509ExtensionsGenerator.cs b/crypto/src/asn1/x509/X509ExtensionsGenerator.cs
index 53d18ecff..7ea6084af 100644
--- a/crypto/src/asn1/x509/X509ExtensionsGenerator.cs
+++ b/crypto/src/asn1/x509/X509ExtensionsGenerator.cs
@@ -90,8 +90,6 @@ namespace Org.BouncyCastle.Asn1.X509
             }
         }
 
-
-
         /// <summary>Return true if there are no extension present in this generator.</summary>
         /// <returns>True if empty, false otherwise</returns>
         public bool IsEmpty