Source: security/validity-period.js

  1. /**
  2. * Copyright (C) 2016-2018 Regents of the University of California.
  3. * @author: Jeff Thompson <jefft0@remap.ucla.edu>
  4. * @author: From ndn-cxx src/security https://github.com/named-data/ndn-cxx
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. * A copy of the GNU Lesser General Public License is in the file COPYING.
  19. */
  20. /**
  21. * A ValidityPeriod is used in a Data packet's SignatureInfo and represents the
  22. * begin and end times of a certificate's validity period.
  23. *
  24. * There are three forms of the ValidityPeriod constructor:
  25. * ValidityPeriod() - Create a default ValidityPeriod where the period is not
  26. * specified.
  27. * ValidityPeriod(validityPeriod) - Create a new ValidityPeriod with a copy of
  28. * the fields in the given validityPeriod object.
  29. * ValidityPeriod(notBefore, notAfter) - Create a ValidityPeriod with the given
  30. * period.
  31. * @param {ValidityPeriod} validityPeriod The ValidityPeriod to copy.
  32. * @param {number} notBefore The beginning of the validity period range as
  33. * milliseconds since Jan 1, 1970 UTC. Note that this is rounded up to the
  34. * nearest whole second.
  35. * @param {number} notAfter The end of the validity period range as milliseconds
  36. * since Jan 1, 1970 UTC. Note that this is rounded down to the nearest whole
  37. * second.
  38. * @constructor
  39. */
  40. var ValidityPeriod = function ValidityPeriod(validityPeriodOrNotBefore, notAfter)
  41. {
  42. this.changeCount_ = 0;
  43. if (typeof validityPeriodOrNotBefore === 'object' &&
  44. validityPeriodOrNotBefore instanceof ValidityPeriod) {
  45. // Copy values.
  46. validityPeriod = validityPeriodOrNotBefore;
  47. this.notBefore_ = validityPeriod.notBefore_;
  48. this.notAfter_ = validityPeriod.notAfter_;
  49. }
  50. else if (notAfter != undefined) {
  51. notBefore = validityPeriodOrNotBefore;
  52. this.setPeriod(notBefore, notAfter)
  53. }
  54. else
  55. this.clear();
  56. };
  57. exports.ValidityPeriod = ValidityPeriod;
  58. /**
  59. * Check if the period has been set.
  60. * @return {boolean} True if the period has been set, false if the period is not
  61. * specified (after calling the default constructor or clear).
  62. */
  63. ValidityPeriod.prototype.hasPeriod = function()
  64. {
  65. return !(this.notBefore_ === Number.MAX_VALUE &&
  66. this.notAfter_ === -Number.MAX_VALUE);
  67. };
  68. /**
  69. * Get the beginning of the validity period range.
  70. * @return {number} The time as milliseconds since Jan 1, 1970 UTC.
  71. */
  72. ValidityPeriod.prototype.getNotBefore = function() { return this.notBefore_; };
  73. /**
  74. * Get the end of the validity period range.
  75. * @return {number} The time as milliseconds since Jan 1, 1970 UTC.
  76. */
  77. ValidityPeriod.prototype.getNotAfter = function() { return this.notAfter_; };
  78. /** Reset to a default ValidityPeriod where the period is not specified.
  79. */
  80. ValidityPeriod.prototype.clear = function()
  81. {
  82. this.notBefore_ = Number.MAX_VALUE;
  83. this.notAfter_ = -Number.MAX_VALUE;
  84. ++this.changeCount_;
  85. };
  86. /**
  87. * Set the validity period.
  88. * @param {number} notBefore The beginning of the validity period range as
  89. * milliseconds since Jan 1, 1970 UTC. Note that this is rounded up to the
  90. * nearest whole second.
  91. * @param {number} notAfter The end of the validity period range as milliseconds
  92. * since Jan 1, 1970 UTC. Note that this is rounded down to the nearest whole
  93. * second.
  94. * @return {ValidityPeriod} This ValidityPeriod so that you can chain calls to
  95. * update values.
  96. */
  97. ValidityPeriod.prototype.setPeriod = function(notBefore, notAfter)
  98. {
  99. // Round up to the nearest second.
  100. this.notBefore_ = Math.round(Math.ceil(Math.round(notBefore) / 1000.0) * 1000.0);
  101. // Round down to the nearest second.
  102. this.notAfter_ = Math.round(Math.floor(Math.round(notAfter) / 1000.0) * 1000.0);
  103. ++this.changeCount_;
  104. return this;
  105. };
  106. /**
  107. * Check if the time falls within the validity period.
  108. * @param {number} time (optional) The time to check as milliseconds since
  109. * Jan 1, 1970 UTC. If omitted, use the current time.
  110. * @return {boolean} True if the beginning of the validity period is less than
  111. * or equal to time and time is less than or equal to the end of the validity
  112. * period.
  113. */
  114. ValidityPeriod.prototype.isValid = function(time)
  115. {
  116. if (time == undefined)
  117. // Round up to the nearest second like in setPeriod.
  118. time = Math.round(Math.ceil
  119. (Math.round(new Date().getTime()) / 1000.0) * 1000.0);
  120. return this.notBefore_ <= time && time <= this.notAfter_;
  121. };
  122. /**
  123. * If the signature is a type that has a ValidityPeriod (so that
  124. * getFromSignature will succeed), return true. Note: This is a static method of
  125. * ValidityPeriod instead of a method of Signature so that the Signature base
  126. * class does not need to be overloaded with all the different kinds of
  127. * information that various signature algorithms may use.
  128. * @param {Signature} An object of a subclass of Signature.
  129. * @return {boolean} True if the signature is a type that has a ValidityPeriod,
  130. * otherwise false.
  131. */
  132. ValidityPeriod.canGetFromSignature = function(signature)
  133. {
  134. return signature.constructor != undefined &&
  135. (signature.constructor.name === "Sha256WithRsaSignature" ||
  136. signature.constructor.name === "Sha256WithEcdsaSignature");
  137. };
  138. /**
  139. * If the signature is a type that has a ValidityPeriod, then return it.
  140. * Otherwise throw an error.
  141. * @param {Signature} An object of a subclass of Signature.
  142. * @return {ValidityPeriod} The signature's ValidityPeriod. It is an error if
  143. * signature doesn't have a ValidityPeriod.
  144. */
  145. ValidityPeriod.getFromSignature = function(signature)
  146. {
  147. if (signature.constructor != undefined &&
  148. (signature.constructor.name === "Sha256WithRsaSignature" ||
  149. signature.constructor.name === "Sha256WithEcdsaSignature"))
  150. return signature.getValidityPeriod();
  151. else
  152. throw new Error
  153. ("ValidityPeriod.getFromSignature: Signature type does not have a ValidityPeriod");
  154. };
  155. /**
  156. * Get the change count, which is incremented each time this object is changed.
  157. * @return {number} The change count.
  158. */
  159. ValidityPeriod.prototype.getChangeCount = function()
  160. {
  161. return this.changeCount_;
  162. };