2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include "internal/cryptlib.h"
13 #include <openssl/asn1.h>
14 #include "asn1_local.h"
15 #include <openssl/asn1t.h>
17 IMPLEMENT_ASN1_DUP_FUNCTION(ASN1_UTCTIME)
19 /* This is the primary function used to parse ASN1_UTCTIME */
20 int ossl_asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d)
22 /* wrapper around ossl_asn1_time_to_tm */
23 if (d->type != V_ASN1_UTCTIME)
25 return ossl_asn1_time_to_tm(tm, d);
28 int ASN1_UTCTIME_check(const ASN1_UTCTIME *d)
30 return ossl_asn1_utctime_to_tm(NULL, d);
33 /* Sets the string via simple copy without cleaning it up */
34 int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str)
38 t.type = V_ASN1_UTCTIME;
39 t.length = strlen(str);
40 t.data = (unsigned char *)str;
43 if (!ASN1_UTCTIME_check(&t))
46 if (s != NULL && !ASN1_STRING_copy(s, &t))
52 ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t)
54 return ASN1_UTCTIME_adj(s, t, 0, 0);
57 ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
58 int offset_day, long offset_sec)
63 ts = OPENSSL_gmtime(&t, &data);
67 if (offset_day || offset_sec) {
68 if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
72 return ossl_asn1_time_from_tm(s, ts, V_ASN1_UTCTIME);
75 int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t)
80 if (!ossl_asn1_utctime_to_tm(&stm, s))
83 if (OPENSSL_gmtime(&t, &ttm) == NULL)
86 if (!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm))
89 if (day > 0 || sec > 0)
91 if (day < 0 || sec < 0)
96 int ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm)
98 if (tm->type != V_ASN1_UTCTIME)
100 return ASN1_TIME_print(bp, tm);