TBSCertificateStructure.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. * The TbsCertificate object.
  8. * <pre>
  9. * TbsCertificate ::= Sequence {
  10. * version [ 0 ] Version DEFAULT v1(0),
  11. * serialNumber CertificateSerialNumber,
  12. * signature AlgorithmIdentifier,
  13. * issuer Name,
  14. * validity Validity,
  15. * subject Name,
  16. * subjectPublicKeyInfo SubjectPublicKeyInfo,
  17. * issuerUniqueID [ 1 ] IMPLICIT UniqueIdentifier OPTIONAL,
  18. * subjectUniqueID [ 2 ] IMPLICIT UniqueIdentifier OPTIONAL,
  19. * extensions [ 3 ] Extensions OPTIONAL
  20. * }
  21. * </pre>
  22. * <p>
  23. * Note: issuerUniqueID and subjectUniqueID are both deprecated by the IETF. This class
  24. * will parse them, but you really shouldn't be creating new ones.</p>
  25. */
  26. public class TbsCertificateStructure
  27. : Asn1Encodable
  28. {
  29. internal Asn1Sequence seq;
  30. internal DerInteger version;
  31. internal DerInteger serialNumber;
  32. internal AlgorithmIdentifier signature;
  33. internal X509Name issuer;
  34. internal Time startDate, endDate;
  35. internal X509Name subject;
  36. internal SubjectPublicKeyInfo subjectPublicKeyInfo;
  37. internal DerBitString issuerUniqueID;
  38. internal DerBitString subjectUniqueID;
  39. internal X509Extensions extensions;
  40. public static TbsCertificateStructure GetInstance(
  41. Asn1TaggedObject obj,
  42. bool explicitly)
  43. {
  44. return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
  45. }
  46. public static TbsCertificateStructure GetInstance(
  47. object obj)
  48. {
  49. if (obj is TbsCertificateStructure)
  50. return (TbsCertificateStructure) obj;
  51. if (obj != null)
  52. return new TbsCertificateStructure(Asn1Sequence.GetInstance(obj));
  53. return null;
  54. }
  55. internal TbsCertificateStructure(
  56. Asn1Sequence seq)
  57. {
  58. int seqStart = 0;
  59. this.seq = seq;
  60. //
  61. // some certficates don't include a version number - we assume v1
  62. //
  63. if (seq[0] is DerTaggedObject)
  64. {
  65. version = DerInteger.GetInstance((Asn1TaggedObject)seq[0], true);
  66. }
  67. else
  68. {
  69. seqStart = -1; // field 0 is missing!
  70. version = new DerInteger(0);
  71. }
  72. serialNumber = DerInteger.GetInstance(seq[seqStart + 1]);
  73. signature = AlgorithmIdentifier.GetInstance(seq[seqStart + 2]);
  74. issuer = X509Name.GetInstance(seq[seqStart + 3]);
  75. //
  76. // before and after dates
  77. //
  78. Asn1Sequence dates = (Asn1Sequence)seq[seqStart + 4];
  79. startDate = Time.GetInstance(dates[0]);
  80. endDate = Time.GetInstance(dates[1]);
  81. subject = X509Name.GetInstance(seq[seqStart + 5]);
  82. //
  83. // public key info.
  84. //
  85. subjectPublicKeyInfo = SubjectPublicKeyInfo.GetInstance(seq[seqStart + 6]);
  86. for (int extras = seq.Count - (seqStart + 6) - 1; extras > 0; extras--)
  87. {
  88. DerTaggedObject extra = (DerTaggedObject) seq[seqStart + 6 + extras];
  89. switch (extra.TagNo)
  90. {
  91. case 1:
  92. issuerUniqueID = DerBitString.GetInstance(extra, false);
  93. break;
  94. case 2:
  95. subjectUniqueID = DerBitString.GetInstance(extra, false);
  96. break;
  97. case 3:
  98. extensions = X509Extensions.GetInstance(extra);
  99. break;
  100. }
  101. }
  102. }
  103. public int Version
  104. {
  105. get { return version.Value.IntValue + 1; }
  106. }
  107. public DerInteger VersionNumber
  108. {
  109. get { return version; }
  110. }
  111. public DerInteger SerialNumber
  112. {
  113. get { return serialNumber; }
  114. }
  115. public AlgorithmIdentifier Signature
  116. {
  117. get { return signature; }
  118. }
  119. public X509Name Issuer
  120. {
  121. get { return issuer; }
  122. }
  123. public Time StartDate
  124. {
  125. get { return startDate; }
  126. }
  127. public Time EndDate
  128. {
  129. get { return endDate; }
  130. }
  131. public X509Name Subject
  132. {
  133. get { return subject; }
  134. }
  135. public SubjectPublicKeyInfo SubjectPublicKeyInfo
  136. {
  137. get { return subjectPublicKeyInfo; }
  138. }
  139. public DerBitString IssuerUniqueID
  140. {
  141. get { return issuerUniqueID; }
  142. }
  143. public DerBitString SubjectUniqueID
  144. {
  145. get { return subjectUniqueID; }
  146. }
  147. public X509Extensions Extensions
  148. {
  149. get { return extensions; }
  150. }
  151. public override Asn1Object ToAsn1Object()
  152. {
  153. return seq;
  154. }
  155. }
  156. }
  157. #endif