From be61ddb3d1078980008c3b368e2ca8fba0197fb9 Mon Sep 17 00:00:00 2001 From: Jan Vratislav Date: Wed, 29 Jul 2020 14:57:17 +0200 Subject: [PATCH] Add possibility for key analysis --- README.md | 5 +++-- src/SnInstallPfx.cs | 27 +++++++++++++++++++-------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 79d6fae..984c18c 100644 --- a/README.md +++ b/README.md @@ -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 -SnInstallPfx.exe +SnInstallPfx.exe // show information about the pfx_infile +SnInstallPfx.exe // install the pfx_infile +SnInstallPfx.exe // install the pfx_infile under container_name ``` The hash computing is copied from the MSBuild source code on GitHub. diff --git a/src/SnInstallPfx.cs b/src/SnInstallPfx.cs index e753676..b94e11f 100644 --- a/src/SnInstallPfx.cs +++ b/src/SnInstallPfx.cs @@ -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. @@ -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 into a key container compatible for MSBuild."); Console.WriteLine("This utility is an alternative for command sn.exe -i ."); Console.WriteLine("It accepts password from command line and automatically generates a container name for if no container name is specified via the argument."); Console.WriteLine(); Console.WriteLine("Usage:"); - Console.WriteLine($"{Assembly.GetEntryAssembly().GetName().Name}.exe "); - Console.WriteLine($"{Assembly.GetEntryAssembly().GetName().Name}.exe "); + Console.WriteLine($"{Assembly.GetEntryAssembly().GetName().Name}.exe // show information about the pfx_infile"); + Console.WriteLine($"{Assembly.GetEntryAssembly().GetName().Name}.exe // install the pfx_infile"); + Console.WriteLine($"{Assembly.GetEntryAssembly().GetName().Name}.exe // 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. @@ -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; @@ -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; - + } }