X509CertificateStructure.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Asn1.Pkcs;
  4. namespace Org.BouncyCastle.Asn1.X509
  5. {
  6. /**
  7. * an X509Certificate structure.
  8. * <pre>
  9. * Certificate ::= Sequence {
  10. * tbsCertificate TbsCertificate,
  11. * signatureAlgorithm AlgorithmIdentifier,
  12. * signature BIT STRING
  13. * }
  14. * </pre>
  15. */
  16. public class X509CertificateStructure
  17. : Asn1Encodable
  18. {
  19. private readonly TbsCertificateStructure tbsCert;
  20. private readonly AlgorithmIdentifier sigAlgID;
  21. private readonly DerBitString sig;
  22. public static X509CertificateStructure GetInstance(
  23. Asn1TaggedObject obj,
  24. bool explicitly)
  25. {
  26. return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
  27. }
  28. public static X509CertificateStructure GetInstance(
  29. object obj)
  30. {
  31. if (obj is X509CertificateStructure)
  32. return (X509CertificateStructure)obj;
  33. if (obj == null)
  34. return null;
  35. return new X509CertificateStructure(Asn1Sequence.GetInstance(obj));
  36. }
  37. public X509CertificateStructure(
  38. TbsCertificateStructure tbsCert,
  39. AlgorithmIdentifier sigAlgID,
  40. DerBitString sig)
  41. {
  42. if (tbsCert == null)
  43. throw new ArgumentNullException("tbsCert");
  44. if (sigAlgID == null)
  45. throw new ArgumentNullException("sigAlgID");
  46. if (sig == null)
  47. throw new ArgumentNullException("sig");
  48. this.tbsCert = tbsCert;
  49. this.sigAlgID = sigAlgID;
  50. this.sig = sig;
  51. }
  52. private X509CertificateStructure(
  53. Asn1Sequence seq)
  54. {
  55. if (seq.Count != 3)
  56. throw new ArgumentException("sequence wrong size for a certificate", "seq");
  57. //
  58. // correct x509 certficate
  59. //
  60. tbsCert = TbsCertificateStructure.GetInstance(seq[0]);
  61. sigAlgID = AlgorithmIdentifier.GetInstance(seq[1]);
  62. sig = DerBitString.GetInstance(seq[2]);
  63. }
  64. public TbsCertificateStructure TbsCertificate
  65. {
  66. get { return tbsCert; }
  67. }
  68. public int Version
  69. {
  70. get { return tbsCert.Version; }
  71. }
  72. public DerInteger SerialNumber
  73. {
  74. get { return tbsCert.SerialNumber; }
  75. }
  76. public X509Name Issuer
  77. {
  78. get { return tbsCert.Issuer; }
  79. }
  80. public Time StartDate
  81. {
  82. get { return tbsCert.StartDate; }
  83. }
  84. public Time EndDate
  85. {
  86. get { return tbsCert.EndDate; }
  87. }
  88. public X509Name Subject
  89. {
  90. get { return tbsCert.Subject; }
  91. }
  92. public SubjectPublicKeyInfo SubjectPublicKeyInfo
  93. {
  94. get { return tbsCert.SubjectPublicKeyInfo; }
  95. }
  96. public AlgorithmIdentifier SignatureAlgorithm
  97. {
  98. get { return sigAlgID; }
  99. }
  100. public DerBitString Signature
  101. {
  102. get { return sig; }
  103. }
  104. public byte[] GetSignatureOctets()
  105. {
  106. return sig.GetOctets();
  107. }
  108. public override Asn1Object ToAsn1Object()
  109. {
  110. return new DerSequence(tbsCert, sigAlgID, sig);
  111. }
  112. }
  113. }
  114. #endif