Asn1Object.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1
  6. {
  7. public abstract class Asn1Object
  8. : Asn1Encodable
  9. {
  10. public override void EncodeTo(Stream output)
  11. {
  12. Asn1OutputStream asn1Out = Asn1OutputStream.Create(output);
  13. GetEncoding(asn1Out.Encoding).Encode(asn1Out);
  14. asn1Out.FlushInternal();
  15. }
  16. public override void EncodeTo(Stream output, string encoding)
  17. {
  18. Asn1OutputStream asn1Out = Asn1OutputStream.Create(output, encoding);
  19. GetEncoding(asn1Out.Encoding).Encode(asn1Out);
  20. asn1Out.FlushInternal();
  21. }
  22. public bool Equals(Asn1Object other)
  23. {
  24. return this == other || Asn1Equals(other);
  25. }
  26. /// <summary>Create a base ASN.1 object from a byte array.</summary>
  27. /// <param name="data">The byte array to parse.</param>
  28. /// <returns>The base ASN.1 object represented by the byte array.</returns>
  29. /// <exception cref="IOException">
  30. /// If there is a problem parsing the data, or parsing an object did not exhaust the available data.
  31. /// </exception>
  32. public static Asn1Object FromByteArray(
  33. byte[] data)
  34. {
  35. try
  36. {
  37. MemoryStream input = new MemoryStream(data, false);
  38. Asn1InputStream asn1 = new Asn1InputStream(input, data.Length);
  39. Asn1Object result = asn1.ReadObject();
  40. if (input.Position != input.Length)
  41. throw new IOException("extra data found after object");
  42. return result;
  43. }
  44. catch (InvalidCastException)
  45. {
  46. throw new IOException("cannot recognise object in byte array");
  47. }
  48. }
  49. /// <summary>Read a base ASN.1 object from a stream.</summary>
  50. /// <param name="inStr">The stream to parse.</param>
  51. /// <returns>The base ASN.1 object represented by the byte array.</returns>
  52. /// <exception cref="IOException">If there is a problem parsing the data.</exception>
  53. public static Asn1Object FromStream(
  54. Stream inStr)
  55. {
  56. try
  57. {
  58. return new Asn1InputStream(inStr).ReadObject();
  59. }
  60. catch (InvalidCastException)
  61. {
  62. throw new IOException("cannot recognise object in stream");
  63. }
  64. }
  65. public sealed override Asn1Object ToAsn1Object()
  66. {
  67. return this;
  68. }
  69. internal abstract IAsn1Encoding GetEncoding(int encoding);
  70. internal abstract IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo);
  71. protected abstract bool Asn1Equals(Asn1Object asn1Object);
  72. protected abstract int Asn1GetHashCode();
  73. internal bool CallAsn1Equals(Asn1Object obj)
  74. {
  75. return Asn1Equals(obj);
  76. }
  77. internal int CallAsn1GetHashCode()
  78. {
  79. return Asn1GetHashCode();
  80. }
  81. }
  82. }
  83. #pragma warning restore
  84. #endif