CmpCertificate.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cmp
  8. {
  9. public class CmpCertificate
  10. : Asn1Encodable, IAsn1Choice
  11. {
  12. public static CmpCertificate GetInstance(object obj)
  13. {
  14. // TODO[cmp] Review this whole metho
  15. if (obj == null)
  16. return null;
  17. if (obj is CmpCertificate cmpCertificate)
  18. return cmpCertificate;
  19. if (obj is byte[] bs)
  20. {
  21. try
  22. {
  23. obj = Asn1Object.FromByteArray(bs);
  24. }
  25. catch (IOException)
  26. {
  27. throw new ArgumentException("Invalid encoding in CmpCertificate");
  28. }
  29. }
  30. if (obj is Asn1Sequence)
  31. return new CmpCertificate(X509CertificateStructure.GetInstance(obj));
  32. if (obj is Asn1TaggedObject taggedObject)
  33. return new CmpCertificate(taggedObject.TagNo, taggedObject.GetObject());
  34. throw new ArgumentException("Invalid object: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), nameof(obj));
  35. }
  36. public static CmpCertificate GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
  37. {
  38. // TODO[cmp]
  39. if (taggedObject == null)
  40. return null;
  41. if (!declaredExplicit)
  42. throw new ArgumentException("tag must be explicit");
  43. // TODO[cmp]
  44. return GetInstance(taggedObject.GetObject());
  45. }
  46. private readonly X509CertificateStructure m_x509v3PKCert;
  47. private readonly int m_otherTagValue;
  48. private readonly Asn1Encodable m_otherCert;
  49. /**
  50. * Note: the addition of other certificates is a BC extension. If you use this constructor they
  51. * will be added with an explicit tag value of type.
  52. *
  53. * @param type the type of the certificate (used as a tag value).
  54. * @param otherCert the object representing the certificate
  55. */
  56. public CmpCertificate(int type, Asn1Encodable otherCert)
  57. {
  58. m_otherTagValue = type;
  59. m_otherCert = otherCert;
  60. }
  61. public CmpCertificate(X509CertificateStructure x509v3PKCert)
  62. {
  63. if (x509v3PKCert.Version != 3)
  64. throw new ArgumentException("only version 3 certificates allowed", nameof(x509v3PKCert));
  65. m_x509v3PKCert = x509v3PKCert;
  66. }
  67. public virtual bool IsX509v3PKCert => m_x509v3PKCert != null;
  68. public virtual X509CertificateStructure X509v3PKCert => m_x509v3PKCert;
  69. public virtual int OtherCertTag => m_otherTagValue;
  70. public virtual Asn1Encodable OtherCert => m_otherCert;
  71. /**
  72. * <pre>
  73. * CMPCertificate ::= CHOICE {
  74. * x509v3PKCert Certificate
  75. * x509v2AttrCert [1] AttributeCertificate
  76. * }
  77. * </pre>
  78. * Note: the addition of attribute certificates is a BC extension.
  79. *
  80. * @return a basic ASN.1 object representation.
  81. */
  82. public override Asn1Object ToAsn1Object()
  83. {
  84. if (m_otherCert != null)
  85. {
  86. // explicit following CMP conventions
  87. return new DerTaggedObject(true, m_otherTagValue, m_otherCert);
  88. }
  89. return m_x509v3PKCert.ToAsn1Object();
  90. }
  91. }
  92. }
  93. #pragma warning restore
  94. #endif