TBSCertificateStructure.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509
  7. {
  8. /**
  9. * The TbsCertificate object.
  10. * <pre>
  11. * TbsCertificate ::= Sequence {
  12. * version [ 0 ] Version DEFAULT v1(0),
  13. * serialNumber CertificateSerialNumber,
  14. * signature AlgorithmIdentifier,
  15. * issuer Name,
  16. * validity Validity,
  17. * subject Name,
  18. * subjectPublicKeyInfo SubjectPublicKeyInfo,
  19. * issuerUniqueID [ 1 ] IMPLICIT UniqueIdentifier OPTIONAL,
  20. * subjectUniqueID [ 2 ] IMPLICIT UniqueIdentifier OPTIONAL,
  21. * extensions [ 3 ] Extensions OPTIONAL
  22. * }
  23. * </pre>
  24. * <p>
  25. * Note: issuerUniqueID and subjectUniqueID are both deprecated by the IETF. This class
  26. * will parse them, but you really shouldn't be creating new ones.</p>
  27. */
  28. public class TbsCertificateStructure
  29. : Asn1Encodable
  30. {
  31. internal Asn1Sequence seq;
  32. internal DerInteger version;
  33. internal DerInteger serialNumber;
  34. internal AlgorithmIdentifier signature;
  35. internal X509Name issuer;
  36. internal Time startDate, endDate;
  37. internal X509Name subject;
  38. internal SubjectPublicKeyInfo subjectPublicKeyInfo;
  39. internal DerBitString issuerUniqueID;
  40. internal DerBitString subjectUniqueID;
  41. internal X509Extensions extensions;
  42. public static TbsCertificateStructure GetInstance(
  43. Asn1TaggedObject obj,
  44. bool explicitly)
  45. {
  46. return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
  47. }
  48. public static TbsCertificateStructure GetInstance(
  49. object obj)
  50. {
  51. if (obj is TbsCertificateStructure)
  52. return (TbsCertificateStructure) obj;
  53. if (obj != null)
  54. return new TbsCertificateStructure(Asn1Sequence.GetInstance(obj));
  55. return null;
  56. }
  57. internal TbsCertificateStructure(
  58. Asn1Sequence seq)
  59. {
  60. int seqStart = 0;
  61. this.seq = seq;
  62. //
  63. // some certficates don't include a version number - we assume v1
  64. //
  65. if (seq[0] is DerTaggedObject)
  66. {
  67. version = DerInteger.GetInstance((Asn1TaggedObject)seq[0], true);
  68. }
  69. else
  70. {
  71. seqStart = -1; // field 0 is missing!
  72. version = new DerInteger(0);
  73. }
  74. bool isV1 = false;
  75. bool isV2 = false;
  76. if (version.Value.Equals(BigInteger.Zero))
  77. {
  78. isV1 = true;
  79. }
  80. else if (version.Value.Equals(BigInteger.One))
  81. {
  82. isV2 = true;
  83. }
  84. else if (!version.Value.Equals(BigInteger.Two))
  85. {
  86. throw new ArgumentException("version number not recognised");
  87. }
  88. serialNumber = DerInteger.GetInstance(seq[seqStart + 1]);
  89. signature = AlgorithmIdentifier.GetInstance(seq[seqStart + 2]);
  90. issuer = X509Name.GetInstance(seq[seqStart + 3]);
  91. //
  92. // before and after dates
  93. //
  94. Asn1Sequence dates = (Asn1Sequence)seq[seqStart + 4];
  95. startDate = Time.GetInstance(dates[0]);
  96. endDate = Time.GetInstance(dates[1]);
  97. subject = X509Name.GetInstance(seq[seqStart + 5]);
  98. //
  99. // public key info.
  100. //
  101. subjectPublicKeyInfo = SubjectPublicKeyInfo.GetInstance(seq[seqStart + 6]);
  102. int extras = seq.Count - (seqStart + 6) - 1;
  103. if (extras != 0 && isV1)
  104. throw new ArgumentException("version 1 certificate contains extra data");
  105. while (extras > 0)
  106. {
  107. DerTaggedObject extra = (DerTaggedObject)seq[seqStart + 6 + extras];
  108. switch (extra.TagNo)
  109. {
  110. case 1:
  111. {
  112. issuerUniqueID = DerBitString.GetInstance(extra, false);
  113. break;
  114. }
  115. case 2:
  116. {
  117. subjectUniqueID = DerBitString.GetInstance(extra, false);
  118. break;
  119. }
  120. case 3:
  121. {
  122. if (isV2)
  123. throw new ArgumentException("version 2 certificate cannot contain extensions");
  124. extensions = X509Extensions.GetInstance(Asn1Sequence.GetInstance(extra, true));
  125. break;
  126. }
  127. default:
  128. {
  129. throw new ArgumentException("Unknown tag encountered in structure: " + extra.TagNo);
  130. }
  131. }
  132. extras--;
  133. }
  134. }
  135. public int Version
  136. {
  137. get { return version.Value.IntValue + 1; }
  138. }
  139. public DerInteger VersionNumber
  140. {
  141. get { return version; }
  142. }
  143. public DerInteger SerialNumber
  144. {
  145. get { return serialNumber; }
  146. }
  147. public AlgorithmIdentifier Signature
  148. {
  149. get { return signature; }
  150. }
  151. public X509Name Issuer
  152. {
  153. get { return issuer; }
  154. }
  155. public Time StartDate
  156. {
  157. get { return startDate; }
  158. }
  159. public Time EndDate
  160. {
  161. get { return endDate; }
  162. }
  163. public X509Name Subject
  164. {
  165. get { return subject; }
  166. }
  167. public SubjectPublicKeyInfo SubjectPublicKeyInfo
  168. {
  169. get { return subjectPublicKeyInfo; }
  170. }
  171. public DerBitString IssuerUniqueID
  172. {
  173. get { return issuerUniqueID; }
  174. }
  175. public DerBitString SubjectUniqueID
  176. {
  177. get { return subjectUniqueID; }
  178. }
  179. public X509Extensions Extensions
  180. {
  181. get { return extensions; }
  182. }
  183. public override Asn1Object ToAsn1Object()
  184. {
  185. return seq;
  186. }
  187. }
  188. }
  189. #pragma warning restore
  190. #endif