ServiceAccountCredential fails for non-exportable CryptoAPI key
See original GitHub issue- Create a certificate with a non-exportable CryptoAPI key
New-SelfSignedCertificate `
-KeyUsage DigitalSignature `
-FriendlyName "Sample CryptoAPI service account key" `
-Subject "Sample CryptoAPI service account key" `
-KeyExportPolicy NonExportable `
-CertStoreLocation "cert:\CurrentUser\My" `
-Provider "Microsoft Base Cryptographic Provider v1.0" `
-KeySpec Signature
- Use they to initialize a
ServiceAccountCredential
:
using (var store = new X509Store(StoreName.My, StoreLocation.CurrentUser))
{
store.Open(OpenFlags.ReadOnly);
using (var certificate = store.Certificates
.Cast<X509Certificate2>()
.Where(c => c.FriendlyName == "Sample CryptoAPI service account key")
.FirstOrDefault()
?? throw new Exception("Certificate not found"))
{
//
// Initialize a ServiceAccountCredential with the certificate.
//
var credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(emailAddress)
{
Scopes = new[] { "https://www.googleapis.com/auth/cloud-platform" }
}
.FromCertificate(certificate));
}
}
- Run the code on .NET Core --> works
- Run the code on .NET Framework
Expected: Works too
Actual:
Unhandled Exception: System.Security.Cryptography.CryptographicException: Key not valid for use in specified state.
at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
at System.Security.Cryptography.Utils.ExportCspBlob(SafeKeyHandle hKey, Int32 blobType, ObjectHandleOnStack retBlob)
at System.Security.Cryptography.Utils.ExportCspBlobHelper(Boolean includePrivateParameters, CspParameters parameters, SafeKeyHandle safeKeyHandle)
at Google.Apis.Auth.OAuth2.ServiceAccountCredential.Initializer.FromCertificate(X509Certificate2 certificate) in C:\Apiary\2021-03-18.17-02-24\Src\Support\Google.Apis.Auth\OAuth2\ServiceAccountCredential.cs:line 128
at CryptoApiNetFx.Program.Main(String[] args) in C:\...\Program.cs:line 33
For some reason, FromCertificate
tries to export and re-import the key on .NET Framework. It’s marked as a workaround, but I am not sure for what.
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Extend ServiceAccountCredential to support CryptoNG ...
On .NET Framework, ServiceAccountCredential is hardcoded to use ... ServiceAccountCredential fails for non-exportable CryptoAPI key #1871.
Read more >Using a CryptoAPI-backed key as service account key
NET Framework, ServiceAccountCredential tries to export the certificate's private key, which fails because we marked the key as NonExportable .
Read more >Using a CNG-backed key as service account key
ServiceAccountCredential is trying to treat a CNG key as CryptoAPI key, which obviously doesn't work. Update: This issue has been fixed in v1....
Read more >c# - Why does ServiceAccountCredential fail with invalid ...
I created my own service account, downloaded the .p12 key, enabled datastore. But I still get this error: Unhandled Exception: Google.
Read more >Deploy Active Directory Federation Services on a Managed ...
This guide describes how you can deploy Microsoft Active Directory Federation Services (AD FS) for Windows Server 2019 in a Managed Microsoft AD...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
FYI: This has been released on the v1.52.0 of the Google.Apis libraries.
I agree with your analysis and would expect that a 4.6.1 target should let us fix both issues.