File: helper.c

package info (click to toggle)
openssl 3.5.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 142,688 kB
  • sloc: ansic: 672,420; perl: 233,226; asm: 6,546; sh: 1,569; pascal: 975; python: 596; makefile: 538; lisp: 35; ruby: 16; cpp: 10; sed: 6
file content (89 lines) | stat: -rw-r--r-- 2,156 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * Copyright 2020-2024 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <stdio.h>
#include <time.h>
#include <openssl/asn1t.h>
#include "../testutil.h"

/*
 * tweak for Windows
 */
#ifdef WIN32
#define timezone _timezone
#endif

#if defined(__FreeBSD__) || defined(__wasi__)
#define USE_TIMEGM
#endif

time_t test_asn1_string_to_time_t(const char *asn1_string)
{
    ASN1_TIME *timestamp_asn1 = NULL;
    struct tm *timestamp_tm = NULL;
#if defined(__DJGPP__)
    char *tz = NULL;
#elif !defined(USE_TIMEGM)
    time_t timestamp_local;
#endif
    time_t timestamp_utc;

    timestamp_asn1 = ASN1_TIME_new();
    if (timestamp_asn1 == NULL)
        return -1;
    if (!ASN1_TIME_set_string(timestamp_asn1, asn1_string)) {
        ASN1_TIME_free(timestamp_asn1);
        return -1;
    }

    timestamp_tm = OPENSSL_malloc(sizeof(*timestamp_tm));
    if (timestamp_tm == NULL) {
        ASN1_TIME_free(timestamp_asn1);
        return -1;
    }
    if (!(ASN1_TIME_to_tm(timestamp_asn1, timestamp_tm))) {
        OPENSSL_free(timestamp_tm);
        ASN1_TIME_free(timestamp_asn1);
        return -1;
    }
    ASN1_TIME_free(timestamp_asn1);

#if defined(__DJGPP__)
    /*
     * This is NOT thread-safe.  Do not use this method for platforms other
     * than djgpp.
     */
    tz = getenv("TZ");
    if (tz != NULL) {
        tz = OPENSSL_strdup(tz);
        if (tz == NULL) {
            OPENSSL_free(timestamp_tm);
            return -1;
        }
    }
    setenv("TZ", "UTC", 1);

    timestamp_utc = mktime(timestamp_tm);

    if (tz != NULL) {
        setenv("TZ", tz, 1);
        OPENSSL_free(tz);
    } else {
        unsetenv("TZ");
    }
#elif defined(USE_TIMEGM)
    timestamp_utc = timegm(timestamp_tm);
#else
    timestamp_local = mktime(timestamp_tm);
    timestamp_utc = timestamp_local - timezone;
#endif
    OPENSSL_free(timestamp_tm);

    return timestamp_utc;
}