CertificatePair.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509
  6. {
  7. /**
  8. * This class helps to support crossCerfificatePairs in a LDAP directory
  9. * according RFC 2587
  10. *
  11. * <pre>
  12. * crossCertificatePairATTRIBUTE::={
  13. * WITH SYNTAX CertificatePair
  14. * EQUALITY MATCHING RULE certificatePairExactMatch
  15. * ID joint-iso-ccitt(2) ds(5) attributeType(4) crossCertificatePair(40)}
  16. * </pre>
  17. *
  18. * <blockquote> The forward elements of the crossCertificatePair attribute of a
  19. * CA's directory entry shall be used to store all, except self-issued
  20. * certificates issued to this CA. Optionally, the reverse elements of the
  21. * crossCertificatePair attribute, of a CA's directory entry may contain a
  22. * subset of certificates issued by this CA to other CAs. When both the forward
  23. * and the reverse elements are present in a single attribute value, issuer name
  24. * in one certificate shall match the subject name in the other and vice versa,
  25. * and the subject public key in one certificate shall be capable of verifying
  26. * the digital signature on the other certificate and vice versa.
  27. *
  28. * When a reverse element is present, the forward element value and the reverse
  29. * element value need not be stored in the same attribute value; in other words,
  30. * they can be stored in either a single attribute value or two attribute
  31. * values. </blockquote>
  32. *
  33. * <pre>
  34. * CertificatePair ::= SEQUENCE {
  35. * forward [0] Certificate OPTIONAL,
  36. * reverse [1] Certificate OPTIONAL,
  37. * -- at least one of the pair shall be present -- }
  38. * </pre>
  39. */
  40. public class CertificatePair
  41. : Asn1Encodable
  42. {
  43. private X509CertificateStructure forward, reverse;
  44. public static CertificatePair GetInstance(
  45. object obj)
  46. {
  47. if (obj == null || obj is CertificatePair)
  48. {
  49. return (CertificatePair) obj;
  50. }
  51. if (obj is Asn1Sequence)
  52. {
  53. return new CertificatePair((Asn1Sequence) obj);
  54. }
  55. throw new ArgumentException("unknown object in factory: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  56. }
  57. /**
  58. * Constructor from Asn1Sequence.
  59. * <p/>
  60. * The sequence is of type CertificatePair:
  61. * <p/>
  62. * <pre>
  63. * CertificatePair ::= SEQUENCE {
  64. * forward [0] Certificate OPTIONAL,
  65. * reverse [1] Certificate OPTIONAL,
  66. * -- at least one of the pair shall be present -- }
  67. * </pre>
  68. *
  69. * @param seq The ASN.1 sequence.
  70. */
  71. private CertificatePair(
  72. Asn1Sequence seq)
  73. {
  74. if (seq.Count != 1 && seq.Count != 2)
  75. {
  76. throw new ArgumentException("Bad sequence size: " + seq.Count, "seq");
  77. }
  78. foreach (object obj in seq)
  79. {
  80. Asn1TaggedObject o = Asn1TaggedObject.GetInstance(obj);
  81. if (o.TagNo == 0)
  82. {
  83. forward = X509CertificateStructure.GetInstance(o, true);
  84. }
  85. else if (o.TagNo == 1)
  86. {
  87. reverse = X509CertificateStructure.GetInstance(o, true);
  88. }
  89. else
  90. {
  91. throw new ArgumentException("Bad tag number: " + o.TagNo);
  92. }
  93. }
  94. }
  95. /**
  96. * Constructor from a given details.
  97. *
  98. * @param forward Certificates issued to this CA.
  99. * @param reverse Certificates issued by this CA to other CAs.
  100. */
  101. public CertificatePair(
  102. X509CertificateStructure forward,
  103. X509CertificateStructure reverse)
  104. {
  105. this.forward = forward;
  106. this.reverse = reverse;
  107. }
  108. /**
  109. * Produce an object suitable for an Asn1OutputStream.
  110. * <p/>
  111. * Returns:
  112. * <p/>
  113. * <pre>
  114. * CertificatePair ::= SEQUENCE {
  115. * forward [0] Certificate OPTIONAL,
  116. * reverse [1] Certificate OPTIONAL,
  117. * -- at least one of the pair shall be present -- }
  118. * </pre>
  119. *
  120. * @return a DERObject
  121. */
  122. public override Asn1Object ToAsn1Object()
  123. {
  124. Asn1EncodableVector v = new Asn1EncodableVector();
  125. v.AddOptionalTagged(true, 0, forward);
  126. v.AddOptionalTagged(true, 1, reverse);
  127. return new DerSequence(v);
  128. }
  129. /**
  130. * @return Returns the forward.
  131. */
  132. public X509CertificateStructure Forward
  133. {
  134. get { return forward; }
  135. }
  136. /**
  137. * @return Returns the reverse.
  138. */
  139. public X509CertificateStructure Reverse
  140. {
  141. get { return reverse; }
  142. }
  143. }
  144. }
  145. #pragma warning restore
  146. #endif