diff --git a/src/libopensc/card.c b/src/libopensc/card.c index 6bd0ef2f..aeba79ac 100644 --- a/src/libopensc/card.c +++ b/src/libopensc/card.c @@ -1363,11 +1363,13 @@ sc_card_sm_load(struct sc_card *card, const char *module_path, const char *in_mo #ifdef _WIN32 if (!module_path || strlen(module_path) == 0) { - temp_len = PATH_MAX; + temp_len = PATH_MAX-1; rv = sc_ctx_win32_get_config_value(NULL, "SmDir", "Software\\OpenSC Project\\OpenSC", temp_path, &temp_len); - if (rv == SC_SUCCESS) + if (rv == SC_SUCCESS) { + temp_path[temp_len] = '\0'; module_path = temp_path; + } } expanded_len = PATH_MAX; expanded_len = ExpandEnvironmentStringsA(module_path, expanded_val, expanded_len); diff --git a/src/libopensc/ctx.c b/src/libopensc/ctx.c index 6aefae7a..46dcce49 100644 --- a/src/libopensc/ctx.c +++ b/src/libopensc/ctx.c @@ -167,12 +167,9 @@ struct _sc_ctx_options { int sc_ctx_win32_get_config_value(const char *name_env, const char *name_reg, const char *name_key, - char *out, size_t *out_len) + void *out, size_t *out_len) { #ifdef _WIN32 - char temp[PATH_MAX + 1]; - char *value = NULL; - DWORD temp_len = PATH_MAX; long rc; HKEY hKey; @@ -180,9 +177,12 @@ sc_ctx_win32_get_config_value(const char *name_env, return SC_ERROR_INVALID_ARGUMENTS; if (name_env) { - value = getenv(name_env); - if (value) - goto done; + char *value = value = getenv(name_env); + if (strlen(value) < *out_len) + return SC_ERROR_NOT_ENOUGH_MEMORY; + memcpy(out, value, strlen(value)); + *out_len = strlen(value); + return SC_SUCCESS; } if (!name_reg) @@ -193,36 +193,26 @@ sc_ctx_win32_get_config_value(const char *name_env, rc = RegOpenKeyExA(HKEY_CURRENT_USER, name_key, 0, KEY_QUERY_VALUE, &hKey); if (rc == ERROR_SUCCESS) { - temp_len = PATH_MAX; - rc = RegQueryValueEx( hKey, name_reg, NULL, NULL, (LPBYTE) temp, &temp_len); - if ((rc == ERROR_SUCCESS) && (temp_len < PATH_MAX)) - value = temp; + DWORD len = *out_len; + rc = RegQueryValueEx(hKey, name_reg, NULL, NULL, out, &len); RegCloseKey(hKey); - } - - if (!value) { - rc = RegOpenKeyExA( HKEY_LOCAL_MACHINE, name_key, 0, KEY_QUERY_VALUE, &hKey ); if (rc == ERROR_SUCCESS) { - temp_len = PATH_MAX; - rc = RegQueryValueEx( hKey, name_reg, NULL, NULL, (LPBYTE) temp, &temp_len); - if ((rc == ERROR_SUCCESS) && (temp_len < PATH_MAX)) - value = temp; - RegCloseKey(hKey); + *out_len = len; + return SC_SUCCESS; } } -done: - if (value) { - if (strlen(value) >= *out_len) - return SC_ERROR_BUFFER_TOO_SMALL; - strcpy(out, value); - *out_len = strlen(out); - return SC_SUCCESS; + rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE, name_key, 0, KEY_QUERY_VALUE, &hKey); + if (rc == ERROR_SUCCESS) { + DWORD len = *out_len; + rc = RegQueryValueEx(hKey, name_reg, NULL, NULL, out, &len); + RegCloseKey(hKey); + if (rc == ERROR_SUCCESS) { + *out_len = len; + return SC_SUCCESS; + } } - memset(out, 0, *out_len); - *out_len = 0; - return SC_ERROR_OBJECT_NOT_FOUND; #else return SC_ERROR_NOT_SUPPORTED; @@ -677,13 +667,14 @@ static void process_config_file(sc_context_t *ctx, struct _sc_ctx_options *opts) memset(ctx->conf_blocks, 0, sizeof(ctx->conf_blocks)); #ifdef _WIN32 - temp_len = PATH_MAX; + temp_len = PATH_MAX-1; r = sc_ctx_win32_get_config_value("OPENSC_CONF", "ConfigFile", "Software\\OpenSC Project\\OpenSC", temp_path, &temp_len); if (r) { sc_log(ctx, "process_config_file doesn't find opensc config file. Please set the registry key."); return; } + temp_path[temp_len] = '\0'; conf_path = temp_path; #else conf_path = getenv("OPENSC_CONF"); diff --git a/src/libopensc/opensc.h b/src/libopensc/opensc.h index 9853e489..b9b960d8 100644 --- a/src/libopensc/opensc.h +++ b/src/libopensc/opensc.h @@ -840,7 +840,7 @@ int sc_ctx_detect_readers(sc_context_t *ctx); */ int sc_ctx_win32_get_config_value(const char *env, const char *reg, const char *key, - char *out, size_t *out_size); + void *out, size_t *out_size); /** * Returns a pointer to the specified sc_reader_t object diff --git a/src/pkcs15init/profile.c b/src/pkcs15init/profile.c index 2ae105b3..107529a5 100644 --- a/src/pkcs15init/profile.c +++ b/src/pkcs15init/profile.c @@ -342,11 +342,12 @@ sc_profile_load(struct sc_profile *profile, const char *filename) if (!profile_dir) { #ifdef _WIN32 - temp_len = PATH_MAX; + temp_len = PATH_MAX - 1; res = sc_ctx_win32_get_config_value(NULL, "ProfileDir", "Software\\OpenSC Project\\OpenSC", temp_path, &temp_len); if (res) LOG_FUNC_RETURN(ctx, res); + temp_path[temp_len] = '\0'; profile_dir = temp_path; #else profile_dir = SC_PKCS15_PROFILE_DIRECTORY;