Asn1OctetString.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
  7. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1
  8. {
  9. public abstract class Asn1OctetString
  10. : Asn1Object, Asn1OctetStringParser
  11. {
  12. internal class Meta : Asn1UniversalType
  13. {
  14. internal static readonly Asn1UniversalType Instance = new Meta();
  15. private Meta() : base(typeof(Asn1OctetString), Asn1Tags.OctetString) {}
  16. internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString)
  17. {
  18. return octetString;
  19. }
  20. internal override Asn1Object FromImplicitConstructed(Asn1Sequence sequence)
  21. {
  22. return sequence.ToAsn1OctetString();
  23. }
  24. }
  25. internal static readonly byte[] EmptyOctets = new byte[0];
  26. /**
  27. * return an Octet string from the given object.
  28. *
  29. * @param obj the object we want converted.
  30. * @exception ArgumentException if the object cannot be converted.
  31. */
  32. public static Asn1OctetString GetInstance(object obj)
  33. {
  34. if (obj == null)
  35. return null;
  36. if (obj is Asn1OctetString asn1OctetString)
  37. return asn1OctetString;
  38. if (obj is IAsn1Convertible asn1Convertible)
  39. {
  40. Asn1Object asn1Object = asn1Convertible.ToAsn1Object();
  41. if (asn1Object is Asn1OctetString converted)
  42. return converted;
  43. }
  44. else if (obj is byte[] bytes)
  45. {
  46. try
  47. {
  48. return (Asn1OctetString)Meta.Instance.FromByteArray(bytes);
  49. }
  50. catch (IOException e)
  51. {
  52. throw new ArgumentException("failed to construct OCTET STRING from byte[]: " + e.Message);
  53. }
  54. }
  55. throw new ArgumentException("illegal object in GetInstance: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  56. }
  57. /**
  58. * return an octet string from a tagged object.
  59. *
  60. * @param taggedObject the tagged object holding the object we want.
  61. * @param declaredExplicit true if the object is meant to be explicitly tagged false otherwise.
  62. * @exception ArgumentException if the tagged object cannot be converted.
  63. */
  64. public static Asn1OctetString GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
  65. {
  66. return (Asn1OctetString)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit);
  67. }
  68. internal readonly byte[] contents;
  69. /**
  70. * @param string the octets making up the octet string.
  71. */
  72. internal Asn1OctetString(byte[] contents)
  73. {
  74. if (null == contents)
  75. throw new ArgumentNullException("contents");
  76. this.contents = contents;
  77. }
  78. public Stream GetOctetStream()
  79. {
  80. return new MemoryStream(contents, false);
  81. }
  82. public Asn1OctetStringParser Parser
  83. {
  84. get { return this; }
  85. }
  86. public virtual byte[] GetOctets()
  87. {
  88. return contents;
  89. }
  90. protected override int Asn1GetHashCode()
  91. {
  92. return Arrays.GetHashCode(GetOctets());
  93. }
  94. protected override bool Asn1Equals(
  95. Asn1Object asn1Object)
  96. {
  97. DerOctetString other = asn1Object as DerOctetString;
  98. if (other == null)
  99. return false;
  100. return Arrays.AreEqual(GetOctets(), other.GetOctets());
  101. }
  102. public override string ToString()
  103. {
  104. return "#" + Hex.ToHexString(contents);
  105. }
  106. internal static Asn1OctetString CreatePrimitive(byte[] contents)
  107. {
  108. return new DerOctetString(contents);
  109. }
  110. }
  111. }
  112. #pragma warning restore
  113. #endif