DerSet.cs 2.2 KB

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