X509Extensions.cs 13 KB

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