a_utctm.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright 1995-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. #include <stdio.h>
  10. #include <time.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/asn1.h>
  13. #include "asn1_locl.h"
  14. /* This is the primary function used to parse ASN1_UTCTIME */
  15. int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d)
  16. {
  17. /* wrapper around ans1_time_to_tm */
  18. if (d->type != V_ASN1_UTCTIME)
  19. return 0;
  20. return asn1_time_to_tm(tm, d);
  21. }
  22. int ASN1_UTCTIME_check(const ASN1_UTCTIME *d)
  23. {
  24. return asn1_utctime_to_tm(NULL, d);
  25. }
  26. /* Sets the string via simple copy without cleaning it up */
  27. int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str)
  28. {
  29. ASN1_UTCTIME t;
  30. t.type = V_ASN1_UTCTIME;
  31. t.length = strlen(str);
  32. t.data = (unsigned char *)str;
  33. t.flags = 0;
  34. if (!ASN1_UTCTIME_check(&t))
  35. return 0;
  36. if (s != NULL && !ASN1_STRING_copy(s, &t))
  37. return 0;
  38. return 1;
  39. }
  40. ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t)
  41. {
  42. return ASN1_UTCTIME_adj(s, t, 0, 0);
  43. }
  44. ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
  45. int offset_day, long offset_sec)
  46. {
  47. struct tm *ts;
  48. struct tm data;
  49. ts = OPENSSL_gmtime(&t, &data);
  50. if (ts == NULL)
  51. return NULL;
  52. if (offset_day || offset_sec) {
  53. if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
  54. return NULL;
  55. }
  56. return asn1_time_from_tm(s, ts, V_ASN1_UTCTIME);
  57. }
  58. int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t)
  59. {
  60. struct tm stm, ttm;
  61. int day, sec;
  62. if (!asn1_utctime_to_tm(&stm, s))
  63. return -2;
  64. if (OPENSSL_gmtime(&t, &ttm) == NULL)
  65. return -2;
  66. if (!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm))
  67. return -2;
  68. if (day > 0 || sec > 0)
  69. return 1;
  70. if (day < 0 || sec < 0)
  71. return -1;
  72. return 0;
  73. }
  74. int ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm)
  75. {
  76. if (tm->type != V_ASN1_UTCTIME)
  77. return 0;
  78. return ASN1_TIME_print(bp, tm);
  79. }