Ejemplo n.º 1
0
        // * //



        // Unmount encrypted filesystem //
        public static BooleanResult UnmountEncryptedFS(string targetDrive)
        {
            using (Process process = new Process())
            {
                // GET Dokan DIRECTORY
                string programDir = Toolbox.GetSoftwareDirectory(Config.Software.Dokan);
                if (programDir == null)
                {
                    return(new BooleanResult()
                    {
                        Success = false, Message = "ERROR: Dokan inaccessible!"
                    });
                }



                // UNMOUNT ENCRYPTED FS
                ProcessStartInfo startInfo = new ProcessStartInfo();
                try
                {
                    //.\dokanctl.exe /u Z:

                    startInfo.WindowStyle    = ProcessWindowStyle.Hidden;
                    startInfo.CreateNoWindow = true;
                    startInfo.FileName       = "cmd.exe";
                    startInfo.Arguments      = "/C \"\"" + programDir + "dokanctl.exe\"  /u \"" + targetDrive + ":\"\"";
                    process.StartInfo        = startInfo;
                    process.Start();
                    process.WaitForExit();

                    // Ensure no errors were thrown (unmount status 0 = error, 1 = unmounted)
                    if (process.ExitCode != 1)
                    {
                        return(new BooleanResult()
                        {
                            Success = false, Message = "ERROR: Error while unmounting! " + process.ExitCode
                        });
                    }
                }
                catch (Exception err)
                {
                    return(new BooleanResult()
                    {
                        Success = false, Message = "ERROR: Failed to unmount encrypted FS! " + err.Message
                    });
                }
            }

            // Invalidate EncFS instances that claim to be using this drive
            EncryptFS.InvalidateDrive(targetDrive);

            return(new BooleanResult()
            {
                Success = true
            });
        }
Ejemplo n.º 2
0
        // * //



        // Mount encrypted filesystem //
        public static BooleanResult MountEncryptedFS(string guid, string targetDrive, string masterKey, string label)
        {
            // Determine location of configuration file
            string configLoc = (string)Registry.GetValue(Config.CURR_USR_REG_DRIVE_ROOT + guid, "configLoc", string.Empty);

            if (string.IsNullOrEmpty(configLoc))
            {
                return(new BooleanResult()
                {
                    Success = false, Message = "ERROR: Invalid GUID given to mount!"
                });
            }


            // Pull enc volume location from registry for this GUID
            string volumeLoc = (string)Registry.GetValue(Config.CURR_USR_REG_DRIVE_ROOT + guid, "encContainerLoc", string.Empty);

            if (string.IsNullOrEmpty(volumeLoc))
            {
                return(new BooleanResult()
                {
                    Success = false, Message = "ERROR: Invalid GUID given to mount!"
                });
            }

            // Path cannot end with a slash due to CMD usage
            volumeLoc = volumeLoc.TrimEnd(new[] { '/', '\\' });



            using (Process process = new Process())
            {
                // GET EncFS DIRECTORY
                string programDir = Toolbox.GetSoftwareDirectory(Config.Software.EncFS);
                if (programDir == null)
                {
                    return(new BooleanResult()
                    {
                        Success = false, Message = "ERROR: EncFS inaccessible!"
                    });
                }


                // GET Dokan DIRECTORY
                string dokanDir = Toolbox.GetSoftwareDirectory(Config.Software.Dokan);
                if (dokanDir == null)
                {
                    return(new BooleanResult()
                    {
                        Success = false, Message = "ERROR: Dokan inaccessible!"
                    });
                }



                // MOUNT ENCRYPTED CONTAINER
                ProcessStartInfo startInfo = new ProcessStartInfo();
                try
                {
                    //.\encfs.exe "C:\Users\jetwhiz\Desktop\encfs4win\testing" "Z:" --stdinpass -o volname="Secure Dropbox"

                    startInfo.WindowStyle           = ProcessWindowStyle.Hidden;
                    startInfo.CreateNoWindow        = true;
                    startInfo.RedirectStandardInput = true;
                    startInfo.UseShellExecute       = false;
                    startInfo.FileName = "cmd.exe";
                    startInfo.EnvironmentVariables["ENCFS6_CONFIG"] = configLoc + Config.ENCFS_CONFIG_FILENAME;
                    startInfo.Arguments = "/C \"\"" + programDir + "encfs.exe\" --require-macs --stdinpass -o volname=\"" + label + "\" \"" + volumeLoc + "\" \"" + targetDrive + ":\"\"";
                    process.StartInfo   = startInfo;
                    process.Start();

                    // Send stdin inputs to program
                    StreamWriter myStreamWriter = process.StandardInput;
                    myStreamWriter.WriteLine(masterKey); // user's password
                    myStreamWriter.Close();



                    // Give it a few seconds to finish
                    // TODO: Figure out better way to determine when EncFS has finished
                    process.WaitForExit(10000);



                    // If it exited by itself, it was in error
                    if (process.HasExited)
                    {
                        return(new BooleanResult()
                        {
                            Success = false, Message = "ERROR: Error while mounting encrypted FS! " + process.ExitCode
                        });
                    }


                    // Invalidate all other EncFS instances that still claim to be using this drive
                    EncryptFS.InvalidateDrive(targetDrive);

                    // Save where we mounted the encrypted volume
                    Registry.SetValue(Config.CURR_USR_REG_DRIVE_ROOT + guid, "encDrive", targetDrive);


                    // Process will block indefinitely (until unmount called), so just return
                    //process.WaitForExit();
                }
                catch (Exception err)
                {
                    return(new BooleanResult()
                    {
                        Success = false, Message = "ERROR: Failed to mount encrypted FS! " + err.Message
                    });
                }
            }


            return(new BooleanResult()
            {
                Success = true
            });
        }
