You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

241 lines
4.6 KiB

  1. /*
  2. * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. // Tests for X509 time functions.
  10. #include <openssl/x509.h>
  11. #include <string.h>
  12. #include <time.h>
  13. #include <gtest/gtest.h>
  14. #include <openssl/asn1.h>
  15. struct TestData {
  16. const char *data;
  17. int type;
  18. time_t cmp_time;
  19. // -1 if asn1_time <= cmp_time, 1 if asn1_time > cmp_time, 0 if error.
  20. int expected;
  21. };
  22. static TestData kX509CmpTests[] = {
  23. {
  24. "20170217180154Z",
  25. V_ASN1_GENERALIZEDTIME,
  26. // The same in seconds since epoch.
  27. 1487354514,
  28. -1,
  29. },
  30. {
  31. "20170217180154Z",
  32. V_ASN1_GENERALIZEDTIME,
  33. // One second more.
  34. 1487354515,
  35. -1,
  36. },
  37. {
  38. "20170217180154Z",
  39. V_ASN1_GENERALIZEDTIME,
  40. // One second less.
  41. 1487354513,
  42. 1,
  43. },
  44. // Same as UTC time.
  45. {
  46. "170217180154Z",
  47. V_ASN1_UTCTIME,
  48. // The same in seconds since epoch.
  49. 1487354514,
  50. -1,
  51. },
  52. {
  53. "170217180154Z",
  54. V_ASN1_UTCTIME,
  55. // One second more.
  56. 1487354515,
  57. -1,
  58. },
  59. {
  60. "170217180154Z",
  61. V_ASN1_UTCTIME,
  62. // One second less.
  63. 1487354513,
  64. 1,
  65. },
  66. // UTCTime from the 20th century.
  67. {
  68. "990217180154Z",
  69. V_ASN1_UTCTIME,
  70. // The same in seconds since epoch.
  71. 919274514,
  72. -1,
  73. },
  74. {
  75. "990217180154Z",
  76. V_ASN1_UTCTIME,
  77. // One second more.
  78. 919274515,
  79. -1,
  80. },
  81. {
  82. "990217180154Z",
  83. V_ASN1_UTCTIME,
  84. // One second less.
  85. 919274513,
  86. 1,
  87. },
  88. // Various invalid formats.
  89. {
  90. // No trailing Z.
  91. "20170217180154",
  92. V_ASN1_GENERALIZEDTIME,
  93. 0,
  94. 0,
  95. },
  96. {
  97. // No trailing Z, UTCTime.
  98. "170217180154",
  99. V_ASN1_UTCTIME,
  100. 0,
  101. 0,
  102. },
  103. {
  104. // No seconds.
  105. "201702171801Z",
  106. V_ASN1_GENERALIZEDTIME,
  107. 0,
  108. 0,
  109. },
  110. {
  111. // No seconds, UTCTime.
  112. "1702171801Z",
  113. V_ASN1_UTCTIME,
  114. 0,
  115. 0,
  116. },
  117. {
  118. // Fractional seconds.
  119. "20170217180154.001Z",
  120. V_ASN1_GENERALIZEDTIME,
  121. 0,
  122. 0,
  123. },
  124. {
  125. // Fractional seconds, UTCTime.
  126. "170217180154.001Z",
  127. V_ASN1_UTCTIME,
  128. 0,
  129. 0,
  130. },
  131. {
  132. // Timezone offset.
  133. "20170217180154+0100",
  134. V_ASN1_GENERALIZEDTIME,
  135. 0,
  136. 0,
  137. },
  138. {
  139. // Timezone offset, UTCTime.
  140. "170217180154+0100",
  141. V_ASN1_UTCTIME,
  142. 0,
  143. 0,
  144. },
  145. {
  146. // Extra digits.
  147. "2017021718015400Z",
  148. V_ASN1_GENERALIZEDTIME,
  149. 0,
  150. 0,
  151. },
  152. {
  153. // Extra digits, UTCTime.
  154. "17021718015400Z",
  155. V_ASN1_UTCTIME,
  156. 0,
  157. 0,
  158. },
  159. {
  160. // Non-digits.
  161. "2017021718015aZ",
  162. V_ASN1_GENERALIZEDTIME,
  163. 0,
  164. 0,
  165. },
  166. {
  167. // Non-digits, UTCTime.
  168. "17021718015aZ",
  169. V_ASN1_UTCTIME,
  170. 0,
  171. 0,
  172. },
  173. {
  174. // Trailing garbage.
  175. "20170217180154Zlongtrailinggarbage",
  176. V_ASN1_GENERALIZEDTIME,
  177. 0,
  178. 0,
  179. },
  180. {
  181. // Trailing garbage, UTCTime.
  182. "170217180154Zlongtrailinggarbage",
  183. V_ASN1_UTCTIME,
  184. 0,
  185. 0,
  186. },
  187. {
  188. // Swapped type.
  189. "20170217180154Z",
  190. V_ASN1_UTCTIME,
  191. 0,
  192. 0,
  193. },
  194. {
  195. // Swapped type.
  196. "170217180154Z",
  197. V_ASN1_GENERALIZEDTIME,
  198. 0,
  199. 0,
  200. },
  201. {
  202. // Bad type.
  203. "20170217180154Z",
  204. V_ASN1_OCTET_STRING,
  205. 0,
  206. 0,
  207. },
  208. };
  209. TEST(X509TimeTest, TestCmpTime) {
  210. for (auto &test : kX509CmpTests) {
  211. SCOPED_TRACE(test.data);
  212. ASN1_TIME t;
  213. memset(&t, 0, sizeof(t));
  214. t.type = test.type;
  215. t.data = (unsigned char*) test.data;
  216. t.length = strlen(test.data);
  217. EXPECT_EQ(test.expected,
  218. X509_cmp_time(&t, &test.cmp_time));
  219. }
  220. }
  221. TEST(X509TimeTest, TestCmpTimeCurrent) {
  222. time_t now = time(NULL);
  223. // Pick a day earlier and later, relative to any system clock.
  224. bssl::UniquePtr<ASN1_TIME> asn1_before(ASN1_TIME_adj(NULL, now, -1, 0));
  225. bssl::UniquePtr<ASN1_TIME> asn1_after(ASN1_TIME_adj(NULL, now, 1, 0));
  226. ASSERT_EQ(-1, X509_cmp_time(asn1_before.get(), NULL));
  227. ASSERT_EQ(1, X509_cmp_time(asn1_after.get(), NULL));
  228. }