Home | History | Annotate | Line # | Download | only in testutil
      1      1.1  christos /*
      2      1.1  christos  * Copyright 2020-2024 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  *
      4      1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5      1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      6      1.1  christos  * in the file LICENSE in the source distribution or at
      7      1.1  christos  * https://www.openssl.org/source/license.html
      8      1.1  christos  */
      9      1.1  christos 
     10      1.1  christos #include <stdio.h>
     11      1.1  christos #include <time.h>
     12      1.1  christos #include <openssl/asn1t.h>
     13      1.1  christos #include "../testutil.h"
     14      1.1  christos 
     15      1.1  christos /*
     16      1.1  christos  * tweak for Windows
     17      1.1  christos  */
     18      1.1  christos #ifdef WIN32
     19  1.1.1.2  christos #define timezone _timezone
     20      1.1  christos #endif
     21      1.1  christos 
     22      1.1  christos #if defined(__FreeBSD__) || defined(__wasi__)
     23  1.1.1.2  christos #define USE_TIMEGM
     24      1.1  christos #endif
     25      1.1  christos 
     26      1.1  christos time_t test_asn1_string_to_time_t(const char *asn1_string)
     27      1.1  christos {
     28      1.1  christos     ASN1_TIME *timestamp_asn1 = NULL;
     29      1.1  christos     struct tm *timestamp_tm = NULL;
     30      1.1  christos #if defined(__DJGPP__)
     31      1.1  christos     char *tz = NULL;
     32      1.1  christos #elif !defined(USE_TIMEGM)
     33      1.1  christos     time_t timestamp_local;
     34      1.1  christos #endif
     35      1.1  christos     time_t timestamp_utc;
     36      1.1  christos 
     37      1.1  christos     timestamp_asn1 = ASN1_TIME_new();
     38  1.1.1.2  christos     if (timestamp_asn1 == NULL)
     39      1.1  christos         return -1;
     40  1.1.1.2  christos     if (!ASN1_TIME_set_string(timestamp_asn1, asn1_string)) {
     41      1.1  christos         ASN1_TIME_free(timestamp_asn1);
     42      1.1  christos         return -1;
     43      1.1  christos     }
     44      1.1  christos 
     45      1.1  christos     timestamp_tm = OPENSSL_malloc(sizeof(*timestamp_tm));
     46      1.1  christos     if (timestamp_tm == NULL) {
     47      1.1  christos         ASN1_TIME_free(timestamp_asn1);
     48      1.1  christos         return -1;
     49      1.1  christos     }
     50      1.1  christos     if (!(ASN1_TIME_to_tm(timestamp_asn1, timestamp_tm))) {
     51      1.1  christos         OPENSSL_free(timestamp_tm);
     52      1.1  christos         ASN1_TIME_free(timestamp_asn1);
     53      1.1  christos         return -1;
     54      1.1  christos     }
     55      1.1  christos     ASN1_TIME_free(timestamp_asn1);
     56      1.1  christos 
     57      1.1  christos #if defined(__DJGPP__)
     58      1.1  christos     /*
     59      1.1  christos      * This is NOT thread-safe.  Do not use this method for platforms other
     60      1.1  christos      * than djgpp.
     61      1.1  christos      */
     62      1.1  christos     tz = getenv("TZ");
     63      1.1  christos     if (tz != NULL) {
     64      1.1  christos         tz = OPENSSL_strdup(tz);
     65      1.1  christos         if (tz == NULL) {
     66      1.1  christos             OPENSSL_free(timestamp_tm);
     67      1.1  christos             return -1;
     68      1.1  christos         }
     69      1.1  christos     }
     70      1.1  christos     setenv("TZ", "UTC", 1);
     71      1.1  christos 
     72      1.1  christos     timestamp_utc = mktime(timestamp_tm);
     73      1.1  christos 
     74      1.1  christos     if (tz != NULL) {
     75      1.1  christos         setenv("TZ", tz, 1);
     76      1.1  christos         OPENSSL_free(tz);
     77      1.1  christos     } else {
     78      1.1  christos         unsetenv("TZ");
     79      1.1  christos     }
     80      1.1  christos #elif defined(USE_TIMEGM)
     81      1.1  christos     timestamp_utc = timegm(timestamp_tm);
     82      1.1  christos #else
     83      1.1  christos     timestamp_local = mktime(timestamp_tm);
     84      1.1  christos     timestamp_utc = timestamp_local - timezone;
     85      1.1  christos #endif
     86      1.1  christos     OPENSSL_free(timestamp_tm);
     87      1.1  christos 
     88      1.1  christos     return timestamp_utc;
     89      1.1  christos }
     90