MacOS Sierra – error: The specified item could not be found in the keychain – within ssh

code-signingkeychainmacosssh

When I try to run codesign --force --deep-verify --verbose --sign "CERT-NAME" ... over SSH I always get an error: The specified item could not be found in the keychain.

But when I run the same command local all works fine.

Any ideas?

Best Answer

It's hard to say for certain without seeing the actual full command, but I'd guess that you're probably running into quoting issues because the command gets passed through two shells (local shell -> ssh tunnel -> remote shell -> codesign command). Each shell interprets and removes quotes and escapes before passing strings on, and you want those double-quotes to be interpreted by the final shell, so you may need to add another layer of quotes. Here are some examples:

ssh user@server codesign --force --deep-verify --verbose --sign "CERT NAME"

This doesn't work because the double-quotes are interpreted and removed by the local shell, so the final command (effectively) has the cert name unquoted.

ssh user@server 'codesign --force --deep-verify --verbose --sign "CERT NAME"'

This works because the outer (single-) quotes are removed by the local shell, leaving the inner (double-) quotes to be interpreted by the remote shell, so it'll pass the entire cert name to the codesign command as a single argument.

ssh user@server "codesign --force --deep-verify --verbose --sign \"CERT NAME\""

This also works, but here double-quotes are used for both the outer and inner layer, so the inner layer needs to be escaped.