diff options
Diffstat (limited to 'crypto/src/asn1/Asn1OctetString.cs')
-rw-r--r-- | crypto/src/asn1/Asn1OctetString.cs | 85 |
1 files changed, 38 insertions, 47 deletions
diff --git a/crypto/src/asn1/Asn1OctetString.cs b/crypto/src/asn1/Asn1OctetString.cs index 83156e091..d5766e299 100644 --- a/crypto/src/asn1/Asn1OctetString.cs +++ b/crypto/src/asn1/Asn1OctetString.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.IO; using Org.BouncyCastle.Utilities; @@ -12,31 +11,6 @@ namespace Org.BouncyCastle.Asn1 { internal static readonly byte[] EmptyOctets = new byte[0]; - internal byte[] str; - - /** - * return an Octet string from a tagged object. - * - * @param obj the tagged object holding the object we want. - * @param explicitly true if the object is meant to be explicitly - * tagged false otherwise. - * @exception ArgumentException if the tagged object cannot - * be converted. - */ - public static Asn1OctetString GetInstance( - Asn1TaggedObject obj, - bool isExplicit) - { - Asn1Object o = obj.GetObject(); - - if (isExplicit || o is Asn1OctetString) - { - return GetInstance(o); - } - - return BerOctetString.FromSequence(Asn1Sequence.GetInstance(o)); - } - /** * return an Octet string from the given object. * @@ -49,6 +23,15 @@ namespace Org.BouncyCastle.Asn1 { return (Asn1OctetString)obj; } + //else if (obj is Asn1OctetStringParser) + else if (obj is IAsn1Convertible) + { + Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); + if (asn1Object is Asn1OctetString) + { + return (Asn1OctetString)asn1Object; + } + } else if (obj is byte[]) { try @@ -60,39 +43,47 @@ namespace Org.BouncyCastle.Asn1 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), "obj"); + } + + /** + * return an Octet string from a tagged object. + * + * @param obj the tagged object holding the object we want. + * @param explicitly true if the object is meant to be explicitly + * tagged false otherwise. + * @exception ArgumentException if the tagged object cannot + * be converted. + */ + public static Asn1OctetString GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) + { + Asn1Object baseObject = taggedObject.GetObject(); + + if (declaredExplicit || baseObject is Asn1OctetString) + { + return GetInstance(baseObject); } - throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj)); + return BerOctetString.FromSequence(Asn1Sequence.GetInstance(baseObject)); } + internal readonly byte[] contents; + /** * @param string the octets making up the octet string. */ - internal Asn1OctetString( - byte[] str) + internal Asn1OctetString(byte[] contents) { - if (str == null) - throw new ArgumentNullException("str"); + if (null == contents) + throw new ArgumentNullException("contents"); - this.str = str; + this.contents = contents; } public Stream GetOctetStream() { - return new MemoryStream(str, false); + return new MemoryStream(contents, false); } public Asn1OctetStringParser Parser @@ -102,7 +93,7 @@ namespace Org.BouncyCastle.Asn1 public virtual byte[] GetOctets() { - return str; + return contents; } protected override int Asn1GetHashCode() @@ -123,7 +114,7 @@ namespace Org.BouncyCastle.Asn1 public override string ToString() { - return "#" + Hex.ToHexString(str); + return "#" + Hex.ToHexString(contents); } } } |