Skip to content

Commit 8f8d8d8

Browse files
Merge pull request #85 from LinuxJedi/token-path
Improve token path handling Windows failure to be fixed in #84
2 parents e74dfa8 + 1bc86f7 commit 8f8d8d8

4 files changed

Lines changed: 571 additions & 1 deletion

File tree

configure.ac

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,16 @@ then
437437
AM_CFLAGS="$AM_CFLAGS -DWOLFPKCS11_NSS"
438438
fi
439439

440+
AC_ARG_WITH([default-token-path],
441+
[AS_HELP_STRING([--with-default-token-path=PATH],[Set default token storage path (default: none)])],
442+
[ WOLFPKCS11_DEFAULT_TOKEN_PATH=$withval ],
443+
[ WOLFPKCS11_DEFAULT_TOKEN_PATH="" ]
444+
)
445+
if test "x$WOLFPKCS11_DEFAULT_TOKEN_PATH" != "x"
446+
then
447+
AM_CFLAGS="$AM_CFLAGS -DWOLFPKCS11_DEFAULT_TOKEN_PATH=\"$WOLFPKCS11_DEFAULT_TOKEN_PATH\""
448+
fi
449+
440450

441451
AM_CONDITIONAL([BUILD_STATIC],[test "x$enable_shared" = "xno"])
442452

@@ -611,3 +621,4 @@ echo " * DH: $ENABLED_DH"
611621
echo " * ECC: $ENABLED_ECC"
612622
echo " * HKDF: $ENABLED_HKDF"
613623
echo " * NSS modifications: $ENABLED_NSS"
624+
echo " * Default token path: $WOLFPKCS11_DEFAULT_TOKEN_PATH"

src/internal.c

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@
4343
#include <wolfssl/wolfcrypt/cmac.h>
4444
#include <wolfssl/wolfcrypt/kdf.h>
4545

46+
/* OS-specific includes for directory creation */
47+
#if defined(_WIN32) || defined(_MSC_VER)
48+
#include <direct.h>
49+
#include <io.h>
50+
#define MKDIR(path) _mkdir(path)
51+
#else
52+
#include <sys/stat.h>
53+
#include <errno.h>
54+
#define MKDIR(path) mkdir(path, 0700)
55+
#endif
56+
4657
#include <wolfpkcs11/internal.h>
4758
#include <wolfpkcs11/store.h>
4859

@@ -955,12 +966,59 @@ int wolfPKCS11_Store_OpenSz(int type, CK_ULONG id1, CK_ULONG id2, int read,
955966
#endif
956967

957968
#else
969+
/* Path order:
970+
* 1. Environment variable WOLFPKCS11_TOKEN_PATH
971+
* 2. Home directory with .wolfPKCS11 (or APPDIR with wolfPKCS11 for
972+
* Windows)
973+
* 3. WOLFPKCS11_DEFAULT_TOKEN_PATH, if set
974+
* 4. /tmp in Linux, %TEMP% or C:\Windows\Temp in Windows
975+
*/
958976
#ifndef WOLFPKCS11_NO_ENV
959977
str = XGETENV("WOLFPKCS11_TOKEN_PATH");
960978
#endif
979+
980+
if (str == NULL) {
981+
char homePath[47]; /* Must fit within name buffer size limit */
982+
const char* homeDir = NULL;
983+
984+
#if defined(_WIN32) || defined(_MSC_VER)
985+
homeDir = XGETENV("%APPDIR%");
986+
if (homeDir != NULL && XSTRLEN(homeDir) <= sizeof(homePath) - 13) {
987+
int len = XSNPRINTF(homePath, sizeof(homePath), "%s\\wolfPKCS11",
988+
homeDir);
989+
if (len > 0 && len < (int)sizeof(homePath)) {
990+
str = homePath;
991+
}
992+
}
993+
#else
994+
homeDir = XGETENV("HOME");
995+
if (homeDir != NULL && XSTRLEN(homeDir) <= sizeof(homePath) - 13) {
996+
int len = XSNPRINTF(homePath, sizeof(homePath), "%s/.wolfPKCS11",
997+
homeDir);
998+
if (len > 0 && len < (int)sizeof(homePath)) {
999+
str = homePath;
1000+
}
1001+
}
1002+
#endif
1003+
}
1004+
1005+
#ifdef WOLFPKCS11_DEFAULT_TOKEN_PATH
9611006
if (str == NULL) {
1007+
str = WC_STRINGIFY(WOLFPKCS11_DEFAULT_TOKEN_PATH);
1008+
}
1009+
#else
1010+
if (str == NULL) {
1011+
#if defined(_WIN32) || defined(_MSC_VER)
1012+
str = XGETENV("%TEMP%");
1013+
if (str == NULL) {
1014+
str = "C:\\Windows\\Temp";
1015+
}
1016+
#else
9621017
str = "/tmp";
1018+
#endif
9631019
}
1020+
#endif
1021+
9641022

9651023
/* 47 is maximum number of character to a filename and path separator. */
9661024
if (str == NULL || (XSTRLEN(str) > sizeof(name) - 47)) {
@@ -1024,7 +1082,36 @@ int wolfPKCS11_Store_OpenSz(int type, CK_ULONG id1, CK_ULONG id2, int read,
10241082
else {
10251083
file = XFOPEN(name, "w");
10261084
if (file == NULL) {
1027-
ret = READ_ONLY_E;
1085+
/* Try to create directory if it doesn't exist */
1086+
char* lastSlash = NULL;
1087+
char dirPath[120];
1088+
int i;
1089+
1090+
/* Find the last directory separator */
1091+
for (i = 0; name[i] != '\0'; i++) {
1092+
if (name[i] == '/' || name[i] == '\\') {
1093+
lastSlash = (char*)&name[i];
1094+
}
1095+
}
1096+
1097+
if (lastSlash != NULL) {
1098+
/* Extract directory path */
1099+
int dirLen = (int)(lastSlash - name);
1100+
if (dirLen > 0 && dirLen < (int)sizeof(dirPath)) {
1101+
XMEMCPY(dirPath, name, dirLen);
1102+
dirPath[dirLen] = '\0';
1103+
1104+
/* Try to create the directory */
1105+
if (MKDIR(dirPath) == 0 || errno == EEXIST) {
1106+
/* Directory created or already exists, try opening file again */
1107+
file = XFOPEN(name, "w");
1108+
}
1109+
}
1110+
}
1111+
1112+
if (file == NULL) {
1113+
ret = READ_ONLY_E;
1114+
}
10281115
}
10291116
}
10301117
}

tests/include.am

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ noinst_PROGRAMS += tests/pkcs11str
1616
tests_pkcs11str_SOURCES = tests/pkcs11str.c
1717
tests_pkcs11str_LDADD =
1818

19+
check_PROGRAMS += tests/token_path_test
20+
noinst_PROGRAMS += tests/token_path_test
21+
tests_token_path_test_SOURCES = tests/token_path_test.c
22+
tests_token_path_test_LDADD =
23+
1924
if BUILD_STATIC
2025
tests_pkcs11test_LDADD += src/libwolfpkcs11.la
2126
tests_pkcs11mtt_LDADD += src/libwolfpkcs11.la
2227
tests_pkcs11str_LDADD += src/libwolfpkcs11.la
28+
tests_token_path_test_LDADD += src/libwolfpkcs11.la
2329
endif
2430

2531
EXTRA_DIST += tests/unit.h \

0 commit comments

Comments
 (0)