summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjsing <>2015-10-04 15:15:11 +0000
committerjsing <>2015-10-04 15:15:11 +0000
commit726014e92eca3a02222ed5d31eebedde1b32b828 (patch)
treeaef67661749b4e1ff4ac8dc2f8039a60b75743b1
parent9001a2c5adce53e827d3b8f30da9b5170fbea15f (diff)
downloadopenbsd-726014e92eca3a02222ed5d31eebedde1b32b828.tar.gz
openbsd-726014e92eca3a02222ed5d31eebedde1b32b828.tar.bz2
openbsd-726014e92eca3a02222ed5d31eebedde1b32b828.zip
Apply some style(9), tweak a few things for readability and add some
additional bounds checks. ok beck@
-rw-r--r--src/lib/libcrypto/asn1/a_time_tm.c76
-rw-r--r--src/lib/libssl/src/crypto/asn1/a_time_tm.c76
2 files changed, 80 insertions, 72 deletions
diff --git a/src/lib/libcrypto/asn1/a_time_tm.c b/src/lib/libcrypto/asn1/a_time_tm.c
index 65f75c68cc..7b25e439c4 100644
--- a/src/lib/libcrypto/asn1/a_time_tm.c
+++ b/src/lib/libcrypto/asn1/a_time_tm.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: a_time_tm.c,v 1.1 2015/10/02 15:04:45 beck Exp $ */ 1/* $OpenBSD: a_time_tm.c,v 1.2 2015/10/04 15:15:11 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2015 Bob Beck <beck@openbsd.org> 3 * Copyright (c) 2015 Bob Beck <beck@openbsd.org>
4 * 4 *
@@ -32,13 +32,16 @@ gentime_string_from_tm(struct tm *tm)
32{ 32{
33 char *ret = NULL; 33 char *ret = NULL;
34 int year; 34 int year;
35
35 year = tm->tm_year + 1900; 36 year = tm->tm_year + 1900;
36 if (year < 0 || year > 9999) 37 if (year < 0 || year > 9999)
37 return (NULL); 38 return (NULL);
39
38 if (asprintf(&ret, "%04u%02u%02u%02u%02u%02uZ", year, 40 if (asprintf(&ret, "%04u%02u%02u%02u%02u%02uZ", year,
39 tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, 41 tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min,
40 tm->tm_sec) == -1) 42 tm->tm_sec) == -1)
41 ret = NULL; 43 ret = NULL;
44
42 return (ret); 45 return (ret);
43} 46}
44 47
@@ -46,12 +49,15 @@ char *
46utctime_string_from_tm(struct tm *tm) 49utctime_string_from_tm(struct tm *tm)
47{ 50{
48 char *ret = NULL; 51 char *ret = NULL;
52
49 if (tm->tm_year >= 150 || tm->tm_year < 50) 53 if (tm->tm_year >= 150 || tm->tm_year < 50)
50 return (NULL); 54 return (NULL);
55
51 if (asprintf(&ret, "%02u%02u%02u%02u%02u%02uZ", 56 if (asprintf(&ret, "%02u%02u%02u%02u%02u%02uZ",
52 tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday, 57 tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday,
53 tm->tm_hour, tm->tm_min, tm->tm_sec) == -1) 58 tm->tm_hour, tm->tm_min, tm->tm_sec) == -1)
54 ret = NULL; 59 ret = NULL;
60
55 return (ret); 61 return (ret);
56} 62}
57 63
@@ -74,10 +80,11 @@ utctime_string_from_tm(struct tm *tm)
74 */ 80 */
75#define RFC5280 0 81#define RFC5280 0
76#define ATOI2(ar) ((ar) += 2, ((ar)[-2] - '0') * 10 + ((ar)[-1] - '0')) 82#define ATOI2(ar) ((ar) += 2, ((ar)[-2] - '0') * 10 + ((ar)[-1] - '0'))
77int asn1_time_parse(const char * bytes, size_t len, struct tm *tm, int mode) 83int
84asn1_time_parse(const char *bytes, size_t len, struct tm *tm, int mode)
78{ 85{
79 char *p, *buf = NULL, *dot = NULL, *tz = NULL; 86 char *p, *buf = NULL, *dot = NULL, *tz = NULL;
80 int i, offset, noseconds = 0, type = 0; 87 int i, offset = 0, noseconds = 0, type = 0, ret = -1;
81 struct tm ltm; 88 struct tm ltm;
82 struct tm *lt; 89 struct tm *lt;
83 size_t tlen; 90 size_t tlen;
@@ -89,12 +96,13 @@ int asn1_time_parse(const char * bytes, size_t len, struct tm *tm, int mode)
89 if (len > INT_MAX) 96 if (len > INT_MAX)
90 goto err; 97 goto err;
91 98
92 /* Constrain the RFC5280 case within max/min valid lengths. */ 99 /* Constrain the RFC5280 case within min/max valid lengths. */
93 if (mode == RFC5280 && (len > 15 || len < 13)) 100 if (mode == RFC5280 && (len < 13 || len > 15))
94 goto err; 101 goto err;
95 102
96 if ((buf = strndup(bytes, len)) == NULL) 103 if ((buf = strndup(bytes, len)) == NULL)
97 goto err; 104 goto err;
105
98 lt = tm; 106 lt = tm;
99 if (lt == NULL) { 107 if (lt == NULL) {
100 time_t t = time(NULL); 108 time_t t = time(NULL);
@@ -116,7 +124,7 @@ int asn1_time_parse(const char * bytes, size_t len, struct tm *tm, int mode)
116 dot = t; 124 dot = t;
117 continue; 125 continue;
118 } 126 }
119 if ((*t == 'Z' || *t == '+' || *t == '-') && tz == NULL) { 127 if ((*t == 'Z' || *t == '+' || *t == '-') && tz == NULL) {
120 tz = t; 128 tz = t;
121 continue; 129 continue;
122 } 130 }
@@ -134,21 +142,20 @@ int asn1_time_parse(const char * bytes, size_t len, struct tm *tm, int mode)
134 if (tzc == 'Z') { 142 if (tzc == 'Z') {
135 if (*tz != '\0') 143 if (*tz != '\0')
136 goto err; 144 goto err;
137 offset = 0;
138 } else if (mode != RFC5280 && (tzc == '+' || tzc == '-') && 145 } else if (mode != RFC5280 && (tzc == '+' || tzc == '-') &&
139 (strlen(tz) == 4)) { 146 strlen(tz) == 4) {
140 int hours, mins; 147 int hours = ATOI2(tz);
141 hours = ATOI2(tz); 148 int mins = ATOI2(tz);
142 mins = ATOI2(tz); 149
143 if (hours > 12 || mins > 59) 150 if (hours < 0 || hours > 12 || mins < 0 || mins > 59)
144 goto err; 151 goto err;
145 offset = hours * 3600 + mins * 60; 152 offset = hours * 3600 + mins * 60;
146 if (tzc == '-') 153 if (tzc == '-')
147 offset = -offset; 154 offset = -offset;
148 } else 155 } else
149 goto err; 156 goto err;
150 157
151 if (mode != RFC5280) { 158 if (offset != 0) {
152 /* XXX - yuck - OPENSSL_gmtime_adj should go away */ 159 /* XXX - yuck - OPENSSL_gmtime_adj should go away */
153 if (!OPENSSL_gmtime_adj(lt, 0, offset)) 160 if (!OPENSSL_gmtime_adj(lt, 0, offset))
154 goto err; 161 goto err;
@@ -175,10 +182,9 @@ int asn1_time_parse(const char * bytes, size_t len, struct tm *tm, int mode)
175 switch (tlen) { 182 switch (tlen) {
176 case 14: 183 case 14:
177 lt->tm_year = (ATOI2(p) * 100) - 1900; /* cc */ 184 lt->tm_year = (ATOI2(p) * 100) - 1900; /* cc */
178 if (mode == RFC5280 || mode == V_ASN1_GENERALIZEDTIME) 185 if (mode != RFC5280 && mode != V_ASN1_GENERALIZEDTIME)
179 type = V_ASN1_GENERALIZEDTIME;
180 else
181 goto err; 186 goto err;
187 type = V_ASN1_GENERALIZEDTIME;
182 /* FALLTHROUGH */ 188 /* FALLTHROUGH */
183 case 12: 189 case 12:
184 if (type == 0 && mode == V_ASN1_GENERALIZEDTIME) { 190 if (type == 0 && mode == V_ASN1_GENERALIZEDTIME) {
@@ -203,10 +209,9 @@ int asn1_time_parse(const char * bytes, size_t len, struct tm *tm, int mode)
203 if (mode == V_ASN1_GENERALIZEDTIME) 209 if (mode == V_ASN1_GENERALIZEDTIME)
204 goto err; 210 goto err;
205 if (tlen == 10) { 211 if (tlen == 10) {
206 if (mode == V_ASN1_UTCTIME) 212 if (mode != V_ASN1_UTCTIME)
207 noseconds = 1;
208 else
209 goto err; 213 goto err;
214 noseconds = 1;
210 } 215 }
211 type = V_ASN1_UTCTIME; 216 type = V_ASN1_UTCTIME;
212 } 217 }
@@ -215,25 +220,24 @@ int asn1_time_parse(const char * bytes, size_t len, struct tm *tm, int mode)
215 if (lt->tm_year < 50) 220 if (lt->tm_year < 50)
216 lt->tm_year += 100; 221 lt->tm_year += 100;
217 } 222 }
218 lt->tm_mon = ATOI2(p); /* mm */ 223 lt->tm_mon = ATOI2(p) - 1; /* mm */
219 if ((lt->tm_mon > 12) || !lt->tm_mon) 224 if (lt->tm_mon < 0 || lt->tm_mon > 11)
220 goto err; 225 goto err;
221 --lt->tm_mon; /* struct tm is 0 - 11 */
222 lt->tm_mday = ATOI2(p); /* dd */ 226 lt->tm_mday = ATOI2(p); /* dd */
223 if ((lt->tm_mday > 31) || !lt->tm_mday) 227 if (lt->tm_mday < 1 || lt->tm_mday > 31)
224 goto err; 228 goto err;
225 lt->tm_hour = ATOI2(p); /* HH */ 229 lt->tm_hour = ATOI2(p); /* HH */
226 if (lt->tm_hour > 23) 230 if (lt->tm_hour < 0 || lt->tm_hour > 23)
227 goto err; 231 goto err;
228 lt->tm_min = ATOI2(p); /* MM */ 232 lt->tm_min = ATOI2(p); /* MM */
229 if (lt->tm_min > 59) 233 if (lt->tm_hour < 0 || lt->tm_min > 59)
230 goto err; 234 goto err;
231 lt->tm_sec = 0; /* SS */ 235 lt->tm_sec = 0; /* SS */
232 if (noseconds) 236 if (noseconds)
233 break; 237 break;
234 lt->tm_sec = ATOI2(p); 238 lt->tm_sec = ATOI2(p);
235 /* Leap second 60 is not accepted. Reconsider later? */ 239 /* Leap second 60 is not accepted. Reconsider later? */
236 if (lt->tm_sec > 59) 240 if (lt->tm_hour < 0 || lt->tm_sec > 59)
237 goto err; 241 goto err;
238 break; 242 break;
239 default: 243 default:
@@ -241,17 +245,17 @@ int asn1_time_parse(const char * bytes, size_t len, struct tm *tm, int mode)
241 } 245 }
242 246
243 /* RFC 5280 section 4.1.2.5 */ 247 /* RFC 5280 section 4.1.2.5 */
244 if (mode == RFC5280 && lt->tm_year < 150 && 248 if (mode == RFC5280) {
245 type != V_ASN1_UTCTIME) 249 if (lt->tm_year < 150 && type != V_ASN1_UTCTIME)
246 goto err; 250 goto err;
247 if (mode == RFC5280 && lt->tm_year >= 150 && 251 if (lt->tm_year >= 150 && type != V_ASN1_GENERALIZEDTIME)
248 type != V_ASN1_GENERALIZEDTIME) 252 goto err;
249 goto err; 253 }
250 254
251 free(buf); 255 ret = type;
252 return type;
253 256
254err: 257err:
255 free(buf); 258 free(buf);
256 return -1; 259
260 return (ret);
257} 261}
diff --git a/src/lib/libssl/src/crypto/asn1/a_time_tm.c b/src/lib/libssl/src/crypto/asn1/a_time_tm.c
index 65f75c68cc..7b25e439c4 100644
--- a/src/lib/libssl/src/crypto/asn1/a_time_tm.c
+++ b/src/lib/libssl/src/crypto/asn1/a_time_tm.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: a_time_tm.c,v 1.1 2015/10/02 15:04:45 beck Exp $ */ 1/* $OpenBSD: a_time_tm.c,v 1.2 2015/10/04 15:15:11 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2015 Bob Beck <beck@openbsd.org> 3 * Copyright (c) 2015 Bob Beck <beck@openbsd.org>
4 * 4 *
@@ -32,13 +32,16 @@ gentime_string_from_tm(struct tm *tm)
32{ 32{
33 char *ret = NULL; 33 char *ret = NULL;
34 int year; 34 int year;
35
35 year = tm->tm_year + 1900; 36 year = tm->tm_year + 1900;
36 if (year < 0 || year > 9999) 37 if (year < 0 || year > 9999)
37 return (NULL); 38 return (NULL);
39
38 if (asprintf(&ret, "%04u%02u%02u%02u%02u%02uZ", year, 40 if (asprintf(&ret, "%04u%02u%02u%02u%02u%02uZ", year,
39 tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, 41 tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min,
40 tm->tm_sec) == -1) 42 tm->tm_sec) == -1)
41 ret = NULL; 43 ret = NULL;
44
42 return (ret); 45 return (ret);
43} 46}
44 47
@@ -46,12 +49,15 @@ char *
46utctime_string_from_tm(struct tm *tm) 49utctime_string_from_tm(struct tm *tm)
47{ 50{
48 char *ret = NULL; 51 char *ret = NULL;
52
49 if (tm->tm_year >= 150 || tm->tm_year < 50) 53 if (tm->tm_year >= 150 || tm->tm_year < 50)
50 return (NULL); 54 return (NULL);
55
51 if (asprintf(&ret, "%02u%02u%02u%02u%02u%02uZ", 56 if (asprintf(&ret, "%02u%02u%02u%02u%02u%02uZ",
52 tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday, 57 tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday,
53 tm->tm_hour, tm->tm_min, tm->tm_sec) == -1) 58 tm->tm_hour, tm->tm_min, tm->tm_sec) == -1)
54 ret = NULL; 59 ret = NULL;
60
55 return (ret); 61 return (ret);
56} 62}
57 63
@@ -74,10 +80,11 @@ utctime_string_from_tm(struct tm *tm)
74 */ 80 */
75#define RFC5280 0 81#define RFC5280 0
76#define ATOI2(ar) ((ar) += 2, ((ar)[-2] - '0') * 10 + ((ar)[-1] - '0')) 82#define ATOI2(ar) ((ar) += 2, ((ar)[-2] - '0') * 10 + ((ar)[-1] - '0'))
77int asn1_time_parse(const char * bytes, size_t len, struct tm *tm, int mode) 83int
84asn1_time_parse(const char *bytes, size_t len, struct tm *tm, int mode)
78{ 85{
79 char *p, *buf = NULL, *dot = NULL, *tz = NULL; 86 char *p, *buf = NULL, *dot = NULL, *tz = NULL;
80 int i, offset, noseconds = 0, type = 0; 87 int i, offset = 0, noseconds = 0, type = 0, ret = -1;
81 struct tm ltm; 88 struct tm ltm;
82 struct tm *lt; 89 struct tm *lt;
83 size_t tlen; 90 size_t tlen;
@@ -89,12 +96,13 @@ int asn1_time_parse(const char * bytes, size_t len, struct tm *tm, int mode)
89 if (len > INT_MAX) 96 if (len > INT_MAX)
90 goto err; 97 goto err;
91 98
92 /* Constrain the RFC5280 case within max/min valid lengths. */ 99 /* Constrain the RFC5280 case within min/max valid lengths. */
93 if (mode == RFC5280 && (len > 15 || len < 13)) 100 if (mode == RFC5280 && (len < 13 || len > 15))
94 goto err; 101 goto err;
95 102
96 if ((buf = strndup(bytes, len)) == NULL) 103 if ((buf = strndup(bytes, len)) == NULL)
97 goto err; 104 goto err;
105
98 lt = tm; 106 lt = tm;
99 if (lt == NULL) { 107 if (lt == NULL) {
100 time_t t = time(NULL); 108 time_t t = time(NULL);
@@ -116,7 +124,7 @@ int asn1_time_parse(const char * bytes, size_t len, struct tm *tm, int mode)
116 dot = t; 124 dot = t;
117 continue; 125 continue;
118 } 126 }
119 if ((*t == 'Z' || *t == '+' || *t == '-') && tz == NULL) { 127 if ((*t == 'Z' || *t == '+' || *t == '-') && tz == NULL) {
120 tz = t; 128 tz = t;
121 continue; 129 continue;
122 } 130 }
@@ -134,21 +142,20 @@ int asn1_time_parse(const char * bytes, size_t len, struct tm *tm, int mode)
134 if (tzc == 'Z') { 142 if (tzc == 'Z') {
135 if (*tz != '\0') 143 if (*tz != '\0')
136 goto err; 144 goto err;
137 offset = 0;
138 } else if (mode != RFC5280 && (tzc == '+' || tzc == '-') && 145 } else if (mode != RFC5280 && (tzc == '+' || tzc == '-') &&
139 (strlen(tz) == 4)) { 146 strlen(tz) == 4) {
140 int hours, mins; 147 int hours = ATOI2(tz);
141 hours = ATOI2(tz); 148 int mins = ATOI2(tz);
142 mins = ATOI2(tz); 149
143 if (hours > 12 || mins > 59) 150 if (hours < 0 || hours > 12 || mins < 0 || mins > 59)
144 goto err; 151 goto err;
145 offset = hours * 3600 + mins * 60; 152 offset = hours * 3600 + mins * 60;
146 if (tzc == '-') 153 if (tzc == '-')
147 offset = -offset; 154 offset = -offset;
148 } else 155 } else
149 goto err; 156 goto err;
150 157
151 if (mode != RFC5280) { 158 if (offset != 0) {
152 /* XXX - yuck - OPENSSL_gmtime_adj should go away */ 159 /* XXX - yuck - OPENSSL_gmtime_adj should go away */
153 if (!OPENSSL_gmtime_adj(lt, 0, offset)) 160 if (!OPENSSL_gmtime_adj(lt, 0, offset))
154 goto err; 161 goto err;
@@ -175,10 +182,9 @@ int asn1_time_parse(const char * bytes, size_t len, struct tm *tm, int mode)
175 switch (tlen) { 182 switch (tlen) {
176 case 14: 183 case 14:
177 lt->tm_year = (ATOI2(p) * 100) - 1900; /* cc */ 184 lt->tm_year = (ATOI2(p) * 100) - 1900; /* cc */
178 if (mode == RFC5280 || mode == V_ASN1_GENERALIZEDTIME) 185 if (mode != RFC5280 && mode != V_ASN1_GENERALIZEDTIME)
179 type = V_ASN1_GENERALIZEDTIME;
180 else
181 goto err; 186 goto err;
187 type = V_ASN1_GENERALIZEDTIME;
182 /* FALLTHROUGH */ 188 /* FALLTHROUGH */
183 case 12: 189 case 12:
184 if (type == 0 && mode == V_ASN1_GENERALIZEDTIME) { 190 if (type == 0 && mode == V_ASN1_GENERALIZEDTIME) {
@@ -203,10 +209,9 @@ int asn1_time_parse(const char * bytes, size_t len, struct tm *tm, int mode)
203 if (mode == V_ASN1_GENERALIZEDTIME) 209 if (mode == V_ASN1_GENERALIZEDTIME)
204 goto err; 210 goto err;
205 if (tlen == 10) { 211 if (tlen == 10) {
206 if (mode == V_ASN1_UTCTIME) 212 if (mode != V_ASN1_UTCTIME)
207 noseconds = 1;
208 else
209 goto err; 213 goto err;
214 noseconds = 1;
210 } 215 }
211 type = V_ASN1_UTCTIME; 216 type = V_ASN1_UTCTIME;
212 } 217 }
@@ -215,25 +220,24 @@ int asn1_time_parse(const char * bytes, size_t len, struct tm *tm, int mode)
215 if (lt->tm_year < 50) 220 if (lt->tm_year < 50)
216 lt->tm_year += 100; 221 lt->tm_year += 100;
217 } 222 }
218 lt->tm_mon = ATOI2(p); /* mm */ 223 lt->tm_mon = ATOI2(p) - 1; /* mm */
219 if ((lt->tm_mon > 12) || !lt->tm_mon) 224 if (lt->tm_mon < 0 || lt->tm_mon > 11)
220 goto err; 225 goto err;
221 --lt->tm_mon; /* struct tm is 0 - 11 */
222 lt->tm_mday = ATOI2(p); /* dd */ 226 lt->tm_mday = ATOI2(p); /* dd */
223 if ((lt->tm_mday > 31) || !lt->tm_mday) 227 if (lt->tm_mday < 1 || lt->tm_mday > 31)
224 goto err; 228 goto err;
225 lt->tm_hour = ATOI2(p); /* HH */ 229 lt->tm_hour = ATOI2(p); /* HH */
226 if (lt->tm_hour > 23) 230 if (lt->tm_hour < 0 || lt->tm_hour > 23)
227 goto err; 231 goto err;
228 lt->tm_min = ATOI2(p); /* MM */ 232 lt->tm_min = ATOI2(p); /* MM */
229 if (lt->tm_min > 59) 233 if (lt->tm_hour < 0 || lt->tm_min > 59)
230 goto err; 234 goto err;
231 lt->tm_sec = 0; /* SS */ 235 lt->tm_sec = 0; /* SS */
232 if (noseconds) 236 if (noseconds)
233 break; 237 break;
234 lt->tm_sec = ATOI2(p); 238 lt->tm_sec = ATOI2(p);
235 /* Leap second 60 is not accepted. Reconsider later? */ 239 /* Leap second 60 is not accepted. Reconsider later? */
236 if (lt->tm_sec > 59) 240 if (lt->tm_hour < 0 || lt->tm_sec > 59)
237 goto err; 241 goto err;
238 break; 242 break;
239 default: 243 default:
@@ -241,17 +245,17 @@ int asn1_time_parse(const char * bytes, size_t len, struct tm *tm, int mode)
241 } 245 }
242 246
243 /* RFC 5280 section 4.1.2.5 */ 247 /* RFC 5280 section 4.1.2.5 */
244 if (mode == RFC5280 && lt->tm_year < 150 && 248 if (mode == RFC5280) {
245 type != V_ASN1_UTCTIME) 249 if (lt->tm_year < 150 && type != V_ASN1_UTCTIME)
246 goto err; 250 goto err;
247 if (mode == RFC5280 && lt->tm_year >= 150 && 251 if (lt->tm_year >= 150 && type != V_ASN1_GENERALIZEDTIME)
248 type != V_ASN1_GENERALIZEDTIME) 252 goto err;
249 goto err; 253 }
250 254
251 free(buf); 255 ret = type;
252 return type;
253 256
254err: 257err:
255 free(buf); 258 free(buf);
256 return -1; 259
260 return (ret);
257} 261}