diff --git a/src/libopensc/card-openpgp.c b/src/libopensc/card-openpgp.c index 956605ad..ab37bb5f 100644 --- a/src/libopensc/card-openpgp.c +++ b/src/libopensc/card-openpgp.c @@ -2265,6 +2265,7 @@ pgp_store_creationtime(sc_card_t *card, u8 key_id, time_t *outtime) const size_t timestrlen = 64; char timestring[65]; u8 buf[4]; + struct tm tm; LOG_FUNC_CALLED(card->ctx); @@ -2278,7 +2279,14 @@ pgp_store_creationtime(sc_card_t *card, u8 key_id, time_t *outtime) /* set output */ *outtime = createtime = time(NULL); - strftime(timestring, timestrlen, "%c %Z", gmtime(&createtime)); +#ifdef _WIN32 + if (0 != gmtime_s(&tm, &createtime)) + LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL); +#else + if (NULL == gmtime_r(&createtime, &tm)) + LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL); +#endif + strftime(timestring, timestrlen, "%c %Z", &tm); sc_log(card->ctx, "Creation time %s.", timestring); /* Code borrowed from GnuPG */ ulong2bebytes(buf, (unsigned long)createtime); diff --git a/src/libopensc/pkcs15.c b/src/libopensc/pkcs15.c index 31e97cb1..8c4dc7ae 100644 --- a/src/libopensc/pkcs15.c +++ b/src/libopensc/pkcs15.c @@ -2605,7 +2605,7 @@ sc_pkcs15_get_generalized_time(struct sc_context *ctx, char **out) #ifdef HAVE_GETTIMEOFDAY struct timeval tv; #endif - struct tm *tm_time; + struct tm tm; time_t t; if (!ctx || !out) @@ -2618,16 +2618,21 @@ sc_pkcs15_get_generalized_time(struct sc_context *ctx, char **out) #else t = time(NULL); #endif - tm_time = gmtime(&t); - if (!tm_time) - LOG_TEST_RET(ctx, SC_ERROR_INTERNAL, "gmtime failed"); + +#ifdef _WIN32 + if (0 != gmtime_s(&tm, &t)) + LOG_FUNC_RETURN(ctx, SC_ERROR_INTERNAL); +#else + if (NULL == gmtime_r(&t, &tm)) + LOG_FUNC_RETURN(ctx, SC_ERROR_INTERNAL); +#endif *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)) { + if (!strftime(*out, 16, "%Y%m%d%H%M%SZ", &tm)) { free(*out); LOG_TEST_RET(ctx, SC_ERROR_INTERNAL, "strftime failed"); } diff --git a/src/tools/openpgp-tool.c b/src/tools/openpgp-tool.c index eb071c44..25174ee2 100644 --- a/src/tools/openpgp-tool.c +++ b/src/tools/openpgp-tool.c @@ -240,11 +240,17 @@ static char *prettify_date(u8 *data, size_t length) { if (data != NULL && length == 4) { time_t time = (time_t) (data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]); - struct tm *tp; + struct tm tm; static char result[64]; /* large enough */ - tp = gmtime(&time); - strftime(result, sizeof(result), "%Y-%m-%d %H:%M:%S", tp); +#ifdef _WIN32 + if (0 != gmtime_s(&tm, &time)) + return NULL; +#else + if (NULL == gmtime_r(&time, &tm)) + return NULL; +#endif + strftime(result, sizeof(result), "%Y-%m-%d %H:%M:%S", &tm); return result; } return NULL;