ProtectKeysWithCertificate only searches the "My" StoreName
We push various certificates to workstations and server groups via GPO, and sometimes these end up in the Trusted Root Certification Authorities store.
ProtectKeysWithCertificate(thumbprint) seems to only search StoreName.My by default, e.g. the Personal store for both the Current User and the Local Computer accounts.
This means that we cannot use certificates in anything but the Personal stores to encrypt keys at rest.
A) Can this be a feature added? The ability to either automatically search all stores, or the ability to search specific StoreNames. e.g. StoreName.Root.
configure.ProtectKeysWithCertificate("thumbprint", StoreName.Root);
B) Or...is it the intention that if I need to search for a certificate by thumbprint in a different StoreName, that I just duplicate the code already in the CertificateResolver and search my own stores with the X509Store.
var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
try
{
store.Open(OpenFlags.ReadOnly);
var matching = store.Certificates.Find(
X509FindType.FindByThumbprint,
"thumbprint",
validOnly: true);
}
finally
{
store.Close();
}
Thanks.
@blowdart thoughts?
By design. You can of courser provide your own X509Certificate2 when configuring.
Providing certificate doesn't solves anything.
Using ProtectKeysWithCertificate( X509Certificate2 ) still searches the stores and if the certificate is not there raises Cryptography_Xml_MissingDecryptionKey (System.Security.Cryptography.Xml.EncryptedXml).
If the certificate is in the store then the exception becomes Bad key\r\n which is guess is because of the certificate purposes.
I also ran into:
System.InvalidOperationException: A certificate with the thumbprint 'C03BB5A6410741CDD2927B4FF88C3E67215A393B' could not be found.
and looking at the code here - i get the impression that it should be implemented such it first look in User Store, then Machine. Therefore I am confused that it fails with above when below works- where I find the certificate myself.
services.AddDataProtection() .ProtectKeysWithCertificate(X509.LocalMachine.My.Thumbprint.Find("C03BB5A6410741CDD2927B4FF88C3E67215A393B", validOnly: false).FirstOrDefault())
Anyway, the latter solution is fine for me - just expected the overload to work also
Remarking for triage: .NET Core 2.0 adds support for EncryptedXml, however, at the moment, the X509Store on Linux will only support the 'LocalMachine\Root' and 'LocalMachine\CertificateAuthority' stores.
See this issue: https://github.com/dotnet/corefx/issues/3690 - currently marked as up-for-grabs
To clarify my last comment, CurrentUser\My is supported on Linux in .NET Core 2.0. It's LocalMachine\My that isn't which means there's no good machine-wide place to add store certs.
This issue was moved to aspnet/Home#2524