Time difference functions.
authorDr. Stephen Henson <steve@openssl.org>
Mon, 19 Nov 2012 15:12:07 +0000 (15:12 +0000)
committerDr. Stephen Henson <steve@openssl.org>
Mon, 19 Aug 2013 20:55:07 +0000 (21:55 +0100)
Backport of ASN1_TIME_diff and OPENSSL_gmtime_diff functions from master
branch.

CHANGES
crypto/asn1/a_gentm.c
crypto/asn1/a_time.c
crypto/asn1/a_utctm.c
crypto/asn1/asn1.h
crypto/asn1/asn1_locl.h
crypto/asn1/x_crl.c
crypto/o_time.c
crypto/o_time.h

diff --git a/CHANGES b/CHANGES
index b77336fe64a7dc0402d6cdd4bdab22b21c71cf40..2f0fd51b46f39dc3973505cc1817b3cbbc437502 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -4,6 +4,10 @@
 
  Changes between 1.0.1 and 1.0.2 [xx XXX xxxx]
 
+  *) New functions OPENSSL_gmtime_diff and ASN1_TIME_diff to find the
+     difference in days and seconds between two tm or ASN1_TIME structures.
+     [Steve Henson]
+
   *) Add -rev test option to s_server to just reverse order of characters
      received by client and send back to server. Also prints an abbreviated
      summary of the connection parameters.
index c79c6f538cdccc97f1c5ff3157d4102e6f59e8ba..6450b6a0c08630179927ba9576c9c9f64fc89522 100644 (file)
@@ -63,6 +63,7 @@
 #include "cryptlib.h"
 #include "o_time.h"
 #include <openssl/asn1.h>
+#include "asn1_locl.h"
 
 #if 0
 
@@ -115,7 +116,7 @@ err:
 
 #endif
 
-int ASN1_GENERALIZEDTIME_check(ASN1_GENERALIZEDTIME *d)
+int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d)
        {
        static const int min[9]={ 0, 0, 1, 1, 0, 0, 0, 0, 0};
        static const int max[9]={99, 99,12,31,23,59,59,12,59};
@@ -135,7 +136,12 @@ int ASN1_GENERALIZEDTIME_check(ASN1_GENERALIZEDTIME *d)
                {
                if ((i == 6) && ((a[o] == 'Z') ||
                        (a[o] == '+') || (a[o] == '-')))
-                       { i++; break; }
+                       {
+                       i++;
+                       if (tm)
+                               tm->tm_sec = 0;
+                       break;
+                       }
                if ((a[o] < '0') || (a[o] > '9')) goto err;
                n= a[o]-'0';
                if (++o > l) goto err;
@@ -145,6 +151,33 @@ int ASN1_GENERALIZEDTIME_check(ASN1_GENERALIZEDTIME *d)
                if (++o > l) goto err;
 
                if ((n < min[i]) || (n > max[i])) goto err;
+               if (tm)
+                       {
+                       switch(i)
+                               {
+                       case 0:
+                               tm->tm_year = n * 100 - 1900;
+                               break;
+                       case 1:
+                               tm->tm_year += n;
+                               break;
+                       case 2:
+                               tm->tm_mon = n - 1;
+                               break;
+                       case 3:
+                               tm->tm_mday = n;
+                               break;
+                       case 4:
+                               tm->tm_hour = n;
+                               break;
+                       case 5:
+                               tm->tm_min = n;
+                               break;
+                       case 6:
+                               tm->tm_sec = n;
+                               break;
+                               }
+                       }
                }
        /* Optional fractional seconds: decimal point followed by one
         * or more digits.
@@ -163,6 +196,7 @@ int ASN1_GENERALIZEDTIME_check(ASN1_GENERALIZEDTIME *d)
                o++;
        else if ((a[o] == '+') || (a[o] == '-'))
                {
+               int offsign = a[o] == '-' ? -1 : 1, offset = 0;
                o++;
                if (o+4 > l) goto err;
                for (i=7; i<9; i++)
@@ -173,10 +207,19 @@ int ASN1_GENERALIZEDTIME_check(ASN1_GENERALIZEDTIME *d)
                        if ((a[o] < '0') || (a[o] > '9')) goto err;
                        n=(n*10)+ a[o]-'0';
                        if ((n < min[i]) || (n > max[i])) goto err;
+                       if (tm)
+                               {
+                               if (i == 7)
+                                       offset = n * 3600;
+                               else if (i == 8)
+                                       offset += n * 60;
+                               }
                        o++;
                        }
+               if (offset && !OPENSSL_gmtime_adj(tm, 0, offset * offsign))
+                       return 0;
                }
-       else
+       else if (a[o])
                {
                /* Missing time zone information. */
                goto err;
@@ -186,6 +229,11 @@ err:
        return(0);
        }
 
