libopensc: general usage method to allocate generalized time

This commit is contained in:
Viktor Tarasov 2013-12-29 22:00:12 +01:00
parent 13d1b8e9f2
commit 4d5b80fd3f
4 changed files with 57 additions and 56 deletions

View File

@ -2431,8 +2431,8 @@ sc_pkcs15_get_supported_algo(struct sc_pkcs15_card *p15card,
break;
if (ii < SC_MAX_SUPPORTED_ALGORITHMS && p15card->tokeninfo->supported_algos[ii].reference) {
info = &p15card->tokeninfo->supported_algos[ii];
sc_log(ctx, "found supported algorithm (ref:%X,mech:%X,ops:%X,algo_ref:%X)",
info = &p15card->tokeninfo->supported_algos[ii];
sc_log(ctx, "found supported algorithm (ref:%X,mech:%X,ops:%X,algo_ref:%X)",
info->reference, info->mechanism, info->operations, info->algo_ref);
}
@ -2440,6 +2440,43 @@ sc_pkcs15_get_supported_algo(struct sc_pkcs15_card *p15card,
}
int
sc_pkcs15_get_generalized_time(struct sc_context *ctx, char **out)
{
#ifdef HAVE_GETTIMEOFDAY
struct timeval tv;
#endif
struct tm *tm_time;
time_t t;
if (!ctx || !out)
return SC_ERROR_INVALID_ARGUMENTS;
*out = NULL;
#ifdef HAVE_GETTIMEOFDAY
gettimeofday(&tv, NULL);
t = tv.tv_sec;
#else
t = time(NULL);
#endif
tm_time = gmtime(&t);
if (!tm_time)
LOG_TEST_RET(ctx, SC_ERROR_INTERNAL, "gmtime failed");
*out = calloc(1, 16);
if (*out == NULL)
LOG_TEST_RET(ctx, SC_ERROR_OUT_OF_MEMORY, "memory failure");
/* print time in generalized time format */
if (!strftime(*out, 16, "%Y%m%d%H%M%SZ", tm_time)) {
free(*out);
LOG_TEST_RET(ctx, SC_ERROR_INTERNAL, "strftime failed");
}
return SC_SUCCESS;
}
int
sc_pkcs15_add_supported_algo_ref(struct sc_pkcs15_object *obj,
struct sc_supported_algo_info *algo)

View File

@ -969,6 +969,9 @@ int sc_pkcs15_convert_pubkey(struct sc_pkcs15_pubkey *key, void *evp_key);
/* Get 'LastUpdate' string */
char *sc_pkcs15_get_lastupdate(struct sc_pkcs15_card *p15card);
/* Allocate generalized time string */
int sc_pkcs15_get_generalized_time(struct sc_context *ctx, char **out);
/* New object search API.
* More complex, but also more powerful.
*/

View File

@ -281,8 +281,8 @@ typedef struct sc_apdu {
int cse; /* APDU case */
unsigned char cla, ins, p1, p2; /* CLA, INS, P1 and P2 bytes */
size_t lc, le; /* Lc and Le bytes */
const unsigned char *data; /* C-APDU data */
size_t datalen; /* length of data in C-APDU */
unsigned char *data; /* S-APDU data */
size_t datalen; /* length of data in S-APDU */
unsigned char *resp; /* R-APDU data buffer */
size_t resplen; /* in: size of R-APDU buffer,
* out: length of data returned in R-APDU */

View File

@ -2524,74 +2524,36 @@ sc_pkcs15init_update_dir(struct sc_pkcs15_card *p15card,
}
static char *
get_generalized_time(struct sc_context *ctx)
{
#ifdef HAVE_GETTIMEOFDAY
struct timeval tv;
#endif
struct tm *tm_time;
time_t t;
char* ret;
size_t r;
#ifdef HAVE_GETTIMEOFDAY
gettimeofday(&tv, NULL);
t = tv.tv_sec;
#else
t = time(NULL);
#endif
tm_time = gmtime(&t);
if (tm_time == NULL) {
sc_log(ctx, "error: gmtime failed");
return NULL;
}
ret = calloc(1, 16);
if (ret == NULL) {
sc_log(ctx, "error: calloc failed");
return NULL;
}
/* print time in generalized time format */
r = strftime(ret, 16, "%Y%m%d%H%M%SZ", tm_time);
if (r == 0) {
sc_log(ctx, "error: strftime failed");
free(ret);
return NULL;
}
return ret;
}
static int
sc_pkcs15init_update_tokeninfo(struct sc_pkcs15_card *p15card, struct sc_profile *profile)
{
struct sc_context *ctx = p15card->card->ctx;
struct sc_card *card = p15card->card;
struct sc_pkcs15_tokeninfo tokeninfo;
unsigned char *buf = NULL;
size_t size;
int r;
int rv;
LOG_FUNC_CALLED(ctx);
LOG_FUNC_CALLED(p15card->card->ctx);
/* set lastUpdate field */
if (p15card->tokeninfo->last_update.gtime != NULL)
free(p15card->tokeninfo->last_update.gtime);
p15card->tokeninfo->last_update.gtime = get_generalized_time(card->ctx);
if (p15card->tokeninfo->last_update.gtime == NULL)
return SC_ERROR_INTERNAL;
rv = sc_pkcs15_get_generalized_time(ctx, &p15card->tokeninfo->last_update.gtime);
LOG_TEST_RET(ctx, rv, "Cannot allocate generalized time string");
tokeninfo = *(p15card->tokeninfo);
if (profile->ops->emu_update_tokeninfo)
return profile->ops->emu_update_tokeninfo(profile, p15card, &tokeninfo);
r = sc_pkcs15_encode_tokeninfo(card->ctx, &tokeninfo, &buf, &size);
if (r >= 0)
r = sc_pkcs15init_update_file(profile, p15card, p15card->file_tokeninfo, buf, size);
rv = sc_pkcs15_encode_tokeninfo(ctx, &tokeninfo, &buf, &size);
if (rv >= 0)
rv = sc_pkcs15init_update_file(profile, p15card, p15card->file_tokeninfo, buf, size);
if (buf)
free(buf);
LOG_FUNC_RETURN(p15card->card->ctx, r);
LOG_FUNC_RETURN(ctx, rv);
}
@ -2617,9 +2579,8 @@ sc_pkcs15init_update_lastupdate(struct sc_pkcs15_card *p15card, struct sc_profil
/* update 'lastUpdate' file */
if (last_update->gtime != NULL)
free(last_update->gtime);
last_update->gtime = get_generalized_time(ctx);
if (last_update->gtime == NULL)
return SC_ERROR_INTERNAL;
r = sc_pkcs15_get_generalized_time(ctx, &last_update->gtime);
LOG_TEST_RET(ctx, r, "Cannot allocate generalized time string");
sc_copy_asn1_entry(c_asn1_last_update, asn1_last_update);
lupdate_len = strlen(last_update->gtime);