DerObjectIdentifier.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 DerObjectIdentifier
  11. : Asn1Object
  12. {
  13. internal class Meta : Asn1UniversalType
  14. {
  15. internal static readonly Asn1UniversalType Instance = new Meta();
  16. private Meta() : base(typeof(DerObjectIdentifier), Asn1Tags.ObjectIdentifier) {}
  17. internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString)
  18. {
  19. return CreatePrimitive(octetString.GetOctets(), false);
  20. }
  21. }
  22. public static DerObjectIdentifier FromContents(byte[] contents)
  23. {
  24. return CreatePrimitive(contents, true);
  25. }
  26. /**
  27. * return an OID from the passed in object
  28. *
  29. * @exception ArgumentException if the object cannot be converted.
  30. */
  31. public static DerObjectIdentifier GetInstance(object obj)
  32. {
  33. if (obj == null)
  34. return null;
  35. if (obj is DerObjectIdentifier derObjectIdentifier)
  36. return derObjectIdentifier;
  37. if (obj is IAsn1Convertible asn1Convertible)
  38. {
  39. Asn1Object asn1Object = asn1Convertible.ToAsn1Object();
  40. if (asn1Object is DerObjectIdentifier converted)
  41. return converted;
  42. }
  43. else if (obj is byte[] bytes)
  44. {
  45. try
  46. {
  47. return (DerObjectIdentifier)Meta.Instance.FromByteArray(bytes);
  48. }
  49. catch (IOException e)
  50. {
  51. throw new ArgumentException("failed to construct object identifier from byte[]: " + e.Message);
  52. }
  53. }
  54. throw new ArgumentException("illegal object in GetInstance: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  55. }
  56. public static DerObjectIdentifier GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
  57. {
  58. /*
  59. * TODO[asn1] This block here is for backward compatibility, but should eventually be removed.
  60. *
  61. * - see https://github.com/bcgit/bc-java/issues/1015
  62. */
  63. if (!declaredExplicit && !taggedObject.IsParsed())
  64. {
  65. Asn1Object baseObject = taggedObject.GetObject();
  66. if (!(baseObject is DerObjectIdentifier))
  67. return FromContents(Asn1OctetString.GetInstance(baseObject).GetOctets());
  68. }
  69. return (DerObjectIdentifier)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit);
  70. }
  71. private const long LongLimit = (long.MaxValue >> 7) - 0x7F;
  72. private static readonly DerObjectIdentifier[] cache = new DerObjectIdentifier[1024];
  73. private readonly string identifier;
  74. private byte[] contents;
  75. public DerObjectIdentifier(string identifier)
  76. {
  77. if (identifier == null)
  78. throw new ArgumentNullException("identifier");
  79. if (!IsValidIdentifier(identifier))
  80. throw new FormatException("string " + identifier + " not an OID");
  81. this.identifier = identifier;
  82. }
  83. private DerObjectIdentifier(DerObjectIdentifier oid, string branchID)
  84. {
  85. if (!Asn1RelativeOid.IsValidIdentifier(branchID, 0))
  86. throw new ArgumentException("string " + branchID + " not a valid OID branch", "branchID");
  87. this.identifier = oid.Id + "." + branchID;
  88. }
  89. private DerObjectIdentifier(byte[] contents, bool clone)
  90. {
  91. this.identifier = ParseContents(contents);
  92. this.contents = clone ? Arrays.Clone(contents) : contents;
  93. }
  94. public virtual DerObjectIdentifier Branch(string branchID)
  95. {
  96. return new DerObjectIdentifier(this, branchID);
  97. }
  98. public string Id
  99. {
  100. get { return identifier; }
  101. }
  102. /**
  103. * Return true if this oid is an extension of the passed in branch, stem.
  104. * @param stem the arc or branch that is a possible parent.
  105. * @return true if the branch is on the passed in stem, false otherwise.
  106. */
  107. public virtual bool On(DerObjectIdentifier stem)
  108. {
  109. string id = Id, stemId = stem.Id;
  110. return id.Length > stemId.Length && id[stemId.Length] == '.' && Org.BouncyCastle.Utilities.Platform.StartsWith(id, stemId);
  111. }
  112. public override string ToString()
  113. {
  114. return identifier;
  115. }
  116. protected override bool Asn1Equals(Asn1Object asn1Object)
  117. {
  118. DerObjectIdentifier that = asn1Object as DerObjectIdentifier;
  119. return null != that
  120. && this.identifier == that.identifier;
  121. }
  122. protected override int Asn1GetHashCode()
  123. {
  124. return identifier.GetHashCode();
  125. }
  126. internal override IAsn1Encoding GetEncoding(int encoding)
  127. {
  128. return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.ObjectIdentifier, GetContents());
  129. }
  130. internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo)
  131. {
  132. return new PrimitiveEncoding(tagClass, tagNo, GetContents());
  133. }
  134. private void DoOutput(MemoryStream bOut)
  135. {
  136. OidTokenizer tok = new OidTokenizer(identifier);
  137. string token = tok.NextToken();
  138. int first = int.Parse(token) * 40;
  139. token = tok.NextToken();
  140. if (token.Length <= 18)
  141. {
  142. Asn1RelativeOid.WriteField(bOut, first + long.Parse(token));
  143. }
  144. else
  145. {
  146. Asn1RelativeOid.WriteField(bOut, new BigInteger(token).Add(BigInteger.ValueOf(first)));
  147. }
  148. while (tok.HasMoreTokens)
  149. {
  150. token = tok.NextToken();
  151. if (token.Length <= 18)
  152. {
  153. Asn1RelativeOid.WriteField(bOut, long.Parse(token));
  154. }
  155. else
  156. {
  157. Asn1RelativeOid.WriteField(bOut, new BigInteger(token));
  158. }
  159. }
  160. }
  161. private byte[] GetContents()
  162. {
  163. lock (this)
  164. {
  165. if (contents == null)
  166. {
  167. MemoryStream bOut = new MemoryStream();
  168. DoOutput(bOut);
  169. contents = bOut.ToArray();
  170. }
  171. return contents;
  172. }
  173. }
  174. internal static DerObjectIdentifier CreatePrimitive(byte[] contents, bool clone)
  175. {
  176. int hashCode = Arrays.GetHashCode(contents);
  177. int first = hashCode & 1023;
  178. lock (cache)
  179. {
  180. DerObjectIdentifier entry = cache[first];
  181. if (entry != null && Arrays.AreEqual(contents, entry.GetContents()))
  182. {
  183. return entry;
  184. }
  185. return cache[first] = new DerObjectIdentifier(contents, clone);
  186. }
  187. }
  188. private static bool IsValidIdentifier(string identifier)
  189. {
  190. if (identifier.Length < 3 || identifier[1] != '.')
  191. return false;
  192. char first = identifier[0];
  193. if (first < '0' || first > '2')
  194. return false;
  195. return Asn1RelativeOid.IsValidIdentifier(identifier, 2);
  196. }
  197. private static string ParseContents(byte[] contents)
  198. {
  199. StringBuilder objId = new StringBuilder();
  200. long value = 0;
  201. BigInteger bigValue = null;
  202. bool first = true;
  203. for (int i = 0; i != contents.Length; i++)
  204. {
  205. int b = contents[i];
  206. if (value <= LongLimit)
  207. {
  208. value += b & 0x7F;
  209. if ((b & 0x80) == 0)
  210. {
  211. if (first)
  212. {
  213. if (value < 40)
  214. {
  215. objId.Append('0');
  216. }
  217. else if (value < 80)
  218. {
  219. objId.Append('1');
  220. value -= 40;
  221. }
  222. else
  223. {
  224. objId.Append('2');
  225. value -= 80;
  226. }
  227. first = false;
  228. }
  229. objId.Append('.');
  230. objId.Append(value);
  231. value = 0;
  232. }
  233. else
  234. {
  235. value <<= 7;
  236. }
  237. }
  238. else
  239. {
  240. if (bigValue == null)
  241. {
  242. bigValue = BigInteger.ValueOf(value);
  243. }
  244. bigValue = bigValue.Or(BigInteger.ValueOf(b & 0x7F));
  245. if ((b & 0x80) == 0)
  246. {
  247. if (first)
  248. {
  249. objId.Append('2');
  250. bigValue = bigValue.Subtract(BigInteger.ValueOf(80));
  251. first = false;
  252. }
  253. objId.Append('.');
  254. objId.Append(bigValue);
  255. bigValue = null;
  256. value = 0;
  257. }
  258. else
  259. {
  260. bigValue = bigValue.ShiftLeft(7);
  261. }
  262. }
  263. }
  264. return objId.ToString();
  265. }
  266. }
  267. }
  268. #pragma warning restore
  269. #endif