From c7ceb874c28c0cae77870629f492d61340384944 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Tue, 14 May 2002 16:51:00 +0000 Subject: [PATCH] sm/ * gpgsm.c: New option --faked-system-time * sign.c (gpgsm_sign): And use it here. * certpath.c (gpgsm_validate_path): Ditto. common/ * gettime.c: New. agent/ * cache.c (housekeeping, agent_put_cache): Use our time() wrapper. / * doc/: New * configure.ac, Makefile.am: Added doc/ --- agent/ChangeLog | 4 +++ agent/cache.c | 8 ++--- common/ChangeLog | 4 +++ common/Makefile.am | 3 +- common/gettime.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++ common/util.h | 7 ++++ sm/ChangeLog | 6 ++++ sm/certchain.c | 2 +- sm/certpath.c | 2 +- sm/gpgsm.c | 18 ++++++++-- sm/sign.c | 2 +- 11 files changed, 132 insertions(+), 11 deletions(-) create mode 100644 common/gettime.c diff --git a/agent/ChangeLog b/agent/ChangeLog index 721b0c712..fb358f43b 100644 --- a/agent/ChangeLog +++ b/agent/ChangeLog @@ -1,3 +1,7 @@ +2002-05-14 Werner Koch + + * cache.c (housekeeping, agent_put_cache): Use our time() wrapper. + 2002-04-26 Werner Koch * cache.c (agent_put_cache): Reinitialize the creation time and diff --git a/agent/cache.c b/agent/cache.c index 4819ad197..ed9c8cfd9 100644 --- a/agent/cache.c +++ b/agent/cache.c @@ -82,7 +82,7 @@ static void housekeeping (void) { ITEM r, rprev; - time_t current = time (NULL); + time_t current = gnupg_get_time (); /* first expire the actual data */ for (r=thecache; r; r = r->next) @@ -170,7 +170,7 @@ agent_put_cache (const char *key, const char *data, int ttl) } if (data) { - r->created = r->accessed = time (NULL); + r->created = r->accessed = gnupg_get_time (); r->ttl = ttl; r->pw = new_data (data, strlen (data)+1); if (!r->pw) @@ -185,7 +185,7 @@ agent_put_cache (const char *key, const char *data, int ttl) else { strcpy (r->key, key); - r->created = r->accessed = time (NULL); + r->created = r->accessed = gnupg_get_time (); r->ttl = ttl; r->pw = new_data (data, strlen (data)+1); if (!r->pw) @@ -223,7 +223,7 @@ agent_get_cache (const char *key) { /* put_cache does only put strings into the cache, so we don't need the lengths */ - r->accessed = time (NULL); + r->accessed = gnupg_get_time (); if (DBG_CACHE) log_debug ("... hit\n"); return r->pw->data; diff --git a/common/ChangeLog b/common/ChangeLog index a5092f58b..1e9c9d179 100644 --- a/common/ChangeLog +++ b/common/ChangeLog @@ -1,3 +1,7 @@ +2002-05-14 Werner Koch + + * gettime.c: New. + 2002-05-03 Werner Koch * errors.h: Added STARUS_EXPSIG and STATUS_EXPKEYSIG. diff --git a/common/Makefile.am b/common/Makefile.am index 4c3fc8947..85dcfe285 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -33,7 +33,8 @@ libcommon_a_SOURCES = \ maperror.c \ sysutils.c sysutils.h \ no-pth.c \ - cryptmiss.c + cryptmiss.c \ + gettime.c libcommon_a_LIBADD = @LIBOBJS@ diff --git a/common/gettime.c b/common/gettime.c new file mode 100644 index 000000000..6f656c8e4 --- /dev/null +++ b/common/gettime.c @@ -0,0 +1,87 @@ +/* gettime.c - Wrapper for time functions + * Copyright (C) 2002 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 2 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#include +#include +#include + +#include "util.h" + +static unsigned long timewarp; +static enum { NORMAL = 0, FROZEN, FUTURE, PAST } timemode; + +/* Wrapper for the time(3). We use this here so we can fake the time + for tests */ +time_t +gnupg_get_time () +{ + time_t current = time (NULL); + if (timemode == NORMAL) + return current; + else if (timemode == FROZEN) + return timewarp; + else if (timemode == FUTURE) + return current + timewarp; + else + return current - timewarp; +} + +/* set the time to NEWTIME so that gnupg_get_time returns a time + starting with this one. With FREEZE set to 1 the returned time + will never change. Just for completeness, a value of (time_t)-1 + for NEWTIME gets you back to rality. Note that this is obviously + not thread-safe but this is not required. */ +void +gnupg_set_time (time_t newtime, int freeze) +{ + time_t current = time (NULL); + + if ( newtime == (time_t)-1 || current == newtime) + { + timemode = NORMAL; + timewarp = 0; + } + else if (freeze) + { + timemode = FROZEN; + timewarp = current; + } + else if (newtime > current) + { + timemode = FUTURE; + timewarp = newtime - current; + } + else + { + timemode = PAST; + timewarp = current - newtime; + } +} + +/* Returns true when we are in timewarp mode */ +int +gnupg_faked_time_p (void) +{ + return timemode; +} + + + + diff --git a/common/util.h b/common/util.h index ff20bfe15..72c847e99 100644 --- a/common/util.h +++ b/common/util.h @@ -22,6 +22,7 @@ #define GNUPG_COMMON_UTIL_H #include /* we need this for the memory function protos */ +#include /* we need time_t */ /* to pass hash functions to libksba we need to cast it */ #define HASH_FNC ((void (*)(void *, const void*,size_t))gcry_md_write) @@ -55,6 +56,12 @@ int map_kbx_err (int err); int map_assuan_err (int err); int map_to_assuan_status (int rc); +/*-- gettime.c --*/ +time_t gnupg_get_time (void); +void gnupg_set_time (time_t newtime, int freeze); +int gnupg_faked_time_p (void); + + /*-- replacement functions from funcname.c --*/ #if !HAVE_VASPRINTF #include diff --git a/sm/ChangeLog b/sm/ChangeLog index 7ab2f9a1f..25b88d915 100644 --- a/sm/ChangeLog +++ b/sm/ChangeLog @@ -1,3 +1,9 @@ +2002-05-14 Werner Koch + + * gpgsm.c: New option --faked-system-time + * sign.c (gpgsm_sign): And use it here. + * certpath.c (gpgsm_validate_path): Ditto. + 2002-05-03 Werner Koch * certpath.c (gpgsm_validate_path): Added EXPTIME arg and changed diff --git a/sm/certchain.c b/sm/certchain.c index 1283ce46d..28c0be52f 100644 --- a/sm/certchain.c +++ b/sm/certchain.c @@ -315,7 +315,7 @@ gpgsm_validate_path (KsbaCert cert, time_t *r_exptime) char *subject = NULL; KEYDB_HANDLE kh = keydb_new (0); KsbaCert subject_cert = NULL, issuer_cert = NULL; - time_t current_time = time (NULL); + time_t current_time = gnupg_get_time (); time_t exptime = 0; if (r_exptime) diff --git a/sm/certpath.c b/sm/certpath.c index 1283ce46d..28c0be52f 100644 --- a/sm/certpath.c +++ b/sm/certpath.c @@ -315,7 +315,7 @@ gpgsm_validate_path (KsbaCert cert, time_t *r_exptime) char *subject = NULL; KEYDB_HANDLE kh = keydb_new (0); KsbaCert subject_cert = NULL, issuer_cert = NULL; - time_t current_time = time (NULL); + time_t current_time = gnupg_get_time (); time_t exptime = 0; if (r_exptime) diff --git a/sm/gpgsm.c b/sm/gpgsm.c index cb2c3bdf7..b5b144afe 100644 --- a/sm/gpgsm.c +++ b/sm/gpgsm.c @@ -92,8 +92,7 @@ enum cmd_and_opt_values { oLCmessages, oDirmngrProgram, - - + oFakedSystemTime, oAssumeArmor, @@ -111,7 +110,7 @@ enum cmd_and_opt_values { oDisablePolicyChecks, oEnablePolicyChecks, - + oTextmode, oFingerprint, @@ -353,6 +352,8 @@ static ARGPARSE_OPTS opts[] = { { oLCctype, "lc-ctype", 2, "@" }, { oLCmessages, "lc-messages", 2, "@" }, { oDirmngrProgram, "dirmngr-program", 2 , "@" }, + { oFakedSystemTime, "faked-system-time", 4, "@" }, /* (epoch time) */ + { oNoBatch, "no-batch", 0, "@" }, { oWithColons, "with-colons", 0, "@"}, @@ -861,6 +862,10 @@ main ( int argc, char **argv) case oLCmessages: opt.lc_messages = xstrdup (pargs.r.ret_str); break; case oDirmngrProgram: opt.dirmngr_program = pargs.r.ret_str; break; + case oFakedSystemTime: + gnupg_set_time ( (time_t)pargs.r.ret_ulong, 0); + break; + case oNoDefKeyring: default_keyring = 0; break; case oNoGreeting: nogreeting = 1; break; @@ -975,6 +980,13 @@ main ( int argc, char **argv) if (may_coredump && !opt.quiet) log_info (_("WARNING: program may create a core file!\n")); + + if (gnupg_faked_time_p ()) + { + log_info (_("WARNING: running with faked system time: ")); + gpgsm_dump_time (gnupg_get_time ()); + log_printf ("\n"); + } /*FIXME if (opt.batch) */ /* tty_batchmode (1); */ diff --git a/sm/sign.c b/sm/sign.c index 2781baa84..46fa170ba 100644 --- a/sm/sign.c +++ b/sm/sign.c @@ -393,7 +393,7 @@ gpgsm_sign (CTRL ctrl, int data_fd, int detached, FILE *out_fp) } } - signed_at = time (NULL); + signed_at = gnupg_get_time (); err = ksba_cms_set_signing_time (cms, signer, signed_at); if (err) {