DerSequence.cs 1.8 KB

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