summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2020-02-21 18:04:07 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2020-02-21 18:04:07 +0700
commitb204309029cd6bd92d8a20e5778e6cf328bf7745 (patch)
treeda220b61292493a744a210c2c7e00740468838a2
parentImprovements to permitted sub-trees (diff)
downloadBouncyCastle.NET-ed25519-b204309029cd6bd92d8a20e5778e6cf328bf7745.tar.xz
Update ASN.1 GetInstance methods
-rw-r--r--crypto/src/asn1/Asn1Sequence.cs4
-rw-r--r--crypto/src/asn1/tsp/Accuracy.cs26
-rw-r--r--crypto/src/asn1/tsp/MessageImprint.cs32
-rw-r--r--crypto/src/asn1/tsp/TSTInfo.cs34
-rw-r--r--crypto/src/asn1/tsp/TimeStampReq.cs26
-rw-r--r--crypto/src/asn1/tsp/TimeStampResp.cs26
-rw-r--r--crypto/src/asn1/x509/X509Name.cs10
7 files changed, 49 insertions, 109 deletions
diff --git a/crypto/src/asn1/Asn1Sequence.cs b/crypto/src/asn1/Asn1Sequence.cs
index 854c81590..286b731f5 100644
--- a/crypto/src/asn1/Asn1Sequence.cs
+++ b/crypto/src/asn1/Asn1Sequence.cs
@@ -28,13 +28,13 @@ namespace Org.BouncyCastle.Asn1
             }
             else if (obj is Asn1SequenceParser)
             {
-                return Asn1Sequence.GetInstance(((Asn1SequenceParser)obj).ToAsn1Object());
+                return GetInstance(((Asn1SequenceParser)obj).ToAsn1Object());
             }
             else if (obj is byte[])
             {
                 try
                 {
-                    return Asn1Sequence.GetInstance(FromByteArray((byte[])obj));
+                    return GetInstance(FromByteArray((byte[])obj));
                 }
                 catch (IOException e)
                 {
diff --git a/crypto/src/asn1/tsp/Accuracy.cs b/crypto/src/asn1/tsp/Accuracy.cs
index 31289db99..0cbc731ac 100644
--- a/crypto/src/asn1/tsp/Accuracy.cs
+++ b/crypto/src/asn1/tsp/Accuracy.cs
@@ -75,24 +75,16 @@ namespace Org.BouncyCastle.Asn1.Tsp
 			}
 		}
 
-		public static Accuracy GetInstance(
-			object o)
-		{
-			if (o == null || o is Accuracy)
-			{
-				return (Accuracy) o;
-			}
-
-			if (o is Asn1Sequence)
-			{
-				return new Accuracy((Asn1Sequence) o);
-			}
-
-			throw new ArgumentException(
-				"Unknown object in 'Accuracy' factory: " + Platform.GetTypeName(o));
-		}
+        public static Accuracy GetInstance(object obj)
+        {
+            if (obj is Accuracy)
+                return (Accuracy)obj;
+            if (obj == null)
+                return null;
+            return new Accuracy(Asn1Sequence.GetInstance(obj));
+        }
 
-		public DerInteger Seconds
+        public DerInteger Seconds
 		{
 			get { return seconds; }
 		}
diff --git a/crypto/src/asn1/tsp/MessageImprint.cs b/crypto/src/asn1/tsp/MessageImprint.cs
index 44ef7d177..cb728629c 100644
--- a/crypto/src/asn1/tsp/MessageImprint.cs
+++ b/crypto/src/asn1/tsp/MessageImprint.cs
@@ -11,28 +11,16 @@ namespace Org.BouncyCastle.Asn1.Tsp
 		private readonly AlgorithmIdentifier	hashAlgorithm;
 		private readonly byte[]					hashedMessage;
 
