PkixCertPath.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using System.Text;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Cms;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.OpenSsl;
  14. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security.Certificates;
  15. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  16. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  17. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Pkix
  18. {
  19. /**
  20. * An immutable sequence of certificates (a certification path).<br />
  21. * <br />
  22. * This is an abstract class that defines the methods common to all CertPaths.
  23. * Subclasses can handle different kinds of certificates (X.509, PGP, etc.).<br />
  24. * <br />
  25. * All CertPath objects have a type, a list of Certificates, and one or more
  26. * supported encodings. Because the CertPath class is immutable, a CertPath
  27. * cannot change in any externally visible way after being constructed. This
  28. * stipulation applies to all public fields and methods of this class and any
  29. * added or overridden by subclasses.<br />
  30. * <br />
  31. * The type is a string that identifies the type of Certificates in the
  32. * certification path. For each certificate cert in a certification path
  33. * certPath, cert.getType().equals(certPath.getType()) must be true.<br />
  34. * <br />
  35. * The list of Certificates is an ordered List of zero or more Certificates.
  36. * This List and all of the Certificates contained in it must be immutable.<br />
  37. * <br />
  38. * Each CertPath object must support one or more encodings so that the object
  39. * can be translated into a byte array for storage or transmission to other
  40. * parties. Preferably, these encodings should be well-documented standards
  41. * (such as PKCS#7). One of the encodings supported by a CertPath is considered
  42. * the default encoding. This encoding is used if no encoding is explicitly
  43. * requested (for the {@link #getEncoded()} method, for instance).<br />
  44. * <br />
  45. * All CertPath objects are also Serializable. CertPath objects are resolved
  46. * into an alternate {@link CertPathRep} object during serialization. This
  47. * allows a CertPath object to be serialized into an equivalent representation
  48. * regardless of its underlying implementation.<br />
  49. * <br />
  50. * CertPath objects can be created with a CertificateFactory or they can be
  51. * returned by other classes, such as a CertPathBuilder.<br />
  52. * <br />
  53. * By convention, X.509 CertPaths (consisting of X509Certificates), are ordered
  54. * starting with the target certificate and ending with a certificate issued by
  55. * the trust anchor. That is, the issuer of one certificate is the subject of
  56. * the following one. The certificate representing the
  57. * {@link TrustAnchor TrustAnchor} should not be included in the certification
  58. * path. Unvalidated X.509 CertPaths may not follow these conventions. PKIX
  59. * CertPathValidators will detect any departure from these conventions that
  60. * cause the certification path to be invalid and throw a
  61. * CertPathValidatorException.<br />
  62. * <br />
  63. * <strong>Concurrent Access</strong><br />
  64. * <br />
  65. * All CertPath objects must be thread-safe. That is, multiple threads may
  66. * concurrently invoke the methods defined in this class on a single CertPath
  67. * object (or more than one) with no ill effects. This is also true for the List
  68. * returned by CertPath.getCertificates.<br />
  69. * <br />
  70. * Requiring CertPath objects to be immutable and thread-safe allows them to be
  71. * passed around to various pieces of code without worrying about coordinating
  72. * access. Providing this thread-safety is generally not difficult, since the
  73. * CertPath and List objects in question are immutable.
  74. *
  75. * @see CertificateFactory
  76. * @see CertPathBuilder
  77. */
  78. /// <summary>
  79. /// CertPath implementation for X.509 certificates.
  80. /// </summary>
  81. public class PkixCertPath
  82. // : CertPath
  83. {
  84. internal static readonly IList certPathEncodings;
  85. static PkixCertPath()
  86. {
  87. IList encodings = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  88. encodings.Add("PkiPath");
  89. encodings.Add("PEM");
  90. encodings.Add("PKCS7");
  91. certPathEncodings = CollectionUtilities.ReadOnly(encodings);
  92. }
  93. private readonly IList certificates;
  94. /**
  95. * @param certs
  96. */
  97. private static IList SortCerts(
  98. IList certs)
  99. {
  100. if (certs.Count < 2)
  101. return certs;
  102. X509Name issuer = ((X509Certificate)certs[0]).IssuerDN;
  103. bool okay = true;
  104. for (int i = 1; i != certs.Count; i++)
  105. {
  106. X509Certificate cert = (X509Certificate)certs[i];
  107. if (issuer.Equivalent(cert.SubjectDN, true))
  108. {
  109. issuer = ((X509Certificate)certs[i]).IssuerDN;
  110. }
  111. else
  112. {
  113. okay = false;
  114. break;
  115. }
  116. }
  117. if (okay)
  118. return certs;
  119. // find end-entity cert
  120. IList retList = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(certs.Count);
  121. IList orig = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(certs);
  122. for (int i = 0; i < certs.Count; i++)
  123. {
  124. X509Certificate cert = (X509Certificate)certs[i];
  125. bool found = false;
  126. X509Name subject = cert.SubjectDN;
  127. foreach (X509Certificate c in certs)
  128. {
  129. if (c.IssuerDN.Equivalent(subject, true))
  130. {
  131. found = true;
  132. break;
  133. }
  134. }
  135. if (!found)
  136. {
  137. retList.Add(cert);
  138. certs.RemoveAt(i);
  139. }
  140. }
  141. // can only have one end entity cert - something's wrong, give up.
  142. if (retList.Count > 1)
  143. return orig;
  144. for (int i = 0; i != retList.Count; i++)
  145. {
  146. issuer = ((X509Certificate)retList[i]).IssuerDN;
  147. for (int j = 0; j < certs.Count; j++)
  148. {
  149. X509Certificate c = (X509Certificate)certs[j];
  150. if (issuer.Equivalent(c.SubjectDN, true))
  151. {
  152. retList.Add(c);
  153. certs.RemoveAt(j);
  154. break;
  155. }
  156. }
  157. }
  158. // make sure all certificates are accounted for.
  159. if (certs.Count > 0)
  160. return orig;
  161. return retList;
  162. }
  163. /**
  164. * Creates a CertPath of the specified type.
  165. * This constructor is protected because most users should use
  166. * a CertificateFactory to create CertPaths.
  167. * @param type the standard name of the type of Certificatesin this path
  168. **/
  169. public PkixCertPath(
  170. ICollection certificates)
  171. // : base("X.509")
  172. {
  173. this.certificates = SortCerts(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(certificates));
  174. }
  175. public PkixCertPath(
  176. Stream inStream)
  177. : this(inStream, "PkiPath")
  178. {
  179. }
  180. /**
  181. * Creates a CertPath of the specified type.
  182. * This constructor is protected because most users should use
  183. * a CertificateFactory to create CertPaths.
  184. *
  185. * @param type the standard name of the type of Certificatesin this path
  186. **/
  187. public PkixCertPath(
  188. Stream inStream,
  189. string encoding)
  190. // : base("X.509")
  191. {
  192. string upper = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(encoding);
  193. IList certs;
  194. try
  195. {
  196. if (upper.Equals(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant("PkiPath")))
  197. {
  198. Asn1InputStream derInStream = new Asn1InputStream(inStream);
  199. Asn1Object derObject = derInStream.ReadObject();
  200. if (!(derObject is Asn1Sequence))
  201. {
  202. throw new CertificateException(
  203. "input stream does not contain a ASN1 SEQUENCE while reading PkiPath encoded data to load CertPath");
  204. }
  205. certs = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  206. foreach (Asn1Encodable ae in (Asn1Sequence)derObject)
  207. {
  208. byte[] derBytes = ae.GetEncoded(Asn1Encodable.Der);
  209. Stream certInStream = new MemoryStream(derBytes, false);
  210. // TODO Is inserting at the front important (list will be sorted later anyway)?
  211. certs.Insert(0, new X509CertificateParser().ReadCertificate(certInStream));
  212. }
  213. }
  214. else if (upper.Equals("PKCS7") || upper.Equals("PEM"))
  215. {
  216. certs = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(new X509CertificateParser().ReadCertificates(inStream));
  217. }
  218. else
  219. {
  220. throw new CertificateException("unsupported encoding: " + encoding);
  221. }
  222. }
  223. catch (IOException ex)
  224. {
  225. throw new CertificateException(
  226. "IOException throw while decoding CertPath:\n"
  227. + ex.ToString());
  228. }
  229. this.certificates = SortCerts(certs);
  230. }
  231. /**
  232. * Returns an iteration of the encodings supported by this
  233. * certification path, with the default encoding
  234. * first. Attempts to modify the returned Iterator via its
  235. * remove method result in an UnsupportedOperationException.
  236. *
  237. * @return an Iterator over the names of the supported encodings (as Strings)
  238. **/
  239. public virtual IEnumerable Encodings
  240. {
  241. get { return new EnumerableProxy(certPathEncodings); }
  242. }
  243. /**
  244. * Compares this certification path for equality with the specified object.
  245. * Two CertPaths are equal if and only if their types are equal and their
  246. * certificate Lists (and by implication the Certificates in those Lists)
  247. * are equal. A CertPath is never equal to an object that is not a CertPath.<br />
  248. * <br />
  249. * This algorithm is implemented by this method. If it is overridden, the
  250. * behavior specified here must be maintained.
  251. *
  252. * @param other
  253. * the object to test for equality with this certification path
  254. *
  255. * @return true if the specified object is equal to this certification path,
  256. * false otherwise
  257. *
  258. * @see Object#hashCode() Object.hashCode()
  259. */
  260. public override bool Equals(
  261. object obj)
  262. {
  263. if (this == obj)
  264. return true;
  265. PkixCertPath other = obj as PkixCertPath;
  266. if (other == null)
  267. return false;
  268. // if (!this.Type.Equals(other.Type))
  269. // return false;
  270. //return this.Certificates.Equals(other.Certificates);
  271. // TODO Extract this to a utility class
  272. IList thisCerts = this.Certificates;
  273. IList otherCerts = other.Certificates;
  274. if (thisCerts.Count != otherCerts.Count)
  275. return false;
  276. IEnumerator e1 = thisCerts.GetEnumerator();
  277. IEnumerator e2 = otherCerts.GetEnumerator();
  278. while (e1.MoveNext())
  279. {
  280. e2.MoveNext();
  281. if (!BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Equals(e1.Current, e2.Current))
  282. return false;
  283. }
  284. return true;
  285. }
  286. public override int GetHashCode()
  287. {
  288. // FIXME?
  289. return this.Certificates.GetHashCode();
  290. }
  291. /**
  292. * Returns the encoded form of this certification path, using
  293. * the default encoding.
  294. *
  295. * @return the encoded bytes
  296. * @exception CertificateEncodingException if an encoding error occurs
  297. **/
  298. public virtual byte[] GetEncoded()
  299. {
  300. foreach (object enc in Encodings)
  301. {
  302. if (enc is string)
  303. {
  304. return GetEncoded((string)enc);
  305. }
  306. }
  307. return null;
  308. }
  309. /**
  310. * Returns the encoded form of this certification path, using
  311. * the specified encoding.
  312. *
  313. * @param encoding the name of the encoding to use
  314. * @return the encoded bytes
  315. * @exception CertificateEncodingException if an encoding error
  316. * occurs or the encoding requested is not supported
  317. *
  318. */
  319. public virtual byte[] GetEncoded(
  320. string encoding)
  321. {
  322. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(encoding, "PkiPath"))
  323. {
  324. Asn1EncodableVector v = new Asn1EncodableVector();
  325. for (int i = certificates.Count - 1; i >= 0; i--)
  326. {
  327. v.Add(ToAsn1Object((X509Certificate) certificates[i]));
  328. }
  329. return ToDerEncoded(new DerSequence(v));
  330. }
  331. else if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(encoding, "PKCS7"))
  332. {
  333. Asn1.Pkcs.ContentInfo encInfo = new Asn1.Pkcs.ContentInfo(
  334. PkcsObjectIdentifiers.Data, null);
  335. Asn1EncodableVector v = new Asn1EncodableVector();
  336. for (int i = 0; i != certificates.Count; i++)
  337. {
  338. v.Add(ToAsn1Object((X509Certificate)certificates[i]));
  339. }
  340. Asn1.Pkcs.SignedData sd = new Asn1.Pkcs.SignedData(
  341. new DerInteger(1),
  342. new DerSet(),
  343. encInfo,
  344. new DerSet(v),
  345. null,
  346. new DerSet());
  347. return ToDerEncoded(new Asn1.Pkcs.ContentInfo(PkcsObjectIdentifiers.SignedData, sd));
  348. }
  349. else if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase(encoding, "PEM"))
  350. {
  351. MemoryStream bOut = new MemoryStream();
  352. PemWriter pWrt = new PemWriter(new StreamWriter(bOut));
  353. try
  354. {
  355. for (int i = 0; i != certificates.Count; i++)
  356. {
  357. pWrt.WriteObject(certificates[i]);
  358. }
  359. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(pWrt.Writer);
  360. }
  361. catch (Exception)
  362. {
  363. throw new CertificateEncodingException("can't encode certificate for PEM encoded path");
  364. }
  365. return bOut.ToArray();
  366. }
  367. else
  368. {
  369. throw new CertificateEncodingException("unsupported encoding: " + encoding);
  370. }
  371. }
  372. /// <summary>
  373. /// Returns the list of certificates in this certification
  374. /// path.
  375. /// </summary>
  376. public virtual IList Certificates
  377. {
  378. get { return CollectionUtilities.ReadOnly(certificates); }
  379. }
  380. /**
  381. * Return a DERObject containing the encoded certificate.
  382. *
  383. * @param cert the X509Certificate object to be encoded
  384. *
  385. * @return the DERObject
  386. **/
  387. private Asn1Object ToAsn1Object(
  388. X509Certificate cert)
  389. {
  390. try
  391. {
  392. return Asn1Object.FromByteArray(cert.GetEncoded());
  393. }
  394. catch (Exception e)
  395. {
  396. throw new CertificateEncodingException("Exception while encoding certificate", e);
  397. }
  398. }
  399. private byte[] ToDerEncoded(Asn1Encodable obj)
  400. {
  401. try
  402. {
  403. return obj.GetEncoded(Asn1Encodable.Der);
  404. }
  405. catch (IOException e)
  406. {
  407. throw new CertificateEncodingException("Exception thrown", e);
  408. }
  409. }
  410. }
  411. }
  412. #pragma warning restore
  413. #endif