+int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d)
+       {
+       return asn1_generalizedtime_to_tm(NULL, d);
+       }
+
 int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str)
        {
        ASN1_GENERALIZEDTIME t;
index 1cf886757355b46e528b23d56f9c98b368f0859f..d7925bf80ab53bafcd6c0513c6e3980c4fc57c93 100644 (file)
@@ -66,6 +66,7 @@
 #include "cryptlib.h"
 #include "o_time.h"
 #include <openssl/asn1t.h>
+#include "asn1_locl.h"
 
 IMPLEMENT_ASN1_MSTRING(ASN1_TIME, B_ASN1_TIME)
 
@@ -197,64 +198,32 @@ int ASN1_TIME_set_string(ASN1_TIME *s, const char *str)
        return 1;
        }
 
-#if 0
-static int asn1_time_to_tm(struct tm *tm, const ASN1_TIME *s)
+static int asn1_time_to_tm(struct tm *tm, const ASN1_TIME *t)
        {
-       const unsigned char *p;
-
-       if (!ASN1_TIME_check(s))
-               return 0;
-
-       memset(tm, 0 ,sizeof tm);
-       p = s->data;
-
-#define g2(p) (((p)[0] - '0') * 10 + ((p)[1] - '0'))
-       if (s->type == V_ASN1_GENERALIZEDTIME)
-               {
-               int yr = g2(p) * 100 + g2(p + 2);
-               if (yr < 1900)
-                       return 0;
-               tm->tm_year = yr - 1900;
-               p += 4;
-               }
-       else
-               {
-               tm->tm_year=g2(p);
-               if(tm->tm_year < 50)
-                       tm->tm_year+=100;
-               p += 2;
-               }
-       tm->tm_mon=g2(p)-1;
-       tm->tm_mday=g2(p + 2);
-       tm->tm_hour=g2(p + 4);
-       tm->tm_min=g2(p + 6);
-       p += 8;
-       /* Seconds optional in UTCTime */
-       if (s->type == V_ASN1_GENERALIZEDTIME || (*p >= '0' && *p <= '9'))
-               {
-               tm->tm_sec=g2(p);
-               p += 2;
-               }
-       else
-               tm->tm_sec = 0;
-       if (s->type == V_ASN1_GENERALIZEDTIME)
+       if (t == NULL)
                {
-               /* Skip any fractional seconds */
-               if (*p == '.')
-                       {
-                       p++;
-                       while (*p >= '0' && *p <= '9')
-                               p++;
-                       }
-               }
-       /* Timezone */
-       if(*p != 'Z')
-               {
-               int off_sec = g2(p + 1) * 3600 + g2(p + 3) * 60;
-               if(*p == '-')
-                       off_sec = -off_sec;
-               OPENSSL_gmtime_adj(tm, 0, off_sec);
+               time_t now_t;
+               time(&now_t);
+               if (OPENSSL_gmtime(&now_t, tm))
+                       return 1;
+               return 0;
                }
-       return 1;
+               
+       if (t->type == V_ASN1_UTCTIME)
+               return asn1_utctime_to_tm(tm, t);
+       else if (t->type == V_ASN1_GENERALIZEDTIME)
+               return asn1_generalizedtime_to_tm(tm, t);
+
+       return 0;
        }
-#endif
+
+int ASN1_TIME_diff(int *pday, int *psec,
+                       const ASN1_TIME *from, const ASN1_TIME *to)
+       {
+       struct tm tm_from, tm_to;
+       if (!asn1_time_to_tm(&tm_from, from))
+               return 0;
+       if (!asn1_time_to_tm(&tm_to, to))
+               return 0;
+       return OPENSSL_gmtime_diff(pday, psec, &tm_from, &tm_to);
+       }       
index 072e23659233479ce32b756276276638d95466a6..d0d7be62a322191a3e87f5a9406bc1148212e802 100644 (file)
@@ -61,6 +61,7 @@
 #include "cryptlib.h"
 #include "o_time.h"
 #include <openssl/asn1.h>
+#include "asn1_locl.h"
 
 #if 0
 int i2d_ASN1_UTCTIME(ASN1_UTCTIME *a, unsigned char **pp)
@@ -112,7 +113,7 @@ err:
 
 #endif
 
