diff options
author | Werner Koch <wk@gnupg.org> | 2007-07-05 18:58:19 +0200 |
---|---|---|
committer | Werner Koch <wk@gnupg.org> | 2007-07-05 18:58:19 +0200 |
commit | 4631bc8ddf86b3917bf786c315273d8b1c7798e8 (patch) | |
tree | 2022343674f6703aefb41f2e142765ba319dbf5f /common | |
parent | 2007-07-05 Marcus Brinkmann <marcus@g10code.de> (diff) | |
download | gnupg2-4631bc8ddf86b3917bf786c315273d8b1c7798e8.tar.xz gnupg2-4631bc8ddf86b3917bf786c315273d8b1c7798e8.zip |
Fixed card key generation of gpg2.
Reveal less information about timings while generating a key.
Diffstat (limited to 'common')
-rw-r--r-- | common/ChangeLog | 5 | ||||
-rw-r--r-- | common/Makefile.am | 6 | ||||
-rw-r--r-- | common/gettime.c | 81 | ||||
-rw-r--r-- | common/t-gettime.c | 98 | ||||
-rw-r--r-- | common/util.h | 2 |
5 files changed, 186 insertions, 6 deletions
diff --git a/common/ChangeLog b/common/ChangeLog index cbdf9ffca..63289215e 100644 --- a/common/ChangeLog +++ b/common/ChangeLog @@ -1,3 +1,8 @@ +2007-07-05 Werner Koch <wk@g10code.com> + + * t-gettime.c: New. + * gettime.c (isotime2epoch, epoch2isotime): New. + 2007-07-04 Werner Koch <wk@g10code.com> * estream.c (es_init_do): Do not throw an error if pth as already diff --git a/common/Makefile.am b/common/Makefile.am index 3a064f316..08ead7996 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -81,13 +81,15 @@ libgpgrl_a_SOURCES = \ # # Module tests # -module_tests = t-convert +module_tests = t-convert t-gettime -t_common_ldadd = ../jnlib/libjnlib.a $(libcommon) ../gl/libgnu.a \ +t_common_ldadd = $(libcommon) ../jnlib/libjnlib.a ../gl/libgnu.a \ $(LIBGCRYPT_LIBS) $(GPG_ERROR_LIBS) $(LIBINTL) $(LIBICONV) t_convert_DEPENDENCIES = convert.c libcommon.a t_convert_LDADD = $(t_common_ldadd) +t_gettime_DEPENDENCIES = gettime.c libcommon.a +t_gettime_LDADD = $(t_common_ldadd) $(PROGRAMS): ../jnlib/libjnlib.a $(libcommon) ../gl/libgnu.a diff --git a/common/gettime.c b/common/gettime.c index b33f8aba5..56ff40eec 100644 --- a/common/gettime.c +++ b/common/gettime.c @@ -1,5 +1,5 @@ /* gettime.c - Wrapper for time functions - * Copyright (C) 1998, 2002 Free Software Foundation, Inc. + * Copyright (C) 1998, 2002, 2007 Free Software Foundation, Inc. * * This file is part of GnuPG. * @@ -20,6 +20,7 @@ #include <config.h> #include <stdlib.h> #include <time.h> +#include <ctype.h> #ifdef HAVE_LANGINFO_H #include <langinfo.h> #endif @@ -64,9 +65,9 @@ gnupg_get_isotime (gnupg_isotime_t timebuf) #else tp = gmtime (&atime); #endif - sprintf (timebuf,"%04d%02d%02dT%02d%02d%02d", - 1900 + tp->tm_year, tp->tm_mon+1, tp->tm_mday, - tp->tm_hour, tp->tm_min, tp->tm_sec); + snprintf (timebuf, 16, "%04d%02d%02dT%02d%02d%02d", + 1900 + tp->tm_year, tp->tm_mon+1, tp->tm_mday, + tp->tm_hour, tp->tm_min, tp->tm_sec); } } @@ -164,6 +165,78 @@ scan_isodatestr( const char *string ) return stamp; } +/* Scan am ISO timestamp and return a epoch based timestamp. The only + supported format is "yyyymmddThhmmss" delimited by white space, nul, a + colon or a comma. Returns (time_t)(-1) for an invalid string. */ +time_t +isotime2epoch (const char *string) +{ + const char *s; + int year, month, day, hour, minu, sec; + struct tm tmbuf; + int i; + + if (!*string) + return (time_t)(-1); + for (s=string, i=0; i < 8; i++, s++) + if (!digitp (s)) + return (time_t)(-1); + if (*s != 'T') + return (time_t)(-1); + for (s++, i=9; i < 15; i++, s++) + if (!digitp (s)) + return (time_t)(-1); + if ( !(!*s || (isascii (*s) && isspace(*s)) || *s == ':' || *s == ',')) + return (time_t)(-1); /* Wrong delimiter. */ + + year = atoi_4 (string); + month = atoi_2 (string + 4); + day = atoi_2 (string + 6); + hour = atoi_2 (string + 9); + minu = atoi_2 (string + 11); + sec = atoi_2 (string + 13); + + /* Basic checks. */ + if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 + || hour > 23 || minu > 59 || sec > 61 ) + return (time_t)(-1); + + memset (&tmbuf, 0, sizeof tmbuf); + tmbuf.tm_sec = sec; + tmbuf.tm_min = minu; + tmbuf.tm_hour = hour; + tmbuf.tm_mday = day; + tmbuf.tm_mon = month-1; + tmbuf.tm_year = year - 1900; + tmbuf.tm_isdst = -1; + return timegm (&tmbuf); +} + + +/* Convert an Epoch time to an iso time stamp. */ +void +epoch2isotime (gnupg_isotime_t timebuf, time_t atime) +{ + if (atime < 0) + *timebuf = 0; + else + { + struct tm *tp; +#ifdef HAVE_GMTIME_R + struct tm tmbuf; + + tp = gmtime_r (&atime, &tmbuf); +#else + tp = gmtime (&atime); +#endif + snprintf (timebuf, 16, "%04d%02d%02dT%02d%02d%02d", + 1900 + tp->tm_year, tp->tm_mon+1, tp->tm_mday, + tp->tm_hour, tp->tm_min, tp->tm_sec); + } +} + + + u32 add_days_to_timestamp( u32 stamp, u16 days ) diff --git a/common/t-gettime.c b/common/t-gettime.c new file mode 100644 index 000000000..495de37bd --- /dev/null +++ b/common/t-gettime.c @@ -0,0 +1,98 @@ +/* t-gettime.c - Module test for gettime.c + * Copyright (C) 2007 Free Software Foundation, Inc. + * + * This file is part of GnuPG. + * + * GnuPG is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GnuPG is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see <http://www.gnu.org/licenses/>. + */ + +#include <config.h> +#include <stdio.h> +#include <stdlib.h> + +#include "util.h" + +#define pass() do { ; } while(0) +#define fail(a) do { fprintf (stderr, "%s:%d: test %d failed\n",\ + __FILE__,__LINE__, (a)); \ + errcount++; \ + } while(0) + +static int verbose; +static int errcount; +#define INVALID ((time_t)(-1)) + + +static void +test_isotime2epoch (void) +{ + struct { const char *string; time_t expected; } array [] = { + { "19700101T000001", 1 }, + { "19700101T235959", 86399 }, + { "19980815T143712", 903191832 }, + { "19700101T000000", 0 }, + { "19691231T235959", INVALID }, + { "19000101T000000", INVALID }, + { "", INVALID }, + { "19000101T00000", INVALID }, + { "20010101t123456", INVALID }, + { "20010101T123456", 978352496 }, + { "20070629T160000", 1183132800 }, + { "20070629T160000:", 1183132800 }, + { "20070629T160000,", 1183132800 }, + { "20070629T160000 ", 1183132800 }, + { "20070629T160000\n", 1183132800 }, + { "20070629T160000.", INVALID }, + { NULL, 0 } + }; + int idx; + u32 val; + gnupg_isotime_t tbuf; + + for (idx=0; array[idx].string; idx++) + { + val = isotime2epoch (array[idx].string); + if (val != array[idx].expected ) + { + fail (idx); + if (verbose) + fprintf (stderr, "string `%s' exp: %ld got: %ld\n", + array[idx].string, (long)array[idx].expected, + (long)val); + } + if (array[idx].expected != INVALID) + { + epoch2isotime (tbuf, val); + if (strlen (tbuf) != 15) + fail (idx); + if (strncmp (array[idx].string, tbuf, 15)) + fail (idx); + } + } +} + + + + +int +main (int argc, char **argv) +{ + if (argc > 1 && !strcmp (argv[1], "--verbose")) + verbose = 1; + + test_isotime2epoch (); + + return !!errcount; +} + diff --git a/common/util.h b/common/util.h index cdd034206..3ec5200bf 100644 --- a/common/util.h +++ b/common/util.h @@ -103,6 +103,8 @@ void gnupg_set_time (time_t newtime, int freeze); int gnupg_faked_time_p (void); u32 make_timestamp (void); u32 scan_isodatestr (const char *string); +time_t isotime2epoch (const char *string); +void epoch2isotime (gnupg_isotime_t timebuf, time_t atime); u32 add_days_to_timestamp (u32 stamp, u16 days); const char *strtimevalue (u32 stamp); const char *strtimestamp (u32 stamp); /* GMT */ |