summaryrefslogtreecommitdiffstats
path: root/crypto/asn1/a_time.c
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 /crypto/asn1/a_time.c
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)
Diffstat (limited to 'crypto/asn1/a_time.c')
-rw-r--r--crypto/asn1/a_time.c44
1 files changed, 41 insertions, 3 deletions
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;
}