-int ASN1_UTCTIME_check(ASN1_UTCTIME *d)
+int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d)
        {
        static const int min[8]={ 0, 1, 1, 0, 0, 0, 0, 0};
        static const int max[8]={99,12,31,23,59,59,12,59};
@@ -129,7 +130,12 @@ int ASN1_UTCTIME_check(ASN1_UTCTIME *d)
                {
                if ((i == 5) && ((a[o] == 'Z') ||
                        (a[o] == '+') || (a[o] == '-')))
-                       { i++; break; }
+                       {
+                       i++;
+                       if (tm)
+                               tm->tm_sec = 0;
+                       break;
+                       }
                if ((a[o] < '0') || (a[o] > '9')) goto err;
                n= a[o]-'0';
                if (++o > l) goto err;
@@ -139,11 +145,36 @@ int ASN1_UTCTIME_check(ASN1_UTCTIME *d)
                if (++o > l) goto err;
 
                if ((n < min[i]) || (n > max[i])) goto err;
+               if (tm)
+                       {
+                       switch(i)
+                               {
+                       case 0:
+                               tm->tm_year = n < 50 ? n + 100 : n;
+                               break;
+                       case 1:
+                               tm->tm_mon = n - 1;
+                               break;
+                       case 2:
+                               tm->tm_mday = n;
+                               break;
+                       case 3:
+                               tm->tm_hour = n;
+                               break;
+                       case 4:
+                               tm->tm_min = n;
+                               break;
+                       case 5:
+                               tm->tm_sec = n;
+                               break;
+                               }
+                       }
                }
        if (a[o] == 'Z')
                o++;
        else if ((a[o] == '+') || (a[o] == '-'))
                {
+               int offsign = a[o] == '-' ? -1 : 1, offset = 0;
                o++;
                if (o+4 > l) goto err;
                for (i=6; i<8; i++)
@@ -154,12 +185,26 @@ int ASN1_UTCTIME_check(ASN1_UTCTIME *d)
                        if ((a[o] < '0') || (a[o] > '9')) goto err;
                        n=(n*10)+ a[o]-'0';
                        if ((n < min[i]) || (n > max[i])) goto err;
+                       if (tm)
+                               {
+                               if (i == 6)
+                                       offset = n * 3600;
+                               else if (i == 7)
+                                       offset += n * 60;
+                               }
                        o++;
                        }
+               if (offset && !OPENSSL_gmtime_adj(tm, 0, offset * offsign))
+                       return 0;
                }
-       return(o == l);
+       return o == l;
 err:
-       return(0);
+       return 0;
+       }
+
+int ASN1_UTCTIME_check(const ASN1_UTCTIME *d)
+       {
+       return asn1_utctime_to_tm(NULL, d);
        }
 
 int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str)
@@ -242,39 +287,26 @@ ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
 
 int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t)
        {
-       struct tm *tm;
-       struct tm data;
-       int offset;
-       int year;
-
-#define g2(p) (((p)[0]-'0')*10+(p)[1]-'0')
-
-       if (s->data[12] == 'Z')
-               offset=0;
-       else
-               {
-               offset = g2(s->data+13)*60+g2(s->data+15);
-               if (s->data[12] == '-')
-                       offset = -offset;
-               }
-
-       t -= offset*60; /* FIXME: may overflow in extreme cases */
-
-       tm = OPENSSL_gmtime(&t, &data);
-       
-#define return_cmp(a,b) if ((a)<(b)) return -1; else if ((a)>(b)) return 1
-       year = g2(s->data);
-       if (year < 50)
-               year += 100;
-       return_cmp(year,              tm->tm_year);
-       return_cmp(g2(s->data+2) - 1, tm->tm_mon);
-       return_cmp(g2(s->data+4),     tm->tm_mday);
-       return_cmp(g2(s->data+6),     tm->tm_hour);
-       return_cmp(g2(s->data+8),     tm->tm_min);
-       return_cmp(g2(s->data+10),    tm->tm_sec);
-#undef g2
-#undef return_cmp
-
+       struct tm stm, ttm;
+       int day, sec;
+
+       if (!asn1_utctime_to_tm(&stm, s))
+               return -2;
+
+       if (!OPENSSL_gmtime(&t, &ttm))
+               return -2;
+
+       if (!OPENSSL_gmtime_diff(&day, &sec, &stm, &ttm))
+               return -2;
+
+       if (day > 0)
+               return 1;
+       if (day < 0)
+               return -1;
+       if (sec > 0)
+               return 1;
+       if (sec < 0)
+               return -1;
        return 0;
        }
 
