Skip to content

ECDsa.TrySignData(...) returns true, with bytes written == 64 for null values #93972

Closed

Description

@brentschmaltz

Calling: ECDsa.TrySignData(data, destination, HashAlgorithmName, DSASignatureFormat.IeeeP1363FixedFieldConcatenation, out bytesWritten);

where:
data = Encoding.UTF8.GetBytes("foobar");
destination = ((byte[])(null)).AsSpan();
HashAlgorithmName = HashAlgorithmName.SHA256

returns:
true
bytesWritten == 64;

Crypto siging issue.

Activity

  1. ghost added
    untriaged
    on Oct 24, 2023
  2. transferred this issue fromdotnet/sdkon Oct 25, 2023
  3. eerhardt commented on Oct 25, 2023

    @eerhardt
    Member

    I'm able to repro this with the following code:

    using System.Security.Cryptography;
    using System.Text;
    
    var data = Encoding.UTF8.GetBytes("foobar");
    var destination = ((byte[])(null)).AsSpan();
    var hashAlgorithmName = System.Security.Cryptography.HashAlgorithmName.SHA256;
    
    
    var ecdsa = ECDsa.Create(); // doesn't repro
    TrySignData(data, destination, hashAlgorithmName, ecdsa);
    
    ecdsa = new ECDsaCng(); // repros
    TrySignData(data, destination, hashAlgorithmName, ecdsa);
    
    static void TrySignData(byte[] data, Span<byte> destination, HashAlgorithmName hashAlgorithmName, ECDsa ecdsa)
    {
        var result = ecdsa.TrySignData(data, destination, hashAlgorithmName, DSASignatureFormat.IeeeP1363FixedFieldConcatenation, out var bytesWritten);
    
        Console.WriteLine($"Result: {result}");
        Console.WriteLine($"Bytes Written: {bytesWritten}");
    }

    This program prints:

    Result: False
    Bytes Written: 0
    Result: True
    Bytes Written: 132
    

    Debugging this code, it looks like Windows is returning ERROR_SUCCESS even though the signature.Length was 0 in this call:

    public static unsafe bool TrySignHash(this SafeNCryptKeyHandle keyHandle, ReadOnlySpan<byte> hash, Span<byte> signature, AsymmetricPaddingMode paddingMode, void* pPaddingInfo, out int bytesWritten)
    {
    for (int i = 0; i <= StatusUnsuccessfulRetryCount; i++)
    {
    ErrorCode error = Interop.NCrypt.NCryptSignHash(
    keyHandle,
    pPaddingInfo,
    hash,
    hash.Length,
    signature,
    signature.Length,
    out int numBytesNeeded,
    paddingMode);
    switch (error)
    {
    case ErrorCode.ERROR_SUCCESS:
    bytesWritten = numBytesNeeded;
    return true;

  4. vcsjones commented on Oct 25, 2023

    @vcsjones
    Member

    @eerhardt were you intending to fix this, or would you like me to take a look?

  5. vcsjones commented on Oct 25, 2023

    @vcsjones
    Member

    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 = stackalloc byte[1].Slice(1);

    Then it correctly "fails". So This seems like a problem of "Pinning null-ref span results in nullptr".

  6. eerhardt commented on Oct 25, 2023

    @eerhardt
    Member

    @eerhardt were you intending to fix this, or would you like me to take a look?

    No I wasn't. Please take a look if you have time.

  7. self-assigned this
    on Oct 25, 2023
  8. vcsjones commented on Oct 25, 2023

    @vcsjones
    Member

    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:

    [LibraryImport(blah)]
    internal partial int Foo(
        [MarshalUsing(typeof(NeverNullIMeanItReadOnlySpanMarshaller))] ReadOnlySpan<byte> pbData,
        int cbData);
  9. vcsjones commented on Oct 25, 2023

    @vcsjones
    Member

    Repros back to .NET 6, so not a recent regression.

  10. added this to the 9.0.0 milestone on Oct 25, 2023
  11. removed
    untriaged
    on Oct 25, 2023
  12. jkoritzinsky commented on Oct 25, 2023

    @jkoritzinsky
    Member

    We had a marshaller proposal for this years ago that got shot down. We can easily bring it back for internal usage.

  13. 2 remaining items

  14. MichalPetryka commented on Oct 25, 2023

    @MichalPetryka
    Contributor

    doesn't matter where, could be 0x01 or whatever

    Why not Array.Empty?

  15. jkoritzinsky commented on Oct 25, 2023

    @jkoritzinsky
    Member

    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.

  16. ghost removed
    in-pr
    on Oct 25, 2023
  17. vcsjones commented on Oct 25, 2023

    @vcsjones
    Member

    Re-opening for backport consideration.

  18. brentschmaltz commented on Oct 25, 2023

    @brentschmaltz
    Author

    Thanks.

  19. brentschmaltz commented on Oct 25, 2023

    @brentschmaltz
    Author

    left open for backport.

  20. modified the milestones: 9.0.0, 8.0.x on Nov 19, 2023
  21. vcsjones commented on Nov 19, 2023

    @vcsjones
    Member

    Closing - the backport has been merged for .NET 8.

  22. locked and limited conversation to collaborators on Dec 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions