Asn1OutputStream.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1
  6. {
  7. public class Asn1OutputStream
  8. : DerOutputStream
  9. {
  10. public static Asn1OutputStream Create(Stream output)
  11. {
  12. return new Asn1OutputStream(output);
  13. }
  14. public static Asn1OutputStream Create(Stream output, string encoding)
  15. {
  16. if (Asn1Encodable.Der.Equals(encoding))
  17. {
  18. return new DerOutputStreamNew(output);
  19. }
  20. else
  21. {
  22. return new Asn1OutputStream(output);
  23. }
  24. }
  25. public Asn1OutputStream(Stream os)
  26. : base(os)
  27. {
  28. }
  29. public override void WriteObject(Asn1Encodable encodable)
  30. {
  31. if (null == encodable)
  32. throw new IOException("null object detected");
  33. WritePrimitive(encodable.ToAsn1Object(), true);
  34. FlushInternal();
  35. }
  36. public override void WriteObject(Asn1Object primitive)
  37. {
  38. if (null == primitive)
  39. throw new IOException("null object detected");
  40. WritePrimitive(primitive, true);
  41. FlushInternal();
  42. }
  43. internal void FlushInternal()
  44. {
  45. // Placeholder to support future internal buffering
  46. }
  47. internal virtual bool IsBer
  48. {
  49. get { return true; }
  50. }
  51. internal void WriteDL(int length)
  52. {
  53. if (length < 128)
  54. {
  55. WriteByte((byte)length);
  56. }
  57. else
  58. {
  59. byte[] stack = new byte[5];
  60. int pos = stack.Length;
  61. do
  62. {
  63. stack[--pos] = (byte)length;
  64. length >>= 8;
  65. }
  66. while (length > 0);
  67. int count = stack.Length - pos;
  68. stack[--pos] = (byte)(0x80 | count);
  69. Write(stack, pos, count + 1);
  70. }
  71. }
  72. internal virtual void WriteElements(Asn1Encodable[] elements)
  73. {
  74. for (int i = 0, count = elements.Length; i < count; ++i)
  75. {
  76. elements[i].ToAsn1Object().Encode(this, true);
  77. }
  78. }
  79. internal void WriteEncodingDL(bool withID, int identifier, byte contents)
  80. {
  81. WriteIdentifier(withID, identifier);
  82. WriteDL(1);
  83. WriteByte(contents);
  84. }
  85. internal void WriteEncodingDL(bool withID, int identifier, byte[] contents)
  86. {
  87. WriteIdentifier(withID, identifier);
  88. WriteDL(contents.Length);
  89. Write(contents, 0, contents.Length);
  90. }
  91. internal void WriteEncodingDL(bool withID, int identifier, byte[] contents, int contentsOff, int contentsLen)
  92. {
  93. WriteIdentifier(withID, identifier);
  94. WriteDL(contentsLen);
  95. Write(contents, contentsOff, contentsLen);
  96. }
  97. internal void WriteEncodingDL(bool withID, int identifier, byte contentsPrefix, byte[] contents,
  98. int contentsOff, int contentsLen)
  99. {
  100. WriteIdentifier(withID, identifier);
  101. WriteDL(1 + contentsLen);
  102. WriteByte(contentsPrefix);
  103. Write(contents, contentsOff, contentsLen);
  104. }
  105. internal void WriteEncodingDL(bool withID, int identifier, byte[] contents, int contentsOff, int contentsLen,
  106. byte contentsSuffix)
  107. {
  108. WriteIdentifier(withID, identifier);
  109. WriteDL(contentsLen + 1);
  110. Write(contents, contentsOff, contentsLen);
  111. WriteByte(contentsSuffix);
  112. }
  113. internal void WriteEncodingDL(bool withID, int flags, int tag, byte[] contents)
  114. {
  115. WriteIdentifier(withID, flags, tag);
  116. WriteDL(contents.Length);
  117. Write(contents, 0, contents.Length);
  118. }
  119. internal void WriteEncodingIL(bool withID, int identifier, Asn1Encodable[] elements)
  120. {
  121. WriteIdentifier(withID, identifier);
  122. WriteByte(0x80);
  123. WriteElements(elements);
  124. WriteByte(0x00);
  125. WriteByte(0x00);
  126. }
  127. internal void WriteIdentifier(bool withID, int identifier)
  128. {
  129. if (withID)
  130. {
  131. WriteByte((byte)identifier);
  132. }
  133. }
  134. internal void WriteIdentifier(bool withID, int flags, int tag)
  135. {
  136. if (!withID)
  137. {
  138. // Don't write the identifier
  139. }
  140. else if (tag < 31)
  141. {
  142. WriteByte((byte)(flags | tag));
  143. }
  144. else
  145. {
  146. byte[] stack = new byte[6];
  147. int pos = stack.Length;
  148. stack[--pos] = (byte)(tag & 0x7F);
  149. while (tag > 127)
  150. {
  151. tag >>= 7;
  152. stack[--pos] = (byte)(tag & 0x7F | 0x80);
  153. }
  154. stack[--pos] = (byte)(flags | 0x1F);
  155. Write(stack, pos, stack.Length - pos);
  156. }
  157. }
  158. internal virtual void WritePrimitive(Asn1Object primitive, bool withID)
  159. {
  160. primitive.Encode(this, withID);
  161. }
  162. internal virtual void WritePrimitives(Asn1Object[] primitives)
  163. {
  164. for (int i = 0, count = primitives.Length; i < count; ++i)
  165. {
  166. WritePrimitive(primitives[i], true);
  167. }
  168. }
  169. internal static int GetLengthOfDL(int dl)
  170. {
  171. if (dl < 128)
  172. return 1;
  173. int length = 2;
  174. while ((dl >>= 8) > 0)
  175. {
  176. ++length;
  177. }
  178. return length;
  179. }
  180. internal static int GetLengthOfEncodingDL(bool withID, int contentsLength)
  181. {
  182. return (withID ? 1 : 0) + GetLengthOfDL(contentsLength) + contentsLength;
  183. }
  184. internal static int GetLengthOfEncodingDL(bool withID, int tag, int contentsLength)
  185. {
  186. return (withID ? GetLengthOfIdentifier(tag) : 0) + GetLengthOfDL(contentsLength) + contentsLength;
  187. }
  188. internal static int GetLengthOfIdentifier(int tag)
  189. {
  190. if (tag < 31)
  191. return 1;
  192. int length = 2;
  193. while ((tag >>= 7) > 0)
  194. {
  195. ++length;
  196. }
  197. return length;
  198. }
  199. }
  200. }
  201. #pragma warning restore
  202. #endif