X509Extensions.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509
  8. {
  9. public class X509Extensions
  10. : Asn1Encodable
  11. {
  12. /**
  13. * Subject Directory Attributes
  14. */
  15. public static readonly DerObjectIdentifier SubjectDirectoryAttributes = new DerObjectIdentifier("2.5.29.9");
  16. /**
  17. * Subject Key Identifier
  18. */
  19. public static readonly DerObjectIdentifier SubjectKeyIdentifier = new DerObjectIdentifier("2.5.29.14");
  20. /**
  21. * Key Usage
  22. */
  23. public static readonly DerObjectIdentifier KeyUsage = new DerObjectIdentifier("2.5.29.15");
  24. /**
  25. * Private Key Usage Period
  26. */
  27. public static readonly DerObjectIdentifier PrivateKeyUsagePeriod = new DerObjectIdentifier("2.5.29.16");
  28. /**
  29. * Subject Alternative Name
  30. */
  31. public static readonly DerObjectIdentifier SubjectAlternativeName = new DerObjectIdentifier("2.5.29.17");
  32. /**
  33. * Issuer Alternative Name
  34. */
  35. public static readonly DerObjectIdentifier IssuerAlternativeName = new DerObjectIdentifier("2.5.29.18");
  36. /**
  37. * Basic Constraints
  38. */
  39. public static readonly DerObjectIdentifier BasicConstraints = new DerObjectIdentifier("2.5.29.19");
  40. /**
  41. * CRL Number
  42. */
  43. public static readonly DerObjectIdentifier CrlNumber = new DerObjectIdentifier("2.5.29.20");
  44. /**
  45. * Reason code
  46. */
  47. public static readonly DerObjectIdentifier ReasonCode = new DerObjectIdentifier("2.5.29.21");
  48. /**
  49. * Hold Instruction Code
  50. */
  51. public static readonly DerObjectIdentifier InstructionCode = new DerObjectIdentifier("2.5.29.23");
  52. /**
  53. * Invalidity Date
  54. */
  55. public static readonly DerObjectIdentifier InvalidityDate = new DerObjectIdentifier("2.5.29.24");
  56. /**
  57. * Delta CRL indicator
  58. */
  59. public static readonly DerObjectIdentifier DeltaCrlIndicator = new DerObjectIdentifier("2.5.29.27");
  60. /**
  61. * Issuing Distribution Point
  62. */
  63. public static readonly DerObjectIdentifier IssuingDistributionPoint = new DerObjectIdentifier("2.5.29.28");
  64. /**
  65. * Certificate Issuer
  66. */
  67. public static readonly DerObjectIdentifier CertificateIssuer = new DerObjectIdentifier("2.5.29.29");
  68. /**
  69. * Name Constraints
  70. */
  71. public static readonly DerObjectIdentifier NameConstraints = new DerObjectIdentifier("2.5.29.30");
  72. /**
  73. * CRL Distribution Points
  74. */
  75. public static readonly DerObjectIdentifier CrlDistributionPoints = new DerObjectIdentifier("2.5.29.31");
  76. /**
  77. * Certificate Policies
  78. */
  79. public static readonly DerObjectIdentifier CertificatePolicies = new DerObjectIdentifier("2.5.29.32");
  80. /**
  81. * Policy Mappings
  82. */
  83. public static readonly DerObjectIdentifier PolicyMappings = new DerObjectIdentifier("2.5.29.33");
  84. /**
  85. * Authority Key Identifier
  86. */
  87. public static readonly DerObjectIdentifier AuthorityKeyIdentifier = new DerObjectIdentifier("2.5.29.35");
  88. /**
  89. * Policy Constraints
  90. */
  91. public static readonly DerObjectIdentifier PolicyConstraints = new DerObjectIdentifier("2.5.29.36");
  92. /**
  93. * Extended Key Usage
  94. */
  95. public static readonly DerObjectIdentifier ExtendedKeyUsage = new DerObjectIdentifier("2.5.29.37");
  96. /**
  97. * Freshest CRL
  98. */
  99. public static readonly DerObjectIdentifier FreshestCrl = new DerObjectIdentifier("2.5.29.46");
  100. /**
  101. * Inhibit Any Policy
  102. */
  103. public static readonly DerObjectIdentifier InhibitAnyPolicy = new DerObjectIdentifier("2.5.29.54");
  104. /**
  105. * Authority Info Access
  106. */
  107. public static readonly DerObjectIdentifier AuthorityInfoAccess = new DerObjectIdentifier("1.3.6.1.5.5.7.1.1");
  108. /**
  109. * Subject Info Access
  110. */
  111. public static readonly DerObjectIdentifier SubjectInfoAccess = new DerObjectIdentifier("1.3.6.1.5.5.7.1.11");
  112. /**
  113. * Logo Type
  114. */
  115. public static readonly DerObjectIdentifier LogoType = new DerObjectIdentifier("1.3.6.1.5.5.7.1.12");
  116. /**
  117. * BiometricInfo
  118. */
  119. public static readonly DerObjectIdentifier BiometricInfo = new DerObjectIdentifier("1.3.6.1.5.5.7.1.2");
  120. /**
  121. * QCStatements
  122. */
  123. public static readonly DerObjectIdentifier QCStatements = new DerObjectIdentifier("1.3.6.1.5.5.7.1.3");
  124. /**
  125. * Audit identity extension in attribute certificates.
  126. */
  127. public static readonly DerObjectIdentifier AuditIdentity = new DerObjectIdentifier("1.3.6.1.5.5.7.1.4");
  128. /**
  129. * NoRevAvail extension in attribute certificates.
  130. */
  131. public static readonly DerObjectIdentifier NoRevAvail = new DerObjectIdentifier("2.5.29.56");
  132. /**
  133. * TargetInformation extension in attribute certificates.
  134. */
  135. public static readonly DerObjectIdentifier TargetInformation = new DerObjectIdentifier("2.5.29.55");
  136. /**
  137. * Expired Certificates on CRL extension
  138. */
  139. public static readonly DerObjectIdentifier ExpiredCertsOnCrl = new DerObjectIdentifier("2.5.29.60");
  140. private readonly IDictionary extensions = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  141. private readonly IList ordering;
  142. public static X509Extensions GetInstance(
  143. Asn1TaggedObject obj,
  144. bool explicitly)
  145. {
  146. return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
  147. }
  148. public static X509Extensions GetInstance(
  149. object obj)
  150. {
  151. if (obj == null || obj is X509Extensions)
  152. {
  153. return (X509Extensions) obj;
  154. }
  155. if (obj is Asn1Sequence)
  156. {
  157. return new X509Extensions((Asn1Sequence) obj);
  158. }
  159. if (obj is Asn1TaggedObject)
  160. {
  161. return GetInstance(((Asn1TaggedObject) obj).GetObject());
  162. }
  163. throw new ArgumentException("unknown object in factory: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  164. }
  165. /**
  166. * Constructor from Asn1Sequence.
  167. *
  168. * the extensions are a list of constructed sequences, either with (Oid, OctetString) or (Oid, Boolean, OctetString)
  169. */
  170. private X509Extensions(
  171. Asn1Sequence seq)
  172. {
  173. this.ordering = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  174. foreach (Asn1Encodable ae in seq)
  175. {
  176. Asn1Sequence s = Asn1Sequence.GetInstance(ae.ToAsn1Object());
  177. if (s.Count < 2 || s.Count > 3)
  178. throw new ArgumentException("Bad sequence size: " + s.Count);
  179. DerObjectIdentifier oid = DerObjectIdentifier.GetInstance(s[0].ToAsn1Object());
  180. bool isCritical = s.Count == 3
  181. && DerBoolean.GetInstance(s[1].ToAsn1Object()).IsTrue;
  182. Asn1OctetString octets = Asn1OctetString.GetInstance(s[s.Count - 1].ToAsn1Object());
  183. if (extensions.Contains(oid))
  184. throw new ArgumentException("repeated extension found: " + oid);
  185. extensions.Add(oid, new X509Extension(isCritical, octets));
  186. ordering.Add(oid);
  187. }
  188. }
  189. /**
  190. * constructor from a table of extensions.
  191. * <p>
  192. * it's is assumed the table contains Oid/string pairs.</p>
  193. */
  194. public X509Extensions(
  195. IDictionary extensions)
  196. : this(null, extensions)
  197. {
  198. }
  199. /**
  200. * Constructor from a table of extensions with ordering.
  201. * <p>
  202. * It's is assumed the table contains Oid/string pairs.</p>
  203. */
  204. public X509Extensions(
  205. IList ordering,
  206. IDictionary extensions)
  207. {
  208. if (ordering == null)
  209. {
  210. this.ordering = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(extensions.Keys);
  211. }
  212. else
  213. {
  214. this.ordering = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(ordering);
  215. }
  216. foreach (DerObjectIdentifier oid in this.ordering)
  217. {
  218. this.extensions.Add(oid, (X509Extension)extensions[oid]);
  219. }
  220. }
  221. /**
  222. * Constructor from two vectors
  223. *
  224. * @param objectIDs an ArrayList of the object identifiers.
  225. * @param values an ArrayList of the extension values.
  226. */
  227. public X509Extensions(
  228. IList oids,
  229. IList values)
  230. {
  231. this.ordering = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(oids);
  232. int count = 0;
  233. foreach (DerObjectIdentifier oid in this.ordering)
  234. {
  235. this.extensions.Add(oid, (X509Extension)values[count++]);
  236. }
  237. }
  238. #if !(SILVERLIGHT || PORTABLE || NETFX_CORE)
  239. /**
  240. * constructor from a table of extensions.
  241. * <p>
  242. * it's is assumed the table contains Oid/string pairs.</p>
  243. */
  244. [Obsolete]
  245. public X509Extensions(
  246. Hashtable extensions)
  247. : this(null, extensions)
  248. {
  249. }
  250. /**
  251. * Constructor from a table of extensions with ordering.
  252. * <p>
  253. * It's is assumed the table contains Oid/string pairs.</p>
  254. */
  255. [Obsolete]
  256. public X509Extensions(
  257. ArrayList ordering,
  258. Hashtable extensions)
  259. {
  260. if (ordering == null)
  261. {
  262. this.ordering = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(extensions.Keys);
  263. }
  264. else
  265. {
  266. this.ordering = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(ordering);
  267. }
  268. foreach (DerObjectIdentifier oid in this.ordering)
  269. {
  270. this.extensions.Add(oid, (X509Extension) extensions[oid]);
  271. }
  272. }
  273. /**
  274. * Constructor from two vectors
  275. *
  276. * @param objectIDs an ArrayList of the object identifiers.
  277. * @param values an ArrayList of the extension values.
  278. */
  279. [Obsolete]
  280. public X509Extensions(
  281. ArrayList oids,
  282. ArrayList values)
  283. {
  284. this.ordering = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(oids);
  285. int count = 0;
  286. foreach (DerObjectIdentifier oid in this.ordering)
  287. {
  288. this.extensions.Add(oid, (X509Extension) values[count++]);
  289. }
  290. }
  291. #endif
  292. [Obsolete("Use ExtensionOids IEnumerable property")]
  293. public IEnumerator Oids()
  294. {
  295. return ExtensionOids.GetEnumerator();
  296. }
  297. /**
  298. * return an Enumeration of the extension field's object ids.
  299. */
  300. public IEnumerable ExtensionOids
  301. {
  302. get { return new EnumerableProxy(ordering); }
  303. }
  304. /**
  305. * return the extension represented by the object identifier
  306. * passed in.
  307. *
  308. * @return the extension if it's present, null otherwise.
  309. */
  310. public X509Extension GetExtension(
  311. DerObjectIdentifier oid)
  312. {
  313. return (X509Extension) extensions[oid];
  314. }
  315. /**
  316. * <pre>
  317. * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
  318. *
  319. * Extension ::= SEQUENCE {
  320. * extnId EXTENSION.&amp;id ({ExtensionSet}),
  321. * critical BOOLEAN DEFAULT FALSE,
  322. * extnValue OCTET STRING }
  323. * </pre>
  324. */
  325. public override Asn1Object ToAsn1Object()
  326. {
  327. Asn1EncodableVector vec = new Asn1EncodableVector();
  328. foreach (DerObjectIdentifier oid in ordering)
  329. {
  330. X509Extension ext = (X509Extension) extensions[oid];
  331. Asn1EncodableVector v = new Asn1EncodableVector(oid);
  332. if (ext.IsCritical)
  333. {
  334. v.Add(DerBoolean.True);
  335. }
  336. v.Add(ext.Value);
  337. vec.Add(new DerSequence(v));
  338. }
  339. return new DerSequence(vec);
  340. }
  341. public bool Equivalent(
  342. X509Extensions other)
  343. {
  344. if (extensions.Count != other.extensions.Count)
  345. return false;
  346. foreach (DerObjectIdentifier oid in extensions.Keys)
  347. {
  348. if (!extensions[oid].Equals(other.extensions[oid]))
  349. return false;
  350. }
  351. return true;
  352. }
  353. public DerObjectIdentifier[] GetExtensionOids()
  354. {
  355. return ToOidArray(ordering);
  356. }
  357. public DerObjectIdentifier[] GetNonCriticalExtensionOids()
  358. {
  359. return GetExtensionOids(false);
  360. }
  361. public DerObjectIdentifier[] GetCriticalExtensionOids()
  362. {
  363. return GetExtensionOids(true);
  364. }
  365. private DerObjectIdentifier[] GetExtensionOids(bool isCritical)
  366. {
  367. IList oids = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  368. foreach (DerObjectIdentifier oid in this.ordering)
  369. {
  370. X509Extension ext = (X509Extension)extensions[oid];
  371. if (ext.IsCritical == isCritical)
  372. {
  373. oids.Add(oid);
  374. }
  375. }
  376. return ToOidArray(oids);
  377. }
  378. private static DerObjectIdentifier[] ToOidArray(IList oids)
  379. {
  380. DerObjectIdentifier[] oidArray = new DerObjectIdentifier[oids.Count];
  381. oids.CopyTo(oidArray, 0);
  382. return oidArray;
  383. }
  384. }
  385. }
  386. #pragma warning restore
  387. #endif