System.Security.Cryptography.CryptographicException: Key not valid for use in specified state

Hi guys,
I am using Octopus Deploy 3.1.4 and I have a problem. Could you please help me to solve that?

I was doing migration from Octopus 2.6 which was hosted on a different server. My deployment process has several Azure steps. Yesterday just after the migration I’ve tried to deploy from the Octopus dashboard - the deployment was successful. Later I was trying to implement automatic deployments by creating a script which uses Octo.exe. In fact I’ve just reused the old script - the only thing I’ve changed is the API key and server address. I missed that the Octo.exe was from the previous version of OctopusTools (2.6.3.59).

Now my deployment does not work at all. Both deployments from the portal and from Octo.exe fail every time with the same exception:

System.Security.Cryptography.CryptographicException: Key not valid for use in specified state.
08:31:56Error
at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
08:31:56Error
at System.Security.Cryptography.X509Certificates.X509Utils._ExportCertificatesToBlob(SafeCertStoreHandle safeCertStoreHandle, X509ContentType contentType, IntPtr password)
08:31:56Error
at System.Security.Cryptography.X509Certificates.X509Certificate.ExportHelper(X509ContentType contentType, Object password)
08:31:56Error
at Calamari.Azure.Integration.AzurePowerShellContext.CreateAzureCertificate(String workingDirectory, VariableDictionary variables) in Y:\work\b1fd300731d5f2fc\source\Calamari.Azure\Integration\AzurePowershellContext.cs:line 73
08:31:56Error
at Calamari.Azure.Integration.AzurePowerShellContext.ExecuteScript(IScriptEngine scriptEngine, String scriptFile, CalamariVariableDictionary variables, ICommandLineRunner commandLineRunner) in Y:\work\b1fd300731d5f2fc\source\Calamari.Azure\Integration\AzurePowershellContext.cs:line 46
08:31:56Error
at Calamari.Azure.Integration.AzurePowerShellScriptEngine.Execute(String scriptFile, CalamariVariableDictionary variables, ICommandLineRunner commandLineRunner) in Y:\work\b1fd300731d5f2fc\source\Calamari.Azure\Integration\AzurePowerShellScriptEngine.cs:line 24
08:31:56Error
at Calamari.Azure.Deployment.Conventions.DeployAzureCloudServicePackageConvention.Install(RunningDeployment deployment) in Y:\work\b1fd300731d5f2fc\source\Calamari.Azure\Deployment\Conventions\DeployAzureCloudServicePackageConvention.cs:line 49
08:31:56Error
at Calamari.Deployment.ConventionProcessor.RunInstallConventions() in Y:\work\b1fd300731d5f2fc\source\Calamari\Deployment\ConventionProcessor.cs:line 60
08:31:56Error
at Calamari.Deployment.ConventionProcessor.RunConventions() in Y:\work\b1fd300731d5f2fc\source\Calamari\Deployment\ConventionProcessor.cs:line 28
08:31:56Error
Running rollback conventions…
08:31:56Error
System.Security.Cryptography.CryptographicException: Key not valid for use in specified state.
08:31:56Error
at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
08:31:56Error
at System.Security.Cryptography.X509Certificates.X509Utils._ExportCertificatesToBlob(SafeCertStoreHandle safeCertStoreHandle, X509ContentType contentType, IntPtr password)
08:31:56Error
at System.Security.Cryptography.X509Certificates.X509Certificate.ExportHelper(X509ContentType contentType, Object password)
08:31:56Error
at Calamari.Azure.Integration.AzurePowerShellContext.CreateAzureCertificate(String workingDirectory, VariableDictionary variables) in Y:\work\b1fd300731d5f2fc\source\Calamari.Azure\Integration\AzurePowershellContext.cs:line 73
08:31:56Error
at Calamari.Azure.Integration.AzurePowerShellContext.ExecuteScript(IScriptEngine scriptEngine, String scriptFile, CalamariVariableDictionary variables, ICommandLineRunner commandLineRunner) in Y:\work\b1fd300731d5f2fc\source\Calamari.Azure\Integration\AzurePowershellContext.cs:line 46
08:31:56Error
at Calamari.Azure.Integration.AzurePowerShellScriptEngine.Execute(String scriptFile, CalamariVariableDictionary variables, ICommandLineRunner commandLineRunner) in Y:\work\b1fd300731d5f2fc\source\Calamari.Azure\Integration\AzurePowerShellScriptEngine.cs:line 24
08:31:56Error
at Calamari.Azure.Deployment.Conventions.DeployAzureCloudServicePackageConvention.Install(RunningDeployment deployment) in Y:\work\b1fd300731d5f2fc\source\Calamari.Azure\Deployment\Conventions\DeployAzureCloudServicePackageConvention.cs:line 49
08:31:56Error
at Calamari.Deployment.ConventionProcessor.RunInstallConventions() in Y:\work\b1fd300731d5f2fc\source\Calamari\Deployment\ConventionProcessor.cs:line 60
08:31:56Error
at Calamari.Deployment.ConventionProcessor.RunConventions() in Y:\work\b1fd300731d5f2fc\source\Calamari\Deployment\ConventionProcessor.cs:line 50
08:31:56Error
at Calamari.Azure.Commands.DeployAzureCloudServiceCommand.Execute(String[] commandLineArguments) in Y:\work\b1fd300731d5f2fc\source\Calamari.Azure\Commands\DeployAzureCloudServiceCommand.cs:line 93
08:31:56Error
at Calamari.Program.Execute(String[] args) in Y:\work\b1fd300731d5f2fc\source\Calamari\Program.cs:line 27
08:31:56Fatal
The step failed: The remote script failed with exit code 100

If you need more info - I can add whatever you need.
Thanks in advance

Hi Anton,

I suspect the problem is the certificate you are using to authenticate with Azure (the one associated with the Azure Subscription Account in Octopus) is not marked as exportable.

Either recreate the certificate, ensuring it is flagged exportable (e.g. passing -pe to the makecert.exe tool), or let Octopus generate the certificate for you.

I hope this helps,
Michael