2016-05-17 18:51:34 +00:00
|
|
|
/*
|
2017-07-06 01:39:03 +00:00
|
|
|
* Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 10:52:47 +00:00
|
|
|
*
|
2016-05-17 18:51:34 +00: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 10:52:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2001-02-20 13:22:35 +00:00
|
|
|
#include <openssl/asn1.h>
|
2012-11-19 15:12:07 +00:00
|
|
|
#include "asn1_locl.h"
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2017-08-04 01:24:03 +00:00
|
|
|
/* This is the primary function used to parse ASN1_UTCTIME */
|
2012-11-19 15:12:07 +00:00
|
|
|
int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-07-10 19:01:24 +00:00
|
|
|
/* wrapper around ans1_time_to_tm */
|
2015-01-22 03:40:55 +00:00
|
|
|
if (d->type != V_ASN1_UTCTIME)
|
2017-07-06 01:39:03 +00:00
|
|
|
return 0;
|
2017-07-10 19:01:24 +00:00
|
|
|
return asn1_time_to_tm(tm, d);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2012-11-19 15:12:07 +00:00
|
|
|
|
|
|
|
int ASN1_UTCTIME_check(const ASN1_UTCTIME *d)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return asn1_utctime_to_tm(NULL, d);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2017-08-04 01:24:03 +00:00
|
|
|
/* Sets the string via simple copy without cleaning it up */
|
2004-03-15 23:15:26 +00:00
|
|
|
int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
ASN1_UTCTIME t;
|
|
|
|
|
|
|
|
t.type = V_ASN1_UTCTIME;
|
|
|
|
t.length = strlen(str);
|
|
|
|
t.data = (unsigned char *)str;
|
2017-06-11 20:36:07 +00:00
|
|
|
t.flags = 0;
|
|
|
|
|
2017-08-04 01:24:03 +00:00
|
|
|
if (!ASN1_UTCTIME_check(&t))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (s != NULL && !ASN1_STRING_copy(s, &t))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:56:39 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return ASN1_UTCTIME_adj(s, t, 0, 0);
|
|
|
|
}
|
2008-10-07 22:55:27 +00:00
|
|
|
|
|
|
|
ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
|
2015-01-22 03:40:55 +00:00
|
|
|
int offset_day, long offset_sec)
|
|
|
|
{
|
|
|
|
struct tm *ts;
|
|
|
|
struct tm data;
|
|
|
|
|
|
|
|
ts = OPENSSL_gmtime(&t, &data);
|
|
|
|
if (ts == NULL)
|
2017-08-04 01:24:03 +00:00
|
|
|
return NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
if (offset_day || offset_sec) {
|
|
|
|
if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
|
2017-08-04 01:24:03 +00:00
|
|
|
return NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 01:24:03 +00:00
|
|
|
return asn1_time_from_tm(s, ts, V_ASN1_UTCTIME);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2000-09-06 15:40:52 +00:00
|
|
|
|
|
|
|
int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
struct tm stm, ttm;
|
|
|
|
int day, sec;
|
|
|
|
|
|
|
|
if (!asn1_utctime_to_tm(&stm, s))
|
|
|
|
return -2;
|
|
|
|
|
2017-07-27 04:54:27 +00:00
|
|
|
if (OPENSSL_gmtime(&t, &ttm) == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
return -2;
|
|
|
|
|
2015-01-31 10:22:47 +00:00
|
|
|
if (!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm))
|
2015-01-22 03:40:55 +00:00
|
|
|
return -2;
|
|
|
|
|
2017-08-04 01:24:03 +00:00
|
|
|
if (day > 0 || sec > 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
return 1;
|
2017-08-04 01:24:03 +00:00
|
|
|
if (day < 0 || sec < 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
2015-09-22 15:05:33 +00:00
|
|
|
|
|
|
|
int ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm)
|
|
|
|
{
|
2017-07-31 00:14:58 +00:00
|
|
|
if (tm->type != V_ASN1_UTCTIME)
|
|
|
|
return 0;
|
|
|
|
return ASN1_TIME_print(bp, tm);
|
2015-09-22 15:05:33 +00:00
|
|
|
}
|