Asn1Null.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1
  7. {
  8. /**
  9. * A Null object.
  10. */
  11. public abstract class Asn1Null
  12. : Asn1Object
  13. {
  14. internal class Meta : Asn1UniversalType
  15. {
  16. internal static readonly Asn1UniversalType Instance = new Meta();
  17. private Meta() : base(typeof(Asn1Null), Asn1Tags.Null) {}
  18. internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString)
  19. {
  20. return CreatePrimitive(octetString.GetOctets());
  21. }
  22. }
  23. public static Asn1Null GetInstance(object obj)
  24. {
  25. if (obj == null)
  26. return null;
  27. if (obj is Asn1Null asn1Null)
  28. return asn1Null;
  29. if (obj is IAsn1Convertible asn1Convertible)
  30. {
  31. Asn1Object asn1Object = asn1Convertible.ToAsn1Object();
  32. if (asn1Object is Asn1Null converted)
  33. return converted;
  34. }
  35. else if (obj is byte[] bytes)
  36. {
  37. try
  38. {
  39. return (Asn1Null)Meta.Instance.FromByteArray(bytes);
  40. }
  41. catch (IOException e)
  42. {
  43. throw new ArgumentException("failed to construct NULL from byte[]: " + e.Message);
  44. }
  45. }
  46. throw new ArgumentException("illegal object in GetInstance: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  47. }
  48. public static Asn1Null GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
  49. {
  50. return (Asn1Null)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit);
  51. }
  52. internal Asn1Null()
  53. {
  54. }
  55. public override string ToString()
  56. {
  57. return "NULL";
  58. }
  59. internal static Asn1Null CreatePrimitive(byte[] contents)
  60. {
  61. if (0 != contents.Length)
  62. throw new InvalidOperationException("malformed NULL encoding encountered");
  63. return DerNull.Instance;
  64. }
  65. }
  66. }
  67. #pragma warning restore
  68. #endif