Asn1Object.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. public override void EncodeTo(Stream output)
  11. {
  12. Asn1OutputStream.Create(output).WriteObject(this);
  13. }
  14. public override void EncodeTo(Stream output, string encoding)
  15. {
  16. Asn1OutputStream.Create(output, encoding).WriteObject(this);
  17. }
  18. /// <summary>Create a base ASN.1 object from a byte array.</summary>
  19. /// <param name="data">The byte array to parse.</param>
  20. /// <returns>The base ASN.1 object represented by the byte array.</returns>
  21. /// <exception cref="IOException">
  22. /// If there is a problem parsing the data, or parsing an object did not exhaust the available data.
  23. /// </exception>
  24. public static Asn1Object FromByteArray(
  25. byte[] data)
  26. {
  27. try
  28. {
  29. MemoryStream input = new MemoryStream(data, false);
  30. Asn1InputStream asn1 = new Asn1InputStream(input, data.Length);
  31. Asn1Object result = asn1.ReadObject();
  32. if (input.Position != input.Length)
  33. throw new IOException("extra data found after object");
  34. return result;
  35. }
  36. catch (InvalidCastException)
  37. {
  38. throw new IOException("cannot recognise object in byte array");
  39. }
  40. }
  41. /// <summary>Read a base ASN.1 object from a stream.</summary>
  42. /// <param name="inStr">The stream to parse.</param>
  43. /// <returns>The base ASN.1 object represented by the byte array.</returns>
  44. /// <exception cref="IOException">If there is a problem parsing the data.</exception>
  45. public static Asn1Object FromStream(
  46. Stream inStr)
  47. {
  48. try
  49. {
  50. return new Asn1InputStream(inStr).ReadObject();
  51. }
  52. catch (InvalidCastException)
  53. {
  54. throw new IOException("cannot recognise object in stream");
  55. }
  56. }
  57. public sealed override Asn1Object ToAsn1Object()
  58. {
  59. return this;
  60. }
  61. internal abstract int EncodedLength(bool withID);
  62. internal abstract void Encode(Asn1OutputStream asn1Out, bool withID);
  63. protected abstract bool Asn1Equals(Asn1Object asn1Object);
  64. protected abstract int Asn1GetHashCode();
  65. internal bool CallAsn1Equals(Asn1Object obj)
  66. {
  67. return Asn1Equals(obj);
  68. }
  69. internal int CallAsn1GetHashCode()
  70. {
  71. return Asn1GetHashCode();
  72. }
  73. }
  74. }
  75. #pragma warning restore
  76. #endif