Asn1Object.cs 2.0 KB

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