a_gentm.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. /*
  10. * GENERALIZEDTIME implementation. Based on UTCTIME
  11. */
  12. #include <stdio.h>
  13. #include <time.h>
  14. #include "internal/cryptlib.h"
  15. #include <openssl/asn1.h>
  16. #include "asn1_local.h"
  17. /* This is the primary function used to parse ASN1_GENERALIZEDTIME */
  18. int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d)
  19. {
  20. /* wrapper around asn1_time_to_tm */
  21. if (d->type != V_ASN1_GENERALIZEDTIME)
  22. return 0;
  23. return asn1_time_to_tm(tm, d);
  24. }
  25. int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d)
  26. {
  27. return asn1_generalizedtime_to_tm(NULL, d);
  28. }
  29. int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str)
  30. {
  31. ASN1_GENERALIZEDTIME t;
  32. t.type = V_ASN1_GENERALIZEDTIME;
  33. t.length = strlen(str);
  34. t.data = (unsigned char *)str;
  35. t.flags = 0;
  36. if (!ASN1_GENERALIZEDTIME_check(&t))
  37. return 0;
  38. if (s != NULL && !ASN1_STRING_copy(s, &t))
  39. return 0;
  40. return 1;
  41. }
  42. ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
  43. time_t t)
  44. {
  45. return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0);
  46. }
  47. ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
  48. time_t t, int offset_day,
  49. long offset_sec)
  50. {
  51. struct tm *ts;
  52. struct tm data;
  53. ts = OPENSSL_gmtime(&t, &data);
  54. if (ts == NULL)
  55. return NULL;
  56. if (offset_day || offset_sec) {
  57. if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
  58. return NULL;
  59. }
  60. return asn1_time_from_tm(s, ts, V_ASN1_GENERALIZEDTIME);
  61. }
  62. int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
  63. {
  64. if (tm->type != V_ASN1_GENERALIZEDTIME)
  65. return 0;
  66. return ASN1_TIME_print(bp, tm);
  67. }