Asn1RelativeOid.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using System.Text;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1
  9. {
  10. public class Asn1RelativeOid
  11. : Asn1Object
  12. {
  13. internal class Meta : Asn1UniversalType
  14. {
  15. internal static readonly Asn1UniversalType Instance = new Meta();
  16. private Meta() : base(typeof(Asn1RelativeOid), Asn1Tags.RelativeOid) {}
  17. internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString)
  18. {
  19. return CreatePrimitive(octetString.GetOctets(), false);
  20. }
  21. }
  22. public static Asn1RelativeOid FromContents(byte[] contents)
  23. {
  24. return CreatePrimitive(contents, true);
  25. }
  26. public static Asn1RelativeOid GetInstance(object obj)
  27. {
  28. if (obj == null)
  29. return null;
  30. if (obj is Asn1RelativeOid asn1RelativeOid)
  31. return asn1RelativeOid;
  32. if (obj is IAsn1Convertible asn1Convertible)
  33. {
  34. Asn1Object asn1Object = asn1Convertible.ToAsn1Object();
  35. if (asn1Object is Asn1RelativeOid converted)
  36. return converted;
  37. }
  38. else if (obj is byte[] bytes)
  39. {
  40. try
  41. {
  42. return (Asn1RelativeOid)FromByteArray(bytes);
  43. }
  44. catch (IOException e)
  45. {
  46. throw new ArgumentException("failed to construct relative OID from byte[]: " + e.Message);
  47. }
  48. }
  49. throw new ArgumentException("illegal object in GetInstance: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  50. }
  51. public static Asn1RelativeOid GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
  52. {
  53. return (Asn1RelativeOid)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit);
  54. }
  55. private const long LongLimit = (long.MaxValue >> 7) - 0x7F;
  56. private readonly string identifier;
  57. private byte[] contents;
  58. public Asn1RelativeOid(string identifier)
  59. {
  60. if (identifier == null)
  61. throw new ArgumentNullException("identifier");
  62. if (!IsValidIdentifier(identifier, 0))
  63. throw new FormatException("string " + identifier + " not a relative OID");
  64. this.identifier = identifier;
  65. }
  66. private Asn1RelativeOid(Asn1RelativeOid oid, string branchID)
  67. {
  68. if (!IsValidIdentifier(branchID, 0))
  69. throw new FormatException("string " + branchID + " not a valid relative OID branch");
  70. this.identifier = oid.Id + "." + branchID;
  71. }
  72. private Asn1RelativeOid(byte[] contents, bool clone)
  73. {
  74. this.identifier = ParseContents(contents);
  75. this.contents = clone ? Arrays.Clone(contents) : contents;
  76. }
  77. public virtual Asn1RelativeOid Branch(string branchID)
  78. {
  79. return new Asn1RelativeOid(this, branchID);
  80. }
  81. public string Id
  82. {
  83. get { return identifier; }
  84. }
  85. public override string ToString()
  86. {
  87. return identifier;
  88. }
  89. protected override bool Asn1Equals(Asn1Object asn1Object)
  90. {
  91. Asn1RelativeOid that = asn1Object as Asn1RelativeOid;
  92. return null != that
  93. && this.identifier == that.identifier;
  94. }
  95. protected override int Asn1GetHashCode()
  96. {
  97. return identifier.GetHashCode();
  98. }
  99. internal override IAsn1Encoding GetEncoding(int encoding)
  100. {
  101. return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.RelativeOid, GetContents());
  102. }
  103. internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo)
  104. {
  105. return new PrimitiveEncoding(tagClass, tagNo, GetContents());
  106. }
  107. private void DoOutput(MemoryStream bOut)
  108. {
  109. OidTokenizer tok = new OidTokenizer(identifier);
  110. while (tok.HasMoreTokens)
  111. {
  112. string token = tok.NextToken();
  113. if (token.Length <= 18)
  114. {
  115. WriteField(bOut, long.Parse(token));
  116. }
  117. else
  118. {
  119. WriteField(bOut, new BigInteger(token));
  120. }
  121. }
  122. }
  123. private byte[] GetContents()
  124. {
  125. lock (this)
  126. {
  127. if (contents == null)
  128. {
  129. MemoryStream bOut = new MemoryStream();
  130. DoOutput(bOut);
  131. contents = bOut.ToArray();
  132. }
  133. return contents;
  134. }
  135. }
  136. internal static Asn1RelativeOid CreatePrimitive(byte[] contents, bool clone)
  137. {
  138. return new Asn1RelativeOid(contents, clone);
  139. }
  140. internal static bool IsValidIdentifier(string identifier, int from)
  141. {
  142. int digitCount = 0;
  143. int pos = identifier.Length;
  144. while (--pos >= from)
  145. {
  146. char ch = identifier[pos];
  147. if (ch == '.')
  148. {
  149. if (0 == digitCount || (digitCount > 1 && identifier[pos + 1] == '0'))
  150. return false;
  151. digitCount = 0;
  152. }
  153. else if ('0' <= ch && ch <= '9')
  154. {
  155. ++digitCount;
  156. }
  157. else
  158. {
  159. return false;
  160. }
  161. }
  162. if (0 == digitCount || (digitCount > 1 && identifier[pos + 1] == '0'))
  163. return false;
  164. return true;
  165. }
  166. internal static void WriteField(Stream outputStream, long fieldValue)
  167. {
  168. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  169. Span<byte> result = stackalloc byte[9];
  170. #else
  171. byte[] result = new byte[9];
  172. #endif
  173. int pos = 8;
  174. result[pos] = (byte)((int)fieldValue & 0x7F);
  175. while (fieldValue >= (1L << 7))
  176. {
  177. fieldValue >>= 7;
  178. result[--pos] = (byte)((int)fieldValue | 0x80);
  179. }
  180. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  181. outputStream.Write(result[pos..]);
  182. #else
  183. outputStream.Write(result, pos, 9 - pos);
  184. #endif
  185. }
  186. internal static void WriteField(Stream outputStream, BigInteger fieldValue)
  187. {
  188. int byteCount = (fieldValue.BitLength + 6) / 7;
  189. if (byteCount == 0)
  190. {
  191. outputStream.WriteByte(0);
  192. }
  193. else
  194. {
  195. BigInteger tmpValue = fieldValue;
  196. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  197. Span<byte> tmp = byteCount <= 16
  198. ? stackalloc byte[byteCount]
  199. : new byte[byteCount];
  200. #else
  201. byte[] tmp = new byte[byteCount];
  202. #endif
  203. for (int i = byteCount - 1; i >= 0; i--)
  204. {
  205. tmp[i] = (byte)(tmpValue.IntValue | 0x80);
  206. tmpValue = tmpValue.ShiftRight(7);
  207. }
  208. tmp[byteCount - 1] &= 0x7F;
  209. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  210. outputStream.Write(tmp);
  211. #else
  212. outputStream.Write(tmp, 0, tmp.Length);
  213. #endif
  214. }
  215. }
  216. private static string ParseContents(byte[] contents)
  217. {
  218. StringBuilder objId = new StringBuilder();
  219. long value = 0;
  220. BigInteger bigValue = null;
  221. bool first = true;
  222. for (int i = 0; i != contents.Length; i++)
  223. {
  224. int b = contents[i];
  225. if (value <= LongLimit)
  226. {
  227. value += b & 0x7F;
  228. if ((b & 0x80) == 0)
  229. {
  230. if (first)
  231. {
  232. first = false;
  233. }
  234. else
  235. {
  236. objId.Append('.');
  237. }
  238. objId.Append(value);
  239. value = 0;
  240. }
  241. else
  242. {
  243. value <<= 7;
  244. }
  245. }
  246. else
  247. {
  248. if (bigValue == null)
  249. {
  250. bigValue = BigInteger.ValueOf(value);
  251. }
  252. bigValue = bigValue.Or(BigInteger.ValueOf(b & 0x7F));
  253. if ((b & 0x80) == 0)
  254. {
  255. if (first)
  256. {
  257. first = false;
  258. }
  259. else
  260. {
  261. objId.Append('.');
  262. }
  263. objId.Append(bigValue);
  264. bigValue = null;
  265. value = 0;
  266. }
  267. else
  268. {
  269. bigValue = bigValue.ShiftLeft(7);
  270. }
  271. }
  272. }
  273. return objId.ToString();
  274. }
  275. }
  276. }
  277. #pragma warning restore
  278. #endif