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