CertOrEncCert.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Crmf;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cmp
  7. {
  8. public class CertOrEncCert
  9. : Asn1Encodable, IAsn1Choice
  10. {
  11. public static CertOrEncCert GetInstance(object obj)
  12. {
  13. if (obj is CertOrEncCert certOrEncCert)
  14. return certOrEncCert;
  15. if (obj is Asn1TaggedObject taggedObject)
  16. return new CertOrEncCert(taggedObject);
  17. throw new ArgumentException("Invalid object: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), nameof(obj));
  18. }
  19. private readonly CmpCertificate m_certificate;
  20. private readonly EncryptedKey m_encryptedCert;
  21. private CertOrEncCert(Asn1TaggedObject taggedObject)
  22. {
  23. if (taggedObject.TagNo == 0)
  24. {
  25. m_certificate = CmpCertificate.GetInstance(taggedObject.GetObject());
  26. }
  27. else if (taggedObject.TagNo == 1)
  28. {
  29. m_encryptedCert = EncryptedKey.GetInstance(taggedObject.GetObject());
  30. }
  31. else
  32. {
  33. throw new ArgumentException("unknown tag: " + taggedObject.TagNo, nameof(taggedObject));
  34. }
  35. }
  36. public CertOrEncCert(CmpCertificate certificate)
  37. {
  38. if (certificate == null)
  39. throw new ArgumentNullException(nameof(certificate));
  40. m_certificate = certificate;
  41. }
  42. public CertOrEncCert(EncryptedValue encryptedValue)
  43. {
  44. if (encryptedValue == null)
  45. throw new ArgumentNullException(nameof(encryptedValue));
  46. m_encryptedCert = new EncryptedKey(encryptedValue);
  47. }
  48. public CertOrEncCert(EncryptedKey encryptedKey)
  49. {
  50. if (encryptedKey == null)
  51. throw new ArgumentNullException(nameof(encryptedKey));
  52. m_encryptedCert = encryptedKey;
  53. }
  54. public virtual CmpCertificate Certificate => m_certificate;
  55. public virtual EncryptedKey EncryptedCert => m_encryptedCert;
  56. /**
  57. * <pre>
  58. * CertOrEncCert ::= CHOICE {
  59. * certificate [0] CMPCertificate,
  60. * encryptedCert [1] EncryptedKey
  61. * }
  62. * </pre>
  63. * @return a basic ASN.1 object representation.
  64. */
  65. public override Asn1Object ToAsn1Object()
  66. {
  67. if (m_certificate != null)
  68. return new DerTaggedObject(true, 0, m_certificate);
  69. return new DerTaggedObject(true, 1, m_encryptedCert);
  70. }
  71. }
  72. }
  73. #pragma warning restore
  74. #endif