-		/**
-		 * @param o
-		 * @return a MessageImprint object.
-		 */
-		public static MessageImprint GetInstance(
-			object o)
-		{
-			if (o == null || o is MessageImprint)
-			{
-				return (MessageImprint) o;
-			}
-
-			if (o is Asn1Sequence)
-			{
-				return new MessageImprint((Asn1Sequence) o);
-			}
-
-			throw new ArgumentException(
-				"Unknown object in 'MessageImprint' factory: " + Platform.GetTypeName(o));
-		}
-
-		private MessageImprint(
+        public static MessageImprint GetInstance(object obj)
+        {
+            if (obj is MessageImprint)
+                return (MessageImprint)obj;
+            if (obj == null)
+                return null;
+            return new MessageImprint(Asn1Sequence.GetInstance(obj));
+        }
+
+        private MessageImprint(
 			Asn1Sequence seq)
 		{
 			if (seq.Count != 2)
diff --git a/crypto/src/asn1/tsp/TSTInfo.cs b/crypto/src/asn1/tsp/TSTInfo.cs
index ee4dd67f1..3f5ab28bb 100644
--- a/crypto/src/asn1/tsp/TSTInfo.cs
+++ b/crypto/src/asn1/tsp/TSTInfo.cs
@@ -21,35 +21,13 @@ namespace Org.BouncyCastle.Asn1.Tsp
 		private readonly GeneralName			tsa;
 		private readonly X509Extensions			extensions;
 
-		public static TstInfo GetInstance(
-			object o)
+		public static TstInfo GetInstance(object obj)
 		{
-			if (o == null || o is TstInfo)
-			{
-				return (TstInfo) o;
-			}
-
-			if (o is Asn1Sequence)
-			{
-				return new TstInfo((Asn1Sequence) o);
-			}
-
-			if (o is Asn1OctetString)
-			{
-				try
-				{
-					byte[] octets = ((Asn1OctetString)o).GetOctets();
-					return GetInstance(Asn1Object.FromByteArray(octets));
-				}
-				catch (IOException)
-				{
-					throw new ArgumentException(
-						"Bad object format in 'TstInfo' factory.");
-				}
-			}
-
-			throw new ArgumentException(
-				"Unknown object in 'TstInfo' factory: " + Platform.GetTypeName(o));
+            if (obj is TstInfo)
+                return (TstInfo)obj;
+            if (obj == null)
+                return null;
+            return new TstInfo(Asn1Sequence.GetInstance(obj));
 		}
 
 		private TstInfo(
diff --git a/crypto/src/asn1/tsp/TimeStampReq.cs b/crypto/src/asn1/tsp/TimeStampReq.cs
index b71fe83ab..7173172c4 100644
--- a/crypto/src/asn1/tsp/TimeStampReq.cs
+++ b/crypto/src/asn1/tsp/TimeStampReq.cs
@@ -15,24 +15,16 @@ namespace Org.BouncyCastle.Asn1.Tsp
 		private readonly DerBoolean				certReq;
 		private readonly X509Extensions			extensions;
 
-		public static TimeStampReq GetInstance(
-			object o)
-		{
-			if (o == null || o is TimeStampReq)
-			{
-				return (TimeStampReq) o;
-			}
-
-			if (o is Asn1Sequence)
-			{
-				return new TimeStampReq((Asn1Sequence) o);
-			}
-
-			throw new ArgumentException(
-				"Unknown object in 'TimeStampReq' factory: " + Platform.GetTypeName(o));
-		}
+        public static TimeStampReq GetInstance(object obj)
+        {
+            if (obj is TimeStampReq)
+                return (TimeStampReq)obj;
+            if (obj == null)
+                return null;
+            return new TimeStampReq(Asn1Sequence.GetInstance(obj));
+        }
 
-		private TimeStampReq(
+        private TimeStampReq(
 			Asn1Sequence seq)
 		{
 			int nbObjects = seq.Count;
diff --git a/crypto/src/asn1/tsp/TimeStampResp.cs b/crypto/src/asn1/tsp/TimeStampResp.cs
index f5186ca4f..3dde0dfce 100644
--- a/crypto/src/asn1/tsp/TimeStampResp.cs
+++ b/crypto/src/asn1/tsp/TimeStampResp.cs
@@ -12,24 +12,16 @@ namespace Org.BouncyCastle.Asn1.Tsp
 		private readonly PkiStatusInfo	pkiStatusInfo;
 		private readonly ContentInfo	timeStampToken;
 
-		public static TimeStampResp GetInstance(
-			object o)
-		{
-			if (o == null || o is TimeStampResp)
-			{
-				return (TimeStampResp) o;
-			}
-
-			if (o is Asn1Sequence)
-			{
-				return new TimeStampResp((Asn1Sequence) o);
-			}
-
-			throw new ArgumentException(
-				"Unknown object in 'TimeStampResp' factory: " + Platform.GetTypeName(o));
-		}
+        public static TimeStampResp GetInstance(object obj)
+        {
+            if (obj is TimeStampResp)
+                return (TimeStampResp)obj;
+            if (obj == null)
+                return null;
+            return new TimeStampResp(Asn1Sequence.GetInstance(obj));
+        }
 
-		private TimeStampResp(
+        private TimeStampResp(
 			Asn1Sequence seq)
 		{
 			this.pkiStatusInfo = PkiStatusInfo.GetInstance(seq[0]);
diff --git a/crypto/src/asn1/x509/X509Name.cs b/crypto/src/asn1/x509/X509Name.cs
index c3c3cc6c9..bd8f9fb3e 100644
--- a/crypto/src/asn1/x509/X509Name.cs
+++ b/crypto/src/asn1/x509/X509Name.cs
@@ -364,13 +364,11 @@ namespace Org.BouncyCastle.Asn1.X509
         public static X509Name GetInstance(
             object obj)
         {
-            if (obj == null || obj is X509Name)
+            if (obj is X509Name)
                 return (X509Name)obj;
-
-            if (obj != null)
-                return new X509Name(Asn1Sequence.GetInstance(obj));
-
-            throw new ArgumentException("null object in factory", "obj");
+            if (obj == null)
+                return null;
+            return new X509Name(Asn1Sequence.GetInstance(obj));
         }
 
         protected X509Name()