IetfUtilities.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using System.Text;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
  9. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X500.Style
  10. {
  11. public abstract class IetfUtilities
  12. {
  13. public static string ValueToString(Asn1Encodable value)
  14. {
  15. StringBuilder vBuf = new StringBuilder();
  16. if (value is IAsn1String && !(value is DerUniversalString))
  17. {
  18. string v = ((IAsn1String)value).GetString();
  19. if (v.Length > 0 && v[0] == '#')
  20. {
  21. vBuf.Append('\\');
  22. }
  23. vBuf.Append(v);
  24. }
  25. else
  26. {
  27. try
  28. {
  29. vBuf.Append('#');
  30. vBuf.Append(Hex.ToHexString(value.ToAsn1Object().GetEncoded(Asn1Encodable.Der)));
  31. }
  32. catch (IOException e)
  33. {
  34. throw new ArgumentException("Other value has no encoded form", e);
  35. }
  36. }
  37. int end = vBuf.Length;
  38. int index = 0;
  39. if (vBuf.Length >= 2 && vBuf[0] == '\\' && vBuf[1] == '#')
  40. {
  41. index += 2;
  42. }
  43. while (index != end)
  44. {
  45. switch (vBuf[index])
  46. {
  47. case ',':
  48. case '"':
  49. case '\\':
  50. case '+':
  51. case '=':
  52. case '<':
  53. case '>':
  54. case ';':
  55. {
  56. vBuf.Insert(index, "\\");
  57. index += 2;
  58. ++end;
  59. break;
  60. }
  61. default:
  62. {
  63. ++index;
  64. break;
  65. }
  66. }
  67. }
  68. int start = 0;
  69. if (vBuf.Length > 0)
  70. {
  71. while (vBuf.Length > start && vBuf[start] == ' ')
  72. {
  73. vBuf.Insert(start, "\\");
  74. start += 2;
  75. }
  76. }
  77. int endBuf = vBuf.Length - 1;
  78. while (endBuf >= 0 && vBuf[endBuf] == ' ')
  79. {
  80. vBuf.Insert(endBuf, "\\");
  81. endBuf--;
  82. }
  83. return vBuf.ToString();
  84. }
  85. public static string Canonicalize(string s)
  86. {
  87. string value = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToLowerInvariant(s);
  88. if (value.Length > 0 && value[0] == '#')
  89. {
  90. Asn1Object obj = DecodeObject(value);
  91. if (obj is IAsn1String)
  92. {
  93. value = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToLowerInvariant(((IAsn1String)obj).GetString());
  94. }
  95. }
  96. if (value.Length > 1)
  97. {
  98. int start = 0;
  99. while (start + 1 < value.Length && value[start] == '\\' && value[start + 1] == ' ')
  100. {
  101. start += 2;
  102. }
  103. int end = value.Length - 1;
  104. while (end - 1 > 0 && value[end - 1] == '\\' && value[end] == ' ')
  105. {
  106. end -= 2;
  107. }
  108. if (start > 0 || end < value.Length - 1)
  109. {
  110. value = value.Substring(start, end + 1 - start);
  111. }
  112. }
  113. return StripInternalSpaces(value);
  114. }
  115. public static string CanonicalString(Asn1Encodable value)
  116. {
  117. return Canonicalize(ValueToString(value));
  118. }
  119. private static Asn1Object DecodeObject(string oValue)
  120. {
  121. try
  122. {
  123. return Asn1Object.FromByteArray(Hex.DecodeStrict(oValue, 1, oValue.Length - 1));
  124. }
  125. catch (IOException e)
  126. {
  127. throw new InvalidOperationException("unknown encoding in name: " + e);
  128. }
  129. }
  130. public static string StripInternalSpaces(string str)
  131. {
  132. if (str.IndexOf(" ") < 0)
  133. return str;
  134. StringBuilder res = new StringBuilder();
  135. char c1 = str[0];
  136. res.Append(c1);
  137. for (int k = 1; k < str.Length; k++)
  138. {
  139. char c2 = str[k];
  140. if (!(' ' == c1 && ' ' == c2))
  141. {
  142. res.Append(c2);
  143. c1 = c2;
  144. }
  145. }
  146. return res.ToString();
  147. }
  148. public static bool RdnAreEqual(Rdn rdn1, Rdn rdn2)
  149. {
  150. if (rdn1.Count != rdn2.Count)
  151. return false;
  152. AttributeTypeAndValue[] atvs1 = rdn1.GetTypesAndValues();
  153. AttributeTypeAndValue[] atvs2 = rdn2.GetTypesAndValues();
  154. if (atvs1.Length != atvs2.Length)
  155. return false;
  156. for (int i = 0; i != atvs1.Length; i++)
  157. {
  158. if (!AtvAreEqual(atvs1[i], atvs2[i]))
  159. return false;
  160. }
  161. return true;
  162. }
  163. private static bool AtvAreEqual(AttributeTypeAndValue atv1, AttributeTypeAndValue atv2)
  164. {
  165. if (atv1 == atv2)
  166. return true;
  167. if (null == atv1 || null == atv2)
  168. return false;
  169. DerObjectIdentifier o1 = atv1.Type;
  170. DerObjectIdentifier o2 = atv2.Type;
  171. if (!o1.Equals(o2))
  172. return false;
  173. string v1 = CanonicalString(atv1.Value);
  174. string v2 = CanonicalString(atv2.Value);
  175. if (!v1.Equals(v2))
  176. return false;
  177. return true;
  178. }
  179. }
  180. }
  181. #pragma warning restore
  182. #endif