X509Extensions.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections.Generic;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  7. namespace Best.HTTP.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 Dictionary<DerObjectIdentifier, X509Extension> m_extensions =
  141. new Dictionary<DerObjectIdentifier, X509Extension>();
  142. private readonly List<DerObjectIdentifier> m_ordering;
  143. public static X509Extension GetExtension(X509Extensions extensions, DerObjectIdentifier oid)
  144. {
  145. return null == extensions ? null : extensions.GetExtension(oid);
  146. }
  147. public static Asn1Encodable GetExtensionParsedValue(X509Extensions extensions, DerObjectIdentifier oid)
  148. {
  149. return null == extensions ? null : extensions.GetExtensionParsedValue(oid);
  150. }
  151. public static X509Extensions GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
  152. {
  153. return GetInstance(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
  154. }
  155. public static X509Extensions GetInstance(
  156. object obj)
  157. {
  158. if (obj == null || obj is X509Extensions)
  159. {
  160. return (X509Extensions) obj;
  161. }
  162. if (obj is Asn1Sequence)
  163. {
  164. return new X509Extensions((Asn1Sequence) obj);
  165. }
  166. if (obj is Asn1TaggedObject)
  167. {
  168. return GetInstance(((Asn1TaggedObject) obj).GetObject());
  169. }
  170. throw new ArgumentException("unknown object in factory: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  171. }
  172. /**
  173. * Constructor from Asn1Sequence.
  174. *
  175. * the extensions are a list of constructed sequences, either with (Oid, OctetString) or (Oid, Boolean, OctetString)
  176. */
  177. private X509Extensions(Asn1Sequence seq)
  178. {
  179. m_ordering = new List<DerObjectIdentifier>();
  180. foreach (Asn1Encodable ae in seq)
  181. {
  182. Asn1Sequence s = Asn1Sequence.GetInstance(ae.ToAsn1Object());
  183. if (s.Count < 2 || s.Count > 3)
  184. throw new ArgumentException("Bad sequence size: " + s.Count);
  185. DerObjectIdentifier oid = DerObjectIdentifier.GetInstance(s[0].ToAsn1Object());
  186. bool isCritical = s.Count == 3
  187. && DerBoolean.GetInstance(s[1].ToAsn1Object()).IsTrue;
  188. Asn1OctetString octets = Asn1OctetString.GetInstance(s[s.Count - 1].ToAsn1Object());
  189. if (m_extensions.ContainsKey(oid))
  190. throw new ArgumentException("repeated extension found: " + oid);
  191. m_extensions.Add(oid, new X509Extension(isCritical, octets));
  192. m_ordering.Add(oid);
  193. }
  194. }
  195. /**
  196. * constructor from a table of extensions.
  197. * <p>
  198. * it's is assumed the table contains Oid/string pairs.</p>
  199. */
  200. public X509Extensions(IDictionary<DerObjectIdentifier, X509Extension> extensions)
  201. : this(null, extensions)
  202. {
  203. }
  204. /**
  205. * Constructor from a table of extensions with ordering.
  206. * <p>
  207. * It's is assumed the table contains Oid/string pairs.</p>
  208. */
  209. public X509Extensions(IList<DerObjectIdentifier> ordering,
  210. IDictionary<DerObjectIdentifier, X509Extension> extensions)
  211. {
  212. if (ordering == null)
  213. {
  214. m_ordering = new List<DerObjectIdentifier>(extensions.Keys);
  215. }
  216. else
  217. {
  218. m_ordering = new List<DerObjectIdentifier>(ordering);
  219. }
  220. foreach (DerObjectIdentifier oid in m_ordering)
  221. {
  222. m_extensions.Add(oid, extensions[oid]);
  223. }
  224. }
  225. /**
  226. * Constructor from two vectors
  227. *
  228. * @param objectIDs an ArrayList of the object identifiers.
  229. * @param values an ArrayList of the extension values.
  230. */
  231. public X509Extensions(IList<DerObjectIdentifier> oids, IList<X509Extension> values)
  232. {
  233. m_ordering = new List<DerObjectIdentifier>(oids);
  234. int count = 0;
  235. foreach (DerObjectIdentifier oid in m_ordering)
  236. {
  237. m_extensions.Add(oid, values[count++]);
  238. }
  239. }
  240. /**
  241. * return an Enumeration of the extension field's object ids.
  242. */
  243. public IEnumerable<DerObjectIdentifier> ExtensionOids
  244. {
  245. get { return CollectionUtilities.Proxy(m_ordering); }
  246. }
  247. /**
  248. * return the extension represented by the object identifier
  249. * passed in.
  250. *
  251. * @return the extension if it's present, null otherwise.
  252. */
  253. public X509Extension GetExtension(DerObjectIdentifier oid)
  254. {
  255. return CollectionUtilities.GetValueOrNull(m_extensions, oid);
  256. }
  257. /**
  258. * return the parsed value of the extension represented by the object identifier
  259. * passed in.
  260. *
  261. * @return the parsed value of the extension if it's present, null otherwise.
  262. */
  263. public Asn1Encodable GetExtensionParsedValue(DerObjectIdentifier oid)
  264. {
  265. return GetExtension(oid)?.GetParsedValue();
  266. }
  267. /**
  268. * <pre>
  269. * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
  270. *
  271. * Extension ::= SEQUENCE {
  272. * extnId EXTENSION.&amp;id ({ExtensionSet}),
  273. * critical BOOLEAN DEFAULT FALSE,
  274. * extnValue OCTET STRING }
  275. * </pre>
  276. */
  277. public override Asn1Object ToAsn1Object()
  278. {
  279. Asn1EncodableVector v = new Asn1EncodableVector(m_ordering.Count);
  280. foreach (DerObjectIdentifier oid in m_ordering)
  281. {
  282. X509Extension ext = m_extensions[oid];
  283. if (ext.IsCritical)
  284. {
  285. v.Add(new DerSequence(oid, DerBoolean.True, ext.Value));
  286. }
  287. else
  288. {
  289. v.Add(new DerSequence(oid, ext.Value));
  290. }
  291. }
  292. return new DerSequence(v);
  293. }
  294. public bool Equivalent(X509Extensions other)
  295. {
  296. if (m_extensions.Count != other.m_extensions.Count)
  297. return false;
  298. foreach (var entry in m_extensions)
  299. {
  300. if (!entry.Value.Equals(other.GetExtension(entry.Key)))
  301. return false;
  302. }
  303. return true;
  304. }
  305. public DerObjectIdentifier[] GetExtensionOids()
  306. {
  307. return m_ordering.ToArray();
  308. }
  309. public DerObjectIdentifier[] GetNonCriticalExtensionOids()
  310. {
  311. return GetExtensionOids(false);
  312. }
  313. public DerObjectIdentifier[] GetCriticalExtensionOids()
  314. {
  315. return GetExtensionOids(true);
  316. }
  317. private DerObjectIdentifier[] GetExtensionOids(bool isCritical)
  318. {
  319. var oids = new List<DerObjectIdentifier>();
  320. foreach (DerObjectIdentifier oid in m_ordering)
  321. {
  322. if (m_extensions[oid].IsCritical == isCritical)
  323. {
  324. oids.Add(oid);
  325. }
  326. }
  327. return oids.ToArray();
  328. }
  329. }
  330. }
  331. #pragma warning restore
  332. #endif