1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <stdio.h>
11e1051a39Sopenharmony_ci#include <time.h>
12e1051a39Sopenharmony_ci#include "internal/cryptlib.h"
13e1051a39Sopenharmony_ci#include <openssl/asn1.h>
14e1051a39Sopenharmony_ci#include "asn1_local.h"
15e1051a39Sopenharmony_ci#include <openssl/asn1t.h>
16e1051a39Sopenharmony_ci
17e1051a39Sopenharmony_ciIMPLEMENT_ASN1_DUP_FUNCTION(ASN1_UTCTIME)
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_ci/* This is the primary function used to parse ASN1_UTCTIME */
20e1051a39Sopenharmony_ciint ossl_asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d)
21e1051a39Sopenharmony_ci{
22e1051a39Sopenharmony_ci    /* wrapper around ossl_asn1_time_to_tm */
23e1051a39Sopenharmony_ci    if (d->type != V_ASN1_UTCTIME)
24e1051a39Sopenharmony_ci        return 0;
25e1051a39Sopenharmony_ci    return ossl_asn1_time_to_tm(tm, d);
26e1051a39Sopenharmony_ci}
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_ciint ASN1_UTCTIME_check(const ASN1_UTCTIME *d)
29e1051a39Sopenharmony_ci{
30e1051a39Sopenharmony_ci    return ossl_asn1_utctime_to_tm(NULL, d);
31e1051a39Sopenharmony_ci}
32e1051a39Sopenharmony_ci
33e1051a39Sopenharmony_ci/* Sets the string via simple copy without cleaning it up */
34e1051a39Sopenharmony_ciint ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str)
35e1051a39Sopenharmony_ci{
36e1051a39Sopenharmony_ci    ASN1_UTCTIME t;
37e1051a39Sopenharmony_ci
38e1051a39Sopenharmony_ci    t.type = V_ASN1_UTCTIME;
39e1051a39Sopenharmony_ci    t.length = strlen(str);
40e1051a39Sopenharmony_ci    t.data = (unsigned char *)str;
41e1051a39Sopenharmony_ci    t.flags = 0;
42e1051a39Sopenharmony_ci
43e1051a39Sopenharmony_ci    if (!ASN1_UTCTIME_check(&t))
44e1051a39Sopenharmony_ci        return 0;
45e1051a39Sopenharmony_ci
46e1051a39Sopenharmony_ci    if (s != NULL && !ASN1_STRING_copy(s, &t))
47e1051a39Sopenharmony_ci        return 0;
48e1051a39Sopenharmony_ci
49e1051a39Sopenharmony_ci    return 1;
50e1051a39Sopenharmony_ci}
51e1051a39Sopenharmony_ci
52e1051a39Sopenharmony_ciASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t)
53e1051a39Sopenharmony_ci{
54e1051a39Sopenharmony_ci    return ASN1_UTCTIME_adj(s, t, 0, 0);
55e1051a39Sopenharmony_ci}
56e1051a39Sopenharmony_ci
57e1051a39Sopenharmony_ciASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
58e1051a39Sopenharmony_ci                               int offset_day, long offset_sec)
59e1051a39Sopenharmony_ci{
60e1051a39Sopenharmony_ci    struct tm *ts;
61e1051a39Sopenharmony_ci    struct tm data;
62e1051a39Sopenharmony_ci
63e1051a39Sopenharmony_ci    ts = OPENSSL_gmtime(&t, &data);
64e1051a39Sopenharmony_ci    if (ts == NULL)
65e1051a39Sopenharmony_ci        return NULL;
66e1051a39Sopenharmony_ci
67e1051a39Sopenharmony_ci    if (offset_day || offset_sec) {
68e1051a39Sopenharmony_ci        if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
69e1051a39Sopenharmony_ci            return NULL;
70e1051a39Sopenharmony_ci    }
71e1051a39Sopenharmony_ci
72e1051a39Sopenharmony_ci    return ossl_asn1_time_from_tm(s, ts, V_ASN1_UTCTIME);
73e1051a39Sopenharmony_ci}
74e1051a39Sopenharmony_ci
75e1051a39Sopenharmony_ciint ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t)
76e1051a39Sopenharmony_ci{
77e1051a39Sopenharmony_ci    struct tm stm, ttm;
78e1051a39Sopenharmony_ci    int day, sec;
79e1051a39Sopenharmony_ci
80e1051a39Sopenharmony_ci    if (!ossl_asn1_utctime_to_tm(&stm, s))
81e1051a39Sopenharmony_ci        return -2;
82e1051a39Sopenharmony_ci
83e1051a39Sopenharmony_ci    if (OPENSSL_gmtime(&t, &ttm) == NULL)
84e1051a39Sopenharmony_ci        return -2;
85e1051a39Sopenharmony_ci
86e1051a39Sopenharmony_ci    if (!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm))
87e1051a39Sopenharmony_ci        return -2;
88e1051a39Sopenharmony_ci
89e1051a39Sopenharmony_ci    if (day > 0 || sec > 0)
90e1051a39Sopenharmony_ci        return 1;
91e1051a39Sopenharmony_ci    if (day < 0 || sec < 0)
92e1051a39Sopenharmony_ci        return -1;
93e1051a39Sopenharmony_ci    return 0;
94e1051a39Sopenharmony_ci}
95e1051a39Sopenharmony_ci
96e1051a39Sopenharmony_ciint ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm)
97e1051a39Sopenharmony_ci{
98e1051a39Sopenharmony_ci    if (tm->type != V_ASN1_UTCTIME)
99e1051a39Sopenharmony_ci        return 0;
100e1051a39Sopenharmony_ci    return ASN1_TIME_print(bp, tm);
101e1051a39Sopenharmony_ci}
102