DerSequence.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1
  8. {
  9. public class DerSequence
  10. : Asn1Sequence
  11. {
  12. public static readonly DerSequence Empty = new DerSequence();
  13. public static DerSequence FromVector(
  14. Asn1EncodableVector v)
  15. {
  16. return v.Count < 1 ? Empty : new DerSequence(v);
  17. }
  18. /**
  19. * create an empty sequence
  20. */
  21. public DerSequence()
  22. : base(0)
  23. {
  24. }
  25. /**
  26. * create a sequence containing one object
  27. */
  28. public DerSequence(
  29. Asn1Encodable obj)
  30. : base(1)
  31. {
  32. AddObject(obj);
  33. }
  34. public DerSequence(
  35. params Asn1Encodable[] v)
  36. : base(v.Length)
  37. {
  38. foreach (Asn1Encodable ae in v)
  39. {
  40. AddObject(ae);
  41. }
  42. }
  43. /**
  44. * create a sequence containing a vector of objects.
  45. */
  46. public DerSequence(
  47. Asn1EncodableVector v)
  48. : base(v.Count)
  49. {
  50. foreach (Asn1Encodable ae in v)
  51. {
  52. AddObject(ae);
  53. }
  54. }
  55. /*
  56. * A note on the implementation:
  57. * <p>
  58. * As Der requires the constructed, definite-length model to
  59. * be used for structured types, this varies slightly from the
  60. * ASN.1 descriptions given. Rather than just outputing Sequence,
  61. * we also have to specify Constructed, and the objects length.
  62. */
  63. internal override void Encode(
  64. DerOutputStream derOut)
  65. {
  66. // TODO Intermediate buffer could be avoided if we could calculate expected length
  67. MemoryStream bOut = new MemoryStream();
  68. DerOutputStream dOut = new DerOutputStream(bOut);
  69. foreach (Asn1Encodable obj in this)
  70. {
  71. dOut.WriteObject(obj);
  72. }
  73. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(dOut);
  74. byte[] bytes = bOut.ToArray();
  75. derOut.WriteEncoded(Asn1Tags.Sequence | Asn1Tags.Constructed, bytes);
  76. }
  77. }
  78. }
  79. #pragma warning restore
  80. #endif