Accuracy.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.Tsp
  6. {
  7. public class Accuracy
  8. : Asn1Encodable
  9. {
  10. private readonly DerInteger seconds;
  11. private readonly DerInteger millis;
  12. private readonly DerInteger micros;
  13. // constants
  14. protected const int MinMillis = 1;
  15. protected const int MaxMillis = 999;
  16. protected const int MinMicros = 1;
  17. protected const int MaxMicros = 999;
  18. public Accuracy(
  19. DerInteger seconds,
  20. DerInteger millis,
  21. DerInteger micros)
  22. {
  23. //Verifications
  24. if (millis != null
  25. && (millis.Value.IntValue < MinMillis
  26. || millis.Value.IntValue > MaxMillis))
  27. {
  28. throw new ArgumentException(
  29. "Invalid millis field : not in (1..999)");
  30. }
  31. if (micros != null
  32. && (micros.Value.IntValue < MinMicros
  33. || micros.Value.IntValue > MaxMicros))
  34. {
  35. throw new ArgumentException(
  36. "Invalid micros field : not in (1..999)");
  37. }
  38. this.seconds = seconds;
  39. this.millis = millis;
  40. this.micros = micros;
  41. }
  42. private Accuracy(
  43. Asn1Sequence seq)
  44. {
  45. for (int i = 0; i < seq.Count; ++i)
  46. {
  47. // seconds
  48. if (seq[i] is DerInteger)
  49. {
  50. seconds = (DerInteger) seq[i];
  51. }
  52. else if (seq[i] is DerTaggedObject)
  53. {
  54. DerTaggedObject extra = (DerTaggedObject) seq[i];
  55. switch (extra.TagNo)
  56. {
  57. case 0:
  58. millis = DerInteger.GetInstance(extra, false);
  59. if (millis.Value.IntValue < MinMillis
  60. || millis.Value.IntValue > MaxMillis)
  61. {
  62. throw new ArgumentException(
  63. "Invalid millis field : not in (1..999).");
  64. }
  65. break;
  66. case 1:
  67. micros = DerInteger.GetInstance(extra, false);
  68. if (micros.Value.IntValue < MinMicros
  69. || micros.Value.IntValue > MaxMicros)
  70. {
  71. throw new ArgumentException(
  72. "Invalid micros field : not in (1..999).");
  73. }
  74. break;
  75. default:
  76. throw new ArgumentException("Invalig tag number");
  77. }
  78. }
  79. }
  80. }
  81. public static Accuracy GetInstance(
  82. object o)
  83. {
  84. if (o == null || o is Accuracy)
  85. {
  86. return (Accuracy) o;
  87. }
  88. if (o is Asn1Sequence)
  89. {
  90. return new Accuracy((Asn1Sequence) o);
  91. }
  92. throw new ArgumentException(
  93. "Unknown object in 'Accuracy' factory: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(o));
  94. }
  95. public DerInteger Seconds
  96. {
  97. get { return seconds; }
  98. }
  99. public DerInteger Millis
  100. {
  101. get { return millis; }
  102. }
  103. public DerInteger Micros
  104. {
  105. get { return micros; }
  106. }
  107. /**
  108. * <pre>
  109. * Accuracy ::= SEQUENCE {
  110. * seconds INTEGER OPTIONAL,
  111. * millis [0] INTEGER (1..999) OPTIONAL,
  112. * micros [1] INTEGER (1..999) OPTIONAL
  113. * }
  114. * </pre>
  115. */
  116. public override Asn1Object ToAsn1Object()
  117. {
  118. Asn1EncodableVector v = new Asn1EncodableVector();
  119. if (seconds != null)
  120. {
  121. v.Add(seconds);
  122. }
  123. if (millis != null)
  124. {
  125. v.Add(new DerTaggedObject(false, 0, millis));
  126. }
  127. if (micros != null)
  128. {
  129. v.Add(new DerTaggedObject(false, 1, micros));
  130. }
  131. return new DerSequence(v);
  132. }
  133. }
  134. }
  135. #pragma warning restore
  136. #endif