diff options
Diffstat (limited to 'crypto/src')
-rw-r--r-- | crypto/src/asn1/Asn1OctetString.cs | 47 |
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. |