X509CertificatePair.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security.Certificates;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.X509
  9. {
  10. /// <remarks>
  11. /// This class contains a cross certificate pair. Cross certificates pairs may
  12. /// contain two cross signed certificates from two CAs. A certificate from the
  13. /// other CA to this CA is contained in the forward certificate, the certificate
  14. /// from this CA to the other CA is contained in the reverse certificate.
  15. /// </remarks>
  16. public class X509CertificatePair
  17. {
  18. private readonly X509Certificate forward;
  19. private readonly X509Certificate reverse;
  20. /// <summary>Constructor</summary>
  21. /// <param name="forward">Certificate from the other CA to this CA.</param>
  22. /// <param name="reverse">Certificate from this CA to the other CA.</param>
  23. public X509CertificatePair(
  24. X509Certificate forward,
  25. X509Certificate reverse)
  26. {
  27. this.forward = forward;
  28. this.reverse = reverse;
  29. }
  30. /// <summary>Constructor from a ASN.1 CertificatePair structure.</summary>
  31. /// <param name="pair">The <c>CertificatePair</c> ASN.1 object.</param>
  32. public X509CertificatePair(
  33. CertificatePair pair)
  34. {
  35. if (pair.Forward != null)
  36. {
  37. this.forward = new X509Certificate(pair.Forward);
  38. }
  39. if (pair.Reverse != null)
  40. {
  41. this.reverse = new X509Certificate(pair.Reverse);
  42. }
  43. }
  44. public byte[] GetEncoded()
  45. {
  46. try
  47. {
  48. X509CertificateStructure f = null, r = null;
  49. if (forward != null)
  50. {
  51. f = X509CertificateStructure.GetInstance(
  52. Asn1Object.FromByteArray(forward.GetEncoded()));
  53. if (f == null)
  54. throw new CertificateEncodingException("unable to get encoding for forward");
  55. }
  56. if (reverse != null)
  57. {
  58. r = X509CertificateStructure.GetInstance(
  59. Asn1Object.FromByteArray(reverse.GetEncoded()));
  60. if (r == null)
  61. throw new CertificateEncodingException("unable to get encoding for reverse");
  62. }
  63. return new CertificatePair(f, r).GetDerEncoded();
  64. }
  65. catch (Exception e)
  66. {
  67. // TODO
  68. // throw new ExtCertificateEncodingException(e.toString(), e);
  69. throw new CertificateEncodingException(e.Message, e);
  70. }
  71. }
  72. /// <summary>Returns the certificate from the other CA to this CA.</summary>
  73. public X509Certificate Forward
  74. {
  75. get { return forward; }
  76. }
  77. /// <summary>Returns the certificate from this CA to the other CA.</summary>
  78. public X509Certificate Reverse
  79. {
  80. get { return reverse; }
  81. }
  82. public override bool Equals(
  83. object obj)
  84. {
  85. if (obj == this)
  86. return true;
  87. X509CertificatePair other = obj as X509CertificatePair;
  88. if (other == null)
  89. return false;
  90. return BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Equals(this.forward, other.forward)
  91. && BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Equals(this.reverse, other.reverse);
  92. }
  93. public override int GetHashCode()
  94. {
  95. int hash = -1;
  96. if (forward != null)
  97. {
  98. hash ^= forward.GetHashCode();
  99. }
  100. if (reverse != null)
  101. {
  102. hash *= 17;
  103. hash ^= reverse.GetHashCode();
  104. }
  105. return hash;
  106. }
  107. }
  108. }
  109. #pragma warning restore
  110. #endif