EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Encrypting filename in MapperAES example

Also by EldoS: RawDisk
Access locked and protected files in Windows, read and write disks and partitions and more.
#32856
Posted: 03/30/2015 13:14:09
by Ighor Domingues (Basic support level)
Joined: 03/13/2015
Posts: 4

Hi,

We are trying to encrypt the filenames in MapperAES example and we are facing case sensitive problems I think.

When we add:

MyFile.txt to "C:\MountedPoint" (for example)

the system encrypts and sends the file to:

"C:\EncryptedFolder" as xyz.txt (encrypted file)

that appears to work fine.

When we unmount and then mount again the virtual folder ("C:\MountedPoint"), "MyFile.txt is shown, but we are not able to open/delete/rename the file.

It looks like CbFsGetFileInfo are receiving FileName as "MYFILE.TXT" instead of "MyFile.txt", so OnCreateFile, OnOpenFile, OnDeleteFile and OnRenameFile are not getting the correct FileName and then Windows shows a "File not found..." error message.

We tried to set CaseSensitiveFileNames = true before mounting the virtual folder; tried to return RealFileName from the CbFsGetFileInfo callback as FileName when the file is valid.

As anything worked, we tested adding a file with a real uppercase FileName:

"MYFILE.TXT" to "C:\MountedPoint"

and everything seem to work all right.


Are we missing something?
#32857
Posted: 03/30/2015 13:36:11
by Eugene Mayevski (EldoS Corp.)

From your description I understand that you have modified MapperAES sample to change the names of the files. Most likely some of the changes are either incomplete or incoherent.

Please start with setting MetadataCacheEnabled property to false and see if this helps. We'll decide on the possible problem resolution route based on this check.


Sincerely yours
Eugene Mayevski
#32859
Posted: 03/30/2015 14:31:50
by Ighor Domingues (Basic support level)
Joined: 03/13/2015
Posts: 4

I just tried to set MetaDataCacheEnabled to false and nothing changed.

This is what we added to the Constructor:

Code
            cbfs.CaseSensitiveFileNames = true;
            cbfs.FileSystemName = "NTFS";
            cbfs.MetaDataCacheEnabled = false;

-------------------------------------------------------------------------------------------------------------------
In CbFsCreateFile/CbFsOpenFile we changed:

                ...
                String newFileName = FileName;
                if (ENCRYPT_FILENAME == true)
                {
                    //Encrypts the filename
                    newFileName = EncryptFileName(FileName);
                }

                Context = new EncryptContext(cbfs, newFileName, FileAttributes, FileInfo, true, this.rootPath, this.password, this.salt, this.keySize);
                ...

