From ae5d6ffcc7f98ea4cd92320ad71c19c405b63828 Mon Sep 17 00:00:00 2001 From: verhoek <30193551+verhoek@users.noreply.github.com> Date: Sun, 23 Dec 2018 20:32:21 +0100 Subject: [PATCH] Added option to generate new priv/pub key pair without verifying the manifest. --- BuildTools/AutoUpdateBuilder/Program.cs | 32 +++++++++++++------ .../Library/AutoUpdater/AutoUpdateSettings.cs | 9 +++--- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/BuildTools/AutoUpdateBuilder/Program.cs b/BuildTools/AutoUpdateBuilder/Program.cs index d2df70e61..de961dea8 100644 --- a/BuildTools/AutoUpdateBuilder/Program.cs +++ b/BuildTools/AutoUpdateBuilder/Program.cs @@ -1,10 +1,24 @@ using System; using System.Collections.Generic; +using System.Security.Cryptography; namespace AutoUpdateBuilder { public class Program { + private static RSACryptoServiceProvider privkey; + + private static void CompareToManifestPublicKey() + { + if (Duplicati.Library.AutoUpdater.AutoUpdateSettings.SignKey == null || privkey.ToXmlString(false) != Duplicati.Library.AutoUpdater.AutoUpdateSettings.SignKey.ToXmlString(false)) + { + Console.WriteLine("The public key in the project is not the same as the public key from the file"); + Console.WriteLine("Try setting the key to: "); + Console.WriteLine(privkey.ToXmlString(false)); + System.Environment.Exit(5); + } + } + public static int Main(string[] _args) { var args = new List(_args); @@ -17,16 +31,18 @@ namespace AutoUpdateBuilder string keyfilepassword; string gpgkeyfile; string gpgpath; + string allowNewKey; opts.TryGetValue("input", out inputfolder); opts.TryGetValue("output", out outputfolder); + opts.TryGetValue("allow-new-key", out allowNewKey); opts.TryGetValue("keyfile", out keyfile); opts.TryGetValue("manifest", out manifestfile); opts.TryGetValue("keyfile-password", out keyfilepassword); opts.TryGetValue("gpgkeyfile", out gpgkeyfile); opts.TryGetValue("gpgpath", out gpgpath); - var usedoptions = new string[] { "input", "output", "keyfile", "manifest", "keyfile-password", "gpgkeyfile", "gpgpath" }; + var usedoptions = new string[] { "allow-new-key", "input", "output", "keyfile", "manifest", "keyfile-password", "gpgkeyfile", "gpgpath" }; if (string.IsNullOrWhiteSpace(inputfolder)) { @@ -61,7 +77,7 @@ namespace AutoUpdateBuilder if (!System.IO.File.Exists(keyfile)) { Console.WriteLine("Keyfile not found, creating new"); - var newkey = System.Security.Cryptography.RSACryptoServiceProvider.Create().ToXmlString(true); + var newkey = RSA.Create().ToXmlString(true); using (var enc = new Duplicati.Library.Encryption.AESEncryption(keyfilepassword, new Dictionary())) using (var fs = System.IO.File.OpenWrite(keyfile)) using (var ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(newkey))) @@ -71,7 +87,7 @@ namespace AutoUpdateBuilder if (!System.IO.Directory.Exists(outputfolder)) System.IO.Directory.CreateDirectory(outputfolder); - var privkey = (System.Security.Cryptography.RSACryptoServiceProvider)System.Security.Cryptography.RSACryptoServiceProvider.Create(); + privkey = (RSACryptoServiceProvider) RSA.Create(); using(var enc = new Duplicati.Library.Encryption.AESEncryption(keyfilepassword, new Dictionary())) using(var ms = new System.IO.MemoryStream()) @@ -84,16 +100,12 @@ namespace AutoUpdateBuilder privkey.FromXmlString(sr.ReadToEnd()); } - if (Duplicati.Library.AutoUpdater.AutoUpdateSettings.SignKey == null || privkey.ToXmlString(false) != Duplicati.Library.AutoUpdater.AutoUpdateSettings.SignKey.ToXmlString(false)) + if (!Boolean.TryParse(allowNewKey, out Boolean newKeyAllowed) || !newKeyAllowed) { - Console.WriteLine("The public key in the project is not the same as the public key from the file"); - Console.WriteLine("Try setting the key to: "); - Console.WriteLine(privkey.ToXmlString(false)); - return 5; + CompareToManifestPublicKey(); } - - string gpgkeyid = null; + string gpgkeyid = null; string gpgkeypassphrase = null; if (string.IsNullOrWhiteSpace(gpgkeyfile)) diff --git a/Duplicati/Library/AutoUpdater/AutoUpdateSettings.cs b/Duplicati/Library/AutoUpdater/AutoUpdateSettings.cs index aa5209756..7c4ec1289 100644 --- a/Duplicati/Library/AutoUpdater/AutoUpdateSettings.cs +++ b/Duplicati/Library/AutoUpdater/AutoUpdateSettings.cs @@ -66,11 +66,10 @@ namespace Duplicati.Library.AutoUpdater private static string ReadResourceText(string name, string oemname) { - string result; - if (_cache.TryGetValue(name, out result)) + // First try to read from _cache + if (_cache.TryGetValue(name, out string result)) return result; - try { using (var rd = new System.IO.StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(typeof(AutoUpdateSettings), name))) @@ -180,11 +179,11 @@ namespace Duplicati.Library.AutoUpdater public static System.Security.Cryptography.RSACryptoServiceProvider SignKey { - get + get { try { - var key = System.Security.Cryptography.RSACryptoServiceProvider.Create(); + var key = System.Security.Cryptography.RSA.Create(); key.FromXmlString(ReadResourceText(UPDATE_KEY, OEM_UPDATE_KEY)); return (System.Security.Cryptography.RSACryptoServiceProvider)key; }