1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
#region Disclaimer / License
// Copyright (C) 2011, Kenneth Skovhede
// http://www.hexad.dk, opensource@hexad.dk
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Duplicati.Library.Interface;
namespace Duplicati.Library.Encryption
{
/// <summary>
/// Implements a encryption/decryption with the GNU Privacy Guard (GPG)
/// </summary>
public class GPGEncryption : EncryptionBase
{
#region Commandline option constants
/// <summary>
/// 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>
/// 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>
/// 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>
/// 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";
#endregion
/// <summary>
/// Always present commandline options (--passphrase-fd 0)
/// </summary>
private const string GPG_COMMANDLINE_STANDARD_OPTIONS = "--batch --passphrase-fd 0";
/// <summary>
/// The options that are supplied as default when encrypting (default is empty)
/// </summary>
private const string GPG_ENCRYPTION_DEFAULT_OPTIONS = "";
/// <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)
/// </summary>
private const string GPG_ARMOR_OPTION = "--armor";
/// <summary>
/// The command used to signal encryption (--symmetric)
/// </summary>
private const string GPG_ENCRYPTION_COMMAND = "--symmetric";
/// <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>
private string m_programpath = "gpg";
/// <summary>
/// Commandline switches for encryption
/// </summary>
private string m_encryption_args;
/// <summary>
/// Commandline switches for decryption
/// </summary>
private string m_decryption_args;
/// <summary>
/// The key used for cryptographic operations
/// </summary>
private string m_key;
/// <summary>
/// Constructs a GPG instance for reading the interface values
/// </summary>
public GPGEncryption()
{
}
/// <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!
bool enableArmor = false;
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);
}
if (enableArmor)
{
//--armor is an option
m_encryption_args += GPG_ARMOR_OPTION;
m_decryption_args += GPG_ARMOR_OPTION;
}
//--passphrase-fd is an option
m_encryption_args += " " + GPG_COMMANDLINE_STANDARD_OPTIONS;
m_decryption_args += " " + GPG_COMMANDLINE_STANDARD_OPTIONS;
if (options.ContainsKey(COMMANDLINE_OPTIONS_ENCRYPTION_OPTIONS))
m_encryption_args += " " + options[COMMANDLINE_OPTIONS_ENCRYPTION_OPTIONS];
else
m_encryption_args += " " + GPG_ENCRYPTION_DEFAULT_OPTIONS;
if (options.ContainsKey(COMMANDLINE_OPTIONS_DECRYPTION_OPTIONS))
m_decryption_args += " " + options[COMMANDLINE_OPTIONS_DECRYPTION_OPTIONS];
else
m_decryption_args += " " + GPG_DECRYPTION_DEFAULT_OPTIONS;
//These are commands and thus inserted last
if (options.ContainsKey(COMMANDLINE_OPTIONS_ENCRYPTION_COMMAND))
m_encryption_args += " " + options[COMMANDLINE_OPTIONS_ENCRYPTION_COMMAND];
else
m_encryption_args += " " + GPG_ENCRYPTION_COMMAND;
if (options.ContainsKey(COMMANDLINE_OPTIONS_DECRYPTION_COMMAND))
m_decryption_args += " " + options[COMMANDLINE_OPTIONS_DECRYPTION_COMMAND];
else
m_decryption_args += " " + GPG_DECRYPTION_COMMAND;
if (options.ContainsKey(COMMANDLINE_OPTIONS_PATH))
m_programpath = Environment.ExpandEnvironmentVariables(options[COMMANDLINE_OPTIONS_PATH]);
}
#region IEncryption Members
public override string FilenameExtension { get { return "gpg"; } }
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; }
public override System.IO.Stream Encrypt(System.IO.Stream input)
{
return this.Execute(m_encryption_args, input, true);
}
public override System.IO.Stream Decrypt(System.IO.Stream input)
{
return this.Execute(m_decryption_args, input, false);
}
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, string.Format(Strings.GPGEncryption.Gpgencryptiondisablearmordeprecated, COMMANDLINE_OPTIONS_ENABLE_ARMOR)),
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, string.Format(Strings.GPGEncryption.GpgencryptionencryptioncommandLong, GPG_ENCRYPTION_COMMAND, "--encrypt"), GPG_ENCRYPTION_COMMAND),
new CommandLineArgument(COMMANDLINE_OPTIONS_DECRYPTION_COMMAND , CommandLineArgument.ArgumentType.String, Strings.GPGEncryption.GpgencryptiondecryptioncommandShort, Strings.GPGEncryption.GpgencryptiondecryptioncommandLong, GPG_DECRYPTION_COMMAND),
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
/// <summary>
/// Internal helper that wraps GPG usage
/// </summary>
/// <param name="args">The commandline arguments</param>
/// <param name="input">The input stream</param>
/// <param name="output">The output stream</param>
private System.IO.Stream Execute(string args, System.IO.Stream input, bool encrypt)
{
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.Arguments = args;
psi.CreateNoWindow = true;
psi.FileName = m_programpath;
psi.RedirectStandardError = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
#if DEBUG
psi.CreateNoWindow = false;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
Console.Error.WriteLine(string.Format("Running command: \"{0}\" {1}", m_programpath, args));
#endif
System.Diagnostics.Process p;
try
{
p = System.Diagnostics.Process.Start(psi);
p.StandardInput.WriteLine(m_key);
p.StandardInput.Flush();
System.Threading.Thread.Sleep(1000);
if (p.HasExited)
throw new Exception(p.StandardError.ReadToEnd());
}
catch (Exception ex)
{
throw new Exception(string.Format(Strings.GPGEncryption.GPGExecuteError, string.Format("\"{0}\" {1}", m_programpath, args), ex.Message), ex);
}
if (encrypt)
{
//Prevent blocking of the output buffer
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Runner));
t.Start(new object[] { p.StandardOutput.BaseStream, input });
return new GPGStreamWrapper(p, t, p.StandardInput.BaseStream);
}
else
{
//Prevent blocking of the input buffer
System.Threading.Thread 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);
}
}
/// <summary>
/// Copies the content of one stream into another, invoked as a thread
/// </summary>
/// <param name="x">An array with two stream instances</param>
private void Runner(object x)
{
//Unwrap arguments and read stream
object[] tmp = (object[])x;
Utility.Utility.CopyStream((Stream)tmp[0], (Stream)tmp[1]);
((Stream)tmp[1]).Close();
}
}
}
|