DerUTCTime.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Globalization;
  5. using System.Text;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1
  8. {
  9. /**
  10. * UTC time object.
  11. */
  12. public class DerUtcTime
  13. : Asn1Object
  14. {
  15. private readonly string time;
  16. /**
  17. * return an UTC Time from the passed in object.
  18. *
  19. * @exception ArgumentException if the object cannot be converted.
  20. */
  21. public static DerUtcTime GetInstance(
  22. object obj)
  23. {
  24. if (obj == null || obj is DerUtcTime)
  25. {
  26. return (DerUtcTime)obj;
  27. }
  28. throw new ArgumentException("illegal object in GetInstance: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  29. }
  30. /**
  31. * return an UTC Time from a tagged object.
  32. *
  33. * @param obj the tagged object holding the object we want
  34. * @param explicitly true if the object is meant to be explicitly
  35. * tagged false otherwise.
  36. * @exception ArgumentException if the tagged object cannot
  37. * be converted.
  38. */
  39. public static DerUtcTime GetInstance(
  40. Asn1TaggedObject obj,
  41. bool isExplicit)
  42. {
  43. Asn1Object o = obj.GetObject();
  44. if (isExplicit || o is DerUtcTime)
  45. {
  46. return GetInstance(o);
  47. }
  48. return new DerUtcTime(((Asn1OctetString)o).GetOctets());
  49. }
  50. /**
  51. * The correct format for this is YYMMDDHHMMSSZ (it used to be that seconds were
  52. * never encoded. When you're creating one of these objects from scratch, that's
  53. * what you want to use, otherwise we'll try to deal with whatever Gets read from
  54. * the input stream... (this is why the input format is different from the GetTime()
  55. * method output).
  56. * <p>
  57. * @param time the time string.</p>
  58. */
  59. public DerUtcTime(
  60. string time)
  61. {
  62. if (time == null)
  63. throw new ArgumentNullException("time");
  64. this.time = time;
  65. try
  66. {
  67. ToDateTime();
  68. }
  69. catch (FormatException e)
  70. {
  71. throw new ArgumentException("invalid date string: " + e.Message);
  72. }
  73. }
  74. /**
  75. * base constructor from a DateTime object
  76. */
  77. public DerUtcTime(
  78. DateTime time)
  79. {
  80. #if PORTABLE || NETFX_CORE
  81. this.time = time.ToUniversalTime().ToString("yyMMddHHmmss", CultureInfo.InvariantCulture) + "Z";
  82. #else
  83. this.time = time.ToString("yyMMddHHmmss", CultureInfo.InvariantCulture) + "Z";
  84. #endif
  85. }
  86. internal DerUtcTime(
  87. byte[] bytes)
  88. {
  89. //
  90. // explicitly convert to characters
  91. //
  92. this.time = Strings.FromAsciiByteArray(bytes);
  93. }
  94. // public DateTime ToDateTime()
  95. // {
  96. // string tm = this.AdjustedTimeString;
  97. //
  98. // return new DateTime(
  99. // Int16.Parse(tm.Substring(0, 4)),
  100. // Int16.Parse(tm.Substring(4, 2)),
  101. // Int16.Parse(tm.Substring(6, 2)),
  102. // Int16.Parse(tm.Substring(8, 2)),
  103. // Int16.Parse(tm.Substring(10, 2)),
  104. // Int16.Parse(tm.Substring(12, 2)));
  105. // }
  106. /**
  107. * return the time as a date based on whatever a 2 digit year will return. For
  108. * standardised processing use ToAdjustedDateTime().
  109. *
  110. * @return the resulting date
  111. * @exception ParseException if the date string cannot be parsed.
  112. */
  113. public DateTime ToDateTime()
  114. {
  115. return ParseDateString(TimeString, @"yyMMddHHmmss'GMT'zzz");
  116. }
  117. /**
  118. * return the time as an adjusted date
  119. * in the range of 1950 - 2049.
  120. *
  121. * @return a date in the range of 1950 to 2049.
  122. * @exception ParseException if the date string cannot be parsed.
  123. */
  124. public DateTime ToAdjustedDateTime()
  125. {
  126. return ParseDateString(AdjustedTimeString, @"yyyyMMddHHmmss'GMT'zzz");
  127. }
  128. private DateTime ParseDateString(
  129. string dateStr,
  130. string formatStr)
  131. {
  132. DateTime dt = DateTime.ParseExact(
  133. dateStr,
  134. formatStr,
  135. DateTimeFormatInfo.InvariantInfo);
  136. return dt.ToUniversalTime();
  137. }
  138. /**
  139. * return the time - always in the form of
  140. * YYMMDDhhmmssGMT(+hh:mm|-hh:mm).
  141. * <p>
  142. * Normally in a certificate we would expect "Z" rather than "GMT",
  143. * however adding the "GMT" means we can just use:
  144. * <pre>
  145. * dateF = new SimpleDateFormat("yyMMddHHmmssz");
  146. * </pre>
  147. * To read in the time and Get a date which is compatible with our local
  148. * time zone.</p>
  149. * <p>
  150. * <b>Note:</b> In some cases, due to the local date processing, this
  151. * may lead to unexpected results. If you want to stick the normal
  152. * convention of 1950 to 2049 use the GetAdjustedTime() method.</p>
  153. */
  154. public string TimeString
  155. {
  156. get
  157. {
  158. //
  159. // standardise the format.
  160. //
  161. if (time.IndexOf('-') < 0 && time.IndexOf('+') < 0)
  162. {
  163. if (time.Length == 11)
  164. {
  165. return time.Substring(0, 10) + "00GMT+00:00";
  166. }
  167. else
  168. {
  169. return time.Substring(0, 12) + "GMT+00:00";
  170. }
  171. }
  172. else
  173. {
  174. int index = time.IndexOf('-');
  175. if (index < 0)
  176. {
  177. index = time.IndexOf('+');
  178. }
  179. string d = time;
  180. if (index == time.Length - 3)
  181. {
  182. d += "00";
  183. }
  184. if (index == 10)
  185. {
  186. return d.Substring(0, 10) + "00GMT" + d.Substring(10, 3) + ":" + d.Substring(13, 2);
  187. }
  188. else
  189. {
  190. return d.Substring(0, 12) + "GMT" + d.Substring(12, 3) + ":" + d.Substring(15, 2);
  191. }
  192. }
  193. }
  194. }
  195. public string AdjustedTime
  196. {
  197. get { return AdjustedTimeString; }
  198. }
  199. /// <summary>
  200. /// Return a time string as an adjusted date with a 4 digit year.
  201. /// This goes in the range of 1950 - 2049.
  202. /// </summary>
  203. public string AdjustedTimeString
  204. {
  205. get
  206. {
  207. string d = TimeString;
  208. string c = d[0] < '5' ? "20" : "19";
  209. return c + d;
  210. }
  211. }
  212. private byte[] GetOctets()
  213. {
  214. return Strings.ToAsciiByteArray(time);
  215. }
  216. internal override int EncodedLength(bool withID)
  217. {
  218. return Asn1OutputStream.GetLengthOfEncodingDL(withID, time.Length);
  219. }
  220. internal override void Encode(Asn1OutputStream asn1Out, bool withID)
  221. {
  222. asn1Out.WriteEncodingDL(withID, Asn1Tags.UtcTime, GetOctets());
  223. }
  224. protected override bool Asn1Equals(
  225. Asn1Object asn1Object)
  226. {
  227. DerUtcTime other = asn1Object as DerUtcTime;
  228. if (other == null)
  229. return false;
  230. return this.time.Equals(other.time);
  231. }
  232. protected override int Asn1GetHashCode()
  233. {
  234. return time.GetHashCode();
  235. }
  236. public override string ToString()
  237. {
  238. return time;
  239. }
  240. }
  241. }
  242. #pragma warning restore
  243. #endif