@@ -161,7 +161,7 @@ int read_file(SSL_CTX* ctx, const char* file, int format, CertType type)
161161 TaoCrypt::DSA_PrivateKey dsaKey;
162162 dsaKey.Initialize (dsaSource);
163163
164- if (rsaSource .GetError ().What ()) {
164+ if (dsaSource .GetError ().What ()) {
165165 // neither worked
166166 ret = SSL_FAILURE;
167167 }
@@ -784,40 +784,67 @@ int SSL_CTX_load_verify_locations(SSL_CTX* ctx, const char* file,
784784 WIN32_FIND_DATA FindFileData;
785785 HANDLE hFind;
786786
787- char name[MAX_PATH + 1 ]; // directory specification
788- strncpy (name, path, MAX_PATH - 3 );
789- strncat (name, " \\ *" , 3 );
787+ const int DELIMITER_SZ = 2 ;
788+ const int DELIMITER_STAR_SZ = 3 ;
789+ int pathSz = (int )strlen (path);
790+ int nameSz = pathSz + DELIMITER_STAR_SZ + 1 ; // plus 1 for terminator
791+ char * name = NEW_YS char [nameSz]; // directory specification
792+ memset (name, 0 , nameSz);
793+ strncpy (name, path, nameSz - DELIMITER_STAR_SZ - 1 );
794+ strncat (name, " \\ *" , DELIMITER_STAR_SZ);
790795
791796 hFind = FindFirstFile (name, &FindFileData);
792- if (hFind == INVALID_HANDLE_VALUE) return SSL_BAD_PATH;
797+ if (hFind == INVALID_HANDLE_VALUE) {
798+ ysArrayDelete (name);
799+ return SSL_BAD_PATH;
800+ }
793801
794802 do {
795- if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) {
796- strncpy (name, path, MAX_PATH - 2 - HALF_PATH);
797- strncat (name, " \\ " , 2 );
798- strncat (name, FindFileData.cFileName , HALF_PATH);
803+ if (!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
804+ int curSz = (int )strlen (FindFileData.cFileName );
805+ if (pathSz + curSz + DELIMITER_SZ + 1 > nameSz) {
806+ ysArrayDelete (name);
807+ // plus 1 for terminator
808+ nameSz = pathSz + curSz + DELIMITER_SZ + 1 ;
809+ name = NEW_YS char [nameSz];
810+ }
811+ memset (name, 0 , nameSz);
812+ strncpy (name, path, nameSz - curSz - DELIMITER_SZ - 1 );
813+ strncat (name, " \\ " , DELIMITER_SZ);
814+ strncat (name, FindFileData.cFileName ,
815+ nameSz - pathSz - DELIMITER_SZ - 1 );
799816 ret = read_file (ctx, name, SSL_FILETYPE_PEM, CA);
800817 }
801818 } while (ret == SSL_SUCCESS && FindNextFile (hFind, &FindFileData));
802819
820+ ysArrayDelete (name);
803821 FindClose (hFind);
804822
805823#else // _WIN32
806-
807- const int MAX_PATH = 260 ;
808-
809824 DIR* dir = opendir (path);
810825 if (!dir) return SSL_BAD_PATH;
811826
812827 struct dirent * entry;
813828 struct stat buf;
814- char name[MAX_PATH + 1 ];
829+ const int DELIMITER_SZ = 1 ;
830+ int pathSz = (int )strlen (path);
831+ int nameSz = pathSz + DELIMITER_SZ + 1 ; // plus 1 for null terminator
832+ char * name = NEW_YS char [nameSz]; // directory specification
815833
816834 while (ret == SSL_SUCCESS && (entry = readdir (dir))) {
817- strncpy (name, path, MAX_PATH - 1 - HALF_PATH);
818- strncat (name, " /" , 1 );
819- strncat (name, entry->d_name , HALF_PATH);
835+ int curSz = (int )strlen (entry->d_name );
836+ if (pathSz + curSz + DELIMITER_SZ + 1 > nameSz) {
837+ ysArrayDelete (name);
838+ nameSz = pathSz + DELIMITER_SZ + curSz + 1 ;
839+ name = NEW_YS char [nameSz];
840+ }
841+ memset (name, 0 , nameSz);
842+ strncpy (name, path, nameSz - curSz - 1 );
843+ strncat (name, " /" , DELIMITER_SZ);
844+ strncat (name, entry->d_name , nameSz - pathSz - DELIMITER_SZ - 1 );
845+
820846 if (stat (name, &buf) < 0 ) {
847+ ysArrayDelete (name);
821848 closedir (dir);
822849 return SSL_BAD_STAT;
823850 }
@@ -826,6 +853,7 @@ int SSL_CTX_load_verify_locations(SSL_CTX* ctx, const char* file,
826853 ret = read_file (ctx, name, SSL_FILETYPE_PEM, CA);
827854 }
828855
856+ ysArrayDelete (name);
829857 closedir (dir);
830858
831859#endif
0 commit comments