summary refs log tree commit diff
path: root/crypto/src/asn1/Asn1OctetString.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2020-06-25 13:32:04 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2020-06-25 13:32:04 +0700
commitffaa15dc7fa8c4ae9e9195874221dd3dd9596771 (patch)
tree07ee552b87d153f35b242b40ab953caddef9d94d /crypto/src/asn1/Asn1OctetString.cs
parentFix spurious table entry (diff)
downloadBouncyCastle.NET-ed25519-ffaa15dc7fa8c4ae9e9195874221dd3dd9596771.tar.xz
Update Asn1OctetString.GetInstance from bc-java
Diffstat (limited to 'crypto/src/asn1/Asn1OctetString.cs')
-rw-r--r--crypto/src/asn1/Asn1OctetString.cs47
1 files changed, 34 insertions, 13 deletions
diff --git a/crypto/src/asn1/Asn1OctetString.cs b/crypto/src/asn1/Asn1OctetString.cs
index da7dfae46..9a64c7e97 100644
--- a/crypto/src/asn1/Asn1OctetString.cs
+++ b/crypto/src/asn1/Asn1OctetString.cs
@@ -41,19 +41,40 @@ namespace Org.BouncyCastle.Asn1
          * @param obj the object we want converted.
          * @exception ArgumentException if the object cannot be converted.
          */
-		public static Asn1OctetString GetInstance(object obj)
-		{
-			if (obj == null || obj is Asn1OctetString)
-			{
-				return (Asn1OctetString)obj;
-			}
-
-			// TODO: this needs to be deleted in V2
-			if (obj is Asn1TaggedObject)
-				return GetInstance(((Asn1TaggedObject)obj).GetObject());
-
-			throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
-		}
+        public static Asn1OctetString GetInstance(object obj)
+        {
+            if (obj == null || obj is Asn1OctetString)
+            {
+                return (Asn1OctetString)obj;
+            }
+            else if (obj is byte[])
+            {
+                try
+                {
+                    return GetInstance(FromByteArray((byte[])obj));
+                }
+                catch (IOException e)
+                {
+                    throw new ArgumentException("failed to construct OCTET STRING from byte[]: " + e.Message);
+                }
+            }
+            // TODO: this needs to be deleted in V2
+            else if (obj is Asn1TaggedObject)
+            {
+                return GetInstance(((Asn1TaggedObject)obj).GetObject());
+            }
+            else if (obj is Asn1Encodable)
+            {
+                Asn1Object primitive = ((Asn1Encodable)obj).ToAsn1Object();
+
+                if (primitive is Asn1OctetString)
+                {
+                    return (Asn1OctetString)primitive;
+                }
+            }
+
+            throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
+        }
 
         /**
          * @param string the octets making up the octet string.