x509_time_test.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 <string.h>
  11. #include <time.h>
  12. #include <openssl/asn1.h>
  13. #include <openssl/x509.h>
  14. #include "testutil.h"
  15. #include "e_os.h"
  16. typedef struct {
  17. const char *data;
  18. int type;
  19. time_t cmp_time;
  20. /* -1 if asn1_time <= cmp_time, 1 if asn1_time > cmp_time, 0 if error. */
  21. int expected;
  22. } TESTDATA;
  23. static TESTDATA x509_cmp_tests[] = {
  24. {
  25. "20170217180154Z", V_ASN1_GENERALIZEDTIME,
  26. /* The same in seconds since epoch. */
  27. 1487354514, -1,
  28. },
  29. {
  30. "20170217180154Z", V_ASN1_GENERALIZEDTIME,
  31. /* One second more. */
  32. 1487354515, -1,
  33. },
  34. {
  35. "20170217180154Z", V_ASN1_GENERALIZEDTIME,
  36. /* One second less. */
  37. 1487354513, 1,
  38. },
  39. /* Same as UTC time. */
  40. {
  41. "170217180154Z", V_ASN1_UTCTIME,
  42. /* The same in seconds since epoch. */
  43. 1487354514, -1,
  44. },
  45. {
  46. "170217180154Z", V_ASN1_UTCTIME,
  47. /* One second more. */
  48. 1487354515, -1,
  49. },
  50. {
  51. "170217180154Z", V_ASN1_UTCTIME,
  52. /* One second less. */
  53. 1487354513, 1,
  54. },
  55. /* UTCTime from the 20th century. */
  56. {
  57. "990217180154Z", V_ASN1_UTCTIME,
  58. /* The same in seconds since epoch. */
  59. 919274514, -1,
  60. },
  61. {
  62. "990217180154Z", V_ASN1_UTCTIME,
  63. /* One second more. */
  64. 919274515, -1,
  65. },
  66. {
  67. "990217180154Z", V_ASN1_UTCTIME,
  68. /* One second less. */
  69. 919274513, 1,
  70. },
  71. /* Various invalid formats. */
  72. {
  73. /* No trailing Z. */
  74. "20170217180154", V_ASN1_GENERALIZEDTIME, 0, 0,
  75. },
  76. {
  77. /* No trailing Z, UTCTime. */
  78. "170217180154", V_ASN1_UTCTIME, 0, 0,
  79. },
  80. {
  81. /* No seconds. */
  82. "201702171801Z", V_ASN1_GENERALIZEDTIME, 0, 0,
  83. },
  84. {
  85. /* No seconds, UTCTime. */
  86. "1702171801Z", V_ASN1_UTCTIME, 0, 0,
  87. },
  88. {
  89. /* Fractional seconds. */
  90. "20170217180154.001Z", V_ASN1_GENERALIZEDTIME, 0, 0,
  91. },
  92. {
  93. /* Fractional seconds, UTCTime. */
  94. "170217180154.001Z", V_ASN1_UTCTIME, 0, 0,
  95. },
  96. {
  97. /* Timezone offset. */
  98. "20170217180154+0100", V_ASN1_GENERALIZEDTIME, 0, 0,
  99. },
  100. {
  101. /* Timezone offset, UTCTime. */
  102. "170217180154+0100", V_ASN1_UTCTIME, 0, 0,
  103. },
  104. {
  105. /* Extra digits. */
  106. "2017021718015400Z", V_ASN1_GENERALIZEDTIME, 0, 0,
  107. },
  108. {
  109. /* Extra digits, UTCTime. */
  110. "17021718015400Z", V_ASN1_UTCTIME, 0, 0,
  111. },
  112. {
  113. /* Non-digits. */
  114. "2017021718015aZ", V_ASN1_GENERALIZEDTIME, 0, 0,
  115. },
  116. {
  117. /* Non-digits, UTCTime. */
  118. "17021718015aZ", V_ASN1_UTCTIME, 0, 0,
  119. },
  120. {
  121. /* Trailing garbage. */
  122. "20170217180154Zlongtrailinggarbage", V_ASN1_GENERALIZEDTIME, 0, 0,
  123. },
  124. {
  125. /* Trailing garbage, UTCTime. */
  126. "170217180154Zlongtrailinggarbage", V_ASN1_UTCTIME, 0, 0,
  127. },
  128. {
  129. /* Swapped type. */
  130. "20170217180154Z", V_ASN1_UTCTIME, 0, 0,
  131. },
  132. {
  133. /* Swapped type. */
  134. "170217180154Z", V_ASN1_GENERALIZEDTIME, 0, 0,
  135. },
  136. {
  137. /* Bad type. */
  138. "20170217180154Z", V_ASN1_OCTET_STRING, 0, 0,
  139. },
  140. };
  141. static int test_x509_cmp_time(int idx)
  142. {
  143. ASN1_TIME t;
  144. int result;
  145. memset(&t, 0, sizeof(t));
  146. t.type = x509_cmp_tests[idx].type;
  147. t.data = (unsigned char*)(x509_cmp_tests[idx].data);
  148. t.length = strlen(x509_cmp_tests[idx].data);
  149. result = X509_cmp_time(&t, &x509_cmp_tests[idx].cmp_time);
  150. if (!TEST_int_eq(result, x509_cmp_tests[idx].expected)) {
  151. TEST_info("test_x509_cmp_time(%d) failed: expected %d, got %d\n",
  152. idx, x509_cmp_tests[idx].expected, result);
  153. return 0;
  154. }
  155. return 1;
  156. }
  157. static int test_x509_cmp_time_current()
  158. {
  159. time_t now = time(NULL);
  160. /* Pick a day earlier and later, relative to any system clock. */
  161. ASN1_TIME *asn1_before = NULL, *asn1_after = NULL;
  162. int cmp_result, failed = 0;
  163. asn1_before = ASN1_TIME_adj(NULL, now, -1, 0);
  164. asn1_after = ASN1_TIME_adj(NULL, now, 1, 0);
  165. cmp_result = X509_cmp_time(asn1_before, NULL);
  166. if (!TEST_int_eq(cmp_result, -1))
  167. failed = 1;
  168. cmp_result = X509_cmp_time(asn1_after, NULL);
  169. if (!TEST_int_eq(cmp_result, 1))
  170. failed = 1;
  171. ASN1_TIME_free(asn1_before);
  172. ASN1_TIME_free(asn1_after);
  173. return failed == 0;
  174. }
  175. void register_tests()
  176. {
  177. ADD_TEST(test_x509_cmp_time_current);
  178. ADD_ALL_TESTS(test_x509_cmp_time, OSSL_NELEM(x509_cmp_tests));
  179. }