2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
13 #include "internal/cryptlib.h"
14 #include <openssl/asn1.h>
15 #include "asn1_locl.h"
17 /* This is the primary function used to parse ASN1_UTCTIME */
18 int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d)
20 /* wrapper around ans1_time_to_tm */
21 if (d->type != V_ASN1_UTCTIME)
23 return asn1_time_to_tm(tm, d);
26 int ASN1_UTCTIME_check(const ASN1_UTCTIME *d)
28 return asn1_utctime_to_tm(NULL, d);
31 /* Sets the string via simple copy without cleaning it up */
32 int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str)
36 t.type = V_ASN1_UTCTIME;
37 t.length = strlen(str);
38 t.data = (unsigned char *)str;
41 if (!ASN1_UTCTIME_check(&t))
44 if (s != NULL && !ASN1_STRING_copy(s, &t))
50 ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t)
52 return ASN1_UTCTIME_adj(s, t, 0, 0);
55 ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
56 int offset_day, long offset_sec)
61 ts = OPENSSL_gmtime(&t, &data);
65 if (offset_day || offset_sec) {
66 if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
70 return asn1_time_from_tm(s, ts, V_ASN1_UTCTIME);
73 int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t)
78 if (!asn1_utctime_to_tm(&stm, s))
81 if (OPENSSL_gmtime(&t, &ttm) == NULL)
84 if (!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm))
87 if (day > 0 || sec > 0)
89 if (day < 0 || sec < 0)
94 int ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm)
96 if (tm->type != V_ASN1_UTCTIME)
98 return ASN1_TIME_print(bp, tm);