Skip to content

Program incorrectly handles crypto key when running under Linux .NET 9 but is fine under .NET 8 #113555

@Dave-Lowndes

Description

@Dave-Lowndes

Description

This is a duplicate of this report on developercommunity.

The attached C# .NET program (see the developercommunity bug report) throws an exception when built for .NET 9 and is run on Linux platforms. I’ve tried Mint, Ubuntu (WSL), & Raspberry Pi (ARM64), all behave the same. However its fine on those when built for .NET 8 (and .NET 7) and similarly under Windows for .NET 7, 8, & 9.

The issue seems to arise in loading the private key in the call to ImportPkcs8PrivateKey. As noted in comments in the code, after this call in the good scenario, the rsa.KeySize is 3080, when run under Linux & .NET9, it’s 3073.

Presumably something in the Linux implementation has altered between .NET 8 & 9 that’s giving rise to this issue?

Here’s an example run of a .NET 8 and .NET 9 versions under Ubuntu (WSL):

david@MyPC:~/test$ ./EncDecTest8
Net V8.0.11 OS: Unix 4.4.0.19041
Private Key Length: 1793; Public Key Length: 420

Read 1793 bytes; privateKey Length: 1793
rsa KeySize: 3080; rsa alg: RSA; rsa sig alg: http://www.w3.org/2000/09/xmldsig#rsa-sha1
Hello World

david@MyPC:~/test$ ./EncDecTest9
.Net V9.0.0 OS: Unix 4.4.0.19041
Private Key Length: 1793; Public Key Length: 420

Read 1793 bytes; privateKey Length: 1793
rsa KeySize: 3073; rsa alg: RSA; rsa sig alg: http://www.w3.org/2000/09/xmldsig#rsa-sha1
Unhandled exception. System.Security.Cryptography.CryptographicException: The length of the data to decrypt is not valid for the size of this key.
   at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb, Boolean fOAEP)
   at EncDecTest.Program.Decrypt(ReadOnlySpan`1 privateKey, String data)
   at EncDecTest.Program.Main(String[] args)
Aborted (core dumped)

Reproduction Steps

Build the program (for .NET 9) and run it on a Linux platform. You should get the exception noted in the description.
Rebuild the program for .NET8 and run it on a Linux platform. It should work.
Note that the .NET 9 build will work fine on Windows.

Expected behavior

The .NET 9 version should work as the .NET 8 one does.

Actual behavior

.NET 9 version doesn't work.

Regression?

Yes, works fine for .NET8 on Linux platforms.

Known Workarounds

None - other than not to use .NET 9.

Configuration

Works on Linux platforms (x64 & ARM) when built for .NET 8, doesn't when built for .NET 9.

Other information

My presumption is that it's due to something having changed in the underlying Linux libraries between .NET8 & 9 - but that's just my uneducated guess.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions