a_gentm.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. #include <openssl/asn1t.h>
  18. IMPLEMENT_ASN1_DUP_FUNCTION(ASN1_GENERALIZEDTIME)
  19. /* This is the primary function used to parse ASN1_GENERALIZEDTIME */
  20. int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d)
  21. {
  22. /* wrapper around asn1_time_to_tm */
  23. if (d->type != V_ASN1_GENERALIZEDTIME)
  24. return 0;
  25. return asn1_time_to_tm(tm, d);
  26. }
  27. int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d)
  28. {
  29. return asn1_generalizedtime_to_tm(NULL, d);
  30. }
  31. int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str)
  32. {
  33. ASN1_GENERALIZEDTIME t;
  34. t.type = V_ASN1_GENERALIZEDTIME;
  35. t.length = strlen(str);
  36. t.data = (unsigned char *)str;
  37. t.flags = 0;
  38. if (!ASN1_GENERALIZEDTIME_check(&t))
  39. return 0;
  40. if (s != NULL && !ASN1_STRING_copy(s, &t))
  41. return 0;
  42. return 1;
  43. }
  44. ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
  45. time_t t)
  46. {
  47. return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0);
  48. }
  49. ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
  50. time_t t, int offset_day,
  51. long offset_sec)
  52. {
  53. struct tm *ts;
  54. struct tm data;
  55. ts = OPENSSL_gmtime(&t, &data);
  56. if (ts == NULL)
  57. return NULL;
  58. if (offset_day || offset_sec) {
  59. if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
  60. return NULL;
  61. }
  62. return asn1_time_from_tm(s, ts, V_ASN1_GENERALIZEDTIME);
  63. }
  64. int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
  65. {
  66. if (tm->type != V_ASN1_GENERALIZEDTIME)
  67. return 0;
  68. return ASN1_TIME_print(bp, tm);
  69. }