]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/a_gentm.c
threads_pthread.c: change inline to ossl_inline
[thirdparty/openssl.git] / crypto / asn1 / a_gentm.c
CommitLineData
b1322259 1/*
3c2bdd7d 2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
0f113f3e 3 *
365a2d99 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
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
f6aed2cd
DSH
8 */
9
0f113f3e 10/*
b1322259 11 * GENERALIZEDTIME implementation. Based on UTCTIME
0f113f3e 12 */
f6aed2cd
DSH
13
14#include <stdio.h>
15#include <time.h>
b39fc560 16#include "internal/cryptlib.h"
ec577822 17#include <openssl/asn1.h>
706457b7 18#include "asn1_local.h"
fe4309b0
PY
19#include <openssl/asn1t.h>
20
21IMPLEMENT_ASN1_DUP_FUNCTION(ASN1_GENERALIZEDTIME)
f6aed2cd 22
cf37aaa3 23/* This is the primary function used to parse ASN1_GENERALIZEDTIME */
adf7e6d1
SL
24static int asn1_generalizedtime_to_tm(struct tm *tm,
25 const ASN1_GENERALIZEDTIME *d)
0f113f3e 26{
adf7e6d1 27 /* wrapper around ossl_asn1_time_to_tm */
0f113f3e 28 if (d->type != V_ASN1_GENERALIZEDTIME)
3d0f1cb9 29 return 0;
adf7e6d1 30 return ossl_asn1_time_to_tm(tm, d);
52b6e17d 31}
f6aed2cd 32
1c455bc0 33int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d)
0f113f3e
MC
34{
35 return asn1_generalizedtime_to_tm(NULL, d);
36}
1c455bc0 37
875a644a 38int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str)
0f113f3e
MC
39{
40 ASN1_GENERALIZEDTIME t;
f6aed2cd 41
0f113f3e
MC
42 t.type = V_ASN1_GENERALIZEDTIME;
43 t.length = strlen(str);
44 t.data = (unsigned char *)str;
04e62715
RS
45 t.flags = 0;
46
cf37aaa3
TS
47 if (!ASN1_GENERALIZEDTIME_check(&t))
48 return 0;
49
50 if (s != NULL && !ASN1_STRING_copy(s, &t))
51 return 0;
52
53 return 1;
0f113f3e 54}
f6aed2cd 55
6b691a5c 56ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
0f113f3e
MC
57 time_t t)
58{
59 return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0);
60}
87d3a0cd
DSH
61
62ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
0f113f3e
MC
63 time_t t, int offset_day,
64 long offset_sec)
65{
0f113f3e
MC
66 struct tm *ts;
67 struct tm data;
f6aed2cd 68
0f113f3e
MC
69 ts = OPENSSL_gmtime(&t, &data);
70 if (ts == NULL)
cf37aaa3 71 return NULL;
b8e35bd6 72
0f113f3e
MC
73 if (offset_day || offset_sec) {
74 if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
cf37aaa3 75 return NULL;
0f113f3e 76 }
f6aed2cd 77
adf7e6d1 78 return ossl_asn1_time_from_tm(s, ts, V_ASN1_GENERALIZEDTIME);
0f113f3e 79}
0d0099ea 80
0d0099ea
DSH
81int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
82{
f673c4f8
PY
83 if (tm->type != V_ASN1_GENERALIZEDTIME)
84 return 0;
85 return ASN1_TIME_print(bp, tm);
0d0099ea 86}