DerBMPString.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1
  6. {
  7. /**
  8. * Der BMPString object.
  9. */
  10. public class DerBmpString
  11. : DerStringBase
  12. {
  13. private readonly string str;
  14. /**
  15. * return a BMP string from the given object.
  16. *
  17. * @param obj the object we want converted.
  18. * @exception ArgumentException if the object cannot be converted.
  19. */
  20. public static DerBmpString GetInstance(
  21. object obj)
  22. {
  23. if (obj == null || obj is DerBmpString)
  24. {
  25. return (DerBmpString)obj;
  26. }
  27. throw new ArgumentException("illegal object in GetInstance: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  28. }
  29. /**
  30. * return a BMP string from a tagged object.
  31. *
  32. * @param obj the tagged object holding the object we want
  33. * @param explicitly true if the object is meant to be explicitly
  34. * tagged false otherwise.
  35. * @exception ArgumentException if the tagged object cannot
  36. * be converted.
  37. */
  38. public static DerBmpString GetInstance(
  39. Asn1TaggedObject obj,
  40. bool isExplicit)
  41. {
  42. Asn1Object o = obj.GetObject();
  43. if (isExplicit || o is DerBmpString)
  44. {
  45. return GetInstance(o);
  46. }
  47. return new DerBmpString(Asn1OctetString.GetInstance(o).GetOctets());
  48. }
  49. /**
  50. * basic constructor - byte encoded string.
  51. */
  52. public DerBmpString(byte[] str)
  53. {
  54. if (str == null)
  55. throw new ArgumentNullException("str");
  56. int byteLen = str.Length;
  57. if (0 != (byteLen & 1))
  58. throw new ArgumentException("malformed BMPString encoding encountered", "str");
  59. int charLen = byteLen / 2;
  60. char[] cs = new char[charLen];
  61. for (int i = 0; i != charLen; i++)
  62. {
  63. cs[i] = (char)((str[2 * i] << 8) | (str[2 * i + 1] & 0xff));
  64. }
  65. this.str = new string(cs);
  66. }
  67. internal DerBmpString(char[] str)
  68. {
  69. if (str == null)
  70. throw new ArgumentNullException("str");
  71. this.str = new string(str);
  72. }
  73. /**
  74. * basic constructor
  75. */
  76. public DerBmpString(string str)
  77. {
  78. if (str == null)
  79. throw new ArgumentNullException("str");
  80. this.str = str;
  81. }
  82. public override string GetString()
  83. {
  84. return str;
  85. }
  86. protected override bool Asn1Equals(
  87. Asn1Object asn1Object)
  88. {
  89. DerBmpString other = asn1Object as DerBmpString;
  90. if (other == null)
  91. return false;
  92. return this.str.Equals(other.str);
  93. }
  94. internal override int EncodedLength(bool withID)
  95. {
  96. return Asn1OutputStream.GetLengthOfEncodingDL(withID, str.Length * 2);
  97. }
  98. internal override void Encode(Asn1OutputStream asn1Out, bool withID)
  99. {
  100. char[] c = str.ToCharArray();
  101. byte[] b = new byte[c.Length * 2];
  102. for (int i = 0; i != c.Length; i++)
  103. {
  104. b[2 * i] = (byte)(c[i] >> 8);
  105. b[2 * i + 1] = (byte)c[i];
  106. }
  107. asn1Out.WriteEncodingDL(withID, Asn1Tags.BmpString, b);
  108. }
  109. }
  110. }
  111. #pragma warning restore
  112. #endif