|
43 | 43 | #include <wolfssl/wolfcrypt/cmac.h> |
44 | 44 | #include <wolfssl/wolfcrypt/kdf.h> |
45 | 45 |
|
| 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 | + |
46 | 57 | #include <wolfpkcs11/internal.h> |
47 | 58 | #include <wolfpkcs11/store.h> |
48 | 59 |
|
@@ -955,12 +966,59 @@ int wolfPKCS11_Store_OpenSz(int type, CK_ULONG id1, CK_ULONG id2, int read, |
955 | 966 | #endif |
956 | 967 |
|
957 | 968 | #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 | + */ |
958 | 976 | #ifndef WOLFPKCS11_NO_ENV |
959 | 977 | str = XGETENV("WOLFPKCS11_TOKEN_PATH"); |
960 | 978 | #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 |
961 | 1006 | 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 |
962 | 1017 | str = "/tmp"; |
| 1018 | + #endif |
963 | 1019 | } |
| 1020 | + #endif |
| 1021 | + |
964 | 1022 |
|
965 | 1023 | /* 47 is maximum number of character to a filename and path separator. */ |
966 | 1024 | 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, |
1024 | 1082 | else { |
1025 | 1083 | file = XFOPEN(name, "w"); |
1026 | 1084 | 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 | + } |
1028 | 1115 | } |
1029 | 1116 | } |
1030 | 1117 | } |
|
0 commit comments