]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/asn1/a_utctm.c
2a864180e3e9a12525669602acbdad23f290cfd2
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
12 #include "internal/cryptlib.h"
13 #include <openssl/asn1.h>
14 #include "asn1_locl.h"
16 int asn1_utctime_to_tm(struct tm
*tm
, const ASN1_UTCTIME
*d
)
18 /* wrapper around ans1_time_to_tm */
19 if (d
->type
!= V_ASN1_UTCTIME
)
21 return asn1_time_to_tm(tm
, d
);
24 int ASN1_UTCTIME_check(const ASN1_UTCTIME
*d
)
26 return asn1_utctime_to_tm(NULL
, d
);
29 int ASN1_UTCTIME_set_string(ASN1_UTCTIME
*s
, const char *str
)
33 t
.type
= V_ASN1_UTCTIME
;
34 t
.length
= strlen(str
);
35 t
.data
= (unsigned char *)str
;
38 if (ASN1_UTCTIME_check(&t
)) {
40 if (!ASN1_STRING_set((ASN1_STRING
*)s
, str
, t
.length
))
42 s
->type
= V_ASN1_UTCTIME
;
49 ASN1_UTCTIME
*ASN1_UTCTIME_set(ASN1_UTCTIME
*s
, time_t t
)
51 return ASN1_UTCTIME_adj(s
, t
, 0, 0);
54 ASN1_UTCTIME
*ASN1_UTCTIME_adj(ASN1_UTCTIME
*s
, time_t t
,
55 int offset_day
, long offset_sec
)
60 const size_t len
= 20;
64 s
= ASN1_UTCTIME_new();
70 ts
= OPENSSL_gmtime(&t
, &data
);
74 if (offset_day
|| offset_sec
) {
75 if (!OPENSSL_gmtime_adj(ts
, offset_day
, offset_sec
))
79 if ((ts
->tm_year
< 50) || (ts
->tm_year
>= 150))
83 if ((p
== NULL
) || ((size_t)s
->length
< len
)) {
84 p
= OPENSSL_malloc(len
);
86 ASN1err(ASN1_F_ASN1_UTCTIME_ADJ
, ERR_R_MALLOC_FAILURE
);
89 OPENSSL_free(s
->data
);
90 s
->data
= (unsigned char *)p
;
93 s
->length
= BIO_snprintf(p
, len
, "%02d%02d%02d%02d%02d%02dZ",
94 ts
->tm_year
% 100, ts
->tm_mon
+ 1, ts
->tm_mday
,
95 ts
->tm_hour
, ts
->tm_min
, ts
->tm_sec
);
96 s
->type
= V_ASN1_UTCTIME
;
97 #ifdef CHARSET_EBCDIC_not
98 ebcdic2ascii(s
->data
, s
->data
, s
->length
);
103 ASN1_UTCTIME_free(s
);
107 int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME
*s
, time_t t
)
112 if (!asn1_utctime_to_tm(&stm
, s
))
115 if (!OPENSSL_gmtime(&t
, &ttm
))
118 if (!OPENSSL_gmtime_diff(&day
, &sec
, &ttm
, &stm
))
132 int ASN1_UTCTIME_print(BIO
*bp
, const ASN1_UTCTIME
*tm
)
137 int y
= 0, M
= 0, d
= 0, h
= 0, m
= 0, s
= 0;
140 v
= (const char *)tm
->data
;
146 for (i
= 0; i
< 10; i
++)
147 if ((v
[i
] > '9') || (v
[i
] < '0'))
149 y
= (v
[0] - '0') * 10 + (v
[1] - '0');
152 M
= (v
[2] - '0') * 10 + (v
[3] - '0');
153 if ((M
> 12) || (M
< 1))
155 d
= (v
[4] - '0') * 10 + (v
[5] - '0');
156 h
= (v
[6] - '0') * 10 + (v
[7] - '0');
157 m
= (v
[8] - '0') * 10 + (v
[9] - '0');
158 if (tm
->length
>= 12 &&
159 (v
[10] >= '0') && (v
[10] <= '9') && (v
[11] >= '0') && (v
[11] <= '9'))
160 s
= (v
[10] - '0') * 10 + (v
[11] - '0');
162 if (BIO_printf(bp
, "%s %2d %02d:%02d:%02d %d%s",
163 _asn1_mon
[M
- 1], d
, h
, m
, s
, y
+ 1900,
164 (gmt
) ? " GMT" : "") <= 0)
168 BIO_write(bp
, "Bad time value", 14);