Asn1Object.cs 2.1 KB

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