codesign error message:

error: The specified item could not be found in the keychain

This was a frustratingly unhelpful error message, and Googling the error message did not help me find the problem

The specific circumstances was after an OS upgrade. Downloading an reinstalling the developer certificates wasn't sufficient as the private key used to generate the certificates didn't match.

This became obvious when using XCode. Go to XCode>Preferences>Accounts>Manage Certificates The affected certificates' icon has a red cross, and Status states “Missing Private Key”

Assuming you don't have a backup for the private keys (which I didn't) follow instructions for requesting new certificate from Apple, and install that. Make sure you delete the old certificates from the keychain.

You may need to lock and unlock the keychain using the Keychain Access utility in order for the password provided to codesign to work.