Skip to content

Commit

Permalink
added support for gpg option --pinentry-mode loopback for gpg version…
Browse files Browse the repository at this point in the history
… 2.1 or higher

added ability for user to provider cli arguments for gpg
fixed nuget build script
  • Loading branch information
bentonstark committed Apr 4, 2018
1 parent b383be1 commit 8620b2f
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 15 deletions.
10 changes: 6 additions & 4 deletions Starksoft.Aspen.Test/Starksoft.Aspen.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\Microsoft.NET.Test.Sdk.15.6.1\build\net45\Microsoft.Net.Test.Sdk.props" Condition="Exists('..\packages\Microsoft.NET.Test.Sdk.15.6.1\build\net45\Microsoft.Net.Test.Sdk.props')" />
<Import Project="..\packages\NUnit3TestAdapter.3.10.0\build\net35\NUnit3TestAdapter.props" Condition="Exists('..\packages\NUnit3TestAdapter.3.10.0\build\net35\NUnit3TestAdapter.props')" />
<Import Project="..\packages\Microsoft.NET.Test.Sdk.15.7.0\build\net45\Microsoft.Net.Test.Sdk.props" Condition="Exists('..\packages\Microsoft.NET.Test.Sdk.15.7.0\build\net45\Microsoft.Net.Test.Sdk.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand Down Expand Up @@ -94,8 +95,9 @@
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Microsoft.NET.Test.Sdk.15.6.1\build\net45\Microsoft.Net.Test.Sdk.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.NET.Test.Sdk.15.6.1\build\net45\Microsoft.Net.Test.Sdk.props'))" />
<Error Condition="!Exists('..\packages\Microsoft.NET.Test.Sdk.15.6.1\build\net45\Microsoft.Net.Test.Sdk.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.NET.Test.Sdk.15.6.1\build\net45\Microsoft.Net.Test.Sdk.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.NET.Test.Sdk.15.7.0\build\net45\Microsoft.Net.Test.Sdk.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.NET.Test.Sdk.15.7.0\build\net45\Microsoft.Net.Test.Sdk.props'))" />
<Error Condition="!Exists('..\packages\Microsoft.NET.Test.Sdk.15.7.0\build\net45\Microsoft.Net.Test.Sdk.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.NET.Test.Sdk.15.7.0\build\net45\Microsoft.Net.Test.Sdk.targets'))" />
<Error Condition="!Exists('..\packages\NUnit3TestAdapter.3.10.0\build\net35\NUnit3TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit3TestAdapter.3.10.0\build\net35\NUnit3TestAdapter.props'))" />
</Target>
<Import Project="..\packages\Microsoft.NET.Test.Sdk.15.6.1\build\net45\Microsoft.Net.Test.Sdk.targets" Condition="Exists('..\packages\Microsoft.NET.Test.Sdk.15.6.1\build\net45\Microsoft.Net.Test.Sdk.targets')" />
<Import Project="..\packages\Microsoft.NET.Test.Sdk.15.7.0\build\net45\Microsoft.Net.Test.Sdk.targets" Condition="Exists('..\packages\Microsoft.NET.Test.Sdk.15.7.0\build\net45\Microsoft.Net.Test.Sdk.targets')" />
</Project>
4 changes: 2 additions & 2 deletions Starksoft.Aspen.Test/packages.config
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.CodeCoverage" version="1.0.3" targetFramework="net45" />
<package id="Microsoft.NET.Test.Sdk" version="15.6.1" targetFramework="net45" />
<package id="NUnit3TestAdapter" version="3.10.0" targetFramework="net45" />
<package id="NUnitTestAdapter" version="2.1.1" targetFramework="net45" />
</packages>
10 changes: 5 additions & 5 deletions Starksoft.Aspen/Ftps/FtpsListItemParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public class FtpsListItemParser : IFtpsItemParser
Regex _unixType = new Regex(@"(d|l|-|b|c|p|s)(?=(r|w|x|-|t|s){9})", RegexOptions.Compiled);

