TBSCertificateStructure.cs 6.6 KB

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