Asn1OctetString.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 static readonly byte[] EmptyOctets = new byte[0];
  14. internal byte[] str;
  15. /**
  16. * return an Octet string from a tagged object.
  17. *
  18. * @param obj the tagged object holding the object we want.
  19. * @param explicitly true if the object is meant to be explicitly
  20. * tagged false otherwise.
  21. * @exception ArgumentException if the tagged object cannot
  22. * be converted.
  23. */
  24. public static Asn1OctetString GetInstance(
  25. Asn1TaggedObject obj,
  26. bool isExplicit)
  27. {
  28. Asn1Object o = obj.GetObject();
  29. if (isExplicit || o is Asn1OctetString)
  30. {
  31. return GetInstance(o);
  32. }
  33. return BerOctetString.FromSequence(Asn1Sequence.GetInstance(o));
  34. }
  35. /**
  36. * return an Octet string from the given object.
  37. *
  38. * @param obj the object we want converted.
  39. * @exception ArgumentException if the object cannot be converted.
  40. */
  41. public static Asn1OctetString GetInstance(object obj)
  42. {
  43. if (obj == null || obj is Asn1OctetString)
  44. {
  45. return (Asn1OctetString)obj;
  46. }
  47. else if (obj is byte[])
  48. {
  49. try
  50. {
  51. return GetInstance(FromByteArray((byte[])obj));
  52. }
  53. catch (IOException e)
  54. {
  55. throw new ArgumentException("failed to construct OCTET STRING from byte[]: " + e.Message);
  56. }
  57. }
  58. // TODO: this needs to be deleted in V2
  59. else if (obj is Asn1TaggedObject)
  60. {
  61. return GetInstance(((Asn1TaggedObject)obj).GetObject());
  62. }
  63. else if (obj is Asn1Encodable)
  64. {
  65. Asn1Object primitive = ((Asn1Encodable)obj).ToAsn1Object();
  66. if (primitive is Asn1OctetString)
  67. {
  68. return (Asn1OctetString)primitive;
  69. }
  70. }
  71. throw new ArgumentException("illegal object in GetInstance: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  72. }
  73. /**
  74. * @param string the octets making up the octet string.
  75. */
  76. internal Asn1OctetString(
  77. byte[] str)
  78. {
  79. if (str == null)
  80. throw new ArgumentNullException("str");
  81. this.str = str;
  82. }
  83. public Stream GetOctetStream()
  84. {
  85. return new MemoryStream(str, false);
  86. }
  87. public Asn1OctetStringParser Parser
  88. {
  89. get { return this; }
  90. }
  91. public virtual byte[] GetOctets()
  92. {
  93. return str;
  94. }
  95. protected override int Asn1GetHashCode()
  96. {
  97. return Arrays.GetHashCode(GetOctets());
  98. }
  99. protected override bool Asn1Equals(
  100. Asn1Object asn1Object)
  101. {
  102. DerOctetString other = asn1Object as DerOctetString;
  103. if (other == null)
  104. return false;
  105. return Arrays.AreEqual(GetOctets(), other.GetOctets());
  106. }
  107. public override string ToString()
  108. {
  109. return "#" + Hex.ToHexString(str);
  110. }
  111. }
  112. }
  113. #pragma warning restore
  114. #endif