LazyDERSequence.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1
  6. {
  7. internal class LazyDerSequence
  8. : DerSequence
  9. {
  10. private byte[] encoded;
  11. internal LazyDerSequence(byte[] encoded)
  12. : base()
  13. {
  14. if (null == encoded)
  15. throw new ArgumentNullException("encoded");
  16. this.encoded = encoded;
  17. }
  18. private void Parse()
  19. {
  20. lock (this)
  21. {
  22. if (null != encoded)
  23. {
  24. Asn1InputStream e = new LazyAsn1InputStream(encoded);
  25. Asn1EncodableVector v = e.ReadVector();
  26. this.elements = v.TakeElements();
  27. this.encoded = null;
  28. }
  29. }
  30. }
  31. public override Asn1Encodable this[int index]
  32. {
  33. get
  34. {
  35. Parse();
  36. return base[index];
  37. }
  38. }
  39. public override IEnumerator GetEnumerator()
  40. {
  41. Parse();
  42. return base.GetEnumerator();
  43. }
  44. public override int Count
  45. {
  46. get
  47. {
  48. Parse();
  49. return base.Count;
  50. }
  51. }
  52. internal override int EncodedLength(bool withID)
  53. {
  54. lock (this)
  55. {
  56. if (encoded == null)
  57. {
  58. return base.EncodedLength(withID);
  59. }
  60. else
  61. {
  62. return Asn1OutputStream.GetLengthOfEncodingDL(withID, encoded.Length);
  63. }
  64. }
  65. }
  66. internal override void Encode(Asn1OutputStream asn1Out, bool withID)
  67. {
  68. lock (this)
  69. {
  70. if (encoded == null)
  71. {
  72. base.Encode(asn1Out, withID);
  73. }
  74. else
  75. {
  76. asn1Out.WriteEncodingDL(withID, Asn1Tags.Constructed | Asn1Tags.Sequence, encoded);
  77. }
  78. }
  79. }
  80. }
  81. }
  82. #pragma warning restore
  83. #endif