BEROctetStringGenerator.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 DerOutputStream _derOut;
  47. internal BufferedBerOctetStream(
  48. BerOctetStringGenerator gen,
  49. byte[] buf)
  50. {
  51. _gen = gen;
  52. _buf = buf;
  53. _off = 0;
  54. _derOut = new DerOutputStream(_gen.Out);
  55. }
  56. public override void WriteByte(
  57. byte b)
  58. {
  59. _buf[_off++] = b;
  60. if (_off == _buf.Length)
  61. {
  62. DerOctetString.Encode(_derOut, _buf, 0, _off);
  63. _off = 0;
  64. }
  65. }
  66. public override void Write(
  67. byte[] buf,
  68. int offset,
  69. int len)
  70. {
  71. while (len > 0)
  72. {
  73. int numToCopy = System.Math.Min(len, _buf.Length - _off);
  74. if (numToCopy == _buf.Length)
  75. {
  76. DerOctetString.Encode(_derOut, buf, offset, numToCopy);
  77. }
  78. else
  79. {
  80. Array.Copy(buf, offset, _buf, _off, numToCopy);
  81. _off += numToCopy;
  82. if (_off < _buf.Length)
  83. break;
  84. DerOctetString.Encode(_derOut, _buf, 0, _off);
  85. _off = 0;
  86. }
  87. offset += numToCopy;
  88. len -= numToCopy;
  89. }
  90. }
  91. #if PORTABLE || NETFX_CORE
  92. protected override void Dispose(bool disposing)
  93. {
  94. if (disposing)
  95. {
  96. if (_off != 0)
  97. {
  98. DerOctetString.Encode(_derOut, _buf, 0, _off);
  99. }
  100. _gen.WriteBerEnd();
  101. }
  102. base.Dispose(disposing);
  103. }
  104. #else
  105. public override void Close()
  106. {
  107. if (_off != 0)
  108. {
  109. DerOctetString.Encode(_derOut, _buf, 0, _off);
  110. }
  111. _gen.WriteBerEnd();
  112. base.Close();
  113. }
  114. #endif
  115. }
  116. }
  117. }
  118. #pragma warning restore
  119. #endif