SecurityTimestamp.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Security
  5. {
  6. using System.Globalization;
  7. using System.Runtime;
  8. using System.Xml;
  9. sealed class SecurityTimestamp
  10. {
  11. const string DefaultFormat = "yyyy-MM-ddTHH:mm:ss.fffZ";
  12. // 012345678901234567890123
  13. internal static readonly TimeSpan defaultTimeToLive = SecurityProtocolFactory.defaultTimestampValidityDuration;
  14. char[] computedCreationTimeUtc;
  15. char[] computedExpiryTimeUtc;
  16. DateTime creationTimeUtc;
  17. DateTime expiryTimeUtc;
  18. readonly string id;
  19. readonly string digestAlgorithm;
  20. readonly byte[] digest;
  21. public SecurityTimestamp(DateTime creationTimeUtc, DateTime expiryTimeUtc, string id)
  22. : this(creationTimeUtc, expiryTimeUtc, id, null, null)
  23. {
  24. }
  25. internal SecurityTimestamp(DateTime creationTimeUtc, DateTime expiryTimeUtc, string id, string digestAlgorithm, byte[] digest)
  26. {
  27. Fx.Assert(creationTimeUtc.Kind == DateTimeKind.Utc, "creation time must be in UTC");
  28. Fx.Assert(expiryTimeUtc.Kind == DateTimeKind.Utc, "expiry time must be in UTC");
  29. if (creationTimeUtc > expiryTimeUtc)
  30. {
  31. throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new ArgumentOutOfRangeException("recordedExpiryTime", SR.GetString(SR.CreationTimeUtcIsAfterExpiryTime)));
  32. }
  33. this.creationTimeUtc = creationTimeUtc;
  34. this.expiryTimeUtc = expiryTimeUtc;
  35. this.id = id;
  36. this.digestAlgorithm = digestAlgorithm;
  37. this.digest = digest;
  38. }
  39. public DateTime CreationTimeUtc
  40. {
  41. get
  42. {
  43. return this.creationTimeUtc;
  44. }
  45. }
  46. public DateTime ExpiryTimeUtc
  47. {
  48. get
  49. {
  50. return this.expiryTimeUtc;
  51. }
  52. }
  53. public string Id
  54. {
  55. get
  56. {
  57. return this.id;
  58. }
  59. }
  60. public string DigestAlgorithm
  61. {
  62. get
  63. {
  64. return this.digestAlgorithm;
  65. }
  66. }
  67. internal byte[] GetDigest()
  68. {
  69. return this.digest;
  70. }
  71. internal char[] GetCreationTimeChars()
  72. {
  73. if (this.computedCreationTimeUtc == null)
  74. {
  75. this.computedCreationTimeUtc = ToChars(ref this.creationTimeUtc);
  76. }
  77. return this.computedCreationTimeUtc;
  78. }
  79. internal char[] GetExpiryTimeChars()
  80. {
  81. if (this.computedExpiryTimeUtc == null)
  82. {
  83. this.computedExpiryTimeUtc = ToChars(ref this.expiryTimeUtc);
  84. }
  85. return this.computedExpiryTimeUtc;
  86. }
  87. static char[] ToChars(ref DateTime utcTime)
  88. {
  89. char[] buffer = new char[DefaultFormat.Length];
  90. int offset = 0;
  91. ToChars(utcTime.Year, buffer, ref offset, 4);
  92. buffer[offset++] = '-';
  93. ToChars(utcTime.Month, buffer, ref offset, 2);
  94. buffer[offset++] = '-';
  95. ToChars(utcTime.Day, buffer, ref offset, 2);
  96. buffer[offset++] = 'T';
  97. ToChars(utcTime.Hour, buffer, ref offset, 2);
  98. buffer[offset++] = ':';
  99. ToChars(utcTime.Minute, buffer, ref offset, 2);
  100. buffer[offset++] = ':';
  101. ToChars(utcTime.Second, buffer, ref offset, 2);
  102. buffer[offset++] = '.';
  103. ToChars(utcTime.Millisecond, buffer, ref offset, 3);
  104. buffer[offset++] = 'Z';
  105. return buffer;
  106. }
  107. static void ToChars(int n, char[] buffer, ref int offset, int count)
  108. {
  109. for (int i = offset + count - 1; i >= offset; i--)
  110. {
  111. buffer[i] = (char)('0' + (n % 10));
  112. n /= 10;
  113. }
  114. Fx.Assert(n == 0, "Overflow in encoding timestamp field");
  115. offset += count;
  116. }
  117. public override string ToString()
  118. {
  119. return string.Format(
  120. CultureInfo.InvariantCulture,
  121. "SecurityTimestamp: Id={0}, CreationTimeUtc={1}, ExpirationTimeUtc={2}",
  122. this.Id,
  123. XmlConvert.ToString(this.CreationTimeUtc, XmlDateTimeSerializationMode.RoundtripKind),
  124. XmlConvert.ToString(this.ExpiryTimeUtc, XmlDateTimeSerializationMode.RoundtripKind));
  125. }
  126. /// <summary>
  127. /// Internal method that checks if the timestamp is fresh with respect to the
  128. /// timeToLive and allowedClockSkew values passed in.
  129. /// Throws if the timestamp is stale.
  130. /// </summary>
  131. /// <param name="timeToLive"></param>
  132. /// <param name="allowedClockSkew"></param>
  133. internal void ValidateRangeAndFreshness(TimeSpan timeToLive, TimeSpan allowedClockSkew)
  134. {
  135. // Check that the creation time is less than expiry time
  136. if (this.CreationTimeUtc >= this.ExpiryTimeUtc)
  137. {
  138. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.GetString(SR.TimeStampHasCreationAheadOfExpiry, this.CreationTimeUtc.ToString(DefaultFormat, CultureInfo.CurrentCulture), this.ExpiryTimeUtc.ToString(DefaultFormat, CultureInfo.CurrentCulture))));
  139. }
  140. ValidateFreshness(timeToLive, allowedClockSkew);
  141. }
  142. internal void ValidateFreshness(TimeSpan timeToLive, TimeSpan allowedClockSkew)
  143. {
  144. DateTime now = DateTime.UtcNow;
  145. // check that the message has not expired
  146. if (this.ExpiryTimeUtc <= TimeoutHelper.Subtract(now, allowedClockSkew))
  147. {
  148. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.GetString(SR.TimeStampHasExpiryTimeInPast, this.ExpiryTimeUtc.ToString(DefaultFormat, CultureInfo.CurrentCulture), now.ToString(DefaultFormat, CultureInfo.CurrentCulture), allowedClockSkew)));
  149. }
  150. // check that creation time is not in the future (modulo clock skew)
  151. if (this.CreationTimeUtc >= TimeoutHelper.Add(now, allowedClockSkew))
  152. {
  153. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.GetString(SR.TimeStampHasCreationTimeInFuture, this.CreationTimeUtc.ToString(DefaultFormat, CultureInfo.CurrentCulture), now.ToString(DefaultFormat, CultureInfo.CurrentCulture), allowedClockSkew)));
  154. }
  155. // check that the creation time is not more than timeToLive in the past
  156. if (this.CreationTimeUtc <= TimeoutHelper.Subtract(now, TimeoutHelper.Add(timeToLive, allowedClockSkew)))
  157. {
  158. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.GetString(SR.TimeStampWasCreatedTooLongAgo, this.CreationTimeUtc.ToString(DefaultFormat, CultureInfo.CurrentCulture), now.ToString(DefaultFormat, CultureInfo.CurrentCulture), timeToLive, allowedClockSkew)));
  159. }
  160. // this is a fresh timestamp
  161. }
  162. }
  163. }