index 220a0c8c637e12e5aa96ffd403616b182280580a..88bc79d8fa72e0172c96765178a6a262187d918b 100644 (file)
@@ -839,7 +839,7 @@ int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y);
 
 DECLARE_ASN1_FUNCTIONS(ASN1_ENUMERATED)
 
-int ASN1_UTCTIME_check(ASN1_UTCTIME *a);
+int ASN1_UTCTIME_check(const ASN1_UTCTIME *a);
 ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s,time_t t);
 ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
                                int offset_day, long offset_sec);
@@ -849,11 +849,13 @@ int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t);
 time_t ASN1_UTCTIME_get(const ASN1_UTCTIME *s);
 #endif
 
-int ASN1_GENERALIZEDTIME_check(ASN1_GENERALIZEDTIME *a);
+int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *a);
 ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,time_t t);
 ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
             time_t t, int offset_day, long offset_sec);
 int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str);
+int ASN1_TIME_diff(int *pday, int *psec,
+                       const ASN1_TIME *from, const ASN1_TIME *to);
 
 DECLARE_ASN1_FUNCTIONS(ASN1_OCTET_STRING)
 ASN1_OCTET_STRING *    ASN1_OCTET_STRING_dup(const ASN1_OCTET_STRING *a);
index 9fcf0d9530ff9a82dc008d45b537fda2c9ead109..5452f2ac62e24d1128cebd289e1dac01cdc30688 100644 (file)
@@ -58,6 +58,9 @@
 
 /* Internal ASN1 structures and functions: not for application use */
 
+int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d);
+int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d);
+
 /* ASN1 print context structure */
 
 struct asn1_pctx_st
index e1ea9f3154e127268112d2f691ee5f4d65c08184..a4dfee8473880123c0c38b7b61b4386ae586fb63 100644 (file)
@@ -58,8 +58,8 @@
 
 #include <stdio.h>
 #include "cryptlib.h"
-#include "asn1_locl.h"
 #include <openssl/asn1t.h>
+#include "asn1_locl.h"
 #include <openssl/x509.h>
 #include <openssl/x509v3.h>
 
index 9030fdef7aed055c2113c63c8359f267d6d5f112..b78f5ca6304b19f6032e09bc28234a275a5bf5e7 100644 (file)
@@ -234,9 +234,76 @@ struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)
 
 static long date_to_julian(int y, int m, int d);
 static void julian_to_date(long jd, int *y, int *m, int *d);
