V3TBSCertificateGenerator.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509
  5. {
  6. /**
  7. * Generator for Version 3 TbsCertificateStructures.
  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. *
  23. */
  24. public class V3TbsCertificateGenerator
  25. {
  26. internal DerTaggedObject version = new DerTaggedObject(0, new DerInteger(2));
  27. internal DerInteger serialNumber;
  28. internal AlgorithmIdentifier signature;
  29. internal X509Name issuer;
  30. internal Time startDate, endDate;
  31. internal X509Name subject;
  32. internal SubjectPublicKeyInfo subjectPublicKeyInfo;
  33. internal X509Extensions extensions;
  34. private bool altNamePresentAndCritical;
  35. private DerBitString issuerUniqueID;
  36. private DerBitString subjectUniqueID;
  37. public V3TbsCertificateGenerator()
  38. {
  39. }
  40. public void SetSerialNumber(
  41. DerInteger serialNumber)
  42. {
  43. this.serialNumber = serialNumber;
  44. }
  45. public void SetSignature(
  46. AlgorithmIdentifier signature)
  47. {
  48. this.signature = signature;
  49. }
  50. public void SetIssuer(
  51. X509Name issuer)
  52. {
  53. this.issuer = issuer;
  54. }
  55. public void SetStartDate(
  56. DerUtcTime startDate)
  57. {
  58. this.startDate = new Time(startDate);
  59. }
  60. public void SetStartDate(
  61. Time startDate)
  62. {
  63. this.startDate = startDate;
  64. }
  65. public void SetEndDate(
  66. DerUtcTime endDate)
  67. {
  68. this.endDate = new Time(endDate);
  69. }
  70. public void SetEndDate(
  71. Time endDate)
  72. {
  73. this.endDate = endDate;
  74. }
  75. public void SetSubject(
  76. X509Name subject)
  77. {
  78. this.subject = subject;
  79. }
  80. public void SetIssuerUniqueID(
  81. DerBitString uniqueID)
  82. {
  83. this.issuerUniqueID = uniqueID;
  84. }
  85. public void SetSubjectUniqueID(
  86. DerBitString uniqueID)
  87. {
  88. this.subjectUniqueID = uniqueID;
  89. }
  90. public void SetSubjectPublicKeyInfo(
  91. SubjectPublicKeyInfo pubKeyInfo)
  92. {
  93. this.subjectPublicKeyInfo = pubKeyInfo;
  94. }
  95. public void SetExtensions(
  96. X509Extensions extensions)
  97. {
  98. this.extensions = extensions;
  99. if (extensions != null)
  100. {
  101. X509Extension altName = extensions.GetExtension(X509Extensions.SubjectAlternativeName);
  102. if (altName != null && altName.IsCritical)
  103. {
  104. altNamePresentAndCritical = true;
  105. }
  106. }
  107. }
  108. public TbsCertificateStructure GenerateTbsCertificate()
  109. {
  110. if ((serialNumber == null) || (signature == null)
  111. || (issuer == null) || (startDate == null) || (endDate == null)
  112. || (subject == null && !altNamePresentAndCritical)
  113. || (subjectPublicKeyInfo == null))
  114. {
  115. throw new InvalidOperationException("not all mandatory fields set in V3 TBScertificate generator");
  116. }
  117. DerSequence validity = new DerSequence(startDate, endDate); // before and after dates
  118. Asn1EncodableVector v = new Asn1EncodableVector(
  119. version, serialNumber, signature, issuer, validity);
  120. if (subject != null)
  121. {
  122. v.Add(subject);
  123. }
  124. else
  125. {
  126. v.Add(DerSequence.Empty);
  127. }
  128. v.Add(subjectPublicKeyInfo);
  129. if (issuerUniqueID != null)
  130. {
  131. v.Add(new DerTaggedObject(false, 1, issuerUniqueID));
  132. }
  133. if (subjectUniqueID != null)
  134. {
  135. v.Add(new DerTaggedObject(false, 2, subjectUniqueID));
  136. }
  137. if (extensions != null)
  138. {
  139. v.Add(new DerTaggedObject(3, extensions));
  140. }
  141. return new TbsCertificateStructure(new DerSequence(v));
  142. }
  143. }
  144. }
  145. #pragma warning restore
  146. #endif