BerOctetString.cs 2.9 KB

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