From 475b82f43a083abb07c882ed0e362e794caebf9c Mon Sep 17 00:00:00 2001 From: Hylke Bons Date: Sat, 29 Sep 2012 23:57:35 +0200 Subject: [PATCH] crypto: use a random salt per repo, store it as a remote branch name --- SparkleLib/Git/SparkleFetcherGit.cs | 34 ++++++++++++++++++- SparkleLib/Git/SparkleLib.Git.csproj | 11 ++++-- SparkleLib/Git/SparkleRepoGit.cs | 17 ++++++++++ SparkleLib/SparkleFetcherBase.cs | 46 +++++++++++++++++++------- SparkleLib/SparkleLib.csproj | 18 ++++++---- SparkleShare/Linux/SparkleSetup.cs | 3 +- SparkleShare/Mac/SparkleSetup.cs | 2 +- SparkleShare/Mac/SparkleShare.csproj | 12 ++++++- SparkleShare/Mac/SparkleShare.sln | 7 ++++ SparkleShare/SparkleControllerBase.cs | 1 + SparkleShare/SparkleSetupController.cs | 1 + SparkleShare/Windows/SparkleSetup.cs | 2 +- 12 files changed, 128 insertions(+), 26 deletions(-) diff --git a/SparkleLib/Git/SparkleFetcherGit.cs b/SparkleLib/Git/SparkleFetcherGit.cs index f82f130c..69bb8d7c 100755 --- a/SparkleLib/Git/SparkleFetcherGit.cs +++ b/SparkleLib/Git/SparkleFetcherGit.cs @@ -30,7 +30,39 @@ namespace SparkleLib.Git { private SparkleGit git; private bool use_git_bin; - private string crypto_salt = "e0d592768d7cf99a"; // TODO: Make unique per repo + + private string cached_salt; + + private string crypto_salt { + get { + if (!string.IsNullOrEmpty (this.cached_salt)) + return this.cached_salt; + + // Check if the repo's salt is stored in a branch... + SparkleGit git = new SparkleGit (TargetFolder, "branch -a"); + string [] branches = git.StartAndReadStandardOutput ().Split (Environment.NewLine.ToCharArray ()); + // TODO double check env.newline ^ + + foreach (string branch in branches) { + if (branch.StartsWith (" remotes/origin/salt-")) { + this.cached_salt = branch.Substring (22); + break; + } + } + + // ...if not, create a new salt for the repo + if (string.IsNullOrEmpty (this.cached_salt)) { + this.cached_salt = GenerateCryptoSalt (); + string salt_file_path = new string [] { TargetFolder, ".git", "salt" }.Combine (); + + // Temporarily store the salt in a file, so the Repo can + // push it to a branch on the host later + File.WriteAllText (salt_file_path, this.cached_salt); + } + + return this.cached_salt; + } + } public SparkleFetcher (string server, string required_fingerprint, string remote_path, diff --git a/SparkleLib/Git/SparkleLib.Git.csproj b/SparkleLib/Git/SparkleLib.Git.csproj index ff76bd13..a67702e7 100644 --- a/SparkleLib/Git/SparkleLib.Git.csproj +++ b/SparkleLib/Git/SparkleLib.Git.csproj @@ -1,7 +1,7 @@ - Release + Debug AnyCPU 9.0.30729 2.0 @@ -11,15 +11,22 @@ SparkleLib.Git SparkleLib.Git 512 + pdbonly - true + True ..\..\bin\ TRACE prompt 4 + + none + False + bin\Debug + 4 + diff --git a/SparkleLib/Git/SparkleRepoGit.cs b/SparkleLib/Git/SparkleRepoGit.cs index 2504ba4b..b92e7e89 100644 --- a/SparkleLib/Git/SparkleRepoGit.cs +++ b/SparkleLib/Git/SparkleRepoGit.cs @@ -35,6 +35,7 @@ namespace SparkleLib.Git { { // TODO: Set git locale to en-US + // Check if we should use git-bin SparkleGit git = new SparkleGit (LocalPath, "config --get filter.bin.clean"); git.StartAndWaitForExit (); @@ -177,6 +178,22 @@ namespace SparkleLib.Git { string message = FormatCommitMessage (); Commit (message); + + string salt_file_path = new string [] { LocalPath, ".git", "salt" }.Combine (); + + // If the repo is encrypted, create a branch to + // store the in and push it to the host + if (File.Exists (salt_file_path)) { + string salt = File.ReadAllText (salt_file_path).Trim (); + + SparkleGit git_salt = new SparkleGit (LocalPath, "branch salt-" + salt); + git_salt.StartAndWaitForExit (); + + git_salt = new SparkleGit (LocalPath, "push origin salt-" + salt); + git_salt.StartAndWaitForExit (); + + File.Delete (salt_file_path); + } } SparkleGit git; diff --git a/SparkleLib/SparkleFetcherBase.cs b/SparkleLib/SparkleFetcherBase.cs index 5cfa8194..92fa7052 100755 --- a/SparkleLib/SparkleFetcherBase.cs +++ b/SparkleLib/SparkleFetcherBase.cs @@ -160,7 +160,7 @@ namespace SparkleLib { IsActive = false; // TODO: Find better way to determine if folder should have crypto setup - bool repo_is_encrypted = RemoteUrl.ToString ().Contains ("crypto"); + bool repo_is_encrypted = RemoteUrl.ToString ().Contains ("-crypto"); Finished (repo_is_encrypted, IsFetchedRepoEmpty, Warnings); } else { @@ -207,18 +207,23 @@ namespace SparkleLib { uri_builder.Password = ""; } - string text = "Congratulations, you've successfully created a SparkleShare repository!" + n + - n + - "Any files you add or change in this folder will be automatically synced to " + n + - uri_builder.ToString () + " and everyone connected to it." + n + - n + - "SparkleShare is an Open Source software program that helps people " + n + - "collaborate and share files. If you like what we do, please consider a small " + n + - "donation to support the project: http://sparkleshare.org/support-us/" + n + - n + - "Have fun! :)" + n; + // TODO: Find better way to determine if folder should have crypto setup + bool repo_is_encrypted = RemoteUrl.ToString ().Contains ("crypto"); - File.WriteAllText (file_path, text); + if (!repo_is_encrypted) { + string text = "Congratulations, you've successfully created a SparkleShare repository!" + n + + n + + "Any files you add or change in this folder will be automatically synced to " + n + + uri_builder.ToString () + " and everyone connected to it." + n + + n + + "SparkleShare is an Open Source software program that helps people " + n + + "collaborate and share files. If you like what we do, please consider a small " + n + + "donation to support the project: http://www.sparkleshare.org/" + n + + n + + "Have fun! :)" + n; + + File.WriteAllText (file_path, text); + } } @@ -258,6 +263,23 @@ namespace SparkleLib { } + protected string GenerateCryptoSalt () + { + int seed = new Random ().Next (1, int.MaxValue); + string allowed_chars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789"; + char [] chars = new char [256]; + Random random = new Random (seed); + + for (var i = 0; i < 256; i++) + chars [i] = allowed_chars [random.Next (0, allowed_chars.Length)]; + + string salt = new string (chars); + salt = salt.SHA1 (); + + return salt.Substring (0, 16); + } + + private string GetHostKey () { string host = RemoteUrl.Host; diff --git a/SparkleLib/SparkleLib.csproj b/SparkleLib/SparkleLib.csproj index 345c0d69..94b5b57d 100644 --- a/SparkleLib/SparkleLib.csproj +++ b/SparkleLib/SparkleLib.csproj @@ -1,7 +1,7 @@ - Release + Debug AnyCPU 8.0.50727 2.0 @@ -9,14 +9,21 @@ Library SparkleLib SparkleLib + none - false + False ..\bin prompt 4 - false + False + + + none + False + bin\Debug + 4 @@ -36,14 +43,13 @@ - - - + + diff --git a/SparkleShare/Linux/SparkleSetup.cs b/SparkleShare/Linux/SparkleSetup.cs index beb63152..25ec3fdd 100755 --- a/SparkleShare/Linux/SparkleSetup.cs +++ b/SparkleShare/Linux/SparkleSetup.cs @@ -691,8 +691,7 @@ namespace SparkleShare { Description = "You can find it in your SparkleShare folder"; // A button that opens the synced folder - Button open_folder_button = new Button (string.Format ("Open {0}", - System.IO.Path.GetFileName (Controller.PreviousPath))); + Button open_folder_button = new Button ("Show Folder"); open_folder_button.Clicked += delegate { Controller.OpenFolderClicked (); diff --git a/SparkleShare/Mac/SparkleSetup.cs b/SparkleShare/Mac/SparkleSetup.cs index 4332219a..e03017ba 100755 --- a/SparkleShare/Mac/SparkleSetup.cs +++ b/SparkleShare/Mac/SparkleSetup.cs @@ -805,7 +805,7 @@ namespace SparkleShare { OpenFolderButton = new NSButton () { - Title = string.Format ("Open {0}", Path.GetFileName (Controller.PreviousPath)) + Title = "Show folder" }; FinishButton = new NSButton () { diff --git a/SparkleShare/Mac/SparkleShare.csproj b/SparkleShare/Mac/SparkleShare.csproj index 629b6acc..600a6628 100644 --- a/SparkleShare/Mac/SparkleShare.csproj +++ b/SparkleShare/Mac/SparkleShare.csproj @@ -1,7 +1,7 @@ - Release + Debug AnyCPU 10.0.0 2.0 @@ -33,6 +33,16 @@ Mac Developer 3rd Party Mac Developer Installer + + none + False + bin\Debug + 4 + False + False + False + False + diff --git a/SparkleShare/Mac/SparkleShare.sln b/SparkleShare/Mac/SparkleShare.sln index 039d38f4..9319b543 100644 --- a/SparkleShare/Mac/SparkleShare.sln +++ b/SparkleShare/Mac/SparkleShare.sln @@ -10,12 +10,19 @@ EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Release|Any CPU = Release|Any CPU + Debug|Any CPU = Debug|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution + {009FDCD7-1D57-4202-BB6D-8477D8C6B8EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {009FDCD7-1D57-4202-BB6D-8477D8C6B8EE}.Debug|Any CPU.Build.0 = Debug|Any CPU {009FDCD7-1D57-4202-BB6D-8477D8C6B8EE}.Release|Any CPU.ActiveCfg = Release|Any CPU {009FDCD7-1D57-4202-BB6D-8477D8C6B8EE}.Release|Any CPU.Build.0 = Release|Any CPU + {2C914413-B31C-4362-93C7-1AE34F09112A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2C914413-B31C-4362-93C7-1AE34F09112A}.Debug|Any CPU.Build.0 = Debug|Any CPU {2C914413-B31C-4362-93C7-1AE34F09112A}.Release|Any CPU.ActiveCfg = Release|Any CPU {2C914413-B31C-4362-93C7-1AE34F09112A}.Release|Any CPU.Build.0 = Release|Any CPU + {CF5BC8DB-A633-4FCC-8A3E-E3AC9B59FABC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CF5BC8DB-A633-4FCC-8A3E-E3AC9B59FABC}.Debug|Any CPU.Build.0 = Debug|Any CPU {CF5BC8DB-A633-4FCC-8A3E-E3AC9B59FABC}.Release|Any CPU.ActiveCfg = Release|Any CPU {CF5BC8DB-A633-4FCC-8A3E-E3AC9B59FABC}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection diff --git a/SparkleShare/SparkleControllerBase.cs b/SparkleShare/SparkleControllerBase.cs index d1c9adff..e8656ae9 100644 --- a/SparkleShare/SparkleControllerBase.cs +++ b/SparkleShare/SparkleControllerBase.cs @@ -563,6 +563,7 @@ namespace SparkleShare { this.fetcher.Complete (); string canonical_name = Path.GetFileNameWithoutExtension (this.fetcher.RemoteUrl.AbsolutePath); + canonical_name = canonical_name.Replace ("-crypto", ""); bool target_folder_exists = Directory.Exists ( Path.Combine (this.config.FoldersPath, canonical_name)); diff --git a/SparkleShare/SparkleSetupController.cs b/SparkleShare/SparkleSetupController.cs index e0237a3b..4c7ddbc5 100755 --- a/SparkleShare/SparkleSetupController.cs +++ b/SparkleShare/SparkleSetupController.cs @@ -341,6 +341,7 @@ namespace SparkleShare { public void AddPageCompleted (string address, string remote_path) { SyncingFolder = Path.GetFileNameWithoutExtension (remote_path); + SyncingFolder = SyncingFolder.Replace ("-crypto", ""); ProgressBarPercentage = 1.0; ChangePageEvent (PageType.Syncing, null); diff --git a/SparkleShare/Windows/SparkleSetup.cs b/SparkleShare/Windows/SparkleSetup.cs index 82ea8752..bd8bf62e 100644 --- a/SparkleShare/Windows/SparkleSetup.cs +++ b/SparkleShare/Windows/SparkleSetup.cs @@ -734,7 +734,7 @@ namespace SparkleShare { }; Button open_folder_button = new Button () { - Content = string.Format ("Open {0}", Path.GetFileName (Controller.PreviousPath)) + Content = "Show folder" }; if (warnings.Length > 0) {