// dos and other expressions
Regex _dosName = new System.Text.RegularExpressions.Regex(@"((?<=<DIR>\s+).+)|((?<=\d\d:\d\d\s+).+)|((?<=(\d\d:\d\d(AM|PM|am|pm)\s+\d+\s+)).+)", RegexOptions.Compiled);
Regex _dosDate = new System.Text.RegularExpressions.Regex(@"(\d\d-\d\d-\d\d)", RegexOptions.Compiled);
Regex _dosTime = new System.Text.RegularExpressions.Regex(@"(\d\d:\d\d\s*(AM|PM|am|pm))|(\d\d:\d\d)", RegexOptions.Compiled);
Regex _dosSize = new System.Text.RegularExpressions.Regex(@"((?<=(\d\d:\d\d\s*(AM|PM|am|pm)\s*))\d+)|(\d+(?=\s+\d\d-\d\d-\d\d\s+))", RegexOptions.Compiled);
Regex _dosDir = new System.Text.RegularExpressions.Regex(@"<DIR>|\sDIR\s", RegexOptions.Compiled);
Regex _dosName = new Regex(@"((?<=<DIR>\s+).+)|((?<=\d\d:\d\d\s+).+)|((?<=(\d\d:\d\d(AM|PM|am|pm)\s+\d+\s+)).+)", RegexOptions.Compiled);
Regex _dosDate = new Regex(@"(\d\d-\d\d-\d\d)", RegexOptions.Compiled);
Regex _dosTime = new Regex(@"(\d\d:\d\d\s*(AM|PM|am|pm))|(\d\d:\d\d)", RegexOptions.Compiled);
Regex _dosSize = new Regex(@"((?<=(\d\d:\d\d\s*(AM|PM|am|pm)\s*))\d+)|(\d+(?=\s+\d\d-\d\d-\d\d\s+))", RegexOptions.Compiled);
Regex _dosDir = new Regex(@"<DIR>|\sDIR\s", RegexOptions.Compiled);


/// <summary>
Expand Down
36 changes: 35 additions & 1 deletion Starksoft.Aspen/GnuPG/Gpg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public class Gpg : IDisposable
private string _filename;
private long _copyBufferSize = DEFAULT_COPY_BUFFER_SIZE;
private bool _ignoreErrors;
private string _userOptions;

private Stream _outputStream;
private Stream _errorStream;
Expand Down Expand Up @@ -324,6 +325,17 @@ public bool IgnoreErrors
set { _ignoreErrors = value; }
}

/// <summary>
/// Gets or sets user specified option GPG CLI argument string for any additional GPG options
/// that need to be specified by the user.
/// </summary>
/// <value>String containing GPGP user command line options.</value>
public string UserOptions
{
get { return _userOptions; }
set { _userOptions = value; }
}

/// <summary>
/// Sign + encrypt data using the gpg executable with --sign arg. Input data is provide via a stream. Output
/// data is returned as a stream. Note that MemoryStream is supported for use.
Expand Down Expand Up @@ -483,6 +495,17 @@ public string Import(Stream inputStream)
throw new GpgException("Unable to identify name of imported key. Possible import error or unrecognized text output.");
}

/// <summary>
/// Gets the GPG binary version number as reported by the executable.
/// </summary>
/// <returns>GPG version number in the string format x.y.z</returns>
public GpgVersion GetGpgVersion()
{
GpgVersion ver = GpgVersionParser.Parse(ExecuteGpgNoIO("--version"));
return ver;
}


/// <summary>
/// Executes the gpg program as a single command without input or output streams.
/// This method is used to report data back from gpg such as key list information.
Expand Down Expand Up @@ -562,7 +585,14 @@ private string GetGpgArgs(ActionTypes action)

// tell gpg to read the passphrase from the standard input so we can automate providing it
options.Append("--passphrase-fd 0 ");


// if gpg cli version is >= 2.1 then instruct gpg not to prompt for a password
GpgVersion ver = GetGpgVersion();
if (ver.Major >= 2 && ver.Minor >= 1)
{
options.Append("--pinentry-mode loopback ");
}

// turn off verbose statements
options.Append("--no-verbose ");

Expand All @@ -580,6 +610,10 @@ private string GetGpgArgs(ActionTypes action)
if (!String.IsNullOrEmpty(_recipient))
options.Append(String.Format(CultureInfo.InvariantCulture, "--recipient {0} ", _recipient));

// add any user specific options if provided
if (!String.IsNullOrEmpty(_userOptions))
options.Append(_userOptions);

