Updated the SSH KeyGen to be more structured

This commit is contained in:
Kenneth Skovhede
2014-03-12 10:21:28 +01:00
parent 9bb1787947
commit 421cba6f71
4 changed files with 270 additions and 16 deletions
@@ -56,6 +56,9 @@
<DependentUpon>SSHv2Backend.resx</DependentUpon>
</Compile>
<Compile Include="KeyGenerator.cs" />
<Compile Include="Strings\KeyGenerator.Designer.cs">
<DependentUpon>KeyGenerator.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Interface\Duplicati.Library.Interface.csproj">
@@ -75,6 +78,10 @@
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>SSHv2Backend.Designer.cs</LastGenOutput>
</EmbeddedResource>
<EmbeddedResource Include="Strings\KeyGenerator.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>KeyGenerator.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+22 -16
View File
@@ -25,7 +25,6 @@ namespace Duplicati.Library.Backend
{
public class KeyGenerator : IWebModule
{
private const string GENERATE_COMMAND = "generate-keys";
private const string KEY_TYPE_NAME = "key-type";
private const string KEY_USERNAME = "key-username";
private const string KEY_KEYLEN = "key-bits";
@@ -35,6 +34,13 @@ namespace Duplicati.Library.Backend
private const string PUB_KEY_FORMAT_DSA = "ssh-dss";
private const string PUB_KEY_FORMAT_RSA = "ssh-rsa";
private const string KEYTYPE_DSA = "dsa";
private const string KEYTYPE_RSA = "rsa";
private readonly string DEFAULT_USERNAME = "backup-user@" + System.Environment.MachineName;
private const string DEFAULT_KEYTYPE = KEYTYPE_DSA;
private const int DEFAULT_KEYLEN = 1024;
private static void EncodePEMLength(System.IO.Stream s, uint length)
{
s.WriteByte((byte)((length >> 24) & 0xff));
@@ -103,18 +109,15 @@ namespace Duplicati.Library.Backend
}
#region IWebModule implementation
public IDictionary<string, string> Execute(string command, IDictionary<string, string> options)
{
if (!GENERATE_COMMAND.Equals(command, StringComparison.InvariantCultureIgnoreCase))
throw new Exception(string.Format("Invalid command: {0}", command));
public IDictionary<string, string> Execute(IDictionary<string, string> options)
{
string keytype;
if (!options.TryGetValue(KEY_TYPE_NAME, out keytype))
keytype = "dsa";
keytype = DEFAULT_KEYTYPE;
string username;
if (!options.TryGetValue(KEY_USERNAME, out username))
username = "someone@example.com";
username = DEFAULT_USERNAME;
string keylen_s;
if (!options.TryGetValue(KEY_KEYLEN, out keylen_s))
@@ -122,15 +125,15 @@ namespace Duplicati.Library.Backend
int keylen;
if (!int.TryParse(keylen_s, out keylen))
keylen = 0;
keylen = DEFAULT_KEYLEN;
if ("rsa".Equals(keytype, StringComparison.InvariantCultureIgnoreCase))
if (KEYTYPE_RSA.Equals(keytype, StringComparison.InvariantCultureIgnoreCase))
{
var rsa = RSACryptoServiceProvider.Create();
if (keylen > 0)
rsa.KeySize = keylen;
else
rsa.KeySize = 1024;
rsa.KeySize = DEFAULT_KEYLEN;
var key = rsa.ExportParameters(true);
@@ -151,14 +154,15 @@ namespace Duplicati.Library.Backend
res["pubkey"] = PUB_KEY_FORMAT_RSA + " " + Convert.ToBase64String(pubkey) + " " + username;
return res;
}
else if ("dsa".Equals(keytype, StringComparison.InvariantCultureIgnoreCase))
else if (KEYTYPE_DSA.Equals(keytype, StringComparison.InvariantCultureIgnoreCase))
{
var dsa = DSACryptoServiceProvider.Create();
if (keylen > 0)
dsa.KeySize = keylen;
else
dsa.KeySize = 1024;
dsa.KeySize = DEFAULT_KEYLEN;
var key = dsa.ExportParameters(true);
var privateEntries = new byte[][] { new byte[] { 0x0 }, key.P, key.Q, key.G, key.Y, key.X };
@@ -187,14 +191,16 @@ namespace Duplicati.Library.Backend
}
public string Key { get { return "ssh-keygen"; } }
public string DisplayName { get { return "SSH Keygenerator"; } }
public string Description { get { return "Module for generating SSH private/public keys"; } }
public string DisplayName { get { return Strings.KeyGenerator.DisplayName; } }
public string Description { get { return Strings.KeyGenerator.Description; } }
public IList<ICommandLineArgument> SupportedCommands
{
get
{
return new List<ICommandLineArgument>(new ICommandLineArgument[] {
new CommandLineArgument(GENERATE_COMMAND)
new CommandLineArgument(KEY_KEYLEN, CommandLineArgument.ArgumentType.Integer, Strings.KeyGenerator.KeyLenShort, Strings.KeyGenerator.KeyLenLong, DEFAULT_KEYLEN.ToString(), null, new string[] {"1024", "2048"}),
new CommandLineArgument(KEY_TYPE_NAME, CommandLineArgument.ArgumentType.Enumeration, Strings.KeyGenerator.KeyTypeShort, Strings.KeyGenerator.KeyTypeLong, DEFAULT_KEYTYPE, null, new string[] {KEYTYPE_DSA, KEYTYPE_RSA}),
new CommandLineArgument(KEY_USERNAME, CommandLineArgument.ArgumentType.Integer, Strings.KeyGenerator.KeyUsernameShort, Strings.KeyGenerator.KeyUsernameLong, DEFAULT_USERNAME),
});
}
}
@@ -0,0 +1,97 @@
// ------------------------------------------------------------------------------
// <autogenerated>
// This code was generated by a tool.
// Mono Runtime Version: 4.0.30319.17020
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </autogenerated>
// ------------------------------------------------------------------------------
namespace Duplicati.Library.Backend.Strings {
using System;
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class KeyGenerator {
private static System.Resources.ResourceManager resourceMan;
private static System.Globalization.CultureInfo resourceCulture;
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal KeyGenerator() {
}
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
internal static System.Resources.ResourceManager ResourceManager {
get {
if (object.Equals(null, resourceMan)) {
System.Resources.ResourceManager temp = new System.Resources.ResourceManager("Duplicati.Library.Backend.Strings.KeyGenerator", typeof(KeyGenerator).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
internal static System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
internal static string KeyLenShort {
get {
return ResourceManager.GetString("KeyLenShort", resourceCulture);
}
}
internal static string KeyTypeShort {
get {
return ResourceManager.GetString("KeyTypeShort", resourceCulture);
}
}
internal static string Description {
get {
return ResourceManager.GetString("Description", resourceCulture);
}
}
internal static string DisplayName {
get {
return ResourceManager.GetString("DisplayName", resourceCulture);
}
}
internal static string KeyTypeLong {
get {
return ResourceManager.GetString("KeyTypeLong", resourceCulture);
}
}
internal static string KeyUsernameShort {
get {
return ResourceManager.GetString("KeyUsernameShort", resourceCulture);
}
}
internal static string KeyLenLong {
get {
return ResourceManager.GetString("KeyLenLong", resourceCulture);
}
}
internal static string KeyUsernameLong {
get {
return ResourceManager.GetString("KeyUsernameLong", resourceCulture);
}
}
}
}
@@ -0,0 +1,144 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Description" xml:space="preserve">
<value>Module for generating SSH private/public keys</value>
</data>
<data name="DisplayName" xml:space="preserve">
<value>SSH Key Generator</value>
</data>
<data name="KeyUsernameShort" xml:space="preserve">
<value>Public key username</value>
</data>
<data name="KeyUsernameLong" xml:space="preserve">
<value>A username to append to the public key</value>
</data>
<data name="KeyTypeShort" xml:space="preserve">
<value>The key type</value>
</data>
<data name="KeyTypeLong" xml:space="preserve">
<value>Determines the type of key to generate</value>
</data>
<data name="KeyLenShort" xml:space="preserve">
<value>The key length</value>
</data>
<data name="KeyLenLong" xml:space="preserve">
<value>The length of the key in bits</value>
</data>
</root>