You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ECDsa.Create(ECParameters) behavior is not consistent across platforms with a public/private key mismatch #27830
On Windows, success (the Q value seems to be rederived from D).
Since it's probably hard to make Linux and macOS coerce Q, it's probably more practical to make Windows throw (theoretical process: NCryptImportKey, then NCryptExportKey and compare the target Q value, then dispose the new key handle and throw before replacing the existing key handle).
@bartonjs I can pick this up if you want; or at least noodle around with some ideas. I have some spare cycles to pick up 3.0 bugs. Let me know if you think there is something else that would be a better use of my time.
@bartonjs I haven't made any significant progress. I was meant to get back in the swing of things but only a few hours ago have I managed to figure out why I could not build CoreFX for the past two weeks due to dotnet/corefx#37907 😅
However when exported again, the insignificant zeros are not included. So if we do a import / export / test in Windows and compare byte-for-byte, we end up with a scenario where Windows does not tolerate insignificant zeros, but OpenSSL does, and the behavior remains inconsistent.
You can't blindly pull off zeros, either, since the curve points may legitimately need the octets to be the appropriate size. For example:
I think then, the appropriate way to handle this then is, when exporting for comparison, we assume that the import validated the points are on the curve. When comparing, compare the octets from the end using the smallest number of octets. For the one that is bigger, if there is, assert that the remainder are zeros.
For the sake of completeness, if your D, QY and QX all have leading zeros, you can import them still with OpenSSL (haven't checked Windows, yet). e.g. these work:
The most consistent behavior, then, would be to determine the correct parameter size post-import and reject it if it was non-canonical. (Alternatively, on macOS create a new key using the the curve identifier, memoize the resulting size (okay, yeah, currently there's only the 3 so they could be hard coded), and fix up the parameters to be canonical.)
(This is a brain dump of "stuff" just so future Kevin knows what present Kevin recently learned)
I picked this up (again, determined to make progress on it) and it seems that Windows' behavior is not entirely understood. It appears to re-derive Q... sometimes. This for example, will fail:
Unhandled exception. Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: The requested operation is not supported.
My current thinking right now is that Windows will re-compute Q only if whatever Q it was supplied with is on the Curve (as we already know, CNG will also re-compute in the case of Q=(0,0) but it handles that differently)
Here is another example that "works" where the Q value is on the Curve, but G × D != Q:
What I am thinking then, if we want to match Windows.
OpenSSL we can get pretty close. We can distinguish between "point not on curve" and "This Q is not for that D".
If EC_KEY_set_public_key_affine_coordinates fails when importing, we have it the case of "Q is not on the curve". We continue to let this fail to match Windows.
We make it to EC_KEY_check_key and that fails. This is what Windows is handling. If we fail here, and we have D, then we can re-compute Q.
When "exporting", OpenSSL behavior will differ from Windows. OpenSSL will export the Q based on whatever it calculated from D. Windows will export whatever the caller supplied. If we really wanted OpenSSL to match Windows, we would have to stash the original Q for the lifetime of the key and personally I don't see a ton of value in that.
macOS does not let us make this distinction. The key either imports, or it doesn't. We can try again if the import fails without Q if we have D. MacOS would also have the same export characteristics as OpenSSL.
When Windows is in a mismatched Q state, what does the public key export say? (Imported Q or correct Q?) Even if that's the "imported" Q we could... complicatedly... always double-import, once with Q as-was, once with Q=0, and then normalize the behavior across mismatches.
Everything is consistent. If I import with a wrong Q in ImportECPrivateKey then ExportParameters(false) and ExportSubjectPublicKeyInfo are the caller supplied Q, so "imported" everywhere.
we could... complicatedly... always double-import, once with Q as-was, once with Q=0
😅 This seems like a minor behavior deviation that doesn't justify the complexity, IMO. (Your house, your rules though). The end result would be consistent across platforms from signing / verification, even if you export it and import it it again. You just can't easily observe the Q correction in Windows.
It looks like, because the Apple PAL has a handle for both the public key and the private key, we are tearing apart the public key and private key from ECPrivateKey in to ECParameters.
So we end up with a private key for D, and a public key for an unrelated Q. It seems then we are signing with one key and verifying with another.
Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.
This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.
Activity
vcsjones commentedon Apr 17, 2019
@bartonjs I can pick this up if you want; or at least noodle around with some ideas. I have some spare cycles to pick up 3.0 bugs. Let me know if you think there is something else that would be a better use of my time.
bartonjs commentedon Jun 4, 2019
@vcsjones Any progress / are you still looking at this one? https://github.com/dotnet/corefx/issues/37595 seems pretty similar.
vcsjones commentedon Jun 4, 2019
@bartonjs I haven't made any significant progress. I was meant to get back in the swing of things but only a few hours ago have I managed to figure out why I could not build CoreFX for the past two weeks due to dotnet/corefx#37907 😅
I'll be looking at this very near term.
bartonjs commentedon Jun 12, 2019
Given that this isn't new in 3.0 I'm moving to Future (PRs while 3.0 is still open are still welcome).
vcsjones commentedon Jun 20, 2019
Hm. One thing that makes this interesting is insignificant zeros. For example, these parameters work fine with OpenSSL:
However when exported again, the insignificant zeros are not included. So if we do a import / export / test in Windows and compare byte-for-byte, we end up with a scenario where Windows does not tolerate insignificant zeros, but OpenSSL does, and the behavior remains inconsistent.
You can't blindly pull off zeros, either, since the curve points may legitimately need the octets to be the appropriate size. For example:
I think then, the appropriate way to handle this then is, when exporting for comparison, we assume that the import validated the points are on the curve. When comparing, compare the octets from the end using the smallest number of octets. For the one that is bigger, if there is, assert that the remainder are zeros.
For example, given:
"000087E2DD50E8204FB80A5FBFC041859CCE0730ACCE1A828918E01461E468DDAD87"is exported as
"87E2DD50E8204FB80A5FBFC041859CCE0730ACCE1A828918E01461E468DDAD87"Compare from the end of the byte array (since big endian) using the number of bytes that was exported (32) and verify the rest are zeros.
bartonjs commentedon Jun 20, 2019
Yeah,
X.Slice(X.Length - exportedX.Length).SequenceEqual(exportedX)seems like a reasonable comparison.bartonjs commentedon Jun 20, 2019
(Er, and that any remaining bytes of X were zero)
vcsjones commentedon Jun 21, 2019
For the sake of completeness, if your D, QY and QX all have leading zeros, you can import them still with OpenSSL (haven't checked Windows, yet). e.g. these work:
These are 31-byte values for a p256 curve.
So it's possible that the byte values imported are smaller than the ones that get exported.
vcsjones commentedon Jun 21, 2019
Heh, well, macOS will not import 31-byte points for p256, nor will it tolerate "zero padded" values. It is pickier.
bartonjs commentedon Jun 21, 2019
The most consistent behavior, then, would be to determine the correct parameter size post-import and reject it if it was non-canonical. (Alternatively, on macOS create a new key using the the curve identifier, memoize the resulting size (okay, yeah, currently there's only the 3 so they could be hard coded), and fix up the parameters to be canonical.)
2 remaining items
vcsjones commentedon Oct 14, 2020
(This is a brain dump of "stuff" just so future Kevin knows what present Kevin recently learned)
I picked this up (again, determined to make progress on it) and it seems that Windows' behavior is not entirely understood. It appears to re-derive Q... sometimes. This for example, will fail:
My current thinking right now is that Windows will re-compute Q only if whatever Q it was supplied with is on the Curve (as we already know, CNG will also re-compute in the case of Q=(0,0) but it handles that differently)
Here is another example that "works" where the Q value is on the Curve, but G × D != Q:
Another interesting quirk is that CNG appears to export the original, bad, Q. Consider:
This will print "true". So as far as we can see, the key did not change when being washed through CNG.
But if you run the key through openssl, we can see it is invalid.
What I am thinking then, if we want to match Windows.
OpenSSL we can get pretty close. We can distinguish between "point not on curve" and "This Q is not for that D".
EC_KEY_set_public_key_affine_coordinatesfails when importing, we have it the case of "Q is not on the curve". We continue to let this fail to match Windows.EC_KEY_check_keyand that fails. This is what Windows is handling. If we fail here, and we have D, then we can re-compute Q.macOS does not let us make this distinction. The key either imports, or it doesn't. We can try again if the import fails without Q if we have D. MacOS would also have the same export characteristics as OpenSSL.
bartonjs commentedon Oct 14, 2020
When Windows is in a mismatched Q state, what does the public key export say? (Imported Q or correct Q?) Even if that's the "imported" Q we could... complicatedly... always double-import, once with Q as-was, once with Q=0, and then normalize the behavior across mismatches.
vcsjones commentedon Oct 15, 2020
Everything is consistent. If I import with a wrong Q in
ImportECPrivateKeythenExportParameters(false)andExportSubjectPublicKeyInfoare the caller supplied Q, so "imported" everywhere.😅 This seems like a minor behavior deviation that doesn't justify the complexity, IMO. (Your house, your rules though). The end result would be consistent across platforms from signing / verification, even if you export it and import it it again. You just can't easily observe the Q correction in Windows.
vcsjones commentedon Oct 16, 2020
On the macOS 11 front, the behavior here is... strange. No exception is raised anywhere but a simple signature round-trip does not work.
This occurs in netcoreapp3.1 too, so there hasn't been a regression. I will dig to see if I can make any sense out of what is happening.
vcsjones commentedon Oct 16, 2020
It looks like, because the Apple PAL has a handle for both the public key and the private key, we are tearing apart the public key and private key from ECPrivateKey in to
ECParameters.So we end up with a private key for D, and a public key for an unrelated Q. It seems then we are signing with one key and verifying with another.
The easiest fix is to ignore Q if we have D.
dotnet-policy-service commentedon Apr 17, 2025
Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.
This process is part of our issue cleanup automation.
dotnet-policy-service commentedon May 1, 2025
This issue will now be closed since it had been marked
no-recent-activitybut received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.