DerSet.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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(Asn1EncodableVector elementVector)
  16. {
  17. return elementVector.Count < 1 ? Empty : new DerSet(elementVector);
  18. }
  19. internal static DerSet FromVector(Asn1EncodableVector elementVector, bool needsSorting)
  20. {
  21. return elementVector.Count < 1 ? Empty : new DerSet(elementVector, needsSorting);
  22. }
  23. /**
  24. * create an empty set
  25. */
  26. public DerSet()
  27. : base()
  28. {
  29. }
  30. /**
  31. * @param obj - a single object that makes up the set.
  32. */
  33. public DerSet(Asn1Encodable element)
  34. : base(element)
  35. {
  36. }
  37. public DerSet(params Asn1Encodable[] elements)
  38. : base(elements)
  39. {
  40. Sort();
  41. }
  42. /**
  43. * @param v - a vector of objects making up the set.
  44. */
  45. public DerSet(Asn1EncodableVector elementVector)
  46. : this(elementVector, true)
  47. {
  48. }
  49. internal DerSet(Asn1EncodableVector elementVector, bool needsSorting)
  50. : base(elementVector)
  51. {
  52. if (needsSorting)
  53. {
  54. Sort();
  55. }
  56. }
  57. internal override int EncodedLength(bool withID)
  58. {
  59. throw BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateNotImplementedException("DerSet.EncodedLength");
  60. }
  61. /*
  62. * A note on the implementation:
  63. * <p>
  64. * As Der requires the constructed, definite-length model to
  65. * be used for structured types, this varies slightly from the
  66. * ASN.1 descriptions given. Rather than just outputing Set,
  67. * we also have to specify Constructed, and the objects length.
  68. */
  69. internal override void Encode(Asn1OutputStream asn1Out, bool withID)
  70. {
  71. if (Count < 1)
  72. {
  73. asn1Out.WriteEncodingDL(withID, Asn1Tags.Constructed | Asn1Tags.Set, Asn1OctetString.EmptyOctets);
  74. return;
  75. }
  76. // TODO Intermediate buffer could be avoided if we could calculate expected length
  77. MemoryStream bOut = new MemoryStream();
  78. Asn1OutputStream dOut = Asn1OutputStream.Create(bOut, Der);
  79. dOut.WriteElements(elements);
  80. dOut.Flush();
  81. #if PORTABLE || NETFX_CORE
  82. byte[] bytes = bOut.ToArray();
  83. int length = bytes.Length;
  84. #else
  85. byte[] bytes = bOut.GetBuffer();
  86. int length = (int)bOut.Position;
  87. #endif
  88. asn1Out.WriteEncodingDL(withID, Asn1Tags.Constructed | Asn1Tags.Set, bytes, 0, length);
  89. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(dOut);
  90. }
  91. }
  92. }
  93. #pragma warning restore
  94. #endif