Add asn1_time_to_tm function and check days in month
[openssl.git] / crypto / asn1 / a_gentm.c
1 /*
2  * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /*
11  * GENERALIZEDTIME implementation. Based on UTCTIME
12  */
13
14 #include <stdio.h>
15 #include <time.h>
16 #include "internal/cryptlib.h"
17 #include <openssl/asn1.h>
18 #include "asn1_locl.h"
19
20 int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d)
21 {
22     /* wrapper around asn1_time_to_tm */
23     if (d->type != V_ASN1_GENERALIZEDTIME)
24         return 0;
25     return asn1_time_to_tm(tm, d);
26  }
27
28 int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d)
29 {
30     return asn1_generalizedtime_to_tm(NULL, d);
31 }
32
33 int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str)
34 {
35     ASN1_GENERALIZEDTIME t;
36
37     t.type = V_ASN1_GENERALIZEDTIME;
38     t.length = strlen(str);
39     t.data = (unsigned char *)str;
40     t.flags = 0;
41
42     if (ASN1_GENERALIZEDTIME_check(&t)) {
43         if (s != NULL) {
44             if (!ASN1_STRING_set((ASN1_STRING *)s, str, t.length))
45                 return 0;
46             s->type = V_ASN1_GENERALIZEDTIME;
47         }
48         return (1);
49     } else
50         return (0);
51 }
52
53 ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
54                                                time_t t)
55 {
56     return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0);
57 }
58
59 ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
60                                                time_t t, int offset_day,
61                                                long offset_sec)
62 {
63     char *p;
64     struct tm *ts;
65     struct tm data;
66     const size_t len = 20;
67     ASN1_GENERALIZEDTIME *tmps = NULL;
68
69     if (s == NULL)
70         tmps = ASN1_GENERALIZEDTIME_new();
71     else
72         tmps = s;
73     if (tmps == NULL)
74         return NULL;
75
76     ts = OPENSSL_gmtime(&t, &data);
77     if (ts == NULL)
78         goto err;
79
80     if (offset_day || offset_sec) {
81         if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
82             goto err;
83     }
84
85     p = (char *)tmps->data;
86     if ((p == NULL) || ((size_t)tmps->length < len)) {
87         p = OPENSSL_malloc(len);
88         if (p == NULL) {
89             ASN1err(ASN1_F_ASN1_GENERALIZEDTIME_ADJ, ERR_R_MALLOC_FAILURE);
90             goto err;
91         }
92         OPENSSL_free(tmps->data);
93         tmps->data = (unsigned char *)p;
94     }
95
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);
100     tmps->type = V_ASN1_GENERALIZEDTIME;
101 #ifdef CHARSET_EBCDIC_not
102     ebcdic2ascii(tmps->data, tmps->data, tmps->length);
103 #endif
104     return tmps;
105  err:
106     if (s == NULL)
107         ASN1_GENERALIZEDTIME_free(tmps);
108     return NULL;
109 }
110
111 const char *_asn1_mon[12] = {
112     "Jan", "Feb", "Mar", "Apr", "May", "Jun",
113     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
114 };
115
116 int 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 }