Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major changes for all cards that needs developer review #850

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a78bc32
Set PIN-PUK association for cards that don't have it set
maciejsszmigiero Aug 13, 2016
ac2412d
Print size_t variables on properly on Windows
maciejsszmigiero Sep 28, 2016
5ce4937
Make minidriver installer custom action library optional
maciejsszmigiero Aug 25, 2016
2ae5f93
Make minidriver buildable again on mingw
maciejsszmigiero Jan 25, 2017
3f49d9b
Fix most of warnings shown when building on Linux and mingw
maciejsszmigiero Jan 25, 2017
435a4a0
Move SM test in configure.ac after LIB_PRE and DYN_LIB_EXT assignment
maciejsszmigiero Oct 22, 2016
31c63c2
Add session handle uniqueness check to PKCS#11 C_OpenSession()
maciejsszmigiero Aug 25, 2016
b83a806
Add multiple PINs support to minidriver
maciejsszmigiero Aug 25, 2016
480b442
Add reset operation to opensc-tool
maciejsszmigiero Aug 23, 2016
db5e03c
Provide notification about and handle card resets by other contexts
maciejsszmigiero Jan 28, 2017
8e4941b
Add ptrdiff_t (pointer difference) printf length modifier
maciejsszmigiero Aug 23, 2016
1b56dba
Remove logprintf() mingw hack in minidriver
maciejsszmigiero Aug 23, 2016
2a9e175
Support PIN unblocking in minidriver via PUK as response to challenge
maciejsszmigiero Aug 25, 2016
f2df771
Keep track of card resets by other contexts in minidriver
maciejsszmigiero Jan 28, 2017
4678706
Add GCC format checking attributes to log functions
maciejsszmigiero Aug 24, 2016
ee6d7f2
Fix log messages format and parameter issues flagged by GCC
maciejsszmigiero Jan 1, 2017
35efa57
Fix cases of log function format strings not being a string literal
maciejsszmigiero Aug 26, 2016
ee8f1cc
Use built-in formatted output functions on mingw
maciejsszmigiero Jan 28, 2017
6f2bbf5
Add GCC format checking attributes to minidriver logging function
maciejsszmigiero Sep 30, 2016
00478c8
Fix minidriver log messages format and parameter issues flagged by GCC
maciejsszmigiero Oct 1, 2016
5128d0d
Minidriver CardGetChallenge() parameters are output only
maciejsszmigiero Oct 1, 2016
5deedf3
Minidriver CardReadFile() parameters are optional
maciejsszmigiero Oct 1, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use built-in formatted output functions on mingw
Mingw currently links to msvcrt.dll as C runtime.
This library is documented by Microsoft as off-limits to applications and
its feature set vary between Windows versions.

Due to this, presence of particular printf() format string directives
depends on which Windows version the code is run.

This is, naturally, bad, so mingw developers introduced ability to replace
formatted output functions with built-in equivalents with defined feature
set by setting "__USE_MINGW_ANSI_STDIO" macro to 1.
There are, however, no built-in equivalents for "_s" suffixed functions.
Fortunately, they are used only a few times in minidriver so let's simply
replace them with equivalent code using standard functions.

This also allows skipping "MINGW_HAS_SECURE_API" macro definition so any
future uses will be caught by compiler.

Signed-off-by: Maciej S. Szmigiero <[email protected]>
  • Loading branch information
maciejsszmigiero committed Jan 28, 2017
commit ee8f1cc803118da8d0e9769bb1d72bcb56e25264
6 changes: 6 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ AC_DEFINE_UNQUOTED([DEBUG_FILE], ["${DEBUG_FILE}"], [Debug file])
AC_DEFINE_UNQUOTED([PROFILE_DIR], ["${PROFILE_DIR}"], [Directory of profiles])
AC_DEFINE_UNQUOTED([PROFILE_DIR_DEFAULT], ["${PROFILE_DIR_DEFAULT}"], [Default directory of profiles])

