Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-entering Password In Sn.exe

Tags:

c#

signing

sn.exe

I need to create post build event to perform the following:

sn -i MyKey.pfx MyKeyContainerName
tlbimp $(ConfigurationName)\MyCom.tlb /out:$(ConfigurationName)\NETMyCom.dll /keycontainer:MyKeyContainerName
sn -d MyKeyContainerName

When the Visual Studio executes the 1st statement it requires a password and waits until the user specifies it and fails.

Microsoft (R) .NET Framework Strong Name Utility Version 2.0.50727.42 Copyright (c) Microsoft Corporation. All rights reserved.

Enter the password for the PKCS#12 key file: Failed to parse the PKCS#12 blob in mykey.pfx -- The handle is invalid.

I tried to specify the password using sn command line arguments, but I could not see a way to do it.

Please help.

Regards, Hilmi.

like image 967
HJ. Avatar asked Jun 29 '10 05:06

HJ.


People also ask

What does SN exe do?

The Strong Name tool (Sn.exe) helps sign assemblies with strong names. Sn.exe provides options for key management, signature generation, and signature verification. Do not rely on strong names for security. They provide a unique identity only.

Where can I find SN exe?

It is named “sn.exe” so the full path is “C:\Program Files (x86)\Microsoft SDKs\Windows\v10. 0A\bin\NETFX 4.6. 1 Tools\sn.exe”.

How do I know if a name is strong signing?

Or directly through Microsoft Visual Studio IDE via project's properties – click on signing tab, check “Sign the assembly” then browse to the . snk file: By building your project, you get a strong name signed assembly.

How do I create a strong name key in Visual Studio?

In Visual Studio Solution Explorer, right-click the project and then click Properties. Click the Signing tab and choose Browse in the Choose a strong name key file drop down box. Browse to the key file and click it. Click Open, and then close the project properties.


2 Answers

After a long investigation i can run sn.ex for various automated environments such as Azure devops. My code is here:

Start-Process cmd.exe 
Sleep 3
$WshShell = New-Object -ComObject WScript.Shell
Sleep 3
$WshShell.sendkeys(".\sn.exe -i  $PfxCertificatePath  VS_KEY_10D1C85C6387479B{Enter}");
Sleep 3;
$WshShell.sendkeys("**password**{Enter}");
Sleep 3;
$WshShell.sendkeys("{Enter}");
like image 162
Oleg Steblev Avatar answered Sep 28 '22 05:09

Oleg Steblev


Unfortunately, no approached mentioned here worked for me. I have to register couple pfxs in a docker container.

So I re-developed the sn.exe -i <infile> <container> command in C# using the RSACryptoServiceProvider. The source and the app are on GitHub in the SnInstallPfx project.

The SnInstallPfx app accepts a PFX key and its password. It computes the key container name (VS_KEY_*) automatically (borrowed from MSBuild source code) and installs it to the strong name CSP.

Usage:

SnInstallPfx.exe <pfx_infile> <pfx_password>
like image 24
honzajscz Avatar answered Sep 28 '22 03:09

honzajscz