DerGraphicString.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. public class DerGraphicString
  9. : DerStringBase
  10. {
  11. internal class Meta : Asn1UniversalType
  12. {
  13. internal static readonly Asn1UniversalType Instance = new Meta();
  14. private Meta() : base(typeof(DerGraphicString), Asn1Tags.GraphicString) {}
  15. internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString)
  16. {
  17. return CreatePrimitive(octetString.GetOctets());
  18. }
  19. }
  20. /**
  21. * return a Graphic String from the passed in object
  22. *
  23. * @param obj a DerGraphicString or an object that can be converted into one.
  24. * @exception IllegalArgumentException if the object cannot be converted.
  25. * @return a DerGraphicString instance, or null.
  26. */
  27. public static DerGraphicString GetInstance(object obj)
  28. {
  29. if (obj == null)
  30. return null;
  31. if (obj is DerGraphicString derGraphicString)
  32. return derGraphicString;
  33. if (obj is IAsn1Convertible asn1Convertible)
  34. {
  35. Asn1Object asn1Object = asn1Convertible.ToAsn1Object();
  36. if (asn1Object is DerGraphicString converted)
  37. return converted;
  38. }
  39. else if (obj is byte[] bytes)
  40. {
  41. try
  42. {
  43. return (DerGraphicString)Meta.Instance.FromByteArray(bytes);
  44. }
  45. catch (IOException e)
  46. {
  47. throw new ArgumentException("failed to construct graphic string from byte[]: " + e.Message);
  48. }
  49. }
  50. throw new ArgumentException("illegal object in GetInstance: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  51. }
  52. /**
  53. * return a Graphic String from a tagged object.
  54. *
  55. * @param taggedObject the tagged object holding the object we want
  56. * @param declaredExplicit true if the object is meant to be explicitly tagged false otherwise.
  57. * @exception IllegalArgumentException if the tagged object cannot be converted.
  58. * @return a DerGraphicString instance, or null.
  59. */
  60. public static DerGraphicString GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
  61. {
  62. return (DerGraphicString)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit);
  63. }
  64. private readonly byte[] m_contents;
  65. public DerGraphicString(byte[] contents)
  66. : this(contents, true)
  67. {
  68. }
  69. internal DerGraphicString(byte[] contents, bool clone)
  70. {
  71. if (null == contents)
  72. throw new ArgumentNullException("contents");
  73. this.m_contents = clone ? Arrays.Clone(contents) : contents;
  74. }
  75. public override string GetString()
  76. {
  77. return Strings.FromByteArray(m_contents);
  78. }
  79. public byte[] GetOctets()
  80. {
  81. return Arrays.Clone(m_contents);
  82. }
  83. internal override IAsn1Encoding GetEncoding(int encoding)
  84. {
  85. return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.GraphicString, m_contents);
  86. }
  87. internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo)
  88. {
  89. return new PrimitiveEncoding(tagClass, tagNo, m_contents);
  90. }
  91. protected override int Asn1GetHashCode()
  92. {
  93. return Arrays.GetHashCode(m_contents);
  94. }
  95. protected override bool Asn1Equals(Asn1Object asn1Object)
  96. {
  97. DerGraphicString that = asn1Object as DerGraphicString;
  98. return null != that
  99. && Arrays.AreEqual(this.m_contents, that.m_contents);
  100. }
  101. internal static DerGraphicString CreatePrimitive(byte[] contents)
  102. {
  103. return new DerGraphicString(contents, false);
  104. }
  105. }
  106. }
  107. #pragma warning restore
  108. #endif