-------------------------------------------------------------------------------------------------------------------
In CbFsGetFileInfo we changed:
            
            ...
            String newFileName = FileName;
            if (ENCRYPT_FILENAME == true)
            {
                //Encrypts the filename
                newFileName = EncryptFileName(FileName);
            }

            BY_HANDLE_FILE_INFORMATION fi;
            SafeFileHandle hFile = CreateFile(rootPath + newFileName, READ_CONTROL, 0, IntPtr.Zero, FileMode.Open, FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero);
            FileExists = false;

            if (!hFile.IsInvalid)
            {
                IntPtr buffer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(BY_HANDLE_FILE_INFORMATION)));

                bool Ok = GetFileInformationByHandle(hFile, buffer);

                if (Ok)
                {                
                    FileExists = true;
                    fi = (BY_HANDLE_FILE_INFORMATION)Marshal.PtrToStructure(buffer, typeof(BY_HANDLE_FILE_INFORMATION));
                    CreationTime = DateTime.FromFileTimeUtc((((long)fi.ftCreationTime.dwHighDateTime) << 32) + fi.ftCreationTime.dwLowDateTime);
                    LastAccessTime = DateTime.FromFileTimeUtc((((long)fi.ftLastAccessTime.dwHighDateTime) << 32) + fi.ftLastAccessTime.dwLowDateTime);
                    LastWriteTime = DateTime.FromFileTimeUtc((((long)fi.ftLastWriteTime.dwHighDateTime) << 32) + fi.ftLastWriteTime.dwLowDateTime);
                    EndOfFile = (long)(((ulong)fi.nFileSizeHigh << 32 & 0xFFFFFFFF00000000) | (ulong)fi.nFileSizeLow);
                    AllocationSize = EndOfFile;
                    FileAttributes = fi.dwFileAttributes;
                    RealFileName = FileName; //ADDED
                    ...
-------------------------------------------------------------------------------------------------------------------
In CbFsEnumerateDirectory we changed:

            ...
            while (FileFound = context.GetNextFileInfo(out finfo))
            {
                if (finfo.Name != "." && finfo.Name != ".." && (finfo.Extension == ".crypt")) break;
            }
            if (FileFound)
            {                
                String newFileName = finfo.Name;
                if (ENCRYPT_FILENAME == true)
                {
                    //Decrypts the file name
                    newFileName = DecryptFileName(finfo.Name);
                }

                FileName = newFileName;

                CreationTime = finfo.CreationTime;

                LastAccessTime = finfo.LastAccessTime;

                LastWriteTime = finfo.LastWriteTime;
                ...

-------------------------------------------------------------------------------------------------------------------
In CbFsSetFileAttributes we changed:
            
            ...
            String newFileName = FileInfo.FileName;
            if (ENCRYPT_FILENAME == true)
            {
                //Encrypts the filename
                newFileName = EncryptFileName(FileInfo.FileName);
            }
            // the case when FileAttributes == 0 indicates that file attributes
            // not changed during this callback
            if (Attributes != 0)
                SetFileAttributes(rootPath + newFileName, Attributes);
            ...

-------------------------------------------------------------------------------------------------------------------
In CbFsDeleteFile we changed:

            ...
            String newFileName = FileInfo.FileName;
            if (ENCRYPT_FILENAME == true)
            {
                //Encrypts the filename
                newFileName = EncryptFileName(FileInfo.FileName);
            }

            if ((File.GetAttributes(rootPath + newFileName) & FileAttributes.Directory) != 0)
            {
                info = new DirectoryInfo(rootPath + FileInfo.FileName);
            }
            else
            {
                info = new FileInfo(rootPath + newFileName);
            }
            info.Delete();
            ...

-------------------------------------------------------------------------------------------------------------------
In CbFsRenameOrMoveFile we changed:

            String fileName = FileInfo.FileName;
            String newFileName = NewFileName;
            if (ENCRYPT_FILENAME == true)
            {
                //Encrypts the filenames
                fileName = EncryptFileName(FileInfo.FileName);
                newFileName = EncryptFileName(NewFileName);
            }

            if ((File.GetAttributes(rootPath + fileName) & FileAttributes.Directory) != 0)
            {
                DirectoryInfo dirinfo = new DirectoryInfo(rootPath + FileInfo.FileName);
                dirinfo.MoveTo(rootPath + NewFileName);

            }
            else
            {
                FileInfo finfo = new FileInfo(rootPath + fileName);

                FileInfo finfo1 = new FileInfo(rootPath + newFileName);

                if (finfo1.Exists)
                {
                    finfo1.Delete();
                }
                finfo.MoveTo(rootPath + newFileName);

            }

-------------------------------------------------------------------------------------------------------------------
In CbFsEnumerateNamedStreams we changed:

                ...
                if (IntPtr.Zero == EnumerationInfo.UserContext)
                {                    
                    String fileName = FileInfo.FileName;
                    if (ENCRYPT_FILENAME == true)
                    {
                        //Encrypts the filename
                        fileName = EncryptFileName(FileInfo.FileName);
                    }

                    SafeFileHandle safeHandle = CreateFile(rootPath + fileName, READ_CONTROL, 0, IntPtr.Zero, FileMode.Open, FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero);
                    Ctx = new AlternateDataStreamContext(safeHandle);
                    EnumerationInfo.UserContext = GCHandle.ToIntPtr(GCHandle.Alloc(Ctx));
                }
                ...

-------------------------------------------------------------------------------------------------------------------
In CbFsGetFileSecurity we changed:

                    ...
                    String fileName = FileInfo.FileName;
                    if (ENCRYPT_FILENAME == true)
                    {
                        //Encrypts the filename
                        fileName = EncryptFileName(FileInfo.FileName);
                    }

                    if (!GetFileSecurity(rootPath + fileName, SecurityInformation, SecurityDescriptor, Length, ref LengthNeeded))
                    {
                        UInt32 Error = CallbackFileSystem.GetLastWin32Error();
                        throw new ECBFSError(Error);
                    }
                    ...


Do we need to change anywhere else or set any other property?
#32861
Posted: 03/30/2015 15:15:13
by Eugene Mayevski (EldoS Corp.)

CaseSensitive property is irrelevant here. NTFS used as a filesystem name can confuse certain applications and even the OS, so I would suggest removing "NTFS" as a filesystem name and reverting CaseSensitive to false, then testing your sample again.

If this doesn't work, then we need a compilable test case from you. It would be great if you could pack your modified sample (please, no obj and bin directories) and post it to the HelpDesk to the ticket that I've created for you there (you'll see the ticket when entering the HelpDesk).

On a side note it would help a lot if you used CODE button (or write [ CODE ] and [ /CODE ] respectively by hand) to mark the beginning and the end of the code blocks in your messages. This would enable syntax highlighting and line numbering on the code and make it easier for analysis.


Sincerely yours
Eugene Mayevski
#32870
Posted: 03/31/2015 14:33:51
by Ighor Domingues (Basic support level)
Joined: 03/13/2015
Posts: 4

Hi Eugene,

I have sent our compilable test case to the HelpDesk. Could you give me your opinion about what might be incomplete or incoherent?

Thank you.
#32871
Posted: 03/31/2015 14:37:58
by Eugene Mayevski (EldoS Corp.)

Please check the communication there in Helpdesk.


Sincerely yours
Eugene Mayevski
Also by EldoS: BizCrypto
Components for BizTalk® and SQL Server® Integration Services that let you securely store and transfer information in your business automation solutions.

Reply

Statistics

Topic viewed 3126 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!