2016-05-18 02:51:34 +08:00
|
|
|
/*
|
2017-07-06 09:39:03 +08:00
|
|
|
* Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2016-05-18 02:51:34 +08:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 18:52:47 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
2001-02-20 21:22:35 +08:00
|
|
|
#include <openssl/asn1.h>
|
2012-11-19 23:12:07 +08:00
|
|
|
#include "asn1_locl.h"
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2012-11-19 23:12:07 +08:00
|
|
|
int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-07-11 03:01:24 +08:00
|
|
|
/* wrapper around ans1_time_to_tm */
|
2015-01-22 11:40:55 +08:00
|
|
|
if (d->type != V_ASN1_UTCTIME)
|
2017-07-06 09:39:03 +08:00
|
|
|
return 0;
|
2017-07-11 03:01:24 +08:00
|
|
|
return asn1_time_to_tm(tm, d);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2012-11-19 23:12:07 +08:00
|
|
|
|
|
|
|
int ASN1_UTCTIME_check(const ASN1_UTCTIME *d)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
return asn1_utctime_to_tm(NULL, d);
|
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2004-03-16 07:15:26 +08:00
|
|
|
int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
ASN1_UTCTIME t;
|
|
|
|
|
|
|
|
t.type = V_ASN1_UTCTIME;
|
|
|
|
t.length = strlen(str);
|
|
|
|
t.data = (unsigned char *)str;
|
2017-06-12 04:36:07 +08:00
|
|
|
t.flags = 0;
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (ASN1_UTCTIME_check(&t)) {
|
|
|
|
if (s != NULL) {
|
2016-08-06 23:54:32 +08:00
|
|
|
if (!ASN1_STRING_set((ASN1_STRING *)s, str, t.length))
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
s->type = V_ASN1_UTCTIME;
|
|
|
|
}
|
2017-07-06 09:39:03 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:56:39 +08:00
|
|
|
|
1999-04-20 05:31:43 +08:00
|
|
|
ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
return ASN1_UTCTIME_adj(s, t, 0, 0);
|
|
|
|
}
|
2008-10-08 06:55:27 +08:00
|
|
|
|
|
|
|
ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
|
2015-01-22 11:40:55 +08:00
|
|
|
int offset_day, long offset_sec)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
struct tm *ts;
|
|
|
|
struct tm data;
|
2017-07-06 09:39:03 +08:00
|
|
|
const size_t len = 20;
|
2015-01-22 11:40:55 +08:00
|
|
|
int free_s = 0;
|
|
|
|
|
|
|
|
if (s == NULL) {
|
2015-03-14 12:16:42 +08:00
|
|
|
s = ASN1_UTCTIME_new();
|
2015-03-24 19:52:24 +08:00
|
|
|
if (s == NULL)
|
|
|
|
goto err;
|
|
|
|
free_s = 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ts = OPENSSL_gmtime(&t, &data);
|
|
|
|
if (ts == NULL)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (offset_day || offset_sec) {
|
|
|
|
if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ts->tm_year < 50) || (ts->tm_year >= 150))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
p = (char *)s->data;
|
|
|
|
if ((p == NULL) || ((size_t)s->length < len)) {
|
|
|
|
p = OPENSSL_malloc(len);
|
|
|
|
if (p == NULL) {
|
|
|
|
ASN1err(ASN1_F_ASN1_UTCTIME_ADJ, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-05-01 22:02:07 +08:00
|
|
|
OPENSSL_free(s->data);
|
2015-01-22 11:40:55 +08:00
|
|
|
s->data = (unsigned char *)p;
|
|
|
|
}
|
|
|
|
|
2017-07-06 09:39:03 +08:00
|
|
|
s->length = BIO_snprintf(p, len, "%02d%02d%02d%02d%02d%02dZ",
|
|
|
|
ts->tm_year % 100, ts->tm_mon + 1, ts->tm_mday,
|
|
|
|
ts->tm_hour, ts->tm_min, ts->tm_sec);
|
2015-01-22 11:40:55 +08:00
|
|
|
s->type = V_ASN1_UTCTIME;
|
1999-06-05 05:35:58 +08:00
|
|
|
#ifdef CHARSET_EBCDIC_not
|
2015-01-22 11:40:55 +08:00
|
|
|
ebcdic2ascii(s->data, s->data, s->length);
|
1999-06-05 05:35:58 +08:00
|
|
|
#endif
|
2017-07-06 09:39:03 +08:00
|
|
|
return s;
|
2015-01-22 11:40:55 +08:00
|
|
|
err:
|
2015-03-24 19:52:24 +08:00
|
|
|
if (free_s)
|
2015-03-14 12:16:42 +08:00
|
|
|
ASN1_UTCTIME_free(s);
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-09-06 23:40:52 +08:00
|
|
|
|
|
|
|
int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
struct tm stm, ttm;
|
|
|
|
int day, sec;
|
|
|
|
|
|
|
|
if (!asn1_utctime_to_tm(&stm, s))
|
|
|
|
return -2;
|
|
|
|
|
2017-07-27 12:54:27 +08:00
|
|
|
if (OPENSSL_gmtime(&t, &ttm) == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
return -2;
|
|
|
|
|
2015-01-31 18:22:47 +08:00
|
|
|
if (!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm))
|
2015-01-22 11:40:55 +08:00
|
|
|
return -2;
|
|
|
|
|
|
|
|
if (day > 0)
|
|
|
|
return 1;
|
|
|
|
if (day < 0)
|
|
|
|
return -1;
|
|
|
|
if (sec > 0)
|
|
|
|
return 1;
|
|
|
|
if (sec < 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
2015-09-22 23:05:33 +08:00
|
|
|
|
2017-07-27 12:54:27 +08:00
|
|
|
static const char _asn1_mon[12][4] = {
|
|
|
|
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
|
|
|
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
|
|
|
};
|
|
|
|
|
2015-09-22 23:05:33 +08:00
|
|
|
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');
|
|
|
|
|
2017-07-27 12:54:27 +08:00
|
|
|
return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d%s",
|
|
|
|
_asn1_mon[M - 1], d, h, m, s, y + 1900,
|
|
|
|
(gmt) ? " GMT" : "") > 0;
|
2015-09-22 23:05:33 +08:00
|
|
|
err:
|
|
|
|
BIO_write(bp, "Bad time value", 14);
|
2017-07-06 09:39:03 +08:00
|
|
|
return 0;
|
2015-09-22 23:05:33 +08:00
|
|
|
}
|