// handle the action
switch (action)
{
Expand Down
52 changes: 52 additions & 0 deletions Starksoft.Aspen/GnuPG/GpgVersion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Starksoft.Aspen.GnuPG
{
/// <summary>
/// GPG version number class.
/// </summary>
public class GpgVersion
{
private int _major;
private int _minor;
private string _revision;

public GpgVersion(int major, int minor, string revision)
{
_major = major;
_minor = minor;
_revision = revision;
}

/// <summary>
/// Gets the GPG major version number.
/// </summary>
/// <returns>Major version number</returns>
public int Major
{
get { return _major; }
}

/// <summary>
/// Gets the GPG minor version number.
/// </summary>
/// <returns>Minor version number</returns>
public int Minor
{
get { return _minor; }
}

/// <summary>
/// Gets the GPG revision string.
/// </summary>
/// <returns>Revision string</returns>
public string Revision
{
get { return _revision; }
}
}
}
65 changes: 65 additions & 0 deletions Starksoft.Aspen/GnuPG/GpgVersionParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Starksoft.Aspen.GnuPG
{
/// <summary>
/// GPG command line version parser. This class parses the GPG stdout data in the expected format
/// gpg GnuPG major.minor.revision
/// where major and minor are expected to be integers and revsision can be an integer or string.
///
/// Examples:
/// gpg (GnuPG) 2.2.4
/// gpg (GnuPG) 2.1.1-beta3
/// </summary>
class GpgVersionParser
{
private static Regex _gpgVersion = new Regex(@"(?<=gpg \(GnuPG\) )([0-9]*\.[0-9]*\..*)", RegexOptions.Compiled);

/// <summary>
/// Private constructor.
/// </summary>
private GpgVersionParser() { }

/// <summary>
/// Parse the GPG version information from the Stdout of the GPG command line interface
/// executed with the --version argument.
/// </summary>
/// <param name="data">stdout stream data</param>
/// <returns>GPG version object</returns>
public static GpgVersion Parse(StreamReader data)
{
string firstLine = ReadFirstStdOutLine(data);
GpgVersion ver = ParseVersion(firstLine);
return ver;
}

private static string ReadFirstStdOutLine(StreamReader data)
{
return data.ReadLine();
}

private static GpgVersion ParseVersion(string line) {

if (!_gpgVersion.IsMatch(line))
{
throw new GpgException(String.Format("unexpected gpg command line version data '{0}'", line));
}

string[] version = _gpgVersion.Match(line).ToString().Split('.');
int major = Int32.Parse(version[0]);
int minor = Int32.Parse(version[1]);
string revision = version[2];

return new GpgVersion(major, minor, revision);
}

}


}
4 changes: 2 additions & 2 deletions Starksoft.Aspen/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
[assembly: AssemblyCopyright("Copyright (c) 2018 Benton Stark")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("1.1.4.0")]
[assembly: AssemblyFileVersion("1.1.4.0")]
[assembly: AssemblyVersion("1.1.5.0")]
[assembly: AssemblyFileVersion("1.1.5.0")]
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyName("")]

Expand Down
2 changes: 2 additions & 0 deletions Starksoft.Aspen/Starksoft.Aspen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
<Compile Include="ArrayBuilder.cs" />
<Compile Include="ArrayUtils.cs" />
<Compile Include="Crc16.cs" />
<Compile Include="GnuPG\GpgVersion.cs" />
<Compile Include="GnuPG\GpgVersionParser.cs" />
<Compile Include="Lrc.cs" />
<Compile Include="Ftps\EventArgs\ConnectionClosedEventArgs.cs" />
<Compile Include="Ftps\Exceptions\FtpsAsynchronousOperationException.cs" />
Expand Down
6 changes: 5 additions & 1 deletion Starksoft.Aspen/Starksoft.Aspen.nuspec
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>starksoft.aspen</id>
<version>1.1.4</version>
<version>1.1.5</version>
<authors>Benton Stark</authors>
<owners>Benton Stark</owners>
<licenseUrl>https://github.com/bentonstark/starksoft-aspen/blob/master/Starksoft.Aspen/COPYING.LESSER</licenseUrl>
Expand All @@ -15,4 +15,8 @@
<dependencies>
</dependencies>
</metadata>
<files>
<file src="bin\Release\starksoft.aspen.dll" target="lib\net45" />
<file src="bin\Release\starksoft.aspen.pdb" target="lib\net45" />
</files>
</package>
Empty file modified Starksoft.Aspen/build_nupkg.bat
100755 → 100644
Empty file.
Binary file added Starksoft.Aspen/starksoft.aspen.1.1.5.nupkg
Binary file not shown.

0 comments on commit 8620b2f

Please sign in to comment.