]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/a_utctm.c
Update copyright year
[thirdparty/openssl.git] / crypto / asn1 / a_utctm.c
CommitLineData
2039c421 1/*
3c2bdd7d 2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
365a2d99 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
2039c421
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
11#include <time.h>
b39fc560 12#include "internal/cryptlib.h"
7ab1a391 13#include <openssl/asn1.h>
706457b7 14#include "asn1_local.h"
fe4309b0
PY
15#include <openssl/asn1t.h>
16
17IMPLEMENT_ASN1_DUP_FUNCTION(ASN1_UTCTIME)
d02b48c6 18
cf37aaa3 19/* This is the primary function used to parse ASN1_UTCTIME */
adf7e6d1 20int ossl_asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d)
0f113f3e 21{
adf7e6d1 22 /* wrapper around ossl_asn1_time_to_tm */
0f113f3e 23 if (d->type != V_ASN1_UTCTIME)
60eba30f 24 return 0;
adf7e6d1 25 return ossl_asn1_time_to_tm(tm, d);
0f113f3e 26}
1c455bc0
DSH
27
28int ASN1_UTCTIME_check(const ASN1_UTCTIME *d)
0f113f3e 29{
adf7e6d1 30 return ossl_asn1_utctime_to_tm(NULL, d);
0f113f3e 31}
d02b48c6 32
cf37aaa3 33/* Sets the string via simple copy without cleaning it up */
875a644a 34int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str)
0f113f3e
MC
35{
36 ASN1_UTCTIME t;
37
38 t.type = V_ASN1_UTCTIME;
39 t.length = strlen(str);
40 t.data = (unsigned char *)str;
04e62715
RS
41 t.flags = 0;
42
cf37aaa3
TS
43 if (!ASN1_UTCTIME_check(&t))
44 return 0;
45
46 if (s != NULL && !ASN1_STRING_copy(s, &t))
47 return 0;
48
49 return 1;
0f113f3e 50}
58964a49 51
6b691a5c 52ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t)
0f113f3e
MC
53{
54 return ASN1_UTCTIME_adj(s, t, 0, 0);
55}
87d3a0cd
DSH
56
57ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
0f113f3e
MC
58 int offset_day, long offset_sec)
59{
0f113f3e
MC
60 struct tm *ts;
61 struct tm data;
0f113f3e
MC
62
63 ts = OPENSSL_gmtime(&t, &data);
64 if (ts == NULL)
cf37aaa3 65 return NULL;
0f113f3e
MC
66
67 if (offset_day || offset_sec) {
68 if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
cf37aaa3 69 return NULL;
0f113f3e
MC
70 }
71
adf7e6d1 72 return ossl_asn1_time_from_tm(s, ts, V_ASN1_UTCTIME);
0f113f3e 73}
61f175f4
BM
74
75int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t)
0f113f3e
MC
76{
77 struct tm stm, ttm;
78 int day, sec;
79
adf7e6d1 80 if (!ossl_asn1_utctime_to_tm(&stm, s))
0f113f3e
MC
81 return -2;
82
52b6e17d 83 if (OPENSSL_gmtime(&t, &ttm) == NULL)
0f113f3e
MC
84 return -2;
85
da27006d 86 if (!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm))
0f113f3e
MC
87 return -2;
88
cf37aaa3 89 if (day > 0 || sec > 0)
0f113f3e 90 return 1;
cf37aaa3 91 if (day < 0 || sec < 0)
0f113f3e
MC
92 return -1;
93 return 0;
94}
0d0099ea
DSH
95
96int ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm)
97{
f673c4f8
PY
98 if (tm->type != V_ASN1_UTCTIME)
99 return 0;
100 return ASN1_TIME_print(bp, tm);
0d0099ea 101}