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.TrySignData(...) returns true, with bytes written == 64 for null values #93972
My guess here is that CNG thinks "Oh you are specifying a signature of nullptr, you must be trying to ask me how large the signature is. The signature is 132 bytes! I successfully told you the signature size."
From the NCryptSignHash docs
If this parameter is NULL, this function will calculate the size required for the signature and return the size in the location pointed to by the pcbResult parameter.
If we change destination to this:
Span<byte>destination=stackallocbyte[1].Slice(1);
Then it correctly "fails". So This seems like a problem of "Pinning null-ref span results in nullptr".
@jkoritzinsky How difficult would be be, at least for internal usage, to make a never-null {ReadOnly}Span marshaller when marshaling from managed to native?
We have gotten bitten a few times where Win32 null-reference spans when pinned are nullptr, and Win32 / CNG may handle that differently.
We had a marshaller proposal for this years ago that got shot down. We can easily bring it back for internal usage.
Thanks. The fix for this PR is more surgical since we may want to backport it.
Presumably the way to do this then is to take the existing one and have GetPinnableReference return a ref to some location (doesn't matter where, could be 0x01 or whatever) if the ref is null and the span length is zero.
Does that sound right?
Last time we did this, we took a ref to a static byte field or to Array.Empty<byte>() so it at least looks like a valid address.
Unrelated from my marshaling questions, this issue also impacts DSA and RSA.
Good to know
doesn't matter where, could be 0x01 or whatever
Why not Array.Empty?
We can't use Array.Empty as-is as that hits the span zero-length normalization. But doing a ref to the first element of Array.Empty is a good idea.
Activity
eerhardt commentedon Oct 25, 2023
I'm able to repro this with the following code:
This program prints:
Debugging this code, it looks like Windows is returning
ERROR_SUCCESSeven though the signature.Length was0in this call:runtime/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CngCommon.SignVerify.cs
Lines 48 to 66 in 47873d8
vcsjones commentedon Oct 25, 2023
@eerhardt were you intending to fix this, or would you like me to take a look?
vcsjones commentedon Oct 25, 2023
My guess here is that CNG thinks "Oh you are specifying a signature of nullptr, you must be trying to ask me how large the signature is. The signature is 132 bytes! I successfully told you the signature size."
From the NCryptSignHash docs
If we change
destinationto this:Then it correctly "fails". So This seems like a problem of "Pinning null-ref span results in nullptr".
eerhardt commentedon Oct 25, 2023
No I wasn't. Please take a look if you have time.
vcsjones commentedon Oct 25, 2023
Thinking "long term" here...
@jkoritzinsky How difficult would be be, at least for internal usage, to make a never-null {ReadOnly}Span marshaller when marshaling from managed to native?
We have gotten bitten a few times where Win32 null-reference spans when pinned are nullptr, and Win32 / CNG may handle that differently.
Is it possible to build something like:
vcsjones commentedon Oct 25, 2023
Repros back to .NET 6, so not a recent regression.
jkoritzinsky commentedon Oct 25, 2023
We had a marshaller proposal for this years ago that got shot down. We can easily bring it back for internal usage.
2 remaining items
MichalPetryka commentedon Oct 25, 2023
Why not Array.Empty?
jkoritzinsky commentedon Oct 25, 2023
Last time we did this, we took a
refto a static byte field or toArray.Empty<byte>()so it at least looks like a valid address.Good to know
We can't use
Array.Emptyas-is as that hits the span zero-length normalization. But doing a ref to the first element of Array.Empty is a good idea.vcsjones commentedon Oct 25, 2023
Re-opening for backport consideration.
brentschmaltz commentedon Oct 25, 2023
Thanks.
brentschmaltz commentedon Oct 25, 2023
left open for backport.
vcsjones commentedon Nov 19, 2023
Closing - the backport has been merged for .NET 8.