a_gentm.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 1995-2021 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. static int asn1_generalizedtime_to_tm(struct tm *tm,
  21. const ASN1_GENERALIZEDTIME *d)
  22. {
  23. /* wrapper around ossl_asn1_time_to_tm */
  24. if (d->type != V_ASN1_GENERALIZEDTIME)
  25. return 0;
  26. return ossl_asn1_time_to_tm(tm, d);
  27. }
  28. int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d)
  29. {
  30. return asn1_generalizedtime_to_tm(NULL, d);
  31. }
  32. int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str)
  33. {
  34. ASN1_GENERALIZEDTIME t;
  35. t.type = V_ASN1_GENERALIZEDTIME;
  36. t.length = strlen(str);
  37. t.data = (unsigned char *)str;
  38. t.flags = 0;
  39. if (!ASN1_GENERALIZEDTIME_check(&t))
  40. return 0;
  41. if (s != NULL && !ASN1_STRING_copy(s, &t))
  42. return 0;
  43. return 1;
  44. }
  45. ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
  46. time_t t)
  47. {
  48. return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0);
  49. }
  50. ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
  51. time_t t, int offset_day,
  52. long offset_sec)
  53. {
  54. struct tm *ts;
  55. struct tm data;
  56. ts = OPENSSL_gmtime(&t, &data);
  57. if (ts == NULL)
  58. return NULL;
  59. if (offset_day || offset_sec) {
  60. if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
  61. return NULL;
  62. }
  63. return ossl_asn1_time_from_tm(s, ts, V_ASN1_GENERALIZEDTIME);
  64. }
  65. int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
  66. {
  67. if (tm->type != V_ASN1_GENERALIZEDTIME)
  68. return 0;
  69. return ASN1_TIME_print(bp, tm);
  70. }