BERGenerator.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System.IO;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1
  6. {
  7. public class BerGenerator
  8. : Asn1Generator
  9. {
  10. private bool _tagged = false;
  11. private bool _isExplicit;
  12. private int _tagNo;
  13. protected BerGenerator(
  14. Stream outStream)
  15. : base(outStream)
  16. {
  17. }
  18. public BerGenerator(
  19. Stream outStream,
  20. int tagNo,
  21. bool isExplicit)
  22. : base(outStream)
  23. {
  24. _tagged = true;
  25. _isExplicit = isExplicit;
  26. _tagNo = tagNo;
  27. }
  28. public override void AddObject(
  29. Asn1Encodable obj)
  30. {
  31. new BerOutputStream(Out).WriteObject(obj);
  32. }
  33. public override Stream GetRawOutputStream()
  34. {
  35. return Out;
  36. }
  37. public override void Close()
  38. {
  39. WriteBerEnd();
  40. }
  41. private void WriteHdr(
  42. int tag)
  43. {
  44. Out.WriteByte((byte) tag);
  45. Out.WriteByte(0x80);
  46. }
  47. protected void WriteBerHeader(
  48. int tag)
  49. {
  50. if (_tagged)
  51. {
  52. int tagNum = _tagNo | Asn1Tags.Tagged;
  53. if (_isExplicit)
  54. {
  55. WriteHdr(tagNum | Asn1Tags.Constructed);
  56. WriteHdr(tag);
  57. }
  58. else
  59. {
  60. if ((tag & Asn1Tags.Constructed) != 0)
  61. {
  62. WriteHdr(tagNum | Asn1Tags.Constructed);
  63. }
  64. else
  65. {
  66. WriteHdr(tagNum);
  67. }
  68. }
  69. }
  70. else
  71. {
  72. WriteHdr(tag);
  73. }
  74. }
  75. protected void WriteBerBody(
  76. Stream contentStream)
  77. {
  78. Streams.PipeAll(contentStream, Out);
  79. }
  80. protected void WriteBerEnd()
  81. {
  82. Out.WriteByte(0x00);
  83. Out.WriteByte(0x00);
  84. if (_tagged && _isExplicit) // write extra end for tag header
  85. {
  86. Out.WriteByte(0x00);
  87. Out.WriteByte(0x00);
  88. }
  89. }
  90. }
  91. }
  92. #pragma warning restore
  93. #endif