X509Extensions.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 X509Extension GetExtension(X509Extensions extensions, DerObjectIdentifier oid)
  143. {
  144. return null == extensions ? null : extensions.GetExtension(oid);
  145. }
  146. public static Asn1Encodable GetExtensionParsedValue(X509Extensions extensions, DerObjectIdentifier oid)
  147. {
  148. return null == extensions ? null : extensions.GetExtensionParsedValue(oid);
  149. }
  150. public static X509Extensions GetInstance(
  151. Asn1TaggedObject obj,
  152. bool explicitly)
  153. {
  154. return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
  155. }
  156. public static X509Extensions GetInstance(
  157. object obj)
  158. {
  159. if (obj == null || obj is X509Extensions)
  160. {
  161. return (X509Extensions) obj;
  162. }
  163. if (obj is Asn1Sequence)
  164. {
  165. return new X509Extensions((Asn1Sequence) obj);
  166. }
  167. if (obj is Asn1TaggedObject)
  168. {
  169. return GetInstance(((Asn1TaggedObject) obj).GetObject());
  170. }
  171. throw new ArgumentException("unknown object in factory: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  172. }
  173. /**
  174. * Constructor from Asn1Sequence.
  175. *
  176. * the extensions are a list of constructed sequences, either with (Oid, OctetString) or (Oid, Boolean, OctetString)
  177. */
  178. private X509Extensions(
  179. Asn1Sequence seq)
  180. {
  181. this.ordering = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  182. foreach (Asn1Encodable ae in seq)
  183. {
  184. Asn1Sequence s = Asn1Sequence.GetInstance(ae.ToAsn1Object());
  185. if (s.Count < 2 || s.Count > 3)
  186. throw new ArgumentException("Bad sequence size: " + s.Count);
  187. DerObjectIdentifier oid = DerObjectIdentifier.GetInstance(s[0].ToAsn1Object());
  188. bool isCritical = s.Count == 3
  189. && DerBoolean.GetInstance(s[1].ToAsn1Object()).IsTrue;
  190. Asn1OctetString octets = Asn1OctetString.GetInstance(s[s.Count - 1].ToAsn1Object());
  191. if (extensions.Contains(oid))
  192. throw new ArgumentException("repeated extension found: " + oid);
  193. extensions.Add(oid, new X509Extension(isCritical, octets));
  194. ordering.Add(oid);
  195. }
  196. }
  197. /**
  198. * constructor from a table of extensions.
  199. * <p>
  200. * it's is assumed the table contains Oid/string pairs.</p>
  201. */
  202. public X509Extensions(
  203. IDictionary extensions)
  204. : this(null, extensions)
  205. {
  206. }
  207. /**
  208. * Constructor from a table of extensions with ordering.
  209. * <p>
  210. * It's is assumed the table contains Oid/string pairs.</p>
  211. */
  212. public X509Extensions(
  213. IList ordering,
  214. IDictionary extensions)
  215. {
  216. if (ordering == null)
  217. {
  218. this.ordering = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(extensions.Keys);
  219. }
  220. else
  221. {
  222. this.ordering = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(ordering);
  223. }
  224. foreach (DerObjectIdentifier oid in this.ordering)
  225. {
  226. this.extensions.Add(oid, (X509Extension)extensions[oid]);
  227. }
  228. }
  229. /**
  230. * Constructor from two vectors
  231. *
  232. * @param objectIDs an ArrayList of the object identifiers.
  233. * @param values an ArrayList of the extension values.
  234. */
  235. public X509Extensions(
  236. IList oids,
  237. IList values)
  238. {
  239. this.ordering = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(oids);
  240. int count = 0;
  241. foreach (DerObjectIdentifier oid in this.ordering)
  242. {
  243. this.extensions.Add(oid, (X509Extension)values[count++]);
  244. }
  245. }
  246. #if !(SILVERLIGHT || PORTABLE || NETFX_CORE)
  247. /**
  248. * constructor from a table of extensions.
  249. * <p>
  250. * it's is assumed the table contains Oid/string pairs.</p>
  251. */
  252. [Obsolete]
  253. public X509Extensions(
  254. Hashtable extensions)
  255. : this(null, extensions)
  256. {
  257. }
  258. /**
  259. * Constructor from a table of extensions with ordering.
  260. * <p>
  261. * It's is assumed the table contains Oid/string pairs.</p>
  262. */
  263. [Obsolete]
  264. public X509Extensions(
  265. ArrayList ordering,
  266. Hashtable extensions)
  267. {
  268. if (ordering == null)
  269. {
  270. this.ordering = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(extensions.Keys);
  271. }
  272. else
  273. {
  274. this.ordering = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(ordering);
  275. }
  276. foreach (DerObjectIdentifier oid in this.ordering)
  277. {
  278. this.extensions.Add(oid, (X509Extension) extensions[oid]);
  279. }
  280. }
  281. /**
  282. * Constructor from two vectors
  283. *
  284. * @param objectIDs an ArrayList of the object identifiers.
  285. * @param values an ArrayList of the extension values.
  286. */
  287. [Obsolete]
  288. public X509Extensions(
  289. ArrayList oids,
  290. ArrayList values)
  291. {
  292. this.ordering = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(oids);
  293. int count = 0;
  294. foreach (DerObjectIdentifier oid in this.ordering)
  295. {
  296. this.extensions.Add(oid, (X509Extension) values[count++]);
  297. }
  298. }
  299. #endif
  300. public IEnumerator Oids()
  301. {
  302. return ExtensionOids.GetEnumerator();
  303. }
  304. /**
  305. * return an Enumeration of the extension field's object ids.
  306. */
  307. public IEnumerable ExtensionOids
  308. {
  309. get { return new EnumerableProxy(ordering); }
  310. }
  311. /**
  312. * return the extension represented by the object identifier
  313. * passed in.
  314. *
  315. * @return the extension if it's present, null otherwise.
  316. */
  317. public X509Extension GetExtension(
  318. DerObjectIdentifier oid)
  319. {
  320. return (X509Extension)extensions[oid];
  321. }
  322. /**
  323. * return the parsed value of the extension represented by the object identifier
  324. * passed in.
  325. *
  326. * @return the parsed value of the extension if it's present, null otherwise.
  327. */
  328. public Asn1Encodable GetExtensionParsedValue(DerObjectIdentifier oid)
  329. {
  330. X509Extension ext = GetExtension(oid);
  331. return ext == null ? null : ext.GetParsedValue();
  332. }
  333. /**
  334. * <pre>
  335. * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
  336. *
  337. * Extension ::= SEQUENCE {
  338. * extnId EXTENSION.&amp;id ({ExtensionSet}),
  339. * critical BOOLEAN DEFAULT FALSE,
  340. * extnValue OCTET STRING }
  341. * </pre>
  342. */
  343. public override Asn1Object ToAsn1Object()
  344. {
  345. Asn1EncodableVector vec = new Asn1EncodableVector();
  346. foreach (DerObjectIdentifier oid in ordering)
  347. {
  348. X509Extension ext = (X509Extension) extensions[oid];
  349. Asn1EncodableVector v = new Asn1EncodableVector(oid);
  350. if (ext.IsCritical)
  351. {
  352. v.Add(DerBoolean.True);
  353. }
  354. v.Add(ext.Value);
  355. vec.Add(new DerSequence(v));
  356. }
  357. return new DerSequence(vec);
  358. }
  359. public bool Equivalent(
  360. X509Extensions other)
  361. {
  362. if (extensions.Count != other.extensions.Count)
  363. return false;
  364. foreach (DerObjectIdentifier oid in extensions.Keys)
  365. {
  366. if (!extensions[oid].Equals(other.extensions[oid]))
  367. return false;
  368. }
  369. return true;
  370. }
  371. public DerObjectIdentifier[] GetExtensionOids()
  372. {
  373. return ToOidArray(ordering);
  374. }
  375. public DerObjectIdentifier[] GetNonCriticalExtensionOids()
  376. {
  377. return GetExtensionOids(false);
  378. }
  379. public DerObjectIdentifier[] GetCriticalExtensionOids()
  380. {
  381. return GetExtensionOids(true);
  382. }
  383. private DerObjectIdentifier[] GetExtensionOids(bool isCritical)
  384. {
  385. IList oids = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  386. foreach (DerObjectIdentifier oid in this.ordering)
  387. {
  388. X509Extension ext = (X509Extension)extensions[oid];
  389. if (ext.IsCritical == isCritical)
  390. {
  391. oids.Add(oid);
  392. }
  393. }
  394. return ToOidArray(oids);
  395. }
  396. private static DerObjectIdentifier[] ToOidArray(IList oids)
  397. {
  398. DerObjectIdentifier[] oidArray = new DerObjectIdentifier[oids.Count];
  399. oids.CopyTo(oidArray, 0);
  400. return oidArray;
  401. }
  402. }
  403. }
  404. #pragma warning restore
  405. #endif