BERGenerator.cs 2.3 KB

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