Asn1OctetString.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1
  9. {
  10. public abstract class Asn1OctetString
  11. : Asn1Object, Asn1OctetStringParser
  12. {
  13. internal byte[] str;
  14. /**
  15. * return an Octet string from a tagged object.
  16. *
  17. * @param obj the tagged object holding the object we want.
  18. * @param explicitly true if the object is meant to be explicitly
  19. * tagged false otherwise.
  20. * @exception ArgumentException if the tagged object cannot
  21. * be converted.
  22. */
  23. public static Asn1OctetString GetInstance(
  24. Asn1TaggedObject obj,
  25. bool isExplicit)
  26. {
  27. Asn1Object o = obj.GetObject();
  28. if (isExplicit || o is Asn1OctetString)
  29. {
  30. return GetInstance(o);
  31. }
  32. return BerOctetString.FromSequence(Asn1Sequence.GetInstance(o));
  33. }
  34. /**
  35. * return an Octet string from the given object.
  36. *
  37. * @param obj the object we want converted.
  38. * @exception ArgumentException if the object cannot be converted.
  39. */
  40. public static Asn1OctetString GetInstance(object obj)
  41. {
  42. if (obj == null || obj is Asn1OctetString)
  43. {
  44. return (Asn1OctetString)obj;
  45. }
  46. // TODO: this needs to be deleted in V2
  47. if (obj is Asn1TaggedObject)
  48. return GetInstance(((Asn1TaggedObject)obj).GetObject());
  49. throw new ArgumentException("illegal object in GetInstance: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  50. }
  51. /**
  52. * @param string the octets making up the octet string.
  53. */
  54. internal Asn1OctetString(
  55. byte[] str)
  56. {
  57. if (str == null)
  58. throw new ArgumentNullException("str");
  59. this.str = str;
  60. }
  61. internal Asn1OctetString(
  62. Asn1Encodable obj)
  63. {
  64. try
  65. {
  66. this.str = obj.GetEncoded(Asn1Encodable.Der);
  67. }
  68. catch (IOException e)
  69. {
  70. throw new ArgumentException("Error processing object : " + e.ToString());
  71. }
  72. }
  73. public Stream GetOctetStream()
  74. {
  75. return new MemoryStream(str, false);
  76. }
  77. public Asn1OctetStringParser Parser
  78. {
  79. get { return this; }
  80. }
  81. public virtual byte[] GetOctets()
  82. {
  83. return str;
  84. }
  85. protected override int Asn1GetHashCode()
  86. {
  87. return Arrays.GetHashCode(GetOctets());
  88. }
  89. protected override bool Asn1Equals(
  90. Asn1Object asn1Object)
  91. {
  92. DerOctetString other = asn1Object as DerOctetString;
  93. if (other == null)
  94. return false;
  95. return Arrays.AreEqual(GetOctets(), other.GetOctets());
  96. }
  97. public override string ToString()
  98. {
  99. return "#" + Hex.ToHexString(str);
  100. }
  101. }
  102. }
  103. #pragma warning restore
  104. #endif