DerSet.cs 2.1 KB

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