]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/a_gentm.c
Add asn1_time_to_tm function and check days in month
[thirdparty/openssl.git] / crypto / asn1 / a_gentm.c
CommitLineData
b1322259 1/*
60eba30f 2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
0f113f3e 3 *
b1322259
RS
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
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>
1c455bc0 18#include "asn1_locl.h"
f6aed2cd 19
1c455bc0 20int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d)
0f113f3e 21{
3d0f1cb9 22 /* wrapper around asn1_time_to_tm */
0f113f3e 23 if (d->type != V_ASN1_GENERALIZEDTIME)
3d0f1cb9
PY
24 return 0;
25 return asn1_time_to_tm(tm, d);
26 }
f6aed2cd 27
1c455bc0 28int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d)
0f113f3e
MC
29{
30 return asn1_generalizedtime_to_tm(NULL, d);
31}
1c455bc0 32
875a644a 33int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str)
0f113f3e
MC
34{
35 ASN1_GENERALIZEDTIME t;
f6aed2cd 36
0f113f3e
MC
37 t.type = V_ASN1_GENERALIZEDTIME;
38 t.length = strlen(str);
39 t.data = (unsigned char *)str;
04e62715
RS
40 t.flags = 0;
41
0f113f3e
MC
42 if (ASN1_GENERALIZEDTIME_check(&t)) {
43 if (s != NULL) {
a026fbf9 44 if (!ASN1_STRING_set((ASN1_STRING *)s, str, t.length))
0f113f3e
MC
45 return 0;
46 s->type = V_ASN1_GENERALIZEDTIME;
47 }
48 return (1);
49 } else
50 return (0);
51}
f6aed2cd 52
6b691a5c 53ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
0f113f3e
MC
54 time_t t)
55{
56 return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0);
57}
87d3a0cd
DSH
58
59ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
0f113f3e
MC
60 time_t t, int offset_day,
61 long offset_sec)
62{
63 char *p;
64 struct tm *ts;
65 struct tm data;
60eba30f 66 const size_t len = 20;
fe71bb3a 67 ASN1_GENERALIZEDTIME *tmps = NULL;
f6aed2cd 68
0f113f3e 69 if (s == NULL)
fe71bb3a
MC
70 tmps = ASN1_GENERALIZEDTIME_new();
71 else
72 tmps = s;
73 if (tmps == NULL)
74 return NULL;
f6aed2cd 75
0f113f3e
MC
76 ts = OPENSSL_gmtime(&t, &data);
77 if (ts == NULL)
fe71bb3a 78 goto err;
b8e35bd6 79
0f113f3e
MC
80 if (offset_day || offset_sec) {
81 if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
fe71bb3a 82 goto err;
0f113f3e 83 }
87d3a0cd 84
fe71bb3a
MC
85 p = (char *)tmps->data;
86 if ((p == NULL) || ((size_t)tmps->length < len)) {
0f113f3e
MC
87 p = OPENSSL_malloc(len);
88 if (p == NULL) {
89 ASN1err(ASN1_F_ASN1_GENERALIZEDTIME_ADJ, ERR_R_MALLOC_FAILURE);
fe71bb3a 90 goto err;
0f113f3e 91 }
fe71bb3a
MC
92 OPENSSL_free(tmps->data);
93 tmps->data = (unsigned char *)p;
0f113f3e 94 }
f6aed2cd 95
60eba30f
P
96 tmps->length = BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ",
97 ts->tm_year + 1900, ts->tm_mon + 1,
98 ts->tm_mday, ts->tm_hour, ts->tm_min,
99 ts->tm_sec);
fe71bb3a 100 tmps->type = V_ASN1_GENERALIZEDTIME;
a53955d8 101#ifdef CHARSET_EBCDIC_not
fe71bb3a 102 ebcdic2ascii(tmps->data, tmps->data, tmps->length);
a53955d8 103#endif
fe71bb3a
MC
104 return tmps;
105 err:
106 if (s == NULL)
107 ASN1_GENERALIZEDTIME_free(tmps);
108 return NULL;
0f113f3e 109}
0d0099ea
DSH
110
111const char *_asn1_mon[12] = {
112 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
113 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
114};
115
116int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
117{
118 char *v;
119 int gmt = 0;
120 int i;
121 int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0;
122 char *f = NULL;
123 int f_len = 0;
124
125 i = tm->length;
126 v = (char *)tm->data;
127
128 if (i < 12)
129 goto err;
130 if (v[i - 1] == 'Z')
131 gmt = 1;
132 for (i = 0; i < 12; i++)
133 if ((v[i] > '9') || (v[i] < '0'))
134 goto err;
135 y = (v[0] - '0') * 1000 + (v[1] - '0') * 100
136 + (v[2] - '0') * 10 + (v[3] - '0');
137 M = (v[4] - '0') * 10 + (v[5] - '0');
138 if ((M > 12) || (M < 1))
139 goto err;
140 d = (v[6] - '0') * 10 + (v[7] - '0');
141 h = (v[8] - '0') * 10 + (v[9] - '0');
142 m = (v[10] - '0') * 10 + (v[11] - '0');
143 if (tm->length >= 14 &&
144 (v[12] >= '0') && (v[12] <= '9') &&
145 (v[13] >= '0') && (v[13] <= '9')) {
146 s = (v[12] - '0') * 10 + (v[13] - '0');
147 /* Check for fractions of seconds. */
148 if (tm->length >= 15 && v[14] == '.') {
149 int l = tm->length;
150 f = &v[14]; /* The decimal point. */
151 f_len = 1;
152 while (14 + f_len < l && f[f_len] >= '0' && f[f_len] <= '9')
153 ++f_len;
154 }
155 }
156
157 if (BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s",
158 _asn1_mon[M - 1], d, h, m, s, f_len, f, y,
159 (gmt) ? " GMT" : "") <= 0)
160 return (0);
161 else
162 return (1);
163 err:
164 BIO_write(bp, "Bad time value", 14);
165 return (0);
166}