Files
duplicati/Duplicati/Library/Encryption/GPGEncryption.cs
T

292 lines
14 KiB
C#
Raw Normal View History

2024-02-28 15:45:30 +01:00
// Copyright (C) 2024, The Duplicati Team
// https://duplicati.com, hello@duplicati.com
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
2009-01-14 17:29:30 +00:00
using System;
using System.Collections.Generic;
using System.IO;
2010-06-19 21:08:21 +00:00
using Duplicati.Library.Interface;
2019-07-25 11:14:45 -04:00
using Duplicati.Library.Common;
using Duplicati.Library.Logging;
using Duplicati.Library.Utility;
2009-01-14 17:29:30 +00:00
namespace Duplicati.Library.Encryption
{
/// <summary>
/// Implements a encryption/decryption with the GNU Privacy Guard (GPG)
/// </summary>
2013-05-05 17:07:24 +02:00
public class GPGEncryption : EncryptionBase
2009-01-14 17:29:30 +00:00
{
private static readonly string LOGTAG = Logging.Log.LogTagFromType<GPGEncryption>();
2010-06-19 21:08:21 +00:00
#region Commandline option constants
2009-01-14 17:29:30 +00:00
/// <summary>
2010-06-19 21:08:21 +00:00
/// The commandline option supplied if armor should be disabled (--gpg-encryption-disable-armor)
/// </summary>
private const string COMMANDLINE_OPTIONS_DISABLE_ARMOR = "gpg-encryption-disable-armor";
/// <summary>
2011-09-27 06:59:17 +00:00
/// The commandline option supplied if armor should be enabled (--gpg-encryption-enable-armor)
/// </summary>
private const string COMMANDLINE_OPTIONS_ENABLE_ARMOR = "gpg-encryption-enable-armor";
/// <summary>
2010-06-19 21:08:21 +00:00
/// The commandline option supplied if the encryption should have non-default switches (--gpg-encryption-switches)
/// </summary>
private const string COMMANDLINE_OPTIONS_ENCRYPTION_OPTIONS = "gpg-encryption-switches";
/// <summary>
/// The commandline option supplied if the decryption should have non-default switches (--gpg-decryption-switches)
/// </summary>
private const string COMMANDLINE_OPTIONS_DECRYPTION_OPTIONS = "gpg-decryption-switches";
/// <summary>
/// The commandline option supplied, indicating the path to the GPG program executable (--gpg-program-path)
/// </summary>
public const string COMMANDLINE_OPTIONS_PATH = "gpg-program-path";
/// <summary>
2013-02-24 13:26:17 +00:00
/// The commandline option that changes the default encryption command (--gpg-encryption-command)
/// </summary>
private const string COMMANDLINE_OPTIONS_ENCRYPTION_COMMAND = "gpg-encryption-command";
/// <summary>
/// The commandline option that changes the default encryption command (--gpg-decryption-command)
/// </summary>
private const string COMMANDLINE_OPTIONS_DECRYPTION_COMMAND = "gpg-decryption-command";
2010-06-19 21:08:21 +00:00
#endregion
/// <summary>
/// Always present commandline options (--passphrase-fd 0)
/// </summary>
2012-01-10 08:40:08 +00:00
private const string GPG_COMMANDLINE_STANDARD_OPTIONS = "--batch --passphrase-fd 0";
2010-06-19 21:08:21 +00:00
/// <summary>
2013-02-24 13:26:17 +00:00
/// The options that are supplied as default when encrypting (default is empty)
2010-06-19 21:08:21 +00:00
/// </summary>
2013-02-24 13:26:17 +00:00
private const string GPG_ENCRYPTION_DEFAULT_OPTIONS = "";
2010-06-19 21:08:21 +00:00
/// <summary>
/// The options that are supplied as default when encrypting (default is empty)
/// </summary>
private const string GPG_DECRYPTION_DEFAULT_OPTIONS = "";
/// <summary>
/// The commandline option that specifies that armor is used (--armor)
2009-01-14 17:29:30 +00:00
/// </summary>
2010-06-19 21:08:21 +00:00
private const string GPG_ARMOR_OPTION = "--armor";
2009-01-14 17:29:30 +00:00
/// <summary>
2013-02-24 13:26:17 +00:00
/// The command used to signal encryption (--symmetric)
2009-01-14 17:29:30 +00:00
/// </summary>
2013-02-24 13:26:17 +00:00
private const string GPG_ENCRYPTION_COMMAND = "--symmetric";
2019-07-26 09:18:16 -04:00
2010-06-19 21:08:21 +00:00
/// <summary>
/// The command used to signal decryption (--decrypt)
/// </summary>
private const string GPG_DECRYPTION_COMMAND = "--decrypt";
/// <summary>
/// The PGP program to use, should be with absolute path
/// </summary>
2019-07-25 11:14:45 -04:00
private string m_programpath { get; set; } = GetGpgProgramPath();
2009-01-14 17:29:30 +00:00
/// <summary>
/// Commandline switches for encryption
/// </summary>
private readonly string m_encryption_args;
2009-01-14 17:29:30 +00:00
/// <summary>
/// Commandline switches for decryption
/// </summary>
private readonly string m_decryption_args;
2009-01-14 17:29:30 +00:00
/// <summary>
/// The key used for cryptographic operations
/// </summary>
private string m_key;
/// <summary>
2010-06-19 21:08:21 +00:00
/// Constructs a GPG instance for reading the interface values
2009-01-14 17:29:30 +00:00
/// </summary>
2010-06-19 21:08:21 +00:00
public GPGEncryption()
2009-01-14 17:29:30 +00:00
{
2010-06-19 21:08:21 +00:00
}
/// <summary>
/// Constructs a new GPG instance
/// </summary>
/// <param name="options">The options passed on the commandline</param>
/// <param name="passphrase">The passphrase to be used for encryption</param>
public GPGEncryption(string passphrase, Dictionary<string, string> options)
{
m_key = passphrase;
//NOTE: For reasons unknown, GPG commandline options are divided into "options" and "commands".
//NOTE: The "options" must be placed before "commands" or it wont work!
2011-09-27 06:59:17 +00:00
bool enableArmor = false;
2011-09-27 05:35:49 +00:00
2011-09-27 06:59:17 +00:00
if (options.ContainsKey(COMMANDLINE_OPTIONS_ENABLE_ARMOR))
{
enableArmor = Utility.Utility.ParseBoolOption(options, COMMANDLINE_OPTIONS_ENABLE_ARMOR);
}
else
{
//Special handling of this option, it should have been --enable-armor instead,
//so this is now deprecated
if (options.ContainsKey(COMMANDLINE_OPTIONS_DISABLE_ARMOR))
enableArmor = !Utility.Utility.ParseBoolOption(options, COMMANDLINE_OPTIONS_DISABLE_ARMOR);
}
2010-06-19 21:08:21 +00:00
2011-09-27 06:59:17 +00:00
if (enableArmor)
2010-06-19 21:08:21 +00:00
{
//--armor is an option
m_encryption_args += GPG_ARMOR_OPTION;
m_decryption_args += GPG_ARMOR_OPTION;
}
2018-11-19 09:40:33 +01:00
var encryptOptions = options.ContainsKey(COMMANDLINE_OPTIONS_ENCRYPTION_OPTIONS) ? options[COMMANDLINE_OPTIONS_ENCRYPTION_OPTIONS] : GPG_ENCRYPTION_DEFAULT_OPTIONS;
var encryptCmd = options.ContainsKey(COMMANDLINE_OPTIONS_ENCRYPTION_COMMAND) ? options[COMMANDLINE_OPTIONS_ENCRYPTION_COMMAND] : GPG_ENCRYPTION_COMMAND;
2019-07-26 09:18:16 -04:00
m_encryption_args += " " + GPG_COMMANDLINE_STANDARD_OPTIONS + " " + encryptOptions + " " + encryptCmd;
2010-06-19 21:08:21 +00:00
2018-11-19 09:40:33 +01:00
var decryptOptions = options.ContainsKey(COMMANDLINE_OPTIONS_DECRYPTION_OPTIONS) ? options[COMMANDLINE_OPTIONS_DECRYPTION_OPTIONS] : GPG_DECRYPTION_DEFAULT_OPTIONS;
var decryptCmd = options.ContainsKey(COMMANDLINE_OPTIONS_DECRYPTION_COMMAND) ? options[COMMANDLINE_OPTIONS_DECRYPTION_COMMAND] : GPG_DECRYPTION_COMMAND;
2019-07-26 09:18:16 -04:00
m_decryption_args += " " + GPG_COMMANDLINE_STANDARD_OPTIONS + " " + decryptOptions + " " + decryptCmd;
2013-02-24 13:26:17 +00:00
2010-06-19 21:08:21 +00:00
if (options.ContainsKey(COMMANDLINE_OPTIONS_PATH))
m_programpath = Environment.ExpandEnvironmentVariables(options[COMMANDLINE_OPTIONS_PATH]);
2010-06-19 21:08:21 +00:00
2009-01-14 17:29:30 +00:00
}
#region IEncryption Members
2009-01-25 19:33:36 +00:00
public override string FilenameExtension { get { return "gpg"; } }
2010-06-19 21:08:21 +00:00
public override string Description { get { return Strings.GPGEncryption.Description; } }
public override string DisplayName { get { return Strings.GPGEncryption.DisplayName; } }
protected override void Dispose(bool disposing) { m_key = null; }
2009-01-25 19:33:36 +00:00
public override System.IO.Stream Encrypt(System.IO.Stream input)
2009-01-14 17:29:30 +00:00
{
2010-06-19 21:08:21 +00:00
return this.Execute(m_encryption_args, input, true);
2009-01-14 17:29:30 +00:00
}
2009-01-25 19:33:36 +00:00
public override System.IO.Stream Decrypt(System.IO.Stream input)
2009-01-14 17:29:30 +00:00
{
2010-06-19 21:08:21 +00:00
return this.Execute(m_decryption_args, input, false);
2009-01-14 17:29:30 +00:00
}
2010-06-19 21:08:21 +00:00
public override IList<ICommandLineArgument> SupportedCommands
{
get
{
return new List<ICommandLineArgument>(new ICommandLineArgument[] {
new CommandLineArgument(COMMANDLINE_OPTIONS_DISABLE_ARMOR, CommandLineArgument.ArgumentType.Boolean, Strings.GPGEncryption.GpgencryptiondisablearmorShort, Strings.GPGEncryption.GpgencryptiondisablearmorLong, "true", null, null, Strings.GPGEncryption.Gpgencryptiondisablearmordeprecated(COMMANDLINE_OPTIONS_ENABLE_ARMOR)),
2011-09-27 06:59:17 +00:00
new CommandLineArgument(COMMANDLINE_OPTIONS_ENABLE_ARMOR, CommandLineArgument.ArgumentType.Boolean, Strings.GPGEncryption.GpgencryptionenablearmorShort, Strings.GPGEncryption.GpgencryptionenablearmorLong, "false"),
new CommandLineArgument(COMMANDLINE_OPTIONS_ENCRYPTION_COMMAND , CommandLineArgument.ArgumentType.String, Strings.GPGEncryption.GpgencryptionencryptioncommandShort, Strings.GPGEncryption.GpgencryptionencryptioncommandLong(GPG_ENCRYPTION_COMMAND, "--encrypt"), GPG_ENCRYPTION_COMMAND),
2013-02-24 13:26:17 +00:00
new CommandLineArgument(COMMANDLINE_OPTIONS_DECRYPTION_COMMAND , CommandLineArgument.ArgumentType.String, Strings.GPGEncryption.GpgencryptiondecryptioncommandShort, Strings.GPGEncryption.GpgencryptiondecryptioncommandLong, GPG_DECRYPTION_COMMAND),
2010-06-19 21:08:21 +00:00
new CommandLineArgument(COMMANDLINE_OPTIONS_ENCRYPTION_OPTIONS , CommandLineArgument.ArgumentType.String, Strings.GPGEncryption.GpgencryptionencryptionswitchesShort, Strings.GPGEncryption.GpgencryptionencryptionswitchesLong, GPG_ENCRYPTION_DEFAULT_OPTIONS),
new CommandLineArgument(COMMANDLINE_OPTIONS_DECRYPTION_OPTIONS, CommandLineArgument.ArgumentType.String, Strings.GPGEncryption.GpgencryptiondecryptionswitchesShort, Strings.GPGEncryption.GpgencryptiondecryptionswitchesLong, GPG_DECRYPTION_DEFAULT_OPTIONS),
new CommandLineArgument(COMMANDLINE_OPTIONS_PATH, CommandLineArgument.ArgumentType.Path, Strings.GPGEncryption.GpgprogrampathShort, Strings.GPGEncryption.GpgprogrampathLong),
});
}
}
#endregion
2019-07-26 09:18:16 -04:00
2009-01-14 17:29:30 +00:00
/// <summary>
/// Internal helper that wraps GPG usage
/// </summary>
/// <param name="args">The commandline arguments</param>
/// <param name="input">The input stream</param>
2009-01-25 19:33:36 +00:00
private System.IO.Stream Execute(string args, System.IO.Stream input, bool encrypt)
2009-01-14 17:29:30 +00:00
{
var psi = new System.Diagnostics.ProcessStartInfo
{
2018-06-12 09:26:52 +02:00
Arguments = args,
CreateNoWindow = true,
FileName = m_programpath,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
2018-06-12 09:26:52 +02:00
};
2009-01-14 17:29:30 +00:00
2009-01-25 19:33:36 +00:00
#if DEBUG
psi.CreateNoWindow = false;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
2020-01-25 17:07:02 -08:00
Console.Error.WriteLine("Running command: {0} {1}", m_programpath, args);
2009-01-25 19:33:36 +00:00
#endif
System.Diagnostics.Process p;
try
{
p = System.Diagnostics.Process.Start(psi);
p.StandardInput.WriteLine(m_key);
p.StandardInput.Flush();
2010-06-19 21:08:21 +00:00
System.Threading.Thread.Sleep(1000);
if (p.HasExited)
throw new Exception(p.StandardError.ReadToEnd());
2009-01-25 19:33:36 +00:00
}
catch (Exception ex)
{
Logging.Log.WriteErrorMessage(LOGTAG, "GPGEncryptFailure", ex, "Error occurred while encrypting with GPG:" + ex.Message);
throw new Exception(Strings.GPGEncryption.GPGExecuteError(m_programpath, args, ex.Message), ex);
2009-01-25 19:33:36 +00:00
}
System.Threading.Thread t;
2009-01-25 19:33:36 +00:00
if (encrypt)
{
//Prevent blocking of the output buffer
t = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Runner));
2009-01-25 19:33:36 +00:00
t.Start(new object[] { p.StandardOutput.BaseStream, input });
return new GPGStreamWrapper(p, t, p.StandardInput.BaseStream);
}
//Prevent blocking of the input buffer
t = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Runner));
t.Start(new object[] { input, p.StandardInput.BaseStream });
return new GPGStreamWrapper(p, t, p.StandardOutput.BaseStream);
2009-01-14 17:29:30 +00:00
}
2010-05-12 08:41:54 +00:00
/// <summary>
/// Copies the content of one stream into another, invoked as a thread
/// </summary>
/// <param name="x">An array with two stream instances</param>
2009-01-14 17:29:30 +00:00
private void Runner(object x)
{
//Unwrap arguments and read stream
object[] tmp = (object[])x;
Utility.Utility.CopyStream((Stream)tmp[0], (Stream)tmp[1]);
2009-01-25 19:33:36 +00:00
((Stream)tmp[1]).Close();
2009-01-14 17:29:30 +00:00
}
/// <summary>
2019-07-25 11:14:45 -04:00
/// Determines the path to the GPG program
/// </summary>
2019-07-25 11:14:45 -04:00
public static string GetGpgProgramPath()
{
2019-07-25 11:14:45 -04:00
Log.WriteInformationMessage("GetGpgProgramPath", "gpg", Platform.IsClientWindows ? WinTools.GetWindowsGpgExePath() : "gpg");
// for Windows return the full path, otherwise just return "gpg"
return Platform.IsClientWindows ? WinTools.GetWindowsGpgExePath() : "gpg";
}
2009-01-14 17:29:30 +00:00
}
}