Home » Code sign PowerShell script with Certum’s open source code-signing certificate

Code sign PowerShell script with Certum’s open source code-signing certificate

by Vlad Drumea
0 comments

In this post I go over the process of using Certum’s open source code signing certificate to code sign PowerShell scripts.

Why code-sign?

In my case, to help anyone who might want to use PSBlitz in a more restrictive environment where PowerShell scripts are required to be code-signed.

The equipment

I’ve opted for Certum’s open source code signing set.
At the time of writing, the product page states that the included card reader is an ACS ACR39T-A1, but I’ve received an ACS ACR40T-A1.

Code signing a PowerShell script

I’ll skip the cryptographic card initializing and certificate configuration process since that’s covered by Certum already.

With the card reader connected to the computer, I run proCertum CardManager open the Common Profile.

proCertum CardManager opened and with the Read Card button marked as step 1
proCertum CardManager opened and with the Common Profile button marked as step 2

Once the Common Profile is opened and the certificate overview is visible, it can be used for code signing.

proCertum CardManager with the Common Profile open and the overview of the code signing certificate visible

In PowerShell, I run the following command to list any available code-signing certificates:

PowerShell window showing the my code signing certificate

So, the code-signing certificate is in my user’s certificate store.
If it isn’t, I can install it from the common profile via “Show certificate details” -> “Install Certificate”.

To reference the certificate itself you need to use the certificate store path followed by the certificate’s thumbprint.

I load the certificate into a variable, to make things easier to read.

Also use a variable for PSBlitz.ps1’s path.

And then use Set-AuthenticodeSignature to sign the script.

I’m prompted for the smart card’s PIN (both PIN and PUK codes are set up while initializing the cryptographic card).

Windows Security prompt for the smart card's PIN

And, after providing it, the script is signed.

Output of Set-AuthenticodeSignature showing that the status is valid

I can validate this using the following command:

PowerShell window showing the output of the above command with the code signer's details

I can also validate the existence of the signature block by reading the last 90 lines of the script.

PowerShell window showing the signature block in the ps1 file

Testing code-signed script

My current execution policy is RemoteSigned, meaning that scripts created locally do not need to be signed.
To properly test, I’ll need to switch the execution policy of my current PowerShell process to AllSigned.

PowerShell window showing that running the signed PSBlitz.ps1 prompts if the signed script should be executed.

In this case, I’m prompted if I want to run the signed version of PSBlitz.

Attempting to run the unsigned version fails instantly due to the execution policy.

PowerShell window showing an error when trying to run the the unsigned PSBlitz.ps1

Conclusion

That’s pretty much it. If you’re curios about the post where I’ve first announced PSBlitz, you can find it here

You may also like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.