+static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
+               long *pday, int *psec);
 
 int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
        {
+       int time_sec, time_year, time_month, time_day;
+       long time_jd;
+
+       /* Convert time and offset into julian day and seconds */
+       if (!julian_adj(tm, off_day, offset_sec, &time_jd, &time_sec))
+               return 0;
+
+       /* Convert Julian day back to date */
+
+       julian_to_date(time_jd, &time_year, &time_month, &time_day);
+
+       if (time_year < 1900 || time_year > 9999)
+               return 0;
+
+       /* Update tm structure */
+
+       tm->tm_year = time_year - 1900;
+       tm->tm_mon = time_month - 1;
+       tm->tm_mday = time_day;
+
+       tm->tm_hour = time_sec / 3600;
+       tm->tm_min = (time_sec / 60) % 60;
+       tm->tm_sec = time_sec % 60;
+
+       return 1;
+               
+}
+
+int OPENSSL_gmtime_diff(int *pday, int *psec,
+                       const struct tm *from, const struct tm *to)
+       {
+       int from_sec, to_sec, diff_sec;
+       long from_jd, to_jd, diff_day;
+       if (!julian_adj(from, 0, 0, &from_jd, &from_sec))
+               return 0;
+       if (!julian_adj(to, 0, 0, &to_jd, &to_sec))
+               return 0;
+       diff_day = to_jd - from_jd;
+       diff_sec = to_sec - from_sec;
+       /* Adjust differences so both positive or both negative */
+       if (diff_day > 0 && diff_sec < 0)
+               {
+               diff_day--;
+               diff_sec += SECS_PER_DAY;
+               }
+       if (diff_day < 0 && diff_sec > 0)
+               {
+               diff_day++;
+               diff_sec -= SECS_PER_DAY;
+               }
+
+       if (pday)
+               *pday = (int)diff_day;
+       if (psec)
+               *psec = diff_sec;
+
+       return 1;
+
+       }
+       
+       
+/* Convert tm structure and offset into julian day and seconds */
+static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
+               long *pday, int *psec)
+       {
        int offset_hms, offset_day;
        long time_jd;
        int time_year, time_month, time_day;
@@ -274,26 +341,11 @@ int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
        if (time_jd < 0)
                return 0;
 
-       /* Convert Julian day back to date */
-
-       julian_to_date(time_jd, &time_year, &time_month, &time_day);
-
-       if (time_year < 1900 || time_year > 9999)
-               return 0;
-
-       /* Update tm structure */
-
-       tm->tm_year = time_year - 1900;
-       tm->tm_mon = time_month - 1;
-       tm->tm_mday = time_day;
-
-       tm->tm_hour = offset_hms / 3600;
-       tm->tm_min = (offset_hms / 60) % 60;
-       tm->tm_sec = offset_hms % 60;
-
+       *pday = time_jd;
+       *psec = offset_hms;
        return 1;
-               
-}
+       }
+
 
 /* Convert date to and from julian day
  * Uses Fliegel & Van Flandern algorithm
@@ -345,28 +397,42 @@ int main(int argc, char **argv)
 
 int check_time(long offset)
        {
-       struct tm tm1, tm2;
+       struct tm tm1, tm2, o1;
+       int off_day, off_sec;
+       long toffset;
        time_t t1, t2;
        time(&t1);
        t2 = t1 + offset;
        OPENSSL_gmtime(&t2, &tm2);
        OPENSSL_gmtime(&t1, &tm1);
+       o1 = tm1;
        OPENSSL_gmtime_adj(&tm1, 0, offset);
-       if ((tm1.tm_year == tm2.tm_year) &&
-           (tm1.tm_mon == tm2.tm_mon) &&
-           (tm1.tm_mday == tm2.tm_mday) &&
-           (tm1.tm_hour == tm2.tm_hour) &&
-           (tm1.tm_min == tm2.tm_min) &&
-           (tm1.tm_sec == tm2.tm_sec))
-               return 1;
-       fprintf(stderr, "TIME ERROR!!\n");
-       fprintf(stderr, "Time1: %d/%d/%d, %d:%02d:%02d\n",
-                       tm2.tm_mday, tm2.tm_mon + 1, tm2.tm_year + 1900,
-                       tm2.tm_hour, tm2.tm_min, tm2.tm_sec);
-       fprintf(stderr, "Time2: %d/%d/%d, %d:%02d:%02d\n",
-                       tm1.tm_mday, tm1.tm_mon + 1, tm1.tm_year + 1900,
-                       tm1.tm_hour, tm1.tm_min, tm1.tm_sec);
-       return 0;
+       if ((tm1.tm_year != tm2.tm_year) ||
+           (tm1.tm_mon != tm2.tm_mon) ||
+           (tm1.tm_mday != tm2.tm_mday) ||
+           (tm1.tm_hour != tm2.tm_hour) ||
+           (tm1.tm_min != tm2.tm_min) ||
+           (tm1.tm_sec != tm2.tm_sec))
+               {
+               fprintf(stderr, "TIME ERROR!!\n");
+               fprintf(stderr, "Time1: %d/%d/%d, %d:%02d:%02d\n",
+                               tm2.tm_mday, tm2.tm_mon + 1, tm2.tm_year + 1900,
+                               tm2.tm_hour, tm2.tm_min, tm2.tm_sec);
+               fprintf(stderr, "Time2: %d/%d/%d, %d:%02d:%02d\n",
+                               tm1.tm_mday, tm1.tm_mon + 1, tm1.tm_year + 1900,
+                               tm1.tm_hour, tm1.tm_min, tm1.tm_sec);
+               return 0;
+               }
+       OPENSSL_gmtime_diff(&o1, &tm1, &off_day, &off_sec);
+       toffset = (long)off_day * SECS_PER_DAY + off_sec;
+       if (offset != toffset)
+               {
+               fprintf(stderr, "TIME OFFSET ERROR!!\n");
+               fprintf(stderr, "Expected %ld, Got %ld (%d:%d)\n",
+                                       offset, toffset, off_day, off_sec);
+               return 0;
+               }
+       return 1;
        }
 
 #endif
index e391da75087e9b11264fb0bd229bf8549e808a18..fcf8c230e182971a467b6e2496aee7fc2f6f1579 100644 (file)
@@ -63,5 +63,7 @@
 
 struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result);
 int OPENSSL_gmtime_adj(struct tm *tm, int offset_day, long offset_sec);
+int OPENSSL_gmtime_diff(int *pday, int *psec,
+                       const struct tm *from, const struct tm *to);
 
 #endif