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; time_t 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) { if (verbose) fprintf (stderr, "string `%s', time-t %ld, revert: `%s'\n", array[idx].string, (long)val, tbuf); fail (idx); } if (strncmp (array[idx].string, tbuf, 15)) fail (idx); } } }
/* Send a GENKEY command to the SCdaemon. SERIALNO is not used in this implementation. If CREATEDATE has been given, it will be passed to SCDAEMON so that the key can be created with this timestamp; note the user needs to use the returned timestamp as old versions of scddaemon don't support this option. */ int agent_scd_genkey (struct agent_card_genkey_s *info, int keyno, int force, const char *serialno, u32 createtime, gcry_sexp_t *r_pubkey) { int rc; char line[ASSUAN_LINELENGTH]; gnupg_isotime_t tbuf; membuf_t data; unsigned char *buf; size_t len; (void)serialno; rc = start_agent (1); if (rc) return rc; if (createtime) epoch2isotime (tbuf, createtime); else *tbuf = 0; memset (info, 0, sizeof *info); snprintf (line, DIM(line)-1, "SCD GENKEY %s%s %s %d", *tbuf? "--timestamp=":"", tbuf, force? "--force":"", keyno); line[DIM(line)-1] = 0; memset (info, 0, sizeof *info); init_membuf_secure (&data, 1024); rc = assuan_transact (agent_ctx, line, membuf_data_cb, &data, default_inq_cb, NULL, scd_genkey_cb, info); status_sc_op_failure (rc); if (rc) { xfree (get_membuf (&data, &len)); } else { buf = get_membuf (&data, &len); if (!buf) return gpg_error_from_syserror (); rc = gcry_sexp_sscan (r_pubkey, NULL, buf, len); xfree (buf); } return rc; }
/* Return the current time in ISO format. */ static void get_current_isotime (my_isotime_t timebuf) { epoch2isotime (timebuf, time (NULL)); }