BERGenerator.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 abstract 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. protected 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(Asn1Encodable obj)
  29. {
  30. obj.EncodeTo(Out);
  31. }
  32. public override void AddObject(Asn1Object obj)
  33. {
  34. obj.EncodeTo(Out);
  35. }
  36. public override Stream GetRawOutputStream()
  37. {
  38. return Out;
  39. }
  40. public override void Close()
  41. {
  42. WriteBerEnd();
  43. }
  44. private void WriteHdr(
  45. int tag)
  46. {
  47. Out.WriteByte((byte) tag);
  48. Out.WriteByte(0x80);
  49. }
  50. protected void WriteBerHeader(
  51. int tag)
  52. {
  53. if (_tagged)
  54. {
  55. int tagNum = _tagNo | Asn1Tags.ContextSpecific;
  56. if (_isExplicit)
  57. {
  58. WriteHdr(tagNum | Asn1Tags.Constructed);
  59. WriteHdr(tag);
  60. }
  61. else
  62. {
  63. if ((tag & Asn1Tags.Constructed) != 0)
  64. {
  65. WriteHdr(tagNum | Asn1Tags.Constructed);
  66. }
  67. else
  68. {
  69. WriteHdr(tagNum);
  70. }
  71. }
  72. }
  73. else
  74. {
  75. WriteHdr(tag);
  76. }
  77. }
  78. protected void WriteBerBody(
  79. Stream contentStream)
  80. {
  81. Streams.PipeAll(contentStream, Out);
  82. }
  83. protected void WriteBerEnd()
  84. {
  85. Out.WriteByte(0x00);
  86. Out.WriteByte(0x00);
  87. if (_tagged && _isExplicit) // write extra end for tag header
  88. {
  89. Out.WriteByte(0x00);
  90. Out.WriteByte(0x00);
  91. }
  92. }
  93. }
  94. }
  95. #pragma warning restore
  96. #endif