Ejemplo n.º 3
0
        // * //



        // Create new encrypted filesystem //
        public static BooleanResult CreateEncryptedFS(string guid, string volumeLoc, string targetDrive, string masterKey, string label, bool keepMounted = false)
        {
            // Path cannot end with a slash due to CMD usage
            volumeLoc = volumeLoc.TrimEnd(new[] { '/', '\\' });


            // GET EncFS DIRECTORY
            string programDir = Toolbox.GetSoftwareDirectory(Config.Software.EncFS);

            if (programDir == null)
            {
                return(new BooleanResult()
                {
                    Success = false, Message = "ERROR: EncFS inaccessible!"
                });
            }


            // GET Dokan DIRECTORY
            string dokanDir = Toolbox.GetSoftwareDirectory(Config.Software.Dokan);

            if (dokanDir == null)
            {
                return(new BooleanResult()
                {
                    Success = false, Message = "ERROR: Dokan inaccessible!"
                });
            }



            // Determine Keenou config file location for this volume
            string configLoc = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\Keenou\" + guid + @"\";

            if (Directory.Exists(configLoc))
            {
                // TODO: auto-recovery?
                return(new BooleanResult()
                {
                    Success = false, Message = "ERROR: Possible GUID collision!"
                });
            }


            // Create config file directory for this FS
            Directory.CreateDirectory(configLoc);


            // Save setting values to registry
            Registry.SetValue(Config.CURR_USR_REG_DRIVE_ROOT + guid, "encContainerLoc", volumeLoc);
            Registry.SetValue(Config.CURR_USR_REG_DRIVE_ROOT + guid, "configLoc", configLoc);


            using (Process process = new Process())
            {
                // MOUNT ENCRYPTED CONTAINER
                ProcessStartInfo startInfo = new ProcessStartInfo();
                try
                {
                    //.\encfs.exe "C:\Users\jetwhiz\Desktop\encfs4win\testing" "Z:" --stdinpass -o volname="Secure Dropbox"

                    startInfo.WindowStyle           = ProcessWindowStyle.Hidden;
                    startInfo.CreateNoWindow        = true;
                    startInfo.RedirectStandardInput = true;
                    startInfo.UseShellExecute       = false;
                    startInfo.FileName = "cmd.exe";
                    startInfo.EnvironmentVariables["ENCFS6_CONFIG"] = configLoc + Config.ENCFS_CONFIG_FILENAME;
                    startInfo.Arguments = "/C \"\"" + programDir + "encfs.exe\" --stdinpass -o volname=\"" + label + "\" \"" + volumeLoc + "\" \"" + targetDrive + ":\"\"";
                    process.StartInfo   = startInfo;
                    process.Start();

                    // Send stdin inputs to program
                    StreamWriter myStreamWriter = process.StandardInput;
                    myStreamWriter.WriteLine("p");       // paranoia mode
                    myStreamWriter.WriteLine(masterKey); // user's password
                    myStreamWriter.Close();



                    // Give it a few seconds to finish
                    // TODO: Figure out better way to determine when EncFS has finished
                    process.WaitForExit(10000);



                    // Unmount drive (forcing the closing of encfs process) if not already exited
                    if (!process.HasExited)
                    {
                        // Auto-unmount unless they asked us not to
                        if (!keepMounted)
                        {
                            BooleanResult res = EncryptFS.UnmountEncryptedFS(targetDrive);
                            if (res == null || !res.Success)
                            {
                                return(res);
                            }
                        }
                        else
                        {
                            // Invalidate all other EncFS instances that still claim to be using this drive
                            EncryptFS.InvalidateDrive(targetDrive);

                            // Save where we mounted the encrypted volume
                            Registry.SetValue(Config.CURR_USR_REG_DRIVE_ROOT + guid, "encDrive", targetDrive);
                        }
                    }
                    else
                    {
                        // If it exited by itself, it was in error
                        return(new BooleanResult()
                        {
                            Success = false, Message = "ERROR: Error while creating encrypted FS! " + process.ExitCode
                        });
                    }


                    // Process will block indefinitely (until unmount called), so just return
                    //process.WaitForExit();
                }
                catch (Exception err)
                {
                    return(new BooleanResult()
                    {
                        Success = false, Message = "ERROR: Failed to create encrypted FS! " + err.Message
                    });
                }
            }

            return(new BooleanResult()
            {
                Success = true
            });
        }