Time.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Globalization;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509
  7. {
  8. public class Time
  9. : Asn1Encodable, IAsn1Choice
  10. {
  11. public static Time GetInstance(object obj)
  12. {
  13. if (obj == null)
  14. return null;
  15. if (obj is Time time)
  16. return time;
  17. if (obj is Asn1UtcTime utcTime)
  18. return new Time(utcTime);
  19. if (obj is Asn1GeneralizedTime generalizedTime)
  20. return new Time(generalizedTime);
  21. throw new ArgumentException("unknown object in factory: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), nameof(obj));
  22. }
  23. public static Time GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
  24. {
  25. return GetInstance(taggedObject.GetObject());
  26. }
  27. private readonly Asn1Object m_timeObject;
  28. public Time(Asn1GeneralizedTime generalizedTime)
  29. {
  30. this.m_timeObject = generalizedTime ?? throw new ArgumentNullException(nameof(generalizedTime));
  31. }
  32. public Time(Asn1UtcTime utcTime)
  33. {
  34. if (utcTime == null)
  35. throw new ArgumentNullException(nameof(utcTime));
  36. // Validate utcTime is in the appropriate year range
  37. utcTime.ToDateTime(2049);
  38. this.m_timeObject = utcTime;
  39. }
  40. /**
  41. * creates a time object from a given date - if the date is between 1950
  42. * and 2049 a UTCTime object is Generated, otherwise a GeneralizedTime
  43. * is used.
  44. */
  45. public Time(DateTime date)
  46. {
  47. DateTime utc = date.ToUniversalTime();
  48. if (utc.Year < 1950 || utc.Year > 2049)
  49. {
  50. m_timeObject = new DerGeneralizedTime(utc);
  51. }
  52. else
  53. {
  54. m_timeObject = new DerUtcTime(utc, 2049);
  55. }
  56. }
  57. /// <summary>
  58. /// Return our time as DateTime.
  59. /// </summary>
  60. /// <returns>A date time.</returns>
  61. public DateTime ToDateTime()
  62. {
  63. try
  64. {
  65. if (m_timeObject is Asn1UtcTime utcTime)
  66. return utcTime.ToDateTime(2049);
  67. return ((Asn1GeneralizedTime)m_timeObject).ToDateTime();
  68. }
  69. catch (FormatException e)
  70. {
  71. // this should never happen
  72. throw new InvalidOperationException("invalid date string: " + e.Message);
  73. }
  74. }
  75. /**
  76. * Produce an object suitable for an Asn1OutputStream.
  77. * <pre>
  78. * Time ::= CHOICE {
  79. * utcTime UTCTime,
  80. * generalTime GeneralizedTime }
  81. * </pre>
  82. */
  83. public override Asn1Object ToAsn1Object()
  84. {
  85. return m_timeObject;
  86. }
  87. public override string ToString()
  88. {
  89. if (m_timeObject is Asn1UtcTime utcTime)
  90. return utcTime.ToDateTime(2049).ToString(@"yyyyMMddHHmmssK", DateTimeFormatInfo.InvariantInfo);
  91. if (m_timeObject is Asn1GeneralizedTime generalizedTime)
  92. return generalizedTime.ToDateTime().ToString(@"yyyyMMddHHmmss.FFFFFFFK", DateTimeFormatInfo.InvariantInfo);
  93. throw new InvalidOperationException();
  94. }
  95. }
  96. }
  97. #pragma warning restore
  98. #endif