CmpCertificate.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cmp
  7. {
  8. public class CmpCertificate
  9. : Asn1Encodable, IAsn1Choice
  10. {
  11. private readonly X509CertificateStructure x509v3PKCert;
  12. private readonly AttributeCertificate x509v2AttrCert;
  13. /**
  14. * Note: the addition of attribute certificates is a BC extension.
  15. */
  16. public CmpCertificate(AttributeCertificate x509v2AttrCert)
  17. {
  18. this.x509v2AttrCert = x509v2AttrCert;
  19. }
  20. public CmpCertificate(X509CertificateStructure x509v3PKCert)
  21. {
  22. if (x509v3PKCert.Version != 3)
  23. throw new ArgumentException("only version 3 certificates allowed", "x509v3PKCert");
  24. this.x509v3PKCert = x509v3PKCert;
  25. }
  26. public static CmpCertificate GetInstance(object obj)
  27. {
  28. if (obj is CmpCertificate)
  29. return (CmpCertificate)obj;
  30. if (obj is Asn1Sequence)
  31. return new CmpCertificate(X509CertificateStructure.GetInstance(obj));
  32. if (obj is Asn1TaggedObject)
  33. return new CmpCertificate(AttributeCertificate.GetInstance(((Asn1TaggedObject)obj).GetObject()));
  34. throw new ArgumentException("Invalid object: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  35. }
  36. public virtual bool IsX509v3PKCert
  37. {
  38. get { return x509v3PKCert != null; }
  39. }
  40. public virtual X509CertificateStructure X509v3PKCert
  41. {
  42. get { return x509v3PKCert; }
  43. }
  44. public virtual AttributeCertificate X509v2AttrCert
  45. {
  46. get { return x509v2AttrCert; }
  47. }
  48. /**
  49. * <pre>
  50. * CMPCertificate ::= CHOICE {
  51. * x509v3PKCert Certificate
  52. * x509v2AttrCert [1] AttributeCertificate
  53. * }
  54. * </pre>
  55. * Note: the addition of attribute certificates is a BC extension.
  56. *
  57. * @return a basic ASN.1 object representation.
  58. */
  59. public override Asn1Object ToAsn1Object()
  60. {
  61. if (x509v2AttrCert != null)
  62. {
  63. // explicit following CMP conventions
  64. return new DerTaggedObject(true, 1, x509v2AttrCert);
  65. }
  66. return x509v3PKCert.ToAsn1Object();
  67. }
  68. }
  69. }
  70. #pragma warning restore
  71. #endif