1/* $OpenBSD: a_time_tm.c,v 1.15 2018/04/25 11:48:21 tb Exp $ */
2/*
3 * Copyright (c) 2015 Bob Beck <beck@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include "includes.h"
19
20#include <ctype.h>
21#include <limits.h>
22#include <stdio.h>
23#include <string.h>
24#include <time.h>
25
26#define GENTIME_LENGTH 15
27#define UTCTIME_LENGTH 13
28
29#define V_ASN1_UTCTIME		23
30#define V_ASN1_GENERALIZEDTIME	24
31
32int
33ASN1_time_tm_cmp(struct tm *tm1, struct tm *tm2)
34{
35	if (tm1->tm_year < tm2->tm_year)
36		return (-1);
37	if (tm1->tm_year > tm2->tm_year)
38		return (1);
39	if (tm1->tm_mon < tm2->tm_mon)
40		return (-1);
41	if (tm1->tm_mon > tm2->tm_mon)
42		return (1);
43	if (tm1->tm_mday < tm2->tm_mday)
44		return (-1);
45	if (tm1->tm_mday > tm2->tm_mday)
46		return (1);
47	if (tm1->tm_hour < tm2->tm_hour)
48		return (-1);
49	if (tm1->tm_hour > tm2->tm_hour)
50		return (1);
51	if (tm1->tm_min < tm2->tm_min)
52		return (-1);
53	if (tm1->tm_min > tm2->tm_min)
54		return (1);
55	if (tm1->tm_sec < tm2->tm_sec)
56		return (-1);
57	if (tm1->tm_sec > tm2->tm_sec)
58		return (1);
59	return 0;
60}
61
62int
63ASN1_time_tm_clamp_notafter(struct tm *tm)
64{
65#ifdef SMALL_TIME_T
66	struct tm broken_os_epoch_tm;
67	time_t broken_os_epoch_time = INT_MAX;
68
69	if (gmtime_r(&broken_os_epoch_time, &broken_os_epoch_tm) == NULL)
70		return 0;
71
72	if (ASN1_time_tm_cmp(tm, &broken_os_epoch_tm) == 1)
73		memcpy(tm, &broken_os_epoch_tm, sizeof(*tm));
74#endif
75	return 1;
76}
77
78/*
79 * Parse an RFC 5280 format ASN.1 time string.
80 *
81 * mode must be:
82 * 0 if we expect to parse a time as specified in RFC 5280 for an X509 object.
83 * V_ASN1_UTCTIME if we wish to parse an RFC5280 format UTC time.
84 * V_ASN1_GENERALIZEDTIME if we wish to parse an RFC5280 format Generalized time.
85 *
86 * Returns:
87 * -1 if the string was invalid.
88 * V_ASN1_UTCTIME if the string validated as a UTC time string.
89 * V_ASN1_GENERALIZEDTIME if the string validated as a Generalized time string.
90 *
91 * Fills in *tm with the corresponding time if tm is non NULL.
92 */
93#define	ATOI2(ar)	((ar) += 2, ((ar)[-2] - '0') * 10 + ((ar)[-1] - '0'))
94int
95ASN1_time_parse(const char *bytes, size_t len, struct tm *tm, int mode)
96{
97	size_t i;
98	int type = 0;
99	struct tm ltm;
100	struct tm *lt;
101	const char *p;
102
103	if (bytes == NULL)
104		return (-1);
105
106	/* Constrain to valid lengths. */
107	if (len != UTCTIME_LENGTH && len != GENTIME_LENGTH)
108		return (-1);
109
110	lt = tm;
111	if (lt == NULL) {
112		memset(&ltm, 0, sizeof(ltm));
113		lt = &ltm;
114	}
115
116	/* Timezone is required and must be GMT (Zulu). */
117	if (bytes[len - 1] != 'Z')
118		return (-1);
119
120	/* Make sure everything else is digits. */
121	for (i = 0; i < len - 1; i++) {
122		if (isdigit((unsigned char)bytes[i]))
123			continue;
124		return (-1);
125	}
126
127	/*
128	 * Validate and convert the time
129	 */
130	p = bytes;
131	switch (len) {
132	case GENTIME_LENGTH:
133		if (mode == V_ASN1_UTCTIME)
134			return (-1);
135		lt->tm_year = (ATOI2(p) * 100) - 1900;	/* cc */
136		type = V_ASN1_GENERALIZEDTIME;
137		/* FALLTHROUGH */
138	case UTCTIME_LENGTH:
139		if (type == 0) {
140			if (mode == V_ASN1_GENERALIZEDTIME)
141				return (-1);
142			type = V_ASN1_UTCTIME;
143		}
144		lt->tm_year += ATOI2(p);		/* yy */
145		if (type == V_ASN1_UTCTIME) {
146			if (lt->tm_year < 50)
147				lt->tm_year += 100;
148		}
149		lt->tm_mon = ATOI2(p) - 1;		/* mm */
150		if (lt->tm_mon < 0 || lt->tm_mon > 11)
151			return (-1);
152		lt->tm_mday = ATOI2(p);			/* dd */
153		if (lt->tm_mday < 1 || lt->tm_mday > 31)
154			return (-1);
155		lt->tm_hour = ATOI2(p);			/* HH */
156		if (lt->tm_hour < 0 || lt->tm_hour > 23)
157			return (-1);
158		lt->tm_min = ATOI2(p);			/* MM */
159		if (lt->tm_min < 0 || lt->tm_min > 59)
160			return (-1);
161		lt->tm_sec = ATOI2(p);			/* SS */
162		/* Leap second 60 is not accepted. Reconsider later? */
163		if (lt->tm_sec < 0 || lt->tm_sec > 59)
164			return (-1);
165		break;
166	default:
167		return (-1);
168	}
169
170	return (type);
171}
172