-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue in ECDsaCertificateExtensions.CopyWithPrivateKey with TPM #75971
Comments
Tagging subscribers to this area: @dotnet/area-system-security, @vcsjones Issue DetailsDescriptionWhen creating a Cng ECDSA private key in the TPM Cng and using CertificateRequest to generate a self signed certificate. The implementation calls ECDsaCertificateExtensions.CopyWithPrivateKey which call Helpers.AreSamePublicECParameters(publicKey.ExportParameters(false), privateKey.ExportParameters(false))). System.ArgumentException: The provided key does not match the public key for this certificate. (Parameter 'privateKey') Reproduction Steps
Expected behaviorNo exception. Actual behaviorException Regression?No response Known WorkaroundsNo know workaround ConfigurationBug occurs on: Other informationNo response
|
I can reproduce it. Taking a look. |
This problem can be reduced to this: #pragma warning disable CA1416
using System;
using System.Security.Cryptography;
CngKeyCreationParameters keyParams = new()
{
Provider = new CngProvider("Microsoft Platform Crypto Provider"),
KeyCreationOptions = CngKeyCreationOptions.OverwriteExistingKey,
};
CngKey key = CngKey.Create(CngAlgorithm.ECDsaP384, "KeyInTPM-TestKey", keyParams);
ECDsaCng tpmEcdsa = new ECDsaCng(key);
ECDsaCng ephemeralEcdsa = new ECDsaCng(ECCurve.NamedCurves.nistP384);
var parameters = tpmEcdsa.ExportParameters(false);
var parameters2 = ephemeralEcdsa.ExportParameters(false);
Console.WriteLine(parameters.Curve.Oid.Value ?? "<null>");
Console.WriteLine(parameters2.Curve.Oid.Value ?? "<null>"); For the Software key, the OID has a runtime/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/Helpers.cs Line 207 in f595e02
Which is called from here: Lines 38 to 43 in f595e02
When we grab the ECDsa public key off of the certificate, there is a @bartonjs I think we can relax this check a bit for Windows. I'll open a pull request for discussion. |
Actually, fixing that just uncovers another problem. runtime/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/ECDsa.cs Lines 1243 to 1250 in 8b6b142
That can be reproduced with:
Okay, but maybe we can fix that by using the ECCurve to derive the field size for known named curves. nistP256 is 256-bit, nistP384 is 384-bit, etc. Then we get an error from CNG in the form of a CryptographicException saying "TPM 2.0: Structure is wrong size". It seems like a number of things need to change for this to work. |
Description
When creating a Cng ECDSA private key in the TPM Cng and using CertificateRequest to generate a self signed certificate.
The implementation calls ECDsaCertificateExtensions.CopyWithPrivateKey which call Helpers.AreSamePublicECParameters(publicKey.ExportParameters(false), privateKey.ExportParameters(false))).
Unfortunately privateKey.ExportParameters returns a structure where the ECParameters.Curve.Oid.Value is not set.
That breaks AreSamePublicECParameters which returns false and raise an exception in CopyWithPrivateKey.
System.ArgumentException: The provided key does not match the public key for this certificate. (Parameter 'privateKey')
at System.Security.Cryptography.X509Certificates.ECDsaCertificateExtensions.CopyWithPrivateKey(X509Certificate2 certificate, ECDsa privateKey)
at System.Security.Cryptography.X509Certificates.CertificateRequest.CreateSelfSigned(DateTimeOffset notBefore, DateTimeOffset notAfter)
Reproduction Steps
Expected behavior
No exception.
Actual behavior
Exception
Regression?
No response
Known Workarounds
No know workaround
Configuration
Bug occurs on:
Windows 10 Pro wich Visual Studio 2022 and .net 5.0.
Windows 10 Pro wich Visual Studio 2022 and .net 6.0.
Other information
No response
The text was updated successfully, but these errors were encountered: