BerOctetString.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1
  8. {
  9. public class BerOctetString
  10. : DerOctetString, IEnumerable
  11. {
  12. public static BerOctetString FromSequence(Asn1Sequence seq)
  13. {
  14. IList v = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  15. foreach (Asn1Encodable obj in seq)
  16. {
  17. v.Add(obj);
  18. }
  19. return new BerOctetString(v);
  20. }
  21. private const int MaxLength = 1000;
  22. /**
  23. * convert a vector of octet strings into a single byte string
  24. */
  25. private static byte[] ToBytes(
  26. IEnumerable octs)
  27. {
  28. MemoryStream bOut = new MemoryStream();
  29. foreach (DerOctetString o in octs)
  30. {
  31. byte[] octets = o.GetOctets();
  32. bOut.Write(octets, 0, octets.Length);
  33. }
  34. return bOut.ToArray();
  35. }
  36. private readonly IEnumerable octs;
  37. /// <param name="str">The octets making up the octet string.</param>
  38. public BerOctetString(
  39. byte[] str)
  40. : base(str)
  41. {
  42. }
  43. public BerOctetString(
  44. IEnumerable octets)
  45. : base(ToBytes(octets))
  46. {
  47. this.octs = octets;
  48. }
  49. public BerOctetString(
  50. Asn1Object obj)
  51. : base(obj)
  52. {
  53. }
  54. public BerOctetString(
  55. Asn1Encodable obj)
  56. : base(obj.ToAsn1Object())
  57. {
  58. }
  59. public override byte[] GetOctets()
  60. {
  61. return str;
  62. }
  63. /**
  64. * return the DER octets that make up this string.
  65. */
  66. public IEnumerator GetEnumerator()
  67. {
  68. if (octs == null)
  69. {
  70. return GenerateOcts().GetEnumerator();
  71. }
  72. return octs.GetEnumerator();
  73. }
  74. [Obsolete("Use GetEnumerator() instead")]
  75. public IEnumerator GetObjects()
  76. {
  77. return GetEnumerator();
  78. }
  79. private IList GenerateOcts()
  80. {
  81. IList vec = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  82. for (int i = 0; i < str.Length; i += MaxLength)
  83. {
  84. int end = System.Math.Min(str.Length, i + MaxLength);
  85. byte[] nStr = new byte[end - i];
  86. Array.Copy(str, i, nStr, 0, nStr.Length);
  87. vec.Add(new DerOctetString(nStr));
  88. }
  89. return vec;
  90. }
  91. internal override void Encode(
  92. DerOutputStream derOut)
  93. {
  94. if (derOut is Asn1OutputStream || derOut is BerOutputStream)
  95. {
  96. derOut.WriteByte(Asn1Tags.Constructed | Asn1Tags.OctetString);
  97. derOut.WriteByte(0x80);
  98. //
  99. // write out the octet array
  100. //
  101. foreach (DerOctetString oct in this)
  102. {
  103. derOut.WriteObject(oct);
  104. }
  105. derOut.WriteByte(0x00);
  106. derOut.WriteByte(0x00);
  107. }
  108. else
  109. {
  110. base.Encode(derOut);
  111. }
  112. }
  113. }
  114. }
  115. #pragma warning restore
  116. #endif