summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Yang <yang.yang@baishancloud.com>2017-07-30 20:14:58 -0400
committerRich Salz <rsalz@openssl.org>2017-07-30 20:14:58 -0400
commitf673c4f8f2a12bb8efcb0869646fdf0efd6bf30e (patch)
treedb79c9fd4f78c83b035e52626e3f2c8db8336211
parent27eb9f23e60e5ed15651011f56eaf04591120630 (diff)
Refactor ASN1_TIME_print functions
Check time string format before parsing Reduce more duplicated code By involving asn1_time_to_tm, we can now get information we mostly need to print a time string. This follows what was discussed at https://github.com/openssl/openssl/pull/4001#discussion_r129092251 Reviewed-by: Paul Dale <paul.dale@oracle.com> Reviewed-by: Rich Salz <rsalz@openssl.org> (Merged from https://github.com/openssl/openssl/pull/4039)
-rw-r--r--crypto/asn1/a_gentm.c53
-rw-r--r--crypto/asn1/a_time.c44
-rw-r--r--crypto/asn1/a_utctm.c42
3 files changed, 47 insertions, 92 deletions
diff --git a/crypto/asn1/a_gentm.c b/crypto/asn1/a_gentm.c
index 9336b20363..8547e226ba 100644
--- a/crypto/asn1/a_gentm.c
+++ b/crypto/asn1/a_gentm.c
@@ -108,56 +108,9 @@ ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
return NULL;
}
-static const char _asn1_mon[12][4] = {
- "Jan", "Feb", "Mar", "Apr", "May", "Jun",
- "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
-};
-
int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
{
- char *v;
- int gmt = 0;
- int i;
- int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0;
- char *f = NULL;
- int f_len = 0;
-
- i = tm->length;
- v = (char *)tm->data;
-
- if (i < 12)
- goto err;
- if (v[i - 1] == 'Z')
- gmt = 1;
- for (i = 0; i < 12; i++)
- if ((v[i] > '9') || (v[i] < '0'))
- goto err;
- y = (v[0] - '0') * 1000 + (v[1] - '0') * 100
- + (v[2] - '0') * 10 + (v[3] - '0');
- M = (v[4] - '0') * 10 + (v[5] - '0');
- if ((M > 12) || (M < 1))
- goto err;
- d = (v[6] - '0') * 10 + (v[7] - '0');
- h = (v[8] - '0') * 10 + (v[9] - '0');
- m = (v[10] - '0') * 10 + (v[11] - '0');
- if (tm->length >= 14 &&
- (v[12] >= '0') && (v[12] <= '9') &&
- (v[13] >= '0') && (v[13] <= '9')) {
- s = (v[12] - '0') * 10 + (v[13] - '0');
- /* Check for fractions of seconds. */
- if (tm->length >= 15 && v[14] == '.') {
- int l = tm->length;
- f = &v[14]; /* The decimal point. */
- f_len = 1;
- while (14 + f_len < l && f[f_len] >= '0' && f[f_len] <= '9')
- ++f_len;
- }
- }
-
- return BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s",
- _asn1_mon[M - 1], d, h, m, s, f_len, f, y,
- (gmt) ? " GMT" : "") > 0;
- err:
- BIO_write(bp, "Bad time value", 14);
- return 0;
+ if (tm->type != V_ASN1_GENERALIZEDTIME)
+ return 0;
+ return ASN1_TIME_print(bp, tm);
}
diff --git a/crypto/asn1/a_time.c b/crypto/asn1/a_time.c
index 83c57ce7a6..b29aabeb51 100644
--- a/crypto/asn1/a_time.c
+++ b/crypto/asn1/a_time.c
@@ -442,12 +442,50 @@ int ASN1_TIME_diff(int *pday, int *psec,
return OPENSSL_gmtime_diff(pday, psec, &tm_from, &tm_to);
}
+static const char _asn1_mon[12][4] = {
+ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
+};
+
int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
{
- if (tm->type == V_ASN1_UTCTIME)
- return ASN1_UTCTIME_print(bp, tm);
+ char *v;
+ char *f = NULL;
+ int f_len = 0, gmt = 0, l;
+ struct tm stm;
+
+ if (!asn1_time_to_tm(&stm, tm)) {
+ /* asn1_time_to_tm will check the time type */
+ goto err;
+ }
+
+ l = tm->length;
+ v = (char *)tm->data;
+ if (v[l - 1] == 'Z')
+ gmt = 1;
+ /*
+ * Try to parse fractional seconds. '14' is the place of
+ * 'fraction point' in a GeneralizedTime string.
+ */
+ if (tm->type == V_ASN1_GENERALIZEDTIME
+ && tm->length >= 15 && v[14] == '.') {
+ f = &v[14];
+ f_len = 1;
+ while (14 + f_len < l && f[f_len] >= '0' && f[f_len] <= '9')
+ ++f_len;
+ }
+
if (tm->type == V_ASN1_GENERALIZEDTIME)
- return ASN1_GENERALIZEDTIME_print(bp, tm);
+ return BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s",
+ _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
+ stm.tm_min, stm.tm_sec, f_len, f, stm.tm_year + 1900,
+ (gmt) ? " GMT" : "") > 0;
+ else
+ return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d%s",
+ _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
+ stm.tm_min, stm.tm_sec, stm.tm_year + 1900,
+ (gmt) ? " GMT" : "") > 0;
+ err:
BIO_write(bp, "Bad time value", 14);
return 0;
}
diff --git a/crypto/asn1/a_utctm.c b/crypto/asn1/a_utctm.c
index 668efa48bb..1f24508ffd 100644
--- a/crypto/asn1/a_utctm.c
+++ b/crypto/asn1/a_utctm.c
@@ -129,45 +129,9 @@ int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t)
return 0;
}
-static const char _asn1_mon[12][4] = {
- "Jan", "Feb", "Mar", "Apr", "May", "Jun",
- "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
-};
-
int ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm)
{
- const char *v;
- int gmt = 0;
- int i;
- int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0;
-
- i = tm->length;
- v = (const char *)tm->data;
-
- if (i < 10)
- goto err;
- if (v[i - 1] == 'Z')
- gmt = 1;
- for (i = 0; i < 10; i++)
- if ((v[i] > '9') || (v[i] < '0'))
- goto err;
- y = (v[0] - '0') * 10 + (v[1] - '0');
- if (y < 50)
- y += 100;
- M = (v[2] - '0') * 10 + (v[3] - '0');
- if ((M > 12) || (M < 1))
- goto err;
- d = (v[4] - '0') * 10 + (v[5] - '0');
- h = (v[6] - '0') * 10 + (v[7] - '0');
- m = (v[8] - '0') * 10 + (v[9] - '0');
- if (tm->length >= 12 &&
- (v[10] >= '0') && (v[10] <= '9') && (v[11] >= '0') && (v[11] <= '9'))
- s = (v[10] - '0') * 10 + (v[11] - '0');
-
- return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d%s",
- _asn1_mon[M - 1], d, h, m, s, y + 1900,
- (gmt) ? " GMT" : "") > 0;
- err:
- BIO_write(bp, "Bad time value", 14);
- return 0;
+ if (tm->type != V_ASN1_UTCTIME)
+ return 0;
+ return ASN1_TIME_print(bp, tm);
}