TBSRequest.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Ocsp
  7. {
  8. public class TbsRequest
  9. : Asn1Encodable
  10. {
  11. private static readonly DerInteger V1 = new DerInteger(0);
  12. private readonly DerInteger version;
  13. private readonly GeneralName requestorName;
  14. private readonly Asn1Sequence requestList;
  15. private readonly X509Extensions requestExtensions;
  16. private bool versionSet;
  17. public static TbsRequest GetInstance(
  18. Asn1TaggedObject obj,
  19. bool explicitly)
  20. {
  21. return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
  22. }
  23. public static TbsRequest GetInstance(
  24. object obj)
  25. {
  26. if (obj == null || obj is TbsRequest)
  27. {
  28. return (TbsRequest)obj;
  29. }
  30. if (obj is Asn1Sequence)
  31. {
  32. return new TbsRequest((Asn1Sequence)obj);
  33. }
  34. throw new ArgumentException("unknown object in factory: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  35. }
  36. public TbsRequest(
  37. GeneralName requestorName,
  38. Asn1Sequence requestList,
  39. X509Extensions requestExtensions)
  40. {
  41. this.version = V1;
  42. this.requestorName = requestorName;
  43. this.requestList = requestList;
  44. this.requestExtensions = requestExtensions;
  45. }
  46. private TbsRequest(
  47. Asn1Sequence seq)
  48. {
  49. int index = 0;
  50. Asn1Encodable enc = seq[0];
  51. if (enc is Asn1TaggedObject)
  52. {
  53. Asn1TaggedObject o = (Asn1TaggedObject) enc;
  54. if (o.TagNo == 0)
  55. {
  56. versionSet = true;
  57. version = DerInteger.GetInstance(o, true);
  58. index++;
  59. }
  60. else
  61. {
  62. version = V1;
  63. }
  64. }
  65. else
  66. {
  67. version = V1;
  68. }
  69. if (seq[index] is Asn1TaggedObject)
  70. {
  71. requestorName = GeneralName.GetInstance((Asn1TaggedObject) seq[index++], true);
  72. }
  73. requestList = (Asn1Sequence) seq[index++];
  74. if (seq.Count == (index + 1))
  75. {
  76. requestExtensions = X509Extensions.GetInstance((Asn1TaggedObject) seq[index], true);
  77. }
  78. }
  79. public DerInteger Version
  80. {
  81. get { return version; }
  82. }
  83. public GeneralName RequestorName
  84. {
  85. get { return requestorName; }
  86. }
  87. public Asn1Sequence RequestList
  88. {
  89. get { return requestList; }
  90. }
  91. public X509Extensions RequestExtensions
  92. {
  93. get { return requestExtensions; }
  94. }
  95. /**
  96. * Produce an object suitable for an Asn1OutputStream.
  97. * <pre>
  98. * TBSRequest ::= Sequence {
  99. * version [0] EXPLICIT Version DEFAULT v1,
  100. * requestorName [1] EXPLICIT GeneralName OPTIONAL,
  101. * requestList Sequence OF Request,
  102. * requestExtensions [2] EXPLICIT Extensions OPTIONAL }
  103. * </pre>
  104. */
  105. public override Asn1Object ToAsn1Object()
  106. {
  107. Asn1EncodableVector v = new Asn1EncodableVector();
  108. //
  109. // if default don't include - unless explicitly provided. Not strictly correct
  110. // but required for some requests
  111. //
  112. if (!version.Equals(V1) || versionSet)
  113. {
  114. v.Add(new DerTaggedObject(true, 0, version));
  115. }
  116. v.AddOptionalTagged(true, 1, requestorName);
  117. v.Add(requestList);
  118. v.AddOptionalTagged(true, 2, requestExtensions);
  119. return new DerSequence(v);
  120. }
  121. }
  122. }
  123. #pragma warning restore
  124. #endif