1/* $OpenBSD: a_time_tm.c,v 1.16 2020/12/16 18:35:59 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		lt = &ltm;
113	memset(lt, 0, sizeof(*lt));
114
115	/* Timezone is required and must be GMT (Zulu). */
116	if (bytes[len - 1] != 'Z')
117		return (-1);
118
119	/* Make sure everything else is digits. */
120	for (i = 0; i < len - 1; i++) {
121		if (isdigit((unsigned char)bytes[i]))
122			continue;
123		return (-1);
124	}
125
126	/*
127	 * Validate and convert the time
128	 */
129	p = bytes;
130	switch (len) {
131	case GENTIME_LENGTH:
132		if (mode == V_ASN1_UTCTIME)
133			return (-1);
134		lt->tm_year = (ATOI2(p) * 100) - 1900;	/* cc */
135		type = V_ASN1_GENERALIZEDTIME;
136		/* FALLTHROUGH */
137	case UTCTIME_LENGTH:
138		if (type == 0) {
139			if (mode == V_ASN1_GENERALIZEDTIME)
140				return (-1);
141			type = V_ASN1_UTCTIME;
142		}
143		lt->tm_year += ATOI2(p);		/* yy */
144		if (type == V_ASN1_UTCTIME) {
145			if (lt->tm_year < 50)
146				lt->tm_year += 100;
147		}
148		lt->tm_mon = ATOI2(p) - 1;		/* mm */
149		if (lt->tm_mon < 0 || lt->tm_mon > 11)
150			return (-1);
151		lt->tm_mday = ATOI2(p);			/* dd */
152		if (lt->tm_mday < 1 || lt->tm_mday > 31)
153			return (-1);
154		lt->tm_hour = ATOI2(p);			/* HH */
155		if (lt->tm_hour < 0 || lt->tm_hour > 23)
156			return (-1);
157		lt->tm_min = ATOI2(p);			/* MM */
158		if (lt->tm_min < 0 || lt->tm_min > 59)
159			return (-1);
160		lt->tm_sec = ATOI2(p);			/* SS */
161		/* Leap second 60 is not accepted. Reconsider later? */
162		if (lt->tm_sec < 0 || lt->tm_sec > 59)
163			return (-1);
164		break;
165	default:
166		return (-1);
167	}
168
169	return (type);
170}