Turns out removing a passphrase from a OpenSSH private key in PowerShell automation scripts on Windows is difficult.
I’m a fan of building golden images for deployment and I like to use Packer as one of my tools. When you want to build a Windows image you will most likely want to use Powershell for scripting.
I scripted all of the Windows image automation in Powershell and one of the steps required to remove a passphrase from a OpenSSH private key.
This is where I hit a snag. Consider the following snippet:
The last argument, which is the new passphrase is empty.
Start-Process : Cannot validate argument on parameter 'ArgumentList'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
This command doesn’t work. I also tried other approaches - including an attempt to control the
stdin. Perhaps I could have also tried making a classic batch file and work around this limitation, but I really don’t like hacks like that.
The bottom of this issue is between what Powershell requires and what OpenSSH ssh-keygen expects.
Powershell simply doesn’t allow empty or null strings in the
Start-Process (and others)
ArgumentList parameter arrays. OpenSSH requires the
ssh-keygen -N "" to remove the passphrase from the private key.
I went and searched for OpenSSH source code and found the main repository for OpenBSD and a GitHub repository for portable versions. I figured I should attempt to make the change in the portable repository.
I found the code responsible for the passphrase changes. The core of the issue is the code just checks if the pointer to the new passphrase is NULL. It can never pass if the parameter value isn’t there - the pointer will always be NULL.
Based on many previous interactions and experience I’ve determined the solution with least impact on existing functionality is introducing a new switch that will just use an empty string as the new passphrase.
Consider the following excerpt from the patch:
I introduced a new static variable
identity_empty_passphrase initialized to an empty string. This is non-NULL by default. Next when the new ‘G’ option is invoked the pointer to this variable is used as the
Now the following command will get the passphrase removed:
I also updated the manual page with the new switch in the full patch. Here’s the pull request on GitHub.
I like solving real problems and open source is a great space for practice.