BEROctetStringGenerator.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1
  7. {
  8. public class BerOctetStringGenerator
  9. : BerGenerator
  10. {
  11. public BerOctetStringGenerator(Stream outStream)
  12. : base(outStream)
  13. {
  14. WriteBerHeader(Asn1Tags.Constructed | Asn1Tags.OctetString);
  15. }
  16. public BerOctetStringGenerator(
  17. Stream outStream,
  18. int tagNo,
  19. bool isExplicit)
  20. : base(outStream, tagNo, isExplicit)
  21. {
  22. WriteBerHeader(Asn1Tags.Constructed | Asn1Tags.OctetString);
  23. }
  24. public Stream GetOctetOutputStream()
  25. {
  26. return GetOctetOutputStream(new byte[1000]); // limit for CER encoding.
  27. }
  28. public Stream GetOctetOutputStream(
  29. int bufSize)
  30. {
  31. return bufSize < 1
  32. ? GetOctetOutputStream()
  33. : GetOctetOutputStream(new byte[bufSize]);
  34. }
  35. public Stream GetOctetOutputStream(
  36. byte[] buf)
  37. {
  38. return new BufferedBerOctetStream(this, buf);
  39. }
  40. private class BufferedBerOctetStream
  41. : BaseOutputStream
  42. {
  43. private byte[] _buf;
  44. private int _off;
  45. private readonly BerOctetStringGenerator _gen;
  46. private readonly Asn1OutputStream _derOut;
  47. internal BufferedBerOctetStream(
  48. BerOctetStringGenerator gen,
  49. byte[] buf)
  50. {
  51. _gen = gen;
  52. _buf = buf;
  53. _off = 0;
  54. _derOut = Asn1OutputStream.Create(_gen.Out, Asn1Encodable.Der);
  55. }
  56. public override void WriteByte(
  57. byte b)
  58. {
  59. _buf[_off++] = b;
  60. if (_off == _buf.Length)
  61. {
  62. DerOctetString.Encode(_derOut, true, _buf, 0, _off);
  63. _off = 0;
  64. }
  65. }
  66. public override void Write(byte[] b, int off, int len)
  67. {
  68. int bufLen = _buf.Length;
  69. int available = bufLen - _off;
  70. if (len < available)
  71. {
  72. Array.Copy(b, off, _buf, _off, len);
  73. _off += len;
  74. return;
  75. }
  76. int count = 0;
  77. if (_off > 0)
  78. {
  79. Array.Copy(b, off, _buf, _off, available);
  80. count += available;
  81. DerOctetString.Encode(_derOut, true, _buf, 0, bufLen);
  82. }
  83. int remaining;
  84. while ((remaining = len - count) >= bufLen)
  85. {
  86. DerOctetString.Encode(_derOut, true, b, off + count, bufLen);
  87. count += bufLen;
  88. }
  89. Array.Copy(b, off + count, _buf, 0, remaining);
  90. this._off = remaining;
  91. }
  92. #if PORTABLE || NETFX_CORE
  93. protected override void Dispose(bool disposing)
  94. {
  95. if (disposing)
  96. {
  97. if (_off != 0)
  98. {
  99. DerOctetString.Encode(_derOut, true, _buf, 0, _off);
  100. }
  101. _derOut.FlushInternal();
  102. _gen.WriteBerEnd();
  103. }
  104. base.Dispose(disposing);
  105. }
  106. #else
  107. public override void Close()
  108. {
  109. if (_off != 0)
  110. {
  111. DerOctetString.Encode(_derOut, true, _buf, 0, _off);
  112. }
  113. _derOut.FlushInternal();
  114. _gen.WriteBerEnd();
  115. base.Close();
  116. }
  117. #endif
  118. }
  119. }
  120. }
  121. #pragma warning restore
  122. #endif