case "${host}" in
*-mingw*)
CPPFLAGS="${CPPFLAGS} -D__USE_MINGW_ANSI_STDIO=1"
;;
esac

AC_ARG_ENABLE(
[strict],
[AS_HELP_STRING([--disable-strict],[disable strict compile mode @<:@disabled@:>@])],
Expand Down
14 changes: 10 additions & 4 deletions src/libopensc/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,22 @@ enum {
#endif

#if defined(__GNUC__)
#if defined(__MINGW32__) && defined (__MINGW_PRINTF_FORMAT)
#define SC_PRINTF_FORMAT __MINGW_PRINTF_FORMAT
#else
#define SC_PRINTF_FORMAT printf
#endif

/* GCC can check format and param correctness for us */
void sc_do_log(struct sc_context *ctx, int level, const char *file, int line,
const char *func, const char *format, ...)
__attribute__ ((format (printf, 6, 7)));
__attribute__ ((format (SC_PRINTF_FORMAT, 6, 7)));
void sc_do_log_noframe(sc_context_t *ctx, int level, const char *format,
va_list args) __attribute__ ((format (printf, 3, 0)));
va_list args) __attribute__ ((format (SC_PRINTF_FORMAT, 3, 0)));
void _sc_debug(struct sc_context *ctx, int level, const char *format, ...)
__attribute__ ((format (printf, 3, 4)));
__attribute__ ((format (SC_PRINTF_FORMAT, 3, 4)));
void _sc_log(struct sc_context *ctx, const char *format, ...)
__attribute__ ((format (printf, 2, 3)));
__attribute__ ((format (SC_PRINTF_FORMAT, 2, 3)));
#else
void sc_do_log(struct sc_context *ctx, int level, const char *file, int line, const char *func,
const char *format, ...);
Expand Down
2 changes: 1 addition & 1 deletion src/libopensc/opensc.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extern "C" {
#include "libopensc/sm.h"
#endif

#if defined(_WIN32)
#if defined(_WIN32) && !(defined(__MINGW32__) && defined (__MINGW_PRINTF_FORMAT))
#define SC_FORMAT_LEN_SIZE_T "I"
#define SC_FORMAT_LEN_PTRDIFF_T "I"
#else
Expand Down
50 changes: 29 additions & 21 deletions src/minidriver/minidriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@
#pragma managed(push, off)
#endif

#define MINGW_HAS_SECURE_API 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <windows.h>

#include "common/compat_strlcpy.h"
#include "common/simclist.h"
#include "libopensc/asn1.h"
#include "libopensc/cardctl.h"
Expand Down Expand Up @@ -266,6 +265,10 @@ static DWORD md_pkcs15_delete_object(PCARD_DATA pCardData, struct sc_pkcs15_obje
static DWORD md_fs_init(PCARD_DATA pCardData);
static void md_fs_finalize(PCARD_DATA pCardData);

#if defined(_MSC_VER) && _MSC_VER < 1900
#define snprintf _snprintf
#endif

static void logprintf(PCARD_DATA pCardData, int level, _Printf_format_string_ const char* format, ...)
{
va_list arg;
Expand Down Expand Up @@ -322,7 +325,8 @@ static void loghex(PCARD_DATA pCardData, int level, PBYTE data, size_t len)
memset(line, 0, sizeof(line));

while(i < len) {
sprintf_s(c, sizeof(line)-(size_t)(c-line),"%02X", *p);
snprintf(c, sizeof(line)-(size_t)(c-line),"%02X", *p);
line[sizeof(line) - 1] = 0;
p++;
c += 2;
i++;
Expand Down Expand Up @@ -959,9 +963,11 @@ static VOID md_generate_guid( __in_ecount(MAX_CONTAINER_NAME_LEN+1) PSTR szGuid)
RPC_CSTR szRPCGuid = NULL;
GUID Label = {0};
UuidCreate(&Label);
UuidToStringA(&Label, &szRPCGuid);
strncpy_s(szGuid, MAX_CONTAINER_NAME_LEN+1, (PSTR) szRPCGuid, MAX_CONTAINER_NAME_LEN);
if (szRPCGuid) RpcStringFreeA(&szRPCGuid);
if (UuidToStringA(&Label, &szRPCGuid) == RPC_S_OK && szRPCGuid) {
strlcpy(szGuid, (PSTR) szRPCGuid, MAX_CONTAINER_NAME_LEN+1);
RpcStringFreeA(&szRPCGuid);
} else
szGuid[0] = 0;
}

static DWORD
Expand Down Expand Up @@ -1002,8 +1008,8 @@ md_contguid_add_conversion(PCARD_DATA pCardData, struct sc_pkcs15_object *prkey,

for (i = 0; i < MD_MAX_CONVERSIONS; i++) {
if (md_static_conversions[i].szWindowsGuid[0] == 0) {
strcpy_s(md_static_conversions[i].szWindowsGuid, MAX_CONTAINER_NAME_LEN+1, szWindowsGuid);
strcpy_s(md_static_conversions[i].szOpenSCGuid, MAX_CONTAINER_NAME_LEN+1, szOpenSCGuid);
strlcpy(md_static_conversions[i].szWindowsGuid, szWindowsGuid, MAX_CONTAINER_NAME_LEN+1);
strlcpy(md_static_conversions[i].szOpenSCGuid, szOpenSCGuid, MAX_CONTAINER_NAME_LEN+1);
logprintf(pCardData, 0, "md_contguid_add_conversion(): Registering conversion '%s' '%s'\n", szWindowsGuid, szOpenSCGuid);
return SCARD_S_SUCCESS;;
}
Expand Down Expand Up @@ -1058,7 +1064,7 @@ md_contguid_build_cont_guid_from_key(PCARD_DATA pCardData, struct sc_pkcs15_obje
memcpy(szGuid, prkey_info->id.value, prkey_info->id.len);
szGuid[prkey_info->id.len] = 0;
} else if (md_is_guid_as_label(pCardData) && key_obj->label[0] != 0) {
strncpy_s(szGuid, MAX_CONTAINER_NAME_LEN+1, key_obj->label, MAX_CONTAINER_NAME_LEN);
strlcpy(szGuid, key_obj->label, MAX_CONTAINER_NAME_LEN+1);
} else {
dwret = md_contguid_get_guid_from_card(pCardData, key_obj, szGuid);
}
Expand Down Expand Up @@ -1150,7 +1156,7 @@ md_fs_add_directory(PCARD_DATA pCardData, struct md_directory **head, char *name
return SCARD_E_NO_MEMORY;
memset(new_dir, 0, sizeof(struct md_directory));

strncpy_s((char *)new_dir->name, sizeof(new_dir->name), name, sizeof(new_dir->name) - 1);
strlcpy((char *)new_dir->name, name, sizeof(new_dir->name));
new_dir->acl = acl;

if (*head == NULL) {
Expand Down Expand Up @@ -1225,7 +1231,7 @@ md_fs_add_file(PCARD_DATA pCardData, struct md_file **head, char *name, CARD_FIL
return SCARD_E_NO_MEMORY;
memset(new_file, 0, sizeof(struct md_file));

strncpy_s((char *)new_file->name, sizeof(new_file->name), name, sizeof(new_file->name) - 1);
strlcpy((char *)new_file->name, name, sizeof(new_file->name));
new_file->size = size;
new_file->acl = acl;

Expand Down Expand Up @@ -1329,9 +1335,9 @@ md_fs_delete_file(PCARD_DATA pCardData, char *parent, char *name)
if (!strcmp(parent, "mscp")) {
int idx = -1;

if(sscanf_s(name, "ksc%d", &idx) > 0) {
if(sscanf(name, "ksc%d", &idx) > 0) {
}
else if(sscanf_s(name, "kxc%d", &idx) > 0) {
else if(sscanf(name, "kxc%d", &idx) > 0) {
}

if (idx >= 0 && idx < MD_MAX_KEY_CONTAINERS) {
Expand Down Expand Up @@ -1417,7 +1423,7 @@ md_pkcs15_update_containers(PCARD_DATA pCardData, unsigned char *blob, size_t si
memset(cont, 0, sizeof(CONTAINER_MAP_RECORD));
}
else {
strcpy_s(cont->guid,MAX_CONTAINER_NAME_LEN+1, szGuid);
strlcpy(cont->guid, szGuid, MAX_CONTAINER_NAME_LEN+1);
cont->index = idx;
cont->flags = pp->bFlags;
cont->size_sign = pp->wSigKeySizeBits;
Expand Down Expand Up @@ -1674,9 +1680,9 @@ md_fs_read_content(PCARD_DATA pCardData, char *parent, struct md_file *file)
if (!strcmp((char *)dir->name, "mscp")) {
int idx, rv;

if(sscanf_s((char *)file->name, "ksc%d", &idx) > 0) {
if(sscanf((char *)file->name, "ksc%d", &idx) > 0) {
}
else if(sscanf_s((char *)file->name, "kxc%d", &idx) > 0) {
else if(sscanf((char *)file->name, "kxc%d", &idx) > 0) {
}
else {
idx = -1;
Expand Down Expand Up @@ -2194,14 +2200,16 @@ md_set_cmapfile(PCARD_DATA pCardData, struct md_file *file)
char k_name[6];

if (vs->p15_containers[ii].size_key_exchange) {
_snprintf_s((char *)k_name, sizeof(k_name), sizeof(k_name)-1, "kxc%02i", ii);
snprintf(k_name, sizeof(k_name), "kxc%02i", ii);
k_name[sizeof(k_name) - 1] = 0;
dwret = md_fs_add_file(pCardData, &(file->next), k_name, file->acl, NULL, 0, NULL);
if (dwret != SCARD_S_SUCCESS)
return dwret;
}

if (vs->p15_containers[ii].size_sign) {
_snprintf_s((char *)k_name, sizeof(k_name), sizeof(k_name)-1, "ksc%02i", ii);
snprintf(k_name, sizeof(k_name), "ksc%02i", ii);
k_name[sizeof(k_name) - 1] = 0;
dwret = md_fs_add_file(pCardData, &(file->next), k_name, file->acl, NULL, 0, NULL);
if (dwret != SCARD_S_SUCCESS)
return dwret;
Expand Down Expand Up @@ -2793,8 +2801,8 @@ md_pkcs15_store_certificate(PCARD_DATA pCardData, char *file_name, unsigned char

/* use container's ID as ID of certificate to store */
idx = -1;
if(sscanf_s(file_name, "ksc%d", &idx) > 0) {
} else if(sscanf_s(file_name, "kxc%d", &idx) > 0) {
if(sscanf(file_name, "ksc%d", &idx) > 0) {
} else if(sscanf(file_name, "kxc%d", &idx) > 0) {
}

if (idx >= 0 && idx < MD_MAX_KEY_CONTAINERS) {
Expand Down Expand Up @@ -4054,7 +4062,7 @@ DWORD WINAPI CardEnumFiles(__in PCARD_DATA pCardData,
file = dir->files;
for (offs = 0; file != NULL && offs < sizeof(mstr) - 10;) {
logprintf(pCardData, 2, "enum files(): file name '%s'\n", file->name);
strcpy_s(mstr+offs, sizeof(mstr) - offs, (char *)file->name);
strlcpy(mstr+offs, (char *)file->name, sizeof(mstr) - offs);
offs += strlen((char *)file->name) + 1;
file = file->next;
}
Expand Down