Skip to content

Commit

Permalink
Add possibility for key analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Vratislav committed Jul 29, 2020
1 parent 7fdad5d commit be61ddb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ This command has two drawbacks
I have written a .NET utility that overcomes the aforementioned drawbacks. It computes the container name from the PFX file (if not specified) and accepts the password as a parameter.

```
SnInstallPfx.exe <pfx_infile> <pfx_password>
SnInstallPfx.exe <pfx_infile> <pfx_password> <container_name>
SnInstallPfx.exe <pfx_infile> // show information about the pfx_infile
SnInstallPfx.exe <pfx_infile> <pfx_password> // install the pfx_infile
SnInstallPfx.exe <pfx_infile> <pfx_password> <container_name> // install the pfx_infile under container_name
```
The hash computing is copied from the MSBuild source code on GitHub.

Expand Down
27 changes: 19 additions & 8 deletions src/SnInstallPfx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using MSBuildCode;

namespace SnInstallPfx
{
// Utility to replace the sn.exe -i command that does not accepts password.
Expand All @@ -12,25 +12,35 @@ public static class SnInstallPfx
static int Main(string[] args)
{
// params and usage
if (args.Length == 0 || args[0] == "?" || args[0] == "-?" || (args.Length != 2 && args.Length != 3))
if (args.Length == 0 || args[0] == "?" || args[0] == "-?" || (args.Length != 1 && args.Length != 2 && args.Length != 3))
{
Console.WriteLine("By Honzajscz at 2019");
Console.WriteLine($"By Honzajscz at {DateTime.Now.Year}");
Console.WriteLine($"https://github.com/honzajscz/SnInstallPfx");
Console.WriteLine();
Console.WriteLine("Installs key pair from <pfx_infile> into a key container compatible for MSBuild.");
Console.WriteLine("This utility is an alternative for command sn.exe -i <infile> <container>.");
Console.WriteLine("It accepts password from command line and automatically generates a container name for <pxf_infile> if no container name is specified via the <container_name> argument.");
Console.WriteLine();
Console.WriteLine("Usage:");
Console.WriteLine($"{Assembly.GetEntryAssembly().GetName().Name}.exe <pfx_infile> <pfx_password>");
Console.WriteLine($"{Assembly.GetEntryAssembly().GetName().Name}.exe <pfx_infile> <pfx_password> <container_name>");
Console.WriteLine($"{Assembly.GetEntryAssembly().GetName().Name}.exe <pfx_infile> // show information about the pfx_infile");
Console.WriteLine($"{Assembly.GetEntryAssembly().GetName().Name}.exe <pfx_infile> <pfx_password> // install the pfx_infile");
Console.WriteLine($"{Assembly.GetEntryAssembly().GetName().Name}.exe <pfx_infile> <pfx_password> <container_name> // install the pfx_infile under container_name");
Console.WriteLine();

return -1;
}

string pfxPath = args[0];
string pfxPassword = args[1];
string pfxContainer = args.Length == 3 ? args[2] : ResolveKeySourceTask.ResolveAssemblyKey(pfxPath);

bool infoOnly = args.Length == 1;
if (infoOnly)
{
Console.WriteLine(pfxContainer);
Console.WriteLine($"Installed: {ResolveKeySourceTask.IsContainerInstalled(pfxContainer)}");
return 0;
}

if (ResolveKeySourceTask.IsContainerInstalled(pfxContainer))
{
//Installs from infile in the specified key container. The key container resides in the strong name CSP.
Expand All @@ -43,6 +53,7 @@ static int Main(string[] args)
return -2;
}

string pfxPassword = args[1];
// open pfx and export its private key
var pfxCert = new X509Certificate2(pfxPath, pfxPassword, X509KeyStorageFlags.Exportable);
var pfxPrivateKey = pfxCert.PrivateKey as RSACryptoServiceProvider;
Expand All @@ -64,14 +75,14 @@ static int Main(string[] args)
rsaCSP.PersistKeyInCsp = true;
rsaCSP.ImportCspBlob(pfxCspBlob);
};

// output
// This not an actual error - just avoiding output pollution.
Console.Error.WriteLine($"The key pair has been installed into the strong name CSP key container '{pfxContainer}'.");
// Write the container to the output
Console.WriteLine(pfxContainer);
return 0;

}
}

Expand Down

0 comments on commit be61ddb

Please sign in to comment.